]> git.proxmox.com Git - systemd.git/blame - src/test/test-bitmap.c
New upstream version 236
[systemd.git] / src / test / test-bitmap.c
CommitLineData
52ad194e 1/* SPDX-License-Identifier: LGPL-2.1+ */
7035cd9e
MP
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[]) {
13d276d0 24 _cleanup_bitmap_free_ Bitmap *b = NULL, *b2 = NULL;
7035cd9e
MP
25 Iterator it;
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
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
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
74 BITMAP_FOREACH(n, b, it) {
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
88 BITMAP_FOREACH(n, b, it) {
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
52ad194e
MB
100 b2 = bitmap_copy(b);
101 assert_se(b2);
102 assert_se(bitmap_equal(b, b2) == true);
103 assert_se(bitmap_equal(b, b) == true);
104 assert_se(bitmap_equal(b, NULL) == false);
105 assert_se(bitmap_equal(NULL, b) == false);
106 assert_se(bitmap_equal(NULL, NULL) == true);
107
7035cd9e
MP
108 bitmap_clear(b);
109 assert_se(bitmap_isclear(b) == true);
52ad194e
MB
110 assert_se(bitmap_equal(b, b2) == false);
111 bitmap_free(b2);
112 b2 = NULL;
7035cd9e
MP
113
114 assert_se(bitmap_set(b, (unsigned) -1) == -ERANGE);
115
13d276d0
MP
116 bitmap_free(b);
117 b = NULL;
118 assert_se(bitmap_ensure_allocated(&b) == 0);
119 assert_se(bitmap_ensure_allocated(&b2) == 0);
120
121 assert_se(bitmap_equal(b, b2));
122 assert_se(bitmap_set(b, 0) == 0);
123 bitmap_unset(b, 0);
124 assert_se(bitmap_equal(b, b2));
125
126 assert_se(bitmap_set(b, 1) == 0);
127 bitmap_clear(b);
128 assert_se(bitmap_equal(b, b2));
129
130 assert_se(bitmap_set(b, 0) == 0);
131 assert_se(bitmap_set(b2, 0) == 0);
132 assert_se(bitmap_equal(b, b2));
133
7035cd9e
MP
134 return 0;
135}