]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-bitmap.c
basic: add a Bitmap implementation
[thirdparty/systemd.git] / src / test / test-bitmap.c
CommitLineData
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
22int main(int argc, const char *argv[]) {
23 _cleanup_bitmap_free_ Bitmap *b = NULL;
24 unsigned n = (unsigned) -1, i = 0;
25
26 b = bitmap_new();
27 assert_se(b);
28
29 assert_se(bitmap_ensure_allocated(&b) == 0);
30 bitmap_free(b);
31 b = NULL;
32 assert_se(bitmap_ensure_allocated(&b) == 0);
33
34 assert_se(bitmap_isset(b, 0) == false);
35 assert_se(bitmap_isset(b, 1) == false);
36 assert_se(bitmap_isset(b, 256) == false);
37 assert_se(bitmap_isclear(b) == true);
38
39 assert_se(bitmap_set(b, 0) == 0);
40 assert_se(bitmap_isset(b, 0) == true);
41 assert_se(bitmap_isclear(b) == false);
42 bitmap_unset(b, 0);
43 assert_se(bitmap_isset(b, 0) == false);
44 assert_se(bitmap_isclear(b) == true);
45
46 assert_se(bitmap_set(b, 1) == 0);
47 assert_se(bitmap_isset(b, 1) == true);
48 assert_se(bitmap_isclear(b) == false);
49 bitmap_unset(b, 1);
50 assert_se(bitmap_isset(b, 1) == false);
51 assert_se(bitmap_isclear(b) == true);
52
53 assert_se(bitmap_set(b, 256) == 0);
54 assert_se(bitmap_isset(b, 256) == true);
55 assert_se(bitmap_isclear(b) == false);
56 bitmap_unset(b, 256);
57 assert_se(bitmap_isset(b, 256) == false);
58 assert_se(bitmap_isclear(b) == true);
59
60 assert_se(bitmap_set(b, 0) == 0);
61 assert_se(bitmap_set(b, 1) == 0);
62 assert_se(bitmap_set(b, 256) == 0);
63
64 BITMAP_FOREACH(n, b) {
65 assert_se(n == i);
66 if (i == 0)
67 i = 1;
68 else if (i == 1)
69 i = 256;
70 else if (i == 256)
71 i = (unsigned) -1;
72 }
73
74 assert_se(i == (unsigned) -1);
75
76 i = 0;
77
78 BITMAP_FOREACH(n, b) {
79 assert_se(n == i);
80 if (i == 0)
81 i = 1;
82 else if (i == 1)
83 i = 256;
84 else if (i == 256)
85 i = (unsigned) -1;
86 }
87
88 assert_se(i == (unsigned) -1);
89
90 bitmap_clear(b);
91 assert_se(bitmap_isclear(b) == true);
92
93 assert_se(bitmap_set(b, (unsigned) -1) == -ERANGE);
94
95 return 0;
96}