Commit | Line | Data |
---|---|---|
1ff641f9 BA |
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 | { | |
eed1b5d2 | 67 | double* d; |
1ff641f9 BA |
68 | key[3] = (char)(48 + i); |
69 | hashtable_get(h, key, d); | |
eed1b5d2 | 70 | lu_assert_dbl_eq(*d, ckValue); |
1ff641f9 BA |
71 | ckValue += 1.0; |
72 | } | |
73 | ||
588a2232 | 74 | // Remove keys / values |
1ff641f9 BA |
75 | for (int i = 0; i < n; i++) |
76 | { | |
1ff641f9 BA |
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); | |
eed1b5d2 BA |
90 | StructTest1* st1 = (StructTest1*) malloc(n * sizeof(StructTest1)); |
91 | char key[] = "key_"; | |
1ff641f9 BA |
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 | { | |
588a2232 | 101 | // Another way to access elements |
1ff641f9 | 102 | key[3] = (char)(48 + i); |
eed1b5d2 | 103 | StructTest1* st1Cell; |
1ff641f9 | 104 | hashtable_get(h, key, st1Cell); |
eed1b5d2 BA |
105 | lu_assert_int_eq(st1Cell->a, st1[i].a); |
106 | lu_assert_dbl_eq(st1Cell->b, st1[i].b); | |
1ff641f9 | 107 | } |
eed1b5d2 | 108 | safe_free(st1); |
1ff641f9 BA |
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: | |
eed1b5d2 BA |
117 | StructTest1 stMod; |
118 | stMod.a = -17; | |
119 | stMod.b = 3.0; | |
1ff641f9 BA |
120 | hashtable_set(h, "key1", stMod); |
121 | hashtable_get(h, "key1", stmp); | |
eed1b5d2 BA |
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; | |
1ff641f9 BA |
126 | hashtable_set(h, "key5", stMod); |
127 | hashtable_get(h, "key5", stmp); | |
eed1b5d2 BA |
128 | lu_assert_int_eq(stmp->a, stMod.a); |
129 | lu_assert_dbl_eq(stmp->b, stMod.b); | |
1ff641f9 | 130 | |
1ff641f9 BA |
131 | hashtable_destroy(h); |
132 | } | |
133 | ||
134 | void t_hashtable_copy() | |
135 | { | |
136 | int n = 10; | |
137 | ||
138 | HashTable* h = hashtable_new(int, 8); | |
eed1b5d2 | 139 | char key[] = "key_"; |
1ff641f9 BA |
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); | |
eed1b5d2 | 148 | int *a, *b; |
1ff641f9 BA |
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); | |
eed1b5d2 | 154 | lu_assert_int_eq(*a, *b); |
1ff641f9 BA |
155 | } |
156 | hashtable_destroy(h); | |
157 | hashtable_destroy(hc); | |
158 | } |