e695f5706dc500d99560dddd5695dc50d50e6568
[cgds.git] / test / t.HashTable.c
1 #include <stdlib.h>
2 #include "cgds/HashTable.h"
3 #include "helpers.h"
4 #include "lut.h"
5
6 void t_hashtable_clear()
7 {
8 HashTable* h = hashtable_new(int, 16);
9 lu_assert(hashtable_empty(h));
10
11 hashtable_set(h, "key1", 0);
12 hashtable_set(h, "key2", 1);
13 hashtable_set(h, "key3", 2);
14
15 hashtable_destroy(h);
16 h = hashtable_new(int, 8);
17
18 hashtable_set(h, "key1", 1);
19 hashtable_set(h, "key2", 2);
20 hashtable_set(h, "key3", 3);
21
22 hashtable_clear(h);
23 lu_assert(hashtable_empty(h));
24
25 hashtable_destroy(h);
26 }
27
28 void t_hashtable_size()
29 {
30 HashTable* h = hashtable_new(int, 16);
31 lu_assert(hashtable_empty(h));
32
33 hashtable_set(h, "key1", 0);
34 hashtable_set(h, "key2", 1);
35 hashtable_set(h, "key3", 2);
36 lu_assert_int_eq(hashtable_size(h), 3);
37
38 hashtable_set(h, "key4", 3);
39 hashtable_set(h, "key5", 4);
40 lu_assert_int_eq(hashtable_size(h), 5);
41
42 hashtable_set(h, "key6", 5);
43 hashtable_set(h, "key7", 6);
44 hashtable_set(h, "key8", 7);
45 lu_assert_int_eq(hashtable_size(h), 8);
46
47 hashtable_destroy(h);
48 }
49
50 void t_hashtable_set_remove_basic()
51 {
52 int n = 10;
53
54 HashTable* h = hashtable_new(double, 4);
55 char key[] = "key_";
56 for (int i = 0; i < n; i++)
57 {
58 key[3] = (char)(48 + i);
59 hashtable_set(h, key, (double)i);
60 }
61 lu_assert_int_eq(hashtable_size(h), n);
62
63 // Check values
64 double ckValue = 0.0;
65 for (int i = 0; i < n; i++)
66 {
67 double* d;
68 key[3] = (char)(48 + i);
69 hashtable_get(h, key, d);
70 lu_assert_dbl_eq(*d, ckValue);
71 ckValue += 1.0;
72 }
73
74 // Remove keys / values
75 for (int i = 0; i < n; i++)
76 {
77 key[3] = (char)(48 + i);
78 hashtable_delete(h, key);
79 }
80 lu_assert_int_eq(hashtable_size(h), 0);
81
82 hashtable_destroy(h);
83 }
84
85 void t_hashtable_getnull_modify()
86 {
87 int n = 10;
88
89 HashTable* h = hashtable_new(StructTest1, 4);
90 StructTest1* st1 = (StructTest1*) malloc(n * sizeof(StructTest1));
91 char key[] = "key_";
92 for (int i = 0; i < n; i++)
93 {
94 key[3] = (char)(48 + i);
95 st1[i].a = random() % 42;
96 st1[i].b = (double) random() / RAND_MAX;
97 hashtable_set(h, key, *(st1 + i));
98 }
99 for (int i = 0; i < n; i++)
100 {
101 // Another way to access elements
102 key[3] = (char)(48 + i);
103 StructTest1* st1Cell;
104 hashtable_get(h, key, st1Cell);
105 lu_assert_int_eq(st1Cell->a, st1[i].a);
106 lu_assert_dbl_eq(st1Cell->b, st1[i].b);
107 }
108 safe_free(st1);
109
110 // Get null:
111 StructTest1* stmp;
112 hashtable_get(h, "key12", stmp);
113 lu_assert(stmp == NULL);
114 hashtable_get(h, "key32", stmp);
115 lu_assert(stmp == NULL);
116 // Modify:
117 StructTest1 stMod;
118 stMod.a = -17;
119 stMod.b = 3.0;
120 hashtable_set(h, "key1", stMod);
121 hashtable_get(h, "key1", stmp);
122 lu_assert_int_eq(stmp->a, stMod.a);
123 lu_assert_dbl_eq(stmp->b, stMod.b);
124 stMod.a = -7;
125 stMod.b = 3.5;
126 hashtable_set(h, "key5", stMod);
127 hashtable_get(h, "key5", stmp);
128 lu_assert_int_eq(stmp->a, stMod.a);
129 lu_assert_dbl_eq(stmp->b, stMod.b);
130
131 hashtable_destroy(h);
132 }
133
134 void t_hashtable_copy()
135 {
136 int n = 10;
137
138 HashTable* h = hashtable_new(int, 8);
139 char key[] = "key_";
140 for (int i = 0; i < n; i++)
141 {
142 key[3] = (char)(48 + i);
143 hashtable_set(h, key, random() % 42);
144 }
145 HashTable* hc = hashtable_copy(h);
146
147 lu_assert_int_eq(h->size, hc->size);
148 int *a, *b;
149 for (int i = 0; i < n; i++)
150 {
151 key[3] = (char)(48 + i);
152 hashtable_get(h, key, a);
153 hashtable_get(hc, key, b);
154 lu_assert_int_eq(*a, *b);
155 }
156 hashtable_destroy(h);
157 hashtable_destroy(hc);
158 }