summaryrefslogtreecommitdiffstats
path: root/src/test/test-bitmap.c
blob: b91b2935bb8605cd9f00146a994a93c7466713ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include "bitmap.h"
#include "tests.h"

int main(int argc, const char *argv[]) {
        _cleanup_bitmap_free_ Bitmap *b = NULL, *b2 = NULL;
        unsigned n = UINT_MAX, i = 0;

        test_setup_logging(LOG_DEBUG);

        b = bitmap_new();
        assert_se(b);

        ASSERT_EQ(bitmap_ensure_allocated(&b), 0);
        b = bitmap_free(b);
        ASSERT_EQ(bitmap_ensure_allocated(&b), 0);

        ASSERT_FALSE(bitmap_isset(b, 0));
        ASSERT_FALSE(bitmap_isset(b, 1));
        ASSERT_FALSE(bitmap_isset(b, 256));
        ASSERT_TRUE(bitmap_isclear(b));

        ASSERT_EQ(bitmap_set(b, 0), 0);
        ASSERT_TRUE(bitmap_isset(b, 0));
        ASSERT_FALSE(bitmap_isclear(b));
        bitmap_unset(b, 0);
        ASSERT_FALSE(bitmap_isset(b, 0));
        ASSERT_TRUE(bitmap_isclear(b));

        ASSERT_EQ(bitmap_set(b, 1), 0);
        ASSERT_TRUE(bitmap_isset(b, 1));
        ASSERT_FALSE(bitmap_isclear(b));
        bitmap_unset(b, 1);
        ASSERT_FALSE(bitmap_isset(b, 1));
        ASSERT_TRUE(bitmap_isclear(b));

        ASSERT_EQ(bitmap_set(b, 256), 0);
        ASSERT_TRUE(bitmap_isset(b, 256));
        ASSERT_FALSE(bitmap_isclear(b));
        bitmap_unset(b, 256);
        ASSERT_FALSE(bitmap_isset(b, 256));
        ASSERT_TRUE(bitmap_isclear(b));

        ASSERT_EQ(bitmap_set(b, 32), 0);
        bitmap_unset(b, 0);
        ASSERT_TRUE(bitmap_isset(b, 32));
        bitmap_unset(b, 32);

        BITMAP_FOREACH(n, NULL)
                assert_not_reached();

        ASSERT_EQ(bitmap_set(b, 0), 0);
        ASSERT_EQ(bitmap_set(b, 1), 0);
        ASSERT_EQ(bitmap_set(b, 256), 0);

        BITMAP_FOREACH(n, b) {
                ASSERT_EQ(n, i);
                if (i == 0)
                        i = 1;
                else if (i == 1)
                        i = 256;
                else if (i == 256)
                        i = UINT_MAX;
        }

        ASSERT_EQ(i, UINT_MAX);

        i = 0;

        BITMAP_FOREACH(n, b) {
                ASSERT_EQ(n, i);
                if (i == 0)
                        i = 1;
                else if (i == 1)
                        i = 256;
                else if (i == 256)
                        i = UINT_MAX;
        }

        ASSERT_EQ(i, UINT_MAX);

        b2 = bitmap_copy(b);
        assert_se(b2);
        ASSERT_TRUE(bitmap_equal(b, b2));
        ASSERT_TRUE(bitmap_equal(b, b));
        ASSERT_FALSE(bitmap_equal(b, NULL));
        ASSERT_FALSE(bitmap_equal(NULL, b));
        ASSERT_TRUE(bitmap_equal(NULL, NULL));

        bitmap_clear(b);
        ASSERT_TRUE(bitmap_isclear(b));
        ASSERT_FALSE(bitmap_equal(b, b2));
        b2 = bitmap_free(b2);

        assert_se(bitmap_set(b, UINT_MAX) == -ERANGE);

        b = bitmap_free(b);
        ASSERT_EQ(bitmap_ensure_allocated(&b), 0);
        ASSERT_EQ(bitmap_ensure_allocated(&b2), 0);

        assert_se(bitmap_equal(b, b2));
        ASSERT_EQ(bitmap_set(b, 0), 0);
        bitmap_unset(b, 0);
        assert_se(bitmap_equal(b, b2));

        ASSERT_EQ(bitmap_set(b, 1), 0);
        bitmap_clear(b);
        assert_se(bitmap_equal(b, b2));

        ASSERT_EQ(bitmap_set(b, 0), 0);
        ASSERT_EQ(bitmap_set(b2, 0), 0);
        assert_se(bitmap_equal(b, b2));

        return 0;
}