fixes, more tests
This commit is contained in:
parent
d42126054f
commit
9109a7430c
3 changed files with 136 additions and 48 deletions
|
@ -873,7 +873,7 @@ sparsemap_set_data_size(sparsemap_t *map, size_t size)
|
|||
if (!m) {
|
||||
return NULL;
|
||||
}
|
||||
memset(m + sizeof(sparsemap_t) + (m->m_capacity * sizeof(uint8_t)), 0, total_size - m->m_capacity - padding);
|
||||
memset(((uint8_t *)m) + sizeof(sparsemap_t) + (m->m_capacity * sizeof(uint8_t)), 0, size - m->m_capacity + padding);
|
||||
m->m_capacity = data_size;
|
||||
m->m_data = (uint8_t *)((uintptr_t)m + sizeof(sparsemap_t) + padding);
|
||||
__sm_when_diag({ __sm_assert(IS_8_BYTE_ALIGNED(m->m_data)); }) return m;
|
||||
|
|
|
@ -147,27 +147,6 @@ ensure_sequential_set(int a[], int l, int r)
|
|||
return value;
|
||||
}
|
||||
|
||||
sparsemap_idx_t
|
||||
sm_add_span(sparsemap_t *map, int map_size, int span_length)
|
||||
{
|
||||
int attempts = map_size / span_length;
|
||||
sparsemap_idx_t placed_at;
|
||||
do {
|
||||
placed_at = random_uint32() % (map_size - span_length - 1);
|
||||
if (sm_occupied(map, placed_at, span_length, true)) {
|
||||
attempts--;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} while (attempts);
|
||||
for (int i = placed_at; i < placed_at + span_length; i++) {
|
||||
if (sparsemap_set(map, i, true) != i) {
|
||||
return placed_at; // TODO error?
|
||||
}
|
||||
}
|
||||
return placed_at;
|
||||
}
|
||||
|
||||
void
|
||||
print_array(int *array, int l)
|
||||
{
|
||||
|
@ -294,6 +273,20 @@ is_unique(int a[], int l, int value)
|
|||
return 1; // Unique
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void
|
||||
setup_test_array(int a[], int l, int max_value)
|
||||
{
|
||||
|
@ -318,15 +311,6 @@ bitmap_from_uint32(sparsemap_t *map, uint32_t number)
|
|||
}
|
||||
}
|
||||
|
||||
void
|
||||
sm_bitmap_from_uint64(sparsemap_t *map, uint64_t number)
|
||||
{
|
||||
for (int i = 0; i < 64; i++) {
|
||||
bool bit = number & ((uint64_t)1 << i);
|
||||
sparsemap_set(map, i, bit);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t
|
||||
rank_uint64(uint64_t number, int n, int p)
|
||||
{
|
||||
|
@ -354,18 +338,34 @@ rank_uint64(uint64_t number, int n, int p)
|
|||
return count;
|
||||
}
|
||||
|
||||
int
|
||||
whats_set_uint64(uint64_t number, int pos[64])
|
||||
void
|
||||
sm_bitmap_from_uint64(sparsemap_t *map, uint64_t number)
|
||||
{
|
||||
int length = 0;
|
||||
|
||||
for (int i = 0; i < 64; i++) {
|
||||
if (number & ((uint64_t)1 << i)) {
|
||||
pos[length++] = i;
|
||||
bool bit = number & ((uint64_t)1 << i);
|
||||
sparsemap_set(map, i, bit);
|
||||
}
|
||||
}
|
||||
|
||||
sparsemap_idx_t
|
||||
sm_add_span(sparsemap_t *map, int map_size, int span_length)
|
||||
{
|
||||
int attempts = map_size / span_length;
|
||||
sparsemap_idx_t placed_at;
|
||||
do {
|
||||
placed_at = random_uint32() % (map_size - span_length - 1);
|
||||
if (sm_occupied(map, placed_at, span_length, true)) {
|
||||
attempts--;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} while (attempts);
|
||||
for (int i = placed_at; i < placed_at + span_length; i++) {
|
||||
if (sparsemap_set(map, i, true) != i) {
|
||||
return placed_at; // TODO error?
|
||||
}
|
||||
}
|
||||
|
||||
return length;
|
||||
return placed_at;
|
||||
}
|
||||
|
||||
void
|
||||
|
|
106
tests/test.c
106
tests/test.c
|
@ -77,7 +77,90 @@ test_api_tear_down(void *fixture)
|
|||
/* -------------------------- API Tests */
|
||||
|
||||
static MunitResult
|
||||
test_api_static_init(const MunitParameter params[], void *data)
|
||||
test_api_new(const MunitParameter params[], void *data)
|
||||
{
|
||||
sparsemap_t *map = sparsemap(1024);
|
||||
(void)params;
|
||||
(void)data;
|
||||
|
||||
assert_ptr_not_null(map);
|
||||
assert_true(map->m_capacity == 1024);
|
||||
assert_true(map->m_data_used == sizeof(uint32_t));
|
||||
|
||||
munit_free(map);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static MunitResult
|
||||
test_api_new_realloc(const MunitParameter params[], void *data)
|
||||
{
|
||||
sparsemap_t *map = sparsemap(1024);
|
||||
(void)params;
|
||||
(void)data;
|
||||
|
||||
assert_ptr_not_null(map);
|
||||
assert_true(map->m_capacity == 1024);
|
||||
assert_true(map->m_data_used == sizeof(uint32_t));
|
||||
|
||||
map = sparsemap_set_data_size(map, 2048);
|
||||
assert_true(map->m_capacity == 2048);
|
||||
assert_true(map->m_data_used == sizeof(uint32_t));
|
||||
|
||||
munit_free(map);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static MunitResult
|
||||
test_api_new_heap(const MunitParameter params[], void *data)
|
||||
{
|
||||
sparsemap_t *map;
|
||||
uint8_t *buf;
|
||||
(void)params;
|
||||
(void)data;
|
||||
|
||||
map = munit_malloc(sizeof(sparsemap_t));
|
||||
assert_ptr_not_null(map);
|
||||
buf = munit_calloc(1024, sizeof(uint8_t));
|
||||
assert_ptr_not_null(buf);
|
||||
sparsemap_init(map, buf, 1024);
|
||||
|
||||
sparsemap_init(map, buf, 1024);
|
||||
assert_ptr_equal(buf, map->m_data);
|
||||
assert_true(map->m_capacity == 1024);
|
||||
assert_true(map->m_data_used == sizeof(uint32_t));
|
||||
|
||||
munit_free(map->m_data);
|
||||
munit_free(map);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static MunitResult
|
||||
test_api_new_static(const MunitParameter params[], void *data)
|
||||
{
|
||||
sparsemap_t a_map, *map = &a_map;
|
||||
uint8_t *buf;
|
||||
(void)params;
|
||||
(void)data;
|
||||
|
||||
buf = munit_calloc(1024, sizeof(uint8_t));
|
||||
assert_ptr_not_null(buf);
|
||||
sparsemap_init(map, buf, 1024);
|
||||
|
||||
sparsemap_init(map, buf, 1024);
|
||||
assert_ptr_equal(buf, map->m_data);
|
||||
assert_true(map->m_capacity == 1024);
|
||||
assert_true(map->m_data_used == sizeof(uint32_t));
|
||||
|
||||
munit_free(map->m_data);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static MunitResult
|
||||
test_api_new_stack(const MunitParameter params[], void *data)
|
||||
{
|
||||
sparsemap_t a_map, *map = &a_map;
|
||||
uint8_t buf[1024] = { 0 };
|
||||
|
@ -85,7 +168,6 @@ test_api_static_init(const MunitParameter params[], void *data)
|
|||
(void)params;
|
||||
(void)data;
|
||||
|
||||
assert_ptr_not_null(map);
|
||||
sparsemap_init(map, buf, 1024);
|
||||
assert_ptr_equal(&buf, map->m_data);
|
||||
assert_true(map->m_capacity == 1024);
|
||||
|
@ -755,7 +837,11 @@ test_api_span(const MunitParameter params[], void *data)
|
|||
|
||||
// clang-format off
|
||||
static MunitTest api_test_suite[] = {
|
||||
{ (char *)"/static_init", test_api_static_init, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/new", test_api_new, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/new/realloc", test_api_new_realloc, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/new/heap", test_api_new_heap, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/new/static", test_api_new_static, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/new/stack", test_api_new_stack, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/clear", test_api_clear, test_api_clear_setup, test_api_clear_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/open", test_api_open, test_api_open_setup, test_api_open_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/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 },
|
||||
|
@ -787,10 +873,11 @@ static MunitTest api_test_suite[] = {
|
|||
static void *
|
||||
test_scale_lots_o_spans_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
(void)params;
|
||||
(void)user_data;
|
||||
sparsemap_t *map = sparsemap(1024);
|
||||
assert_ptr_not_null(map);
|
||||
uint8_t *buf = munit_calloc(10 * 1024, sizeof(uint8_t));
|
||||
assert_ptr_not_null(buf);
|
||||
sparsemap_t *map = (sparsemap_t *)test_api_setup(params, user_data);
|
||||
|
||||
sparsemap_init(map, buf, 10 * 1024);
|
||||
|
||||
return (void *)map;
|
||||
}
|
||||
|
@ -798,8 +885,9 @@ static void
|
|||
test_scale_lots_o_spans_tear_down(void *fixture)
|
||||
{
|
||||
sparsemap_t *map = (sparsemap_t *)fixture;
|
||||
assert_ptr_not_null(map);
|
||||
munit_free(map);
|
||||
assert_ptr_not_null(map->m_data);
|
||||
munit_free(map->m_data);
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_scale_lots_o_spans(const MunitParameter params[], void *data)
|
||||
|
|
Loading…
Reference in a new issue