hash table test
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
#include <check.h>
|
||||
#include <string.h>
|
||||
#include <ucore/htable.h>
|
||||
#include <ucore/utils.h>
|
||||
|
||||
struct MyString {
|
||||
char str[32];
|
||||
struct UHNode node;
|
||||
};
|
||||
|
||||
size_t hash_str(const char *str)
|
||||
{
|
||||
size_t hash = 31;
|
||||
while (*str) {
|
||||
hash *= *str++;
|
||||
}
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
struct MyString *my_find(struct UHTable *table, const char *str)
|
||||
{
|
||||
struct UHNode *it;
|
||||
size_t hash = hash_str(str);
|
||||
|
||||
UC_HTABLE_FOREACH_HASH(table, it, hash) {
|
||||
struct MyString *my_str = UC_CONTAINER_OF(it, struct MyString, node);
|
||||
if (strcmp(my_str->str, str) == 0) {
|
||||
return my_str;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
START_TEST (test_htable1)
|
||||
struct MyString str[5] = {
|
||||
{"One", {0,NULL}},
|
||||
{"Two", {0,NULL}},
|
||||
{"Three",{0,NULL}},
|
||||
{"Four", {0,NULL}},
|
||||
{"Five", {0,NULL}}
|
||||
};
|
||||
struct UHTable table;
|
||||
int i;
|
||||
|
||||
uc_htable_init(&table, 3);
|
||||
|
||||
for (i = 0; i < 5; i++) {
|
||||
uc_htable_insert(&table, &str[i].node, hash_str(str[i].str));
|
||||
}
|
||||
|
||||
ck_assert_int_eq(uc_htable_count(&table), 5);
|
||||
|
||||
fail_if(my_find(&table, "One") != &str[0]);
|
||||
fail_if(my_find(&table, "Two") != &str[1]);
|
||||
fail_if(my_find(&table, "Three") != &str[2]);
|
||||
fail_if(my_find(&table, "Four") != &str[3]);
|
||||
fail_if(my_find(&table, "Five") != &str[4]);
|
||||
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_htable_resize)
|
||||
struct MyString str[5] = {
|
||||
{"One", {0,NULL}},
|
||||
{"Two", {0,NULL}},
|
||||
{"Three",{0,NULL}},
|
||||
{"Four", {0,NULL}},
|
||||
{"Five", {0,NULL}}
|
||||
};
|
||||
struct UHTable table;
|
||||
int i;
|
||||
int rc;
|
||||
|
||||
rc = uc_htable_init(&table, 3);
|
||||
ck_assert_int_eq(rc, 0);
|
||||
|
||||
for (i = 0; i < 5; i++) {
|
||||
uc_htable_insert(&table, &str[i].node, hash_str(str[i].str));
|
||||
}
|
||||
ck_assert_int_eq(uc_htable_count(&table), 5);
|
||||
|
||||
rc = uc_htable_resize(&table, 1024);
|
||||
ck_assert_int_eq(rc, 0);
|
||||
|
||||
ck_assert_int_eq(uc_htable_count(&table), 5);
|
||||
|
||||
fail_if(my_find(&table, "One") != &str[0]);
|
||||
fail_if(my_find(&table, "Two") != &str[1]);
|
||||
fail_if(my_find(&table, "Three") != &str[2]);
|
||||
fail_if(my_find(&table, "Four") != &str[3]);
|
||||
fail_if(my_find(&table, "Five") != &str[4]);
|
||||
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_htable_fail_init_resize)
|
||||
int rc;
|
||||
struct UHTable table;
|
||||
rc = uc_htable_init(&table, 0);
|
||||
ck_assert_int_ne(rc, 0);
|
||||
|
||||
rc = uc_htable_init(&table, 1);
|
||||
ck_assert_int_eq(rc, 0);
|
||||
|
||||
rc = uc_htable_resize(&table, 0);
|
||||
ck_assert_int_ne(rc, 0);
|
||||
|
||||
END_TEST
|
||||
Suite *htable_suite(void)
|
||||
{
|
||||
Suite *s = suite_create("htable");
|
||||
TCase *tc = tcase_create("htable tests");
|
||||
|
||||
tcase_add_test(tc, test_htable1);
|
||||
tcase_add_test(tc, test_htable_resize);
|
||||
tcase_add_test(tc, test_htable_fail_init_resize);
|
||||
|
||||
|
||||
suite_add_tcase(s, tc);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user