diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2025-07-17 08:54:44 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2025-07-17 08:54:44 +0200 |
| commit | 270e5b0444f67781856bf80db51650af9cb89b0c (patch) | |
| tree | 1f895ca8fdbf256dcdf550dd0a28ca99993efc08 /test/121_h48map/h48map_tests.c | |
| parent | 2d0ab15ac1f81c76cb6bb31bbdad3d2ee339e062 (diff) | |
| download | nissy-core-270e5b0444f67781856bf80db51650af9cb89b0c.tar.gz nissy-core-270e5b0444f67781856bf80db51650af9cb89b0c.zip | |
Renamed tests to make space for more coordinate tests
Diffstat (limited to 'test/121_h48map/h48map_tests.c')
| -rw-r--r-- | test/121_h48map/h48map_tests.c | 70 |
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 | |||
| 5 | void h48map_create(h48map_t [static 1], uint64_t, uint64_t); | ||
| 6 | void h48map_destroy(h48map_t [static 1]); | ||
| 7 | void h48map_insertmin(h48map_t [static 1], uint64_t, uint64_t); | ||
| 8 | uint64_t h48map_value(h48map_t [static 1], uint64_t); | ||
| 9 | kvpair_t h48map_nextkvpair(h48map_t [static 1], uint64_t [static 1]); | ||
| 10 | |||
| 11 | char str[STRLENMAX]; | ||
| 12 | |||
| 13 | int 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 | |||
| 22 | uint64_t readl(void) { | ||
| 23 | fgets(str, STRLENMAX, stdin); | ||
| 24 | return atoll(str); | ||
| 25 | } | ||
| 26 | |||
| 27 | void 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 | } | ||
