From 7946c8efc2e2a44a8e78e1263c1691ce9f412a09 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Thu, 18 Jul 2024 11:09:50 +0200 Subject: Converted set to map --- test/112_h48map/h48map_tests.c | 88 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 test/112_h48map/h48map_tests.c (limited to 'test/112_h48map/h48map_tests.c') 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 @@ +#include "../test.h" + +#define MAP_KEYSHIFT UINT64_C(40) + +typedef struct { + uint64_t n; + uint64_t capacity; + uint64_t mod; + uint64_t *table; +} h48map_t; + +typedef struct { + uint64_t key; + uint64_t val; +} kvpair_t; + +void h48map_create(h48map_t *, uint64_t, uint64_t); +void h48map_clear(h48map_t *); +void h48map_destroy(h48map_t *); +uint64_t h48map_lookup(h48map_t *, uint64_t); +void h48map_insertmin(h48map_t *, uint64_t, uint64_t); +uint64_t h48map_value(h48map_t *, uint64_t); +kvpair_t h48map_nextkvpair(h48map_t *, uint64_t *); + +char str[STRLENMAX]; + +int compare(const void *x, const void *y) { + uint64_t a = ((kvpair_t *)x)->key; + uint64_t b = ((kvpair_t *)y)->key; + + if (a > b) return 1; + if (a == b) return 0; + return -1; +} + +uint64_t readl(void) { + fgets(str, STRLENMAX, stdin); + return atoll(str); +} + +void run(void) { + h48map_t map; + uint64_t n, i, j, capacity, mod, x, y, v; + kvpair_t kv, *a, *b; + + capacity = readl(); + mod = readl(); + n = readl(); + + a = malloc(n * sizeof(kvpair_t)); + b = malloc(n * sizeof(kvpair_t)); + for (i = 0; i < n; i++) { + x = readl(); + y = readl(); + a[i] = (kvpair_t) { .key = x, .val = y }; + } + + h48map_create(&map, capacity, mod); + for (i = 0; i < n; i++) + h48map_insertmin(&map, a[i].key, a[i].val); + + i = 0; + for (kv = h48map_nextkvpair(&map, &i), j = 0; + i != map.capacity; + kv = h48map_nextkvpair(&map, &i) + ) { + b[j++] = kv; + } + qsort(b, j, sizeof(kvpair_t), compare); + + printf("%" PRIu64 "\n", map.n); + for (i = 0; i < j; i++) + printf("%" PRIu64 " %" PRIu64 "\n", b[i].key, b[i].val); + if (map.n != j) + printf("Wrong number of elements: map->n = %" PRIu64 ", " + "but scan returns %" PRIu64 "\n", map.n, j); + for (i = 0; i < n; i++) { + v = h48map_value(&map, a[i].key); + if (v > a[i].val) + printf("Value for key %" PRId64 " is larger than " + "expected (%" PRIu64 " > %" PRIu64 ")\n", + a[i].key, v, a[i].val); + } + + h48map_destroy(&map); + free(a); + free(b); +} -- cgit v1.3