blob: 3f2b2a2a253f0a83f78a128b4c82f9e6292705c8 [file] [log] [blame]
Benny Prijono844653c2008-12-23 17:27:53 +00001/* $Id$ */
Benny Prijonof1370372008-07-14 16:58:11 +00002/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijonof1370372008-07-14 16:58:11 +00005 *
6 * This program 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20#include <pj/hash.h>
21#include <pj/rand.h>
22#include <pj/log.h>
23#include <pj/pool.h>
24#include "test.h"
25
26#if INCLUDE_HASH_TEST
27
28#define HASH_COUNT 31
29
30static int hash_test_with_key(pj_pool_t *pool, unsigned char key)
31{
32 pj_hash_table_t *ht;
33 unsigned value = 0x12345;
34 pj_hash_iterator_t it_buf, *it;
35 unsigned *entry;
36
37 ht = pj_hash_create(pool, HASH_COUNT);
38 if (!ht)
39 return -10;
40
41 pj_hash_set(pool, ht, &key, sizeof(key), 0, &value);
42
43 entry = (unsigned*) pj_hash_get(ht, &key, sizeof(key), NULL);
44 if (!entry)
45 return -20;
46
47 if (*entry != value)
48 return -30;
49
50 if (pj_hash_count(ht) != 1)
51 return -30;
52
53 it = pj_hash_first(ht, &it_buf);
54 if (it == NULL)
55 return -40;
56
57 entry = (unsigned*) pj_hash_this(ht, it);
58 if (!entry)
59 return -50;
60
61 if (*entry != value)
62 return -60;
63
64 it = pj_hash_next(ht, it);
65 if (it != NULL)
66 return -70;
67
68 /* Erase item */
69
70 pj_hash_set(NULL, ht, &key, sizeof(key), 0, NULL);
71
72 if (pj_hash_get(ht, &key, sizeof(key), NULL) != NULL)
73 return -80;
74
75 if (pj_hash_count(ht) != 0)
76 return -90;
77
78 it = pj_hash_first(ht, &it_buf);
79 if (it != NULL)
80 return -100;
81
82 return 0;
83}
84
85
86static int hash_collision_test(pj_pool_t *pool)
87{
88 enum {
89 COUNT = HASH_COUNT * 4
90 };
91 pj_hash_table_t *ht;
92 pj_hash_iterator_t it_buf, *it;
93 unsigned char *values;
94 unsigned i;
95
96 ht = pj_hash_create(pool, HASH_COUNT);
97 if (!ht)
98 return -200;
99
100 values = (unsigned char*) pj_pool_alloc(pool, COUNT);
101
102 for (i=0; i<COUNT; ++i) {
103 values[i] = (unsigned char)i;
104 pj_hash_set(pool, ht, &i, sizeof(i), 0, &values[i]);
105 }
106
107 if (pj_hash_count(ht) != COUNT)
108 return -210;
109
110 for (i=0; i<COUNT; ++i) {
111 unsigned char *entry;
112 entry = (unsigned char*) pj_hash_get(ht, &i, sizeof(i), NULL);
113 if (!entry)
114 return -220;
115 if (*entry != values[i])
116 return -230;
117 }
118
119 i = 0;
120 it = pj_hash_first(ht, &it_buf);
121 while (it) {
122 ++i;
123 it = pj_hash_next(ht, it);
124 }
125
126 if (i != COUNT)
127 return -240;
128
129 return 0;
130}
131
132
133/*
134 * Hash table test.
135 */
136int hash_test(void)
137{
138 pj_pool_t *pool = pj_pool_create(mem, "hash", 512, 512, NULL);
139 int rc;
140 unsigned i;
141
142 /* Test to fill in each row in the table */
143 for (i=0; i<=HASH_COUNT; ++i) {
144 rc = hash_test_with_key(pool, (unsigned char)i);
145 if (rc != 0) {
146 pj_pool_release(pool);
147 return rc;
148 }
149 }
150
151 /* Collision test */
152 rc = hash_collision_test(pool);
153 if (rc != 0) {
154 pj_pool_release(pool);
155 return rc;
156 }
157
158 pj_pool_release(pool);
159 return 0;
160}
161
162#endif /* INCLUDE_HASH_TEST */
163