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