Add the hashtable test module.
authorarighi <arighi@38d2e660-2303-0410-9eaa-f027e97ec537>
Thu, 13 May 2010 13:05:48 +0000 (13:05 +0000)
committerarighi <arighi@38d2e660-2303-0410-9eaa-f027e97ec537>
Thu, 13 May 2010 13:05:48 +0000 (13:05 +0000)
git-svn-id: https://src.develer.com/svnoss/bertos/trunk@3678 38d2e660-2303-0410-9eaa-f027e97ec537

bertos/struct/hashtable.c
bertos/struct/hashtable.h
bertos/struct/hashtable_test.c [new file with mode: 0644]
test/run_tests.sh

index 5e4ee652076a69ae581736a147beb3e250b24735..d895fe7559255a6059c3900a816ae4f32edc0a21 100644 (file)
@@ -285,92 +285,3 @@ const void* ht_find(struct HashTable* ht, const void* key, uint8_t key_length)
 
        return *node;
 }
-
-
-#if 0
-
-#include <stdlib.h>
-
-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<NUM_ELEMENTS;i++)
-       {
-               int k;
-               int klen;
-
-               do
-               {
-                       klen = (rand() % 8) + 1;
-                       for (k=0;k<klen;k++)
-                               data[i][k] = keydomain[rand() % (sizeof(keydomain)-1)];
-                       data[i][k]=0;
-               } while (ht_find_str(&test1, data[i]) != NULL);
-
-               ASSERT(ht_insert(&test1, data[i]));
-               ASSERT(ht_insert_str(&test2, data[i], data[i]));
-       }
-
-       for (i=0;i<NUM_ELEMENTS;i++)
-       {
-               char *found1, *found2;
-
-               found1 = (char*)ht_find_str(&test1, data[i]);
-               if (strcmp(found1, data[i]) != 0)
-               {
-                       ASSERT(strcmp(found1,data[i]) == 0);
-                       return false;
-               }
-
-               found2 = (char*)ht_find_str(&test2, data[i]);
-               if (strcmp(found2, data[i]) != 0)
-               {
-                       ASSERT(strcmp(found2,data[i]) == 0);
-                       return false;
-               }
-       }
-
-       return true;
-}
-
-static uint16_t rand_seeds[] = { 1, 42, 666, 0xDEAD, 0xBEEF, 0x1337, 0xB00B };
-
-bool ht_test(void)
-{
-       int i;
-
-       for (i=0;i<countof(rand_seeds);++i)
-       {
-               srand(rand_seeds[i]);
-               if (!single_test())
-               {
-                       kprintf("ht_test failed\n");
-                       return false;
-               }
-       }
-
-       kprintf("ht_test successful\n");
-       return true;
-}
-
-#endif
index f721803e0907c04606ad0fe397fb9bf163aaaaad..0b6f4c8f23816fa9b5f42ba6a676c5cde68b94d6 100644 (file)
@@ -285,4 +285,8 @@ INLINE HashIterator ht_iter_next(HashIterator h)
        return h;
 }
 
+int hashtable_testSetup(void);
+int hashtable_testRun(void);
+int hashtable_testTearDown(void);
+
 #endif /* STRUCT_HASHTABLE_H */
diff --git a/bertos/struct/hashtable_test.c b/bertos/struct/hashtable_test.c
new file mode 100644 (file)
index 0000000..4e5492b
--- /dev/null
@@ -0,0 +1,120 @@
+/**
+ * \file
+ * <!--
+ * This file is part of BeRTOS.
+ *
+ * Bertos is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * As a special exception, you may use this file as part of a free software
+ * library without restriction.  Specifically, if other files instantiate
+ * templates or use macros or inline functions from this file, or you compile
+ * this file and link it with other files to produce an executable, this
+ * file does not by itself cause the resulting executable to be covered by
+ * the GNU General Public License.  This exception does not however
+ * invalidate any other reasons why the executable file might be covered by
+ * the GNU General Public License.
+ *
+ * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
+ * -->
+ *
+ * \brief Test hashtable module.
+ *
+ * Test the hashtable module (insertion and find).
+ *
+ * \author Andrea Righi <arighi@develer.com>
+ *
+ * $test$: cp bertos/cfg/cfg_hashtable.h $cfgdir/
+ */
+
+#include <cfg/debug.h>
+#include <cfg/test.h>
+#include <string.h> /* 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);
index d79f46feb4ddcc23ffd0bc0958e9a20765b189eb..916be6ba76277c61a8895d69c3f745c2b18a816e 100755 (executable)
@@ -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