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 | { | |
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 | double d; | |
78 | key[3] = (char)(48 + i); | |
79 | hashtable_delete(h, key); | |
80 | } | |
81 | lu_assert_int_eq(hashtable_size(h), 0); | |
82 | ||
83 | hashtable_destroy(h); | |
84 | } | |
85 | ||
86 | void t_hashtable_getnull_modify() | |
87 | { | |
88 | int n = 10; | |
89 | ||
90 | HashTable* h = hashtable_new(StructTest1, 4); | |
91 | StructTest1* st1 = (StructTest1*) malloc(n * sizeof (StructTest1)); | |
92 | char* key = "key_"; | |
93 | for (int i = 0; i < n; i++) | |
94 | { | |
95 | key[3] = (char)(48 + i); | |
96 | st1[i].a = random() % 42; | |
97 | st1[i].b = (double) random() / RAND_MAX; | |
98 | hashtable_set(h, key, *(st1 + i)); | |
99 | } | |
100 | for (int i = 0; i < n; i++) | |
101 | { | |
102 | //another way to access elements | |
103 | key[3] = (char)(48 + i); | |
104 | StructTest1 st1Cell; | |
105 | hashtable_get(h, key, st1Cell); | |
106 | lu_assert_int_eq(st1Cell.a, st1[i].a); | |
107 | lu_assert_dbl_eq(st1Cell.b, st1[i].b); | |
108 | } | |
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 | safe_free(st1); | |
132 | hashtable_destroy(h); | |
133 | } | |
134 | ||
135 | void t_hashtable_copy() | |
136 | { | |
137 | int n = 10; | |
138 | ||
139 | HashTable* h = hashtable_new(int, 8); | |
140 | char* key = "key_"; | |
141 | for (int i = 0; i < n; i++) | |
142 | { | |
143 | key[3] = (char)(48 + i); | |
144 | hashtable_set(h, key, random() % 42); | |
145 | } | |
146 | HashTable* hc = hashtable_copy(h); | |
147 | ||
148 | lu_assert_int_eq(h->size, hc->size); | |
149 | int a, b; | |
150 | for (int i = 0; i < n; i++) | |
151 | { | |
152 | key[3] = (char)(48 + i); | |
153 | hashtable_get(h, key, a); | |
154 | hashtable_get(hc, key, b); | |
155 | lu_assert_int_eq(a, b); | |
156 | } | |
157 | hashtable_destroy(h); | |
158 | hashtable_destroy(hc); | |
159 | } |