Add the hashtable test module.
[bertos.git] / bertos / struct / hashtable_test.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  * -->
31  *
32  * \brief Test hashtable module.
33  *
34  * Test the hashtable module (insertion and find).
35  *
36  * \author Andrea Righi <arighi@develer.com>
37  *
38  * $test$: cp bertos/cfg/cfg_hashtable.h $cfgdir/
39  */
40
41 #include <cfg/debug.h>
42 #include <cfg/test.h>
43 #include <string.h> /* strlen() */
44 #include "struct/hashtable.h"
45
46 static const void *test_get_key(const void *ptr, uint8_t *length)
47 {
48         const char *s = ptr;
49
50         *length = strlen(s);
51         return s;
52 }
53
54 #define NUM_ELEMENTS   256
55 DECLARE_HASHTABLE_STATIC(hash1, NUM_ELEMENTS, test_get_key);
56 DECLARE_HASHTABLE_INTERNALKEY_STATIC(hash2, NUM_ELEMENTS);
57
58 static char data[NUM_ELEMENTS][10];
59 static char keydomain[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
60
61 static bool single_test(void)
62 {
63         int i;
64
65         ht_init(&hash1);
66         ht_init(&hash2);
67
68         for (i = 0; i < NUM_ELEMENTS; i++)
69         {
70                 int k, klen;
71
72                 klen = (i % 8) + 1;
73                 for (k = 0; k < klen; k++)
74                         data[i][k] = keydomain[i % (sizeof(keydomain) - 1)];
75                 data[i][k] = 0;
76
77                 ASSERT(ht_insert(&hash1, data[i]));
78                 ASSERT(ht_insert_str(&hash2, data[i], data[i]));
79         }
80         for (i = 0; i < NUM_ELEMENTS; i++)
81         {
82                 const char *found1, *found2;
83
84                 found1 = ht_find_str(&hash1, data[i]);
85                 if (strcmp(found1, data[i]))
86                         return false;
87                 kprintf("hash1: found data[%d] = %s\n", i, found1);
88
89                 found2 = ht_find_str(&hash2, data[i]);
90                 if (strcmp(found2, data[i]))
91                         return false;
92                 kprintf("hash2: found data[%d] = %s\n", i, found2);
93         }
94         return true;
95 }
96
97 int hashtable_testRun(void)
98 {
99         if (!single_test())
100         {
101                 kprintf("hashtable_test failed\n");
102                 return -1;
103         }
104         kprintf("hashtable_test successful\n");
105         return 0;
106 }
107
108 int hashtable_testSetup(void)
109 {
110         kdbg_init();
111         return 0;
112 }
113
114 int hashtable_testTearDown(void)
115 {
116         kputs("TearDown hashtable test.\n");
117         return 0;
118 }
119
120 TEST_MAIN(hashtable);