test_ht.c (1444B)
1 #include <stdlib.h> 2 #include <stdio.h> 3 #include <assert.h> 4 #include "ht.h" 5 6 static void test_ht_init() { 7 ht_t h; 8 9 ht_init(&h); 10 assert(h.nelts == 0); 11 assert(h.nalloc == HT_DEFAULT_SIZE); 12 assert(h.entries != NULL); 13 14 ht_clear(&h); 15 16 ht_init_size(&h, 23); 17 assert(h.nelts == 0); 18 assert(h.nalloc == 23); 19 assert(h.entries != NULL); 20 21 ht_clear(&h); 22 } 23 24 static void test_ht_insert() { 25 ht_t h; 26 ht_init_size(&h, 16); 27 28 sd_t elts[17]; 29 30 size_t i; 31 for (i = 0; i < 17; i++) { 32 sd_init_u64(&elts[i], i); 33 ht_insert(&h, elts[i]); 34 assert(h.nelts == i + 1); 35 } 36 37 sd_t tmp; 38 sd_init_u64(&tmp, 12); 39 ht_insert(&h, tmp); 40 assert(h.nelts == 17); 41 42 for (i = 0; i < 17; i++) 43 sd_clear(&elts[i]); 44 45 ht_clear(&h); 46 } 47 48 static void test_ht_lookup() { 49 ht_t h; 50 ht_init_size(&h, 32); 51 52 sd_t elts[17]; 53 54 size_t i; 55 for (i = 0; i < 17; i++) { 56 sd_init_u64(&elts[i], 2 * i); 57 ht_insert(&h, elts[i]); 58 } 59 60 sd_t nelts[2 * 17]; 61 for (i = 0; i < 2 * 17; i++) { 62 sd_init_u64(&nelts[i], i); 63 if (i % 2 == 0) { 64 int index = ht_lookup(&h, nelts[i]); 65 assert(index <= 17); 66 assert(index >= 0); 67 } else { 68 assert(ht_lookup(&h, nelts[i]) == HT_LOOKUP_NOT_FOUND); 69 } 70 } 71 72 for (i = 0; i < 17; i++) 73 sd_clear(&elts[i]); 74 for (i = 0; i < 2 * 17; i++) 75 sd_clear(&nelts[i]); 76 77 ht_clear(&h); 78 } 79 80 int main() { 81 test_ht_init(); 82 test_ht_insert(); 83 test_ht_lookup(); 84 85 printf("test ok\n"); 86 }