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