1*0e209d39SAndroid Build Coastguard Worker // © 2016 and later: Unicode, Inc. and others.
2*0e209d39SAndroid Build Coastguard Worker // License & terms of use: http://www.unicode.org/copyright.html
3*0e209d39SAndroid Build Coastguard Worker /*
4*0e209d39SAndroid Build Coastguard Worker *******************************************************************************
5*0e209d39SAndroid Build Coastguard Worker * Copyright (C) 2000-2009, International Business Machines
6*0e209d39SAndroid Build Coastguard Worker * Corporation and others. All Rights Reserved.
7*0e209d39SAndroid Build Coastguard Worker *******************************************************************************
8*0e209d39SAndroid Build Coastguard Worker * Date Name Description
9*0e209d39SAndroid Build Coastguard Worker * 03/22/00 aliu Creation.
10*0e209d39SAndroid Build Coastguard Worker * 07/13/00 Madhu Added more tests
11*0e209d39SAndroid Build Coastguard Worker *******************************************************************************
12*0e209d39SAndroid Build Coastguard Worker */
13*0e209d39SAndroid Build Coastguard Worker
14*0e209d39SAndroid Build Coastguard Worker #include <stdbool.h>
15*0e209d39SAndroid Build Coastguard Worker #include "cintltst.h"
16*0e209d39SAndroid Build Coastguard Worker #include "uhash.h"
17*0e209d39SAndroid Build Coastguard Worker #include "unicode/ctest.h"
18*0e209d39SAndroid Build Coastguard Worker #include "unicode/ustring.h"
19*0e209d39SAndroid Build Coastguard Worker #include "cstring.h"
20*0e209d39SAndroid Build Coastguard Worker
21*0e209d39SAndroid Build Coastguard Worker /**********************************************************************
22*0e209d39SAndroid Build Coastguard Worker * Prototypes
23*0e209d39SAndroid Build Coastguard Worker *********************************************************************/
24*0e209d39SAndroid Build Coastguard Worker
25*0e209d39SAndroid Build Coastguard Worker static void TestBasic(void);
26*0e209d39SAndroid Build Coastguard Worker static void TestAllowZero(void);
27*0e209d39SAndroid Build Coastguard Worker static void TestOtherAPI(void);
28*0e209d39SAndroid Build Coastguard Worker static void hashIChars(void);
29*0e209d39SAndroid Build Coastguard Worker
30*0e209d39SAndroid Build Coastguard Worker static int32_t U_EXPORT2 U_CALLCONV hashChars(const UHashTok key);
31*0e209d39SAndroid Build Coastguard Worker
32*0e209d39SAndroid Build Coastguard Worker static UBool U_EXPORT2 U_CALLCONV isEqualChars(const UHashTok key1, const UHashTok key2);
33*0e209d39SAndroid Build Coastguard Worker
34*0e209d39SAndroid Build Coastguard Worker static void _put(UHashtable* hash,
35*0e209d39SAndroid Build Coastguard Worker const char* key,
36*0e209d39SAndroid Build Coastguard Worker int32_t value,
37*0e209d39SAndroid Build Coastguard Worker int32_t expectedOldValue);
38*0e209d39SAndroid Build Coastguard Worker
39*0e209d39SAndroid Build Coastguard Worker static void _get(UHashtable* hash,
40*0e209d39SAndroid Build Coastguard Worker const char* key,
41*0e209d39SAndroid Build Coastguard Worker int32_t expectedValue);
42*0e209d39SAndroid Build Coastguard Worker
43*0e209d39SAndroid Build Coastguard Worker static void _remove(UHashtable* hash,
44*0e209d39SAndroid Build Coastguard Worker const char* key,
45*0e209d39SAndroid Build Coastguard Worker int32_t expectedValue);
46*0e209d39SAndroid Build Coastguard Worker
47*0e209d39SAndroid Build Coastguard Worker void addHashtableTest(TestNode** root);
48*0e209d39SAndroid Build Coastguard Worker
49*0e209d39SAndroid Build Coastguard Worker /**********************************************************************
50*0e209d39SAndroid Build Coastguard Worker * UHashTok wrapper functions
51*0e209d39SAndroid Build Coastguard Worker *********************************************************************/
52*0e209d39SAndroid Build Coastguard Worker
53*0e209d39SAndroid Build Coastguard Worker static UBool
_compareChars(const void * a,const void * b)54*0e209d39SAndroid Build Coastguard Worker _compareChars(const void* a, const void* b) {
55*0e209d39SAndroid Build Coastguard Worker UHashTok s, t;
56*0e209d39SAndroid Build Coastguard Worker s.pointer = (void *)a;
57*0e209d39SAndroid Build Coastguard Worker t.pointer = (void *)b;
58*0e209d39SAndroid Build Coastguard Worker return uhash_compareChars(s, t);
59*0e209d39SAndroid Build Coastguard Worker }
60*0e209d39SAndroid Build Coastguard Worker
61*0e209d39SAndroid Build Coastguard Worker static UBool
_compareIChars(const void * a,const void * b)62*0e209d39SAndroid Build Coastguard Worker _compareIChars(const void* a, const void* b) {
63*0e209d39SAndroid Build Coastguard Worker UHashTok s, t;
64*0e209d39SAndroid Build Coastguard Worker s.pointer = (void *)a;
65*0e209d39SAndroid Build Coastguard Worker t.pointer = (void *)b;
66*0e209d39SAndroid Build Coastguard Worker return uhash_compareIChars(s, t);
67*0e209d39SAndroid Build Coastguard Worker }
68*0e209d39SAndroid Build Coastguard Worker
69*0e209d39SAndroid Build Coastguard Worker static UBool
_compareUChars(const void * a,const void * b)70*0e209d39SAndroid Build Coastguard Worker _compareUChars(const void* a, const void* b) {
71*0e209d39SAndroid Build Coastguard Worker UHashTok s, t;
72*0e209d39SAndroid Build Coastguard Worker s.pointer = (void *)a;
73*0e209d39SAndroid Build Coastguard Worker t.pointer = (void *)b;
74*0e209d39SAndroid Build Coastguard Worker return uhash_compareUChars(s, t);
75*0e209d39SAndroid Build Coastguard Worker }
76*0e209d39SAndroid Build Coastguard Worker
77*0e209d39SAndroid Build Coastguard Worker static UBool
_compareLong(int32_t a,int32_t b)78*0e209d39SAndroid Build Coastguard Worker _compareLong(int32_t a, int32_t b) {
79*0e209d39SAndroid Build Coastguard Worker UHashTok s, t;
80*0e209d39SAndroid Build Coastguard Worker s.integer = a;
81*0e209d39SAndroid Build Coastguard Worker t.integer = b;
82*0e209d39SAndroid Build Coastguard Worker return uhash_compareLong(s, t);
83*0e209d39SAndroid Build Coastguard Worker }
84*0e209d39SAndroid Build Coastguard Worker
85*0e209d39SAndroid Build Coastguard Worker /**********************************************************************
86*0e209d39SAndroid Build Coastguard Worker * FW Registration
87*0e209d39SAndroid Build Coastguard Worker *********************************************************************/
88*0e209d39SAndroid Build Coastguard Worker
addHashtableTest(TestNode ** root)89*0e209d39SAndroid Build Coastguard Worker void addHashtableTest(TestNode** root) {
90*0e209d39SAndroid Build Coastguard Worker
91*0e209d39SAndroid Build Coastguard Worker addTest(root, &TestBasic, "tsutil/chashtst/TestBasic");
92*0e209d39SAndroid Build Coastguard Worker addTest(root, &TestAllowZero, "tsutil/chashtst/TestAllowZero");
93*0e209d39SAndroid Build Coastguard Worker addTest(root, &TestOtherAPI, "tsutil/chashtst/TestOtherAPI");
94*0e209d39SAndroid Build Coastguard Worker addTest(root, &hashIChars, "tsutil/chashtst/hashIChars");
95*0e209d39SAndroid Build Coastguard Worker
96*0e209d39SAndroid Build Coastguard Worker }
97*0e209d39SAndroid Build Coastguard Worker
98*0e209d39SAndroid Build Coastguard Worker /**********************************************************************
99*0e209d39SAndroid Build Coastguard Worker * Test Functions
100*0e209d39SAndroid Build Coastguard Worker *********************************************************************/
101*0e209d39SAndroid Build Coastguard Worker
TestBasic(void)102*0e209d39SAndroid Build Coastguard Worker static void TestBasic(void) {
103*0e209d39SAndroid Build Coastguard Worker static const char one[4] = {0x6F, 0x6E, 0x65, 0}; /* "one" */
104*0e209d39SAndroid Build Coastguard Worker static const char one2[4] = {0x6F, 0x6E, 0x65, 0}; /* Get around compiler optimizations */
105*0e209d39SAndroid Build Coastguard Worker static const char two[4] = {0x74, 0x77, 0x6F, 0}; /* "two" */
106*0e209d39SAndroid Build Coastguard Worker static const char three[6] = {0x74, 0x68, 0x72, 0x65, 0x65, 0}; /* "three" */
107*0e209d39SAndroid Build Coastguard Worker static const char omega[6] = {0x6F, 0x6D, 0x65, 0x67, 0x61, 0}; /* "omega" */
108*0e209d39SAndroid Build Coastguard Worker UErrorCode status = U_ZERO_ERROR;
109*0e209d39SAndroid Build Coastguard Worker UHashtable *hash;
110*0e209d39SAndroid Build Coastguard Worker
111*0e209d39SAndroid Build Coastguard Worker hash = uhash_open(hashChars, isEqualChars, NULL, &status);
112*0e209d39SAndroid Build Coastguard Worker if (U_FAILURE(status)) {
113*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhash_open failed with %s and returned 0x%08x\n",
114*0e209d39SAndroid Build Coastguard Worker u_errorName(status), hash);
115*0e209d39SAndroid Build Coastguard Worker return;
116*0e209d39SAndroid Build Coastguard Worker }
117*0e209d39SAndroid Build Coastguard Worker if (hash == NULL) {
118*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhash_open returned NULL\n");
119*0e209d39SAndroid Build Coastguard Worker return;
120*0e209d39SAndroid Build Coastguard Worker }
121*0e209d39SAndroid Build Coastguard Worker log_verbose("Ok: uhash_open returned 0x%08X\n", hash);
122*0e209d39SAndroid Build Coastguard Worker
123*0e209d39SAndroid Build Coastguard Worker _put(hash, one, 1, 0);
124*0e209d39SAndroid Build Coastguard Worker _put(hash, omega, 24, 0);
125*0e209d39SAndroid Build Coastguard Worker _put(hash, two, 2, 0);
126*0e209d39SAndroid Build Coastguard Worker _put(hash, three, 3, 0);
127*0e209d39SAndroid Build Coastguard Worker _put(hash, one, -1, 1);
128*0e209d39SAndroid Build Coastguard Worker _put(hash, two, -2, 2);
129*0e209d39SAndroid Build Coastguard Worker _put(hash, omega, 48, 24);
130*0e209d39SAndroid Build Coastguard Worker _put(hash, one, 100, -1);
131*0e209d39SAndroid Build Coastguard Worker _get(hash, three, 3);
132*0e209d39SAndroid Build Coastguard Worker _remove(hash, two, -2);
133*0e209d39SAndroid Build Coastguard Worker _get(hash, two, 0);
134*0e209d39SAndroid Build Coastguard Worker _get(hash, one, 100);
135*0e209d39SAndroid Build Coastguard Worker _put(hash, two, 200, 0);
136*0e209d39SAndroid Build Coastguard Worker _get(hash, omega, 48);
137*0e209d39SAndroid Build Coastguard Worker _get(hash, two, 200);
138*0e209d39SAndroid Build Coastguard Worker
139*0e209d39SAndroid Build Coastguard Worker // puti(key, value==0) removes the key's element.
140*0e209d39SAndroid Build Coastguard Worker _put(hash, two, 0, 200);
141*0e209d39SAndroid Build Coastguard Worker
142*0e209d39SAndroid Build Coastguard Worker if(_compareChars((void*)one, (void*)three) == true ||
143*0e209d39SAndroid Build Coastguard Worker _compareChars((void*)one, (void*)one2) != true ||
144*0e209d39SAndroid Build Coastguard Worker _compareChars((void*)one, (void*)one) != true ||
145*0e209d39SAndroid Build Coastguard Worker _compareChars((void*)one, NULL) == true ) {
146*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: compareChars failed\n");
147*0e209d39SAndroid Build Coastguard Worker }
148*0e209d39SAndroid Build Coastguard Worker if(_compareIChars((void*)one, (void*)three) == true ||
149*0e209d39SAndroid Build Coastguard Worker _compareIChars((void*)one, (void*)one) != true ||
150*0e209d39SAndroid Build Coastguard Worker _compareIChars((void*)one, (void*)one2) != true ||
151*0e209d39SAndroid Build Coastguard Worker _compareIChars((void*)one, NULL) == true ) {
152*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: compareIChars failed\n");
153*0e209d39SAndroid Build Coastguard Worker }
154*0e209d39SAndroid Build Coastguard Worker
155*0e209d39SAndroid Build Coastguard Worker uhash_close(hash);
156*0e209d39SAndroid Build Coastguard Worker }
157*0e209d39SAndroid Build Coastguard Worker
TestAllowZero(void)158*0e209d39SAndroid Build Coastguard Worker static void TestAllowZero(void) {
159*0e209d39SAndroid Build Coastguard Worker UErrorCode status = U_ZERO_ERROR;
160*0e209d39SAndroid Build Coastguard Worker UHashtable *hash = uhash_open(hashChars, isEqualChars, NULL, &status);
161*0e209d39SAndroid Build Coastguard Worker if (U_FAILURE(status)) {
162*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhash_open failed with %s and returned 0x%08x\n",
163*0e209d39SAndroid Build Coastguard Worker u_errorName(status), hash);
164*0e209d39SAndroid Build Coastguard Worker return;
165*0e209d39SAndroid Build Coastguard Worker }
166*0e209d39SAndroid Build Coastguard Worker if (hash == NULL) {
167*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhash_open returned NULL\n");
168*0e209d39SAndroid Build Coastguard Worker return;
169*0e209d39SAndroid Build Coastguard Worker }
170*0e209d39SAndroid Build Coastguard Worker log_verbose("Ok: uhash_open returned 0x%08X\n", hash);
171*0e209d39SAndroid Build Coastguard Worker
172*0e209d39SAndroid Build Coastguard Worker int32_t oldValue = uhash_putiAllowZero(hash, (char *)"one", 1, &status);
173*0e209d39SAndroid Build Coastguard Worker UBool found = false;
174*0e209d39SAndroid Build Coastguard Worker if (U_FAILURE(status) || oldValue != 0 || !uhash_containsKey(hash, "one") ||
175*0e209d39SAndroid Build Coastguard Worker uhash_geti(hash, "one") != 1 ||
176*0e209d39SAndroid Build Coastguard Worker uhash_getiAndFound(hash, "one", &found) != 1 || !found) {
177*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhash_putiAllowZero(one, 1)");
178*0e209d39SAndroid Build Coastguard Worker }
179*0e209d39SAndroid Build Coastguard Worker oldValue = uhash_putiAllowZero(hash, (char *)"zero", 0, &status);
180*0e209d39SAndroid Build Coastguard Worker found = false;
181*0e209d39SAndroid Build Coastguard Worker if (U_FAILURE(status) || oldValue != 0 || !uhash_containsKey(hash, "zero") ||
182*0e209d39SAndroid Build Coastguard Worker uhash_geti(hash, "zero") != 0 ||
183*0e209d39SAndroid Build Coastguard Worker uhash_getiAndFound(hash, "zero", &found) != 0 || !found) {
184*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhash_putiAllowZero(zero, 0)");
185*0e209d39SAndroid Build Coastguard Worker }
186*0e209d39SAndroid Build Coastguard Worker // Overwrite "one" to 0.
187*0e209d39SAndroid Build Coastguard Worker oldValue = uhash_putiAllowZero(hash, (char *)"one", 0, &status);
188*0e209d39SAndroid Build Coastguard Worker found = false;
189*0e209d39SAndroid Build Coastguard Worker if (U_FAILURE(status) || oldValue != 1 || !uhash_containsKey(hash, "one") ||
190*0e209d39SAndroid Build Coastguard Worker uhash_geti(hash, "one") != 0 ||
191*0e209d39SAndroid Build Coastguard Worker uhash_getiAndFound(hash, "one", &found) != 0 || !found) {
192*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhash_putiAllowZero(one, 0)");
193*0e209d39SAndroid Build Coastguard Worker }
194*0e209d39SAndroid Build Coastguard Worker // Remove "zero" using puti(zero, 0).
195*0e209d39SAndroid Build Coastguard Worker oldValue = uhash_puti(hash, (char *)"zero", 0, &status);
196*0e209d39SAndroid Build Coastguard Worker found = true;
197*0e209d39SAndroid Build Coastguard Worker if (U_FAILURE(status) || oldValue != 0 || uhash_containsKey(hash, "zero") ||
198*0e209d39SAndroid Build Coastguard Worker uhash_geti(hash, "zero") != 0 ||
199*0e209d39SAndroid Build Coastguard Worker uhash_getiAndFound(hash, "zero", &found) != 0 || found) {
200*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhash_puti(zero, 0)");
201*0e209d39SAndroid Build Coastguard Worker }
202*0e209d39SAndroid Build Coastguard Worker
203*0e209d39SAndroid Build Coastguard Worker uhash_close(hash);
204*0e209d39SAndroid Build Coastguard Worker }
205*0e209d39SAndroid Build Coastguard Worker
TestOtherAPI(void)206*0e209d39SAndroid Build Coastguard Worker static void TestOtherAPI(void){
207*0e209d39SAndroid Build Coastguard Worker
208*0e209d39SAndroid Build Coastguard Worker UErrorCode status = U_ZERO_ERROR;
209*0e209d39SAndroid Build Coastguard Worker UHashtable *hash;
210*0e209d39SAndroid Build Coastguard Worker
211*0e209d39SAndroid Build Coastguard Worker /* Use the correct type when cast to void * */
212*0e209d39SAndroid Build Coastguard Worker static const UChar one[4] = {0x006F, 0x006E, 0x0065, 0}; /* L"one" */
213*0e209d39SAndroid Build Coastguard Worker static const UChar one2[4] = {0x006F, 0x006E, 0x0065, 0}; /* Get around compiler optimizations */
214*0e209d39SAndroid Build Coastguard Worker static const UChar two[4] = {0x0074, 0x0077, 0x006F, 0}; /* L"two" */
215*0e209d39SAndroid Build Coastguard Worker static const UChar two2[4] = {0x0074, 0x0077, 0x006F, 0}; /* L"two" */
216*0e209d39SAndroid Build Coastguard Worker static const UChar three[6] = {0x0074, 0x0068, 0x0072, 0x0065, 0x0065, 0}; /* L"three" */
217*0e209d39SAndroid Build Coastguard Worker static const UChar four[6] = {0x0066, 0x006F, 0x0075, 0x0072, 0}; /* L"four" */
218*0e209d39SAndroid Build Coastguard Worker static const UChar five[6] = {0x0066, 0x0069, 0x0076, 0x0065, 0}; /* L"five" */
219*0e209d39SAndroid Build Coastguard Worker static const UChar five2[6] = {0x0066, 0x0069, 0x0076, 0x0065, 0}; /* L"five" */
220*0e209d39SAndroid Build Coastguard Worker
221*0e209d39SAndroid Build Coastguard Worker hash = uhash_open(uhash_hashUChars, uhash_compareUChars, NULL, &status);
222*0e209d39SAndroid Build Coastguard Worker if (U_FAILURE(status)) {
223*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhash_open failed with %s and returned 0x%08x\n",
224*0e209d39SAndroid Build Coastguard Worker u_errorName(status), hash);
225*0e209d39SAndroid Build Coastguard Worker return;
226*0e209d39SAndroid Build Coastguard Worker }
227*0e209d39SAndroid Build Coastguard Worker if (hash == NULL) {
228*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhash_open returned NULL\n");
229*0e209d39SAndroid Build Coastguard Worker return;
230*0e209d39SAndroid Build Coastguard Worker }
231*0e209d39SAndroid Build Coastguard Worker log_verbose("Ok: uhash_open returned 0x%08X\n", hash);
232*0e209d39SAndroid Build Coastguard Worker
233*0e209d39SAndroid Build Coastguard Worker uhash_puti(hash, (void*)one, 1, &status);
234*0e209d39SAndroid Build Coastguard Worker if(uhash_count(hash) != 1){
235*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhas_count() failed. Expected: 1, Got: %d\n", uhash_count(hash));
236*0e209d39SAndroid Build Coastguard Worker }
237*0e209d39SAndroid Build Coastguard Worker if(uhash_find(hash, (void*)two) != NULL){
238*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhash_find failed\n");
239*0e209d39SAndroid Build Coastguard Worker }
240*0e209d39SAndroid Build Coastguard Worker uhash_puti(hash, (void*)two, 2, &status);
241*0e209d39SAndroid Build Coastguard Worker uhash_puti(hash, (void*)three, 3, &status);
242*0e209d39SAndroid Build Coastguard Worker uhash_puti(hash, (void*)four, 4, &status);
243*0e209d39SAndroid Build Coastguard Worker uhash_puti(hash, (void*)five, 5, &status);
244*0e209d39SAndroid Build Coastguard Worker
245*0e209d39SAndroid Build Coastguard Worker if(uhash_count(hash) != 5){
246*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhas_count() failed. Expected: 5, Got: %d\n", uhash_count(hash));
247*0e209d39SAndroid Build Coastguard Worker }
248*0e209d39SAndroid Build Coastguard Worker
249*0e209d39SAndroid Build Coastguard Worker if(uhash_geti(hash, (void*)two2) != 2){
250*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhash_geti failed\n");
251*0e209d39SAndroid Build Coastguard Worker }
252*0e209d39SAndroid Build Coastguard Worker
253*0e209d39SAndroid Build Coastguard Worker if(uhash_find(hash, (void*)two2) == NULL){
254*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhash_find of \"two\" failed\n");
255*0e209d39SAndroid Build Coastguard Worker }
256*0e209d39SAndroid Build Coastguard Worker
257*0e209d39SAndroid Build Coastguard Worker if(uhash_removei(hash, (void*)five2) != 5){
258*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhash_remove() failed\n");
259*0e209d39SAndroid Build Coastguard Worker }
260*0e209d39SAndroid Build Coastguard Worker if(uhash_count(hash) != 4){
261*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhas_count() failed. Expected: 4, Got: %d\n", uhash_count(hash));
262*0e209d39SAndroid Build Coastguard Worker }
263*0e209d39SAndroid Build Coastguard Worker
264*0e209d39SAndroid Build Coastguard Worker uhash_put(hash, (void*)one, NULL, &status);
265*0e209d39SAndroid Build Coastguard Worker if(uhash_count(hash) != 3){
266*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhash_put() with value=NULL didn't remove the key value pair\n");
267*0e209d39SAndroid Build Coastguard Worker }
268*0e209d39SAndroid Build Coastguard Worker status=U_ILLEGAL_ARGUMENT_ERROR;
269*0e209d39SAndroid Build Coastguard Worker uhash_puti(hash, (void*)one, 1, &status);
270*0e209d39SAndroid Build Coastguard Worker if(uhash_count(hash) != 3){
271*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhash_put() with value!=NULL should fail when status != U_ZERO_ERROR \n");
272*0e209d39SAndroid Build Coastguard Worker }
273*0e209d39SAndroid Build Coastguard Worker
274*0e209d39SAndroid Build Coastguard Worker status=U_ZERO_ERROR;
275*0e209d39SAndroid Build Coastguard Worker uhash_puti(hash, (void*)one, 1, &status);
276*0e209d39SAndroid Build Coastguard Worker if(uhash_count(hash) != 4){
277*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhash_put() with value!=NULL didn't replace the key value pair\n");
278*0e209d39SAndroid Build Coastguard Worker }
279*0e209d39SAndroid Build Coastguard Worker
280*0e209d39SAndroid Build Coastguard Worker if(_compareUChars((void*)one, (void*)two) == true ||
281*0e209d39SAndroid Build Coastguard Worker _compareUChars((void*)one, (void*)one) != true ||
282*0e209d39SAndroid Build Coastguard Worker _compareUChars((void*)one, (void*)one2) != true ||
283*0e209d39SAndroid Build Coastguard Worker _compareUChars((void*)one, NULL) == true ) {
284*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: compareUChars failed\n");
285*0e209d39SAndroid Build Coastguard Worker }
286*0e209d39SAndroid Build Coastguard Worker
287*0e209d39SAndroid Build Coastguard Worker uhash_removeAll(hash);
288*0e209d39SAndroid Build Coastguard Worker if(uhash_count(hash) != 0){
289*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhas_count() failed. Expected: 0, Got: %d\n", uhash_count(hash));
290*0e209d39SAndroid Build Coastguard Worker }
291*0e209d39SAndroid Build Coastguard Worker
292*0e209d39SAndroid Build Coastguard Worker uhash_setKeyComparator(hash, uhash_compareLong);
293*0e209d39SAndroid Build Coastguard Worker uhash_setKeyHasher(hash, uhash_hashLong);
294*0e209d39SAndroid Build Coastguard Worker uhash_iputi(hash, 1001, 1, &status);
295*0e209d39SAndroid Build Coastguard Worker uhash_iputi(hash, 1002, 2, &status);
296*0e209d39SAndroid Build Coastguard Worker uhash_iputi(hash, 1003, 3, &status);
297*0e209d39SAndroid Build Coastguard Worker if(_compareLong(1001, 1002) == true ||
298*0e209d39SAndroid Build Coastguard Worker _compareLong(1001, 1001) != true ||
299*0e209d39SAndroid Build Coastguard Worker _compareLong(1001, 0) == true ) {
300*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: compareLong failed\n");
301*0e209d39SAndroid Build Coastguard Worker }
302*0e209d39SAndroid Build Coastguard Worker /*set the resize policy to just GROW and SHRINK*/
303*0e209d39SAndroid Build Coastguard Worker /*how to test this??*/
304*0e209d39SAndroid Build Coastguard Worker uhash_setResizePolicy(hash, U_GROW_AND_SHRINK);
305*0e209d39SAndroid Build Coastguard Worker uhash_iputi(hash, 1004, 4, &status);
306*0e209d39SAndroid Build Coastguard Worker uhash_iputi(hash, 1005, 5, &status);
307*0e209d39SAndroid Build Coastguard Worker uhash_iputi(hash, 1006, 6, &status);
308*0e209d39SAndroid Build Coastguard Worker if(uhash_count(hash) != 6){
309*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhash_count() failed. Expected: 6, Got: %d\n", uhash_count(hash));
310*0e209d39SAndroid Build Coastguard Worker }
311*0e209d39SAndroid Build Coastguard Worker if(uhash_iremovei(hash, 1004) != 4){
312*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhash_remove failed\n");
313*0e209d39SAndroid Build Coastguard Worker }
314*0e209d39SAndroid Build Coastguard Worker if(uhash_iremovei(hash, 1004) != 0){
315*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhash_remove failed\n");
316*0e209d39SAndroid Build Coastguard Worker }
317*0e209d39SAndroid Build Coastguard Worker
318*0e209d39SAndroid Build Coastguard Worker uhash_removeAll(hash);
319*0e209d39SAndroid Build Coastguard Worker uhash_iput(hash, 2004, (void*)one, &status);
320*0e209d39SAndroid Build Coastguard Worker uhash_iput(hash, 2005, (void*)two, &status);
321*0e209d39SAndroid Build Coastguard Worker if(uhash_count(hash) != 2){
322*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhash_count() failed. Expected: 2, Got: %d\n", uhash_count(hash));
323*0e209d39SAndroid Build Coastguard Worker }
324*0e209d39SAndroid Build Coastguard Worker if(uhash_iremove(hash, 2004) != (void*)one){
325*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhash_remove failed\n");
326*0e209d39SAndroid Build Coastguard Worker }
327*0e209d39SAndroid Build Coastguard Worker if(uhash_iremove(hash, 2004) != NULL){
328*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhash_remove failed\n");
329*0e209d39SAndroid Build Coastguard Worker }
330*0e209d39SAndroid Build Coastguard Worker if(uhash_count(hash) != 1){
331*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhash_count() failed. Expected: 1, Got: %d\n", uhash_count(hash));
332*0e209d39SAndroid Build Coastguard Worker }
333*0e209d39SAndroid Build Coastguard Worker
334*0e209d39SAndroid Build Coastguard Worker uhash_close(hash);
335*0e209d39SAndroid Build Coastguard Worker
336*0e209d39SAndroid Build Coastguard Worker }
337*0e209d39SAndroid Build Coastguard Worker
hashIChars(void)338*0e209d39SAndroid Build Coastguard Worker static void hashIChars(void) {
339*0e209d39SAndroid Build Coastguard Worker static const char which[] = "which";
340*0e209d39SAndroid Build Coastguard Worker static const char WHICH2[] = "WHICH";
341*0e209d39SAndroid Build Coastguard Worker static const char where[] = "where";
342*0e209d39SAndroid Build Coastguard Worker UErrorCode status = U_ZERO_ERROR;
343*0e209d39SAndroid Build Coastguard Worker UHashtable *hash;
344*0e209d39SAndroid Build Coastguard Worker
345*0e209d39SAndroid Build Coastguard Worker hash = uhash_open(uhash_hashIChars, uhash_compareIChars, NULL, &status);
346*0e209d39SAndroid Build Coastguard Worker if (U_FAILURE(status)) {
347*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhash_open failed with %s and returned 0x%08x\n",
348*0e209d39SAndroid Build Coastguard Worker u_errorName(status), hash);
349*0e209d39SAndroid Build Coastguard Worker return;
350*0e209d39SAndroid Build Coastguard Worker }
351*0e209d39SAndroid Build Coastguard Worker if (hash == NULL) {
352*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhash_open returned NULL\n");
353*0e209d39SAndroid Build Coastguard Worker return;
354*0e209d39SAndroid Build Coastguard Worker }
355*0e209d39SAndroid Build Coastguard Worker log_verbose("Ok: uhash_open returned 0x%08X\n", hash);
356*0e209d39SAndroid Build Coastguard Worker
357*0e209d39SAndroid Build Coastguard Worker _put(hash, which, 1, 0);
358*0e209d39SAndroid Build Coastguard Worker _put(hash, WHICH2, 2, 1);
359*0e209d39SAndroid Build Coastguard Worker _put(hash, where, 3, 0);
360*0e209d39SAndroid Build Coastguard Worker if(uhash_count(hash) != 2){
361*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhas_count() failed. Expected: 1, Got: %d\n", uhash_count(hash));
362*0e209d39SAndroid Build Coastguard Worker }
363*0e209d39SAndroid Build Coastguard Worker _remove(hash, which, 2);
364*0e209d39SAndroid Build Coastguard Worker
365*0e209d39SAndroid Build Coastguard Worker uhash_close(hash);
366*0e209d39SAndroid Build Coastguard Worker }
367*0e209d39SAndroid Build Coastguard Worker
368*0e209d39SAndroid Build Coastguard Worker
369*0e209d39SAndroid Build Coastguard Worker /**********************************************************************
370*0e209d39SAndroid Build Coastguard Worker * uhash Callbacks
371*0e209d39SAndroid Build Coastguard Worker *********************************************************************/
372*0e209d39SAndroid Build Coastguard Worker
373*0e209d39SAndroid Build Coastguard Worker /**
374*0e209d39SAndroid Build Coastguard Worker * This hash function is designed to collide a lot to test key equality
375*0e209d39SAndroid Build Coastguard Worker * resolution. It only uses the first char.
376*0e209d39SAndroid Build Coastguard Worker */
hashChars(const UHashTok key)377*0e209d39SAndroid Build Coastguard Worker static int32_t U_EXPORT2 U_CALLCONV hashChars(const UHashTok key) {
378*0e209d39SAndroid Build Coastguard Worker return *(const char*) key.pointer;
379*0e209d39SAndroid Build Coastguard Worker }
380*0e209d39SAndroid Build Coastguard Worker
isEqualChars(const UHashTok key1,const UHashTok key2)381*0e209d39SAndroid Build Coastguard Worker static UBool U_EXPORT2 U_CALLCONV isEqualChars(const UHashTok key1, const UHashTok key2) {
382*0e209d39SAndroid Build Coastguard Worker return (UBool)((key1.pointer != NULL) &&
383*0e209d39SAndroid Build Coastguard Worker (key2.pointer != NULL) &&
384*0e209d39SAndroid Build Coastguard Worker (uprv_strcmp((const char*)key1.pointer, (const char*)key2.pointer) == 0));
385*0e209d39SAndroid Build Coastguard Worker }
386*0e209d39SAndroid Build Coastguard Worker
387*0e209d39SAndroid Build Coastguard Worker /**********************************************************************
388*0e209d39SAndroid Build Coastguard Worker * Wrapper Functions
389*0e209d39SAndroid Build Coastguard Worker *********************************************************************/
390*0e209d39SAndroid Build Coastguard Worker
_put(UHashtable * hash,const char * key,int32_t value,int32_t expectedOldValue)391*0e209d39SAndroid Build Coastguard Worker static void _put(UHashtable* hash,
392*0e209d39SAndroid Build Coastguard Worker const char* key,
393*0e209d39SAndroid Build Coastguard Worker int32_t value,
394*0e209d39SAndroid Build Coastguard Worker int32_t expectedOldValue) {
395*0e209d39SAndroid Build Coastguard Worker UErrorCode status = U_ZERO_ERROR;
396*0e209d39SAndroid Build Coastguard Worker int32_t oldValue =
397*0e209d39SAndroid Build Coastguard Worker uhash_puti(hash, (void*) key, value, &status);
398*0e209d39SAndroid Build Coastguard Worker if (U_FAILURE(status)) {
399*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhash_puti(%s) failed with %s and returned %ld\n",
400*0e209d39SAndroid Build Coastguard Worker key, u_errorName(status), oldValue);
401*0e209d39SAndroid Build Coastguard Worker } else if (oldValue != expectedOldValue) {
402*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhash_puti(%s) returned old value %ld; expected %ld\n",
403*0e209d39SAndroid Build Coastguard Worker key, oldValue, expectedOldValue);
404*0e209d39SAndroid Build Coastguard Worker } else {
405*0e209d39SAndroid Build Coastguard Worker log_verbose("Ok: uhash_puti(%s, %d) returned old value %ld\n",
406*0e209d39SAndroid Build Coastguard Worker key, value, oldValue);
407*0e209d39SAndroid Build Coastguard Worker }
408*0e209d39SAndroid Build Coastguard Worker int32_t newValue = uhash_geti(hash, key);
409*0e209d39SAndroid Build Coastguard Worker if (newValue != value) {
410*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhash_puti(%s) failed to set the intended value %ld: "
411*0e209d39SAndroid Build Coastguard Worker "uhash_geti() returns %ld\n",
412*0e209d39SAndroid Build Coastguard Worker key, value, newValue);
413*0e209d39SAndroid Build Coastguard Worker }
414*0e209d39SAndroid Build Coastguard Worker UBool contained = uhash_containsKey(hash, key);
415*0e209d39SAndroid Build Coastguard Worker if (value == 0) {
416*0e209d39SAndroid Build Coastguard Worker if (contained) {
417*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhash_puti(%s, zero) failed to remove the key item: "
418*0e209d39SAndroid Build Coastguard Worker "uhash_containsKey() returns true\n",
419*0e209d39SAndroid Build Coastguard Worker key);
420*0e209d39SAndroid Build Coastguard Worker }
421*0e209d39SAndroid Build Coastguard Worker } else {
422*0e209d39SAndroid Build Coastguard Worker if (!contained) {
423*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhash_puti(%s, not zero) appears to have removed the key item: "
424*0e209d39SAndroid Build Coastguard Worker "uhash_containsKey() returns false\n",
425*0e209d39SAndroid Build Coastguard Worker key);
426*0e209d39SAndroid Build Coastguard Worker }
427*0e209d39SAndroid Build Coastguard Worker }
428*0e209d39SAndroid Build Coastguard Worker }
429*0e209d39SAndroid Build Coastguard Worker
_get(UHashtable * hash,const char * key,int32_t expectedValue)430*0e209d39SAndroid Build Coastguard Worker static void _get(UHashtable* hash,
431*0e209d39SAndroid Build Coastguard Worker const char* key,
432*0e209d39SAndroid Build Coastguard Worker int32_t expectedValue) {
433*0e209d39SAndroid Build Coastguard Worker int32_t value = uhash_geti(hash, key);
434*0e209d39SAndroid Build Coastguard Worker if (value != expectedValue) {
435*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhash_geti(%s) returned %ld; expected %ld\n",
436*0e209d39SAndroid Build Coastguard Worker key, value, expectedValue);
437*0e209d39SAndroid Build Coastguard Worker } else {
438*0e209d39SAndroid Build Coastguard Worker log_verbose("Ok: uhash_geti(%s) returned value %ld\n",
439*0e209d39SAndroid Build Coastguard Worker key, value);
440*0e209d39SAndroid Build Coastguard Worker }
441*0e209d39SAndroid Build Coastguard Worker }
442*0e209d39SAndroid Build Coastguard Worker
_remove(UHashtable * hash,const char * key,int32_t expectedValue)443*0e209d39SAndroid Build Coastguard Worker static void _remove(UHashtable* hash,
444*0e209d39SAndroid Build Coastguard Worker const char* key,
445*0e209d39SAndroid Build Coastguard Worker int32_t expectedValue) {
446*0e209d39SAndroid Build Coastguard Worker int32_t value = uhash_removei(hash, key);
447*0e209d39SAndroid Build Coastguard Worker if (value != expectedValue) {
448*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhash_removei(%s) returned %ld; expected %ld\n",
449*0e209d39SAndroid Build Coastguard Worker key, value, expectedValue);
450*0e209d39SAndroid Build Coastguard Worker } else {
451*0e209d39SAndroid Build Coastguard Worker log_verbose("Ok: uhash_removei(%s) returned old value %ld\n",
452*0e209d39SAndroid Build Coastguard Worker key, value);
453*0e209d39SAndroid Build Coastguard Worker }
454*0e209d39SAndroid Build Coastguard Worker if (uhash_containsKey(hash, key)) {
455*0e209d39SAndroid Build Coastguard Worker log_err("FAIL: uhash_removei(%s) failed to remove the key item: "
456*0e209d39SAndroid Build Coastguard Worker "uhash_containsKey() returns false\n",
457*0e209d39SAndroid Build Coastguard Worker key);
458*0e209d39SAndroid Build Coastguard Worker }
459*0e209d39SAndroid Build Coastguard Worker }
460