2024-04-07 20:38:57 +00:00
|
|
|
#include <assert.h>
|
2024-05-03 19:15:39 +00:00
|
|
|
#include <common.h>
|
|
|
|
#include <sparsemap.h>
|
2024-04-07 20:38:57 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#define TEST_ARRAY_SIZE 1024
|
|
|
|
|
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
2024-04-08 22:14:47 +00:00
|
|
|
int i;
|
2024-04-07 20:38:57 +00:00
|
|
|
int array[TEST_ARRAY_SIZE];
|
|
|
|
|
2024-04-08 02:20:35 +00:00
|
|
|
xorshift32_seed();
|
2024-04-07 20:38:57 +00:00
|
|
|
|
|
|
|
// 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
|
2024-04-08 22:14:47 +00:00
|
|
|
uint8_t *buf = calloc((size_t)3 * 1024, sizeof(uint8_t));
|
2024-04-07 20:38:57 +00:00
|
|
|
|
|
|
|
// create the sparse bitmap
|
2024-04-24 20:32:09 +00:00
|
|
|
sparsemap_t *map = sparsemap_wrap(buf, sizeof(uint8_t) * 3 * 1024);
|
2024-04-07 20:38:57 +00:00
|
|
|
|
|
|
|
// create an array of ints
|
2024-04-08 02:20:35 +00:00
|
|
|
setup_test_array(array, TEST_ARRAY_SIZE, 1024 * 3);
|
2024-04-07 20:38:57 +00:00
|
|
|
|
|
|
|
// randomize setting the bits on
|
2024-04-08 02:20:35 +00:00
|
|
|
shuffle(array, TEST_ARRAY_SIZE);
|
2024-04-08 02:35:42 +00:00
|
|
|
// print_array(array, TEST_ARRAY_SIZE);
|
|
|
|
// print_spans(array, TEST_ARRAY_SIZE);
|
2024-04-07 20:38:57 +00:00
|
|
|
|
|
|
|
// set all the bits on in a random order
|
|
|
|
for (i = 0; i < TEST_ARRAY_SIZE; i++) {
|
2024-07-29 09:31:55 +00:00
|
|
|
sparsemap_set(map, array[i]);
|
2024-04-07 20:38:57 +00:00
|
|
|
assert(sparsemap_is_set(map, array[i]) == true);
|
|
|
|
}
|
|
|
|
|
2024-04-08 02:35:42 +00:00
|
|
|
// 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++) {
|
2024-04-07 20:38:57 +00:00
|
|
|
__diag("================> %lu\n", len);
|
|
|
|
sparsemap_clear(map);
|
|
|
|
// set all the bits on in a random order
|
2024-04-08 22:14:47 +00:00
|
|
|
ensure_sequential_set(array, TEST_ARRAY_SIZE, (int)len);
|
2024-04-08 02:20:35 +00:00
|
|
|
shuffle(array, TEST_ARRAY_SIZE);
|
2024-04-07 20:38:57 +00:00
|
|
|
print_spans(array, TEST_ARRAY_SIZE);
|
|
|
|
for (i = 0; i < TEST_ARRAY_SIZE; i++) {
|
2024-07-29 09:31:55 +00:00
|
|
|
sparsemap_set(map, array[i]);
|
2024-04-07 20:38:57 +00:00
|
|
|
assert(sparsemap_is_set(map, array[i]) == true);
|
|
|
|
}
|
2024-04-08 22:14:47 +00:00
|
|
|
has_span(map, array, TEST_ARRAY_SIZE, (int)len);
|
2024-04-24 20:32:09 +00:00
|
|
|
size_t l = sparsemap_span(map, 0, len, true);
|
2024-04-07 20:38:57 +00:00
|
|
|
if (l != (size_t)-1) {
|
|
|
|
__diag("Found span in map starting at %lu of length %lu\n", l, len);
|
|
|
|
__diag("is_span(%lu, %lu) == %s\n", l, len, is_span(array, TEST_ARRAY_SIZE, l, len) ? "yes" : "no");
|
|
|
|
i = (int)l;
|
|
|
|
do {
|
|
|
|
bool set = sparsemap_is_set(map, i);
|
|
|
|
if (set) {
|
|
|
|
__diag("verified %d was set\n", i);
|
|
|
|
} else {
|
2024-04-08 22:14:47 +00:00
|
|
|
__diag("darn, %d was not really set, %s\n", i, is_set(array, i) ? "but we thought it was" : "because it wasn't");
|
2024-04-07 20:38:57 +00:00
|
|
|
}
|
2024-04-08 22:14:47 +00:00
|
|
|
} while (++i < (int)(l + len));
|
2024-04-07 20:38:57 +00:00
|
|
|
} else {
|
|
|
|
__diag("UNABLE TO FIND SPAN in map of length %lu\n", len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|