test suite for the sparse bitmap data structure (#1)
Reviewed-on: #1 Co-authored-by: Greg Burd <greg@burd.me> Co-committed-by: Greg Burd <greg@burd.me>
This commit is contained in:
parent
f857692c3c
commit
3bdcf6914a
|
@ -1,5 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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">
|
||||
<sourceRoots>
|
||||
<file path="$PROJECT_DIR$/examples" />
|
||||
|
|
8
.idea/modules.xml
Normal file
8
.idea/modules.xml
Normal 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>
|
3
LICENSE
3
LICENSE
|
@ -1,6 +1,7 @@
|
|||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014 Christoph Rupp
|
||||
Copyright (c) 2014 Christoph Rupp <chris@crupp.de>.
|
||||
Copyright (c) 2024 Gregory Burd <greg@burd.me>.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
|
|
@ -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.
|
||||
|
||||
|
|
|
@ -12,7 +12,14 @@
|
|||
} while (0)
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
// NOTE: currently, this code serves as a sample and unittest.
|
||||
|
||||
/* !!! Duplicated here for testing purposes. Keep in sync, or suffer. !!! */
|
||||
struct sparsemap {
|
||||
uint8_t *m_data;
|
||||
size_t m_capacity;
|
||||
size_t m_data_used;
|
||||
};
|
||||
|
||||
|
||||
int
|
||||
main()
|
||||
|
@ -23,7 +30,7 @@ main()
|
|||
sparsemap_t mmap, *map = &mmap;
|
||||
uint8_t buffer[1024];
|
||||
uint8_t buffer2[1024];
|
||||
sparsemap_init(map, buffer, sizeof(buffer), 0);
|
||||
sparsemap_init(map, buffer, sizeof(buffer));
|
||||
assert(sparsemap_get_size(map) == size);
|
||||
sparsemap_set(map, 0, true);
|
||||
assert(sparsemap_get_size(map) == size + 4 + 8 + 8);
|
||||
|
@ -155,7 +162,7 @@ main()
|
|||
|
||||
// split and move, aligned to MiniMap capacity
|
||||
sparsemap_t _sm2, *sm2 = &_sm2;
|
||||
sparsemap_init(sm2, buffer2, sizeof(buffer2), 0);
|
||||
sparsemap_init(sm2, buffer2, sizeof(buffer2));
|
||||
sparsemap_clear(sm2);
|
||||
for (int i = 0; i < 2048 * 2; i++) {
|
||||
sparsemap_set(map, i, true);
|
||||
|
@ -172,7 +179,7 @@ main()
|
|||
fprintf(stderr, ".");
|
||||
|
||||
// split and move, aligned to BitVector capacity
|
||||
sparsemap_init(sm2, buffer2, sizeof(buffer2), 0);
|
||||
sparsemap_init(sm2, buffer2, sizeof(buffer2));
|
||||
sparsemap_clear(map);
|
||||
for (int i = 0; i < 2048 * 3; i++) {
|
||||
sparsemap_set(map, i, true);
|
||||
|
|
|
@ -30,7 +30,7 @@ main(void)
|
|||
uint8_t *buf = calloc(1024, sizeof(uint8_t));
|
||||
|
||||
// create the sparse bitmap
|
||||
sparsemap_t *map = sparsemap(buf, sizeof(uint8_t) * 1024, 0);
|
||||
sparsemap_t *map = sparsemap(buf, sizeof(uint8_t) * 1024);
|
||||
|
||||
// Set every other bit (pathologically worst case) to see what happens
|
||||
// when the map is full.
|
||||
|
|
|
@ -6,70 +6,12 @@
|
|||
#include <unistd.h>
|
||||
|
||||
#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,23 +47,20 @@ 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));
|
||||
|
||||
// create the sparse bitmap
|
||||
sparsemap_t *map = sparsemap(buf, sizeof(uint8_t) * 1024, 0);
|
||||
sparsemap_t *map = sparsemap(buf, sizeof(uint8_t) * 1024);
|
||||
|
||||
// set all the bits on in a random order
|
||||
for (i = 0; i < 1024; i++) {
|
||||
|
|
294
examples/ex_4.c
294
examples/ex_4.c
|
@ -1,286 +1,38 @@
|
|||
#include <assert.h>
|
||||
#include <ctype.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#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
|
||||
|
||||
uint32_t
|
||||
xorshift(int *state)
|
||||
{
|
||||
if (!state) {
|
||||
return 0;
|
||||
}
|
||||
// Xorshift algorithm
|
||||
uint32_t x = *state; // Dereference state to get the current state value
|
||||
if (x == 0) x = 123456789; // Ensure the state is never zero; use a default seed if so
|
||||
x ^= x << 13;
|
||||
x ^= x >> 17;
|
||||
x ^= x << 5;
|
||||
*state = x; // Update the state
|
||||
return x; // Return the new state as the next pseudo-random number
|
||||
}
|
||||
|
||||
void
|
||||
shuffle(int *array, size_t n, int *prng)
|
||||
{
|
||||
size_t i, j;
|
||||
|
||||
if (n > 1) {
|
||||
for (i = n - 1; i > 0; i--) {
|
||||
j = (unsigned int)(xorshift(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
|
||||
compare_ints(const void *a, const void *b)
|
||||
{
|
||||
return *(const int *)a - *(const int *)b;
|
||||
}
|
||||
|
||||
// Check if there's already a sequence of 'r' sequential integers
|
||||
int has_sequential_set(int *a, size_t l, int r) {
|
||||
int count = 1; // Start with a count of 1 for the first number
|
||||
for (size_t 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) return 1; // Found a sequential set of length 'r'
|
||||
} else {
|
||||
count = 1; // Reset count if the sequence breaks
|
||||
}
|
||||
}
|
||||
return 0; // No sequential set of length 'r' found
|
||||
}
|
||||
|
||||
// Function to ensure an array contains a set of 'r' sequential integers
|
||||
void ensure_sequential_set(int *a, size_t l, int r, uint32_t *prng) {
|
||||
if (r > l) return; // If 'r' is greater than array length, cannot satisfy the condition
|
||||
|
||||
// Sort the array to check for existing sequences
|
||||
qsort(a, l, sizeof(int), compare_ints);
|
||||
|
||||
// Check if a sequential set of length 'r' already exists
|
||||
if (has_sequential_set(a, l, r)) {
|
||||
return; // Sequence already exists, no modification needed
|
||||
}
|
||||
|
||||
// Find the minimum and maximum values in the array
|
||||
int min_value = a[0];
|
||||
int max_value = a[l - 1];
|
||||
|
||||
// Generate a random value between min_value and max_value
|
||||
int value = xorshift(prng) % (max_value - min_value - r + 1);
|
||||
|
||||
// Generate a random location between 0 and l - r
|
||||
int offset = xorshift(prng) % (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) {
|
||||
a[i + offset] = value + i;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
print_array(int *array, size_t l)
|
||||
{
|
||||
int a[l];
|
||||
memcpy(a, array, sizeof(int) * l);
|
||||
qsort(a, l, sizeof(int), compare_ints);
|
||||
|
||||
printf("int a[] = {");
|
||||
for (int i = 0; i < l; i++) {
|
||||
printf("%d", a[i]);
|
||||
if (i != l) {
|
||||
printf(", ");
|
||||
}
|
||||
}
|
||||
printf("};\n");
|
||||
}
|
||||
|
||||
bool
|
||||
has_span(sparsemap_t *map, int *array, size_t l, size_t n)
|
||||
{
|
||||
if (n == 0 || l == 0 || n > l) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int sorted[l];
|
||||
memcpy(sorted, array, sizeof(int) * l);
|
||||
qsort(sorted, l, sizeof(int), compare_ints);
|
||||
|
||||
for (size_t 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++) {
|
||||
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);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool
|
||||
is_span(int *array, size_t n, int x, int l)
|
||||
{
|
||||
if (n == 0 || l < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int a[n];
|
||||
memcpy(a, array, sizeof(int) * n);
|
||||
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++) {
|
||||
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) {
|
||||
return true; // Found the span
|
||||
}
|
||||
}
|
||||
}
|
||||
return false; // Span not found
|
||||
}
|
||||
|
||||
void
|
||||
print_spans(int *array, size_t n)
|
||||
{
|
||||
int a[n];
|
||||
size_t start = 0, end = 0;
|
||||
|
||||
if (n == 0) {
|
||||
fprintf(stderr, "Array is empty\n");
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(a, array, sizeof(int) * n);
|
||||
qsort(a, n, sizeof(int), compare_ints);
|
||||
|
||||
for (size_t i = 1; i < n; i++) {
|
||||
if (a[i] == a[i - 1] + 1) {
|
||||
end = i; // Extend the span
|
||||
} else {
|
||||
// Print the current span
|
||||
if (start == end) {
|
||||
fprintf(stderr, "[%d] ", a[start]);
|
||||
} else {
|
||||
fprintf(stderr, "[%d, %d] ", a[start], a[end]);
|
||||
}
|
||||
// Move to the next span
|
||||
start = i;
|
||||
end = i;
|
||||
}
|
||||
}
|
||||
|
||||
// Print the last span if needed
|
||||
if (start == end) {
|
||||
fprintf(stderr, "[%d]\n", a[start]);
|
||||
} else {
|
||||
fprintf(stderr, "[%d, %d]\n", a[start], a[end]);
|
||||
}
|
||||
}
|
||||
|
||||
bool
|
||||
was_set(size_t bit, const int array[])
|
||||
{
|
||||
for (int i = 0; i < 1024; i++) {
|
||||
if (array[i] == (int)bit) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
#include "../tests/common.h"
|
||||
|
||||
#define TEST_ARRAY_SIZE 1024
|
||||
|
||||
int
|
||||
is_unique(int a[], size_t l, int value) {
|
||||
for (size_t i = 0; i < l; ++i) {
|
||||
if (a[i] == value) {
|
||||
return 0; // Not unique
|
||||
}
|
||||
}
|
||||
return 1; // Unique
|
||||
}
|
||||
|
||||
void
|
||||
setup_test_array(int a[], size_t l, int max_value, int *prng)
|
||||
{
|
||||
if (a == NULL || prng == NULL || max_value < 0) return; // Basic error handling and validation
|
||||
|
||||
for (size_t i = 0; i < l; ++i) {
|
||||
int candidate;
|
||||
do {
|
||||
candidate = xorshift(prng) % (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
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
int i = 0;
|
||||
size_t rank;
|
||||
int i;
|
||||
int array[TEST_ARRAY_SIZE];
|
||||
int prng;
|
||||
|
||||
// seed the PRNG
|
||||
#ifdef SEED
|
||||
prng = 8675309;
|
||||
#else
|
||||
prng = (unsigned int)time(NULL) ^ getpid();
|
||||
#endif
|
||||
xorshift32_seed();
|
||||
|
||||
// disable buffering
|
||||
setvbuf(stdout, 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
|
||||
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);
|
||||
sparsemap_t *map = sparsemap(buf, sizeof(uint8_t) * 3 * 1024);
|
||||
|
||||
// create an array of ints
|
||||
setup_test_array(array, TEST_ARRAY_SIZE, 1024 * 3, &prng);
|
||||
setup_test_array(array, TEST_ARRAY_SIZE, 1024 * 3);
|
||||
|
||||
// randomize setting the bits on
|
||||
shuffle(array, TEST_ARRAY_SIZE, &prng);
|
||||
//print_array(array, TEST_ARRAY_SIZE);
|
||||
//print_spans(array, TEST_ARRAY_SIZE);
|
||||
shuffle(array, TEST_ARRAY_SIZE);
|
||||
// print_array(array, TEST_ARRAY_SIZE);
|
||||
// print_spans(array, TEST_ARRAY_SIZE);
|
||||
|
||||
// set all the bits on in a random order
|
||||
for (i = 0; i < TEST_ARRAY_SIZE; i++) {
|
||||
|
@ -288,26 +40,26 @@ main(void)
|
|||
assert(sparsemap_is_set(map, array[i]) == true);
|
||||
}
|
||||
|
||||
// for (size_t len = 1; len < 20; len++) {
|
||||
// for (size_t len = 1; len < TEST_ARRAY_SIZE - 1; len++) {
|
||||
// for (size_t len = 1; len <= 1; len++) {
|
||||
// for (size_t len = 2; len <= 2; len++) {
|
||||
// for (size_t len = 3; len <= 3; len++) {
|
||||
// for (size_t len = 4; len <= 4; len++) {
|
||||
// for (size_t len = 5; len <= 5; len++) {
|
||||
// for (size_t len = 8; len <= 8; len++) {
|
||||
for (size_t len = 372; len <= 372; len++) {
|
||||
// for (size_t len = 1; len < 20; len++) {
|
||||
// for (size_t len = 1; len < TEST_ARRAY_SIZE - 1; len++) {
|
||||
// for (size_t len = 1; len <= 1; len++) {
|
||||
// for (size_t len = 2; len <= 2; len++) {
|
||||
// for (size_t len = 3; len <= 3; len++) {
|
||||
// for (size_t len = 4; len <= 4; len++) {
|
||||
// for (size_t len = 5; len <= 5; len++) {
|
||||
// for (size_t len = 8; len <= 8; len++) {
|
||||
for (size_t len = 372; len <= 372; len++) {
|
||||
__diag("================> %lu\n", len);
|
||||
sparsemap_clear(map);
|
||||
// set all the bits on in a random order
|
||||
ensure_sequential_set(array, TEST_ARRAY_SIZE, len, &prng);
|
||||
shuffle(array, TEST_ARRAY_SIZE, &prng);
|
||||
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);
|
||||
|
@ -318,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);
|
||||
}
|
||||
|
|
|
@ -44,6 +44,7 @@
|
|||
pkg-config
|
||||
python3
|
||||
ripgrep
|
||||
valgrind
|
||||
];
|
||||
buildInputs = with pkgs; [
|
||||
libbacktrace
|
||||
|
|
|
@ -1,3 +1,25 @@
|
|||
/*
|
||||
* Copyright (c) 2024 Gregory Burd <greg@burd.me>. All rights reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Sparsemap
|
||||
*
|
||||
|
@ -65,20 +87,17 @@
|
|||
* Usually this is an uint64_t.
|
||||
*/
|
||||
|
||||
|
||||
typedef struct sparsemap sparsemap_t;
|
||||
typedef uint32_t sm_idx_t;
|
||||
typedef uint64_t sm_bitvec_t;
|
||||
|
||||
typedef struct sparsemap {
|
||||
uint8_t *m_data; /* The serialized bitmap data */
|
||||
size_t m_data_size; /* The total size of m_data */
|
||||
size_t m_data_used; /* The used size of m_data */
|
||||
} sparsemap_t;
|
||||
|
||||
/* Allocate on a sparsemap_t on the heap and initialize it. */
|
||||
sparsemap_t *sparsemap(uint8_t *data, size_t size, size_t used);
|
||||
sparsemap_t *sparsemap(uint8_t *data, size_t size);
|
||||
|
||||
/* Initialize sparsemap_t with data. */
|
||||
void sparsemap_init(sparsemap_t *map, uint8_t *data, size_t size, size_t used);
|
||||
void sparsemap_init(sparsemap_t *map, uint8_t *data, size_t size);
|
||||
|
||||
/* Clears the whole buffer. */
|
||||
void sparsemap_clear(sparsemap_t *map);
|
||||
|
@ -89,8 +108,11 @@ void sparsemap_open(sparsemap_t *, uint8_t *data, size_t data_size);
|
|||
/* Resizes the data range. */
|
||||
void sparsemap_set_data_size(sparsemap_t *map, size_t data_size);
|
||||
|
||||
/* Calculate remaining capacity, full when 0. */
|
||||
double sparsemap_capacity_remaining(sparsemap_t *map);
|
||||
|
||||
/* Returns the size of the underlying byte array. */
|
||||
size_t sparsemap_get_range_size(sparsemap_t *map);
|
||||
size_t sparsemap_get_capacity(sparsemap_t *map);
|
||||
|
||||
/* Returns the value of a bit at index |idx|. */
|
||||
bool sparsemap_is_set(sparsemap_t *map, size_t idx);
|
||||
|
|
240
src/sparsemap.c
240
src/sparsemap.c
|
@ -1,27 +1,32 @@
|
|||
/*
|
||||
* Copyright (c) 2024
|
||||
* Gregory Burd <greg@burd.me>. All rights reserved.
|
||||
* Copyright (c) 2024 Gregory Burd <greg@burd.me>. All rights reserved.
|
||||
*
|
||||
* ISC License Permission to use, copy, modify, and/or distribute this software
|
||||
* for any purpose with or without fee is hereby granted, provided that the
|
||||
* above copyright notice and this permission notice appear in all copies.
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
* PERFORMANCE OF THIS SOFTWARE.
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <popcount.h>
|
||||
#include <sparsemap.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include <popcount.h>
|
||||
#include <sparsemap.h>
|
||||
|
||||
#ifdef SPARSEMAP_DIAGNOSTIC
|
||||
#pragma GCC diagnostic push
|
||||
|
@ -33,8 +38,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);
|
||||
}
|
||||
|
@ -92,10 +97,20 @@ enum __SM_CHUNK_INFO {
|
|||
SM_NEEDS_TO_SHRINK = 2
|
||||
};
|
||||
|
||||
#define SM_CHUNK_GET_FLAGS(from, at) (((from)) & ((sm_bitvec_t)SM_FLAG_MASK << ((at) * 2))) >> ((at) * 2)
|
||||
|
||||
|
||||
typedef struct {
|
||||
sm_bitvec_t *m_data;
|
||||
} __sm_chunk_t;
|
||||
|
||||
struct sparsemap {
|
||||
uint8_t *m_data; /* The serialized bitmap data */
|
||||
size_t m_capacity; /* The total size of m_data */
|
||||
size_t m_data_used; /* The used size of m_data */
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Calculates the number of sm_bitvec_ts required by a single byte with flags
|
||||
* (in m_data[0]).
|
||||
|
@ -143,7 +158,7 @@ __sm_chunk_map_get_position(__sm_chunk_t *map, size_t bv)
|
|||
|
||||
bv -= num_bytes * SM_FLAGS_PER_INDEX_BYTE;
|
||||
for (size_t i = 0; i < bv; i++) {
|
||||
size_t flags = ((*map->m_data) & ((sm_bitvec_t)SM_FLAG_MASK << (i * 2))) >> (i * 2);
|
||||
size_t flags = SM_CHUNK_GET_FLAGS(*map->m_data, i);
|
||||
if (flags == SM_PAYLOAD_MIXED) {
|
||||
position++;
|
||||
}
|
||||
|
@ -175,7 +190,7 @@ __sm_chunk_map_get_capacity(__sm_chunk_t *map)
|
|||
continue;
|
||||
}
|
||||
for (int j = 0; j < SM_FLAGS_PER_INDEX_BYTE; j++) {
|
||||
size_t flags = ((*p) & ((sm_bitvec_t)SM_FLAG_MASK << (j * 2))) >> (j * 2);
|
||||
size_t flags = SM_CHUNK_GET_FLAGS(*p, j);
|
||||
if (flags == SM_PAYLOAD_NONE) {
|
||||
capacity -= SM_BITS_PER_VECTOR;
|
||||
}
|
||||
|
@ -200,8 +215,8 @@ __sm_chunk_map_set_capacity(__sm_chunk_t *map, size_t capacity)
|
|||
register uint8_t *p = (uint8_t *)map->m_data;
|
||||
for (ssize_t i = sizeof(sm_bitvec_t) - 1; i >= 0; i--) { // TODO:
|
||||
for (int j = SM_FLAGS_PER_INDEX_BYTE - 1; j >= 0; j--) {
|
||||
p[i] &= ~((sm_bitvec_t)0x03 << (j * 2));
|
||||
p[i] |= ((sm_bitvec_t)0x01 << (j * 2));
|
||||
p[i] &= ~((sm_bitvec_t)SM_PAYLOAD_ONES << (j * 2));
|
||||
p[i] |= ((sm_bitvec_t)SM_PAYLOAD_NONE << (j * 2));
|
||||
reduced += SM_BITS_PER_VECTOR;
|
||||
if (capacity + reduced == SM_CHUNK_MAX_CAPACITY) {
|
||||
__sm_assert(__sm_chunk_map_get_capacity(map) == capacity);
|
||||
|
@ -228,7 +243,7 @@ __sm_chunk_map_is_empty(__sm_chunk_t *map)
|
|||
for (size_t i = 0; i < sizeof(sm_bitvec_t); i++, p++) {
|
||||
if (*p) {
|
||||
for (int j = 0; j < SM_FLAGS_PER_INDEX_BYTE; j++) {
|
||||
size_t flags = ((*p) & ((sm_bitvec_t)SM_FLAG_MASK << (j * 2))) >> (j * 2);
|
||||
size_t flags = SM_CHUNK_GET_FLAGS(*p, j);
|
||||
if (flags != SM_PAYLOAD_NONE && flags != SM_PAYLOAD_ZEROS) {
|
||||
return (false);
|
||||
}
|
||||
|
@ -266,7 +281,7 @@ __sm_chunk_map_is_set(__sm_chunk_t *map, size_t idx)
|
|||
__sm_assert(bv < SM_FLAGS_PER_INDEX);
|
||||
|
||||
/* now retrieve the flags of that sm_bitvec_t */
|
||||
size_t flags = ((*map->m_data) & ((sm_bitvec_t)SM_FLAG_MASK << (bv * 2))) >> (bv * 2);
|
||||
size_t flags = SM_CHUNK_GET_FLAGS(*map->m_data, bv);
|
||||
switch (flags) {
|
||||
case SM_PAYLOAD_ZEROS:
|
||||
case SM_PAYLOAD_NONE:
|
||||
|
@ -301,7 +316,7 @@ __sm_chunk_map_set(__sm_chunk_t *map, size_t idx, bool value, size_t *pos, sm_bi
|
|||
__sm_assert(bv < SM_FLAGS_PER_INDEX);
|
||||
|
||||
/* Now retrieve the flags of that sm_bitvec_t. */
|
||||
size_t flags = ((*map->m_data) & ((sm_bitvec_t)SM_FLAG_MASK << (bv * 2))) >> (bv * 2);
|
||||
size_t flags = SM_CHUNK_GET_FLAGS(*map->m_data, bv);
|
||||
assert(flags != SM_PAYLOAD_NONE);
|
||||
if (flags == SM_PAYLOAD_ZEROS) {
|
||||
/* Easy - set bit to 0 in a sm_bitvec_t of zeroes. */
|
||||
|
@ -317,31 +332,31 @@ __sm_chunk_map_set(__sm_chunk_t *map, size_t idx, bool value, size_t *pos, sm_bi
|
|||
*fill = 0;
|
||||
return SM_NEEDS_TO_GROW;
|
||||
}
|
||||
/* new flags are 2#10 (currently, flags are set to 2#00
|
||||
2#00 | 2#10 = 2#10) */
|
||||
map->m_data[0] |= ((sm_bitvec_t)0x2 << (bv * 2));
|
||||
/* New flags are 2#10 meaning SM_PAYLOAD_MIXED. Currently, flags are set
|
||||
to 2#00, so 2#00 | 2#10 = 2#10. */
|
||||
*map->m_data |= ((sm_bitvec_t)SM_PAYLOAD_MIXED << (bv * 2));
|
||||
/* FALLTHROUGH */
|
||||
} else if (flags == SM_PAYLOAD_ONES) {
|
||||
/* easy - set bit to 1 in a sm_bitvec_t of ones */
|
||||
/* Easy - set bit to 1 in a sm_bitvec_t of ones. */
|
||||
if (value == true) {
|
||||
*pos = 0;
|
||||
*fill = 0;
|
||||
return SM_OK;
|
||||
}
|
||||
/* the sparsemap must grow this __sm_chunk_t by one additional sm_bitvec_t,
|
||||
then try again */
|
||||
/* The sparsemap must grow this __sm_chunk_t by one additional sm_bitvec_t,
|
||||
then try again. */
|
||||
if (!retried) {
|
||||
*pos = 1 + __sm_chunk_map_get_position(map, bv);
|
||||
*fill = (sm_bitvec_t)-1;
|
||||
return SM_NEEDS_TO_GROW;
|
||||
}
|
||||
/* new flags are 2#10 (currently, flags are set to 2#11;
|
||||
2#11 ^ 2#01 = 2#10) */
|
||||
map->m_data[0] ^= ((sm_bitvec_t)0x1 << (bv * 2));
|
||||
/* New flags are 2#10 meaning SM_PAYLOAD_MIXED. Currently, flags are
|
||||
set to 2#11, so 2#11 ^ 2#01 = 2#10. */
|
||||
map->m_data[0] ^= ((sm_bitvec_t)SM_PAYLOAD_NONE << (bv * 2));
|
||||
/* FALLTHROUGH */
|
||||
}
|
||||
|
||||
/* now flip the bit */
|
||||
/* Now flip the bit. */
|
||||
size_t position = 1 + __sm_chunk_map_get_position(map, bv);
|
||||
sm_bitvec_t w = map->m_data[position];
|
||||
if (value) {
|
||||
|
@ -350,7 +365,7 @@ __sm_chunk_map_set(__sm_chunk_t *map, size_t idx, bool value, size_t *pos, sm_bi
|
|||
w &= ~((sm_bitvec_t)1 << (idx % SM_BITS_PER_VECTOR));
|
||||
}
|
||||
|
||||
/* if this sm_bitvec_t is now all zeroes or ones then we can remove it */
|
||||
/* If this sm_bitvec_t is now all zeroes or ones then we can remove it. */
|
||||
if (w == 0) {
|
||||
map->m_data[0] &= ~((sm_bitvec_t)SM_PAYLOAD_ONES << (bv * 2));
|
||||
*pos = position;
|
||||
|
@ -372,7 +387,8 @@ __sm_chunk_map_set(__sm_chunk_t *map, size_t idx, bool value, size_t *pos, sm_bi
|
|||
|
||||
/**
|
||||
* Returns the index of the n'th set bit; sets |*pnew_n| to 0 if the
|
||||
* n'th bit was found in this __sm_chunk_t, or to the new, reduced value of |n|
|
||||
* n'th bit was found in this __sm_chunk_t, or to the new, reduced
|
||||
* value of |n|.
|
||||
*/
|
||||
static size_t
|
||||
__sm_chunk_map_select(__sm_chunk_t *map, size_t n, ssize_t *pnew_n)
|
||||
|
@ -388,7 +404,7 @@ __sm_chunk_map_select(__sm_chunk_t *map, size_t n, ssize_t *pnew_n)
|
|||
}
|
||||
|
||||
for (int j = 0; j < SM_FLAGS_PER_INDEX_BYTE; j++) {
|
||||
size_t flags = ((*p) & ((sm_bitvec_t)SM_FLAG_MASK << (j * 2))) >> (j * 2);
|
||||
size_t flags = SM_CHUNK_GET_FLAGS(*p, j);
|
||||
if (flags == SM_PAYLOAD_NONE) {
|
||||
continue;
|
||||
}
|
||||
|
@ -427,83 +443,61 @@ __sm_chunk_map_select(__sm_chunk_t *map, size_t n, ssize_t *pnew_n)
|
|||
}
|
||||
|
||||
/**
|
||||
* Counts the set bits in the range [first, last] inclusive.
|
||||
* Counts the set bits in the range [0, 'idx'] inclusive ignoring the first
|
||||
* '*offset' bits. Modifies '*offset' decreasing it by the number of bits
|
||||
* ignored during the search. The ranking (counting) will start after the
|
||||
* '*offset' has been reached 0.
|
||||
*/
|
||||
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 *offset, size_t idx)
|
||||
{
|
||||
size_t ret = 0;
|
||||
|
||||
register uint8_t *p = (uint8_t *)map->m_data;
|
||||
for (size_t i = 0; i < sizeof(sm_bitvec_t); i++, p++) {
|
||||
for (int j = 0; j < SM_FLAGS_PER_INDEX_BYTE; j++) {
|
||||
size_t flags = ((*p) & ((sm_bitvec_t)SM_FLAG_MASK << (j * 2))) >> (j * 2);
|
||||
size_t flags = SM_CHUNK_GET_FLAGS(*p, j);
|
||||
if (flags == SM_PAYLOAD_NONE) {
|
||||
continue;
|
||||
}
|
||||
if (flags == SM_PAYLOAD_ZEROS) {
|
||||
if (last > SM_BITS_PER_VECTOR) {
|
||||
if (*after > SM_BITS_PER_VECTOR) {
|
||||
*after = *after - SM_BITS_PER_VECTOR;
|
||||
if (idx > SM_BITS_PER_VECTOR) {
|
||||
if (*offset > SM_BITS_PER_VECTOR) {
|
||||
*offset = *offset - SM_BITS_PER_VECTOR;
|
||||
} else {
|
||||
last -= SM_BITS_PER_VECTOR - *after;
|
||||
*after = 0;
|
||||
idx -= SM_BITS_PER_VECTOR - *offset;
|
||||
*offset = 0;
|
||||
}
|
||||
} else {
|
||||
return (ret);
|
||||
}
|
||||
} else if (flags == SM_PAYLOAD_ONES) {
|
||||
if (last > SM_BITS_PER_VECTOR) {
|
||||
if (*after > SM_BITS_PER_VECTOR) {
|
||||
*after = *after - SM_BITS_PER_VECTOR;
|
||||
if (idx > SM_BITS_PER_VECTOR) {
|
||||
if (*offset > SM_BITS_PER_VECTOR) {
|
||||
*offset = *offset - SM_BITS_PER_VECTOR;
|
||||
} else {
|
||||
last -= SM_BITS_PER_VECTOR - *after;
|
||||
if (*after == 0) {
|
||||
idx -= SM_BITS_PER_VECTOR - *offset;
|
||||
if (*offset == 0) {
|
||||
ret += SM_BITS_PER_VECTOR;
|
||||
}
|
||||
*after = 0;
|
||||
*offset = 0;
|
||||
}
|
||||
} else {
|
||||
return (ret + last);
|
||||
return (ret + idx);
|
||||
}
|
||||
} else if (flags == SM_PAYLOAD_MIXED) {
|
||||
if (last > SM_BITS_PER_VECTOR) {
|
||||
last -= SM_BITS_PER_VECTOR;
|
||||
if (*after > SM_BITS_PER_VECTOR) {
|
||||
*after = *after - SM_BITS_PER_VECTOR;
|
||||
} else {
|
||||
sm_bitvec_t w = map->m_data[1 + __sm_chunk_map_get_position(map, i * SM_FLAGS_PER_INDEX_BYTE + j)];
|
||||
uint64_t mask = UINT64_MAX;
|
||||
if (*after > 0) {
|
||||
mask = ~(mask >> (SM_BITS_PER_VECTOR - *after));
|
||||
size_t amt = popcountll(w & mask);
|
||||
if (amt <= *after) {
|
||||
*after = *after - amt;
|
||||
} else {
|
||||
*after = 0;
|
||||
ret += popcountll(w & ~mask);
|
||||
}
|
||||
} else {
|
||||
ret += popcountll(w);
|
||||
}
|
||||
}
|
||||
sm_bitvec_t w = map->m_data[1 + __sm_chunk_map_get_position(map, i * SM_FLAGS_PER_INDEX_BYTE + j)];
|
||||
if (idx > SM_BITS_PER_VECTOR) {
|
||||
uint64_t mask_offset = ~(UINT64_MAX >> (SM_BITS_PER_VECTOR - *offset));
|
||||
idx -= SM_BITS_PER_VECTOR;
|
||||
ret += popcountll(w & mask_offset);
|
||||
*offset = (*offset > SM_BITS_PER_VECTOR) ? *offset - SM_BITS_PER_VECTOR : 0;
|
||||
} else {
|
||||
sm_bitvec_t w = map->m_data[1 + __sm_chunk_map_get_position(map, i * SM_FLAGS_PER_INDEX_BYTE + j)];
|
||||
size_t ks = 0;
|
||||
if (*after > 0) {
|
||||
if (*after > last) {
|
||||
ks = last;
|
||||
*after = *after - last;
|
||||
} else {
|
||||
ks += *after;
|
||||
*after = 0;
|
||||
}
|
||||
}
|
||||
for (size_t k = ks; k < last; k++) {
|
||||
if (w & ((sm_bitvec_t)1 << k)) {
|
||||
ret++;
|
||||
}
|
||||
}
|
||||
/* Create a mask for the range between offset and idx inclusive [*offset, idx]. */
|
||||
uint64_t offset_mask = (((uint64_t)1 << *offset) - 1);
|
||||
uint64_t idx_mask = idx >= 63 ? UINT64_MAX : ((uint64_t)1 << (idx + 1)) - 1;
|
||||
ret += popcountll(w & (idx_mask - offset_mask));
|
||||
*offset = *offset > idx ? *offset - idx : 0;
|
||||
return (ret);
|
||||
}
|
||||
}
|
||||
|
@ -529,7 +523,7 @@ __sm_chunk_map_scan(__sm_chunk_t *map, sm_idx_t start, void (*scanner)(sm_idx_t[
|
|||
}
|
||||
|
||||
for (int j = 0; j < SM_FLAGS_PER_INDEX_BYTE; j++) {
|
||||
size_t flags = ((*p) & ((sm_bitvec_t)SM_FLAG_MASK << (j * 2))) >> (j * 2);
|
||||
size_t flags = SM_CHUNK_GET_FLAGS(*p, j);
|
||||
if (flags == SM_PAYLOAD_NONE || flags == SM_PAYLOAD_ZEROS) {
|
||||
/* ignore the zeroes */
|
||||
} else if (flags == SM_PAYLOAD_ONES) {
|
||||
|
@ -599,10 +593,10 @@ __sm_get_chunk_map_count(sparsemap_t *map)
|
|||
/**
|
||||
* Returns the data at the specified |offset|.
|
||||
*/
|
||||
static uint8_t *
|
||||
static inline uint8_t *
|
||||
__sm_get_chunk_map_data(sparsemap_t *map, size_t offset)
|
||||
{
|
||||
return (&map->m_data[SM_SIZEOF_OVERHEAD + offset]);
|
||||
return (uint8_t *)(&map->m_data[SM_SIZEOF_OVERHEAD + offset]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -717,7 +711,7 @@ __sm_append_data(sparsemap_t *map, uint8_t *buffer, size_t buffer_size)
|
|||
static int
|
||||
__sm_insert_data(sparsemap_t *map, size_t offset, uint8_t *buffer, size_t buffer_size)
|
||||
{
|
||||
if (map->m_data_used + buffer_size > map->m_data_size) {
|
||||
if (map->m_data_used + buffer_size > map->m_capacity) {
|
||||
__sm_assert(!"buffer overflow");
|
||||
abort();
|
||||
}
|
||||
|
@ -747,6 +741,7 @@ __sm_remove_data(sparsemap_t *map, size_t offset, size_t gap_size)
|
|||
void
|
||||
sparsemap_clear(sparsemap_t *map)
|
||||
{
|
||||
memset(map->m_data, 0, map->m_capacity);
|
||||
map->m_data_used = SM_SIZEOF_OVERHEAD;
|
||||
__sm_set_chunk_map_count(map, 0);
|
||||
}
|
||||
|
@ -755,11 +750,11 @@ sparsemap_clear(sparsemap_t *map)
|
|||
* Allocate on a sparsemap_t on the heap and initialize it.
|
||||
*/
|
||||
sparsemap_t *
|
||||
sparsemap(uint8_t *data, size_t size, size_t used)
|
||||
sparsemap(uint8_t *data, size_t size)
|
||||
{
|
||||
sparsemap_t *map = (sparsemap_t *)calloc(1, sizeof(sparsemap_t));
|
||||
if (map) {
|
||||
sparsemap_init(map, data, size, used);
|
||||
sparsemap_init(map, data, size);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
@ -768,11 +763,11 @@ sparsemap(uint8_t *data, size_t size, size_t used)
|
|||
* Initialize sparsemap_t with data.
|
||||
*/
|
||||
void
|
||||
sparsemap_init(sparsemap_t *map, uint8_t *data, size_t size, size_t used)
|
||||
sparsemap_init(sparsemap_t *map, uint8_t *data, size_t size)
|
||||
{
|
||||
map->m_data = data;
|
||||
map->m_data_used = used;
|
||||
map->m_data_size = size == 0 ? UINT64_MAX : size;
|
||||
map->m_data_used = 0;
|
||||
map->m_capacity = size == 0 ? UINT64_MAX : size;
|
||||
sparsemap_clear(map);
|
||||
}
|
||||
|
||||
|
@ -784,25 +779,43 @@ sparsemap_open(sparsemap_t *map, uint8_t *data, size_t data_size)
|
|||
{
|
||||
map->m_data = data;
|
||||
map->m_data_used = 0;
|
||||
map->m_data_size = data_size;
|
||||
map->m_capacity = data_size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resizes the data range.
|
||||
*
|
||||
* TODO/NOTE: This is a dangerous operation because we cannot verify that
|
||||
* data_size is not exceeding the size of the underlying buffer.
|
||||
*/
|
||||
void
|
||||
sparsemap_set_data_size(sparsemap_t *map, size_t data_size)
|
||||
{
|
||||
map->m_data_size = data_size;
|
||||
map->m_capacity = data_size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the remaining capacity as an integer that approaches 0 to
|
||||
* indicate full.
|
||||
*/
|
||||
double
|
||||
sparsemap_capacity_remaining(sparsemap_t *map) {
|
||||
if (map->m_data_used > map->m_capacity) {
|
||||
return 0;
|
||||
}
|
||||
if (map->m_capacity == 0) {
|
||||
return 100.0;
|
||||
}
|
||||
return 100 - (((double)map->m_data_used / (double)map->m_capacity) * 100);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the size of the underlying byte array.
|
||||
*/
|
||||
size_t
|
||||
sparsemap_get_range_size(sparsemap_t *map)
|
||||
sparsemap_get_capacity(sparsemap_t *map)
|
||||
{
|
||||
return (map->m_data_size);
|
||||
return (map->m_capacity);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -946,7 +959,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 +972,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 +992,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 +1162,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);
|
||||
|
||||
|
@ -1174,25 +1188,28 @@ sparsemap_select(sparsemap_t *map, size_t n)
|
|||
}
|
||||
|
||||
/**
|
||||
* Counts the set bits in the range [first, last] inclusive.
|
||||
* Counts the set bits starting at 'offset' until and including 'idx', meaning
|
||||
* [offset, idx] inclusive.
|
||||
*/
|
||||
size_t
|
||||
sparsemap_rank(sparsemap_t *map, size_t first, size_t last)
|
||||
sparsemap_rank(sparsemap_t *map, size_t offset, size_t idx)
|
||||
{
|
||||
assert(sparsemap_get_size(map) >= SM_SIZEOF_OVERHEAD);
|
||||
size_t result = 0, after = first, count = __sm_get_chunk_map_count(map);
|
||||
size_t result = 0, prev = 0, count = __sm_get_chunk_map_count(map);
|
||||
uint8_t *p = __sm_get_chunk_map_data(map, 0);
|
||||
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
sm_idx_t start = *(sm_idx_t *)p;
|
||||
if (start > last) {
|
||||
if (start > idx) {
|
||||
return (result);
|
||||
}
|
||||
offset -= start - prev;
|
||||
prev = start;
|
||||
p += sizeof(sm_idx_t);
|
||||
__sm_chunk_t chunk;
|
||||
__sm_chunk_map_init(&chunk, p);
|
||||
|
||||
result += __sm_chunk_map_rank(&chunk, first - start, last - start, &after);
|
||||
result += __sm_chunk_map_rank(&chunk, &offset, idx - start);
|
||||
p += __sm_chunk_map_get_size(&chunk);
|
||||
}
|
||||
return (result);
|
||||
|
@ -1205,7 +1222,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) {
|
||||
|
|
338
tests/common.c
Normal file
338
tests/common.c
Normal file
|
@ -0,0 +1,338 @@
|
|||
#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 ignored "-Wvariadic-macros"
|
||||
#define __diag(...) \
|
||||
do { \
|
||||
fprintf(stderr, "%s:%d:%s(): ", __FILE__, __LINE__, __func__); \
|
||||
fprintf(stderr, __VA_ARGS__); \
|
||||
} while (0)
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
int __xorshift32_state = 0;
|
||||
|
||||
// Xorshift algorithm for PRNG
|
||||
uint32_t
|
||||
xorshift32()
|
||||
{
|
||||
uint32_t x = __xorshift32_state;
|
||||
if (x == 0)
|
||||
x = 123456789;
|
||||
x ^= x << 13;
|
||||
x ^= x >> 17;
|
||||
x ^= x << 5;
|
||||
__xorshift32_state = x;
|
||||
return x;
|
||||
}
|
||||
|
||||
void
|
||||
xorshift32_seed()
|
||||
{
|
||||
__xorshift32_state = XORSHIFT_SEED_VALUE;
|
||||
}
|
||||
|
||||
void
|
||||
shuffle(int *array, size_t n) // TODO working?
|
||||
{
|
||||
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];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
compare_ints(const void *a, const void *b)
|
||||
{
|
||||
return *(const int *)a - *(const int *)b;
|
||||
}
|
||||
|
||||
// Check if there's already a sequence of 'r' sequential integers
|
||||
int
|
||||
has_sequential_set(int a[], int l, int r)
|
||||
{
|
||||
// Start with a count of 1 for the first number
|
||||
int count = 1;
|
||||
for (int i = 1; i < l; ++i) {
|
||||
// Check if the current and previous elements are sequential
|
||||
if (a[i] - a[i - 1] == 1) {
|
||||
count++;
|
||||
if (count >= r) {
|
||||
// Found a sequential set of length 'r' starting at 'i'
|
||||
return i;
|
||||
}
|
||||
} else {
|
||||
// Reset count if the sequence breaks
|
||||
count = 1;
|
||||
}
|
||||
}
|
||||
// No sequential set of length 'r' found
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Function to ensure an array contains a set of 'r' sequential integers
|
||||
int
|
||||
ensure_sequential_set(int a[], int l, int r)
|
||||
{
|
||||
if (!a || l == 0 || r < 1 || r > l) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Sort the array to check for existing sequences
|
||||
qsort(a, l, sizeof(int), compare_ints);
|
||||
|
||||
// Check if a sequential set of length 'r' already exists
|
||||
int offset = has_sequential_set(a, l, r);
|
||||
if (offset >= 0) {
|
||||
return offset; // Sequence already exists, no modification needed
|
||||
}
|
||||
|
||||
// Find the minimum and maximum values in the array
|
||||
int min_value = a[0];
|
||||
int max_value = a[l - 1];
|
||||
|
||||
// Generate a random value between min_value and max_value
|
||||
int value = random_uint32() % (max_value - min_value - r + 1);
|
||||
// Generate a random location between 0 and l - r
|
||||
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) {
|
||||
a[i + offset] = value + i;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
int
|
||||
create_sequential_set_in_empty_map(sparsemap_t *map, int s, int r)
|
||||
{
|
||||
int placed_at;
|
||||
placed_at = random_uint32() % (s - r - 1);
|
||||
for (int i = placed_at; i < placed_at + r; i++) {
|
||||
sparsemap_set(map, i, true);
|
||||
}
|
||||
return placed_at;
|
||||
}
|
||||
|
||||
void
|
||||
print_array(int *array, int l)
|
||||
{
|
||||
int a[l];
|
||||
memcpy(a, array, sizeof(int) * l);
|
||||
qsort(a, l, sizeof(int), compare_ints);
|
||||
|
||||
fprintf(stderr, "int a[] = {");
|
||||
for (int i = 0; i < l; i++) {
|
||||
fprintf(stderr, "%d", a[i]);
|
||||
if (i != l - 1) {
|
||||
fprintf(stderr, ", ");
|
||||
}
|
||||
}
|
||||
fprintf(stderr, "};\n");
|
||||
}
|
||||
|
||||
bool
|
||||
has_span(sparsemap_t *map, int *array, int l, int n)
|
||||
{
|
||||
if (n == 0 || l == 0 || n > l) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int sorted[l];
|
||||
memcpy(sorted, array, sizeof(int) * l);
|
||||
qsort(sorted, l, sizeof(int), compare_ints);
|
||||
|
||||
for (int i = 0; i <= l - n; i++) {
|
||||
if (sorted[i] + n - 1 == sorted[i + n - 1]) {
|
||||
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: %d\n", sorted[i], sorted[i + n - 1], n);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool
|
||||
is_span(int *array, int n, int x, int l)
|
||||
{
|
||||
if (n == 0 || l < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int a[n];
|
||||
memcpy(a, array, sizeof(int) * n);
|
||||
qsort(a, n, sizeof(int), compare_ints);
|
||||
|
||||
// Iterate through the array to find a span starting at x of length l
|
||||
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) {
|
||||
return true; // Found the span
|
||||
}
|
||||
}
|
||||
}
|
||||
return false; // Span not found
|
||||
}
|
||||
|
||||
void
|
||||
print_spans(int *array, int n)
|
||||
{
|
||||
int a[n];
|
||||
size_t start = 0, end = 0;
|
||||
|
||||
if (n == 0) {
|
||||
fprintf(stderr, "Array is empty\n");
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(a, array, sizeof(int) * n);
|
||||
qsort(a, n, sizeof(int), compare_ints);
|
||||
|
||||
for (int i = 1; i < n; i++) {
|
||||
if (a[i] == a[i - 1] + 1) {
|
||||
end = i; // Extend the span
|
||||
} else {
|
||||
// Print the current span
|
||||
if (start == end) {
|
||||
fprintf(stderr, "[%d] ", a[start]);
|
||||
} else {
|
||||
fprintf(stderr, "[%d, %d] ", a[start], a[end]);
|
||||
}
|
||||
// Move to the next span
|
||||
start = i;
|
||||
end = i;
|
||||
}
|
||||
}
|
||||
|
||||
// Print the last span if needed
|
||||
if (start == end) {
|
||||
fprintf(stderr, "[%d]\n", a[start]);
|
||||
} else {
|
||||
fprintf(stderr, "[%d, %d]\n", a[start], a[end]);
|
||||
}
|
||||
}
|
||||
|
||||
bool
|
||||
is_set(const int array[], int bit)
|
||||
{
|
||||
for (int i = 0; i < 1024; i++) {
|
||||
if (array[i] == (int)bit) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int
|
||||
is_unique(int a[], int l, int value)
|
||||
{
|
||||
for (int i = 0; i < l; ++i) {
|
||||
if (a[i] == value) {
|
||||
return 0; // Not unique
|
||||
}
|
||||
}
|
||||
return 1; // Unique
|
||||
}
|
||||
|
||||
void
|
||||
setup_test_array(int a[], int l, int max_value)
|
||||
{
|
||||
if (a == NULL || max_value < 0)
|
||||
return; // Basic error handling and validation
|
||||
|
||||
for (int i = 0; i < l; ++i) {
|
||||
int candidate;
|
||||
do {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t
|
||||
rank_uint64(uint64_t number, int n, int p)
|
||||
{
|
||||
if (p < n || p > 63) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Create a mask for the range between n and p.
|
||||
This works by shifting 1 to the left (p+1) times, subtracting 1 to have
|
||||
a sequence of p 1's, then shifting n times to the left to position it
|
||||
starting at n. Finally, subtracting (1 << n) - 1 removes the bits below
|
||||
n from the mask. */
|
||||
uint64_t mask = ((uint64_t)1 << (p + 1)) - 1 - (((uint64_t)1 << n) - 1);
|
||||
|
||||
/* Apply the mask and count the set bits in the result. */
|
||||
uint64_t maskedNumber = number & mask;
|
||||
|
||||
/* Count the bits set in maskedNumber. */
|
||||
uint32_t count = 0;
|
||||
while (maskedNumber) {
|
||||
count += maskedNumber & 1;
|
||||
maskedNumber >>= 1;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void
|
||||
whats_set(sparsemap_t *map, int m)
|
||||
{
|
||||
logf("what's set in the range [0, %d): ", m);
|
||||
for (int i = 0; i < m; i++) {
|
||||
if (sparsemap_is_set(map, i)) {
|
||||
logf("%d ", i);
|
||||
}
|
||||
}
|
||||
logf("\n");
|
||||
}
|
47
tests/common.h
Normal file
47
tests/common.h
Normal file
|
@ -0,0 +1,47 @@
|
|||
|
||||
#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
|
||||
|
||||
#ifdef MUNIT_VERSION
|
||||
#define random_uint32 munit_rand_uint32
|
||||
#define logf(...) munit_logf(MUNIT_LOG_INFO, __VA_ARGS__)
|
||||
#else
|
||||
#define random_uint32 xorshift32
|
||||
#define logf(...) fprintf(stderr, __VA_ARGS__)
|
||||
#endif
|
||||
|
||||
/* Stable seeds make for stable "random" sequences for repeatable tests. */
|
||||
#ifdef STABLE_SEED
|
||||
#define XORSHIFT_SEED_VALUE (8675309)
|
||||
#else
|
||||
#define XORSHIFT_SEED_VALUE ((unsigned int)time(NULL) ^ getpid())
|
||||
#endif
|
||||
|
||||
void xorshift32_seed();
|
||||
uint32_t xorshift32();
|
||||
|
||||
void print_array(int *array, int l);
|
||||
void print_spans(int *array, int n);
|
||||
|
||||
bool is_span(int *array, int n, int x, int l);
|
||||
bool is_set(const int array[], int bit);
|
||||
bool has_span(sparsemap_t *map, int *array, int l, int n);
|
||||
int is_unique(int a[], int l, int value);
|
||||
|
||||
void setup_test_array(int a[], int l, int max_value);
|
||||
void shuffle(int *array, size_t n);
|
||||
int ensure_sequential_set(int a[], int l, int r);
|
||||
int create_sequential_set_in_empty_map(sparsemap_t *map, int s, int r);
|
||||
|
||||
void bitmap_from_uint32(sparsemap_t *map, uint32_t number);
|
||||
void bitmap_from_uint64(sparsemap_t *map, uint64_t number);
|
||||
uint32_t rank_uint64(uint64_t number, int n, int p);
|
||||
int whats_set_uint64(uint64_t number, int bitPositions[64]);
|
||||
|
||||
void whats_set(sparsemap_t *map, int m);
|
2260
tests/munit.c
Normal file
2260
tests/munit.c
Normal file
File diff suppressed because it is too large
Load diff
527
tests/munit.h
Normal file
527
tests/munit.h
Normal file
|
@ -0,0 +1,527 @@
|
|||
/* µnit Testing Framework
|
||||
* Copyright (c) 2013-2017 Evan Nemerson <evan@nemerson.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person
|
||||
* obtaining a copy of this software and associated documentation
|
||||
* files (the "Software"), to deal in the Software without
|
||||
* restriction, including without limitation the rights to use, copy,
|
||||
* modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
* of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#if !defined(MUNIT_H)
|
||||
#define MUNIT_H
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define MUNIT_VERSION(major, minor, revision) \
|
||||
(((major) << 16) | ((minor) << 8) | (revision))
|
||||
|
||||
#define MUNIT_CURRENT_VERSION MUNIT_VERSION(0, 4, 1)
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER < 1600)
|
||||
#define munit_int8_t __int8
|
||||
#define munit_uint8_t unsigned __int8
|
||||
#define munit_int16_t __int16
|
||||
#define munit_uint16_t unsigned __int16
|
||||
#define munit_int32_t __int32
|
||||
#define munit_uint32_t unsigned __int32
|
||||
#define munit_int64_t __int64
|
||||
#define munit_uint64_t unsigned __int64
|
||||
#else
|
||||
#include <stdint.h>
|
||||
#define munit_int8_t int8_t
|
||||
#define munit_uint8_t uint8_t
|
||||
#define munit_int16_t int16_t
|
||||
#define munit_uint16_t uint16_t
|
||||
#define munit_int32_t int32_t
|
||||
#define munit_uint32_t uint32_t
|
||||
#define munit_int64_t int64_t
|
||||
#define munit_uint64_t uint64_t
|
||||
#endif
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER < 1800)
|
||||
#if !defined(PRIi8)
|
||||
#define PRIi8 "i"
|
||||
#endif
|
||||
#if !defined(PRIi16)
|
||||
#define PRIi16 "i"
|
||||
#endif
|
||||
#if !defined(PRIi32)
|
||||
#define PRIi32 "i"
|
||||
#endif
|
||||
#if !defined(PRIi64)
|
||||
#define PRIi64 "I64i"
|
||||
#endif
|
||||
#if !defined(PRId8)
|
||||
#define PRId8 "d"
|
||||
#endif
|
||||
#if !defined(PRId16)
|
||||
#define PRId16 "d"
|
||||
#endif
|
||||
#if !defined(PRId32)
|
||||
#define PRId32 "d"
|
||||
#endif
|
||||
#if !defined(PRId64)
|
||||
#define PRId64 "I64d"
|
||||
#endif
|
||||
#if !defined(PRIx8)
|
||||
#define PRIx8 "x"
|
||||
#endif
|
||||
#if !defined(PRIx16)
|
||||
#define PRIx16 "x"
|
||||
#endif
|
||||
#if !defined(PRIx32)
|
||||
#define PRIx32 "x"
|
||||
#endif
|
||||
#if !defined(PRIx64)
|
||||
#define PRIx64 "I64x"
|
||||
#endif
|
||||
#if !defined(PRIu8)
|
||||
#define PRIu8 "u"
|
||||
#endif
|
||||
#if !defined(PRIu16)
|
||||
#define PRIu16 "u"
|
||||
#endif
|
||||
#if !defined(PRIu32)
|
||||
#define PRIu32 "u"
|
||||
#endif
|
||||
#if !defined(PRIu64)
|
||||
#define PRIu64 "I64u"
|
||||
#endif
|
||||
#else
|
||||
#include <inttypes.h>
|
||||
#endif
|
||||
|
||||
#if !defined(munit_bool)
|
||||
#if defined(bool)
|
||||
#define munit_bool bool
|
||||
#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
|
||||
#define munit_bool _Bool
|
||||
#else
|
||||
#define munit_bool int
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#if defined(__GNUC__)
|
||||
#define MUNIT_LIKELY(expr) (__builtin_expect((expr), 1))
|
||||
#define MUNIT_UNLIKELY(expr) (__builtin_expect((expr), 0))
|
||||
#define MUNIT_UNUSED __attribute__((__unused__))
|
||||
#else
|
||||
#define MUNIT_LIKELY(expr) (expr)
|
||||
#define MUNIT_UNLIKELY(expr) (expr)
|
||||
#define MUNIT_UNUSED
|
||||
#endif
|
||||
|
||||
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
|
||||
!defined(__PGI)
|
||||
#define MUNIT_ARRAY_PARAM(name) name
|
||||
#else
|
||||
#define MUNIT_ARRAY_PARAM(name)
|
||||
#endif
|
||||
|
||||
#if !defined(_WIN32)
|
||||
#define MUNIT_SIZE_MODIFIER "z"
|
||||
#define MUNIT_CHAR_MODIFIER "hh"
|
||||
#define MUNIT_SHORT_MODIFIER "h"
|
||||
#else
|
||||
#if defined(_M_X64) || defined(__amd64__)
|
||||
#define MUNIT_SIZE_MODIFIER "I64"
|
||||
#else
|
||||
#define MUNIT_SIZE_MODIFIER ""
|
||||
#endif
|
||||
#define MUNIT_CHAR_MODIFIER ""
|
||||
#define MUNIT_SHORT_MODIFIER ""
|
||||
#endif
|
||||
|
||||
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
|
||||
#define MUNIT_NO_RETURN _Noreturn
|
||||
#elif defined(__GNUC__)
|
||||
#define MUNIT_NO_RETURN __attribute__((__noreturn__))
|
||||
#elif defined(_MSC_VER)
|
||||
#define MUNIT_NO_RETURN __declspec(noreturn)
|
||||
#else
|
||||
#define MUNIT_NO_RETURN
|
||||
#endif
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1500)
|
||||
#define MUNIT_PUSH_DISABLE_MSVC_C4127_ \
|
||||
__pragma(warning(push)) __pragma(warning(disable : 4127))
|
||||
#define MUNIT_POP_DISABLE_MSVC_C4127_ __pragma(warning(pop))
|
||||
#else
|
||||
#define MUNIT_PUSH_DISABLE_MSVC_C4127_
|
||||
#define MUNIT_POP_DISABLE_MSVC_C4127_
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
MUNIT_LOG_DEBUG,
|
||||
MUNIT_LOG_INFO,
|
||||
MUNIT_LOG_WARNING,
|
||||
MUNIT_LOG_ERROR
|
||||
} MunitLogLevel;
|
||||
|
||||
#if defined(__GNUC__) && !defined(__MINGW32__)
|
||||
#define MUNIT_PRINTF(string_index, first_to_check) \
|
||||
__attribute__((format(printf, string_index, first_to_check)))
|
||||
#else
|
||||
#define MUNIT_PRINTF(string_index, first_to_check)
|
||||
#endif
|
||||
|
||||
MUNIT_PRINTF(4, 5)
|
||||
void munit_logf_ex(MunitLogLevel level, const char *filename, int line,
|
||||
const char *format, ...);
|
||||
|
||||
#define munit_logf(level, format, ...) \
|
||||
munit_logf_ex(level, __FILE__, __LINE__, format, __VA_ARGS__)
|
||||
|
||||
#define munit_log(level, msg) munit_logf(level, "%s", msg)
|
||||
|
||||
MUNIT_NO_RETURN
|
||||
MUNIT_PRINTF(3, 4)
|
||||
void munit_errorf_ex(const char *filename, int line, const char *format, ...);
|
||||
|
||||
#define munit_errorf(format, ...) \
|
||||
munit_errorf_ex(__FILE__, __LINE__, format, __VA_ARGS__)
|
||||
|
||||
#define munit_error(msg) munit_errorf("%s", msg)
|
||||
|
||||
#define munit_assert(expr) \
|
||||
do { \
|
||||
if (!MUNIT_LIKELY(expr)) { \
|
||||
munit_error("assertion failed: " #expr); \
|
||||
} \
|
||||
MUNIT_PUSH_DISABLE_MSVC_C4127_ \
|
||||
} while (0) MUNIT_POP_DISABLE_MSVC_C4127_
|
||||
|
||||
#define munit_assert_true(expr) \
|
||||
do { \
|
||||
if (!MUNIT_LIKELY(expr)) { \
|
||||
munit_error("assertion failed: " #expr " is not true"); \
|
||||
} \
|
||||
MUNIT_PUSH_DISABLE_MSVC_C4127_ \
|
||||
} while (0) MUNIT_POP_DISABLE_MSVC_C4127_
|
||||
|
||||
#define munit_assert_false(expr) \
|
||||
do { \
|
||||
if (!MUNIT_LIKELY(!(expr))) { \
|
||||
munit_error("assertion failed: " #expr " is not false"); \
|
||||
} \
|
||||
MUNIT_PUSH_DISABLE_MSVC_C4127_ \
|
||||
} while (0) MUNIT_POP_DISABLE_MSVC_C4127_
|
||||
|
||||
#define munit_assert_type_full(prefix, suffix, T, fmt, a, op, b) \
|
||||
do { \
|
||||
T munit_tmp_a_ = (a); \
|
||||
T munit_tmp_b_ = (b); \
|
||||
if (!(munit_tmp_a_ op munit_tmp_b_)) { \
|
||||
munit_errorf("assertion failed: %s %s %s (" prefix "%" fmt suffix \
|
||||
" %s " prefix "%" fmt suffix ")", \
|
||||
#a, #op, #b, munit_tmp_a_, #op, munit_tmp_b_); \
|
||||
} \
|
||||
MUNIT_PUSH_DISABLE_MSVC_C4127_ \
|
||||
} while (0) MUNIT_POP_DISABLE_MSVC_C4127_
|
||||
|
||||
#define munit_assert_type(T, fmt, a, op, b) \
|
||||
munit_assert_type_full("", "", T, fmt, a, op, b)
|
||||
|
||||
#define munit_assert_char(a, op, b) \
|
||||
munit_assert_type_full("'\\x", "'", char, "02" MUNIT_CHAR_MODIFIER "x", a, \
|
||||
op, b)
|
||||
#define munit_assert_uchar(a, op, b) \
|
||||
munit_assert_type_full("'\\x", "'", unsigned char, \
|
||||
"02" MUNIT_CHAR_MODIFIER "x", a, op, b)
|
||||
#define munit_assert_short(a, op, b) \
|
||||
munit_assert_type(short, MUNIT_SHORT_MODIFIER "d", a, op, b)
|
||||
#define munit_assert_ushort(a, op, b) \
|
||||
munit_assert_type(unsigned short, MUNIT_SHORT_MODIFIER "u", a, op, b)
|
||||
#define munit_assert_int(a, op, b) munit_assert_type(int, "d", a, op, b)
|
||||
#define munit_assert_uint(a, op, b) \
|
||||
munit_assert_type(unsigned int, "u", a, op, b)
|
||||
#define munit_assert_long(a, op, b) munit_assert_type(long int, "ld", a, op, b)
|
||||
#define munit_assert_ulong(a, op, b) \
|
||||
munit_assert_type(unsigned long int, "lu", a, op, b)
|
||||
#define munit_assert_llong(a, op, b) \
|
||||
munit_assert_type(long long int, "lld", a, op, b)
|
||||
#define munit_assert_ullong(a, op, b) \
|
||||
munit_assert_type(unsigned long long int, "llu", a, op, b)
|
||||
|
||||
#define munit_assert_size(a, op, b) \
|
||||
munit_assert_type(size_t, MUNIT_SIZE_MODIFIER "u", a, op, b)
|
||||
|
||||
#define munit_assert_float(a, op, b) munit_assert_type(float, "f", a, op, b)
|
||||
#define munit_assert_double(a, op, b) munit_assert_type(double, "g", a, op, b)
|
||||
#define munit_assert_ptr(a, op, b) \
|
||||
munit_assert_type(const void *, "p", a, op, b)
|
||||
|
||||
#define munit_assert_int8(a, op, b) \
|
||||
munit_assert_type(munit_int8_t, PRIi8, a, op, b)
|
||||
#define munit_assert_uint8(a, op, b) \
|
||||
munit_assert_type(munit_uint8_t, PRIu8, a, op, b)
|
||||
#define munit_assert_int16(a, op, b) \
|
||||
munit_assert_type(munit_int16_t, PRIi16, a, op, b)
|
||||
#define munit_assert_uint16(a, op, b) \
|
||||
munit_assert_type(munit_uint16_t, PRIu16, a, op, b)
|
||||
#define munit_assert_int32(a, op, b) \
|
||||
munit_assert_type(munit_int32_t, PRIi32, a, op, b)
|
||||
#define munit_assert_uint32(a, op, b) \
|
||||
munit_assert_type(munit_uint32_t, PRIu32, a, op, b)
|
||||
#define munit_assert_int64(a, op, b) \
|
||||
munit_assert_type(munit_int64_t, PRIi64, a, op, b)
|
||||
#define munit_assert_uint64(a, op, b) \
|
||||
munit_assert_type(munit_uint64_t, PRIu64, a, op, b)
|
||||
|
||||
#define munit_assert_double_equal(a, b, precision) \
|
||||
do { \
|
||||
const double munit_tmp_a_ = (a); \
|
||||
const double munit_tmp_b_ = (b); \
|
||||
const double munit_tmp_diff_ = ((munit_tmp_a_ - munit_tmp_b_) < 0) ? \
|
||||
-(munit_tmp_a_ - munit_tmp_b_) : \
|
||||
(munit_tmp_a_ - munit_tmp_b_); \
|
||||
if (MUNIT_UNLIKELY(munit_tmp_diff_ > 1e-##precision)) { \
|
||||
munit_errorf("assertion failed: %s == %s (%0." #precision \
|
||||
"g == %0." #precision "g)", \
|
||||
#a, #b, munit_tmp_a_, munit_tmp_b_); \
|
||||
} \
|
||||
MUNIT_PUSH_DISABLE_MSVC_C4127_ \
|
||||
} while (0) MUNIT_POP_DISABLE_MSVC_C4127_
|
||||
|
||||
#include <string.h>
|
||||
#define munit_assert_string_equal(a, b) \
|
||||
do { \
|
||||
const char *munit_tmp_a_ = a; \
|
||||
const char *munit_tmp_b_ = b; \
|
||||
if (MUNIT_UNLIKELY(strcmp(munit_tmp_a_, munit_tmp_b_) != 0)) { \
|
||||
munit_errorf("assertion failed: string %s == %s (\"%s\" == \"%s\")", #a, \
|
||||
#b, munit_tmp_a_, munit_tmp_b_); \
|
||||
} \
|
||||
MUNIT_PUSH_DISABLE_MSVC_C4127_ \
|
||||
} while (0) MUNIT_POP_DISABLE_MSVC_C4127_
|
||||
|
||||
#define munit_assert_string_not_equal(a, b) \
|
||||
do { \
|
||||
const char *munit_tmp_a_ = a; \
|
||||
const char *munit_tmp_b_ = b; \
|
||||
if (MUNIT_UNLIKELY(strcmp(munit_tmp_a_, munit_tmp_b_) == 0)) { \
|
||||
munit_errorf("assertion failed: string %s != %s (\"%s\" == \"%s\")", #a, \
|
||||
#b, munit_tmp_a_, munit_tmp_b_); \
|
||||
} \
|
||||
MUNIT_PUSH_DISABLE_MSVC_C4127_ \
|
||||
} while (0) MUNIT_POP_DISABLE_MSVC_C4127_
|
||||
|
||||
#define munit_assert_memory_equal(size, a, b) \
|
||||
do { \
|
||||
const unsigned char *munit_tmp_a_ = (const unsigned char *)(a); \
|
||||
const unsigned char *munit_tmp_b_ = (const unsigned char *)(b); \
|
||||
const size_t munit_tmp_size_ = (size); \
|
||||
if (MUNIT_UNLIKELY(memcmp(munit_tmp_a_, munit_tmp_b_, munit_tmp_size_)) != \
|
||||
0) { \
|
||||
size_t munit_tmp_pos_; \
|
||||
for (munit_tmp_pos_ = 0; munit_tmp_pos_ < munit_tmp_size_; \
|
||||
munit_tmp_pos_++) { \
|
||||
if (munit_tmp_a_[munit_tmp_pos_] != munit_tmp_b_[munit_tmp_pos_]) { \
|
||||
munit_errorf( \
|
||||
"assertion failed: memory %s == %s, at offset %" MUNIT_SIZE_MODIFIER \
|
||||
"u", \
|
||||
#a, #b, munit_tmp_pos_); \
|
||||
break; \
|
||||
} \
|
||||
} \
|
||||
} \
|
||||
MUNIT_PUSH_DISABLE_MSVC_C4127_ \
|
||||
} while (0) MUNIT_POP_DISABLE_MSVC_C4127_
|
||||
|
||||
#define munit_assert_memory_not_equal(size, a, b) \
|
||||
do { \
|
||||
const unsigned char *munit_tmp_a_ = (const unsigned char *)(a); \
|
||||
const unsigned char *munit_tmp_b_ = (const unsigned char *)(b); \
|
||||
const size_t munit_tmp_size_ = (size); \
|
||||
if (MUNIT_UNLIKELY(memcmp(munit_tmp_a_, munit_tmp_b_, munit_tmp_size_)) == \
|
||||
0) { \
|
||||
munit_errorf("assertion failed: memory %s != %s (%zu bytes)", #a, #b, \
|
||||
munit_tmp_size_); \
|
||||
} \
|
||||
MUNIT_PUSH_DISABLE_MSVC_C4127_ \
|
||||
} while (0) MUNIT_POP_DISABLE_MSVC_C4127_
|
||||
|
||||
#define munit_assert_ptr_equal(a, b) munit_assert_ptr(a, ==, b)
|
||||
#define munit_assert_ptr_not_equal(a, b) munit_assert_ptr(a, !=, b)
|
||||
#define munit_assert_null(ptr) munit_assert_ptr(ptr, ==, NULL)
|
||||
#define munit_assert_not_null(ptr) munit_assert_ptr(ptr, !=, NULL)
|
||||
#define munit_assert_ptr_null(ptr) munit_assert_ptr(ptr, ==, NULL)
|
||||
#define munit_assert_ptr_not_null(ptr) munit_assert_ptr(ptr, !=, NULL)
|
||||
|
||||
/*** Memory allocation ***/
|
||||
|
||||
void *munit_malloc_ex(const char *filename, int line, size_t size);
|
||||
|
||||
#define munit_malloc(size) munit_malloc_ex(__FILE__, __LINE__, (size))
|
||||
|
||||
#define munit_new(type) ((type *)munit_malloc(sizeof(type)))
|
||||
|
||||
#define munit_calloc(nmemb, size) munit_malloc((nmemb) * (size))
|
||||
|
||||
#define munit_newa(type, nmemb) ((type *)munit_calloc((nmemb), sizeof(type)))
|
||||
|
||||
/*** Random number generation ***/
|
||||
|
||||
void munit_rand_seed(munit_uint32_t seed);
|
||||
munit_uint32_t munit_rand_uint32(void);
|
||||
int munit_rand_int_range(int min, int max);
|
||||
double munit_rand_double(void);
|
||||
void munit_rand_memory(size_t size,
|
||||
munit_uint8_t buffer[MUNIT_ARRAY_PARAM(size)]);
|
||||
|
||||
/*** Tests and Suites ***/
|
||||
|
||||
typedef enum {
|
||||
/* Test successful */
|
||||
MUNIT_OK,
|
||||
/* Test failed */
|
||||
MUNIT_FAIL,
|
||||
/* Test was skipped */
|
||||
MUNIT_SKIP,
|
||||
/* Test failed due to circumstances not intended to be tested
|
||||
* (things like network errors, invalid parameter value, failure to
|
||||
* allocate memory in the test harness, etc.). */
|
||||
MUNIT_ERROR
|
||||
} MunitResult;
|
||||
|
||||
typedef struct {
|
||||
char *name;
|
||||
char **values;
|
||||
} MunitParameterEnum;
|
||||
|
||||
typedef struct {
|
||||
char *name;
|
||||
char *value;
|
||||
} MunitParameter;
|
||||
|
||||
const char *munit_parameters_get(const MunitParameter params[],
|
||||
const char *key);
|
||||
|
||||
typedef enum {
|
||||
MUNIT_TEST_OPTION_NONE = 0,
|
||||
MUNIT_TEST_OPTION_SINGLE_ITERATION = 1 << 0,
|
||||
MUNIT_TEST_OPTION_TODO = 1 << 1
|
||||
} MunitTestOptions;
|
||||
|
||||
typedef MunitResult (
|
||||
*MunitTestFunc)(const MunitParameter params[], void *user_data_or_fixture);
|
||||
typedef void *(*MunitTestSetup)(const MunitParameter params[], void *user_data);
|
||||
typedef void (*MunitTestTearDown)(void *fixture);
|
||||
|
||||
typedef struct {
|
||||
char *name;
|
||||
MunitTestFunc test;
|
||||
MunitTestSetup setup;
|
||||
MunitTestTearDown tear_down;
|
||||
MunitTestOptions options;
|
||||
MunitParameterEnum *parameters;
|
||||
} MunitTest;
|
||||
|
||||
typedef enum { MUNIT_SUITE_OPTION_NONE = 0 } MunitSuiteOptions;
|
||||
|
||||
typedef struct MunitSuite_ MunitSuite;
|
||||
|
||||
struct MunitSuite_ {
|
||||
char *prefix;
|
||||
MunitTest *tests;
|
||||
MunitSuite *suites;
|
||||
unsigned int iterations;
|
||||
MunitSuiteOptions options;
|
||||
};
|
||||
|
||||
int munit_suite_main(const MunitSuite *suite, void *user_data, int argc,
|
||||
char *const argv[MUNIT_ARRAY_PARAM(argc + 1)]);
|
||||
|
||||
/* Note: I'm not very happy with this API; it's likely to change if I
|
||||
* figure out something better. Suggestions welcome. */
|
||||
|
||||
typedef struct MunitArgument_ MunitArgument;
|
||||
|
||||
struct MunitArgument_ {
|
||||
char *name;
|
||||
munit_bool (*parse_argument)(const MunitSuite *suite, void *user_data,
|
||||
int *arg, int argc, char *const argv[MUNIT_ARRAY_PARAM(argc + 1)]);
|
||||
void (*write_help)(const MunitArgument *argument, void *user_data);
|
||||
};
|
||||
|
||||
int munit_suite_main_custom(const MunitSuite *suite, void *user_data, int argc,
|
||||
char *const argv[MUNIT_ARRAY_PARAM(argc + 1)],
|
||||
const MunitArgument arguments[]);
|
||||
|
||||
#if defined(MUNIT_ENABLE_ASSERT_ALIASES)
|
||||
|
||||
#define assert_true(expr) munit_assert_true(expr)
|
||||
#define assert_false(expr) munit_assert_false(expr)
|
||||
#define assert_char(a, op, b) munit_assert_char(a, op, b)
|
||||
#define assert_uchar(a, op, b) munit_assert_uchar(a, op, b)
|
||||
#define assert_short(a, op, b) munit_assert_short(a, op, b)
|
||||
#define assert_ushort(a, op, b) munit_assert_ushort(a, op, b)
|
||||
#define assert_int(a, op, b) munit_assert_int(a, op, b)
|
||||
#define assert_uint(a, op, b) munit_assert_uint(a, op, b)
|
||||
#define assert_long(a, op, b) munit_assert_long(a, op, b)
|
||||
#define assert_ulong(a, op, b) munit_assert_ulong(a, op, b)
|
||||
#define assert_llong(a, op, b) munit_assert_llong(a, op, b)
|
||||
#define assert_ullong(a, op, b) munit_assert_ullong(a, op, b)
|
||||
#define assert_size(a, op, b) munit_assert_size(a, op, b)
|
||||
#define assert_float(a, op, b) munit_assert_float(a, op, b)
|
||||
#define assert_double(a, op, b) munit_assert_double(a, op, b)
|
||||
#define assert_ptr(a, op, b) munit_assert_ptr(a, op, b)
|
||||
|
||||
#define assert_int8(a, op, b) munit_assert_int8(a, op, b)
|
||||
#define assert_uint8(a, op, b) munit_assert_uint8(a, op, b)
|
||||
#define assert_int16(a, op, b) munit_assert_int16(a, op, b)
|
||||
#define assert_uint16(a, op, b) munit_assert_uint16(a, op, b)
|
||||
#define assert_int32(a, op, b) munit_assert_int32(a, op, b)
|
||||
#define assert_uint32(a, op, b) munit_assert_uint32(a, op, b)
|
||||
#define assert_int64(a, op, b) munit_assert_int64(a, op, b)
|
||||
#define assert_uint64(a, op, b) munit_assert_uint64(a, op, b)
|
||||
|
||||
#define assert_double_equal(a, b, precision) \
|
||||
munit_assert_double_equal(a, b, precision)
|
||||
#define assert_string_equal(a, b) munit_assert_string_equal(a, b)
|
||||
#define assert_string_not_equal(a, b) munit_assert_string_not_equal(a, b)
|
||||
#define assert_memory_equal(size, a, b) munit_assert_memory_equal(size, a, b)
|
||||
#define assert_memory_not_equal(size, a, b) \
|
||||
munit_assert_memory_not_equal(size, a, b)
|
||||
#define assert_ptr_equal(a, b) munit_assert_ptr_equal(a, b)
|
||||
#define assert_ptr_not_equal(a, b) munit_assert_ptr_not_equal(a, b)
|
||||
#define assert_ptr_null(ptr) munit_assert_null_equal(ptr)
|
||||
#define assert_ptr_not_null(ptr) munit_assert_not_null(ptr)
|
||||
|
||||
#define assert_null(ptr) munit_assert_null(ptr)
|
||||
#define assert_not_null(ptr) munit_assert_not_null(ptr)
|
||||
|
||||
#endif /* defined(MUNIT_ENABLE_ASSERT_ALIASES) */
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* !defined(MUNIT_H) */
|
||||
|
||||
#if defined(MUNIT_ENABLE_ASSERT_ALIASES)
|
||||
#if defined(assert)
|
||||
#undef assert
|
||||
#endif
|
||||
#define assert(expr) munit_assert(expr)
|
||||
#endif
|
665
tests/test.c
Normal file
665
tests/test.c
Normal file
|
@ -0,0 +1,665 @@
|
|||
/*
|
||||
* smartmap is MIT-licensed, but for this file:
|
||||
*
|
||||
* To the extent possible under law, the author(s) of this file have
|
||||
* waived all copyright and related or neighboring rights to this
|
||||
* work. See <https://creativecommons.org/publicdomain/zero/1.0/> for
|
||||
* details.
|
||||
*/
|
||||
|
||||
#define MUNIT_NO_FORK (1)
|
||||
#define MUNIT_ENABLE_ASSERT_ALIASES (1)
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "../include/sparsemap.h"
|
||||
#include "common.h"
|
||||
#include "munit.h"
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#pragma warning(disable : 4127)
|
||||
#endif
|
||||
|
||||
|
||||
/* !!! Duplicated here for testing purposes. Keep in sync, or suffer. !!! */
|
||||
struct sparsemap {
|
||||
uint8_t *m_data;
|
||||
size_t m_capacity;
|
||||
size_t m_data_used;
|
||||
};
|
||||
|
||||
struct user_data {
|
||||
int foo;
|
||||
};
|
||||
|
||||
void
|
||||
populate_map(sparsemap_t *map, int size, int max_value)
|
||||
{
|
||||
int array[size];
|
||||
|
||||
setup_test_array(array, size, max_value);
|
||||
//TODO ensure_sequential_set(array, size, 10);
|
||||
shuffle(array, size);
|
||||
for (int i = 0; i < size; i++) {
|
||||
sparsemap_set(map, array[i], true);
|
||||
munit_assert_true(sparsemap_is_set(map, array[i]));
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
return (void *)(uintptr_t)map;
|
||||
}
|
||||
|
||||
static void
|
||||
test_api_tear_down(void *fixture)
|
||||
{
|
||||
sparsemap_t *map = (sparsemap_t *)fixture;
|
||||
assert_ptr_not_null(map);
|
||||
free(map);
|
||||
}
|
||||
|
||||
static MunitResult
|
||||
test_api_static_init(const MunitParameter params[], void *data)
|
||||
{
|
||||
sparsemap_t a_map, *map = &a_map;
|
||||
uint8_t buf[1024] = {0};
|
||||
|
||||
(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);
|
||||
assert_true(map->m_data_used == sizeof(uint32_t));
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_clear_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);
|
||||
|
||||
return (void *)map;
|
||||
}
|
||||
static void
|
||||
test_api_clear_tear_down(void *fixture)
|
||||
{
|
||||
sparsemap_t *map = (sparsemap_t *)fixture;
|
||||
free(map->m_data);
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_clear(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));
|
||||
sparsemap_clear(map);
|
||||
assert_false(sparsemap_is_set(map, 42));
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_open_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);
|
||||
populate_map(map, 1024, 3 * 1024);
|
||||
|
||||
return (void *)map;
|
||||
}
|
||||
static void
|
||||
test_api_open_tear_down(void *fixture)
|
||||
{
|
||||
sparsemap_t *map = (sparsemap_t *)fixture;
|
||||
free(map->m_data);
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_open(const MunitParameter params[], void *data)
|
||||
{
|
||||
sparsemap_t _sm, *sm = &_sm, *map = (sparsemap_t *)data;
|
||||
(void)params;
|
||||
|
||||
assert_ptr_not_null(map);
|
||||
|
||||
sparsemap_open(sm, (uint8_t *)map->m_data, map->m_capacity);
|
||||
for (int i = 0; i < 3 * 1024; i++) {
|
||||
assert_true(sparsemap_is_set(sm, i) == sparsemap_is_set(map, i));
|
||||
}
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_set_data_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);
|
||||
populate_map(map, 1024, 3 * 1024);
|
||||
|
||||
return (void *)map;
|
||||
}
|
||||
static void
|
||||
test_api_set_data_size_tear_down(void *fixture)
|
||||
{
|
||||
sparsemap_t *map = (sparsemap_t *)fixture;
|
||||
free(map->m_data);
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_set_data_size(const MunitParameter params[], void *data)
|
||||
{
|
||||
sparsemap_t *map = (sparsemap_t *)data;
|
||||
(void)params;
|
||||
|
||||
assert_ptr_not_null(map);
|
||||
assert_true(map->m_capacity == 1024);
|
||||
assert_true(map->m_capacity == sparsemap_get_capacity(map));
|
||||
sparsemap_set_data_size(map, 512);
|
||||
assert_true(map->m_capacity == 512);
|
||||
assert_true(map->m_capacity == sparsemap_get_capacity(map));
|
||||
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);
|
||||
|
||||
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;
|
||||
double cap;
|
||||
do {
|
||||
sparsemap_set(map, i++, true);
|
||||
cap = sparsemap_capacity_remaining(map);
|
||||
} while (cap > 1.0);
|
||||
//assert_true(i == 169985); when seed is 8675309
|
||||
assert_true(cap <= 1.0);
|
||||
|
||||
sparsemap_clear(map);
|
||||
i = 0;
|
||||
do {
|
||||
int p = munit_rand_int_range(0, 150000);
|
||||
sparsemap_set(map, p, true);
|
||||
i++;
|
||||
cap = sparsemap_capacity_remaining(map);
|
||||
} while (cap > 2.0);
|
||||
//assert_true(i == 64); when seed is 8675309
|
||||
assert_true(cap <= 2.0);
|
||||
|
||||
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);
|
||||
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_capacity(map);
|
||||
assert_true(size == 1024);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_is_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);
|
||||
populate_map(map, 1024, 3 * 1024);
|
||||
|
||||
return (void *)map;
|
||||
}
|
||||
static void
|
||||
test_api_is_set_tear_down(void *fixture)
|
||||
{
|
||||
sparsemap_t *map = (sparsemap_t *)fixture;
|
||||
free(map->m_data);
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_is_set(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));
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
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] = {0};
|
||||
sparsemap_t portion;
|
||||
(void)params;
|
||||
|
||||
assert_ptr_not_null(map);
|
||||
|
||||
sparsemap_init(&portion, buf, 512);
|
||||
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 void *
|
||||
test_api_select_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);
|
||||
bitmap_from_uint64(map, ((uint64_t)0xfeedface << 32) | 0xbadc0ffee);
|
||||
|
||||
return (void *)map;
|
||||
}
|
||||
static void
|
||||
test_api_select_tear_down(void *fixture)
|
||||
{
|
||||
sparsemap_t *map = (sparsemap_t *)fixture;
|
||||
free(map->m_data);
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_select(const MunitParameter params[], void *data)
|
||||
{
|
||||
sparsemap_t *map = (sparsemap_t *)data;
|
||||
(void)params;
|
||||
|
||||
assert_ptr_not_null(map);
|
||||
|
||||
/* NOTE: select() is 0-based, to get the bit position of the 1st logical bit set
|
||||
call select(map, 0), to get the 18th, select(map, 17), etc. */
|
||||
assert_true(sparsemap_select(map, 0) == 1);
|
||||
assert_true(sparsemap_select(map, 4) == 6);
|
||||
assert_true(sparsemap_select(map, 17) == 26);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_rank_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);
|
||||
|
||||
return (void *)map;
|
||||
}
|
||||
static void
|
||||
test_api_rank_tear_down(void *fixture)
|
||||
{
|
||||
sparsemap_t *map = (sparsemap_t *)fixture;
|
||||
free(map->m_data);
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_rank(const MunitParameter params[], void *data)
|
||||
{
|
||||
int r1, r2;
|
||||
sparsemap_t *map = (sparsemap_t *)data;
|
||||
(void)params;
|
||||
|
||||
assert_ptr_not_null(map);
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
sparsemap_set(map, i, true);
|
||||
}
|
||||
for (int i = 0; i < 10; i++) {
|
||||
assert_true(sparsemap_is_set(map, i));
|
||||
}
|
||||
for (int i = 10; i < 1000; i++) {
|
||||
assert_true(!sparsemap_is_set(map, i));
|
||||
}
|
||||
/* 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
|
||||
positions (starting from the LSB) in the index. */
|
||||
r1 = rank_uint64((uint64_t)-1, 0, 9);
|
||||
r2 = sparsemap_rank(map, 0, 9);
|
||||
assert_true(r1 == r2);
|
||||
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 j = i; j < 10; j++) {
|
||||
r1 = rank_uint64((uint64_t)-1, i, j);
|
||||
r2 = sparsemap_rank(map, i, j);
|
||||
assert_true(r1 == r2);
|
||||
}
|
||||
}
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_span_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);
|
||||
|
||||
return (void *)map;
|
||||
}
|
||||
static void
|
||||
test_api_span_tear_down(void *fixture)
|
||||
{
|
||||
sparsemap_t *map = (sparsemap_t *)fixture;
|
||||
free(map->m_data);
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_span(const MunitParameter params[], void *data)
|
||||
{
|
||||
sparsemap_t *map = (sparsemap_t *)data;
|
||||
(void)params;
|
||||
|
||||
assert_ptr_not_null(map);
|
||||
|
||||
int located_at, placed_at, amt = 5000;
|
||||
for (int i = 1; i < amt; i++) {
|
||||
for (int j = 1; j < amt / 10; j++) {
|
||||
sparsemap_clear(map);
|
||||
placed_at = create_sequential_set_in_empty_map(map, amt, j);
|
||||
//logf("i = %d, j = %d\tplaced_at %d\n", i, j, placed_at);
|
||||
//whats_set(map, 5000);
|
||||
located_at = sparsemap_span(map, 0, j);
|
||||
if (placed_at != located_at)
|
||||
logf("a: i = %d, j = %d\tplaced_at %d located_at %d\n", i, j, placed_at, located_at);
|
||||
assert_true(located_at == placed_at);
|
||||
}
|
||||
}
|
||||
/*
|
||||
for (int i = 1; i < amt; i++) {
|
||||
for (int j = 1; j < amt / 10; j++) {
|
||||
sparsemap_clear(map);
|
||||
populate_map(map, 1024, 3 * 1024);
|
||||
placed_at = create_sequential_set_in_empty_map(map, amt, j);
|
||||
located_at = sparsemap_span(map, 0, j);
|
||||
if (located_at >= placed_at)
|
||||
logf("b: i = %d, j = %d\tplaced_at %d located_at %d\n", i, j, placed_at, located_at);
|
||||
//assert_true(located_at >= placed_at);
|
||||
located_at = sparsemap_span(map, (placed_at < j ? 0 : placed_at / 2), i);
|
||||
assert_true(placed_at == located_at);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
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/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/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 },
|
||||
{ (char *)"/api/select", test_api_select, test_api_select_setup, test_api_select_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/api/rank", test_api_rank, test_api_rank_setup, test_api_rank_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/api/span", test_api_span, test_api_span_setup, test_api_span_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 } };
|
||||
|
||||
static MunitSuite other_test_suite[] = { { "/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
|
||||
main(int argc, char *argv[MUNIT_ARRAY_PARAM(argc + 1)])
|
||||
{
|
||||
struct user_data info;
|
||||
return munit_suite_main(&main_test_suite, (void *)&info, argc, argv);
|
||||
}
|
||||
|
||||
/* ARGS: --no-fork --seed 8675309 */
|
Loading…
Reference in a new issue