]> git.ozlabs.org Git - ccan/blob - ccan/bitmap/test/run.c
base64: fix for unsigned chars (e.g. ARM).
[ccan] / ccan / bitmap / test / run.c
1 #include <ccan/bitmap/bitmap.h>
2 #include <ccan/tap/tap.h>
3 #include <ccan/array_size/array_size.h>
4 #include <ccan/foreach/foreach.h>
5
6 #include <ccan/bitmap/bitmap.c>
7
8 int bitmap_sizes[] = {
9         1, 2, 3, 4, 5, 6, 7, 8,
10         16, 17, 24, 32, 33,
11         64, 65, 127, 128, 129,
12         1023, 1024, 1025,
13 };
14 #define NSIZES ARRAY_SIZE(bitmap_sizes)
15 #define NTESTS 9
16
17 static void test_sizes(int nbits, bool dynalloc)
18 {
19         BITMAP_DECLARE(sbitmap, nbits);
20         uint32_t marker;
21         bitmap *bitmap;
22         int i, j;
23         bool wrong;
24
25         if (dynalloc) {
26                 bitmap = bitmap_alloc(nbits);
27                 ok1(bitmap != NULL);
28         } else {
29                 bitmap = sbitmap;
30                 marker = 0xdeadbeef;
31         }
32
33         bitmap_zero(bitmap, nbits);
34         wrong = false;
35         for (i = 0; i < nbits; i++) {
36                 wrong = wrong || bitmap_test_bit(bitmap, i);
37         }
38         ok1(!wrong);
39
40         bitmap_fill(bitmap, nbits);
41         wrong = false;
42         for (i = 0; i < nbits; i++) {
43                 wrong = wrong || !bitmap_test_bit(bitmap, i);
44         }
45         ok1(!wrong);
46
47         wrong = false;
48         for (i = 0; i < nbits; i++) {
49                 bitmap_zero(bitmap, nbits);
50                 bitmap_set_bit(bitmap, i);
51                 for (j = 0; j < nbits; j++) {
52                         bool val = (i == j);
53
54                         wrong = wrong || (bitmap_test_bit(bitmap, j) != val);
55                 }
56         }
57         ok1(!wrong);
58
59         wrong = false;
60         for (i = 0; i < nbits; i++) {
61                 bitmap_fill(bitmap, nbits);
62                 bitmap_clear_bit(bitmap, i);
63                 for (j = 0; j < nbits; j++) {
64                         bool val = !(i == j);
65
66                         wrong = wrong || (bitmap_test_bit(bitmap, j) != val);
67                 }
68         }
69         ok1(!wrong);
70
71         bitmap_zero(bitmap, nbits);
72         ok1(bitmap_empty(bitmap, nbits));
73
74         wrong = false;
75         for (i = 0; i < nbits; i++) {
76                 bitmap_zero(bitmap, nbits);
77                 bitmap_set_bit(bitmap, i);
78                 wrong = wrong || bitmap_empty(bitmap, nbits);
79         }
80         ok1(!wrong);
81
82         bitmap_fill(bitmap, nbits);
83         ok1(bitmap_full(bitmap, nbits));
84
85         wrong = false;
86         for (i = 0; i < nbits; i++) {
87                 bitmap_fill(bitmap, nbits);
88                 bitmap_clear_bit(bitmap, i);
89                 wrong = wrong || bitmap_full(bitmap, nbits);
90         }
91         ok1(!wrong);
92         
93         if (dynalloc) {
94                 free(bitmap);
95         } else {
96                 ok1(marker == 0xdeadbeef);
97         }
98 }
99
100 int main(void)
101 {
102         int i;
103         bool dynalloc;
104
105         /* This is how many tests you plan to run */
106         plan_tests(NSIZES * NTESTS * 2);
107
108         for (i = 0; i < NSIZES; i++) {
109                 foreach_int(dynalloc, false, true) {
110                         diag("Testing %d-bit bitmap (%s allocation)",
111                              bitmap_sizes[i], dynalloc ? "dynamic" : "static");
112                         test_sizes(bitmap_sizes[i], dynalloc);
113                 }
114         }
115
116         /* This exits depending on whether all tests passed */
117         return exit_status();
118 }