X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=bertos%2Fstruct%2Fbitarray_test.c;h=0dad3a270ef9796c87bb8b92f305beb9591120fd;hb=cbdd295b3172dbfc635ff6895e5eaa0b92e07180;hp=fa65c44a0791f625f47c3a0b40b26d043c0a99ab;hpb=4ac7a89ec44f465a12154d2ff695684eccf3bb77;p=bertos.git diff --git a/bertos/struct/bitarray_test.c b/bertos/struct/bitarray_test.c index fa65c44a..0dad3a27 100644 --- a/bertos/struct/bitarray_test.c +++ b/bertos/struct/bitarray_test.c @@ -41,19 +41,133 @@ #include #include -ALLOC_BITARRAY(test1, 128); -BitArray ctx; +#include + +#define TEST1_LEN 31 +#define TEST2_LEN 17 +#define TEST3_LEN 16 +#define TEST4_LEN 23 +#define TEST5_LEN 72 + +BITARRAY_ALLOC(test1, TEST1_LEN); +BITARRAY_ALLOC(test2, TEST2_LEN); +BITARRAY_ALLOC(test3, TEST3_LEN); +BITARRAY_ALLOC(test4, TEST4_LEN); +BITARRAY_ALLOC(test5, TEST5_LEN); + +BitArray bitx1; +BitArray bitx2; +BitArray bitx3; +BitArray bitx4; +BitArray bitx5; int bitarray_testSetup(void) { kdbg_init(); - init_bitarray(&ctx, test1, sizeof(test1)); + bitarray_init(&bitx1, TEST1_LEN, test1, sizeof(test1)); + bitarray_init(&bitx2, TEST2_LEN, test2, sizeof(test2)); + bitarray_init(&bitx3, TEST3_LEN, test3, sizeof(test3)); + bitarray_init(&bitx4, TEST4_LEN, test4, sizeof(test4)); + bitarray_init(&bitx5, TEST5_LEN, test5, sizeof(test5)); return 0; } int bitarray_testRun(void) { + memset(test1, 0xaa, sizeof(test1)); + + bitarray_dump(&bitx1); + for (size_t i = 0; i < TEST1_LEN; i++) + { + if (!((bool)(i % 2) == bitarray_test(&bitx1,i))) + goto error; + } + + memset(test1, 0, sizeof(test1)); + for (size_t i = 0; i < TEST1_LEN; i++) + { + if ((i % 2) == 0) + bitarray_clear(&bitx1,i); + else + bitarray_set(&bitx1, i); + } + + bitarray_dump(&bitx1); + for (size_t i = 0; i < TEST1_LEN; i++) + { + if (!((bool)(i % 2) == bitarray_test(&bitx1, i))) + goto error; + } + + memset(test1, 0, sizeof(test1)); + bitarray_set(&bitx1, 0); + bitarray_dump(&bitx1); + if (!bitarray_test(&bitx1, 0)) + goto error; + + memset(test1, 0, sizeof(test1)); + bitarray_set(&bitx1, TEST1_LEN); + bitarray_dump(&bitx1); + if (!bitarray_test(&bitx1, TEST1_LEN)) + goto error; + + kprintf("Test 2\n"); + memset(test2, 0xFF, sizeof(test2)); + bitarray_dump(&bitx2); + if (!bitarray_isFull(&bitx2)) + goto error; + + memset(test2, 0xFF, sizeof(test2)); + bitarray_clear(&bitx2, 5); + bitarray_dump(&bitx2); + if (bitarray_isFull(&bitx2)) + goto error; + + memset(test2, 0xFF, sizeof(test2)); + bitarray_clear(&bitx2, 13); + bitarray_dump(&bitx2); + if (bitarray_isFull(&bitx2)) + goto error; + + kprintf("Test 3\n"); + bitarray_set(&bitx3, 12); + bitarray_dump(&bitx3); + int pos = 0; + pos = bitarray_firstSetBit(&bitx3); + if (pos != 12) + goto error; + + kprintf("Test 4\n"); + bitarray_set(&bitx4, TEST4_LEN); + bitarray_dump(&bitx4); + pos = 0; + pos = bitarray_firstSetBit(&bitx4); + if (pos != 23) + goto error; + + kprintf("Test 5\n"); + bitarray_set(&bitx5, 71); + bitarray_dump(&bitx5); + pos = 0; + pos = bitarray_firstSetBit(&bitx5); + kprintf("pos %d\n", pos); + if (pos != 71) + goto error; + + kprintf("Test 6\n"); + bitarray_clear(&bitx5, 71); + bitarray_set(&bitx5, 5); + bitarray_dump(&bitx5); + pos = 0; + pos = bitarray_firstSetBit(&bitx5); + if (pos != 5) + goto error; + return 0; + +error: + kprintf("Error!\n"); + return -1; } int bitarray_testTearDown(void)