From 4b025f27d2a79fb44ee515f89bbf0123321fd81f Mon Sep 17 00:00:00 2001 From: arighi Date: Thu, 13 May 2010 13:05:48 +0000 Subject: [PATCH] Add the hashtable test module. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@3678 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/struct/hashtable.c | 89 ------------------------ bertos/struct/hashtable.h | 4 ++ bertos/struct/hashtable_test.c | 120 +++++++++++++++++++++++++++++++++ test/run_tests.sh | 1 + 4 files changed, 125 insertions(+), 89 deletions(-) create mode 100644 bertos/struct/hashtable_test.c 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 -- 2.25.1