]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/cintltst/chashtst.c
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / test / cintltst / chashtst.c
CommitLineData
b75a7d8f
A
1/*
2*******************************************************************************
3* Copyright (C) 2000, International Business Machines
4* Corporation and others. All Rights Reserved.
5*******************************************************************************
6* Date Name Description
7* 03/22/00 aliu Creation.
8* 07/13/00 Madhu Added more tests
9*******************************************************************************
10*/
11
12#include "cintltst.h"
13#include "uhash.h"
14#include "unicode/ctest.h"
15#include "unicode/ustring.h"
16#include "cstring.h"
17
18/**********************************************************************
19 * Prototypes
20 *********************************************************************/
21
22static void TestBasic(void);
23static void TestOtherAPI(void);
24
25static int32_t U_EXPORT2 U_CALLCONV hashChars(const UHashTok key);
26
27static UBool U_EXPORT2 U_CALLCONV isEqualChars(const UHashTok key1, const UHashTok key2);
28
29static void _put(UHashtable* hash,
30 const char* key,
31 int32_t value,
32 int32_t expectedOldValue);
33
34static void _get(UHashtable* hash,
35 const char* key,
36 int32_t expectedValue);
37
38static void _remove(UHashtable* hash,
39 const char* key,
40 int32_t expectedValue);
41
42void addHashtableTest(TestNode** root);
43
44/**********************************************************************
45 * UHashTok wrapper functions
46 *********************************************************************/
47
48static UBool
49_compareChars(void* a, void* b) {
50 UHashTok s, t;
51 s.pointer = a;
52 t.pointer = b;
53 return uhash_compareChars(s, t);
54}
55
56static UBool
57_compareIChars(void* a, void* b) {
58 UHashTok s, t;
59 s.pointer = a;
60 t.pointer = b;
61 return uhash_compareIChars(s, t);
62}
63
64static UBool
65_compareUChars(void* a, void* b) {
66 UHashTok s, t;
67 s.pointer = a;
68 t.pointer = b;
69 return uhash_compareUChars(s, t);
70}
71
72static UBool
73_compareLong(int32_t a, int32_t b) {
74 UHashTok s, t;
75 s.integer = a;
76 t.integer = b;
77 return uhash_compareLong(s, t);
78}
79
80/**********************************************************************
81 * FW Registration
82 *********************************************************************/
83
84void addHashtableTest(TestNode** root) {
85
86 addTest(root, &TestBasic, "tsutil/chashtst/TestBasic");
87 addTest(root, &TestOtherAPI, "tsutil/chashtst/TestOtherAPI");
88
89}
90
91/**********************************************************************
92 * Test Functions
93 *********************************************************************/
94
95static void TestBasic(void) {
96 const char one[4] = {0x6F, 0x6E, 0x65, 0}; /* "one" */
97 const char one2[4] = {0x6F, 0x6E, 0x65, 0}; /* Get around compiler optimizations */
98 const char two[4] = {0x74, 0x77, 0x6F, 0}; /* "two" */
99 const char three[6] = {0x74, 0x68, 0x72, 0x65, 0x65, 0}; /* "three" */
100 const char omega[6] = {0x6F, 0x6D, 0x65, 0x67, 0x61, 0}; /* "omega" */
101 UErrorCode status = U_ZERO_ERROR;
102 UHashtable *hash;
103
104 hash = uhash_open(hashChars, isEqualChars, &status);
105 if (U_FAILURE(status)) {
106 log_err("FAIL: uhash_open failed with %s and returned 0x%08x\n",
107 u_errorName(status), hash);
108 return;
109 }
110 if (hash == NULL) {
111 log_err("FAIL: uhash_open returned NULL\n");
112 return;
113 }
114 log_verbose("Ok: uhash_open returned 0x%08X\n", hash);
115
116 _put(hash, one, 1, 0);
117 _put(hash, omega, 24, 0);
118 _put(hash, two, 2, 0);
119 _put(hash, three, 3, 0);
120 _put(hash, one, -1, 1);
121 _put(hash, two, -2, 2);
122 _put(hash, omega, 48, 24);
123 _put(hash, one, 100, -1);
124 _get(hash, three, 3);
125 _remove(hash, two, -2);
126 _get(hash, two, 0);
127 _get(hash, one, 100);
128 _put(hash, two, 200, 0);
129 _get(hash, omega, 48);
130 _get(hash, two, 200);
131
132 if(_compareChars((void*)one, (void*)three) == TRUE ||
133 _compareChars((void*)one, (void*)one2) != TRUE ||
134 _compareChars((void*)one, (void*)one) != TRUE ||
135 _compareChars((void*)one, NULL) == TRUE ) {
136 log_err("FAIL: compareChars failed\n");
137 }
138 if(_compareIChars((void*)one, (void*)three) == TRUE ||
139 _compareIChars((void*)one, (void*)one) != TRUE ||
140 _compareIChars((void*)one, (void*)one2) != TRUE ||
141 _compareIChars((void*)one, NULL) == TRUE ) {
142 log_err("FAIL: compareIChars failed\n");
143 }
144
145 uhash_close(hash);
146
147}
148
149static void TestOtherAPI(void){
150
151 UErrorCode status = U_ZERO_ERROR;
152 UHashtable *hash;
153
154 /* Use the correct type when cast to void * */
155 const UChar one[4] = {0x006F, 0x006E, 0x0065, 0}; /* L"one" */
156 const UChar one2[4] = {0x006F, 0x006E, 0x0065, 0}; /* Get around compiler optimizations */
157 const UChar two[4] = {0x0074, 0x0077, 0x006F, 0}; /* L"two" */
158 const UChar two2[4] = {0x0074, 0x0077, 0x006F, 0}; /* L"two" */
159 const UChar three[6] = {0x0074, 0x0068, 0x0072, 0x0065, 0x0065, 0}; /* L"three" */
160 const UChar four[6] = {0x0066, 0x006F, 0x0075, 0x0072, 0}; /* L"four" */
161 const UChar five[6] = {0x0066, 0x0069, 0x0076, 0x0065, 0}; /* L"five" */
162 const UChar five2[6] = {0x0066, 0x0069, 0x0076, 0x0065, 0}; /* L"five" */
163
164 hash = uhash_open(uhash_hashUChars, uhash_compareUChars, &status);
165 if (U_FAILURE(status)) {
166 log_err("FAIL: uhash_open failed with %s and returned 0x%08x\n",
167 u_errorName(status), hash);
168 return;
169 }
170 if (hash == NULL) {
171 log_err("FAIL: uhash_open returned NULL\n");
172 return;
173 }
174 log_verbose("Ok: uhash_open returned 0x%08X\n", hash);
175
176 uhash_put(hash, (void*)one, (void*)1, &status);
177 if(uhash_count(hash) != 1){
178 log_err("FAIL: uhas_count() failed. Expected: 1, Got: %d\n", uhash_count(hash));
179 }
180 uhash_put(hash, (void*)two, (void*)2, &status);
181 uhash_put(hash, (void*)three, (void*)3, &status);
182 uhash_put(hash, (void*)four, (void*)4, &status);
183 uhash_put(hash, (void*)five, (void*)5, &status);
184
185 if(uhash_count(hash) != 5){
186 log_err("FAIL: uhas_count() failed. Expected: 5, Got: %d\n", uhash_count(hash));
187 }
188
189 if((int32_t)uhash_get(hash, (void*)two2) != 2){
190 log_err("FAIL: uhash_get failed\n");
191 }
192
193 if((int32_t)uhash_remove(hash, (void*)five2) != 5){
194 log_err("FAIL: uhash_remove() failed\n");
195 }
196 if(uhash_count(hash) != 4){
197 log_err("FAIL: uhas_count() failed. Expected: 4, Got: %d\n", uhash_count(hash));
198 }
199
200 uhash_put(hash, (void*)one, NULL, &status);
201 if(uhash_count(hash) != 3){
202 log_err("FAIL: uhash_put() with value=NULL didn't remove the key value pair\n");
203 }
204 status=U_ILLEGAL_ARGUMENT_ERROR;
205 uhash_put(hash, (void*)one, (void*)1, &status);
206 if(uhash_count(hash) != 3){
207 log_err("FAIL: uhash_put() with value!=NULL should fail when status != U_ZERO_ERROR \n");
208 }
209
210 status=U_ZERO_ERROR;
211 uhash_put(hash, (void*)one, (void*)1, &status);
212 if(uhash_count(hash) != 4){
213 log_err("FAIL: uhash_put() with value!=NULL didn't replace the key value pair\n");
214 }
215
216 if(_compareUChars((void*)one, (void*)two) == TRUE ||
217 _compareUChars((void*)one, (void*)one) != TRUE ||
218 _compareUChars((void*)one, (void*)one2) != TRUE ||
219 _compareUChars((void*)one, NULL) == TRUE ) {
220 log_err("FAIL: compareUChars failed\n");
221 }
222
223 uhash_removeAll(hash);
224 if(uhash_count(hash) != 0){
225 log_err("FAIL: uhas_count() failed. Expected: 0, Got: %d\n", uhash_count(hash));
226 }
227
228 uhash_setKeyComparator(hash, uhash_compareLong);
229 uhash_setKeyHasher(hash, uhash_hashLong);
230 uhash_iput(hash, 1001, (void*)1, &status);
231 uhash_iput(hash, 1002, (void*)2, &status);
232 uhash_iput(hash, 1003, (void*)3, &status);
233 if(_compareLong(1001, 1002) == TRUE ||
234 _compareLong(1001, 1001) != TRUE ||
235 _compareLong(1001, 0) == TRUE ) {
236 log_err("FAIL: compareLong failed\n");
237 }
238 /*set the resize policy to just GROW and SHRINK*/
239 /*how to test this??*/
240 uhash_setResizePolicy(hash, U_GROW_AND_SHRINK);
241 uhash_iput(hash, 1004, (void*)4, &status);
242 uhash_iput(hash, 1005, (void*)5, &status);
243 uhash_iput(hash, 1006, (void*)6, &status);
244 if(uhash_count(hash) != 6){
245 log_err("FAIL: uhash_count() failed. Expected: 6, Got: %d\n", uhash_count(hash));
246 }
247 if((int32_t)uhash_iremove(hash, 1004) != 4){
248 log_err("FAIL: uhash_remove failed\n");
249 }
250 if((int32_t)uhash_iremove(hash, 1004) != 0){
251 log_err("FAIL: uhash_remove failed\n");
252 }
253 uhash_close(hash);
254
255}
256/**********************************************************************
257 * uhash Callbacks
258 *********************************************************************/
259
260/**
261 * This hash function is designed to collide a lot to test key equality
262 * resolution. It only uses the first char.
263 */
264static int32_t U_EXPORT2 U_CALLCONV hashChars(const UHashTok key) {
265 return *(const char*) key.pointer;
266}
267
268static UBool U_EXPORT2 U_CALLCONV isEqualChars(const UHashTok key1, const UHashTok key2) {
269 return (UBool)((key1.pointer != NULL) &&
270 (key2.pointer != NULL) &&
271 (uprv_strcmp((const char*)key1.pointer, (const char*)key2.pointer) == 0));
272}
273
274/**********************************************************************
275 * Wrapper Functions
276 *********************************************************************/
277
278static void _put(UHashtable* hash,
279 const char* key,
280 int32_t value,
281 int32_t expectedOldValue) {
282 UErrorCode status = U_ZERO_ERROR;
283 int32_t oldValue = (int32_t)
284 uhash_put(hash, (void*) key, (void*) value, &status);
285 if (U_FAILURE(status)) {
286 log_err("FAIL: uhash_put(%s) failed with %s and returned %ld\n",
287 key, u_errorName(status), oldValue);
288 } else if (oldValue != expectedOldValue) {
289 log_err("FAIL: uhash_put(%s) returned old value %ld; expected %ld\n",
290 key, oldValue, expectedOldValue);
291 } else {
292 log_verbose("Ok: uhash_put(%s, %d) returned old value %ld\n",
293 key, value, oldValue);
294 }
295}
296
297static void _get(UHashtable* hash,
298 const char* key,
299 int32_t expectedValue) {
300 UErrorCode status = U_ZERO_ERROR;
301 int32_t value = (int32_t) uhash_get(hash, key);
302 if (U_FAILURE(status)) {
303 log_err("FAIL: uhash_get(%s) failed with %s and returned %ld\n",
304 key, u_errorName(status), value);
305 } else if (value != expectedValue) {
306 log_err("FAIL: uhash_get(%s) returned %ld; expected %ld\n",
307 key, value, expectedValue);
308 } else {
309 log_verbose("Ok: uhash_get(%s) returned value %ld\n",
310 key, value);
311 }
312}
313
314static void _remove(UHashtable* hash,
315 const char* key,
316 int32_t expectedValue) {
317 int32_t value = (int32_t) uhash_remove(hash, key);
318 if (value != expectedValue) {
319 log_err("FAIL: uhash_remove(%s) returned %ld; expected %ld\n",
320 key, value, expectedValue);
321 } else {
322 log_verbose("Ok: uhash_remove(%s) returned old value %ld\n",
323 key, value);
324 }
325}