3 #include <winpr/tchar.h>
4 #include <winpr/collections.h>
6 static char* key1 =
"key1";
7 static char* key2 =
"key2";
8 static char* key3 =
"key3";
10 static char* val1 =
"val1";
11 static char* val2 =
"val2";
12 static char* val3 =
"val3";
14 static int test_hash_table_pointer(
void)
19 wHashTable* table = NULL;
20 table = HashTable_New(TRUE);
25 if (!HashTable_Insert(table, key1, val1))
27 if (!HashTable_Insert(table, key2, val2))
29 if (!HashTable_Insert(table, key3, val3))
31 count = HashTable_Count(table);
35 printf(
"HashTable_Count: Expected : 3, Actual: %" PRIuz
"\n", count);
39 if (!HashTable_Remove(table, key2))
41 count = HashTable_Count(table);
45 printf(
"HashTable_Count: Expected : 2, Actual: %" PRIuz
"\n", count);
49 if (!HashTable_Remove(table, key3))
51 count = HashTable_Count(table);
55 printf(
"HashTable_Count: Expected : 1, Actual: %" PRIuz
"\n", count);
59 if (!HashTable_Remove(table, key1))
61 count = HashTable_Count(table);
65 printf(
"HashTable_Count: Expected : 0, Actual: %" PRIuz
"\n", count);
69 if (!HashTable_Insert(table, key1, val1))
71 if (!HashTable_Insert(table, key2, val2))
73 if (!HashTable_Insert(table, key3, val3))
75 count = HashTable_Count(table);
79 printf(
"HashTable_Count: Expected : 3, Actual: %" PRIuz
"\n", count);
83 value = (
char*)HashTable_GetItemValue(table, key1);
85 if (strcmp(value, val1) != 0)
87 printf(
"HashTable_GetItemValue: Expected : %s, Actual: %s\n", val1, value);
91 value = (
char*)HashTable_GetItemValue(table, key2);
93 if (strcmp(value, val2) != 0)
95 printf(
"HashTable_GetItemValue: Expected : %s, Actual: %s\n", val2, value);
99 value = (
char*)HashTable_GetItemValue(table, key3);
101 if (strcmp(value, val3) != 0)
103 printf(
"HashTable_GetItemValue: Expected : %s, Actual: %s\n", val3, value);
107 if (!HashTable_SetItemValue(table, key2,
"apple"))
109 value = (
char*)HashTable_GetItemValue(table, key2);
111 if (strcmp(value,
"apple") != 0)
113 printf(
"HashTable_GetItemValue: Expected : %s, Actual: %s\n",
"apple", value);
117 if (!HashTable_Contains(table, key2))
119 printf(
"HashTable_Contains: Expected : TRUE, Actual: FALSE\n");
123 if (!HashTable_Remove(table, key2))
125 printf(
"HashTable_Remove: Expected : TRUE, Actual: FALSE\n");
129 if (HashTable_Remove(table, key2))
131 printf(
"HashTable_Remove: Expected : FALSE, Actual: TRUE\n");
135 HashTable_Clear(table);
136 count = HashTable_Count(table);
140 printf(
"HashTable_Count: Expected : 0, Actual: %" PRIuz
"\n", count);
146 HashTable_Free(table);
150 static int test_hash_table_string(
void)
155 wHashTable* table = HashTable_New(TRUE);
160 if (!HashTable_SetupForStringData(table, TRUE))
163 if (!HashTable_Insert(table, key1, val1))
165 if (!HashTable_Insert(table, key2, val2))
167 if (!HashTable_Insert(table, key3, val3))
169 count = HashTable_Count(table);
173 printf(
"HashTable_Count: Expected : 3, Actual: %" PRIuz
"\n", count);
177 if (!HashTable_Remove(table, key2))
179 count = HashTable_Count(table);
183 printf(
"HashTable_Count: Expected : 3, Actual: %" PRIuz
"\n", count);
187 if (!HashTable_Remove(table, key3))
189 count = HashTable_Count(table);
193 printf(
"HashTable_Count: Expected : 1, Actual: %" PRIuz
"\n", count);
197 if (!HashTable_Remove(table, key1))
199 count = HashTable_Count(table);
203 printf(
"HashTable_Count: Expected : 0, Actual: %" PRIuz
"\n", count);
207 if (!HashTable_Insert(table, key1, val1))
209 if (!HashTable_Insert(table, key2, val2))
211 if (!HashTable_Insert(table, key3, val3))
213 count = HashTable_Count(table);
217 printf(
"HashTable_Count: Expected : 3, Actual: %" PRIuz
"\n", count);
221 value = (
char*)HashTable_GetItemValue(table, key1);
223 if (strcmp(value, val1) != 0)
225 printf(
"HashTable_GetItemValue: Expected : %s, Actual: %s\n", val1, value);
229 value = (
char*)HashTable_GetItemValue(table, key2);
231 if (strcmp(value, val2) != 0)
233 printf(
"HashTable_GetItemValue: Expected : %s, Actual: %s\n", val2, value);
237 value = (
char*)HashTable_GetItemValue(table, key3);
239 if (strcmp(value, val3) != 0)
241 printf(
"HashTable_GetItemValue: Expected : %s, Actual: %s\n", val3, value);
245 if (!HashTable_SetItemValue(table, key2,
"apple"))
247 value = (
char*)HashTable_GetItemValue(table, key2);
249 if (strcmp(value,
"apple") != 0)
251 printf(
"HashTable_GetItemValue: Expected : %s, Actual: %s\n",
"apple", value);
255 if (!HashTable_Contains(table, key2))
257 printf(
"HashTable_Contains: Expected : TRUE, Actual: FALSE\n");
261 if (!HashTable_Remove(table, key2))
263 printf(
"HashTable_Remove: Expected : TRUE, Actual: FALSE\n");
267 if (HashTable_Remove(table, key2))
269 printf(
"HashTable_Remove: Expected : FALSE, Actual: TRUE\n");
273 HashTable_Clear(table);
274 count = HashTable_Count(table);
278 printf(
"HashTable_Count: Expected : 0, Actual: %" PRIuz
"\n", count);
284 HashTable_Free(table);
291 size_t strlenCounter;
297 static BOOL foreachFn1(
const void* key,
void* value,
void* arg)
299 ForeachData* d = (ForeachData*)arg;
301 d->strlenCounter += strlen((
const char*)value);
305 static BOOL foreachFn2(
const void* key,
void* value,
void* arg)
307 ForeachData* d = (ForeachData*)arg;
312 if (d->foreachCalls == 2)
317 static BOOL foreachFn3(
const void* key,
void* value,
void* arg)
319 const char* keyStr = (
const char*)key;
321 ForeachData* d = (ForeachData*)arg;
325 WINPR_ASSERT(keyStr);
327 if (strcmp(keyStr,
"key1") == 0)
331 HashTable_Remove(d->table,
"key2");
333 if (HashTable_Contains(d->table,
"key2"))
335 d->test3error = TRUE;
339 if (HashTable_ContainsValue(d->table,
"value2"))
341 d->test3error = TRUE;
346 if (HashTable_Count(d->table) != 2)
348 d->test3error = TRUE;
354 d2.strlenCounter = 0;
356 if (!HashTable_Foreach(d->table, foreachFn1, &d2))
358 d->test3error = TRUE;
361 if (d2.strlenCounter != 8)
363 d->test3error = TRUE;
370 static int test_hash_foreach(
void)
372 ForeachData foreachData;
373 wHashTable* table = NULL;
376 foreachData.table = table = HashTable_New(TRUE);
380 if (!HashTable_SetupForStringData(table, TRUE))
383 if (HashTable_Insert(table, key1, val1) < 0 || HashTable_Insert(table, key2, val2) < 0 ||
384 HashTable_Insert(table, key3, val3) < 0)
391 foreachData.strlenCounter = 0;
392 if (!HashTable_Foreach(table, foreachFn1, &foreachData))
397 if (foreachData.strlenCounter != 12)
404 foreachData.foreachCalls = 0;
405 if (HashTable_Foreach(table, foreachFn2, &foreachData))
410 if (foreachData.foreachCalls != 2)
417 foreachData.test3error = FALSE;
418 if (!HashTable_Foreach(table, foreachFn3, &foreachData))
423 if (foreachData.test3error)
430 HashTable_Free(table);
434 int TestHashTable(
int argc,
char* argv[])
439 if (test_hash_table_pointer() < 0)
442 if (test_hash_table_string() < 0)
445 if (test_hash_foreach() < 0)