1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
5 * Copyright (c) 2004-2011, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
9 // Test parts of UVector and UStack
19 //---------------------------------------------------------------------------
21 // Test class boilerplate
23 //---------------------------------------------------------------------------
24 UVectorTest::UVectorTest()
29 UVectorTest::~UVectorTest()
35 void UVectorTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
37 if (exec
) logln("TestSuite UVectorTest: ");
40 case 0: name
= "UVector_API";
41 if (exec
) UVector_API();
43 case 1: name
= "UStack_API";
44 if (exec
) UStack_API();
46 case 2: name
= "Hashtable_API";
47 if (exec
) Hashtable_API();
50 break; //needed to end loop
55 //---------------------------------------------------------------------------
57 // Error Checking / Reporting macros used in all of the tests.
59 //---------------------------------------------------------------------------
60 #define TEST_CHECK_STATUS(status) UPRV_BLOCK_MACRO_BEGIN {\
61 if (U_FAILURE(status)) {\
62 errln("UVectorTest failure at line %d. status=%s\n", __LINE__, u_errorName(status));\
65 } UPRV_BLOCK_MACRO_END
67 #define TEST_ASSERT(expr) UPRV_BLOCK_MACRO_BEGIN {\
69 errln("UVectorTest failure at line %d.\n", __LINE__);\
71 } UPRV_BLOCK_MACRO_END
73 static int8_t U_CALLCONV
74 UVectorTest_compareInt32(UElement key1
, UElement key2
) {
75 if (key1
.integer
> key2
.integer
) {
78 else if (key1
.integer
< key2
.integer
) {
85 static int8_t U_CALLCONV
86 UVectorTest_compareCstrings(const UElement key1
, const UElement key2
) {
87 return !strcmp((const char *)key1
.pointer
, (const char *)key2
.pointer
);
91 //---------------------------------------------------------------------------
93 // UVector_API Check for basic functionality of UVector.
95 //---------------------------------------------------------------------------
96 void UVectorTest::UVector_API() {
98 UErrorCode status
= U_ZERO_ERROR
;
101 a
= new UVector(status
);
102 TEST_CHECK_STATUS(status
);
105 status
= U_ZERO_ERROR
;
106 a
= new UVector(2000, status
);
107 TEST_CHECK_STATUS(status
);
110 status
= U_ZERO_ERROR
;
111 a
= new UVector(status
);
112 a
->sortedInsert((int32_t)10, UVectorTest_compareInt32
, status
);
113 a
->sortedInsert((int32_t)20, UVectorTest_compareInt32
, status
);
114 a
->sortedInsert((int32_t)30, UVectorTest_compareInt32
, status
);
115 a
->sortedInsert((int32_t)15, UVectorTest_compareInt32
, status
);
116 TEST_CHECK_STATUS(status
);
117 TEST_ASSERT(a
->elementAti(0) == 10);
118 TEST_ASSERT(a
->elementAti(1) == 15);
119 TEST_ASSERT(a
->elementAti(2) == 20);
120 TEST_ASSERT(a
->elementAti(3) == 30);
121 TEST_ASSERT(a
->indexOf((int32_t)3) == -1);
122 TEST_ASSERT(a
->indexOf((int32_t)15) == 1);
123 TEST_ASSERT(a
->indexOf((int32_t)15, 2) == -1);
124 TEST_ASSERT(a
->contains((int32_t)15));
125 TEST_ASSERT(!a
->contains((int32_t)5));
129 void UVectorTest::UStack_API() {
130 UErrorCode status
= U_ZERO_ERROR
;
133 a
= new UStack(status
);
134 TEST_CHECK_STATUS(status
);
137 status
= U_ZERO_ERROR
;
138 a
= new UStack(2000, status
);
139 TEST_CHECK_STATUS(status
);
142 status
= U_ZERO_ERROR
;
143 a
= new UStack(NULL
, NULL
, 2000, status
);
144 TEST_CHECK_STATUS(status
);
147 status
= U_ZERO_ERROR
;
148 a
= new UStack(NULL
, UVectorTest_compareCstrings
, status
);
149 TEST_ASSERT(a
->empty());
150 a
->push((void*)"abc", status
);
151 TEST_ASSERT(!a
->empty());
152 a
->push((void*)"bcde", status
);
153 a
->push((void*)"cde", status
);
154 TEST_CHECK_STATUS(status
);
155 TEST_ASSERT(strcmp("cde", (const char *)a
->peek()) == 0);
156 TEST_ASSERT(a
->search((void*)"cde") == 1);
157 TEST_ASSERT(a
->search((void*)"bcde") == 2);
158 TEST_ASSERT(a
->search((void*)"abc") == 3);
159 TEST_ASSERT(strcmp("abc", (const char *)a
->firstElement()) == 0);
160 TEST_ASSERT(strcmp("cde", (const char *)a
->lastElement()) == 0);
161 TEST_ASSERT(strcmp("cde", (const char *)a
->pop()) == 0);
162 TEST_ASSERT(strcmp("bcde", (const char *)a
->pop()) == 0);
163 TEST_ASSERT(strcmp("abc", (const char *)a
->pop()) == 0);
168 static UBool U_CALLCONV
neverTRUE(const UElement
/*key1*/, const UElement
/*key2*/) {
174 void UVectorTest::Hashtable_API() {
175 UErrorCode status
= U_ZERO_ERROR
;
176 Hashtable
*a
= new Hashtable(status
);
177 TEST_ASSERT((a
->puti("a", 1, status
) == 0));
178 TEST_ASSERT((a
->find("a") != NULL
));
179 TEST_ASSERT((a
->find("b") == NULL
));
180 TEST_ASSERT((a
->puti("b", 2, status
) == 0));
181 TEST_ASSERT((a
->find("b") != NULL
));
182 TEST_ASSERT((a
->removei("a") == 1));
183 TEST_ASSERT((a
->find("a") == NULL
));
185 /* verify that setValueComparator works */
187 TEST_ASSERT((!a
->equals(b
)));
188 TEST_ASSERT((b
.puti("b", 2, status
) == 0));
189 TEST_ASSERT((!a
->equals(b
))); // Without a value comparator, this will be FALSE by default.
190 b
.setValueComparator(uhash_compareLong
);
191 TEST_ASSERT((!a
->equals(b
)));
192 a
->setValueComparator(uhash_compareLong
);
193 TEST_ASSERT((a
->equals(b
)));
194 TEST_ASSERT((a
->equals(*a
))); // This better be reflexive.
196 /* verify that setKeyComparator works */
197 TEST_ASSERT((a
->puti("a", 1, status
) == 0));
198 TEST_ASSERT((a
->find("a") != NULL
));
199 a
->setKeyComparator(neverTRUE
);
200 TEST_ASSERT((a
->find("a") == NULL
));