]>
Commit | Line | Data |
---|---|---|
5ffa42cb TG |
1 | /*** |
2 | This file is part of systemd | |
3 | ||
4 | Copyright 2015 Tom Gundersen | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
20 | #include "bitmap.h" | |
21 | ||
22 | int main(int argc, const char *argv[]) { | |
d5fa8199 | 23 | _cleanup_bitmap_free_ Bitmap *b = NULL, *b2 = NULL; |
cb57dd41 | 24 | Iterator it; |
5ffa42cb TG |
25 | unsigned n = (unsigned) -1, i = 0; |
26 | ||
27 | b = bitmap_new(); | |
28 | assert_se(b); | |
29 | ||
30 | assert_se(bitmap_ensure_allocated(&b) == 0); | |
31 | bitmap_free(b); | |
32 | b = NULL; | |
33 | assert_se(bitmap_ensure_allocated(&b) == 0); | |
34 | ||
35 | assert_se(bitmap_isset(b, 0) == false); | |
36 | assert_se(bitmap_isset(b, 1) == false); | |
37 | assert_se(bitmap_isset(b, 256) == false); | |
38 | assert_se(bitmap_isclear(b) == true); | |
39 | ||
40 | assert_se(bitmap_set(b, 0) == 0); | |
41 | assert_se(bitmap_isset(b, 0) == true); | |
42 | assert_se(bitmap_isclear(b) == false); | |
43 | bitmap_unset(b, 0); | |
44 | assert_se(bitmap_isset(b, 0) == false); | |
45 | assert_se(bitmap_isclear(b) == true); | |
46 | ||
47 | assert_se(bitmap_set(b, 1) == 0); | |
48 | assert_se(bitmap_isset(b, 1) == true); | |
49 | assert_se(bitmap_isclear(b) == false); | |
50 | bitmap_unset(b, 1); | |
51 | assert_se(bitmap_isset(b, 1) == false); | |
52 | assert_se(bitmap_isclear(b) == true); | |
53 | ||
54 | assert_se(bitmap_set(b, 256) == 0); | |
55 | assert_se(bitmap_isset(b, 256) == true); | |
56 | assert_se(bitmap_isclear(b) == false); | |
57 | bitmap_unset(b, 256); | |
58 | assert_se(bitmap_isset(b, 256) == false); | |
59 | assert_se(bitmap_isclear(b) == true); | |
60 | ||
cdf6f5ae TG |
61 | assert_se(bitmap_set(b, 32) == 0); |
62 | bitmap_unset(b, 0); | |
63 | assert_se(bitmap_isset(b, 32) == true); | |
64 | bitmap_unset(b, 32); | |
65 | ||
66 | BITMAP_FOREACH(n, NULL, it) | |
67 | assert_not_reached("NULL bitmap"); | |
68 | ||
5ffa42cb TG |
69 | assert_se(bitmap_set(b, 0) == 0); |
70 | assert_se(bitmap_set(b, 1) == 0); | |
71 | assert_se(bitmap_set(b, 256) == 0); | |
72 | ||
cb57dd41 | 73 | BITMAP_FOREACH(n, b, it) { |
5ffa42cb TG |
74 | assert_se(n == i); |
75 | if (i == 0) | |
76 | i = 1; | |
77 | else if (i == 1) | |
78 | i = 256; | |
79 | else if (i == 256) | |
80 | i = (unsigned) -1; | |
81 | } | |
82 | ||
83 | assert_se(i == (unsigned) -1); | |
84 | ||
85 | i = 0; | |
86 | ||
cb57dd41 | 87 | BITMAP_FOREACH(n, b, it) { |
5ffa42cb TG |
88 | assert_se(n == i); |
89 | if (i == 0) | |
90 | i = 1; | |
91 | else if (i == 1) | |
92 | i = 256; | |
93 | else if (i == 256) | |
94 | i = (unsigned) -1; | |
95 | } | |
96 | ||
97 | assert_se(i == (unsigned) -1); | |
98 | ||
99 | bitmap_clear(b); | |
100 | assert_se(bitmap_isclear(b) == true); | |
101 | ||
102 | assert_se(bitmap_set(b, (unsigned) -1) == -ERANGE); | |
103 | ||
d5fa8199 MM |
104 | bitmap_free(b); |
105 | b = NULL; | |
106 | assert_se(bitmap_ensure_allocated(&b) == 0); | |
107 | assert_se(bitmap_ensure_allocated(&b2) == 0); | |
108 | ||
109 | assert_se(bitmap_equal(b, b2)); | |
110 | assert_se(bitmap_set(b, 0) == 0); | |
111 | bitmap_unset(b, 0); | |
112 | assert_se(bitmap_equal(b, b2)); | |
113 | ||
951c3eef MM |
114 | assert_se(bitmap_set(b, 1) == 0); |
115 | bitmap_clear(b); | |
116 | assert_se(bitmap_equal(b, b2)); | |
117 | ||
118 | assert_se(bitmap_set(b, 0) == 0); | |
119 | assert_se(bitmap_set(b2, 0) == 0); | |
120 | assert_se(bitmap_equal(b, b2)); | |
121 | ||
5ffa42cb TG |
122 | return 0; |
123 | } |