1 /*
2 * Copyright (c) 2016,2021 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6 #include <errno.h>
7
8 #include <zephyr/kernel.h>
9 #include <zephyr/ztest.h>
10 #include <zephyr/arch/cpu.h>
11
12 #include <zephyr/tc_util.h>
13 #include <zephyr/sys/bitarray.h>
14 #include <zephyr/sys/util.h>
15
16 #ifdef CONFIG_BIG_ENDIAN
17 #define BIT_INDEX(bit) ((3 - ((bit >> 3) & 0x3)) + 4*(bit >> 5))
18 #else
19 #define BIT_INDEX(bit) (bit >> 3)
20 #endif
21 #define BIT_VAL(bit) (1 << (bit & 0x7))
22 #define BITFIELD_SIZE 512
23
24 /**
25 * @addtogroup kernel_common_tests
26 * @{
27 */
28
29 /* Helper function to compare two bitarrays */
cmp_u32_arrays(uint32_t * a1,uint32_t * a2,size_t sz)30 static bool cmp_u32_arrays(uint32_t *a1, uint32_t *a2, size_t sz)
31 {
32 bool are_equal = true;
33 size_t i;
34
35 for (i = 0; i < sz; i++) {
36 if (a1[i] != a2[i]) {
37 are_equal = false;
38 printk("%s: [%zu] 0x%x != 0x%x", __func__,
39 i, a1[i], a2[i]);
40 break;
41 }
42 }
43
44 return are_equal;
45 }
46
47 #define FAIL_ALLOC_MSG_FMT "sys_bitarray_alloc with region size %i allocated incorrectly"
48 #define FAIL_ALLOC_RET_MSG_FMT "sys_bitarray_alloc with region size %i returned incorrect result"
49 #define FAIL_ALLOC_OFFSET_MSG_FMT "sys_bitarray_alloc with region size %i gave incorrect offset"
50 #define FAIL_FREE_MSG_FMT "sys_bitarray_free with region size %i and offset %i failed"
51 #define FREE 0U
52
validate_bitarray_define(sys_bitarray_t * ba,size_t num_bits)53 void validate_bitarray_define(sys_bitarray_t *ba, size_t num_bits)
54 {
55 size_t num_bundles;
56 int i;
57
58 num_bundles = ROUND_UP(ROUND_UP(num_bits, 8) / 8, sizeof(uint32_t))
59 / sizeof(uint32_t);
60
61 zassert_equal(ba->num_bits, num_bits,
62 "SYS_BITARRAY_DEFINE num_bits expected %u, got %u",
63 num_bits, ba->num_bits);
64
65 zassert_equal(ba->num_bundles, num_bundles,
66 "SYS_BITARRAY_DEFINE num_bundles expected %u, got %u",
67 num_bundles, ba->num_bundles);
68
69 for (i = 0; i < num_bundles; i++) {
70 zassert_equal(ba->bundles[i], FREE,
71 "SYS_BITARRAY_DEFINE bundles[%u] not free for num_bits %u",
72 i, num_bits);
73 }
74 }
75
76 /**
77 * @brief Test defining of bitarrays
78 *
79 * @see SYS_BITARRAY_DEFINE()
80 */
ZTEST(bitarray,test_bitarray_declare)81 ZTEST(bitarray, test_bitarray_declare)
82 {
83 SYS_BITARRAY_DEFINE(ba_1_bit, 1);
84 SYS_BITARRAY_DEFINE(ba_32_bit, 32);
85 SYS_BITARRAY_DEFINE(ba_33_bit, 33);
86 SYS_BITARRAY_DEFINE(ba_64_bit, 64);
87 SYS_BITARRAY_DEFINE(ba_65_bit, 65);
88 SYS_BITARRAY_DEFINE(ba_128_bit, 128);
89 SYS_BITARRAY_DEFINE(ba_129_bit, 129);
90
91 /* Test SYS_BITFIELD_DECLARE by asserting that a sufficient number of uint32_t
92 * in the declared array are set as free to represent the number of bits
93 */
94
95 validate_bitarray_define(&ba_1_bit, 1);
96 validate_bitarray_define(&ba_32_bit, 32);
97 validate_bitarray_define(&ba_33_bit, 33);
98 validate_bitarray_define(&ba_64_bit, 64);
99 validate_bitarray_define(&ba_65_bit, 65);
100 validate_bitarray_define(&ba_128_bit, 128);
101 validate_bitarray_define(&ba_129_bit, 129);
102 }
103
bitarray_bundles_is_zero(sys_bitarray_t * ba)104 bool bitarray_bundles_is_zero(sys_bitarray_t *ba)
105 {
106 bool ret = true;
107 unsigned int i;
108
109 for (i = 0; i < ba->num_bundles; i++) {
110 if (ba->bundles[i] != 0) {
111 ret = false;
112 break;
113 }
114 }
115
116 return ret;
117 }
118
119 /**
120 * @brief Test bitarrays set and clear
121 *
122 * @see sys_bitarray_set_bit()
123 * @see sys_bitarray_clear_bit()
124 * @see sys_bitarray_test_bit()
125 * @see sys_bitarray_test_and_set_bit()
126 * @see sys_bitarray_test_and_clear_bit()
127 */
ZTEST(bitarray,test_bitarray_set_clear)128 ZTEST(bitarray, test_bitarray_set_clear)
129 {
130 int ret;
131 int bit_val;
132 size_t bit, bundle_idx, bit_idx_in_bundle;
133
134 /* Bitarrays have embedded spinlocks and can't on the stack. */
135 if (IS_ENABLED(CONFIG_KERNEL_COHERENCE)) {
136 ztest_test_skip();
137 }
138
139 SYS_BITARRAY_DEFINE(ba, 234);
140
141 for (bit = 0U; bit < ba.num_bits; ++bit) {
142 bundle_idx = bit / (sizeof(ba.bundles[0]) * 8);
143 bit_idx_in_bundle = bit % (sizeof(ba.bundles[0]) * 8);
144
145 ret = sys_bitarray_set_bit(&ba, bit);
146 zassert_equal(ret, 0,
147 "sys_bitarray_set_bit failed on bit %d", bit);
148 zassert_equal(ba.bundles[bundle_idx], BIT(bit_idx_in_bundle),
149 "sys_bitarray_set_bit did not set bit %d\n", bit);
150 zassert_not_equal(sys_bitfield_test_bit((mem_addr_t)ba.bundles, bit),
151 0, "sys_bitarray_set_bit did not set bit %d\n", bit);
152
153 ret = sys_bitarray_test_bit(&ba, bit, &bit_val);
154 zassert_equal(ret, 0,
155 "sys_bitarray_test_bit failed at bit %d", bit);
156 zassert_equal(bit_val, 1,
157 "sys_bitarray_test_bit did not detect bit %d\n", bit);
158
159 ret = sys_bitarray_clear_bit(&ba, bit);
160 zassert_equal(ret, 0,
161 "sys_bitarray_clear_bit failed at bit %d", bit);
162 zassert_equal(ba.bundles[bundle_idx], 0,
163 "sys_bitarray_clear_bit did not clear bit %d\n", bit);
164 zassert_equal(sys_bitfield_test_bit((mem_addr_t)ba.bundles, bit),
165 0, "sys_bitarray_set_bit did not set bit %d\n", bit);
166
167 ret = sys_bitarray_test_bit(&ba, bit, &bit_val);
168 zassert_equal(ret, 0,
169 "sys_bitarray_test_bit failed at bit %d", bit);
170 zassert_equal(bit_val, 0,
171 "sys_bitarray_test_bit erroneously detected bit %d\n",
172 bit);
173
174 ret = sys_bitarray_test_and_set_bit(&ba, bit, &bit_val);
175 zassert_equal(ret, 0,
176 "sys_bitarray_test_and_set_bit failed at bit %d", bit);
177 zassert_equal(bit_val, 0,
178 "sys_bitarray_test_and_set_bit erroneously detected bit %d\n",
179 bit);
180 zassert_equal(ba.bundles[bundle_idx], BIT(bit_idx_in_bundle),
181 "sys_bitarray_test_and_set_bit did not set bit %d\n", bit);
182 zassert_not_equal(sys_bitfield_test_bit((mem_addr_t)ba.bundles, bit),
183 0, "sys_bitarray_set_bit did not set bit %d\n", bit);
184
185 ret = sys_bitarray_test_and_set_bit(&ba, bit, &bit_val);
186 zassert_equal(ret, 0,
187 "sys_bitarray_test_and_set_bit failed at bit %d", bit);
188 zassert_equal(bit_val, 1,
189 "sys_bitarray_test_and_set_bit did not detect bit %d\n",
190 bit);
191 zassert_equal(ba.bundles[bundle_idx], BIT(bit_idx_in_bundle),
192 "sys_bitarray_test_and_set_bit cleared bit %d\n", bit);
193 zassert_not_equal(sys_bitfield_test_bit((mem_addr_t)ba.bundles, bit),
194 0, "sys_bitarray_set_bit did not set bit %d\n", bit);
195
196 ret = sys_bitarray_test_and_clear_bit(&ba, bit, &bit_val);
197 zassert_equal(ret, 0,
198 "sys_bitarray_test_and_clear_bit failed at bit %d", bit);
199 zassert_equal(bit_val, 1,
200 "sys_bitarray_test_and_clear_bit did not detect bit %d\n",
201 bit);
202 zassert_equal(ba.bundles[bundle_idx], 0,
203 "sys_bitarray_test_and_clear_bit did not clear bit %d\n",
204 bit);
205 zassert_equal(sys_bitfield_test_bit((mem_addr_t)ba.bundles, bit),
206 0, "sys_bitarray_set_bit did not set bit %d\n", bit);
207
208 ret = sys_bitarray_test_and_clear_bit(&ba, bit, &bit_val);
209 zassert_equal(ret, 0,
210 "sys_bitarray_test_and_clear_bit failed at bit %d", bit);
211 zassert_equal(bit_val, 0,
212 "sys_bitarray_test_and_clear_bit erroneously detected bit %d\n",
213 bit);
214 zassert_equal(ba.bundles[bundle_idx], 0,
215 "sys_bitarray_test_and_clear_bit set bit %d\n",
216 bit);
217 zassert_equal(sys_bitfield_test_bit((mem_addr_t)ba.bundles, bit),
218 0, "sys_bitarray_set_bit did not set bit %d\n", bit);
219 }
220
221 /* All this should fail because we go outside of
222 * total bits in bit array. Also needs to make sure bits
223 * are not changed.
224 */
225 ret = sys_bitarray_set_bit(&ba, ba.num_bits);
226 zassert_not_equal(ret, 0, "sys_bitarray_set_bit() should fail but not");
227 zassert_true(bitarray_bundles_is_zero(&ba),
228 "sys_bitarray_set_bit() erroneously changed bitarray");
229
230 ret = sys_bitarray_clear_bit(&ba, ba.num_bits);
231 zassert_not_equal(ret, 0, "sys_bitarray_clear_bit() should fail but not");
232 zassert_true(bitarray_bundles_is_zero(&ba),
233 "sys_bitarray_clear_bit() erroneously changed bitarray");
234
235 ret = sys_bitarray_test_bit(&ba, ba.num_bits, &bit_val);
236 zassert_not_equal(ret, 0, "sys_bitarray_test_bit() should fail but not");
237 zassert_true(bitarray_bundles_is_zero(&ba),
238 "sys_bitarray_test_bit() erroneously changed bitarray");
239
240 ret = sys_bitarray_test_and_set_bit(&ba, ba.num_bits, &bit_val);
241 zassert_not_equal(ret, 0,
242 "sys_bitarray_test_and_set_bit() should fail but not");
243 zassert_true(bitarray_bundles_is_zero(&ba),
244 "sys_bitarray_test_and_set_bit() erroneously changed bitarray");
245
246 ret = sys_bitarray_test_and_clear_bit(&ba, ba.num_bits, &bit_val);
247 zassert_not_equal(ret, 0,
248 "sys_bitarray_test_and_clear_bit() should fail but not");
249 zassert_true(bitarray_bundles_is_zero(&ba),
250 "sys_bitarray_test_and_clear_bit() erroneously changed bitarray");
251
252 ret = sys_bitarray_set_bit(&ba, -1);
253 zassert_not_equal(ret, 0, "sys_bitarray_set_bit() should fail but not");
254 zassert_true(bitarray_bundles_is_zero(&ba),
255 "sys_bitarray_set_bit() erroneously changed bitarray");
256
257 ret = sys_bitarray_clear_bit(&ba, -1);
258 zassert_not_equal(ret, 0, "sys_bitarray_clear_bit() should fail but not");
259 zassert_true(bitarray_bundles_is_zero(&ba),
260 "sys_bitarray_clear_bit() erroneously changed bitarray");
261
262 ret = sys_bitarray_test_bit(&ba, -1, &bit_val);
263 zassert_not_equal(ret, 0, "sys_bitarray_test_bit() should fail but not");
264 zassert_true(bitarray_bundles_is_zero(&ba),
265 "sys_bitarray_test_bit() erroneously changed bitarray");
266
267 ret = sys_bitarray_test_and_set_bit(&ba, -1, &bit_val);
268 zassert_not_equal(ret, 0,
269 "sys_bitarray_test_and_set_bit() should fail but not");
270 zassert_true(bitarray_bundles_is_zero(&ba),
271 "sys_bitarray_test_and_set_bit() erroneously changed bitarray");
272
273 ret = sys_bitarray_test_and_clear_bit(&ba, -1, &bit_val);
274 zassert_not_equal(ret, 0,
275 "sys_bitarray_test_and_clear_bit() should fail but not");
276 zassert_true(bitarray_bundles_is_zero(&ba),
277 "sys_bitarray_test_and_clear_bit() erroneously changed bitarray");
278 }
279
alloc_and_free_predefined(void)280 void alloc_and_free_predefined(void)
281 {
282 int ret;
283 size_t offset;
284
285 uint32_t ba_128_expected[4];
286
287 SYS_BITARRAY_DEFINE(ba_128, 128);
288
289 printk("Testing bit array alloc and free with predefined patterns\n");
290
291 /* Pre-populate the bits */
292 ba_128.bundles[0] = 0x0F0F070F;
293 ba_128.bundles[1] = 0x0F0F0F0F;
294 ba_128.bundles[2] = 0x0F0F0F0F;
295 ba_128.bundles[3] = 0x0F0F0000;
296
297 /* Expected values */
298 ba_128_expected[0] = 0x0F0FFF0F;
299 ba_128_expected[1] = 0x0F0F0F0F;
300 ba_128_expected[2] = 0x0F0F0F0F;
301 ba_128_expected[3] = 0x0F0F0000;
302
303 ret = sys_bitarray_alloc(&ba_128, 5, &offset);
304 zassert_equal(ret, 0, "sys_bitarray_alloc() failed: %d", ret);
305 zassert_equal(offset, 11, "sys_bitarray_alloc() offset expected %d, got %d", 11, offset);
306 zassert_true(cmp_u32_arrays(ba_128.bundles, ba_128_expected, ba_128.num_bundles),
307 "sys_bitarray_alloc() failed bits comparison");
308
309 ret = sys_bitarray_alloc(&ba_128, 16, &offset);
310 ba_128_expected[2] = 0xFF0F0F0F;
311 ba_128_expected[3] = 0x0F0F0FFF;
312 zassert_equal(ret, 0, "sys_bitarray_alloc() failed: %d", ret);
313 zassert_equal(offset, 92, "sys_bitarray_alloc() offset expected %d, got %d", 92, offset);
314 zassert_true(cmp_u32_arrays(ba_128.bundles, ba_128_expected, ba_128.num_bundles),
315 "sys_bitarray_alloc() failed bits comparison");
316
317 ret = sys_bitarray_free(&ba_128, 5, 11);
318 ba_128_expected[0] = 0x0F0F070F;
319 zassert_equal(ret, 0, "sys_bitarray_free() failed: %d", ret);
320 zassert_true(cmp_u32_arrays(ba_128.bundles, ba_128_expected, ba_128.num_bundles),
321 "sys_bitarray_free() failed bits comparison");
322
323 ret = sys_bitarray_free(&ba_128, 5, 0);
324 zassert_not_equal(ret, 0, "sys_bitarray_free() should fail but not");
325 zassert_true(cmp_u32_arrays(ba_128.bundles, ba_128_expected, ba_128.num_bundles),
326 "sys_bitarray_free() failed bits comparison");
327
328 ret = sys_bitarray_free(&ba_128, 24, 92);
329 zassert_not_equal(ret, 0, "sys_bitarray_free() should fail but not");
330 zassert_true(cmp_u32_arrays(ba_128.bundles, ba_128_expected, ba_128.num_bundles),
331 "sys_bitarray_free() failed bits comparison");
332
333 ret = sys_bitarray_free(&ba_128, 16, 92);
334 ba_128_expected[2] = 0x0F0F0F0F;
335 ba_128_expected[3] = 0x0F0F0000;
336 zassert_equal(ret, 0, "sys_bitarray_free() failed: %d", ret);
337 zassert_true(cmp_u32_arrays(ba_128.bundles, ba_128_expected, ba_128.num_bundles),
338 "sys_bitarray_free() failed bits comparison");
339
340 /* test in-between bundles */
341 ba_128.bundles[0] = 0x7FFFFFFF;
342 ba_128.bundles[1] = 0xFFFFFFFF;
343 ba_128.bundles[2] = 0x00000000;
344 ba_128.bundles[3] = 0x00000000;
345
346 ba_128_expected[0] = 0x7FFFFFFF;
347 ba_128_expected[1] = 0xFFFFFFFF;
348 ba_128_expected[2] = 0xFFFFFFFF;
349 ba_128_expected[3] = 0x00000003;
350
351 ret = sys_bitarray_alloc(&ba_128, 34, &offset);
352 zassert_equal(ret, 0, "sys_bitarray_alloc() failed: %d", ret);
353 zassert_equal(offset, 64, "sys_bitarray_alloc() offset expected %d, got %d", 64, offset);
354 zassert_true(cmp_u32_arrays(ba_128.bundles, ba_128_expected, ba_128.num_bundles),
355 "sys_bitarray_alloc() failed bits comparison");
356 }
357
count_bits(uint32_t val)358 static inline size_t count_bits(uint32_t val)
359 {
360 /* Implements Brian Kernighan’s Algorithm
361 * to count bits.
362 */
363
364 size_t cnt = 0;
365
366 while (val != 0) {
367 val = val & (val - 1);
368 cnt++;
369 }
370
371 return cnt;
372 }
373
get_bitarray_popcnt(sys_bitarray_t * ba)374 size_t get_bitarray_popcnt(sys_bitarray_t *ba)
375 {
376 size_t popcnt = 0;
377 unsigned int idx;
378
379 for (idx = 0; idx < ba->num_bundles; idx++) {
380 popcnt += count_bits(ba->bundles[idx]);
381 }
382
383 return popcnt;
384 }
385
alloc_and_free_loop(int divisor)386 void alloc_and_free_loop(int divisor)
387 {
388 int ret;
389 size_t offset;
390 size_t bit;
391 size_t num_bits;
392 size_t cur_popcnt;
393 size_t expected_popcnt = 0;
394
395 SYS_BITARRAY_DEFINE(ba, 234);
396
397 printk("Testing bit array alloc and free with divisor %d\n", divisor);
398
399 for (bit = 0U; bit < ba.num_bits; ++bit) {
400 cur_popcnt = get_bitarray_popcnt(&ba);
401 zassert_equal(cur_popcnt, expected_popcnt,
402 "bit count expected %u, got %u (at bit %u)",
403 expected_popcnt, cur_popcnt, bit);
404
405 /* Allocate half of remaining bits */
406 num_bits = (ba.num_bits - bit) / divisor;
407
408 ret = sys_bitarray_alloc(&ba, num_bits, &offset);
409 if (num_bits == 0) {
410 zassert_not_equal(ret, 0,
411 "sys_bitarray_free() should fail but not (bit %u)",
412 bit);
413 } else {
414 zassert_equal(ret, 0,
415 "sys_bitarray_alloc() failed (%d) at bit %u",
416 ret, bit);
417 zassert_equal(offset, bit,
418 "sys_bitarray_alloc() offset expected %d, got %d",
419 bit, offset);
420
421 expected_popcnt += num_bits;
422 }
423
424 cur_popcnt = get_bitarray_popcnt(&ba);
425 zassert_equal(cur_popcnt, expected_popcnt,
426 "bit count expected %u, got %u (at bit %u)",
427 expected_popcnt, cur_popcnt, bit);
428
429 /* Free all but the first bit of allocated region */
430 ret = sys_bitarray_free(&ba, (num_bits - 1), (bit + 1));
431 if ((num_bits == 0) || ((num_bits - 1) == 0)) {
432 zassert_not_equal(ret, 0,
433 "sys_bitarray_free() should fail but not (bit %u)",
434 bit);
435 } else {
436 zassert_equal(ret, 0,
437 "sys_bitarray_free() failed (%d) at bit %u",
438 ret, (bit + 1));
439
440 expected_popcnt -= num_bits - 1;
441 }
442 }
443 }
444
alloc_and_free_interval(void)445 void alloc_and_free_interval(void)
446 {
447 int ret;
448 size_t cnt;
449 size_t offset;
450 size_t expected_offset;
451 size_t expected_popcnt, cur_popcnt;
452
453 /* Make sure number of bits is multiple of 8 */
454 SYS_BITARRAY_DEFINE(ba, 152);
455
456 printk("Testing bit array interval alloc and free\n");
457
458 /* Pre-populate the bits so that 4-bit already allocated,
459 * then 4 free bits, and repeat.
460 */
461 for (cnt = 0; cnt < ba.num_bundles; cnt++) {
462 ba.bundles[cnt] = 0x0F0F0F0F;
463 }
464
465 expected_offset = 4;
466 expected_popcnt = get_bitarray_popcnt(&ba);
467 for (cnt = 0; cnt <= (ba.num_bits / 8); cnt++) {
468
469 ret = sys_bitarray_alloc(&ba, 4, &offset);
470 if (cnt == (ba.num_bits / 8)) {
471 zassert_not_equal(ret, 0,
472 "sys_bitarray_free() should fail but not (cnt %u)",
473 cnt);
474 } else {
475 zassert_equal(ret, 0,
476 "sys_bitarray_alloc() failed (%d) (cnt %u)",
477 ret, cnt);
478
479 zassert_equal(offset, expected_offset,
480 "offset expected %u, got %u (cnt %u)",
481 expected_offset, offset, cnt);
482
483 expected_popcnt += 4;
484
485 cur_popcnt = get_bitarray_popcnt(&ba);
486 zassert_equal(cur_popcnt, expected_popcnt,
487 "bit count expected %u, got %u (cnt %u)",
488 expected_popcnt, cur_popcnt, cnt);
489
490
491 expected_offset += 8;
492 }
493 }
494 }
495
496 /**
497 * @brief Test bitarrays allocation and free
498 *
499 * @see sys_bitarray_alloc()
500 * @see sys_bitarray_free()
501 */
ZTEST(bitarray,test_bitarray_alloc_free)502 ZTEST(bitarray, test_bitarray_alloc_free)
503 {
504 int i;
505
506 /* Bitarrays have embedded spinlocks and can't on the stack. */
507 if (IS_ENABLED(CONFIG_KERNEL_COHERENCE)) {
508 ztest_test_skip();
509 }
510
511 alloc_and_free_predefined();
512
513 i = 1;
514 while (i < 65) {
515 alloc_and_free_loop(i);
516
517 i *= 2;
518 }
519
520 alloc_and_free_interval();
521 }
522
ZTEST(bitarray,test_bitarray_popcount_region)523 ZTEST(bitarray, test_bitarray_popcount_region)
524 {
525 int ret;
526 size_t count;
527
528 /* Bitarrays have embedded spinlocks and can't on the stack. */
529 if (IS_ENABLED(CONFIG_KERNEL_COHERENCE)) {
530 ztest_test_skip();
531 }
532
533 SYS_BITARRAY_DEFINE(ba, 128);
534
535 printk("Testing bit array region popcount spanning single bundle\n");
536
537 /* Pre-populate the bits */
538 ba.bundles[0] = 0x00000005;
539 ba.bundles[1] = 0x00000000;
540 ba.bundles[2] = 0x00000000;
541 ba.bundles[3] = 0x00000000;
542
543 ret = sys_bitarray_popcount_region(&ba, 1, 0, &count);
544 zassert_equal(ret, 0, "sys_bitarray_popcount_region() returned unexpected value: %d", ret);
545 zassert_equal(count, 1, "sys_bitarray_popcount_region() returned unexpected count: %d",
546 count);
547
548 ret = sys_bitarray_popcount_region(&ba, 1, 1, &count);
549 zassert_equal(ret, 0, "sys_bitarray_popcount_region() returned unexpected value: %d", ret);
550 zassert_equal(count, 0, "sys_bitarray_popcount_region() returned unexpected count: %d",
551 count);
552
553 ret = sys_bitarray_popcount_region(&ba, 2, 0, &count);
554 zassert_equal(ret, 0, "sys_bitarray_popcount_region() returned unexpected value: %d", ret);
555 zassert_equal(count, 1, "sys_bitarray_popcount_region() returned unexpected count: %d",
556 count);
557
558 ret = sys_bitarray_popcount_region(&ba, 3, 0, &count);
559 zassert_equal(ret, 0, "sys_bitarray_popcount_region() returned unexpected value: %d", ret);
560 zassert_equal(count, 2, "sys_bitarray_popcount_region() returned unexpected count: %d",
561 count);
562
563 ret = sys_bitarray_popcount_region(&ba, 3, 1, &count);
564 zassert_equal(ret, 0, "sys_bitarray_popcount_region() returned unexpected value: %d", ret);
565 zassert_equal(count, 1, "sys_bitarray_popcount_region() returned unexpected count: %d",
566 count);
567
568 printk("Testing bit array region popcount spanning multiple bundles\n");
569
570 /* Pre-populate the bits.
571 * First and last bit of bitarray are set
572 */
573 ba.bundles[0] = 0x00000001;
574 ba.bundles[1] = 0x00000000;
575 ba.bundles[2] = 0x00000000;
576 ba.bundles[3] = 0x80000000;
577
578 ret = sys_bitarray_popcount_region(&ba, 126, 1, &count);
579 zassert_equal(ret, 0, "sys_bitarray_popcount_region() returned unexpected value: %d", ret);
580 zassert_equal(count, 0, "sys_bitarray_popcount_region() returned unexpected count: %d",
581 count);
582
583 ret = sys_bitarray_popcount_region(&ba, 126, 0, &count);
584 zassert_equal(ret, 0, "sys_bitarray_popcount_region() returned unexpected value: %d", ret);
585 zassert_equal(count, 1, "sys_bitarray_popcount_region() returned unexpected count: %d",
586 count);
587
588 ret = sys_bitarray_popcount_region(&ba, 127, 1, &count);
589 zassert_equal(ret, 0, "sys_bitarray_popcount_region() returned unexpected value: %d", ret);
590 zassert_equal(count, 1, "sys_bitarray_popcount_region() returned unexpected count: %d",
591 count);
592 ret = sys_bitarray_popcount_region(&ba, 1, 127, &count);
593 zassert_equal(ret, 0, "sys_bitarray_popcount_region() returned unexpected value: %d", ret);
594 zassert_equal(count, 1, "sys_bitarray_popcount_region() returned unexpected count: %d",
595 count);
596
597 ret = sys_bitarray_popcount_region(&ba, 128, 0, &count);
598 zassert_equal(ret, 0, "sys_bitarray_popcount_region() returned unexpected value: %d", ret);
599 zassert_equal(count, 2, "sys_bitarray_popcount_region() returned unexpected count: %d",
600 count);
601
602 printk("Testing edge/error cases\n");
603 ret = sys_bitarray_popcount_region(&ba, 0, 0, &count);
604 zassert_equal(ret, -EINVAL, "sys_bitarray_popcount_region() returned unexpected value: %d",
605 ret);
606 ret = sys_bitarray_popcount_region(&ba, 0, 128, &count);
607 zassert_equal(ret, -EINVAL, "sys_bitarray_popcount_region() returned unexpected value: %d",
608 ret);
609 ret = sys_bitarray_popcount_region(&ba, 128, 0, &count);
610 zassert_equal(ret, 0, "sys_bitarray_popcount_region() returned unexpected value: %d", ret);
611
612 ret = sys_bitarray_popcount_region(&ba, 128, 1, &count);
613 zassert_equal(ret, -EINVAL, "sys_bitarray_popcount_region() returned unexpected value: %d",
614 ret);
615 ret = sys_bitarray_popcount_region(&ba, 129, 0, &count);
616 zassert_equal(ret, -EINVAL, "sys_bitarray_popcount_region() returned unexpected value: %d",
617 ret);
618 }
619
ZTEST(bitarray,test_bitarray_xor)620 ZTEST(bitarray, test_bitarray_xor)
621 {
622 int ret;
623
624 /* Bitarrays have embedded spinlocks and can't on the stack. */
625 if (IS_ENABLED(CONFIG_KERNEL_COHERENCE)) {
626 ztest_test_skip();
627 }
628
629 SYS_BITARRAY_DEFINE(ba, 128);
630 SYS_BITARRAY_DEFINE(bb, 128);
631 SYS_BITARRAY_DEFINE(bc, 129);
632
633 printk("Testing bit array region xor spanning single bundle\n");
634
635 /* Pre-populate the bits */
636 ba.bundles[0] = 0x80001001;
637 ba.bundles[1] = 0x10000008;
638 ba.bundles[2] = 0xFFFFFFFF;
639 ba.bundles[3] = 0x00000000;
640
641 bb.bundles[0] = 0x80010001;
642 bb.bundles[1] = 0x10000008;
643 bb.bundles[2] = 0xFFFFFFFF;
644 bb.bundles[3] = 0x00000000;
645
646 ret = sys_bitarray_xor(&ba, &bb, 32, 0);
647 zassert_equal(ret, 0, "sys_bitarray_xor() returned unexpected value: %d", ret);
648 zassert_equal(ba.bundles[0], 0x00011000, "sys_bitarray_xor() result unexpected: %x",
649 ba.bundles[0]);
650 zassert_equal(bb.bundles[0], 0x80010001, "sys_bitarray_xor() result unexpected: %x",
651 bb.bundles[0]);
652
653 zassert_equal(ba.bundles[1], 0x10000008, "sys_bitarray_xor() result unexpected: %x",
654 ba.bundles[1]);
655 zassert_equal(bb.bundles[1], 0x10000008, "sys_bitarray_xor() result unexpected: %x",
656 bb.bundles[1]);
657
658 zassert_equal(ba.bundles[2], 0xFFFFFFFF, "sys_bitarray_xor() result unexpected: %x",
659 ba.bundles[2]);
660 zassert_equal(bb.bundles[2], 0xFFFFFFFF, "sys_bitarray_xor() result unexpected: %x",
661 bb.bundles[2]);
662
663 zassert_equal(ba.bundles[3], 0x00000000, "sys_bitarray_xor() result unexpected: %x",
664 ba.bundles[3]);
665 zassert_equal(bb.bundles[3], 0x00000000, "sys_bitarray_xor() result unexpected: %x",
666 bb.bundles[3]);
667
668 /* Pre-populate the bits */
669 ba.bundles[0] = 0x80001001;
670 ba.bundles[1] = 0x10000008;
671 ba.bundles[2] = 0xFFFFFFFF;
672 ba.bundles[3] = 0x00000000;
673
674 bb.bundles[0] = 0x80010001;
675 bb.bundles[1] = 0x10000008;
676 bb.bundles[2] = 0xFFFFFFFF;
677 bb.bundles[3] = 0x00000000;
678
679 ret = sys_bitarray_xor(&ba, &bb, 16, 0);
680 zassert_equal(ret, 0, "sys_bitarray_xor() returned unexpected value: %d", ret);
681 zassert_equal(ba.bundles[0], 0x80001000, "sys_bitarray_xor() result unexpected: %x",
682 ba.bundles[0]);
683 zassert_equal(bb.bundles[0], 0x80010001, "sys_bitarray_xor() result unexpected: %x",
684 bb.bundles[0]);
685
686 /* Pre-populate the bits */
687 ba.bundles[0] = 0x80001001;
688 ba.bundles[1] = 0x10000008;
689 ba.bundles[2] = 0xFFFFFFFF;
690 ba.bundles[3] = 0x00000000;
691
692 bb.bundles[0] = 0x80010001;
693 bb.bundles[1] = 0x10000008;
694 bb.bundles[2] = 0xFFFFFFFF;
695 bb.bundles[3] = 0x00000000;
696
697 ret = sys_bitarray_xor(&ba, &bb, 16, 16);
698 zassert_equal(ret, 0, "sys_bitarray_xor() returned unexpected value: %d", ret);
699 zassert_equal(ba.bundles[0], 0x00011001, "sys_bitarray_xor() result unexpected: %x",
700 ba.bundles[0]);
701 zassert_equal(bb.bundles[0], 0x80010001, "sys_bitarray_xor() result unexpected: %x",
702 bb.bundles[0]);
703
704 printk("Testing bit array region xor spanning multiple bundles\n");
705
706 /* Pre-populate the bits */
707 ba.bundles[0] = 0x00000000;
708 ba.bundles[1] = 0xFFFFFFFF;
709 ba.bundles[2] = 0xFFFFFFFF;
710 ba.bundles[3] = 0xFFFFFFFF;
711
712 bb.bundles[0] = 0x00000000;
713 bb.bundles[1] = 0xFFFFFFFF;
714 bb.bundles[2] = 0xFFFFFFFF;
715 bb.bundles[3] = 0xFFFFFFFF;
716
717 ret = sys_bitarray_xor(&ba, &bb, 32*3 - 2, 32 + 1);
718 zassert_equal(ret, 0, "sys_bitarray_xor() returned unexpected value: %d", ret);
719 zassert_equal(ba.bundles[0], 0x00000000, "sys_bitarray_xor() result unexpected: %x",
720 ba.bundles[0]);
721 zassert_equal(ba.bundles[1], 0x00000001, "sys_bitarray_xor() result unexpected: %x",
722 ba.bundles[1]);
723 zassert_equal(ba.bundles[2], 0x00000000, "sys_bitarray_xor() result unexpected: %x",
724 ba.bundles[2]);
725 zassert_equal(ba.bundles[3], 0x80000000, "sys_bitarray_xor() result unexpected: %x",
726 ba.bundles[3]);
727
728 printk("Testing error cases\n");
729 /* Pre-populate the bits */
730 ba.bundles[0] = 0x00000000;
731 ba.bundles[1] = 0x00000000;
732 ba.bundles[2] = 0x00000000;
733 ba.bundles[3] = 0x00000000;
734
735 bb.bundles[0] = 0x00000000;
736 bb.bundles[1] = 0x00000000;
737 bb.bundles[2] = 0x00000000;
738 bb.bundles[3] = 0x00000000;
739
740 bc.bundles[0] = 0x00000000;
741 bc.bundles[1] = 0x00000000;
742 bc.bundles[2] = 0x00000000;
743 bc.bundles[3] = 0x00000000;
744 bc.bundles[4] = 0x00000000;
745
746 ret = sys_bitarray_xor(&ba, &bb, 32, 0);
747 zassert_equal(ret, 0, "sys_bitarray_xor() returned unexpected value: %d", ret);
748 ret = sys_bitarray_xor(&ba, &bc, 32, 0);
749 zassert_equal(ret, -EINVAL, "sys_bitarray_xor() returned unexpected value: %d", ret);
750 ret = sys_bitarray_xor(&bc, &ba, 32, 0);
751 zassert_equal(ret, -EINVAL, "sys_bitarray_xor() returned unexpected value: %d", ret);
752
753 ret = sys_bitarray_xor(&ba, &bb, 128, 0);
754 zassert_equal(ret, 0, "sys_bitarray_xor() returned unexpected value: %d", ret);
755 ret = sys_bitarray_xor(&ba, &bb, 128, 1);
756 zassert_equal(ret, -EINVAL, "sys_bitarray_xor() returned unexpected value: %d", ret);
757 ret = sys_bitarray_xor(&ba, &bb, 129, 0);
758 zassert_equal(ret, -EINVAL, "sys_bitarray_xor() returned unexpected value: %d", ret);
759 ret = sys_bitarray_xor(&ba, &bb, 0, 0);
760 zassert_equal(ret, -EINVAL, "sys_bitarray_xor() returned unexpected value: %d", ret);
761 }
762
ZTEST(bitarray,test_bitarray_find_nth_set)763 ZTEST(bitarray, test_bitarray_find_nth_set)
764 {
765 int ret;
766 size_t found_at;
767
768 /* Bitarrays have embedded spinlocks and can't on the stack. */
769 if (IS_ENABLED(CONFIG_KERNEL_COHERENCE)) {
770 ztest_test_skip();
771 }
772
773 SYS_BITARRAY_DEFINE(ba, 128);
774
775 printk("Testing bit array nth bit set finding spanning single bundle\n");
776
777 /* Pre-populate the bits */
778 ba.bundles[0] = 0x80000001;
779 ba.bundles[1] = 0x80000001;
780 ba.bundles[2] = 0x80000001;
781 ba.bundles[3] = 0x80000001;
782
783 ret = sys_bitarray_find_nth_set(&ba, 1, 1, 0, &found_at);
784 zassert_equal(ret, 0, "sys_bitarray_find_nth_set() returned unexpected value: %d", ret);
785 zassert_equal(found_at, 0, "sys_bitarray_find_nth_set() returned unexpected found_at: %d",
786 found_at);
787
788 ret = sys_bitarray_find_nth_set(&ba, 1, 32, 0, &found_at);
789 zassert_equal(ret, 0, "sys_bitarray_find_nth_set() returned unexpected value: %d", ret);
790 zassert_equal(found_at, 0, "sys_bitarray_find_nth_set() returned unexpected found_at: %d",
791 found_at);
792
793 ret = sys_bitarray_find_nth_set(&ba, 2, 32, 0, &found_at);
794 zassert_equal(ret, 0, "sys_bitarray_find_nth_set() returned unexpected value: %d", ret);
795 zassert_equal(found_at, 31, "sys_bitarray_find_nth_set() returned unexpected found_at: %d",
796 found_at);
797
798 ret = sys_bitarray_find_nth_set(&ba, 1, 31, 1, &found_at);
799 zassert_equal(ret, 0, "sys_bitarray_find_nth_set() returned unexpected value: %d", ret);
800 zassert_equal(found_at, 31, "sys_bitarray_find_nth_set() returned unexpected found_at: %d",
801 found_at);
802
803 ret = sys_bitarray_find_nth_set(&ba, 2, 31, 1, &found_at);
804 zassert_equal(ret, 1, "sys_bitarray_find_nth_set() returned unexpected value: %d", ret);
805
806 printk("Testing bit array nth bit set finding spanning multiple bundles\n");
807
808 ret = sys_bitarray_find_nth_set(&ba, 1, 128, 0, &found_at);
809 zassert_equal(ret, 0, "sys_bitarray_find_nth_set() returned unexpected value: %d", ret);
810 zassert_equal(found_at, 0, "sys_bitarray_find_nth_set() returned unexpected found_at: %d",
811 found_at);
812
813 ret = sys_bitarray_find_nth_set(&ba, 8, 128, 0, &found_at);
814 zassert_equal(ret, 0, "sys_bitarray_find_nth_set() returned unexpected value: %d", ret);
815 zassert_equal(found_at, 127, "sys_bitarray_find_nth_set() returned unexpected found_at: %d",
816 found_at);
817
818 ret = sys_bitarray_find_nth_set(&ba, 8, 128, 1, &found_at);
819 zassert_equal(ret, -EINVAL, "sys_bitarray_find_nth_set() returned unexpected value: %d",
820 ret);
821
822 ret = sys_bitarray_find_nth_set(&ba, 7, 127, 1, &found_at);
823 zassert_equal(ret, 0, "sys_bitarray_find_nth_set() returned unexpected value: %d", ret);
824 zassert_equal(found_at, 127, "sys_bitarray_find_nth_set() returned unexpected found_at: %d",
825 found_at);
826
827 ret = sys_bitarray_find_nth_set(&ba, 7, 127, 0, &found_at);
828 zassert_equal(ret, 0, "sys_bitarray_find_nth_set() returned unexpected value: %d", ret);
829 zassert_equal(found_at, 96, "sys_bitarray_find_nth_set() returned unexpected found_at: %d",
830 found_at);
831
832 ret = sys_bitarray_find_nth_set(&ba, 6, 127, 1, &found_at);
833 zassert_equal(ret, 0, "sys_bitarray_find_nth_set() returned unexpected value: %d", ret);
834 zassert_equal(found_at, 96, "sys_bitarray_find_nth_set() returned unexpected found_at: %d",
835 found_at);
836
837 ret = sys_bitarray_find_nth_set(&ba, 6, 127, 1, &found_at);
838 zassert_equal(ret, 0, "sys_bitarray_find_nth_set() returned unexpected value: %d", ret);
839 zassert_equal(found_at, 96, "sys_bitarray_find_nth_set() returned unexpected found_at: %d",
840 found_at);
841
842 ret = sys_bitarray_find_nth_set(&ba, 1, 32, 48, &found_at);
843 zassert_equal(ret, 0, "sys_bitarray_find_nth_set() returned unexpected value: %d", ret);
844 zassert_equal(found_at, 63, "sys_bitarray_find_nth_set() returned unexpected found_at: %d",
845 found_at);
846
847 ret = sys_bitarray_find_nth_set(&ba, 2, 32, 48, &found_at);
848 zassert_equal(ret, 0, "sys_bitarray_find_nth_set() returned unexpected value: %d", ret);
849 zassert_equal(found_at, 64, "sys_bitarray_find_nth_set() returned unexpected found_at: %d",
850 found_at);
851
852 printk("Testing error cases\n");
853
854 ret = sys_bitarray_find_nth_set(&ba, 1, 128, 0, &found_at);
855 zassert_equal(ret, 0, "sys_bitarray_find_nth_set() returned unexpected value: %d", ret);
856
857 ret = sys_bitarray_find_nth_set(&ba, 1, 128, 1, &found_at);
858 zassert_equal(ret, -EINVAL, "sys_bitarray_find_nth_set() returned unexpected value: %d",
859 ret);
860
861 ret = sys_bitarray_find_nth_set(&ba, 1, 129, 0, &found_at);
862 zassert_equal(ret, -EINVAL, "sys_bitarray_find_nth_set() returned unexpected value: %d",
863 ret);
864
865 ret = sys_bitarray_find_nth_set(&ba, 0, 128, 0, &found_at);
866 zassert_equal(ret, -EINVAL, "sys_bitarray_find_nth_set() returned unexpected value: %d",
867 ret);
868 }
869
ZTEST(bitarray,test_bitarray_region_set_clear)870 ZTEST(bitarray, test_bitarray_region_set_clear)
871 {
872 int ret;
873
874 /* Bitarrays have embedded spinlocks and can't on the stack. */
875 if (IS_ENABLED(CONFIG_KERNEL_COHERENCE)) {
876 ztest_test_skip();
877 }
878
879 uint32_t ba_expected[4];
880
881 SYS_BITARRAY_DEFINE(ba, 64);
882
883 printk("Testing bit array region bit tests\n");
884
885 /* Pre-populate the bits */
886 ba.bundles[0] = 0xFF0F0F0F;
887 ba.bundles[1] = 0x0F0F0FFF;
888
889 zassert_true(sys_bitarray_is_region_set(&ba, 4, 0));
890 zassert_true(sys_bitarray_is_region_set(&ba, 12, 32));
891 zassert_true(sys_bitarray_is_region_set(&ba, 8, 32));
892 zassert_true(sys_bitarray_is_region_set(&ba, 14, 30));
893 zassert_true(sys_bitarray_is_region_set(&ba, 20, 24));
894
895 zassert_false(sys_bitarray_is_region_cleared(&ba, 4, 0));
896 zassert_false(sys_bitarray_is_region_cleared(&ba, 12, 32));
897 zassert_false(sys_bitarray_is_region_cleared(&ba, 8, 32));
898 zassert_false(sys_bitarray_is_region_cleared(&ba, 14, 30));
899 zassert_false(sys_bitarray_is_region_cleared(&ba, 20, 24));
900
901 ba.bundles[0] = ~ba.bundles[0];
902 ba.bundles[1] = ~ba.bundles[1];
903
904 zassert_true(sys_bitarray_is_region_cleared(&ba, 4, 0));
905 zassert_true(sys_bitarray_is_region_cleared(&ba, 12, 32));
906 zassert_true(sys_bitarray_is_region_cleared(&ba, 8, 32));
907 zassert_true(sys_bitarray_is_region_cleared(&ba, 14, 30));
908 zassert_true(sys_bitarray_is_region_cleared(&ba, 20, 24));
909
910 zassert_false(sys_bitarray_is_region_set(&ba, 4, 0));
911 zassert_false(sys_bitarray_is_region_set(&ba, 12, 32));
912 zassert_false(sys_bitarray_is_region_set(&ba, 8, 32));
913 zassert_false(sys_bitarray_is_region_set(&ba, 14, 30));
914 zassert_false(sys_bitarray_is_region_set(&ba, 20, 24));
915
916 zassert_false(sys_bitarray_is_region_set(&ba, 10, 60));
917 zassert_false(sys_bitarray_is_region_cleared(&ba, 10, 60));
918 zassert_false(sys_bitarray_is_region_set(&ba, 8, 120));
919 zassert_false(sys_bitarray_is_region_cleared(&ba, 8, 120));
920
921 printk("Testing bit array region bit manipulations\n");
922
923 /* Pre-populate the bits */
924 ba.bundles[0] = 0xFF0F0F0F;
925 ba.bundles[1] = 0x0F0F0FFF;
926
927 /* Expected values */
928 ba_expected[0] = 0xFF0F0F0F;
929 ba_expected[1] = 0x0F0F0FFF;
930
931 ret = sys_bitarray_set_region(&ba, 4, 0);
932 zassert_equal(ret, 0, "sys_bitarray_set_region() failed: %d", ret);
933 zassert_true(cmp_u32_arrays(ba.bundles, ba_expected, ba.num_bundles),
934 "sys_bitarray_set_region() failed bits comparison");
935
936 ret = sys_bitarray_set_region(&ba, 4, 4);
937 ba_expected[0] = 0xFF0F0FFF;
938 zassert_equal(ret, 0, "sys_bitarray_set_region() failed: %d", ret);
939 zassert_true(cmp_u32_arrays(ba.bundles, ba_expected, ba.num_bundles),
940 "sys_bitarray_set_region() failed bits comparison");
941
942 ret = sys_bitarray_clear_region(&ba, 4, 4);
943 ba_expected[0] = 0xFF0F0F0F;
944 zassert_equal(ret, 0, "sys_bitarray_clear_region() failed: %d", ret);
945 zassert_true(cmp_u32_arrays(ba.bundles, ba_expected, ba.num_bundles),
946 "sys_bitarray_clear_region() failed bits comparison");
947
948 ret = sys_bitarray_clear_region(&ba, 14, 30);
949 ba_expected[0] = 0x3F0F0F0F;
950 ba_expected[1] = 0x0F0F0000;
951 zassert_equal(ret, 0, "sys_bitarray_clear_region() failed: %d", ret);
952 zassert_true(cmp_u32_arrays(ba.bundles, ba_expected, ba.num_bundles),
953 "sys_bitarray_clear_region() failed bits comparison");
954
955 ret = sys_bitarray_set_region(&ba, 14, 30);
956 ba_expected[0] = 0xFF0F0F0F;
957 ba_expected[1] = 0x0F0F0FFF;
958 zassert_equal(ret, 0, "sys_bitarray_set_region() failed: %d", ret);
959 zassert_true(cmp_u32_arrays(ba.bundles, ba_expected, ba.num_bundles),
960 "sys_bitarray_set_region() failed bits comparison");
961
962 ret = sys_bitarray_set_region(&ba, 10, 60);
963 zassert_equal(ret, -EINVAL, "sys_bitarray_set_region() should fail but not");
964 zassert_true(cmp_u32_arrays(ba.bundles, ba_expected, ba.num_bundles),
965 "sys_bitarray_set_region() failed bits comparison");
966
967 ret = sys_bitarray_set_region(&ba, 8, 120);
968 zassert_equal(ret, -EINVAL, "sys_bitarray_set_region() should fail but not");
969 zassert_true(cmp_u32_arrays(ba.bundles, ba_expected, ba.num_bundles),
970 "sys_bitarray_set_region() failed bits comparison");
971
972 ret = sys_bitarray_clear_region(&ba, 10, 60);
973 zassert_equal(ret, -EINVAL, "sys_bitarray_clear_region() should fail but not");
974 zassert_true(cmp_u32_arrays(ba.bundles, ba_expected, ba.num_bundles),
975 "sys_bitarray_clear_region() failed bits comparison");
976
977 ret = sys_bitarray_clear_region(&ba, 8, 120);
978 zassert_equal(ret, -EINVAL, "sys_bitarray_clear_region() should fail but not");
979 zassert_true(cmp_u32_arrays(ba.bundles, ba_expected, ba.num_bundles),
980 "sys_bitarray_clear_region() failed bits comparison");
981
982 SYS_BITARRAY_DEFINE(bw, 128);
983
984 /* Pre-populate the bits */
985 bw.bundles[0] = 0xFF0F0F0F;
986 bw.bundles[1] = 0xF0000000;
987 bw.bundles[2] = 0xFFFFFFFF;
988 bw.bundles[3] = 0x0000000F;
989
990 zassert_true(sys_bitarray_is_region_set(&bw, 40, 60));
991 zassert_false(sys_bitarray_is_region_cleared(&bw, 40, 60));
992
993 bw.bundles[2] = 0xFFFEEFFF;
994
995 zassert_false(sys_bitarray_is_region_set(&bw, 40, 60));
996 zassert_false(sys_bitarray_is_region_cleared(&bw, 40, 60));
997
998 bw.bundles[1] = 0x0FFFFFFF;
999 bw.bundles[2] = 0x00000000;
1000 bw.bundles[3] = 0xFFFFFFF0;
1001
1002 zassert_true(sys_bitarray_is_region_cleared(&bw, 40, 60));
1003 zassert_false(sys_bitarray_is_region_set(&bw, 40, 60));
1004
1005 bw.bundles[2] = 0x00011000;
1006
1007 zassert_false(sys_bitarray_is_region_cleared(&bw, 40, 60));
1008 zassert_false(sys_bitarray_is_region_set(&bw, 40, 60));
1009 }
1010
1011 /**
1012 * @brief Test find MSB and LSB operations
1013 *
1014 * @details Verify the functions that find out the most significant
1015 * bit and least significant bit work as expected.
1016 *
1017 * @see find_msb_set(), find_lsb_set()
1018 */
ZTEST(bitarray,test_ffs)1019 ZTEST(bitarray, test_ffs)
1020 {
1021 uint32_t value;
1022 unsigned int bit;
1023
1024 /* boundary test, input is min */
1025 value = 0x0;
1026 zassert_equal(find_msb_set(value), 0, "MSB is not matched");
1027 zassert_equal(find_lsb_set(value), 0, "LSB is not matched");
1028
1029 /* boundary test, input is min + 1 */
1030 value = 0x00000001;
1031 zassert_equal(find_msb_set(value), 1, "MSB is not matched");
1032 zassert_equal(find_lsb_set(value), 1, "LSB is not matched");
1033
1034 /* average value test */
1035 value = 0x80000000;
1036 zassert_equal(find_msb_set(value), 32, "MSB is not matched");
1037 zassert_equal(find_lsb_set(value), 32, "LSB is not matched");
1038
1039 /* mediate value test */
1040 value = 0x000FF000;
1041 zassert_equal(find_msb_set(value), 20, "MSB is not matched");
1042 zassert_equal(find_lsb_set(value), 13, "LSB is not matched");
1043
1044 /* boundary test, input is max */
1045 value = 0xffffffff;
1046 zassert_equal(find_msb_set(value), 32, "MSB is not matched");
1047 zassert_equal(find_lsb_set(value), 1, "LSB is not matched");
1048
1049 /* boundary test, input is max - 1 */
1050 value = 0xfffffffe;
1051 zassert_equal(find_msb_set(value), 32, "MSB is not matched");
1052 zassert_equal(find_lsb_set(value), 2, "LSB is not matched");
1053
1054 /* equivalent class testing, each bit means a class */
1055 for (bit = 0; bit < 32 ; bit++) {
1056 value = 1UL << bit;
1057 zassert_equal(find_msb_set(value), bit + 1, "MSB is not matched");
1058 zassert_equal(find_lsb_set(value), bit + 1, "LSB is not matched");
1059 }
1060 }
1061 extern void *common_setup(void);
1062 ZTEST_SUITE(bitarray, NULL, common_setup, NULL, NULL, NULL);
1063
1064 /**
1065 * @}
1066 */
1067