Blame winpr/libwinpr/utils/test/TestHashTable.c

Packit 1fb8d4
Packit 1fb8d4
#include <winpr/crt.h>
Packit 1fb8d4
#include <winpr/tchar.h>
Packit 1fb8d4
#include <winpr/collections.h>
Packit 1fb8d4
Packit 1fb8d4
static char* key1 = "key1";
Packit 1fb8d4
static char* key2 = "key2";
Packit 1fb8d4
static char* key3 = "key3";
Packit 1fb8d4
Packit 1fb8d4
static char* val1 = "val1";
Packit 1fb8d4
static char* val2 = "val2";
Packit 1fb8d4
static char* val3 = "val3";
Packit 1fb8d4
Packit 1fb8d4
static int test_hash_table_pointer(void)
Packit 1fb8d4
{
Packit 1fb8d4
	int rc = -1;
Packit 1fb8d4
	int count;
Packit 1fb8d4
	char* value;
Packit 1fb8d4
	wHashTable* table;
Packit 1fb8d4
	table = HashTable_New(TRUE);
Packit 1fb8d4
Packit 1fb8d4
	if (!table)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	HashTable_Add(table, key1, val1);
Packit 1fb8d4
	HashTable_Add(table, key2, val2);
Packit 1fb8d4
	HashTable_Add(table, key3, val3);
Packit 1fb8d4
	count = HashTable_Count(table);
Packit 1fb8d4
Packit 1fb8d4
	if (count != 3)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("HashTable_Count: Expected : 3, Actual: %d\n", count);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	HashTable_Remove(table, key2);
Packit 1fb8d4
	count = HashTable_Count(table);
Packit 1fb8d4
Packit 1fb8d4
	if (count != 2)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("HashTable_Count: Expected : 2, Actual: %d\n", count);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	HashTable_Remove(table, key3);
Packit 1fb8d4
	count = HashTable_Count(table);
Packit 1fb8d4
Packit 1fb8d4
	if (count != 1)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("HashTable_Count: Expected : 1, Actual: %d\n", count);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	HashTable_Remove(table, key1);
Packit 1fb8d4
	count = HashTable_Count(table);
Packit 1fb8d4
Packit 1fb8d4
	if (count != 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("HashTable_Count: Expected : 0, Actual: %d\n", count);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	HashTable_Add(table, key1, val1);
Packit 1fb8d4
	HashTable_Add(table, key2, val2);
Packit 1fb8d4
	HashTable_Add(table, key3, val3);
Packit 1fb8d4
	count = HashTable_Count(table);
Packit 1fb8d4
Packit 1fb8d4
	if (count != 3)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("HashTable_Count: Expected : 3, Actual: %d\n", count);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	value = (char*)HashTable_GetItemValue(table, key1);
Packit 1fb8d4
Packit 1fb8d4
	if (strcmp(value, val1) != 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("HashTable_GetItemValue: Expected : %s, Actual: %s\n", val1, value);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	value = (char*)HashTable_GetItemValue(table, key2);
Packit 1fb8d4
Packit 1fb8d4
	if (strcmp(value, val2) != 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("HashTable_GetItemValue: Expected : %s, Actual: %s\n", val2, value);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	value = (char*)HashTable_GetItemValue(table, key3);
Packit 1fb8d4
Packit 1fb8d4
	if (strcmp(value, val3) != 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("HashTable_GetItemValue: Expected : %s, Actual: %s\n", val3, value);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	HashTable_SetItemValue(table, key2, "apple");
Packit Service 5a9772
	value = (char*)HashTable_GetItemValue(table, key2);
Packit 1fb8d4
Packit 1fb8d4
	if (strcmp(value, "apple") != 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("HashTable_GetItemValue: Expected : %s, Actual: %s\n", "apple", value);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (!HashTable_Contains(table, key2))
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("HashTable_Contains: Expected : TRUE, Actual: FALSE\n");
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (!HashTable_Remove(table, key2))
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("HashTable_Remove: Expected : TRUE, Actual: FALSE\n");
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (HashTable_Remove(table, key2))
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("HashTable_Remove: Expected : FALSE, Actual: TRUE\n");
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	HashTable_Clear(table);
Packit 1fb8d4
	count = HashTable_Count(table);
Packit 1fb8d4
Packit 1fb8d4
	if (count != 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("HashTable_Count: Expected : 0, Actual: %d\n", count);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	rc = 1;
Packit 1fb8d4
fail:
Packit 1fb8d4
	HashTable_Free(table);
Packit 1fb8d4
	return rc;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static int test_hash_table_string(void)
Packit 1fb8d4
{
Packit 1fb8d4
	int rc = -1;
Packit 1fb8d4
	int count;
Packit 1fb8d4
	char* value;
Packit 1fb8d4
	wHashTable* table;
Packit 1fb8d4
	table = HashTable_New(TRUE);
Packit 1fb8d4
Packit 1fb8d4
	if (!table)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	table->hash = HashTable_StringHash;
Packit 1fb8d4
	table->keyCompare = HashTable_StringCompare;
Packit 1fb8d4
	table->valueCompare = HashTable_StringCompare;
Packit 1fb8d4
	table->keyClone = HashTable_StringClone;
Packit 1fb8d4
	table->valueClone = HashTable_StringClone;
Packit 1fb8d4
	table->keyFree = HashTable_StringFree;
Packit 1fb8d4
	table->valueFree = HashTable_StringFree;
Packit 1fb8d4
	HashTable_Add(table, key1, val1);
Packit 1fb8d4
	HashTable_Add(table, key2, val2);
Packit 1fb8d4
	HashTable_Add(table, key3, val3);
Packit 1fb8d4
	count = HashTable_Count(table);
Packit 1fb8d4
Packit 1fb8d4
	if (count != 3)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("HashTable_Count: Expected : 3, Actual: %d\n", count);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	HashTable_Remove(table, key2);
Packit 1fb8d4
	count = HashTable_Count(table);
Packit 1fb8d4
Packit 1fb8d4
	if (count != 2)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("HashTable_Count: Expected : 3, Actual: %d\n", count);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	HashTable_Remove(table, key3);
Packit 1fb8d4
	count = HashTable_Count(table);
Packit 1fb8d4
Packit 1fb8d4
	if (count != 1)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("HashTable_Count: Expected : 1, Actual: %d\n", count);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	HashTable_Remove(table, key1);
Packit 1fb8d4
	count = HashTable_Count(table);
Packit 1fb8d4
Packit 1fb8d4
	if (count != 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("HashTable_Count: Expected : 0, Actual: %d\n", count);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	HashTable_Add(table, key1, val1);
Packit 1fb8d4
	HashTable_Add(table, key2, val2);
Packit 1fb8d4
	HashTable_Add(table, key3, val3);
Packit 1fb8d4
	count = HashTable_Count(table);
Packit 1fb8d4
Packit 1fb8d4
	if (count != 3)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("HashTable_Count: Expected : 3, Actual: %d\n", count);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	value = (char*)HashTable_GetItemValue(table, key1);
Packit 1fb8d4
Packit 1fb8d4
	if (strcmp(value, val1) != 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("HashTable_GetItemValue: Expected : %s, Actual: %s\n", val1, value);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	value = (char*)HashTable_GetItemValue(table, key2);
Packit 1fb8d4
Packit 1fb8d4
	if (strcmp(value, val2) != 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("HashTable_GetItemValue: Expected : %s, Actual: %s\n", val2, value);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	value = (char*)HashTable_GetItemValue(table, key3);
Packit 1fb8d4
Packit 1fb8d4
	if (strcmp(value, val3) != 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("HashTable_GetItemValue: Expected : %s, Actual: %s\n", val3, value);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	HashTable_SetItemValue(table, key2, "apple");
Packit Service 5a9772
	value = (char*)HashTable_GetItemValue(table, key2);
Packit 1fb8d4
Packit 1fb8d4
	if (strcmp(value, "apple") != 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("HashTable_GetItemValue: Expected : %s, Actual: %s\n", "apple", value);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (!HashTable_Contains(table, key2))
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("HashTable_Contains: Expected : TRUE, Actual: FALSE\n");
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (!HashTable_Remove(table, key2))
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("HashTable_Remove: Expected : TRUE, Actual: FALSE\n");
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (HashTable_Remove(table, key2))
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("HashTable_Remove: Expected : FALSE, Actual: TRUE\n");
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	HashTable_Clear(table);
Packit 1fb8d4
	count = HashTable_Count(table);
Packit 1fb8d4
Packit 1fb8d4
	if (count != 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("HashTable_Count: Expected : 0, Actual: %d\n", count);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	rc = 1;
Packit 1fb8d4
fail:
Packit 1fb8d4
	HashTable_Free(table);
Packit 1fb8d4
	return rc;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
int TestHashTable(int argc, char* argv[])
Packit 1fb8d4
{
Packit 1fb8d4
	if (test_hash_table_pointer() < 0)
Packit 1fb8d4
		return 1;
Packit 1fb8d4
Packit 1fb8d4
	if (test_hash_table_string() < 0)
Packit 1fb8d4
		return 1;
Packit 1fb8d4
Packit 1fb8d4
	return 0;
Packit 1fb8d4
}