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