/** * \file * * * \brief Test hashtable module. * * Test the hashtable module (insertion and find). * * \author Andrea Righi * * $test$: cp bertos/cfg/cfg_hashtable.h $cfgdir/ */ #include #include #include /* strlen() */ #include "struct/hashtable.h" static const void *test_get_key(const void *ptr, uint8_t *length) { const char *s = ptr; *length = strlen(s); return s; } #define NUM_ELEMENTS 256 DECLARE_HASHTABLE_STATIC(hash1, NUM_ELEMENTS, test_get_key); DECLARE_HASHTABLE_INTERNALKEY_STATIC(hash2, NUM_ELEMENTS); static char data[NUM_ELEMENTS][10]; static char keydomain[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; static bool single_test(void) { int i; ht_init(&hash1); ht_init(&hash2); for (i = 0; i < NUM_ELEMENTS; i++) { int k, klen; klen = (i % 8) + 1; for (k = 0; k < klen; k++) data[i][k] = keydomain[i % (sizeof(keydomain) - 1)]; data[i][k] = 0; ASSERT(ht_insert(&hash1, data[i])); ASSERT(ht_insert_str(&hash2, data[i], data[i])); } for (i = 0; i < NUM_ELEMENTS; i++) { const char *found1, *found2; found1 = ht_find_str(&hash1, data[i]); if (strcmp(found1, data[i])) return false; kprintf("hash1: found data[%d] = %s\n", i, found1); found2 = ht_find_str(&hash2, data[i]); if (strcmp(found2, data[i])) return false; kprintf("hash2: found data[%d] = %s\n", i, found2); } return true; } int hashtable_testRun(void) { if (!single_test()) { kprintf("hashtable_test failed\n"); return -1; } kprintf("hashtable_test successful\n"); return 0; } int hashtable_testSetup(void) { kdbg_init(); return 0; } int hashtable_testTearDown(void) { kputs("TearDown hashtable test.\n"); return 0; } TEST_MAIN(hashtable);