Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */
2 :
3 : #include "bitmap.h"
4 :
5 1 : int main(int argc, const char *argv[]) {
6 1 : _cleanup_bitmap_free_ Bitmap *b = NULL, *b2 = NULL;
7 : Iterator it;
8 1 : unsigned n = (unsigned) -1, i = 0;
9 :
10 1 : b = bitmap_new();
11 1 : assert_se(b);
12 :
13 1 : assert_se(bitmap_ensure_allocated(&b) == 0);
14 1 : bitmap_free(b);
15 1 : b = NULL;
16 1 : assert_se(bitmap_ensure_allocated(&b) == 0);
17 :
18 1 : assert_se(bitmap_isset(b, 0) == false);
19 1 : assert_se(bitmap_isset(b, 1) == false);
20 1 : assert_se(bitmap_isset(b, 256) == false);
21 1 : assert_se(bitmap_isclear(b) == true);
22 :
23 1 : assert_se(bitmap_set(b, 0) == 0);
24 1 : assert_se(bitmap_isset(b, 0) == true);
25 1 : assert_se(bitmap_isclear(b) == false);
26 1 : bitmap_unset(b, 0);
27 1 : assert_se(bitmap_isset(b, 0) == false);
28 1 : assert_se(bitmap_isclear(b) == true);
29 :
30 1 : assert_se(bitmap_set(b, 1) == 0);
31 1 : assert_se(bitmap_isset(b, 1) == true);
32 1 : assert_se(bitmap_isclear(b) == false);
33 1 : bitmap_unset(b, 1);
34 1 : assert_se(bitmap_isset(b, 1) == false);
35 1 : assert_se(bitmap_isclear(b) == true);
36 :
37 1 : assert_se(bitmap_set(b, 256) == 0);
38 1 : assert_se(bitmap_isset(b, 256) == true);
39 1 : assert_se(bitmap_isclear(b) == false);
40 1 : bitmap_unset(b, 256);
41 1 : assert_se(bitmap_isset(b, 256) == false);
42 1 : assert_se(bitmap_isclear(b) == true);
43 :
44 1 : assert_se(bitmap_set(b, 32) == 0);
45 1 : bitmap_unset(b, 0);
46 1 : assert_se(bitmap_isset(b, 32) == true);
47 1 : bitmap_unset(b, 32);
48 :
49 1 : BITMAP_FOREACH(n, NULL, it)
50 0 : assert_not_reached("NULL bitmap");
51 :
52 1 : assert_se(bitmap_set(b, 0) == 0);
53 1 : assert_se(bitmap_set(b, 1) == 0);
54 1 : assert_se(bitmap_set(b, 256) == 0);
55 :
56 4 : BITMAP_FOREACH(n, b, it) {
57 3 : assert_se(n == i);
58 3 : if (i == 0)
59 1 : i = 1;
60 2 : else if (i == 1)
61 1 : i = 256;
62 1 : else if (i == 256)
63 1 : i = (unsigned) -1;
64 : }
65 :
66 1 : assert_se(i == (unsigned) -1);
67 :
68 1 : i = 0;
69 :
70 4 : BITMAP_FOREACH(n, b, it) {
71 3 : assert_se(n == i);
72 3 : if (i == 0)
73 1 : i = 1;
74 2 : else if (i == 1)
75 1 : i = 256;
76 1 : else if (i == 256)
77 1 : i = (unsigned) -1;
78 : }
79 :
80 1 : assert_se(i == (unsigned) -1);
81 :
82 1 : b2 = bitmap_copy(b);
83 1 : assert_se(b2);
84 1 : assert_se(bitmap_equal(b, b2) == true);
85 1 : assert_se(bitmap_equal(b, b) == true);
86 1 : assert_se(bitmap_equal(b, NULL) == false);
87 1 : assert_se(bitmap_equal(NULL, b) == false);
88 1 : assert_se(bitmap_equal(NULL, NULL) == true);
89 :
90 1 : bitmap_clear(b);
91 1 : assert_se(bitmap_isclear(b) == true);
92 1 : assert_se(bitmap_equal(b, b2) == false);
93 1 : bitmap_free(b2);
94 1 : b2 = NULL;
95 :
96 1 : assert_se(bitmap_set(b, (unsigned) -1) == -ERANGE);
97 :
98 1 : bitmap_free(b);
99 1 : b = NULL;
100 1 : assert_se(bitmap_ensure_allocated(&b) == 0);
101 1 : assert_se(bitmap_ensure_allocated(&b2) == 0);
102 :
103 1 : assert_se(bitmap_equal(b, b2));
104 1 : assert_se(bitmap_set(b, 0) == 0);
105 1 : bitmap_unset(b, 0);
106 1 : assert_se(bitmap_equal(b, b2));
107 :
108 1 : assert_se(bitmap_set(b, 1) == 0);
109 1 : bitmap_clear(b);
110 1 : assert_se(bitmap_equal(b, b2));
111 :
112 1 : assert_se(bitmap_set(b, 0) == 0);
113 1 : assert_se(bitmap_set(b2, 0) == 0);
114 1 : assert_se(bitmap_equal(b, b2));
115 :
116 1 : return 0;
117 : }
|