@@ -14,7 +14,7 @@ int compute_cardinality(bitset_t *b) {
14
14
return k ;
15
15
}
16
16
17
- void test_iterate ( ) {
17
+ DEFINE_TEST ( test_iterate ) {
18
18
bitset_t * b = bitset_create ();
19
19
for (int k = 0 ; k < 1000 ; ++ k ) bitset_set (b , 3 * k );
20
20
assert_true (bitset_count (b ) == 1000 );
@@ -28,6 +28,34 @@ void test_iterate() {
28
28
bitset_free (b );
29
29
}
30
30
31
+ DEFINE_TEST (test_next_bits_iterate ) {
32
+ bitset_t * b = bitset_create ();
33
+ for (int i = 0 ; i < 100 ; i ++ ) bitset_set (b , i );
34
+ for (int i = 1000 ; i < 1100 ; i += 2 ) bitset_set (b , i );
35
+
36
+ // Use an odd, small buffer size
37
+ size_t buffer [3 ];
38
+ size_t howmany = 0 ;
39
+ size_t i = 0 ;
40
+ for (size_t startfrom = 0 ;
41
+ (howmany = bitset_next_set_bits (
42
+ b , buffer , sizeof (buffer ) / sizeof (buffer [0 ]), & startfrom )) > 0 ;
43
+ startfrom ++ ) {
44
+ for (size_t j = 0 ; j < howmany ; j ++ ) {
45
+ size_t expected ;
46
+ if (i < 100 ) {
47
+ expected = i ;
48
+ } else {
49
+ expected = 1000 + 2 * (i - 100 );
50
+ }
51
+ assert_int_equal (buffer [j ], expected );
52
+ ++ i ;
53
+ }
54
+ }
55
+ assert_int_equal (i , 150 );
56
+ bitset_free (b );
57
+ }
58
+
31
59
bool increment (size_t value , void * param ) {
32
60
size_t k ;
33
61
memcpy (& k , param , sizeof (size_t ));
@@ -37,7 +65,7 @@ bool increment(size_t value, void *param) {
37
65
return true;
38
66
}
39
67
40
- void test_iterate2 ( ) {
68
+ DEFINE_TEST ( test_iterate2 ) {
41
69
bitset_t * b = bitset_create ();
42
70
for (int k = 0 ; k < 1000 ; ++ k ) bitset_set (b , 3 * k );
43
71
assert_true (compute_cardinality (b ) == 1000 );
@@ -48,7 +76,7 @@ void test_iterate2() {
48
76
bitset_free (b );
49
77
}
50
78
51
- void test_construct ( ) {
79
+ DEFINE_TEST ( test_construct ) {
52
80
bitset_t * b = bitset_create ();
53
81
for (int k = 0 ; k < 1000 ; ++ k ) bitset_set (b , 3 * k );
54
82
assert_true (compute_cardinality (b ) == 1000 );
@@ -58,7 +86,7 @@ void test_construct() {
58
86
bitset_free (b );
59
87
}
60
88
61
- void test_max_min ( ) {
89
+ DEFINE_TEST ( test_max_min ) {
62
90
bitset_t * b = bitset_create ();
63
91
assert_true (bitset_empty (b ));
64
92
for (size_t k = 100 ; k < 1000 ; ++ k ) {
@@ -69,7 +97,7 @@ void test_max_min() {
69
97
bitset_free (b );
70
98
}
71
99
72
- void test_shift_left ( ) {
100
+ DEFINE_TEST ( test_shift_left ) {
73
101
for (size_t sh = 0 ; sh < 256 ; sh ++ ) {
74
102
bitset_t * b = bitset_create ();
75
103
int power = 3 ;
@@ -90,7 +118,7 @@ void test_shift_left() {
90
118
}
91
119
}
92
120
93
- void test_set_to_val ( ) {
121
+ DEFINE_TEST ( test_set_to_val ) {
94
122
bitset_t * b = bitset_create ();
95
123
bitset_set_to_value (b , 1 , true);
96
124
bitset_set_to_value (b , 1 , false);
@@ -101,7 +129,7 @@ void test_set_to_val() {
101
129
bitset_free (b );
102
130
}
103
131
104
- void test_shift_right ( ) {
132
+ DEFINE_TEST ( test_shift_right ) {
105
133
for (size_t sh = 0 ; sh < 256 ; sh ++ ) {
106
134
bitset_t * b = bitset_create ();
107
135
int power = 3 ;
@@ -120,7 +148,7 @@ void test_shift_right() {
120
148
}
121
149
}
122
150
123
- void test_union_intersection ( ) {
151
+ DEFINE_TEST ( test_union_intersection ) {
124
152
bitset_t * b1 = bitset_create ();
125
153
bitset_t * b2 = bitset_create ();
126
154
@@ -148,7 +176,7 @@ void test_union_intersection() {
148
176
bitset_free (b2 );
149
177
}
150
178
151
- void test_counts ( ) {
179
+ DEFINE_TEST ( test_counts ) {
152
180
bitset_t * b1 = bitset_create ();
153
181
bitset_t * b2 = bitset_create ();
154
182
@@ -165,7 +193,7 @@ void test_counts() {
165
193
/* Creates 2 bitsets, one containing even numbers the other odds.
166
194
Checks bitsets_disjoint() returns that they are disjoint, then sets a common
167
195
bit between both sets and checks that they are no longer disjoint. */
168
- void test_disjoint ( ) {
196
+ DEFINE_TEST ( test_disjoint ) {
169
197
bitset_t * evens = bitset_create ();
170
198
bitset_t * odds = bitset_create ();
171
199
@@ -190,7 +218,7 @@ void test_disjoint() {
190
218
/* Creates 2 bitsets, one containing even numbers the other odds.
191
219
Checks that bitsets_intersect() returns that they do not intersect, then sets
192
220
a common bit and checks that they now intersect. */
193
- void test_intersects ( ) {
221
+ DEFINE_TEST ( test_intersects ) {
194
222
bitset_t * evens = bitset_create ();
195
223
bitset_t * odds = bitset_create ();
196
224
@@ -215,7 +243,7 @@ void test_intersects() {
215
243
contains the subset bits plus additional bits after the subset arraysize.
216
244
Checks that the bitset_contains_all() returns false when checking if
217
245
the superset contains all the subset bits, and true in the opposite case. */
218
- void test_contains_all_different_sizes ( ) {
246
+ DEFINE_TEST ( test_contains_all_different_sizes ) {
219
247
const size_t superset_size = 10 ;
220
248
const size_t subset_size = 5 ;
221
249
@@ -240,7 +268,7 @@ void test_contains_all_different_sizes() {
240
268
even bits set in the same range. Checks that the bitset_contains_all()
241
269
returns true, then sets a single bit at 1001 in the prior subset and checks that
242
270
bitset_contains_all() returns false. */
243
- void test_contains_all ( ) {
271
+ DEFINE_TEST ( test_contains_all ) {
244
272
bitset_t * superset = bitset_create ();
245
273
bitset_t * subset = bitset_create ();
246
274
@@ -262,18 +290,22 @@ void test_contains_all() {
262
290
}
263
291
264
292
int main () {
265
- test_set_to_val ();
266
- test_construct ();
267
- test_union_intersection ();
268
- test_iterate ();
269
- test_iterate2 ();
270
- test_max_min ();
271
- test_counts ();
272
- test_shift_right ();
273
- test_shift_left ();
274
- test_disjoint ();
275
- test_intersects ();
276
- test_contains_all ();
277
- test_contains_all_different_sizes ();
278
- printf ("All asserts passed. Code is probably ok.\n" );
293
+ const struct CMUnitTest tests [] = {
294
+ cmocka_unit_test (test_set_to_val ),
295
+ cmocka_unit_test (test_construct ),
296
+ cmocka_unit_test (test_union_intersection ),
297
+ cmocka_unit_test (test_iterate ),
298
+ cmocka_unit_test (test_iterate2 ),
299
+ cmocka_unit_test (test_next_bits_iterate ),
300
+ cmocka_unit_test (test_max_min ),
301
+ cmocka_unit_test (test_counts ),
302
+ cmocka_unit_test (test_shift_right ),
303
+ cmocka_unit_test (test_shift_left ),
304
+ cmocka_unit_test (test_disjoint ),
305
+ cmocka_unit_test (test_intersects ),
306
+ cmocka_unit_test (test_contains_all ),
307
+ cmocka_unit_test (test_contains_all_different_sizes ),
308
+ };
309
+
310
+ return cmocka_run_group_tests (tests , NULL , NULL );
279
311
}
0 commit comments