h48map_tests.c (1645B)
1 #include "../test.h" 2 3 #define MAXPOS 1000 4 5 void h48map_create(h48map_t *, uint64_t, uint64_t); 6 void h48map_destroy(h48map_t *); 7 void h48map_insertmin(h48map_t *, uint64_t, uint64_t); 8 uint64_t h48map_value(h48map_t *, uint64_t); 9 kvpair_t h48map_nextkvpair(h48map_t *, uint64_t *); 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 }