Files
libucore/test/test_bitvec.c
T
Nils O. Selåsdal 16e1ac52ec Coding style change
2012-12-05 18:53:58 +01:00

85 lines
2.1 KiB
C

#include <check.h>
#include <ucore/ucore_bitvec.h>
START_TEST (test_bitvec_1)
unsigned long s[3] = {0};
struct bitvec v = BITVEC_STATIC_INIT(s);
size_t i;
for (i = 0; i < sizeof s * 8; i++)
fail_if(get_bit(&v, i), "bit %zu is not 0", i);
END_TEST
START_TEST (test_bitvec_2)
unsigned long s[3] = {0};
struct bitvec v = BITVEC_STATIC_INIT(s);
size_t i;
for (i = 0; i < sizeof s * 8; i++)
set_bit(&v, i);
for (i = 0; i < sizeof s * 8; i++)
fail_if(get_bit(&v, i) != 1 , "bit %zu is not 1", i);
END_TEST
START_TEST (test_bitvec_clearbit)
unsigned long s[3] = {-1, -1, -1};
struct bitvec v = BITVEC_STATIC_INIT(s);
size_t i;
for (i = 0; i < sizeof(unsigned long) * 8; i++)
fail_if(get_bit(&v, i) != 1 , "bit %zu is not 1", i);
for (i = 0; i < sizeof(unsigned long) * 8; i++)
clear_bit(&v, i);
for (i = 0; i < sizeof(unsigned long) * 8; i++)
fail_if(get_bit(&v, i) != 0 , "bit %zu is not 0", i);
END_TEST
START_TEST (test_bitvec_set_bits_from_array)
struct bitvec *v = bitvec_new(5);
char a[] = {1, 0,1 ,0, 1};
set_bits_from_array(v,a , 5);
fail_if(get_bit(v, 0) != 1 , "bit 0 is wrong");
fail_if(get_bit(v, 1) != 0 , "bit 1 is wrong");
fail_if(get_bit(v, 2) != 1 , "bit 2 is wrong");
fail_if(get_bit(v, 3) != 0 , "bit 3 is wrong");
fail_if(get_bit(v, 4) != 1 , "bit 4 is wrong");
END_TEST
START_TEST (test_bitvec_setall_clearall)
struct bitvec *v = bitvec_new(511);
size_t i;
set_all(v);
for (i = 0; i < 511; i++)
fail_if(get_bit(v, i) != 1 , "bit %zu is not 1", i);
clear_all(v);
for (i = 0; i < sizeof(unsigned long) * 8; i++)
fail_if(get_bit(v, i) != 0 , "bit %zu is not 0", i);
END_TEST
Suite *bitvec_suite(void)
{
Suite *s = suite_create("bitvec");
TCase *tc = tcase_create("bitvec tests");
tcase_add_test(tc, test_bitvec_1);
tcase_add_test(tc, test_bitvec_2);
tcase_add_test(tc, test_bitvec_set_bits_from_array);
tcase_add_test(tc, test_bitvec_clearbit);
tcase_add_test(tc, test_bitvec_setall_clearall);
suite_add_tcase(s, tc);
return s;
}