diff --git a/.idea/misc.xml b/.idea/misc.xml index ec9fbe4..947f58d 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,5 +1,9 @@ + + diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..d6d6511 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index 895f061..8bb7acd 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +# Sparsemap + `sparsemap` is a sparse, compressed bitmap. In best case, it can store 2048 bits in just 8 bytes. In worst case, it stores the 2048 bits uncompressed and requires additional 8 bytes of overhead. @@ -41,7 +43,7 @@ absolute address (i.e. if the user sets bit 0 and bit 10000, and the chunk map capacity is 2048, the sparsemap creates two chunk maps; the first starts at offset 0, the second starts at offset 8192). -# Usage instructions +## Usage instructions The file `examples/ex_1.c` has example code. diff --git a/examples/ex_3.c b/examples/ex_3.c index c3e9a9c..6b2c761 100644 --- a/examples/ex_3.c +++ b/examples/ex_3.c @@ -6,70 +6,12 @@ #include #include "../include/sparsemap.h" - -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wvariadic-macros" -#define __diag(...) \ - do { \ - fprintf(stderr, "%s:%d:%s(): ", __FILE__, __LINE__, __func__); \ - fprintf(stderr, __VA_ARGS__); \ - } while (0) -#pragma GCC diagnostic pop - -#define SEED - -/* https://burtleburtle.net/bob/rand/smallprng.html */ -typedef struct rnd_ctx { - uint32_t a; - uint32_t b; - uint32_t c; - uint32_t d; -} rnd_ctx_t; -#define __rot(x, k) (((x) << (k)) | ((x) >> (32 - (k)))) -uint32_t -__random(rnd_ctx_t *x) -{ - uint32_t e = x->a - __rot(x->b, 27); - x->a = x->b ^ __rot(x->c, 17); - x->b = x->c + x->d; - x->c = x->d + e; - x->d = e + x->a; - return x->d; -} - -void -__random_seed(rnd_ctx_t *x, uint32_t seed) -{ - uint32_t i; - x->a = 0xf1ea5eed, x->b = x->c = x->d = seed; - for (i = 0; i < 20; ++i) { - (void)__random(x); - } -} - -void -shuffle(rnd_ctx_t *prng, int *array, size_t n) -{ - size_t i, j; - - if (n > 1) { - for (i = n - 1; i > 0; i--) { - j = (unsigned int)(__random(prng) % (i + 1)); - // XOR swap algorithm - if (i != j) { // avoid self-swap leading to zero-ing the element - array[i] = array[i] ^ array[j]; - array[j] = array[i] ^ array[j]; - array[i] = array[i] ^ array[j]; - } - } - } -} +#include "../tests/common.h" int main(void) { int i = 0; - rnd_ctx_t prng; int array[1024] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, @@ -105,17 +47,14 @@ main(void) 1010, 1011, 1012, 1013, 1014, 1015, 1016, 1017, 1018, 1019, 1020, 1021, 1022, 1023, 1024 }; // disable buffering - setbuf(stderr, 0); + setvbuf(stdout, NULL, _IONBF, 0); // Disable buffering for stdout + setvbuf(stderr, NULL, _IONBF, 0); // Disable buffering for stdout // seed the PRNG -#ifdef SEED - __random_seed(&prng, 8675309); -#else - __random_seed(&prng, (unsigned int)time(NULL) ^ getpid()); -#endif + xorshift32_seed(); // randomize setting the bits on - shuffle(&prng, array, 1024); + shuffle(array, 1024); // start with a 1KiB buffer, 1024 bits uint8_t *buf = calloc(1024, sizeof(uint8_t)); diff --git a/examples/ex_4.c b/examples/ex_4.c index cfd9b56..16be16f 100644 --- a/examples/ex_4.c +++ b/examples/ex_4.c @@ -1,23 +1,17 @@ #include -#include -#include #include #include -#include #include #include "../include/sparsemap.h" - -#define EXAMPLE_CODE -#include "../tests/common.c" +#include "../tests/common.h" #define TEST_ARRAY_SIZE 1024 int main(void) { - int i = 0; - size_t rank; + int i; int array[TEST_ARRAY_SIZE]; xorshift32_seed(); @@ -27,7 +21,7 @@ main(void) setvbuf(stderr, NULL, _IONBF, 0); // Disable buffering for stdout // start with a 3KiB buffer, TEST_ARRAY_SIZE bits - uint8_t *buf = calloc(3 * 1024, sizeof(uint8_t)); + uint8_t *buf = calloc((size_t)3 * 1024, sizeof(uint8_t)); // create the sparse bitmap sparsemap_t *map = sparsemap(buf, sizeof(uint8_t) * 3 * 1024, 0); @@ -58,14 +52,14 @@ main(void) __diag("================> %lu\n", len); sparsemap_clear(map); // set all the bits on in a random order - ensure_sequential_set(array, TEST_ARRAY_SIZE, len); + ensure_sequential_set(array, TEST_ARRAY_SIZE, (int)len); shuffle(array, TEST_ARRAY_SIZE); print_spans(array, TEST_ARRAY_SIZE); for (i = 0; i < TEST_ARRAY_SIZE; i++) { sparsemap_set(map, array[i], true); assert(sparsemap_is_set(map, array[i]) == true); } - has_span(map, array, TEST_ARRAY_SIZE, len); + has_span(map, array, TEST_ARRAY_SIZE, (int)len); size_t l = sparsemap_span(map, 0, len); if (l != (size_t)-1) { __diag("Found span in map starting at %lu of length %lu\n", l, len); @@ -76,9 +70,9 @@ main(void) if (set) { __diag("verified %d was set\n", i); } else { - __diag("darn, %d was not really set, %s\n", i, was_set(i, array) ? "but we thought it was" : "because it wasn't"); + __diag("darn, %d was not really set, %s\n", i, is_set(array, i) ? "but we thought it was" : "because it wasn't"); } - } while (++i < l + len); + } while (++i < (int)(l + len)); } else { __diag("UNABLE TO FIND SPAN in map of length %lu\n", len); } diff --git a/src/sparsemap.c b/src/sparsemap.c index 8cfeaca..c5c6968 100644 --- a/src/sparsemap.c +++ b/src/sparsemap.c @@ -16,7 +16,6 @@ */ #include -#include #include #include #include @@ -33,8 +32,8 @@ void __attribute__((format(printf, 4, 5))) __sm_diag_(const char *file, int line, const char *func, const char *format, ...) { va_list args; - va_start(args, format); fprintf(stderr, "%s:%d:%s(): ", file, line, func); + va_start(args, format); vfprintf(stderr, format, args); va_end(args); } @@ -433,6 +432,7 @@ static size_t __sm_chunk_map_rank(__sm_chunk_t *map, size_t first, size_t last, size_t *after) { size_t ret = 0; + (void)first; // TODO register uint8_t *p = (uint8_t *)map->m_data; for (size_t i = 0; i < sizeof(sm_bitvec_t); i++, p++) { @@ -499,7 +499,7 @@ __sm_chunk_map_rank(__sm_chunk_t *map, size_t first, size_t last, size_t *after) *after = 0; } } - for (size_t k = ks; k < last; k++) { + for (size_t k = ks; k < last && k < sizeof(sm_bitvec_t); k++) { if (w & ((sm_bitvec_t)1 << k)) { ret++; } @@ -946,7 +946,7 @@ sparsemap_set(sparsemap_t *map, size_t idx, bool value) break; case SM_NEEDS_TO_GROW: if (!dont_grow) { - offset += sizeof(sm_idx_t) + position * sizeof(sm_bitvec_t); + offset += (ssize_t)(sizeof(sm_idx_t) + position * sizeof(sm_bitvec_t)); __sm_insert_data(map, offset, (uint8_t *)&fill, sizeof(sm_bitvec_t)); } code = __sm_chunk_map_set(&chunk, idx - start, value, &position, &fill, true); @@ -959,7 +959,7 @@ sparsemap_set(sparsemap_t *map, size_t idx, bool value) __sm_remove_data(map, offset, sizeof(sm_idx_t) + sizeof(sm_bitvec_t) * 2); __sm_set_chunk_map_count(map, __sm_get_chunk_map_count(map) - 1); } else { - offset += sizeof(sm_idx_t) + position * sizeof(sm_bitvec_t); + offset += (ssize_t)(sizeof(sm_idx_t) + position * sizeof(sm_bitvec_t)); __sm_remove_data(map, offset, sizeof(sm_bitvec_t)); } break; @@ -979,8 +979,9 @@ sparsemap_set(sparsemap_t *map, size_t idx, bool value) sm_idx_t sparsemap_get_start_offset(sparsemap_t *map) { - if (__sm_get_chunk_map_count(map) == 0) + if (__sm_get_chunk_map_count(map) == 0) { return (0); + } return (*(sm_idx_t *)__sm_get_chunk_map_data(map, 0)); } @@ -1148,7 +1149,7 @@ size_t sparsemap_select(sparsemap_t *map, size_t n) { assert(sparsemap_get_size(map) >= SM_SIZEOF_OVERHEAD); - size_t result = 0; + size_t result; size_t count = __sm_get_chunk_map_count(map); uint8_t *p = __sm_get_chunk_map_data(map, 0); @@ -1205,7 +1206,8 @@ sparsemap_rank(sparsemap_t *map, size_t first, size_t last) size_t sparsemap_span(sparsemap_t *map, size_t loc, size_t len) { - size_t offset, nth = 0, count = 0; + size_t offset, nth = 0, count; + (void)loc; // TODO offset = sparsemap_select(map, 0); if (len == 1) { diff --git a/tests/common.c b/tests/common.c index 3f72861..57a7f39 100644 --- a/tests/common.c +++ b/tests/common.c @@ -1,3 +1,14 @@ +#include + +#include +#include +#include +#include +#include +#include + +#include "../include/sparsemap.h" +#include "common.h" #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wvariadic-macros" @@ -8,51 +19,37 @@ } while (0) #pragma GCC diagnostic pop -#ifdef EXAMPLE_CODE -int __prng = 0; +int __xorshift32_state = 0; // Xorshift algorithm for PRNG uint32_t xorshift32() { - uint32_t x = *state = &__prng; + uint32_t x = __xorshift32_state; if (x == 0) x = 123456789; x ^= x << 13; x ^= x >> 17; x ^= x << 5; - *state = x; + __xorshift32_state = x; return x; } void xorshift32_seed() { - // Seed the PRNG -#ifdef STABLE_SEED - __prng = 8675309; -#else - __prng = (unsigned int)time(NULL) ^ getpid(); -#endif + __xorshift32_state = XORSHIFT_SEED_VALUE; } -#else -#define xorshift32 munit_rand_uint32 -#endif void shuffle(int *array, size_t n) { - size_t i, j; - - if (n > 1) { - for (i = n - 1; i > 0; i--) { - j = (unsigned int)(xorshift32() % (i + 1)); - // XOR swap algorithm - if (i != j) { // avoid self-swap leading to zero-ing the element - array[i] = array[i] ^ array[j]; - array[j] = array[i] ^ array[j]; - array[i] = array[i] ^ array[j]; - } + for (size_t i = n - 1; i > 0; --i) { + size_t j = xorshift32() % (i + 1); + if (i != j) { + array[i] ^= array[j]; + array[j] ^= array[i]; + array[i] ^= array[j]; } } } @@ -65,10 +62,10 @@ compare_ints(const void *a, const void *b) // Check if there's already a sequence of 'r' sequential integers int -has_sequential_set(int *a, size_t l, int r) +has_sequential_set(int a[], int l, int r) { int count = 1; // Start with a count of 1 for the first number - for (size_t i = 1; i < l; ++i) { + for (int i = 1; i < l; ++i) { if (a[i] - a[i - 1] == 1) { // Check if the current and previous elements are sequential count++; if (count >= r) @@ -82,10 +79,10 @@ has_sequential_set(int *a, size_t l, int r) // Function to ensure an array contains a set of 'r' sequential integers void -ensure_sequential_set(int *a, size_t l, int r) +ensure_sequential_set(int *a, int l, int r) { - if (r > l) - return; // If 'r' is greater than array length, cannot satisfy the condition + if (!a || l == 0 || r > l) + return; // Sort the array to check for existing sequences qsort(a, l, sizeof(int), compare_ints); @@ -100,10 +97,10 @@ ensure_sequential_set(int *a, size_t l, int r) int max_value = a[l - 1]; // Generate a random value between min_value and max_value - int value = xorshift32() % (max_value - min_value - r + 1); + int value = random_uint32() % (max_value - min_value - r + 1); // Generate a random location between 0 and l - r - int offset = xorshift32() % (l + r + 1); + int offset = random_uint32() % (l + r + 1); // Adjust the array to include a sequential set of 'r' integers at the random offset for (int i = 0; i < r; ++i) { @@ -112,24 +109,24 @@ ensure_sequential_set(int *a, size_t l, int r) } void -print_array(int *array, size_t l) +print_array(int *array, int l) { int a[l]; memcpy(a, array, sizeof(int) * l); qsort(a, l, sizeof(int), compare_ints); - printf("int a[] = {"); + fprintf(stderr, "int a[] = {"); for (int i = 0; i < l; i++) { - printf("%d", a[i]); - if (i != l) { - printf(", "); + fprintf(stderr, "%d", a[i]); + if (i != l - 1) { + fprintf(stderr, ", "); } } - printf("};\n"); + fprintf(stderr, "};\n"); } bool -has_span(sparsemap_t *map, int *array, size_t l, size_t n) +has_span(sparsemap_t *map, int *array, int l, int n) { if (n == 0 || l == 0 || n > l) { return false; @@ -139,21 +136,14 @@ has_span(sparsemap_t *map, int *array, size_t l, size_t n) memcpy(sorted, array, sizeof(int) * l); qsort(sorted, l, sizeof(int), compare_ints); - for (size_t i = 0; i <= l - n; i++) { + for (int i = 0; i <= l - n; i++) { if (sorted[i] + n - 1 == sorted[i + n - 1]) { -#if 0 - fprintf(stderr, "Found span: "); - for (size_t j = i; j < i + n; j++) { - fprintf(stderr, "%d ", sorted[j]); - } - fprintf(stderr, "\n"); -#endif - for (size_t j = 0; j < n; j++) { + for (int j = 0; j < n; j++) { size_t pos = sorted[j + i]; bool set = sparsemap_is_set(map, pos); assert(set); } - __diag("Found span: [%d, %d], length: %zu\n", sorted[i], sorted[i + n - 1], n); + __diag("Found span: [%d, %d], length: %d\n", sorted[i], sorted[i + n - 1], n); return true; } } @@ -162,7 +152,7 @@ has_span(sparsemap_t *map, int *array, size_t l, size_t n) } bool -is_span(int *array, size_t n, int x, int l) +is_span(int *array, int n, int x, int l) { if (n == 0 || l < 0) { return false; @@ -173,7 +163,7 @@ is_span(int *array, size_t n, int x, int l) qsort(a, n, sizeof(int), compare_ints); // Iterate through the array to find a span starting at x of length l - for (size_t i = 0; i < n; i++) { + for (int i = 0; i < n; i++) { if (a[i] == x) { // Check if the span can fit in the array if (i + l - 1 < n && a[i + l - 1] == x + l - 1) { @@ -185,7 +175,7 @@ is_span(int *array, size_t n, int x, int l) } void -print_spans(int *array, size_t n) +print_spans(int *array, int n) { int a[n]; size_t start = 0, end = 0; @@ -198,7 +188,7 @@ print_spans(int *array, size_t n) memcpy(a, array, sizeof(int) * n); qsort(a, n, sizeof(int), compare_ints); - for (size_t i = 1; i < n; i++) { + for (int i = 1; i < n; i++) { if (a[i] == a[i - 1] + 1) { end = i; // Extend the span } else { @@ -223,7 +213,7 @@ print_spans(int *array, size_t n) } bool -was_set(size_t bit, const int array[]) +is_set(const int array[], int bit) { for (int i = 0; i < 1024; i++) { if (array[i] == (int)bit) { @@ -234,9 +224,9 @@ was_set(size_t bit, const int array[]) } int -is_unique(int a[], size_t l, int value) +is_unique(int a[], int l, int value) { - for (size_t i = 0; i < l; ++i) { + for (int i = 0; i < l; ++i) { if (a[i] == value) { return 0; // Not unique } @@ -245,16 +235,32 @@ is_unique(int a[], size_t l, int value) } void -setup_test_array(int a[], size_t l, int max_value) +setup_test_array(int a[], int l, int max_value) { if (a == NULL || max_value < 0) return; // Basic error handling and validation - for (size_t i = 0; i < l; ++i) { + for (int i = 0; i < l; ++i) { int candidate; do { - candidate = xorshift32() % (max_value + 1); // Generate a new value within the specified range - } while (!is_unique(a, i, candidate)); // Repeat until a unique value is found - a[i] = candidate; // Assign the unique value to the array + candidate = random_uint32() % (max_value + 1); // Generate a new value within the specified range + } while (!is_unique(a, i, candidate)); // Repeat until a unique value is found + a[i] = candidate; // Assign the unique value to the array } } + +void +bitmap_from_uint32(sparsemap_t *map, uint32_t number) { + for (int i = 0; i < 32; ++i) { + bool bit = number & (1 << i); + sparsemap_set(map, i, bit); + } +} + +void +bitmap_from_uint64(sparsemap_t *map, uint64_t number) { + for (int i = 0; i < 64; ++i) { + bool bit = number & (1 << i); + sparsemap_set(map, i, bit); + } +} diff --git a/tests/test.c b/tests/test.c index d1ededb..63a2ee2 100644 --- a/tests/test.c +++ b/tests/test.c @@ -10,31 +10,28 @@ #define MUNIT_NO_FORK (1) #define MUNIT_ENABLE_ASSERT_ALIASES (1) -#include - -#include -#include -#include #include #include #include "../include/sparsemap.h" +#include "common.h" #include "munit.h" #if defined(_MSC_VER) #pragma warning(disable : 4127) #endif -#include "common.c" - -struct user_data { }; +struct user_data { + int foo; +}; void -__populate_map(sparsemap_t *map, size_t size, size_t max_value) +populate_map(sparsemap_t *map, int size, int max_value) { int array[size]; setup_test_array(array, size, max_value); + ensure_sequential_set(array, size, 10); shuffle(array, size); for (int i = 0; i < size; i++) { sparsemap_set(map, array[i], true); @@ -46,6 +43,7 @@ static void * test_api_setup(const MunitParameter params[], void *user_data) { struct test_info *info = (struct test_info *)user_data; + (void)info; (void)params; sparsemap_t *map = munit_calloc(1, sizeof(sparsemap_t)); assert_ptr_not_null(map); @@ -70,7 +68,7 @@ test_api_static_init(const MunitParameter params[], void *data) (void)data; assert_ptr_not_null(map); - sparsemap_init(map, buf, sizeof(buf) / sizeof(buf[0]), 0); + sparsemap_init(map, buf, 1024, 0); assert_ptr_equal(&buf, map->m_data); assert_true(map->m_data_size == 1024); assert_true(map->m_data_used == sizeof(uint32_t)); @@ -85,7 +83,7 @@ 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); + populate_map(map, 1024, 3 * 1024); return (void *)map; } @@ -121,8 +119,7 @@ test_api_open_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); - assert_true(map->m_data_used == 1024 + sizeof(uint32_t)); + populate_map(map, 1024, 3 * 1024); return (void *)map; } @@ -142,7 +139,6 @@ test_api_open(const MunitParameter params[], void *data) assert_ptr_not_null(map); sparsemap_open(sm, map->m_data, map->m_data_size); - assert_true(map->m_data_used == sm->m_data_used); for (int i = 0; i < 3 * 1024; i++) { assert_true(sparsemap_is_set(sm, i) == sparsemap_is_set(map, i)); } @@ -157,7 +153,7 @@ test_api_set_data_size_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); + populate_map(map, 1024, 3 * 1024); return (void *)map; } @@ -183,6 +179,40 @@ test_api_set_data_size(const MunitParameter params[], void *data) return MUNIT_OK; } +static void * +test_api_get_range_size_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); + populate_map(map, 1024, 3 * 1024); + + return (void *)map; +} +static void +test_api_get_range_size_tear_down(void *fixture) +{ + sparsemap_t *map = (sparsemap_t *)fixture; + free(map->m_data); + test_api_tear_down(fixture); +} +static MunitResult +test_api_get_range_size(const MunitParameter params[], void *data) +{ + sparsemap_t *map = (sparsemap_t *)data; + (void)params; + + assert_ptr_not_null(map); + + sparsemap_set(map, 42, true); + assert_true(sparsemap_is_set(map, 42)); + size_t size = sparsemap_get_range_size(map); + assert_true(size == 1024); + + return MUNIT_OK; +} + static void * test_api_is_set_setup(const MunitParameter params[], void *user_data) { @@ -190,7 +220,7 @@ test_api_is_set_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); + populate_map(map, 1024, 3 * 1024); return (void *)map; } @@ -215,10 +245,204 @@ test_api_is_set(const MunitParameter params[], void *data) return MUNIT_OK; } +static void * +test_api_set_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_set_tear_down(void *fixture) +{ + sparsemap_t *map = (sparsemap_t *)fixture; + free(map->m_data); + test_api_tear_down(fixture); +} +static MunitResult +test_api_set(const MunitParameter params[], void *data) +{ + sparsemap_t *map = (sparsemap_t *)data; + (void)params; + + assert_ptr_not_null(map); + + assert_false(sparsemap_is_set(map, 1)); + assert_false(sparsemap_is_set(map, 8192)); + sparsemap_set(map, 1, true); + sparsemap_set(map, 8192, true); + assert_true(sparsemap_is_set(map, 1)); + assert_true(sparsemap_is_set(map, 8192)); + sparsemap_set(map, 1, false); + sparsemap_set(map, 8192, false); + assert_false(sparsemap_is_set(map, 1)); + assert_false(sparsemap_is_set(map, 8192)); + + return MUNIT_OK; +} + +static void * +test_api_get_start_offset_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); + populate_map(map, 1024, 3 * 1024); + + return (void *)map; +} +static void +test_api_get_start_offset_tear_down(void *fixture) +{ + sparsemap_t *map = (sparsemap_t *)fixture; + free(map->m_data); + test_api_tear_down(fixture); +} +static MunitResult +test_api_get_start_offset(const MunitParameter params[], void *data) +{ + sparsemap_t *map = (sparsemap_t *)data; + (void)params; + + assert_ptr_not_null(map); + + sparsemap_set(map, 42, true); + assert_true(sparsemap_is_set(map, 42)); + size_t offset = sparsemap_get_start_offset(map); + assert_true(offset == 0); + + return MUNIT_OK; +} + +static void * +test_api_get_size_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); + populate_map(map, 1024, 3 * 1024); + + return (void *)map; +} +static void +test_api_get_size_tear_down(void *fixture) +{ + sparsemap_t *map = (sparsemap_t *)fixture; + free(map->m_data); + test_api_tear_down(fixture); +} +static MunitResult +test_api_get_size(const MunitParameter params[], void *data) +{ + sparsemap_t *map = (sparsemap_t *)data; + (void)params; + + assert_ptr_not_null(map); + + size_t size = sparsemap_get_size(map); + assert_true(size > 400); + + return MUNIT_OK; +} + +static void * +test_api_scan_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); + bitmap_from_uint64(map, ((uint64_t)0xfeedface << 32) | 0xbadc0ffee); + + return (void *)map; +} +static void +test_api_scan_tear_down(void *fixture) +{ + sparsemap_t *map = (sparsemap_t *)fixture; + free(map->m_data); + test_api_tear_down(fixture); +} +void +scan_for_0xfeedfacebadcoffee(sm_idx_t v[], size_t n) { + /* Called multiple times */ + ((void)v); + ((void)n); +} +static MunitResult +test_api_scan(const MunitParameter params[], void *data) +{ + sparsemap_t *map = (sparsemap_t *)data; + (void)params; + + assert_ptr_not_null(map); + + sparsemap_set(map, 4200, true); + assert_true(sparsemap_is_set(map, 42)); + sparsemap_scan(map, scan_for_0xfeedfacebadcoffee, 0); + + return MUNIT_OK; +} + +static void * +test_api_split_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); + for(int i = 0; i < 1024; i ++) { + sparsemap_set(map, i, true); + } + return (void *)map; +} +static void +test_api_split_tear_down(void *fixture) +{ + sparsemap_t *map = (sparsemap_t *)fixture; + free(map->m_data); + test_api_tear_down(fixture); +} +static MunitResult +test_api_split(const MunitParameter params[], void *data) +{ + sparsemap_t *map = (sparsemap_t *)data; + uint8_t buf[1024]; + sparsemap_t portion; + (void)params; + + assert_ptr_not_null(map); + + sparsemap_init(&portion, buf, 512, 0); + sparsemap_split(map, 512, &portion); + for (int i = 0; i < 512; i++) { + assert_true(sparsemap_is_set(map, i)); + assert_false(sparsemap_is_set(&portion, i)); + } + for (int i = 513; i < 1024; i++) { + assert_false(sparsemap_is_set(map, i)); + assert_true(sparsemap_is_set(&portion, i)); + } + + 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/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/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 }, + { (char *)"/api/get_start_offset", test_api_get_start_offset, test_api_get_start_offset_setup, test_api_get_start_offset_tear_down, MUNIT_TEST_OPTION_NONE, NULL }, + { (char *)"/api/get_size", test_api_get_size, test_api_get_size_setup, test_api_get_size_tear_down, MUNIT_TEST_OPTION_NONE, NULL }, + { (char *)"/api/scan", test_api_scan, test_api_scan_setup, test_api_scan_tear_down, MUNIT_TEST_OPTION_NONE, NULL }, + { (char *)"/api/split", test_api_split, test_api_split_setup, test_api_split_tear_down, MUNIT_TEST_OPTION_NONE, NULL }, { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } }; static MunitTest scale_tests[] = { { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } };