wlgol/tests/test_hashtable.c
2024-11-03 12:39:18 +01:00

64 lines
1.4 KiB
C

#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <hashtable.h>
void test_hashtable(int size) {
hashtable h = hashtable_new(size);
void *value1 = "to jest jakaś tam wartość";
void *value2 = "I inna";
void *value3 = "trzecia";
void *value4 = "Czwarta";
void *value5 = "Piąta";
void *retval = hashtable_set(h, "jeden", value1);
assert(retval == NULL);
void *found = hashtable_lookup(h, "jeden");
assert(found != NULL && !strcmp(value1, found));
retval = hashtable_set(h, "dwa", value2);
assert(retval == NULL);
retval = hashtable_set(h, "trzy", value3);
assert(retval == NULL);
retval = hashtable_set(h, "cztery", value4);
assert(retval == NULL);
retval = hashtable_set(h, "jeden", value5);
assert(retval != NULL);
assert (retval == value1);
retval = hashtable_lookup(h, "niema");
assert(retval == NULL);
retval = hashtable_lookup(h, "jeden");
assert(retval == value5);
ht_iterator iter = hashtable_iter(h);
while (ht_iter_next(&iter)) {
printf("Key %s, value: %s\n", ht_iter_key(&iter), (const char*) ht_iter_value(&iter));
}
retval = hashtable_remove(h, "cztery");
assert (retval == value4);
retval = hashtable_remove(h, "trzy");
assert(retval == value3);
retval = hashtable_remove(h, "dwa");
assert(retval == value2);
retval = hashtable_remove(h, "jeden");
assert(retval == value5);
hashtable_destroy(h);
}
int main() {
test_hashtable(2);
test_hashtable(10);
}