Files
2025-07-20 21:44:36 +02:00

126 lines
2.8 KiB
C

#include <check.h>
#include "ucore/bitvec.h"
START_TEST (test_bitvec_1)
{
uc_bv_integer s[3] = {0};
struct UCBitVec v = UC_BV_STATIC_INIT(s);
size_t i;
for (i = 0; i < sizeof s * 8; i++)
ck_assert_int_eq(uc_bv_get_bit(&v, i), 0);
}
END_TEST
START_TEST (test_bitvec_2)
{
uc_bv_integer s[3] = {0};
struct UCBitVec v = UC_BV_STATIC_INIT(s);
size_t i;
for (i = 0; i < sizeof s * 8; i++)
uc_bv_set_bit(&v, i);
for (i = 0; i < sizeof s * 8; i++)
ck_assert_int_eq(uc_bv_get_bit(&v, i), 1);
}
END_TEST
START_TEST (test_bitvec_clearbit)
{
uc_bv_integer s[3] = {-1, -1, -1};
struct UCBitVec v = UC_BV_STATIC_INIT(s);
size_t i;
for (i = 0; i < sizeof(uc_bv_integer) * 8; i++)
ck_assert_int_eq(uc_bv_get_bit(&v, i), 1);
for (i = 0; i < sizeof(uc_bv_integer) * 8; i++)
uc_bv_clr_bit(&v, i);
for (i = 0; i < sizeof(uc_bv_integer) * 8; i++)
ck_assert_int_eq(uc_bv_get_bit(&v, i), 0);
}
END_TEST
START_TEST (test_bitvec_set_bits_from_array)
{
struct UCBitVec *v = uc_bv_new(5);
char a[] = {1, 0, 1 ,0, 1};
uc_bv_set_bits_from_array(v,a , 5);
ck_assert_int_eq(uc_bv_get_bit(v, 0), 1);
ck_assert_int_eq(uc_bv_get_bit(v, 1), 0);
ck_assert_int_eq(uc_bv_get_bit(v, 2), 1);
ck_assert_int_eq(uc_bv_get_bit(v, 3), 0);
ck_assert_int_eq(uc_bv_get_bit(v, 4), 1);
uc_bv_free(v);
}
END_TEST
START_TEST (test_bitvec_setall_clearall)
{
struct UCBitVec *v = uc_bv_new(511);
size_t i;
uc_bv_set_all(v);
for (i = 0; i < 511; i++)
ck_assert_int_eq(uc_bv_get_bit(v, i), 1);
uc_bv_clr_all(v);
for (i = 0; i < sizeof(uc_bv_integer) * 8; i++)
ck_assert_int_eq(uc_bv_get_bit(v, i), 0);
uc_bv_free(v);
}
END_TEST
START_TEST (test_bitvec_find_first_zero)
{
uc_bv_integer buf[8];
struct UCBitVec v = UC_BV_STATIC_INIT(buf);
uc_bv_set_all(&v);
int bit = uc_bv_find_first_zero(&v);
ck_assert_int_eq(bit, -1);
uc_bv_clr_bit(&v, 0);
bit = uc_bv_find_first_zero(&v);
ck_assert_int_eq(bit, 0);
uc_bv_set_bit(&v, 0);
uc_bv_clr_bit(&v, 63);
bit = uc_bv_find_first_zero(&v);
ck_assert_int_eq(bit, 63);
uc_bv_set_bit(&v, 63);
uc_bv_clr_bit(&v, sizeof buf * CHAR_BIT - 1);
bit = uc_bv_find_first_zero(&v);
ck_assert_int_eq(bit, sizeof buf * CHAR_BIT - 1);
}
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);
tcase_add_test(tc, test_bitvec_find_first_zero);
suite_add_tcase(s, tc);
return s;
}