aboutsummaryrefslogtreecommitdiff
path: root/test/112_h48map/h48map_tests.c
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2024-07-18 11:09:50 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2024-07-18 11:09:50 +0200
commit7946c8efc2e2a44a8e78e1263c1691ce9f412a09 (patch)
treef04505d6444a59b26c8c2e55311b70bbbf1ce364 /test/112_h48map/h48map_tests.c
parent3d060c348fdfff074a9b902d56f539664789d831 (diff)
downloadnissy-core-7946c8efc2e2a44a8e78e1263c1691ce9f412a09.tar.gz
nissy-core-7946c8efc2e2a44a8e78e1263c1691ce9f412a09.zip
Converted set to map
Diffstat (limited to '')
-rw-r--r--test/112_h48map/h48map_tests.c88
1 files changed, 88 insertions, 0 deletions
diff --git a/test/112_h48map/h48map_tests.c b/test/112_h48map/h48map_tests.c
new file mode 100644
index 0000000..ce8f657
--- /dev/null
+++ b/test/112_h48map/h48map_tests.c
@@ -0,0 +1,88 @@
1#include "../test.h"
2
3#define MAP_KEYSHIFT UINT64_C(40)
4
5typedef struct {
6 uint64_t n;
7 uint64_t capacity;
8 uint64_t mod;
9 uint64_t *table;
10} h48map_t;
11
12typedef struct {
13 uint64_t key;
14 uint64_t val;
15} kvpair_t;
16
17void h48map_create(h48map_t *, uint64_t, uint64_t);
18void h48map_clear(h48map_t *);
19void h48map_destroy(h48map_t *);
20uint64_t h48map_lookup(h48map_t *, uint64_t);
21void h48map_insertmin(h48map_t *, uint64_t, uint64_t);
22uint64_t h48map_value(h48map_t *, uint64_t);
23kvpair_t h48map_nextkvpair(h48map_t *, uint64_t *);
24
25char str[STRLENMAX];
26
27int compare(const void *x, const void *y) {
28 uint64_t a = ((kvpair_t *)x)->key;
29 uint64_t b = ((kvpair_t *)y)->key;
30
31 if (a > b) return 1;
32 if (a == b) return 0;
33 return -1;
34}
35
36uint64_t readl(void) {
37 fgets(str, STRLENMAX, stdin);
38 return atoll(str);
39}
40
41void run(void) {
42 h48map_t map;
43 uint64_t n, i, j, capacity, mod, x, y, v;
44 kvpair_t kv, *a, *b;
45
46 capacity = readl();
47 mod = readl();
48 n = readl();
49
50 a = malloc(n * sizeof(kvpair_t));
51 b = malloc(n * sizeof(kvpair_t));
52 for (i = 0; i < n; i++) {
53 x = readl();
54 y = readl();
55 a[i] = (kvpair_t) { .key = x, .val = y };
56 }
57
58 h48map_create(&map, capacity, mod);
59 for (i = 0; i < n; i++)
60 h48map_insertmin(&map, a[i].key, a[i].val);
61
62 i = 0;
63 for (kv = h48map_nextkvpair(&map, &i), j = 0;
64 i != map.capacity;
65 kv = h48map_nextkvpair(&map, &i)
66 ) {
67 b[j++] = kv;
68 }
69 qsort(b, j, sizeof(kvpair_t), compare);
70
71 printf("%" PRIu64 "\n", map.n);
72 for (i = 0; i < j; i++)
73 printf("%" PRIu64 " %" PRIu64 "\n", b[i].key, b[i].val);
74 if (map.n != j)
75 printf("Wrong number of elements: map->n = %" PRIu64 ", "
76 "but scan returns %" PRIu64 "\n", map.n, j);
77 for (i = 0; i < n; i++) {
78 v = h48map_value(&map, a[i].key);
79 if (v > a[i].val)
80 printf("Value for key %" PRId64 " is larger than "
81 "expected (%" PRIu64 " > %" PRIu64 ")\n",
82 a[i].key, v, a[i].val);
83 }
84
85 h48map_destroy(&map);
86 free(a);
87 free(b);
88}

Generated with cgit - Back to sebastiano.tronto.net