]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-bitmap.c
Merge pull request #604 from heftig/master
[thirdparty/systemd.git] / src / test / test-bitmap.c
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[]) {
23 _cleanup_bitmap_free_ Bitmap *b = NULL;
24 Iterator it;
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
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
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
73 BITMAP_FOREACH(n, b, it) {
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
87 BITMAP_FOREACH(n, b, it) {
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
104 return 0;
105 }