blob: 336f71c3732ba532ae26072bec38169cdf4a898f [file] [log] [blame]
Benny Prijonof1370372008-07-14 16:58:11 +00001/* $Id:$ */
2/*
3 * Copyright (C)2003-2008 Benny Prijono <benny@prijono.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include <pj/hash.h>
20#include <pj/rand.h>
21#include <pj/log.h>
22#include <pj/pool.h>
23#include "test.h"
24
25#if INCLUDE_HASH_TEST
26
27#define HASH_COUNT 31
28
29static int hash_test_with_key(pj_pool_t *pool, unsigned char key)
30{
31 pj_hash_table_t *ht;
32 unsigned value = 0x12345;
33 pj_hash_iterator_t it_buf, *it;
34 unsigned *entry;
35
36 ht = pj_hash_create(pool, HASH_COUNT);
37 if (!ht)
38 return -10;
39
40 pj_hash_set(pool, ht, &key, sizeof(key), 0, &value);
41
42 entry = (unsigned*) pj_hash_get(ht, &key, sizeof(key), NULL);
43 if (!entry)
44 return -20;
45
46 if (*entry != value)
47 return -30;
48
49 if (pj_hash_count(ht) != 1)
50 return -30;
51
52 it = pj_hash_first(ht, &it_buf);
53 if (it == NULL)
54 return -40;
55
56 entry = (unsigned*) pj_hash_this(ht, it);
57 if (!entry)
58 return -50;
59
60 if (*entry != value)
61 return -60;
62
63 it = pj_hash_next(ht, it);
64 if (it != NULL)
65 return -70;
66
67 /* Erase item */
68
69 pj_hash_set(NULL, ht, &key, sizeof(key), 0, NULL);
70
71 if (pj_hash_get(ht, &key, sizeof(key), NULL) != NULL)
72 return -80;
73
74 if (pj_hash_count(ht) != 0)
75 return -90;
76
77 it = pj_hash_first(ht, &it_buf);
78 if (it != NULL)
79 return -100;
80
81 return 0;
82}
83
84
85static int hash_collision_test(pj_pool_t *pool)
86{
87 enum {
88 COUNT = HASH_COUNT * 4
89 };
90 pj_hash_table_t *ht;
91 pj_hash_iterator_t it_buf, *it;
92 unsigned char *values;
93 unsigned i;
94
95 ht = pj_hash_create(pool, HASH_COUNT);
96 if (!ht)
97 return -200;
98
99 values = (unsigned char*) pj_pool_alloc(pool, COUNT);
100
101 for (i=0; i<COUNT; ++i) {
102 values[i] = (unsigned char)i;
103 pj_hash_set(pool, ht, &i, sizeof(i), 0, &values[i]);
104 }
105
106 if (pj_hash_count(ht) != COUNT)
107 return -210;
108
109 for (i=0; i<COUNT; ++i) {
110 unsigned char *entry;
111 entry = (unsigned char*) pj_hash_get(ht, &i, sizeof(i), NULL);
112 if (!entry)
113 return -220;
114 if (*entry != values[i])
115 return -230;
116 }
117
118 i = 0;
119 it = pj_hash_first(ht, &it_buf);
120 while (it) {
121 ++i;
122 it = pj_hash_next(ht, it);
123 }
124
125 if (i != COUNT)
126 return -240;
127
128 return 0;
129}
130
131
132/*
133 * Hash table test.
134 */
135int hash_test(void)
136{
137 pj_pool_t *pool = pj_pool_create(mem, "hash", 512, 512, NULL);
138 int rc;
139 unsigned i;
140
141 /* Test to fill in each row in the table */
142 for (i=0; i<=HASH_COUNT; ++i) {
143 rc = hash_test_with_key(pool, (unsigned char)i);
144 if (rc != 0) {
145 pj_pool_release(pool);
146 return rc;
147 }
148 }
149
150 /* Collision test */
151 rc = hash_collision_test(pool);
152 if (rc != 0) {
153 pj_pool_release(pool);
154 return rc;
155 }
156
157 pj_pool_release(pool);
158 return 0;
159}
160
161#endif /* INCLUDE_HASH_TEST */
162