aboutsummaryrefslogtreecommitdiffstats
path: root/libnormalform_ref.c
blob: 6d703ebd65d611d229afdbc70c3071785f88e5bb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST


LIBNORMALFORM_SENTENCE *
(libnormalform_ref)(LIBNORMALFORM_SENTENCE *this)
{
	if (this) {
		if (this->refcount == SIZE_MAX) {
			errno = ENOMEM;
			return NULL;
		}
		this->refcount += 1;
	}
	return this;
}


#else


int
main(void)
{
	TEST_BEGIN;

	LIBNORMALFORM_SENTENCE *a;

	errno = 0;
	ASSERT(!libnormalform_ref(NULL) && errno == 0);
	errno = 1;
	ASSERT(!libnormalform_ref(NULL) && errno == 1);

	ASSUME(a = libnormalform_true());

	a->refcount = SIZE_MAX;
	errno = 0;
	ASSERT(!libnormalform_ref(a) && errno == ENOMEM);
	a->refcount -= 1;
	ASSERT(libnormalform_ref(a) == a);
	ASSERT(a->refcount == SIZE_MAX);

	a->refcount = 1;
	ASSERT(libnormalform_ref(a) == a);
	ASSERT(a->refcount == 2);
	ASSERT(libnormalform_ref(a) == a);
	ASSERT(a->refcount == 3);

	a->refcount = 1;
	libnormalform_free(a);

	TEST_END;
}


#endif