more tests

This commit is contained in:
Gregory Burd 2024-04-08 23:23:22 -04:00
parent 26bb560eb7
commit 120eee0beb
4 changed files with 78 additions and 18 deletions

View file

@ -89,6 +89,9 @@ void sparsemap_open(sparsemap_t *, uint8_t *data, size_t data_size);
/* Resizes the data range. */ /* Resizes the data range. */
void sparsemap_set_data_size(sparsemap_t *map, size_t data_size); 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. */ /* Returns the size of the underlying byte array. */
size_t sparsemap_get_range_size(sparsemap_t *map); size_t sparsemap_get_range_size(sparsemap_t *map);

View file

@ -748,6 +748,7 @@ __sm_remove_data(sparsemap_t *map, size_t offset, size_t gap_size)
void void
sparsemap_clear(sparsemap_t *map) sparsemap_clear(sparsemap_t *map)
{ {
memset(map->m_data, 0, map->m_data_size);
map->m_data_used = SM_SIZEOF_OVERHEAD; map->m_data_used = SM_SIZEOF_OVERHEAD;
__sm_set_chunk_map_count(map, 0); __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; 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. * Returns the size of the underlying byte array.
*/ */

View file

@ -251,7 +251,7 @@ setup_test_array(int a[], int l, int max_value)
void void
bitmap_from_uint32(sparsemap_t *map, uint32_t number) { 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); bool bit = number & (1 << i);
sparsemap_set(map, i, bit); sparsemap_set(map, i, bit);
} }
@ -259,7 +259,7 @@ bitmap_from_uint32(sparsemap_t *map, uint32_t number) {
void void
bitmap_from_uint64(sparsemap_t *map, uint64_t number) { 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); bool bit = number & (1 << i);
sparsemap_set(map, i, bit); sparsemap_set(map, i, bit);
} }
@ -291,3 +291,17 @@ rank_uint64(uint64_t number, int n, int p)
return count; 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;
}

View file

@ -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_t *map = (sparsemap_t *)test_api_setup(params, user_data);
sparsemap_init(map, buf, 1024, 0); sparsemap_init(map, buf, 1024, 0);
populate_map(map, 1024, 3 * 1024);
return (void *)map; return (void *)map;
} }
@ -102,12 +101,10 @@ test_api_clear(const MunitParameter params[], void *data)
assert_ptr_not_null(map); 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); sparsemap_clear(map);
assert_false(sparsemap_is_set(map, 42));
assert_true(map->m_data_size == 1024);
assert_true(map->m_data_used == sizeof(uint32_t));
return MUNIT_OK; return MUNIT_OK;
} }
@ -179,6 +176,42 @@ test_api_set_data_size(const MunitParameter params[], void *data)
return MUNIT_OK; 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 * static void *
test_api_get_range_size_setup(const MunitParameter params[], void *user_data) 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 static MunitResult
test_api_rank(const MunitParameter params[], void *data) test_api_rank(const MunitParameter params[], void *data)
{ {
int rank; int r1, r2;
sparsemap_t *map = (sparsemap_t *)data; sparsemap_t *map = (sparsemap_t *)data;
(void)params; (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 /* 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 range as [start, end] of [0, 9] counts the bits set in the first 10
positions (starting from the LSB) in the index. */ positions (starting from the LSB) in the index. */
int r1 = sparsemap_rank(map, 0, 9); r1 = sparsemap_rank(map, 0, 9);
int r2 = rank_uint64((uint64_t)-1, 0, 9); r2 = rank_uint64((uint64_t)-1, 0, 9);
assert_true(r1 == r2); assert_true(r1 == r2);
assert_true(sparsemap_rank(map, 0, 9) == 10); 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 i = 0; i < 10; i++) {
for (int j = i; j < 10; j++) { 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; return MUNIT_OK;
} }
@ -553,7 +581,7 @@ test_api_span(const MunitParameter params[], void *data)
assert_ptr_not_null(map); assert_ptr_not_null(map);
size_t size = sparsemap_span(map, 0, 1); sparsemap_span(map, 0, 1);
return MUNIT_OK; 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/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/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/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/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/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 }, { (char *)"/api/set", test_api_set, test_api_set_setup, test_api_set_tear_down, MUNIT_TEST_OPTION_NONE, NULL },