test suite for the sparse bitmap data structure #1
5 changed files with 48 additions and 242 deletions
|
@ -37,8 +37,8 @@ main(void)
|
||||||
|
|
||||||
// randomize setting the bits on
|
// randomize setting the bits on
|
||||||
shuffle(array, TEST_ARRAY_SIZE);
|
shuffle(array, TEST_ARRAY_SIZE);
|
||||||
//print_array(array, TEST_ARRAY_SIZE);
|
// print_array(array, TEST_ARRAY_SIZE);
|
||||||
//print_spans(array, TEST_ARRAY_SIZE);
|
// print_spans(array, TEST_ARRAY_SIZE);
|
||||||
|
|
||||||
// set all the bits on in a random order
|
// set all the bits on in a random order
|
||||||
for (i = 0; i < TEST_ARRAY_SIZE; i++) {
|
for (i = 0; i < TEST_ARRAY_SIZE; i++) {
|
||||||
|
|
196
tests/api.c
196
tests/api.c
|
@ -1,196 +0,0 @@
|
||||||
static void *
|
|
||||||
test_api_setup(const MunitParameter params[], void *user_data)
|
|
||||||
{
|
|
||||||
struct test_info *info = (struct test_info *)user_data;
|
|
||||||
(void)info;
|
|
||||||
(void)params;
|
|
||||||
|
|
||||||
ex_sl_t *slist = calloc(sizeof(ex_sl_t), 1);
|
|
||||||
if (slist == NULL)
|
|
||||||
return NULL;
|
|
||||||
sl_init(slist, uint32_key_cmp);
|
|
||||||
return (void *)(uintptr_t)slist;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
test_api_tear_down(void *fixture)
|
|
||||||
{
|
|
||||||
ex_sl_t *slist = (ex_sl_t *)fixture;
|
|
||||||
assert_ptr_not_null(slist);
|
|
||||||
sl_node *cursor = sl_begin(slist);
|
|
||||||
while (cursor) {
|
|
||||||
assert_ptr_not_null(cursor);
|
|
||||||
ex_node_t *entry = sl_get_entry(cursor, ex_node_t, snode);
|
|
||||||
assert_ptr_not_null(entry);
|
|
||||||
assert_uint32(entry->key, ==, entry->value);
|
|
||||||
cursor = sl_next(slist, cursor);
|
|
||||||
sl_erase_node(slist, &entry->snode);
|
|
||||||
sl_release_node(&entry->snode);
|
|
||||||
sl_wait_for_free(&entry->snode);
|
|
||||||
sl_free_node(&entry->snode);
|
|
||||||
free(entry);
|
|
||||||
}
|
|
||||||
sl_free(slist);
|
|
||||||
free(fixture);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void *
|
|
||||||
test_api_insert_setup(const MunitParameter params[], void *user_data)
|
|
||||||
{
|
|
||||||
return test_api_setup(params, user_data);
|
|
||||||
}
|
|
||||||
static void
|
|
||||||
test_api_insert_tear_down(void *fixture)
|
|
||||||
{
|
|
||||||
test_api_tear_down(fixture);
|
|
||||||
}
|
|
||||||
static MunitResult
|
|
||||||
test_api_insert(const MunitParameter params[], void *data)
|
|
||||||
{
|
|
||||||
sl_raw *slist = (sl_raw *)data;
|
|
||||||
(void)params;
|
|
||||||
assert_ptr_not_null(data);
|
|
||||||
int n = munit_rand_int_range(128, 4096);
|
|
||||||
int key = munit_rand_int_range(0, (((uint32_t)0) - 1) / 10);
|
|
||||||
while (n--) {
|
|
||||||
ex_node_t *node = (ex_node_t *)calloc(sizeof(ex_node_t), 1);
|
|
||||||
sl_init_node(&node->snode);
|
|
||||||
node->key = key;
|
|
||||||
node->value = key;
|
|
||||||
sl_insert(slist, &node->snode);
|
|
||||||
}
|
|
||||||
return MUNIT_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void *
|
|
||||||
test_api_remove_setup(const MunitParameter params[], void *user_data)
|
|
||||||
{
|
|
||||||
return test_api_setup(params, user_data);
|
|
||||||
}
|
|
||||||
static void
|
|
||||||
test_api_remove_tear_down(void *fixture)
|
|
||||||
{
|
|
||||||
test_api_tear_down(fixture);
|
|
||||||
}
|
|
||||||
static MunitResult
|
|
||||||
test_api_remove(const MunitParameter params[], void *data)
|
|
||||||
{
|
|
||||||
sl_raw *slist = (sl_raw *)data;
|
|
||||||
(void)params;
|
|
||||||
(void)slist;
|
|
||||||
return MUNIT_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void *
|
|
||||||
test_api_find_setup(const MunitParameter params[], void *user_data)
|
|
||||||
{
|
|
||||||
return test_api_setup(params, user_data);
|
|
||||||
}
|
|
||||||
static void
|
|
||||||
test_api_find_tear_down(void *fixture)
|
|
||||||
{
|
|
||||||
test_api_tear_down(fixture);
|
|
||||||
}
|
|
||||||
static MunitResult
|
|
||||||
test_api_find(const MunitParameter params[], void *data)
|
|
||||||
{
|
|
||||||
sl_raw *slist = (sl_raw *)data;
|
|
||||||
(void)params;
|
|
||||||
(void)slist;
|
|
||||||
return MUNIT_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void *
|
|
||||||
test_api_update_setup(const MunitParameter params[], void *user_data)
|
|
||||||
{
|
|
||||||
return test_api_setup(params, user_data);
|
|
||||||
}
|
|
||||||
static void
|
|
||||||
test_api_update_tear_down(void *fixture)
|
|
||||||
{
|
|
||||||
test_api_tear_down(fixture);
|
|
||||||
}
|
|
||||||
static MunitResult
|
|
||||||
test_api_update(const MunitParameter params[], void *data)
|
|
||||||
{
|
|
||||||
sl_raw *slist = (sl_raw *)data;
|
|
||||||
(void)params;
|
|
||||||
(void)slist;
|
|
||||||
return MUNIT_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void *
|
|
||||||
test_api_delete_setup(const MunitParameter params[], void *user_data)
|
|
||||||
{
|
|
||||||
return test_api_setup(params, user_data);
|
|
||||||
}
|
|
||||||
static void
|
|
||||||
test_api_delete_tear_down(void *fixture)
|
|
||||||
{
|
|
||||||
test_api_tear_down(fixture);
|
|
||||||
}
|
|
||||||
static MunitResult
|
|
||||||
test_api_delete(const MunitParameter params[], void *data)
|
|
||||||
{
|
|
||||||
sl_raw *slist = (sl_raw *)data;
|
|
||||||
(void)params;
|
|
||||||
(void)slist;
|
|
||||||
return MUNIT_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void *
|
|
||||||
test_api_duplicates_setup(const MunitParameter params[], void *user_data)
|
|
||||||
{
|
|
||||||
return test_api_setup(params, user_data);
|
|
||||||
}
|
|
||||||
static void
|
|
||||||
test_api_duplicates_tear_down(void *fixture)
|
|
||||||
{
|
|
||||||
test_api_tear_down(fixture);
|
|
||||||
}
|
|
||||||
static MunitResult
|
|
||||||
test_api_duplicates(const MunitParameter params[], void *data)
|
|
||||||
{
|
|
||||||
sl_raw *slist = (sl_raw *)data;
|
|
||||||
(void)params;
|
|
||||||
(void)slist;
|
|
||||||
return MUNIT_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void *
|
|
||||||
test_api_size_setup(const MunitParameter params[], void *user_data)
|
|
||||||
{
|
|
||||||
return test_api_setup(params, user_data);
|
|
||||||
}
|
|
||||||
static void
|
|
||||||
test_api_size_tear_down(void *fixture)
|
|
||||||
{
|
|
||||||
test_api_tear_down(fixture);
|
|
||||||
}
|
|
||||||
static MunitResult
|
|
||||||
test_api_size(const MunitParameter params[], void *data)
|
|
||||||
{
|
|
||||||
sl_raw *slist = (sl_raw *)data;
|
|
||||||
(void)params;
|
|
||||||
(void)slist;
|
|
||||||
return MUNIT_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void *
|
|
||||||
test_api_iterators_setup(const MunitParameter params[], void *user_data)
|
|
||||||
{
|
|
||||||
return test_api_setup(params, user_data);
|
|
||||||
}
|
|
||||||
static void
|
|
||||||
test_api_iterators_tear_down(void *fixture)
|
|
||||||
{
|
|
||||||
test_api_tear_down(fixture);
|
|
||||||
}
|
|
||||||
static MunitResult
|
|
||||||
test_api_iterators(const MunitParameter params[], void *data)
|
|
||||||
{
|
|
||||||
sl_raw *slist = (sl_raw *)data;
|
|
||||||
(void)params;
|
|
||||||
(void)slist;
|
|
||||||
return MUNIT_OK;
|
|
||||||
}
|
|
|
@ -16,7 +16,8 @@ uint32_t
|
||||||
xorshift32()
|
xorshift32()
|
||||||
{
|
{
|
||||||
uint32_t x = *state = &__prng;
|
uint32_t x = *state = &__prng;
|
||||||
if (x == 0) x = 123456789;
|
if (x == 0)
|
||||||
|
x = 123456789;
|
||||||
x ^= x << 13;
|
x ^= x << 13;
|
||||||
x ^= x >> 17;
|
x ^= x >> 17;
|
||||||
x ^= x << 5;
|
x ^= x << 5;
|
||||||
|
@ -25,7 +26,8 @@ xorshift32()
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
xorshift32_seed() {
|
xorshift32_seed()
|
||||||
|
{
|
||||||
// Seed the PRNG
|
// Seed the PRNG
|
||||||
#ifdef STABLE_SEED
|
#ifdef STABLE_SEED
|
||||||
__prng = 8675309;
|
__prng = 8675309;
|
||||||
|
@ -62,12 +64,15 @@ compare_ints(const void *a, const void *b)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if there's already a sequence of 'r' sequential integers
|
// Check if there's already a sequence of 'r' sequential integers
|
||||||
int has_sequential_set(int *a, size_t l, int r) {
|
int
|
||||||
|
has_sequential_set(int *a, size_t l, int r)
|
||||||
|
{
|
||||||
int count = 1; // Start with a count of 1 for the first number
|
int count = 1; // Start with a count of 1 for the first number
|
||||||
for (size_t i = 1; i < l; ++i) {
|
for (size_t i = 1; i < l; ++i) {
|
||||||
if (a[i] - a[i - 1] == 1) { // Check if the current and previous elements are sequential
|
if (a[i] - a[i - 1] == 1) { // Check if the current and previous elements are sequential
|
||||||
count++;
|
count++;
|
||||||
if (count >= r) return 1; // Found a sequential set of length 'r'
|
if (count >= r)
|
||||||
|
return 1; // Found a sequential set of length 'r'
|
||||||
} else {
|
} else {
|
||||||
count = 1; // Reset count if the sequence breaks
|
count = 1; // Reset count if the sequence breaks
|
||||||
}
|
}
|
||||||
|
@ -76,8 +81,11 @@ int has_sequential_set(int *a, size_t l, int r) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to ensure an array contains a set of 'r' sequential integers
|
// Function to ensure an array contains a set of 'r' sequential integers
|
||||||
void ensure_sequential_set(int *a, size_t l, int r) {
|
void
|
||||||
if (r > l) return; // If 'r' is greater than array length, cannot satisfy the condition
|
ensure_sequential_set(int *a, size_t l, int r)
|
||||||
|
{
|
||||||
|
if (r > l)
|
||||||
|
return; // If 'r' is greater than array length, cannot satisfy the condition
|
||||||
|
|
||||||
// Sort the array to check for existing sequences
|
// Sort the array to check for existing sequences
|
||||||
qsort(a, l, sizeof(int), compare_ints);
|
qsort(a, l, sizeof(int), compare_ints);
|
||||||
|
@ -226,7 +234,8 @@ was_set(size_t bit, const int array[])
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
is_unique(int a[], size_t l, int value) {
|
is_unique(int a[], size_t l, int value)
|
||||||
|
{
|
||||||
for (size_t i = 0; i < l; ++i) {
|
for (size_t i = 0; i < l; ++i) {
|
||||||
if (a[i] == value) {
|
if (a[i] == value) {
|
||||||
return 0; // Not unique
|
return 0; // Not unique
|
||||||
|
@ -238,7 +247,8 @@ is_unique(int a[], size_t l, int value) {
|
||||||
void
|
void
|
||||||
setup_test_array(int a[], size_t l, int max_value)
|
setup_test_array(int a[], size_t l, int max_value)
|
||||||
{
|
{
|
||||||
if (a == NULL || max_value < 0) return; // Basic error handling and validation
|
if (a == NULL || max_value < 0)
|
||||||
|
return; // Basic error handling and validation
|
||||||
|
|
||||||
for (size_t i = 0; i < l; ++i) {
|
for (size_t i = 0; i < l; ++i) {
|
||||||
int candidate;
|
int candidate;
|
||||||
|
@ -248,4 +258,3 @@ setup_test_array(int a[], size_t l, int max_value)
|
||||||
a[i] = candidate; // Assign the unique value to the array
|
a[i] = candidate; // Assign the unique value to the array
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,9 @@
|
||||||
* SOFTWARE.
|
* SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#pragma GCC diagnostic push
|
||||||
|
#pragma GCC diagnostic ignored "-Wpedantic"
|
||||||
|
|
||||||
/*** Configuration ***/
|
/*** Configuration ***/
|
||||||
|
|
||||||
/* This is just where the output from the test goes. It's really just
|
/* This is just where the output from the test goes. It's really just
|
||||||
|
@ -2253,3 +2256,5 @@ munit_suite_main(const MunitSuite *suite, void *user_data, int argc,
|
||||||
{
|
{
|
||||||
return munit_suite_main_custom(suite, user_data, argc, argv, NULL);
|
return munit_suite_main_custom(suite, user_data, argc, argv, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#pragma GCC diagnostic pop
|
||||||
|
|
36
tests/test.c
36
tests/test.c
|
@ -10,12 +10,12 @@
|
||||||
#define MUNIT_NO_FORK (1)
|
#define MUNIT_NO_FORK (1)
|
||||||
#define MUNIT_ENABLE_ASSERT_ALIASES (1)
|
#define MUNIT_ENABLE_ASSERT_ALIASES (1)
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "../include/sparsemap.h"
|
#include "../include/sparsemap.h"
|
||||||
|
@ -27,8 +27,7 @@
|
||||||
|
|
||||||
#include "common.c"
|
#include "common.c"
|
||||||
|
|
||||||
struct user_data {
|
struct user_data { };
|
||||||
};
|
|
||||||
|
|
||||||
void
|
void
|
||||||
__populate_map(sparsemap_t *map, size_t size, size_t max_value)
|
__populate_map(sparsemap_t *map, size_t size, size_t max_value)
|
||||||
|
@ -48,7 +47,7 @@ test_api_setup(const MunitParameter params[], void *user_data)
|
||||||
{
|
{
|
||||||
struct test_info *info = (struct test_info *)user_data;
|
struct test_info *info = (struct test_info *)user_data;
|
||||||
(void)params;
|
(void)params;
|
||||||
sparsemap_t *map = munit_calloc(1, sizeof(sparsemap));
|
sparsemap_t *map = munit_calloc(1, sizeof(sparsemap_t));
|
||||||
assert_ptr_not_null(map);
|
assert_ptr_not_null(map);
|
||||||
return (void *)(uintptr_t)map;
|
return (void *)(uintptr_t)map;
|
||||||
}
|
}
|
||||||
|
@ -106,7 +105,6 @@ 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);
|
assert_true(map->m_data_size == 1024);
|
||||||
assert_true(map->m_data_used == 412);
|
|
||||||
|
|
||||||
sparsemap_clear(map);
|
sparsemap_clear(map);
|
||||||
|
|
||||||
|
@ -217,27 +215,17 @@ test_api_is_set(const MunitParameter params[], void *data)
|
||||||
return MUNIT_OK;
|
return MUNIT_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static MunitTest api_test_suite[] = { { (char *)"/api/static_init", test_api_static_init, NULL, NULL, 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/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/is_set", test_api_is_set, test_api_is_set_setup, test_api_is_set_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||||
|
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } };
|
||||||
|
|
||||||
static MunitTest api_test_suite[] = {
|
static MunitTest scale_tests[] = { { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } };
|
||||||
{ (char *)"/api/static_init", test_api_static_init, NULL, NULL, 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/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/is_set", test_api_is_set, test_api_is_set_setup,
|
|
||||||
test_api_is_set_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
|
||||||
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
|
|
||||||
};
|
|
||||||
|
|
||||||
static MunitTest scale_tests[] = { { NULL, NULL, NULL, NULL,
|
static MunitSuite other_test_suite[] = { { "/scale", scale_tests, NULL, 1, MUNIT_SUITE_OPTION_NONE }, { NULL, NULL, NULL, 0, MUNIT_SUITE_OPTION_NONE } };
|
||||||
MUNIT_TEST_OPTION_NONE, NULL } };
|
|
||||||
|
|
||||||
static MunitSuite other_test_suite[] = {
|
static const MunitSuite main_test_suite = { (char *)"/api", api_test_suite, other_test_suite, 1, MUNIT_SUITE_OPTION_NONE };
|
||||||
{ "/scale", scale_tests, NULL, 1, MUNIT_SUITE_OPTION_NONE },
|
|
||||||
{ NULL, NULL, NULL, 0, MUNIT_SUITE_OPTION_NONE } };
|
|
||||||
|
|
||||||
static const MunitSuite main_test_suite = { (char *)"/api", api_test_suite,
|
|
||||||
other_test_suite, 1, MUNIT_SUITE_OPTION_NONE };
|
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char *argv[MUNIT_ARRAY_PARAM(argc + 1)])
|
main(int argc, char *argv[MUNIT_ARRAY_PARAM(argc + 1)])
|
||||||
|
|
Loading…
Reference in a new issue