]>
Commit | Line | Data |
---|---|---|
73c04bcf A |
1 | /******************************************************************** |
2 | * COPYRIGHT: | |
3 | * Copyright (c) 2004-2006, International Business Machines Corporation and | |
4 | * others. All Rights Reserved. | |
5 | ********************************************************************/ | |
6 | ||
7 | // Test parts of UVector and UStack | |
8 | ||
9 | #include "intltest.h" | |
10 | ||
11 | #include "uvectest.h" | |
12 | #include "uvector.h" | |
13 | #include "hash.h" | |
14 | ||
15 | #include "cstring.h" | |
16 | ||
17 | //--------------------------------------------------------------------------- | |
18 | // | |
19 | // Test class boilerplate | |
20 | // | |
21 | //--------------------------------------------------------------------------- | |
22 | UVectorTest::UVectorTest() | |
23 | { | |
24 | } | |
25 | ||
26 | ||
27 | UVectorTest::~UVectorTest() | |
28 | { | |
29 | } | |
30 | ||
31 | ||
32 | ||
33 | void UVectorTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ ) | |
34 | { | |
35 | if (exec) logln("TestSuite UVectorTest: "); | |
36 | switch (index) { | |
37 | ||
38 | case 0: name = "UVector_API"; | |
39 | if (exec) UVector_API(); | |
40 | break; | |
41 | case 1: name = "UStack_API"; | |
42 | if (exec) UStack_API(); | |
43 | break; | |
44 | case 2: name = "Hashtable_API"; | |
45 | if (exec) Hashtable_API(); | |
46 | break; | |
47 | default: name = ""; | |
48 | break; //needed to end loop | |
49 | } | |
50 | } | |
51 | ||
52 | ||
53 | //--------------------------------------------------------------------------- | |
54 | // | |
55 | // Error Checking / Reporting macros used in all of the tests. | |
56 | // | |
57 | //--------------------------------------------------------------------------- | |
58 | #define TEST_CHECK_STATUS(status) \ | |
59 | if (U_FAILURE(status)) {\ | |
60 | errln("UVectorTest failure at line %d. status=%s\n", __LINE__, u_errorName(status));\ | |
61 | return;\ | |
62 | } | |
63 | ||
64 | #define TEST_ASSERT(expr) \ | |
65 | if ((expr)==FALSE) {\ | |
66 | errln("UVectorTest failure at line %d.\n", __LINE__);\ | |
67 | } | |
68 | ||
69 | static int8_t | |
70 | UVectorTest_compareInt32(UHashTok key1, UHashTok key2) { | |
71 | if (key1.integer > key2.integer) { | |
72 | return 1; | |
73 | } | |
74 | else if (key1.integer < key2.integer) { | |
75 | return -1; | |
76 | } | |
77 | return 0; | |
78 | } | |
79 | ||
80 | U_CDECL_BEGIN | |
81 | static int8_t U_CALLCONV | |
82 | UVectorTest_compareCstrings(const UHashTok key1, const UHashTok key2) { | |
83 | return !strcmp((const char *)key1.pointer, (const char *)key2.pointer); | |
84 | } | |
85 | U_CDECL_END | |
86 | ||
87 | //--------------------------------------------------------------------------- | |
88 | // | |
89 | // UVector_API Check for basic functionality of UVector. | |
90 | // | |
91 | //--------------------------------------------------------------------------- | |
92 | void UVectorTest::UVector_API() { | |
93 | ||
94 | UErrorCode status = U_ZERO_ERROR; | |
95 | UVector *a; | |
96 | ||
97 | a = new UVector(status); | |
98 | TEST_CHECK_STATUS(status); | |
99 | delete a; | |
100 | ||
101 | status = U_ZERO_ERROR; | |
102 | a = new UVector(2000, status); | |
103 | TEST_CHECK_STATUS(status); | |
104 | delete a; | |
105 | ||
106 | status = U_ZERO_ERROR; | |
107 | a = new UVector(status); | |
108 | a->sortedInsert((int32_t)10, UVectorTest_compareInt32, status); | |
109 | a->sortedInsert((int32_t)20, UVectorTest_compareInt32, status); | |
110 | a->sortedInsert((int32_t)30, UVectorTest_compareInt32, status); | |
111 | a->sortedInsert((int32_t)15, UVectorTest_compareInt32, status); | |
112 | TEST_CHECK_STATUS(status); | |
113 | TEST_ASSERT(a->elementAti(0) == 10); | |
114 | TEST_ASSERT(a->elementAti(1) == 15); | |
115 | TEST_ASSERT(a->elementAti(2) == 20); | |
116 | TEST_ASSERT(a->elementAti(3) == 30); | |
117 | TEST_ASSERT(a->indexOf((int32_t)3) == -1); | |
118 | TEST_ASSERT(a->indexOf((int32_t)15) == 1); | |
119 | TEST_ASSERT(a->indexOf((int32_t)15, 2) == -1); | |
120 | TEST_ASSERT(a->contains((int32_t)15)); | |
121 | TEST_ASSERT(!a->contains((int32_t)5)); | |
122 | delete a; | |
123 | } | |
124 | ||
125 | void UVectorTest::UStack_API() { | |
126 | UErrorCode status = U_ZERO_ERROR; | |
127 | UStack *a; | |
128 | ||
129 | a = new UStack(status); | |
130 | TEST_CHECK_STATUS(status); | |
131 | delete a; | |
132 | ||
133 | status = U_ZERO_ERROR; | |
134 | a = new UStack(2000, status); | |
135 | TEST_CHECK_STATUS(status); | |
136 | delete a; | |
137 | ||
138 | status = U_ZERO_ERROR; | |
139 | a = new UStack(NULL, NULL, 2000, status); | |
140 | TEST_CHECK_STATUS(status); | |
141 | delete a; | |
142 | ||
143 | status = U_ZERO_ERROR; | |
144 | a = new UStack(NULL, UVectorTest_compareCstrings, status); | |
145 | TEST_ASSERT(a->empty()); | |
146 | a->push((void*)"abc", status); | |
147 | TEST_ASSERT(!a->empty()); | |
148 | a->push((void*)"bcde", status); | |
149 | a->push((void*)"cde", status); | |
150 | TEST_CHECK_STATUS(status); | |
151 | TEST_ASSERT(strcmp("cde", (const char *)a->peek()) == 0); | |
152 | TEST_ASSERT(a->search((void*)"cde") == 1); | |
153 | TEST_ASSERT(a->search((void*)"bcde") == 2); | |
154 | TEST_ASSERT(a->search((void*)"abc") == 3); | |
155 | TEST_ASSERT(strcmp("abc", (const char *)a->firstElement()) == 0); | |
156 | TEST_ASSERT(strcmp("cde", (const char *)a->lastElement()) == 0); | |
157 | TEST_ASSERT(strcmp("cde", (const char *)a->pop()) == 0); | |
158 | TEST_ASSERT(strcmp("bcde", (const char *)a->pop()) == 0); | |
159 | TEST_ASSERT(strcmp("abc", (const char *)a->pop()) == 0); | |
160 | delete a; | |
161 | } | |
162 | ||
46f4442e A |
163 | U_CDECL_BEGIN |
164 | static UBool U_CALLCONV neverTRUE(const UHashTok /*key1*/, const UHashTok /*key2*/) { | |
165 | return FALSE; | |
166 | } | |
167 | ||
168 | U_CDECL_END | |
169 | ||
73c04bcf A |
170 | void UVectorTest::Hashtable_API() { |
171 | UErrorCode status = U_ZERO_ERROR; | |
172 | Hashtable *a = new Hashtable(status); | |
173 | TEST_ASSERT((a->puti("a", 1, status) == 0)); | |
174 | TEST_ASSERT((a->find("a") != NULL)); | |
175 | TEST_ASSERT((a->find("b") == NULL)); | |
176 | TEST_ASSERT((a->puti("b", 2, status) == 0)); | |
177 | TEST_ASSERT((a->find("b") != NULL)); | |
178 | TEST_ASSERT((a->removei("a") == 1)); | |
179 | TEST_ASSERT((a->find("a") == NULL)); | |
46f4442e A |
180 | |
181 | /* verify that setValueCompartor works */ | |
182 | Hashtable b(status); | |
183 | TEST_ASSERT((!a->equals(b))); | |
184 | TEST_ASSERT((b.puti("b", 2, status) == 0)); | |
185 | TEST_ASSERT((!a->equals(b))); // Without a value comparator, this will be FALSE by default. | |
186 | b.setValueCompartor(uhash_compareLong); | |
187 | TEST_ASSERT((!a->equals(b))); | |
188 | a->setValueCompartor(uhash_compareLong); | |
189 | TEST_ASSERT((a->equals(b))); | |
190 | TEST_ASSERT((a->equals(*a))); // This better be reflexive. | |
191 | ||
192 | /* verify that setKeyCompartor works */ | |
193 | TEST_ASSERT((a->puti("a", 1, status) == 0)); | |
194 | TEST_ASSERT((a->find("a") != NULL)); | |
195 | a->setKeyCompartor(neverTRUE); | |
196 | TEST_ASSERT((a->find("a") == NULL)); | |
197 | ||
73c04bcf A |
198 | delete a; |
199 | } | |
200 |