]> git.ozlabs.org Git - ccan/blob - ccan/bitmap/test/run-alloc.c
bitmap: Extend allocation functions
[ccan] / ccan / bitmap / test / run-alloc.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_BASE 4
16 #define NTESTS_REALLOC 10
17
18 static void test_basic_alloc(int nbits)
19 {
20         bitmap *bitmap;
21
22         bitmap = bitmap_alloc0(nbits);
23         ok1(bitmap != NULL);
24         ok1(bitmap_empty(bitmap, nbits));
25
26         free(bitmap);
27
28         bitmap = bitmap_alloc1(nbits);
29         ok1(bitmap != NULL);
30         ok1(bitmap_full(bitmap, nbits));
31
32         free(bitmap);
33 }
34
35 static void test_realloc(int obits, int nbits)
36 {
37         bitmap *bitmap;
38         int i;
39         bool wrong;
40
41         bitmap = bitmap_alloc0(obits);
42         ok1(bitmap != NULL);
43         ok1(bitmap_empty(bitmap, obits));
44
45         bitmap = bitmap_realloc1(bitmap, obits, nbits);
46         ok1(bitmap != NULL);
47         if (obits < nbits)
48                 ok1(bitmap_empty(bitmap, obits));
49         else
50                 ok1(bitmap_empty(bitmap, nbits));
51
52         wrong = false;
53         for (i = obits; i < nbits; i++)
54                 wrong = wrong || !bitmap_test_bit(bitmap, i);
55         ok1(!wrong);
56
57         free(bitmap);
58
59         bitmap = bitmap_alloc1(obits);
60         ok1(bitmap != NULL);
61         ok1(bitmap_full(bitmap, obits));
62
63         bitmap = bitmap_realloc0(bitmap, obits, nbits);
64         ok1(bitmap != NULL);
65         if (obits < nbits)
66                 ok1(bitmap_full(bitmap, obits));
67         else
68                 ok1(bitmap_full(bitmap, nbits));
69
70         wrong = false;
71         for (i = obits; i < nbits; i++)
72                 wrong = wrong || bitmap_test_bit(bitmap, i);
73         ok1(!wrong);
74
75         free(bitmap);
76 }
77
78 int main(void)
79 {
80         int i, j;
81
82         /* This is how many tests you plan to run */
83         plan_tests(NSIZES * NTESTS_BASE + NSIZES * NSIZES * NTESTS_REALLOC);
84
85         for (i = 0; i < NSIZES; i++) {
86                 diag("Testing %d-bit bitmap", bitmap_sizes[i]);
87                 test_basic_alloc(bitmap_sizes[i]);
88         }
89
90         for (i = 0; i < NSIZES; i++) {
91                 for (j = 0; j < NSIZES; j++) {
92                         diag("Testing %d-bit => %d-bit bitmap",
93                              bitmap_sizes[i], bitmap_sizes[j]);
94
95                         test_realloc(bitmap_sizes[i], bitmap_sizes[j]);
96                 }
97         }
98
99         /* This exits depending on whether all tests passed */
100         return exit_status();
101 }