more tests
This commit is contained in:
parent
26bb560eb7
commit
120eee0beb
4 changed files with 78 additions and 18 deletions
|
@ -89,6 +89,9 @@ void sparsemap_open(sparsemap_t *, uint8_t *data, size_t data_size);
|
|||
/* Resizes the data range. */
|
||||
void sparsemap_set_data_size(sparsemap_t *map, size_t data_size);
|
||||
|
||||
/* Calculate remaining capacity, full when 0. */
|
||||
int sparsemap_remaining_capacity(sparsemap_t *map);
|
||||
|
||||
/* Returns the size of the underlying byte array. */
|
||||
size_t sparsemap_get_range_size(sparsemap_t *map);
|
||||
|
||||
|
|
|
@ -748,6 +748,7 @@ __sm_remove_data(sparsemap_t *map, size_t offset, size_t gap_size)
|
|||
void
|
||||
sparsemap_clear(sparsemap_t *map)
|
||||
{
|
||||
memset(map->m_data, 0, map->m_data_size);
|
||||
map->m_data_used = SM_SIZEOF_OVERHEAD;
|
||||
__sm_set_chunk_map_count(map, 0);
|
||||
}
|
||||
|
@ -797,6 +798,19 @@ sparsemap_set_data_size(sparsemap_t *map, size_t data_size)
|
|||
map->m_data_size = data_size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the remaining capacity as an integer that approaches 0 to
|
||||
* indicate full.
|
||||
*/
|
||||
int
|
||||
sparsemap_remaining_capacity(sparsemap_t *map) {
|
||||
if (map->m_data_used > map->m_data_size) {
|
||||
return 0;
|
||||
}
|
||||
int remaining = (int)(map->m_data_size - map->m_data_used);
|
||||
return (remaining > 100) ? 100 : remaining;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the size of the underlying byte array.
|
||||
*/
|
||||
|
|
|
@ -251,7 +251,7 @@ setup_test_array(int a[], int l, int max_value)
|
|||
|
||||
void
|
||||
bitmap_from_uint32(sparsemap_t *map, uint32_t number) {
|
||||
for (int i = 0; i < 32; ++i) {
|
||||
for (int i = 0; i < 32; i++) {
|
||||
bool bit = number & (1 << i);
|
||||
sparsemap_set(map, i, bit);
|
||||
}
|
||||
|
@ -259,7 +259,7 @@ bitmap_from_uint32(sparsemap_t *map, uint32_t number) {
|
|||
|
||||
void
|
||||
bitmap_from_uint64(sparsemap_t *map, uint64_t number) {
|
||||
for (int i = 0; i < 64; ++i) {
|
||||
for (int i = 0; i < 64; i++) {
|
||||
bool bit = number & (1 << i);
|
||||
sparsemap_set(map, i, bit);
|
||||
}
|
||||
|
@ -291,3 +291,17 @@ rank_uint64(uint64_t number, int n, int p)
|
|||
|
||||
return count;
|
||||
}
|
||||
|
||||
int
|
||||
whats_set_uint64(uint64_t number, int pos[64])
|
||||
{
|
||||
int length = 0;
|
||||
|
||||
for (int i = 0; i < 64; i++) {
|
||||
if (number & ((uint64_t)1 << i)) {
|
||||
pos[length++] = i;
|
||||
}
|
||||
}
|
||||
|
||||
return length;
|
||||
}
|
||||
|
|
61
tests/test.c
61
tests/test.c
|
@ -83,7 +83,6 @@ test_api_clear_setup(const MunitParameter params[], void *user_data)
|
|||
sparsemap_t *map = (sparsemap_t *)test_api_setup(params, user_data);
|
||||
|
||||
sparsemap_init(map, buf, 1024, 0);
|
||||
populate_map(map, 1024, 3 * 1024);
|
||||
|
||||
return (void *)map;
|
||||
}
|
||||
|
@ -102,12 +101,10 @@ test_api_clear(const MunitParameter params[], void *data)
|
|||
|
||||
assert_ptr_not_null(map);
|
||||
|
||||
assert_true(map->m_data_size == 1024);
|
||||
|
||||
sparsemap_set(map, 42, true);
|
||||
assert_true(sparsemap_is_set(map, 42));
|
||||
sparsemap_clear(map);
|
||||
|
||||
assert_true(map->m_data_size == 1024);
|
||||
assert_true(map->m_data_used == sizeof(uint32_t));
|
||||
assert_false(sparsemap_is_set(map, 42));
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
@ -179,6 +176,42 @@ test_api_set_data_size(const MunitParameter params[], void *data)
|
|||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_remaining_capacity_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
uint8_t *buf = munit_calloc(1024, sizeof(uint8_t));
|
||||
sparsemap_t *map = (sparsemap_t *)test_api_setup(params, user_data);
|
||||
|
||||
sparsemap_init(map, buf, 1024, 0);
|
||||
|
||||
return (void *)map;
|
||||
}
|
||||
static void
|
||||
test_api_remaining_capacity_tear_down(void *fixture)
|
||||
{
|
||||
sparsemap_t *map = (sparsemap_t *)fixture;
|
||||
free(map->m_data);
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_remaining_capacity(const MunitParameter params[], void *data)
|
||||
{
|
||||
sparsemap_t *map = (sparsemap_t *)data;
|
||||
(void)params;
|
||||
|
||||
assert_ptr_not_null(map);
|
||||
|
||||
int i = 0, cap = sparsemap_remaining_capacity(map);
|
||||
while (cap > 0 && i < 10000) {
|
||||
sparsemap_set(map, i++, true);
|
||||
int new_cap = sparsemap_remaining_capacity(map);
|
||||
assert_true(new_cap <= cap);
|
||||
cap = new_cap;
|
||||
}
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_get_range_size_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
|
@ -487,7 +520,7 @@ test_api_rank_tear_down(void *fixture)
|
|||
static MunitResult
|
||||
test_api_rank(const MunitParameter params[], void *data)
|
||||
{
|
||||
int rank;
|
||||
int r1, r2;
|
||||
sparsemap_t *map = (sparsemap_t *)data;
|
||||
(void)params;
|
||||
|
||||
|
@ -505,10 +538,11 @@ test_api_rank(const MunitParameter params[], void *data)
|
|||
/* rank() is also 0-based, for consistency (and confusion sake); consider the
|
||||
range as [start, end] of [0, 9] counts the bits set in the first 10
|
||||
positions (starting from the LSB) in the index. */
|
||||
int r1 = sparsemap_rank(map, 0, 9);
|
||||
int r2 = rank_uint64((uint64_t)-1, 0, 9);
|
||||
r1 = sparsemap_rank(map, 0, 9);
|
||||
r2 = rank_uint64((uint64_t)-1, 0, 9);
|
||||
assert_true(r1 == r2);
|
||||
assert_true(sparsemap_rank(map, 0, 9) == 10);
|
||||
assert_true(sparsemap_rank(map, 1000, 1050) == 0);
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
for (int j = i; j < 10; j++) {
|
||||
|
@ -518,12 +552,6 @@ test_api_rank(const MunitParameter params[], void *data)
|
|||
}
|
||||
}
|
||||
|
||||
sparsemap_clear(map);
|
||||
uint64_t bits = ((uint64_t)0xfeedface << 32) | 0xbadc0ffee;
|
||||
bitmap_from_uint64(map, bits);
|
||||
rank = sparsemap_rank(map, 3, 18);
|
||||
assert_true(rank == rank_uint64(bits, 3, 18));
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
|
@ -553,7 +581,7 @@ test_api_span(const MunitParameter params[], void *data)
|
|||
|
||||
assert_ptr_not_null(map);
|
||||
|
||||
size_t size = sparsemap_span(map, 0, 1);
|
||||
sparsemap_span(map, 0, 1);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
@ -562,6 +590,7 @@ static MunitTest api_test_suite[] = { { (char *)"/api/static_init", test_api_sta
|
|||
{ (char *)"/api/clear", test_api_clear, test_api_clear_setup, test_api_clear_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/api/open", test_api_open, test_api_open_setup, test_api_open_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/api/set_data_size", test_api_set_data_size, test_api_set_data_size_setup, test_api_set_data_size_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/api/remaining_capacity", test_api_remaining_capacity, test_api_remaining_capacity_setup, test_api_remaining_capacity_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/api/get_range_size", test_api_get_range_size, test_api_get_range_size_setup, test_api_get_range_size_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/api/is_set", test_api_is_set, test_api_is_set_setup, test_api_is_set_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/api/set", test_api_set, test_api_set_setup, test_api_set_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
|
|
Loading…
Reference in a new issue