summaryrefslogtreecommitdiff
path: root/ccan/bitmap/test/run.c
blob: efd245d7774ef58f81a3d601e64f77744b77884d (plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <ccan/bitmap/bitmap.h>
#include <ccan/tap/tap.h>
#include <ccan/array_size/array_size.h>
#include <ccan/foreach/foreach.h>

#include <ccan/bitmap/bitmap.c>

int bitmap_sizes[] = {
	1, 2, 3, 4, 5, 6, 7, 8,
	16, 17, 24, 32, 33,
	64, 65, 127, 128, 129,
	1023, 1024, 1025,
};
#define NSIZES ARRAY_SIZE(bitmap_sizes)
#define NTESTS 9

static void test_sizes(int nbits, bool dynalloc)
{
	BITMAP_DECLARE(sbitmap, nbits);
	uint32_t marker;
	bitmap *bitmap;
	int i, j;
	bool wrong;

	if (dynalloc) {
		bitmap = bitmap_alloc(nbits);
		ok1(bitmap != NULL);
	} else {
		bitmap = sbitmap;
		marker = 0xdeadbeef;
	}

	bitmap_zero(bitmap, nbits);
	wrong = false;
	for (i = 0; i < nbits; i++) {
		wrong = wrong || bitmap_test_bit(bitmap, i);
	}
	ok1(!wrong);

	bitmap_fill(bitmap, nbits);
	wrong = false;
	for (i = 0; i < nbits; i++) {
		wrong = wrong || !bitmap_test_bit(bitmap, i);
	}
	ok1(!wrong);

	wrong = false;
	for (i = 0; i < nbits; i++) {
		bitmap_zero(bitmap, nbits);
		bitmap_set_bit(bitmap, i);
		for (j = 0; j < nbits; j++) {
			bool val = (i == j);

			wrong = wrong || (bitmap_test_bit(bitmap, j) != val);
		}
	}
	ok1(!wrong);

	wrong = false;
	for (i = 0; i < nbits; i++) {
		bitmap_fill(bitmap, nbits);
		bitmap_clear_bit(bitmap, i);
		for (j = 0; j < nbits; j++) {
			bool val = !(i == j);

			wrong = wrong || (bitmap_test_bit(bitmap, j) != val);
		}
	}
	ok1(!wrong);

	bitmap_zero(bitmap, nbits);
	ok1(bitmap_empty(bitmap, nbits));

	wrong = false;
	for (i = 0; i < nbits; i++) {
		bitmap_zero(bitmap, nbits);
		bitmap_set_bit(bitmap, i);
		wrong = wrong || bitmap_empty(bitmap, nbits);
	}
	ok1(!wrong);

	bitmap_fill(bitmap, nbits);
	ok1(bitmap_full(bitmap, nbits));

	wrong = false;
	for (i = 0; i < nbits; i++) {
		bitmap_fill(bitmap, nbits);
		bitmap_clear_bit(bitmap, i);
		wrong = wrong || bitmap_full(bitmap, nbits);
	}
	ok1(!wrong);
	
	if (dynalloc) {
		free(bitmap);
	} else {
		ok1(marker == 0xdeadbeef);
	}
}

int main(void)
{
	int i;
	bool dynalloc;

	/* This is how many tests you plan to run */
	plan_tests(NSIZES * NTESTS * 2);

	for (i = 0; i < NSIZES; i++) {
		foreach_int(dynalloc, false, true) {
			diag("Testing %d-bit bitmap (%s allocation)",
			     bitmap_sizes[i], dynalloc ? "dynamic" : "static");
			test_sizes(bitmap_sizes[i], dynalloc);
		}
	}

	/* This exits depending on whether all tests passed */
	return exit_status();
}