Merge branch 'main' into gburd/tests
This commit is contained in:
commit
38a8ccf748
15 changed files with 765 additions and 3563 deletions
|
@ -102,7 +102,7 @@ PointerAlignment: Right
|
|||
ContinuationIndentWidth: 2
|
||||
IndentWidth: 2
|
||||
TabWidth: 2
|
||||
ColumnLimit: 80
|
||||
ColumnLimit: 160
|
||||
UseTab: Never
|
||||
SpaceAfterCStyleCast: false
|
||||
IncludeBlocks: Regroup
|
||||
|
|
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -2,7 +2,7 @@
|
|||
*.so
|
||||
**/*.o
|
||||
tests/test
|
||||
examples/ex_*
|
||||
examples/ex_?
|
||||
.cache
|
||||
hints.txt
|
||||
tmp/
|
||||
|
@ -13,6 +13,8 @@ git.diff
|
|||
.codelite/
|
||||
.cmaketools.json
|
||||
*.tags
|
||||
tags
|
||||
TAGS
|
||||
*.dll
|
||||
build/
|
||||
cmake-build*
|
||||
|
|
|
@ -1,20 +1,22 @@
|
|||
#include <assert.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.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)
|
||||
#define __diag(...) \
|
||||
do { \
|
||||
fprintf(stderr, "%s:%d:%s(): ", __FILE__, __LINE__, __func__); \
|
||||
fprintf(stderr, __VA_ARGS__); \
|
||||
} while (0)
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
// NOTE: currently, this code serves as a sample and unittest.
|
||||
|
||||
int main() {
|
||||
int
|
||||
main()
|
||||
{
|
||||
size_t size = 4;
|
||||
setbuf(stderr, 0); // disable buffering
|
||||
__diag("Please wait a moment...");
|
||||
|
@ -85,11 +87,11 @@ int main() {
|
|||
}
|
||||
|
||||
// open and compare
|
||||
sparsemap_t *sm2 = sparsemap_open(buffer, sizeof(buffer));
|
||||
sparsemap_t _sm3, *sm3 = &_sm3;
|
||||
sparsemap_open(sm3, buffer, sizeof(buffer));
|
||||
for (int i = 0; i < 10000; i++) {
|
||||
assert(sparsemap_is_set(sm2, i) == sparsemap_is_set(map, i));
|
||||
assert(sparsemap_is_set(sm3, i) == sparsemap_is_set(map, i));
|
||||
}
|
||||
free(sm2);
|
||||
|
||||
// unset [10000..0]
|
||||
for (int i = 10000; i >= 0; i--) {
|
||||
|
@ -152,8 +154,7 @@ int main() {
|
|||
}
|
||||
|
||||
// split and move, aligned to MiniMap capacity
|
||||
sparsemap_t _sm2;
|
||||
sm2 = &_sm2;
|
||||
sparsemap_t _sm2, *sm2 = &_sm2;
|
||||
sparsemap_init(sm2, buffer2, sizeof(buffer2), 0);
|
||||
sparsemap_clear(sm2);
|
||||
for (int i = 0; i < 2048 * 2; i++) {
|
||||
|
@ -187,4 +188,4 @@ int main() {
|
|||
}
|
||||
|
||||
fprintf(stderr, " ok\n");
|
||||
}
|
||||
}
|
||||
|
|
48
examples/ex_2.c
Normal file
48
examples/ex_2.c
Normal file
|
@ -0,0 +1,48 @@
|
|||
#include <assert.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
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
// disable buffering
|
||||
setbuf(stderr, 0);
|
||||
|
||||
// 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);
|
||||
|
||||
// Set every other bit (pathologically worst case) to see what happens
|
||||
// when the map is full.
|
||||
for (i = 0; i < 7744; i++) {
|
||||
if (i % 2)
|
||||
continue;
|
||||
sparsemap_set(map, i, true);
|
||||
assert(sparsemap_is_set(map, i) == true);
|
||||
}
|
||||
// On 1024 KiB of buffer with every other bit set the map holds 7744 bits
|
||||
// and then runs out of space. This next _set() call will fail/abort.
|
||||
sparsemap_set(map, ++i, true);
|
||||
assert(sparsemap_is_set(map, i) == true);
|
||||
return 0;
|
||||
}
|
137
examples/ex_3.c
Normal file
137
examples/ex_3.c
Normal file
|
@ -0,0 +1,137 @@
|
|||
#include <assert.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
|
||||
|
||||
/* https://burtleburtle.net/bob/rand/smallprng.html */
|
||||
typedef struct rnd_ctx {
|
||||
uint32_t a;
|
||||
uint32_t b;
|
||||
uint32_t c;
|
||||
uint32_t d;
|
||||
} rnd_ctx_t;
|
||||
#define __rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
|
||||
uint32_t
|
||||
__random(rnd_ctx_t *x)
|
||||
{
|
||||
uint32_t e = x->a - __rot(x->b, 27);
|
||||
x->a = x->b ^ __rot(x->c, 17);
|
||||
x->b = x->c + x->d;
|
||||
x->c = x->d + e;
|
||||
x->d = e + x->a;
|
||||
return x->d;
|
||||
}
|
||||
|
||||
void
|
||||
__random_seed(rnd_ctx_t *x, uint32_t seed)
|
||||
{
|
||||
uint32_t i;
|
||||
x->a = 0xf1ea5eed, x->b = x->c = x->d = seed;
|
||||
for (i = 0; i < 20; ++i) {
|
||||
(void)__random(x);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
shuffle(rnd_ctx_t *prng, int *array, size_t n)
|
||||
{
|
||||
size_t i, j;
|
||||
|
||||
if (n > 1) {
|
||||
for (i = n - 1; i > 0; i--) {
|
||||
j = (unsigned int)(__random(prng) % (i + 1));
|
||||
// XOR swap algorithm
|
||||
if (i != j) { // avoid self-swap leading to zero-ing the element
|
||||
array[i] = array[i] ^ array[j];
|
||||
array[j] = array[i] ^ array[j];
|
||||
array[i] = array[i] ^ array[j];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
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,
|
||||
113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
|
||||
144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174,
|
||||
175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
|
||||
206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236,
|
||||
237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267,
|
||||
268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298,
|
||||
299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320, 321, 322, 323, 324, 325, 326, 327, 328, 329,
|
||||
330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 360,
|
||||
361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384, 385, 386, 387, 388, 389, 390, 391,
|
||||
392, 393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 419, 420, 421, 422,
|
||||
423, 424, 425, 426, 427, 428, 429, 430, 431, 432, 433, 434, 435, 436, 437, 438, 439, 440, 441, 442, 443, 444, 445, 446, 447, 448, 449, 450, 451, 452, 453,
|
||||
454, 455, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465, 466, 467, 468, 469, 470, 471, 472, 473, 474, 475, 476, 477, 478, 479, 480, 481, 482, 483, 484,
|
||||
485, 486, 487, 488, 489, 490, 491, 492, 493, 494, 495, 496, 497, 498, 499, 500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 511, 512, 513, 514, 515,
|
||||
516, 517, 518, 519, 520, 521, 522, 523, 524, 525, 526, 527, 528, 529, 530, 531, 532, 533, 534, 535, 536, 537, 538, 539, 540, 541, 542, 543, 544, 545, 546,
|
||||
547, 548, 549, 550, 551, 552, 553, 554, 555, 556, 557, 558, 559, 560, 561, 562, 563, 564, 565, 566, 567, 568, 569, 570, 571, 572, 573, 574, 575, 576, 577,
|
||||
578, 579, 580, 581, 582, 583, 584, 585, 586, 587, 588, 589, 590, 591, 592, 593, 594, 595, 596, 597, 598, 599, 600, 601, 602, 603, 604, 605, 606, 607, 608,
|
||||
609, 610, 611, 612, 613, 614, 615, 616, 617, 618, 619, 620, 621, 622, 623, 624, 625, 626, 627, 628, 629, 630, 631, 632, 633, 634, 635, 636, 637, 638, 639,
|
||||
640, 641, 642, 643, 644, 645, 646, 647, 648, 649, 650, 651, 652, 653, 654, 655, 656, 657, 658, 659, 660, 661, 662, 663, 664, 665, 666, 667, 668, 669, 670,
|
||||
671, 672, 673, 674, 675, 676, 677, 678, 679, 680, 681, 682, 683, 684, 685, 686, 687, 688, 689, 690, 691, 692, 693, 694, 695, 696, 697, 698, 699, 700, 701,
|
||||
702, 703, 704, 705, 706, 707, 708, 709, 710, 711, 712, 713, 714, 715, 716, 717, 718, 719, 720, 721, 722, 723, 724, 725, 726, 727, 728, 729, 730, 731, 732,
|
||||
733, 734, 735, 736, 737, 738, 739, 740, 741, 742, 743, 744, 745, 746, 747, 748, 749, 750, 751, 752, 753, 754, 755, 756, 757, 758, 759, 760, 761, 762, 763,
|
||||
764, 765, 766, 767, 768, 769, 770, 771, 772, 773, 774, 775, 776, 777, 778, 779, 780, 781, 782, 783, 784, 785, 786, 787, 788, 789, 790, 791, 792, 793, 794,
|
||||
795, 796, 797, 798, 799, 800, 801, 802, 803, 804, 805, 806, 807, 808, 809, 810, 811, 812, 813, 814, 815, 816, 817, 818, 819, 820, 821, 822, 823, 824, 825,
|
||||
826, 827, 828, 829, 830, 831, 832, 833, 834, 835, 836, 837, 838, 839, 840, 841, 842, 843, 844, 845, 846, 847, 848, 849, 850, 851, 852, 853, 854, 855, 856,
|
||||
857, 858, 859, 860, 861, 862, 863, 864, 865, 866, 867, 868, 869, 870, 871, 872, 873, 874, 875, 876, 877, 878, 879, 880, 881, 882, 883, 884, 885, 886, 887,
|
||||
888, 889, 890, 891, 892, 893, 894, 895, 896, 897, 898, 899, 900, 901, 902, 903, 904, 905, 906, 907, 908, 909, 910, 911, 912, 913, 914, 915, 916, 917, 918,
|
||||
919, 920, 921, 922, 923, 924, 925, 926, 927, 928, 929, 930, 931, 932, 933, 934, 935, 936, 937, 938, 939, 940, 941, 942, 943, 944, 945, 946, 947, 948, 949,
|
||||
950, 951, 952, 953, 954, 955, 956, 957, 958, 959, 960, 961, 962, 963, 964, 965, 966, 967, 968, 969, 970, 971, 972, 973, 974, 975, 976, 977, 978, 979, 980,
|
||||
981, 982, 983, 984, 985, 986, 987, 988, 989, 990, 991, 992, 993, 994, 995, 996, 997, 998, 999, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009,
|
||||
1010, 1011, 1012, 1013, 1014, 1015, 1016, 1017, 1018, 1019, 1020, 1021, 1022, 1023, 1024 };
|
||||
|
||||
// disable buffering
|
||||
setbuf(stderr, 0);
|
||||
|
||||
// seed the PRNG
|
||||
#ifdef SEED
|
||||
__random_seed(&prng, 8675309);
|
||||
#else
|
||||
__random_seed(&prng, (unsigned int)time(NULL) ^ getpid());
|
||||
#endif
|
||||
|
||||
// randomize setting the bits on
|
||||
shuffle(&prng, 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);
|
||||
|
||||
// set all the bits on in a random order
|
||||
for (i = 0; i < 1024; i++) {
|
||||
__diag("set %d\n", array[i]);
|
||||
sparsemap_set(map, array[i], true);
|
||||
assert(sparsemap_is_set(map, array[i]) == true);
|
||||
}
|
||||
|
||||
sparsemap_set(map, 1025, true);
|
||||
assert(sparsemap_is_set(map, 1025) == true);
|
||||
|
||||
return 0;
|
||||
}
|
329
examples/ex_4.c
Normal file
329
examples/ex_4.c
Normal file
|
@ -0,0 +1,329 @@
|
|||
#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;
|
||||
}
|
||||
|
||||
#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 array[TEST_ARRAY_SIZE];
|
||||
int prng;
|
||||
|
||||
// seed the PRNG
|
||||
#ifdef SEED
|
||||
prng = 8675309;
|
||||
#else
|
||||
prng = (unsigned int)time(NULL) ^ getpid();
|
||||
#endif
|
||||
|
||||
// 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));
|
||||
|
||||
// create the sparse bitmap
|
||||
sparsemap_t *map = sparsemap(buf, sizeof(uint8_t) * 3 * 1024, 0);
|
||||
|
||||
// create an array of ints
|
||||
setup_test_array(array, TEST_ARRAY_SIZE, 1024 * 3, &prng);
|
||||
|
||||
// randomize setting the bits on
|
||||
shuffle(array, TEST_ARRAY_SIZE, &prng);
|
||||
//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++) {
|
||||
sparsemap_set(map, array[i], true);
|
||||
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++) {
|
||||
__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);
|
||||
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);
|
||||
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);
|
||||
__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 {
|
||||
__diag("darn, %d was not really set, %s\n", i, was_set(i, array) ? "but we thought it was" : "because it wasn't");
|
||||
}
|
||||
} while (++i < l + len);
|
||||
} else {
|
||||
__diag("UNABLE TO FIND SPAN in map of length %lu\n", len);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
61
flake.lock
61
flake.lock
|
@ -0,0 +1,61 @@
|
|||
{
|
||||
"nodes": {
|
||||
"flake-utils": {
|
||||
"inputs": {
|
||||
"systems": "systems"
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1710146030,
|
||||
"narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=",
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1712192574,
|
||||
"narHash": "sha256-LbbVOliJKTF4Zl2b9salumvdMXuQBr2kuKP5+ZwbYq4=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "f480f9d09e4b4cf87ee6151eba068197125714de",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "NixOS",
|
||||
"ref": "nixpkgs-unstable",
|
||||
"repo": "nixpkgs",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"flake-utils": "flake-utils",
|
||||
"nixpkgs": "nixpkgs"
|
||||
}
|
||||
},
|
||||
"systems": {
|
||||
"locked": {
|
||||
"lastModified": 1681028828,
|
||||
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
|
||||
"owner": "nix-systems",
|
||||
"repo": "default",
|
||||
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "nix-systems",
|
||||
"repo": "default",
|
||||
"type": "github"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
71
flake.nix
71
flake.nix
|
@ -1,8 +1,10 @@
|
|||
{
|
||||
description = "A Concurrent Skip List library for key/value pairs.";
|
||||
|
||||
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
|
||||
inputs.flake-utils.url = "github:numtide/flake-utils";
|
||||
inputs = {
|
||||
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
|
||||
flake-utils.url = "github:numtide/flake-utils";
|
||||
};
|
||||
|
||||
outputs =
|
||||
{ self
|
||||
|
@ -12,40 +14,45 @@
|
|||
}:
|
||||
flake-utils.lib.eachDefaultSystem (system:
|
||||
let
|
||||
# pkgs = nixpkgs.legacyPackages.${system};
|
||||
pkgs = import nixpkgs {
|
||||
inherit system;
|
||||
config = { allowUnfree = true; };
|
||||
};
|
||||
in
|
||||
{
|
||||
devShells.default = pkgs.mkShell {
|
||||
packages = with pkgs; [
|
||||
autoconf
|
||||
bashInteractive
|
||||
clang-tools
|
||||
ed
|
||||
gdb
|
||||
graphviz-nox
|
||||
meson
|
||||
python311Packages.rbtools
|
||||
];
|
||||
supportedSystems = [ "x86_64-linux" ];
|
||||
forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
|
||||
nixpkgsFor = forAllSystems (system: import nixpkgs {
|
||||
inherit system;
|
||||
overlays = [ self.overlay ];
|
||||
});
|
||||
in {
|
||||
pkgs = import nixpkgs {
|
||||
inherit system;
|
||||
devShell = nixpkgs.legacyPackages.${system} {
|
||||
pkgs.mkShell = {
|
||||
nativeBuildInputs = with pkgs.buildPackages; [
|
||||
act
|
||||
autoconf
|
||||
clang
|
||||
ed
|
||||
gcc
|
||||
gdb
|
||||
gettext
|
||||
graphviz-nox
|
||||
libtool
|
||||
m4
|
||||
perl
|
||||
pkg-config
|
||||
python3
|
||||
ripgrep
|
||||
];
|
||||
buildInputs = with pkgs; [
|
||||
libbacktrace
|
||||
glibc.out
|
||||
glibc.static
|
||||
];
|
||||
};
|
||||
DOCKER_BUILDKIT = 1;
|
||||
};
|
||||
};
|
||||
buildInputs = with pkgs; [
|
||||
glibc
|
||||
];
|
||||
nativeBuildInputs = with pkgs.buildPackages; [
|
||||
act
|
||||
binutils
|
||||
coreutils
|
||||
gcc
|
||||
gettext
|
||||
libtool
|
||||
m4
|
||||
make
|
||||
perl
|
||||
pkg-config
|
||||
ripgrep
|
||||
];
|
||||
});
|
||||
}
|
||||
|
|
|
@ -84,7 +84,7 @@ void sparsemap_init(sparsemap_t *map, uint8_t *data, size_t size, size_t used);
|
|||
void sparsemap_clear(sparsemap_t *map);
|
||||
|
||||
/* Opens an existing sparsemap at the specified buffer. */
|
||||
sparsemap_t *sparsemap_open(uint8_t *data, size_t data_size);
|
||||
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);
|
||||
|
@ -105,8 +105,7 @@ sm_idx_t sparsemap_get_start_offset(sparsemap_t *map);
|
|||
size_t sparsemap_get_size(sparsemap_t *map);
|
||||
|
||||
/* Decompresses the whole bitmap; calls scanner for all bits. */
|
||||
void sparsemap_scan(sparsemap_t *map, void (*scanner)(sm_idx_t[], size_t),
|
||||
size_t skip);
|
||||
void sparsemap_scan(sparsemap_t *map, void (*scanner)(sm_idx_t[], size_t), size_t skip);
|
||||
|
||||
/* Appends all chunk maps from |map| starting at |sstart| to |other|, then
|
||||
reduces the chunk map-count appropriately. */
|
||||
|
@ -118,4 +117,6 @@ size_t sparsemap_select(sparsemap_t *map, size_t n);
|
|||
/* Counts the set bits in the range [offset, idx]. */
|
||||
size_t sparsemap_rank(sparsemap_t *map, size_t offset, size_t idx);
|
||||
|
||||
size_t sparsemap_span(sparsemap_t *map, size_t loc, size_t len);
|
||||
|
||||
#endif
|
||||
|
|
BIN
main
BIN
main
Binary file not shown.
217
src/sparsemap.c
217
src/sparsemap.c
|
@ -15,27 +15,22 @@
|
|||
* PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wpedantic"
|
||||
#include <assert.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
#include <errno.h>
|
||||
#include <popcount.h>
|
||||
#include <sparsemap.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef SPARSEMAP_DIAGNOSTIC
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wpedantic"
|
||||
#pragma GCC diagnostic ignored "-Wvariadic-macros"
|
||||
#define __sm_diag(format, ...) \
|
||||
__sm_diag_(__FILE__, __LINE__, __func__, format, ##__VA_ARGS__)
|
||||
#include <stdarg.h>
|
||||
#define __sm_diag(format, ...) __sm_diag_(__FILE__, __LINE__, __func__, format, ##__VA_ARGS__)
|
||||
#pragma GCC diagnostic pop
|
||||
void __attribute__((format(printf, 4, 5))) __sm_diag_(const char *file,
|
||||
int line, const char *func, const char *format, ...)
|
||||
void __attribute__((format(printf, 4, 5))) __sm_diag_(const char *file, int line, const char *func, const char *format, ...)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
|
@ -49,10 +44,9 @@ void __attribute__((format(printf, 4, 5))) __sm_diag_(const char *file,
|
|||
|
||||
#ifndef SPARSEMAP_ASSERT
|
||||
#define SPARSEMAP_ASSERT
|
||||
#define __sm_assert(expr) \
|
||||
if (!(expr)) \
|
||||
fprintf(stderr, "%s:%d:%s(): assertion failed! %s", __FILE__, __LINE__, \
|
||||
__func__, #expr)
|
||||
#define __sm_assert(expr) \
|
||||
if (!(expr)) \
|
||||
fprintf(stderr, "%s:%d:%s(): assertion failed! %s", __FILE__, __LINE__, __func__, #expr)
|
||||
#else
|
||||
#define __sm_assert(expr) ((void)0)
|
||||
#endif
|
||||
|
@ -61,7 +55,7 @@ enum __SM_CHUNK_INFO {
|
|||
/* metadata overhead: 4 bytes for __sm_chunk_t count */
|
||||
SM_SIZEOF_OVERHEAD = sizeof(uint32_t),
|
||||
|
||||
/* number of bits that can be stored in a BitVector */
|
||||
/* number of bits that can be stored in a sm_bitvec_t */
|
||||
SM_BITS_PER_VECTOR = (sizeof(sm_bitvec_t) * 8),
|
||||
|
||||
/* number of flags that can be stored in a single index byte */
|
||||
|
@ -139,8 +133,7 @@ static size_t
|
|||
__sm_chunk_map_get_position(__sm_chunk_t *map, size_t bv)
|
||||
{
|
||||
// handle 4 indices (1 byte) at a time
|
||||
size_t num_bytes = bv /
|
||||
((size_t)SM_FLAGS_PER_INDEX_BYTE * SM_BITS_PER_VECTOR);
|
||||
size_t num_bytes = bv / ((size_t)SM_FLAGS_PER_INDEX_BYTE * SM_BITS_PER_VECTOR);
|
||||
|
||||
size_t position = 0;
|
||||
register uint8_t *p = (uint8_t *)map->m_data;
|
||||
|
@ -150,8 +143,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 = ((*map->m_data) & ((sm_bitvec_t)SM_FLAG_MASK << (i * 2))) >> (i * 2);
|
||||
if (flags == SM_PAYLOAD_MIXED) {
|
||||
position++;
|
||||
}
|
||||
|
@ -163,7 +155,7 @@ __sm_chunk_map_get_position(__sm_chunk_t *map, size_t bv)
|
|||
/**
|
||||
* Initialize __sm_chunk_t with provided data.
|
||||
*/
|
||||
static void
|
||||
static inline void
|
||||
__sm_chunk_map_init(__sm_chunk_t *map, uint8_t *data)
|
||||
{
|
||||
map->m_data = (sm_bitvec_t *)data;
|
||||
|
@ -236,8 +228,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 = ((*p) & ((sm_bitvec_t)SM_FLAG_MASK << (j * 2))) >> (j * 2);
|
||||
if (flags != SM_PAYLOAD_NONE && flags != SM_PAYLOAD_ZEROS) {
|
||||
return (false);
|
||||
}
|
||||
|
@ -275,8 +266,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 = ((*map->m_data) & ((sm_bitvec_t)SM_FLAG_MASK << (bv * 2))) >> (bv * 2);
|
||||
switch (flags) {
|
||||
case SM_PAYLOAD_ZEROS:
|
||||
case SM_PAYLOAD_NONE:
|
||||
|
@ -304,16 +294,14 @@ __sm_chunk_map_is_set(__sm_chunk_t *map, size_t idx)
|
|||
* this time with |retried| = true.
|
||||
*/
|
||||
static int
|
||||
__sm_chunk_map_set(__sm_chunk_t *map, size_t idx, bool value, size_t *pos,
|
||||
sm_bitvec_t *fill, bool retried)
|
||||
__sm_chunk_map_set(__sm_chunk_t *map, size_t idx, bool value, size_t *pos, sm_bitvec_t *fill, bool retried)
|
||||
{
|
||||
/* In which sm_bitvec_t is |idx| stored? */
|
||||
size_t bv = idx / SM_BITS_PER_VECTOR;
|
||||
__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 = ((*map->m_data) & ((sm_bitvec_t)SM_FLAG_MASK << (bv * 2))) >> (bv * 2);
|
||||
assert(flags != SM_PAYLOAD_NONE);
|
||||
if (flags == SM_PAYLOAD_ZEROS) {
|
||||
/* Easy - set bit to 0 in a sm_bitvec_t of zeroes. */
|
||||
|
@ -383,11 +371,11 @@ __sm_chunk_map_set(__sm_chunk_t *map, size_t idx, bool value, size_t *pos,
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns the index of the 'nth' set bit; sets |*pnew_n| to 0 if the
|
||||
* 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|
|
||||
*/
|
||||
static size_t
|
||||
__sm_chunk_map_select(__sm_chunk_t *map, ssize_t n, ssize_t *pnew_n)
|
||||
__sm_chunk_map_select(__sm_chunk_t *map, size_t n, ssize_t *pnew_n)
|
||||
{
|
||||
size_t ret = 0;
|
||||
register uint8_t *p;
|
||||
|
@ -419,8 +407,7 @@ __sm_chunk_map_select(__sm_chunk_t *map, ssize_t n, ssize_t *pnew_n)
|
|||
return (ret + n);
|
||||
}
|
||||
if (flags == SM_PAYLOAD_MIXED) {
|
||||
sm_bitvec_t w = map->m_data[1 +
|
||||
__sm_chunk_map_get_position(map, i * SM_FLAGS_PER_INDEX_BYTE + j)];
|
||||
sm_bitvec_t w = map->m_data[1 + __sm_chunk_map_get_position(map, i * SM_FLAGS_PER_INDEX_BYTE + j)];
|
||||
for (int k = 0; k < SM_BITS_PER_VECTOR; k++) {
|
||||
if (w & ((sm_bitvec_t)1 << k)) {
|
||||
if (n == 0) {
|
||||
|
@ -435,15 +422,15 @@ __sm_chunk_map_select(__sm_chunk_t *map, ssize_t n, ssize_t *pnew_n)
|
|||
}
|
||||
}
|
||||
|
||||
*pnew_n = n;
|
||||
*pnew_n = (ssize_t)n;
|
||||
return (ret);
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts the set bits in the range [0, idx].
|
||||
* Counts the set bits in the range [first, last] inclusive.
|
||||
*/
|
||||
static size_t
|
||||
__sm_chunk_map_rank(__sm_chunk_t *map, size_t idx)
|
||||
__sm_chunk_map_rank(__sm_chunk_t *map, size_t first, size_t last, size_t *after)
|
||||
{
|
||||
size_t ret = 0;
|
||||
|
||||
|
@ -455,27 +442,64 @@ __sm_chunk_map_rank(__sm_chunk_t *map, size_t idx)
|
|||
continue;
|
||||
}
|
||||
if (flags == SM_PAYLOAD_ZEROS) {
|
||||
if (idx > SM_BITS_PER_VECTOR) {
|
||||
idx -= SM_BITS_PER_VECTOR;
|
||||
if (last > SM_BITS_PER_VECTOR) {
|
||||
if (*after > SM_BITS_PER_VECTOR) {
|
||||
*after = *after - SM_BITS_PER_VECTOR;
|
||||
} else {
|
||||
last -= SM_BITS_PER_VECTOR - *after;
|
||||
*after = 0;
|
||||
}
|
||||
} else {
|
||||
return (ret);
|
||||
}
|
||||
} else if (flags == SM_PAYLOAD_ONES) {
|
||||
if (idx > SM_BITS_PER_VECTOR) {
|
||||
idx -= SM_BITS_PER_VECTOR;
|
||||
ret += SM_BITS_PER_VECTOR;
|
||||
if (last > SM_BITS_PER_VECTOR) {
|
||||
if (*after > SM_BITS_PER_VECTOR) {
|
||||
*after = *after - SM_BITS_PER_VECTOR;
|
||||
} else {
|
||||
last -= SM_BITS_PER_VECTOR - *after;
|
||||
if (*after == 0) {
|
||||
ret += SM_BITS_PER_VECTOR;
|
||||
}
|
||||
*after = 0;
|
||||
}
|
||||
} else {
|
||||
return (ret + idx);
|
||||
return (ret + last);
|
||||
}
|
||||
} else if (flags == SM_PAYLOAD_MIXED) {
|
||||
if (idx > SM_BITS_PER_VECTOR) {
|
||||
idx -= SM_BITS_PER_VECTOR;
|
||||
ret += popcountll((uint64_t)map->m_data[1 +
|
||||
__sm_chunk_map_get_position(map, i * SM_FLAGS_PER_INDEX_BYTE + j)]);
|
||||
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);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
sm_bitvec_t w = map->m_data[1 +
|
||||
__sm_chunk_map_get_position(map, i * SM_FLAGS_PER_INDEX_BYTE + j)];
|
||||
for (size_t k = 0; k < idx; k++) {
|
||||
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++;
|
||||
}
|
||||
|
@ -493,8 +517,7 @@ __sm_chunk_map_rank(__sm_chunk_t *map, size_t idx)
|
|||
* Returns the number of (set) bits that were passed to the scanner
|
||||
*/
|
||||
static size_t
|
||||
__sm_chunk_map_scan(__sm_chunk_t *map, sm_idx_t start,
|
||||
void (*scanner)(sm_idx_t[], size_t), size_t skip)
|
||||
__sm_chunk_map_scan(__sm_chunk_t *map, sm_idx_t start, void (*scanner)(sm_idx_t[], size_t), size_t skip)
|
||||
{
|
||||
size_t ret = 0;
|
||||
register uint8_t *p = (uint8_t *)map->m_data;
|
||||
|
@ -531,8 +554,7 @@ __sm_chunk_map_scan(__sm_chunk_t *map, sm_idx_t start,
|
|||
ret += SM_BITS_PER_VECTOR;
|
||||
}
|
||||
} else if (flags == SM_PAYLOAD_MIXED) {
|
||||
sm_bitvec_t w = map->m_data[1 +
|
||||
__sm_chunk_map_get_position(map, i * SM_FLAGS_PER_INDEX_BYTE + j)];
|
||||
sm_bitvec_t w = map->m_data[1 + __sm_chunk_map_get_position(map, i * SM_FLAGS_PER_INDEX_BYTE + j)];
|
||||
int n = 0;
|
||||
if (skip) {
|
||||
for (int b = 0; b < SM_BITS_PER_VECTOR; b++) {
|
||||
|
@ -647,7 +669,7 @@ __sm_get_chunk_map_offset(sparsemap_t *map, size_t idx)
|
|||
uint8_t *p = start;
|
||||
|
||||
for (size_t i = 0; i < count - 1; i++) {
|
||||
sm_idx_t start = *(sm_idx_t *)p; //TODO wtf...
|
||||
sm_idx_t start = *(sm_idx_t *)p;
|
||||
__sm_assert(start == __sm_get_aligned_offset(start));
|
||||
__sm_chunk_t chunk;
|
||||
__sm_chunk_map_init(&chunk, p + sizeof(sm_idx_t));
|
||||
|
@ -692,9 +714,8 @@ __sm_append_data(sparsemap_t *map, uint8_t *buffer, size_t buffer_size)
|
|||
/**
|
||||
* Inserts data somewhere in the middle of m_data.
|
||||
*/
|
||||
static void
|
||||
__sm_insert_data(sparsemap_t *map, size_t offset, 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) {
|
||||
__sm_assert(!"buffer overflow");
|
||||
|
@ -705,6 +726,7 @@ __sm_insert_data(sparsemap_t *map, size_t offset, uint8_t *buffer,
|
|||
memmove(p + buffer_size, p, map->m_data_used - offset);
|
||||
memcpy(p, buffer, buffer_size);
|
||||
map->m_data_used += buffer_size;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -720,8 +742,8 @@ __sm_remove_data(sparsemap_t *map, size_t offset, size_t gap_size)
|
|||
}
|
||||
|
||||
/**
|
||||
* Clears the whole buffer
|
||||
*/
|
||||
* Clears the whole buffer
|
||||
*/
|
||||
void
|
||||
sparsemap_clear(sparsemap_t *map)
|
||||
{
|
||||
|
@ -757,16 +779,12 @@ sparsemap_init(sparsemap_t *map, uint8_t *data, size_t size, size_t used)
|
|||
/**
|
||||
* Opens an existing sparsemap at the specified buffer.
|
||||
*/
|
||||
sparsemap_t *
|
||||
sparsemap_open(uint8_t *data, size_t data_size)
|
||||
void
|
||||
sparsemap_open(sparsemap_t *map, uint8_t *data, size_t data_size)
|
||||
{
|
||||
sparsemap_t *map = (sparsemap_t *)calloc(1, sizeof(sparsemap_t));
|
||||
if (map) {
|
||||
map->m_data = data;
|
||||
map->m_data_used = 0;
|
||||
map->m_data_size = data_size;
|
||||
}
|
||||
return map;
|
||||
map->m_data = data;
|
||||
map->m_data_used = 0;
|
||||
map->m_data_size = data_size;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -922,8 +940,7 @@ sparsemap_set(sparsemap_t *map, size_t idx, bool value)
|
|||
/* Now update the __sm_chunk_t. */
|
||||
size_t position;
|
||||
sm_bitvec_t fill;
|
||||
int code = __sm_chunk_map_set(&chunk, idx - start, value, &position, &fill,
|
||||
false);
|
||||
int code = __sm_chunk_map_set(&chunk, idx - start, value, &position, &fill, false);
|
||||
switch (code) {
|
||||
case SM_OK:
|
||||
break;
|
||||
|
@ -932,8 +949,7 @@ sparsemap_set(sparsemap_t *map, size_t idx, bool value)
|
|||
offset += 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);
|
||||
code = __sm_chunk_map_set(&chunk, idx - start, value, &position, &fill, true);
|
||||
__sm_assert(code == SM_OK);
|
||||
break;
|
||||
case SM_NEEDS_TO_SHRINK:
|
||||
|
@ -985,8 +1001,7 @@ sparsemap_get_size(sparsemap_t *map)
|
|||
* Decompresses the whole bitmap; calls scanner for all bits.
|
||||
*/
|
||||
void
|
||||
sparsemap_scan(sparsemap_t *map, void (*scanner)(sm_idx_t[], size_t),
|
||||
size_t skip)
|
||||
sparsemap_scan(sparsemap_t *map, void (*scanner)(sm_idx_t[], size_t), size_t skip)
|
||||
{
|
||||
uint8_t *p = __sm_get_chunk_map_data(map, 0);
|
||||
size_t count = __sm_get_chunk_map_count(map);
|
||||
|
@ -1135,7 +1150,6 @@ sparsemap_select(sparsemap_t *map, size_t n)
|
|||
assert(sparsemap_get_size(map) >= SM_SIZEOF_OVERHEAD);
|
||||
size_t result = 0;
|
||||
size_t 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++) {
|
||||
|
@ -1144,7 +1158,7 @@ sparsemap_select(sparsemap_t *map, size_t n)
|
|||
__sm_chunk_t chunk;
|
||||
__sm_chunk_map_init(&chunk, p);
|
||||
|
||||
ssize_t new_n = n;
|
||||
ssize_t new_n = (ssize_t)n;
|
||||
size_t index = __sm_chunk_map_select(&chunk, n, &new_n);
|
||||
if (new_n == -1) {
|
||||
return (result + index);
|
||||
|
@ -1153,33 +1167,62 @@ sparsemap_select(sparsemap_t *map, size_t n)
|
|||
|
||||
p += __sm_chunk_map_get_size(&chunk);
|
||||
}
|
||||
#ifdef DEBUG
|
||||
assert(!"shouldn't be here");
|
||||
return (0);
|
||||
#endif
|
||||
return (size_t)-1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts the set bits in the range [offset, idx].
|
||||
* Counts the set bits in the range [first, last] inclusive.
|
||||
*/
|
||||
size_t
|
||||
sparsemap_rank(sparsemap_t *map, size_t offset, size_t idx)
|
||||
sparsemap_rank(sparsemap_t *map, size_t first, size_t last)
|
||||
{
|
||||
assert(sparsemap_get_size(map) >= SM_SIZEOF_OVERHEAD);
|
||||
size_t result = 0;
|
||||
size_t count = __sm_get_chunk_map_count(map);
|
||||
|
||||
uint8_t *p = __sm_get_chunk_map_data(map, offset);
|
||||
size_t result = 0, after = first, 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 > idx) {
|
||||
if (start > last) {
|
||||
return (result);
|
||||
}
|
||||
p += sizeof(sm_idx_t);
|
||||
__sm_chunk_t chunk;
|
||||
__sm_chunk_map_init(&chunk, p);
|
||||
|
||||
result += __sm_chunk_map_rank(&chunk, idx - start);
|
||||
result += __sm_chunk_map_rank(&chunk, first - start, last - start, &after);
|
||||
p += __sm_chunk_map_get_size(&chunk);
|
||||
}
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a span of set bits of at least |len| after |loc|. Returns the index of
|
||||
* the n'th set bit that starts a span of at least |len| bits set to true.
|
||||
*/
|
||||
size_t
|
||||
sparsemap_span(sparsemap_t *map, size_t loc, size_t len)
|
||||
{
|
||||
size_t offset, nth = 0, count = 0;
|
||||
|
||||
offset = sparsemap_select(map, 0);
|
||||
if (len == 1) {
|
||||
return offset;
|
||||
}
|
||||
do {
|
||||
count = sparsemap_rank(map, offset, offset + len);
|
||||
if (count == len) {
|
||||
return offset;
|
||||
} else {
|
||||
count = len;
|
||||
while (--count && sparsemap_is_set(map, offset)) {
|
||||
nth++;
|
||||
}
|
||||
}
|
||||
offset = sparsemap_select(map, nth);
|
||||
} while (offset != ((size_t)-1));
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
|
196
tests/api.c
196
tests/api.c
|
@ -1,196 +0,0 @@
|
|||
static void *
|
||||
test_api_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
struct test_info *info = (struct test_info *)user_data;
|
||||
(void)info;
|
||||
(void)params;
|
||||
|
||||
ex_sl_t *slist = calloc(sizeof(ex_sl_t), 1);
|
||||
if (slist == NULL)
|
||||
return NULL;
|
||||
sl_init(slist, uint32_key_cmp);
|
||||
return (void *)(uintptr_t)slist;
|
||||
}
|
||||
|
||||
static void
|
||||
test_api_tear_down(void *fixture)
|
||||
{
|
||||
ex_sl_t *slist = (ex_sl_t *)fixture;
|
||||
assert_ptr_not_null(slist);
|
||||
sl_node *cursor = sl_begin(slist);
|
||||
while (cursor) {
|
||||
assert_ptr_not_null(cursor);
|
||||
ex_node_t *entry = sl_get_entry(cursor, ex_node_t, snode);
|
||||
assert_ptr_not_null(entry);
|
||||
assert_uint32(entry->key, ==, entry->value);
|
||||
cursor = sl_next(slist, cursor);
|
||||
sl_erase_node(slist, &entry->snode);
|
||||
sl_release_node(&entry->snode);
|
||||
sl_wait_for_free(&entry->snode);
|
||||
sl_free_node(&entry->snode);
|
||||
free(entry);
|
||||
}
|
||||
sl_free(slist);
|
||||
free(fixture);
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_insert_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_insert_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_insert(const MunitParameter params[], void *data)
|
||||
{
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
(void)params;
|
||||
assert_ptr_not_null(data);
|
||||
int n = munit_rand_int_range(128, 4096);
|
||||
int key = munit_rand_int_range(0, (((uint32_t)0) - 1) / 10);
|
||||
while (n--) {
|
||||
ex_node_t *node = (ex_node_t *)calloc(sizeof(ex_node_t), 1);
|
||||
sl_init_node(&node->snode);
|
||||
node->key = key;
|
||||
node->value = key;
|
||||
sl_insert(slist, &node->snode);
|
||||
}
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_remove_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_remove_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_remove(const MunitParameter params[], void *data)
|
||||
{
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
(void)params;
|
||||
(void)slist;
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_find_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_find_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_find(const MunitParameter params[], void *data)
|
||||
{
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
(void)params;
|
||||
(void)slist;
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_update_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_update_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_update(const MunitParameter params[], void *data)
|
||||
{
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
(void)params;
|
||||
(void)slist;
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_delete_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_delete_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_delete(const MunitParameter params[], void *data)
|
||||
{
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
(void)params;
|
||||
(void)slist;
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_duplicates_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_duplicates_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_duplicates(const MunitParameter params[], void *data)
|
||||
{
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
(void)params;
|
||||
(void)slist;
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_size_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_size_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_size(const MunitParameter params[], void *data)
|
||||
{
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
(void)params;
|
||||
(void)slist;
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_iterators_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_iterators_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_iterators(const MunitParameter params[], void *data)
|
||||
{
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
(void)params;
|
||||
(void)slist;
|
||||
return MUNIT_OK;
|
||||
}
|
2255
tests/munit.c
2255
tests/munit.c
File diff suppressed because it is too large
Load diff
527
tests/munit.h
527
tests/munit.h
|
@ -1,527 +0,0 @@
|
|||
/* µ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
|
449
tests/test.c
449
tests/test.c
|
@ -1,449 +0,0 @@
|
|||
/*
|
||||
* skiplist 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 <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define __SL_DEBUG 0
|
||||
#if __SL_DEBUG > 0
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#if __SL_DEBUG >= 1
|
||||
#define __SLD_ASSERT(cond) assert(cond)
|
||||
#define __SLD_(b) b
|
||||
#elif __SL_DEBUG >= 2
|
||||
#define __SLD_P(...) printf(__VA_ARGS__)
|
||||
#elif __SL_DEBUG >= 3
|
||||
typedef struct dbg_node {
|
||||
sl_node snode;
|
||||
int value;
|
||||
} dbg_node_t;
|
||||
|
||||
inline void
|
||||
__sld_rt_ins(int error_code, sl_node *node, int top_layer, int cur_layer)
|
||||
{
|
||||
dbg_node_t *ddd = sl_get_entry(node, dbg_node_t, snode);
|
||||
printf("[INS] retry (code %d) "
|
||||
"%p (top %d, cur %d) %d\n",
|
||||
error_code, node, top_layer, cur_layer, ddd->value);
|
||||
}
|
||||
|
||||
inline void
|
||||
__sld_nc_ins(sl_node *node, sl_node *next_node, int top_layer, int cur_layer)
|
||||
{
|
||||
dbg_node_t *ddd = sl_get_entry(node, dbg_node_t, snode);
|
||||
dbg_node_t *ddd_next = sl_get_entry(next_node, dbg_node_t, snode);
|
||||
|
||||
printf("[INS] next node changed, "
|
||||
"%p %p (top %d, cur %d) %d %d\n",
|
||||
node, next_node, top_layer, cur_layer, ddd->value, ddd_next->value);
|
||||
}
|
||||
|
||||
inline void
|
||||
__sld_rt_rmv(int error_code, sl_node *node, int top_layer, int cur_layer)
|
||||
{
|
||||
dbg_node_t *ddd = sl_get_entry(node, dbg_node_t, snode);
|
||||
printf("[RMV] retry (code %d) "
|
||||
"%p (top %d, cur %d) %d\n",
|
||||
error_code, node, top_layer, cur_layer, ddd->value);
|
||||
}
|
||||
|
||||
inline void
|
||||
__sld_nc_rmv(sl_node *node, sl_node *next_node, int top_layer, int cur_layer)
|
||||
{
|
||||
dbg_node_t *ddd = sl_get_entry(node, dbg_node_t, snode);
|
||||
dbg_node_t *ddd_next = sl_get_entry(next_node, dbg_node_t, snode);
|
||||
|
||||
printf("[RMV] next node changed, "
|
||||
"%p %p (top %d, cur %d) %d %d\n",
|
||||
node, next_node, top_layer, cur_layer, ddd->value, ddd_next->value);
|
||||
}
|
||||
|
||||
inline void
|
||||
__sld_bm(sl_node *node)
|
||||
{
|
||||
dbg_node_t *ddd = sl_get_entry(node, dbg_node_t, snode);
|
||||
printf("[RMV] node is being modified %d\n", ddd->value);
|
||||
}
|
||||
|
||||
#define __SLD_RT_INS(e, n, t, c) __sld_rt_ins(e, n, t, c)
|
||||
#define __SLD_NC_INS(n, nn, t, c) __sld_nc_ins(n, nn, t, c)
|
||||
#define __SLD_RT_RMV(e, n, t, c) __sld_rt_rmv(e, n, t, c)
|
||||
#define __SLD_NC_RMV(n, nn, t, c) __sld_nc_rmv(n, nn, t, c)
|
||||
#define __SLD_BM(n) __sld_bm(n)
|
||||
#endif
|
||||
|
||||
#include "../include/skiplist.h"
|
||||
#include "munit.h"
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#pragma warning(disable : 4127)
|
||||
#endif
|
||||
|
||||
struct user_data {
|
||||
size_t n_ele;
|
||||
};
|
||||
|
||||
typedef struct ex_node {
|
||||
sl_node snode;
|
||||
uint32_t key;
|
||||
uint32_t value;
|
||||
} ex_node_t;
|
||||
|
||||
typedef sl_raw ex_sl_t;
|
||||
|
||||
static int
|
||||
uint32_key_cmp(sl_node *a, sl_node *b, void *aux)
|
||||
{
|
||||
ex_node_t *aa, *bb;
|
||||
(void)aux;
|
||||
aa = sl_get_entry(a, ex_node_t, snode);
|
||||
bb = sl_get_entry(b, ex_node_t, snode);
|
||||
|
||||
if (aa->key < bb->key)
|
||||
return -1;
|
||||
if (aa->key > bb->key)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static size_t
|
||||
__populate_slist(ex_sl_t *slist)
|
||||
{
|
||||
size_t inserted = 0;
|
||||
uint32_t n, key;
|
||||
ex_node_t *node;
|
||||
|
||||
n = munit_rand_int_range(1024, 4196);
|
||||
while (n--) {
|
||||
key = munit_rand_int_range(0, (((uint32_t)0) - 1) / 10);
|
||||
node = (ex_node_t *)calloc(sizeof(ex_node_t), 1);
|
||||
if (node == NULL)
|
||||
return MUNIT_ERROR;
|
||||
sl_init_node(&node->snode);
|
||||
node->key = key;
|
||||
node->value = key;
|
||||
if (sl_insert_nodup(slist, &node->snode) == -1)
|
||||
continue; /* a random duplicate appeared */
|
||||
else
|
||||
inserted++;
|
||||
}
|
||||
return inserted;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
struct test_info *info = (struct test_info *)user_data;
|
||||
(void)info;
|
||||
(void)params;
|
||||
|
||||
ex_sl_t *slist = calloc(sizeof(ex_sl_t), 1);
|
||||
if (slist == NULL)
|
||||
return NULL;
|
||||
sl_init(slist, uint32_key_cmp);
|
||||
return (void *)(uintptr_t)slist;
|
||||
}
|
||||
|
||||
static void
|
||||
test_api_tear_down(void *fixture)
|
||||
{
|
||||
ex_sl_t *slist = (ex_sl_t *)fixture;
|
||||
assert_ptr_not_null(slist);
|
||||
sl_node *cursor = sl_begin(slist);
|
||||
while (cursor) {
|
||||
assert_ptr_not_null(cursor);
|
||||
ex_node_t *entry = sl_get_entry(cursor, ex_node_t, snode);
|
||||
assert_ptr_not_null(entry);
|
||||
assert_uint32(entry->key, ==, entry->value);
|
||||
cursor = sl_next(slist, cursor);
|
||||
sl_erase_node(slist, &entry->snode);
|
||||
sl_release_node(&entry->snode);
|
||||
sl_wait_for_free(&entry->snode);
|
||||
sl_free_node(&entry->snode);
|
||||
free(entry);
|
||||
}
|
||||
sl_free(slist);
|
||||
free(fixture);
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_insert_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_insert_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_insert(const MunitParameter params[], void *data)
|
||||
{
|
||||
int ret;
|
||||
size_t inserted = 0;
|
||||
uint32_t n, key;
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
ex_node_t *node;
|
||||
(void)params;
|
||||
|
||||
assert_ptr_not_null(slist);
|
||||
n = munit_rand_int_range(4096, 8192);
|
||||
while (n--) {
|
||||
key = munit_rand_int_range(0, ((uint32_t)0 - 1) / 10);
|
||||
node = (ex_node_t *)calloc(sizeof(ex_node_t), 1);
|
||||
if (node == NULL)
|
||||
return MUNIT_ERROR;
|
||||
sl_init_node(&node->snode);
|
||||
node->key = key;
|
||||
node->value = key;
|
||||
if ((ret = sl_insert_nodup(slist, &node->snode)) == -1)
|
||||
continue; /* a random duplicate appeared */
|
||||
else {
|
||||
assert_int(ret, ==, 0);
|
||||
inserted++;
|
||||
}
|
||||
}
|
||||
assert_size(inserted, ==, sl_get_size(slist));
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_remove_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
sl_raw *slist = (sl_raw *)test_api_setup(params, user_data);
|
||||
__populate_slist(slist);
|
||||
return (void *)slist;
|
||||
}
|
||||
static void
|
||||
test_api_remove_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_remove(const MunitParameter params[], void *data)
|
||||
{
|
||||
uint32_t key;
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
ex_node_t *node;
|
||||
(void)params;
|
||||
|
||||
assert_ptr_not_null(slist);
|
||||
key = munit_rand_int_range((((uint32_t)0 - 1) / 10) + 1, ((uint32_t)0 - 1));
|
||||
node = (ex_node_t *)calloc(sizeof(ex_node_t), 1);
|
||||
if (node == NULL)
|
||||
return MUNIT_ERROR;
|
||||
sl_init_node(&node->snode);
|
||||
node->key = key;
|
||||
node->value = key;
|
||||
if (sl_insert_nodup(slist, &node->snode) == -1)
|
||||
return MUNIT_ERROR;
|
||||
else {
|
||||
ex_node_t query;
|
||||
query.key = key;
|
||||
sl_node *cursor = sl_find(slist, &query.snode);
|
||||
assert_ptr_not_null(cursor);
|
||||
ex_node_t *entry = sl_get_entry(cursor, ex_node_t, snode);
|
||||
sl_erase_node(slist, &entry->snode);
|
||||
sl_release_node(&entry->snode);
|
||||
sl_wait_for_free(&entry->snode);
|
||||
sl_free_node(&entry->snode);
|
||||
free(entry);
|
||||
}
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_find_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
sl_raw *slist = (sl_raw *)test_api_setup(params, user_data);
|
||||
ex_node_t *node;
|
||||
for (int i = 1; i <= 100; ++i) {
|
||||
node = calloc(sizeof(ex_node_t), 1);
|
||||
if (node == NULL)
|
||||
return NULL;
|
||||
node = (ex_node_t *)calloc(sizeof(ex_node_t), 1);
|
||||
sl_init_node(&node->snode);
|
||||
node->key = i;
|
||||
node->value = i;
|
||||
sl_insert(slist, &node->snode);
|
||||
}
|
||||
return (void *)slist;
|
||||
}
|
||||
static void
|
||||
test_api_find_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_find(const MunitParameter params[], void *data)
|
||||
{
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
(void)params;
|
||||
|
||||
/* find equal every value */
|
||||
assert_ptr_not_null(data);
|
||||
for (int i = 1; i <= 100; i++) {
|
||||
ex_node_t query;
|
||||
query.key = i;
|
||||
sl_node *cursor = sl_find(slist, &query.snode);
|
||||
assert_ptr_not_null(cursor);
|
||||
ex_node_t *entry = sl_get_entry(cursor, ex_node_t, snode);
|
||||
assert_uint32(entry->key, ==, i);
|
||||
}
|
||||
|
||||
/* */
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_update_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_update_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_update(const MunitParameter params[], void *data)
|
||||
{
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
(void)params;
|
||||
(void)slist;
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_delete_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_delete_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_delete(const MunitParameter params[], void *data)
|
||||
{
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
(void)params;
|
||||
(void)slist;
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_duplicates_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_duplicates_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_duplicates(const MunitParameter params[], void *data)
|
||||
{
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
(void)params;
|
||||
(void)slist;
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_size_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_size_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_size(const MunitParameter params[], void *data)
|
||||
{
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
(void)params;
|
||||
(void)slist;
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_iterators_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_iterators_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_iterators(const MunitParameter params[], void *data)
|
||||
{
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
(void)params;
|
||||
(void)slist;
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static MunitTest api_test_suite[] = {
|
||||
{ (char *)"/api/insert", test_api_insert, test_api_insert_setup,
|
||||
test_api_insert_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/api/remove", test_api_remove, test_api_remove_setup,
|
||||
test_api_remove_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/api/find", test_api_find, test_api_find_setup,
|
||||
test_api_find_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/api/update", test_api_update, test_api_update_setup,
|
||||
test_api_update_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/api/delete", test_api_delete, test_api_delete_setup,
|
||||
test_api_delete_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/api/duplicates", test_api_duplicates, test_api_duplicates_setup,
|
||||
test_api_duplicates_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/api/size", test_api_size, test_api_size_setup,
|
||||
test_api_size_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/api/iterators", test_api_iterators, test_api_iterators_setup,
|
||||
test_api_iterators_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
|
||||
};
|
||||
|
||||
static MunitTest mt_tests[] = { { 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[] = { { "/mt", mt_tests, NULL, 1,
|
||||
MUNIT_SUITE_OPTION_NONE },
|
||||
{ "/scale", scale_tests, NULL, 1, MUNIT_SUITE_OPTION_NONE },
|
||||
{ NULL, NULL, NULL, 0, MUNIT_SUITE_OPTION_NONE } };
|
||||
|
||||
static const MunitSuite main_test_suite = { (char *)"/api", api_test_suite,
|
||||
other_test_suite, 1, MUNIT_SUITE_OPTION_NONE };
|
||||
|
||||
int
|
||||
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