From: arighi Date: Thu, 13 May 2010 13:05:48 +0000 (+0000) Subject: Add the hashtable test module. X-Git-Tag: 2.5.0~239 X-Git-Url: https://codewiz.org/gitweb?a=commitdiff_plain;h=4b025f27d2a79fb44ee515f89bbf0123321fd81f;p=bertos.git Add the hashtable test module. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@3678 38d2e660-2303-0410-9eaa-f027e97ec537 --- diff --git a/bertos/struct/hashtable.c b/bertos/struct/hashtable.c index 5e4ee652..d895fe75 100644 --- a/bertos/struct/hashtable.c +++ b/bertos/struct/hashtable.c @@ -285,92 +285,3 @@ const void* ht_find(struct HashTable* ht, const void* key, uint8_t key_length) return *node; } - - -#if 0 - -#include - -bool ht_test(void); - -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(test1, 256, test_get_key); -DECLARE_HASHTABLE_INTERNALKEY_STATIC(test2, 256); - -static char data[NUM_ELEMENTS][10]; -static char keydomain[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - -static bool single_test(void) -{ - int i; - - ht_init(&test1); - ht_init(&test2); - - for (i=0;i + * + * \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); diff --git a/test/run_tests.sh b/test/run_tests.sh index d79f46fe..916be6ba 100755 --- a/test/run_tests.sh +++ b/test/run_tests.sh @@ -47,6 +47,7 @@ SRC_LIST=" bertos/os/hptime.c bertos/struct/kfile_fifo.c bertos/struct/heap.c + bertos/struct/hashtable.c bertos/fs/fatfs/ff.c bertos/emul/diskio_emul.c bertos/fs/fat.c