more tests

This commit is contained in:
Gregory Burd 2024-04-08 18:14:47 -04:00
parent 938fde14c3
commit 306e790f8b
8 changed files with 343 additions and 164 deletions

View file

@ -1,5 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="Black">
<option name="executionMode" value="BINARY" />
<option name="pathToExecutable" value="$USER_HOME$/.nix-profile/bin/black" />
</component>
<component name="CidrRootsConfiguration"> <component name="CidrRootsConfiguration">
<sourceRoots> <sourceRoots>
<file path="$PROJECT_DIR$/examples" /> <file path="$PROJECT_DIR$/examples" />

8
.idea/modules.xml Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/sparsemap.iml" filepath="$PROJECT_DIR$/.idea/sparsemap.iml" />
</modules>
</component>
</project>

View file

@ -1,3 +1,5 @@
# Sparsemap
`sparsemap` is a sparse, compressed bitmap. In best case, it can store 2048 `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 bits in just 8 bytes. In worst case, it stores the 2048 bits uncompressed and
requires additional 8 bytes of overhead. 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 capacity is 2048, the sparsemap creates two chunk maps; the first starts at
offset 0, the second starts at offset 8192). offset 0, the second starts at offset 8192).
# Usage instructions ## Usage instructions
The file `examples/ex_1.c` has example code. The file `examples/ex_1.c` has example code.

View file

@ -6,70 +6,12 @@
#include <unistd.h> #include <unistd.h>
#include "../include/sparsemap.h" #include "../include/sparsemap.h"
#include "../tests/common.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];
}
}
}
}
int int
main(void) main(void)
{ {
int i = 0; 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, 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, 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, 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 }; 1010, 1011, 1012, 1013, 1014, 1015, 1016, 1017, 1018, 1019, 1020, 1021, 1022, 1023, 1024 };
// disable buffering // 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 // seed the PRNG
#ifdef SEED xorshift32_seed();
__random_seed(&prng, 8675309);
#else
__random_seed(&prng, (unsigned int)time(NULL) ^ getpid());
#endif
// randomize setting the bits on // randomize setting the bits on
shuffle(&prng, array, 1024); shuffle(array, 1024);
// start with a 1KiB buffer, 1024 bits // start with a 1KiB buffer, 1024 bits
uint8_t *buf = calloc(1024, sizeof(uint8_t)); uint8_t *buf = calloc(1024, sizeof(uint8_t));

View file

@ -1,23 +1,17 @@
#include <assert.h> #include <assert.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h>
#include <unistd.h> #include <unistd.h>
#include "../include/sparsemap.h" #include "../include/sparsemap.h"
#include "../tests/common.h"
#define EXAMPLE_CODE
#include "../tests/common.c"
#define TEST_ARRAY_SIZE 1024 #define TEST_ARRAY_SIZE 1024
int int
main(void) main(void)
{ {
int i = 0; int i;
size_t rank;
int array[TEST_ARRAY_SIZE]; int array[TEST_ARRAY_SIZE];
xorshift32_seed(); xorshift32_seed();
@ -27,7 +21,7 @@ main(void)
setvbuf(stderr, NULL, _IONBF, 0); // Disable buffering for stdout setvbuf(stderr, NULL, _IONBF, 0); // Disable buffering for stdout
// start with a 3KiB buffer, TEST_ARRAY_SIZE bits // 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 // create the sparse bitmap
sparsemap_t *map = sparsemap(buf, sizeof(uint8_t) * 3 * 1024, 0); sparsemap_t *map = sparsemap(buf, sizeof(uint8_t) * 3 * 1024, 0);
@ -58,14 +52,14 @@ main(void)
__diag("================> %lu\n", len); __diag("================> %lu\n", len);
sparsemap_clear(map); sparsemap_clear(map);
// set all the bits on in a random order // 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); shuffle(array, TEST_ARRAY_SIZE);
print_spans(array, TEST_ARRAY_SIZE); print_spans(array, TEST_ARRAY_SIZE);
for (i = 0; i < TEST_ARRAY_SIZE; i++) { for (i = 0; i < TEST_ARRAY_SIZE; i++) {
sparsemap_set(map, array[i], true); sparsemap_set(map, array[i], true);
assert(sparsemap_is_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); size_t l = sparsemap_span(map, 0, len);
if (l != (size_t)-1) { if (l != (size_t)-1) {
__diag("Found span in map starting at %lu of length %lu\n", l, len); __diag("Found span in map starting at %lu of length %lu\n", l, len);
@ -76,9 +70,9 @@ main(void)
if (set) { if (set) {
__diag("verified %d was set\n", i); __diag("verified %d was set\n", i);
} else { } 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 { } else {
__diag("UNABLE TO FIND SPAN in map of length %lu\n", len); __diag("UNABLE TO FIND SPAN in map of length %lu\n", len);
} }

View file

@ -16,7 +16,6 @@
*/ */
#include <assert.h> #include <assert.h>
#include <errno.h>
#include <popcount.h> #include <popcount.h>
#include <sparsemap.h> #include <sparsemap.h>
#include <stdbool.h> #include <stdbool.h>
@ -33,8 +32,8 @@
void __attribute__((format(printf, 4, 5))) __sm_diag_(const char *file, int line, const char *func, const char *format, ...) void __attribute__((format(printf, 4, 5))) __sm_diag_(const char *file, int line, const char *func, const char *format, ...)
{ {
va_list args; va_list args;
va_start(args, format);
fprintf(stderr, "%s:%d:%s(): ", file, line, func); fprintf(stderr, "%s:%d:%s(): ", file, line, func);
va_start(args, format);
vfprintf(stderr, format, args); vfprintf(stderr, format, args);
va_end(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) __sm_chunk_map_rank(__sm_chunk_t *map, size_t first, size_t last, size_t *after)
{ {
size_t ret = 0; size_t ret = 0;
(void)first; // TODO
register uint8_t *p = (uint8_t *)map->m_data; register uint8_t *p = (uint8_t *)map->m_data;
for (size_t i = 0; i < sizeof(sm_bitvec_t); i++, p++) { 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; *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)) { if (w & ((sm_bitvec_t)1 << k)) {
ret++; ret++;
} }
@ -946,7 +946,7 @@ sparsemap_set(sparsemap_t *map, size_t idx, bool value)
break; break;
case SM_NEEDS_TO_GROW: case SM_NEEDS_TO_GROW:
if (!dont_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)); __sm_insert_data(map, offset, (uint8_t *)&fill, sizeof(sm_bitvec_t));
} }
code = __sm_chunk_map_set(&chunk, idx - start, value, &position, &fill, true); 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_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); __sm_set_chunk_map_count(map, __sm_get_chunk_map_count(map) - 1);
} else { } 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)); __sm_remove_data(map, offset, sizeof(sm_bitvec_t));
} }
break; break;
@ -979,8 +979,9 @@ sparsemap_set(sparsemap_t *map, size_t idx, bool value)
sm_idx_t sm_idx_t
sparsemap_get_start_offset(sparsemap_t *map) 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 (0);
}
return (*(sm_idx_t *)__sm_get_chunk_map_data(map, 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) sparsemap_select(sparsemap_t *map, size_t n)
{ {
assert(sparsemap_get_size(map) >= SM_SIZEOF_OVERHEAD); assert(sparsemap_get_size(map) >= SM_SIZEOF_OVERHEAD);
size_t result = 0; size_t result;
size_t count = __sm_get_chunk_map_count(map); size_t count = __sm_get_chunk_map_count(map);
uint8_t *p = __sm_get_chunk_map_data(map, 0); 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 size_t
sparsemap_span(sparsemap_t *map, size_t loc, size_t len) 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); offset = sparsemap_select(map, 0);
if (len == 1) { if (len == 1) {

View file

@ -1,3 +1,14 @@
#include <sys/types.h>
#include <assert.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "../include/sparsemap.h"
#include "common.h"
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wvariadic-macros" #pragma GCC diagnostic ignored "-Wvariadic-macros"
@ -8,51 +19,37 @@
} while (0) } while (0)
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#ifdef EXAMPLE_CODE int __xorshift32_state = 0;
int __prng = 0;
// Xorshift algorithm for PRNG // Xorshift algorithm for PRNG
uint32_t uint32_t
xorshift32() xorshift32()
{ {
uint32_t x = *state = &__prng; uint32_t x = __xorshift32_state;
if (x == 0) if (x == 0)
x = 123456789; x = 123456789;
x ^= x << 13; x ^= x << 13;
x ^= x >> 17; x ^= x >> 17;
x ^= x << 5; x ^= x << 5;
*state = x; __xorshift32_state = x;
return x; return x;
} }
void void
xorshift32_seed() xorshift32_seed()
{ {
// Seed the PRNG __xorshift32_state = XORSHIFT_SEED_VALUE;
#ifdef STABLE_SEED
__prng = 8675309;
#else
__prng = (unsigned int)time(NULL) ^ getpid();
#endif
} }
#else
#define xorshift32 munit_rand_uint32
#endif
void void
shuffle(int *array, size_t n) shuffle(int *array, size_t n)
{ {
size_t i, j; for (size_t i = n - 1; i > 0; --i) {
size_t j = xorshift32() % (i + 1);
if (n > 1) { if (i != j) {
for (i = n - 1; i > 0; i--) { array[i] ^= array[j];
j = (unsigned int)(xorshift32() % (i + 1)); array[j] ^= array[i];
// XOR swap algorithm array[i] ^= array[j];
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];
}
} }
} }
} }
@ -65,10 +62,10 @@ 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 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 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 if (a[i] - a[i - 1] == 1) { // Check if the current and previous elements are sequential
count++; count++;
if (count >= r) 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 // Function to ensure an array contains a set of 'r' sequential integers
void void
ensure_sequential_set(int *a, size_t l, int r) ensure_sequential_set(int *a, int l, int r)
{ {
if (r > l) if (!a || l == 0 || r > l)
return; // If 'r' is greater than array length, cannot satisfy the condition return;
// 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);
@ -100,10 +97,10 @@ ensure_sequential_set(int *a, size_t l, int r)
int max_value = a[l - 1]; int max_value = a[l - 1];
// Generate a random value between min_value and max_value // 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 // 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 // Adjust the array to include a sequential set of 'r' integers at the random offset
for (int i = 0; i < r; ++i) { for (int i = 0; i < r; ++i) {
@ -112,24 +109,24 @@ ensure_sequential_set(int *a, size_t l, int r)
} }
void void
print_array(int *array, size_t l) print_array(int *array, int l)
{ {
int a[l]; int a[l];
memcpy(a, array, sizeof(int) * l); memcpy(a, array, sizeof(int) * l);
qsort(a, l, sizeof(int), compare_ints); qsort(a, l, sizeof(int), compare_ints);
printf("int a[] = {"); fprintf(stderr, "int a[] = {");
for (int i = 0; i < l; i++) { for (int i = 0; i < l; i++) {
printf("%d", a[i]); fprintf(stderr, "%d", a[i]);
if (i != l) { if (i != l - 1) {
printf(", "); fprintf(stderr, ", ");
} }
} }
printf("};\n"); fprintf(stderr, "};\n");
} }
bool 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) { if (n == 0 || l == 0 || n > l) {
return false; 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); memcpy(sorted, array, sizeof(int) * l);
qsort(sorted, l, sizeof(int), compare_ints); 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 (sorted[i] + n - 1 == sorted[i + n - 1]) {
#if 0 for (int j = 0; j < n; j++) {
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++) {
size_t pos = sorted[j + i]; size_t pos = sorted[j + i];
bool set = sparsemap_is_set(map, pos); bool set = sparsemap_is_set(map, pos);
assert(set); 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; return true;
} }
} }
@ -162,7 +152,7 @@ has_span(sparsemap_t *map, int *array, size_t l, size_t n)
} }
bool 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) { if (n == 0 || l < 0) {
return false; return false;
@ -173,7 +163,7 @@ is_span(int *array, size_t n, int x, int l)
qsort(a, n, sizeof(int), compare_ints); qsort(a, n, sizeof(int), compare_ints);
// Iterate through the array to find a span starting at x of length l // 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) { if (a[i] == x) {
// Check if the span can fit in the array // Check if the span can fit in the array
if (i + l - 1 < n && a[i + l - 1] == x + l - 1) { 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 void
print_spans(int *array, size_t n) print_spans(int *array, int n)
{ {
int a[n]; int a[n];
size_t start = 0, end = 0; size_t start = 0, end = 0;
@ -198,7 +188,7 @@ print_spans(int *array, size_t n)
memcpy(a, array, sizeof(int) * n); memcpy(a, array, sizeof(int) * n);
qsort(a, n, sizeof(int), compare_ints); 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) { if (a[i] == a[i - 1] + 1) {
end = i; // Extend the span end = i; // Extend the span
} else { } else {
@ -223,7 +213,7 @@ print_spans(int *array, size_t n)
} }
bool bool
was_set(size_t bit, const int array[]) is_set(const int array[], int bit)
{ {
for (int i = 0; i < 1024; i++) { for (int i = 0; i < 1024; i++) {
if (array[i] == (int)bit) { if (array[i] == (int)bit) {
@ -234,9 +224,9 @@ was_set(size_t bit, const int array[])
} }
int 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) { if (a[i] == value) {
return 0; // Not unique return 0; // Not unique
} }
@ -245,16 +235,32 @@ 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[], int l, int max_value)
{ {
if (a == NULL || max_value < 0) if (a == NULL || max_value < 0)
return; // Basic error handling and validation return; // Basic error handling and validation
for (size_t i = 0; i < l; ++i) { for (int i = 0; i < l; ++i) {
int candidate; int candidate;
do { do {
candidate = xorshift32() % (max_value + 1); // Generate a new value within the specified range 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 } while (!is_unique(a, i, candidate)); // Repeat until a unique value is found
a[i] = candidate; // Assign the unique value to the array 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);
}
}

View file

@ -10,31 +10,28 @@
#define MUNIT_NO_FORK (1) #define MUNIT_NO_FORK (1)
#define MUNIT_ENABLE_ASSERT_ALIASES (1) #define MUNIT_ENABLE_ASSERT_ALIASES (1)
#include <sys/types.h>
#include <assert.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include "../include/sparsemap.h" #include "../include/sparsemap.h"
#include "common.h"
#include "munit.h" #include "munit.h"
#if defined(_MSC_VER) #if defined(_MSC_VER)
#pragma warning(disable : 4127) #pragma warning(disable : 4127)
#endif #endif
#include "common.c" struct user_data {
int foo;
struct user_data { }; };
void 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]; int array[size];
setup_test_array(array, size, max_value); setup_test_array(array, size, max_value);
ensure_sequential_set(array, size, 10);
shuffle(array, size); shuffle(array, size);
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
sparsemap_set(map, array[i], true); sparsemap_set(map, array[i], true);
@ -46,6 +43,7 @@ static void *
test_api_setup(const MunitParameter params[], void *user_data) 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)info;
(void)params; (void)params;
sparsemap_t *map = munit_calloc(1, sizeof(sparsemap_t)); sparsemap_t *map = munit_calloc(1, sizeof(sparsemap_t));
assert_ptr_not_null(map); assert_ptr_not_null(map);
@ -70,7 +68,7 @@ test_api_static_init(const MunitParameter params[], void *data)
(void)data; (void)data;
assert_ptr_not_null(map); 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_ptr_equal(&buf, map->m_data);
assert_true(map->m_data_size == 1024); assert_true(map->m_data_size == 1024);
assert_true(map->m_data_used == sizeof(uint32_t)); 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_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); populate_map(map, 1024, 3 * 1024);
return (void *)map; 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_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); populate_map(map, 1024, 3 * 1024);
assert_true(map->m_data_used == 1024 + sizeof(uint32_t));
return (void *)map; return (void *)map;
} }
@ -142,7 +139,6 @@ test_api_open(const MunitParameter params[], void *data)
assert_ptr_not_null(map); assert_ptr_not_null(map);
sparsemap_open(sm, map->m_data, map->m_data_size); 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++) { for (int i = 0; i < 3 * 1024; i++) {
assert_true(sparsemap_is_set(sm, i) == sparsemap_is_set(map, 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_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); populate_map(map, 1024, 3 * 1024);
return (void *)map; return (void *)map;
} }
@ -183,6 +179,40 @@ test_api_set_data_size(const MunitParameter params[], void *data)
return MUNIT_OK; 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 * static void *
test_api_is_set_setup(const MunitParameter params[], void *user_data) 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_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); populate_map(map, 1024, 3 * 1024);
return (void *)map; return (void *)map;
} }
@ -215,10 +245,204 @@ test_api_is_set(const MunitParameter params[], void *data)
return MUNIT_OK; 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 }, 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/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/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/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 } }; { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } };
static MunitTest scale_tests[] = { { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } }; static MunitTest scale_tests[] = { { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } };