aboutsummaryrefslogtreecommitdiff
path: root/test/121_h48map/h48map_tests.c
diff options
context:
space:
mode:
Diffstat (limited to 'test/121_h48map/h48map_tests.c')
-rw-r--r--test/121_h48map/h48map_tests.c70
1 files changed, 70 insertions, 0 deletions
diff --git a/test/121_h48map/h48map_tests.c b/test/121_h48map/h48map_tests.c
new file mode 100644
index 0000000..927b157
--- /dev/null
+++ b/test/121_h48map/h48map_tests.c
@@ -0,0 +1,70 @@
1#include "../test.h"
2
3#define MAXPOS 1000
4
5void h48map_create(h48map_t [static 1], uint64_t, uint64_t);
6void h48map_destroy(h48map_t [static 1]);
7void h48map_insertmin(h48map_t [static 1], uint64_t, uint64_t);
8uint64_t h48map_value(h48map_t [static 1], uint64_t);
9kvpair_t h48map_nextkvpair(h48map_t [static 1], uint64_t [static 1]);
10
11char str[STRLENMAX];
12
13int compare(const void *x, const void *y) {
14 uint64_t a = ((kvpair_t *)x)->key;
15 uint64_t b = ((kvpair_t *)y)->key;
16
17 if (a > b) return 1;
18 if (a == b) return 0;
19 return -1;
20}
21
22uint64_t readl(void) {
23 fgets(str, STRLENMAX, stdin);
24 return atoll(str);
25}
26
27void run(void) {
28 h48map_t map;
29 uint64_t n, i, j, capacity, randomizer, x, y, v;
30 kvpair_t kv, a[MAXPOS], b[MAXPOS];
31
32 capacity = readl();
33 randomizer = readl();
34 n = readl();
35
36 for (i = 0; i < n; i++) {
37 x = readl();
38 y = readl();
39 a[i] = (kvpair_t) { .key = x, .val = y };
40 }
41
42 h48map_create(&map, capacity, randomizer);
43 for (i = 0; i < n; i++)
44 h48map_insertmin(&map, a[i].key, a[i].val);
45
46 i = 0;
47 for (kv = h48map_nextkvpair(&map, &i), j = 0;
48 i != map.capacity && j < MAXPOS;
49 kv = h48map_nextkvpair(&map, &i)
50 ) {
51 b[j++] = kv;
52 }
53 qsort(b, j, sizeof(kvpair_t), compare);
54
55 printf("%" PRIu64 "\n", map.n);
56 for (i = 0; i < j; i++)
57 printf("%" PRIu64 " %" PRIu64 "\n", b[i].key, b[i].val);
58 if (map.n != j)
59 printf("Wrong number of elements: map->n = %" PRIu64 ", "
60 "but scan returns %" PRIu64 "\n", map.n, j);
61 for (i = 0; i < n; i++) {
62 v = h48map_value(&map, a[i].key);
63 if (v > a[i].val)
64 printf("Value for key %" PRId64 " is larger than "
65 "expected (%" PRIu64 " > %" PRIu64 ")\n",
66 a[i].key, v, a[i].val);
67 }
68
69 h48map_destroy(&map);
70}

Generated with cgit - Back to sebastiano.tronto.net