2014-02-10 06:55:21 +04:00
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/tchar.h>
|
|
|
|
#include <winpr/collections.h>
|
|
|
|
|
|
|
|
static char* key1 = "key1";
|
|
|
|
static char* key2 = "key2";
|
|
|
|
static char* key3 = "key3";
|
|
|
|
|
|
|
|
static char* val1 = "val1";
|
|
|
|
static char* val2 = "val2";
|
|
|
|
static char* val3 = "val3";
|
|
|
|
|
2014-10-10 00:46:55 +04:00
|
|
|
int test_hash_table_pointer()
|
2014-02-10 06:55:21 +04:00
|
|
|
{
|
|
|
|
int count;
|
|
|
|
char* value;
|
|
|
|
wHashTable* table;
|
|
|
|
|
2014-02-14 07:25:30 +04:00
|
|
|
table = HashTable_New(TRUE);
|
2015-05-18 12:28:00 +03:00
|
|
|
if (!table)
|
|
|
|
return -1;
|
2014-02-10 06:55:21 +04:00
|
|
|
|
|
|
|
HashTable_Add(table, key1, val1);
|
|
|
|
HashTable_Add(table, key2, val2);
|
|
|
|
HashTable_Add(table, key3, val3);
|
|
|
|
|
|
|
|
count = HashTable_Count(table);
|
|
|
|
|
|
|
|
if (count != 3)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("HashTable_Count: Expected : 3, Actual: %d\n", count);
|
2014-02-10 06:55:21 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
HashTable_Remove(table, key2);
|
|
|
|
|
|
|
|
count = HashTable_Count(table);
|
|
|
|
|
|
|
|
if (count != 2)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("HashTable_Count: Expected : 2, Actual: %d\n", count);
|
2014-02-10 06:55:21 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
HashTable_Remove(table, key3);
|
|
|
|
|
|
|
|
count = HashTable_Count(table);
|
|
|
|
|
|
|
|
if (count != 1)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("HashTable_Count: Expected : 1, Actual: %d\n", count);
|
2014-02-10 06:55:21 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
HashTable_Remove(table, key1);
|
|
|
|
|
|
|
|
count = HashTable_Count(table);
|
|
|
|
|
|
|
|
if (count != 0)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("HashTable_Count: Expected : 0, Actual: %d\n", count);
|
2014-02-10 06:55:21 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
HashTable_Add(table, key1, val1);
|
|
|
|
HashTable_Add(table, key2, val2);
|
|
|
|
HashTable_Add(table, key3, val3);
|
|
|
|
|
|
|
|
count = HashTable_Count(table);
|
|
|
|
|
|
|
|
if (count != 3)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("HashTable_Count: Expected : 3, Actual: %d\n", count);
|
2014-02-10 06:55:21 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = (char*) HashTable_GetItemValue(table, key1);
|
|
|
|
|
|
|
|
if (strcmp(value, val1) != 0)
|
|
|
|
{
|
2014-05-09 02:02:02 +04:00
|
|
|
printf("HashTable_GetItemValue: Expected : %s, Actual: %s\n", val1, value);
|
2014-02-10 06:55:21 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = (char*) HashTable_GetItemValue(table, key2);
|
|
|
|
|
|
|
|
if (strcmp(value, val2) != 0)
|
|
|
|
{
|
2014-05-09 02:02:02 +04:00
|
|
|
printf("HashTable_GetItemValue: Expected : %s, Actual: %s\n", val2, value);
|
2014-02-10 06:55:21 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = (char*) HashTable_GetItemValue(table, key3);
|
|
|
|
|
|
|
|
if (strcmp(value, val3) != 0)
|
|
|
|
{
|
2014-05-09 02:02:02 +04:00
|
|
|
printf("HashTable_GetItemValue: Expected : %s, Actual: %s\n", val3, value);
|
2014-02-10 06:55:21 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
HashTable_SetItemValue(table, key2, "apple");
|
|
|
|
|
|
|
|
value = (char*) HashTable_GetItemValue(table, key2);
|
|
|
|
|
|
|
|
if (strcmp(value, "apple") != 0)
|
|
|
|
{
|
2014-05-09 02:02:02 +04:00
|
|
|
printf("HashTable_GetItemValue: Expected : %s, Actual: %s\n", "apple", value);
|
2014-02-10 06:55:21 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!HashTable_Contains(table, key2))
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("HashTable_Contains: Expected : TRUE, Actual: FALSE\n");
|
2014-02-10 06:55:21 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!HashTable_Remove(table, key2))
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("HashTable_Remove: Expected : TRUE, Actual: FALSE\n");
|
2014-02-10 06:55:21 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (HashTable_Remove(table, key2))
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("HashTable_Remove: Expected : FALSE, Actual: TRUE\n");
|
2014-02-10 06:55:21 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
HashTable_Clear(table);
|
|
|
|
|
|
|
|
count = HashTable_Count(table);
|
|
|
|
|
|
|
|
if (count != 0)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("HashTable_Count: Expected : 0, Actual: %d\n", count);
|
2014-02-10 06:55:21 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
HashTable_Free(table);
|
|
|
|
|
2014-10-10 00:46:55 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int test_hash_table_string()
|
|
|
|
{
|
|
|
|
int count;
|
|
|
|
char* value;
|
|
|
|
wHashTable* table;
|
|
|
|
|
|
|
|
table = HashTable_New(TRUE);
|
2015-05-18 12:28:00 +03:00
|
|
|
if (!table)
|
|
|
|
return -1;
|
2014-10-10 00:46:55 +04:00
|
|
|
|
|
|
|
table->hash = HashTable_StringHash;
|
|
|
|
table->keyCompare = HashTable_StringCompare;
|
|
|
|
table->valueCompare = HashTable_StringCompare;
|
|
|
|
table->keyClone = HashTable_StringClone;
|
|
|
|
table->valueClone = HashTable_StringClone;
|
|
|
|
table->keyFree = HashTable_StringFree;
|
|
|
|
table->valueFree = HashTable_StringFree;
|
|
|
|
|
|
|
|
HashTable_Add(table, key1, val1);
|
|
|
|
HashTable_Add(table, key2, val2);
|
|
|
|
HashTable_Add(table, key3, val3);
|
|
|
|
|
|
|
|
count = HashTable_Count(table);
|
|
|
|
|
|
|
|
if (count != 3)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("HashTable_Count: Expected : 3, Actual: %d\n", count);
|
2014-10-10 00:46:55 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
HashTable_Remove(table, key2);
|
|
|
|
|
|
|
|
count = HashTable_Count(table);
|
|
|
|
|
|
|
|
if (count != 2)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("HashTable_Count: Expected : 3, Actual: %d\n", count);
|
2014-10-10 00:46:55 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
HashTable_Remove(table, key3);
|
|
|
|
|
|
|
|
count = HashTable_Count(table);
|
|
|
|
|
|
|
|
if (count != 1)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("HashTable_Count: Expected : 1, Actual: %d\n", count);
|
2014-10-10 00:46:55 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
HashTable_Remove(table, key1);
|
|
|
|
|
|
|
|
count = HashTable_Count(table);
|
|
|
|
|
|
|
|
if (count != 0)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("HashTable_Count: Expected : 0, Actual: %d\n", count);
|
2014-10-10 00:46:55 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
HashTable_Add(table, key1, val1);
|
|
|
|
HashTable_Add(table, key2, val2);
|
|
|
|
HashTable_Add(table, key3, val3);
|
|
|
|
|
|
|
|
count = HashTable_Count(table);
|
|
|
|
|
|
|
|
if (count != 3)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("HashTable_Count: Expected : 3, Actual: %d\n", count);
|
2014-10-10 00:46:55 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = (char*) HashTable_GetItemValue(table, key1);
|
|
|
|
|
|
|
|
if (strcmp(value, val1) != 0)
|
|
|
|
{
|
|
|
|
printf("HashTable_GetItemValue: Expected : %s, Actual: %s\n", val1, value);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = (char*) HashTable_GetItemValue(table, key2);
|
|
|
|
|
|
|
|
if (strcmp(value, val2) != 0)
|
|
|
|
{
|
|
|
|
printf("HashTable_GetItemValue: Expected : %s, Actual: %s\n", val2, value);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = (char*) HashTable_GetItemValue(table, key3);
|
|
|
|
|
|
|
|
if (strcmp(value, val3) != 0)
|
|
|
|
{
|
|
|
|
printf("HashTable_GetItemValue: Expected : %s, Actual: %s\n", val3, value);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
HashTable_SetItemValue(table, key2, "apple");
|
|
|
|
|
|
|
|
value = (char*) HashTable_GetItemValue(table, key2);
|
|
|
|
|
|
|
|
if (strcmp(value, "apple") != 0)
|
|
|
|
{
|
|
|
|
printf("HashTable_GetItemValue: Expected : %s, Actual: %s\n", "apple", value);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!HashTable_Contains(table, key2))
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("HashTable_Contains: Expected : TRUE, Actual: FALSE\n");
|
2014-10-10 00:46:55 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!HashTable_Remove(table, key2))
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("HashTable_Remove: Expected : TRUE, Actual: FALSE\n");
|
2014-10-10 00:46:55 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (HashTable_Remove(table, key2))
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("HashTable_Remove: Expected : FALSE, Actual: TRUE\n");
|
2014-10-10 00:46:55 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
HashTable_Clear(table);
|
|
|
|
|
|
|
|
count = HashTable_Count(table);
|
|
|
|
|
|
|
|
if (count != 0)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("HashTable_Count: Expected : 0, Actual: %d\n", count);
|
2014-10-10 00:46:55 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
HashTable_Free(table);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TestHashTable(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
if (test_hash_table_pointer() < 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (test_hash_table_string() < 0)
|
|
|
|
return 1;
|
|
|
|
|
2014-02-10 06:55:21 +04:00
|
|
|
return 0;
|
|
|
|
}
|