1 /********************************************************************
3 * Copyright (c) 2004-2011, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
7 // Test parts of UVector and UStack
17 //---------------------------------------------------------------------------
19 // Test class boilerplate
21 //---------------------------------------------------------------------------
22 UVectorTest::UVectorTest()
27 UVectorTest::~UVectorTest()
33 void UVectorTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
35 if (exec
) logln("TestSuite UVectorTest: ");
38 case 0: name
= "UVector_API";
39 if (exec
) UVector_API();
41 case 1: name
= "UStack_API";
42 if (exec
) UStack_API();
44 case 2: name
= "Hashtable_API";
45 if (exec
) Hashtable_API();
48 break; //needed to end loop
53 //---------------------------------------------------------------------------
55 // Error Checking / Reporting macros used in all of the tests.
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));\
64 #define TEST_ASSERT(expr) \
66 errln("UVectorTest failure at line %d.\n", __LINE__);\
69 static int8_t U_CALLCONV
70 UVectorTest_compareInt32(UElement key1
, UElement key2
) {
71 if (key1
.integer
> key2
.integer
) {
74 else if (key1
.integer
< key2
.integer
) {
81 static int8_t U_CALLCONV
82 UVectorTest_compareCstrings(const UElement key1
, const UElement key2
) {
83 return !strcmp((const char *)key1
.pointer
, (const char *)key2
.pointer
);
87 //---------------------------------------------------------------------------
89 // UVector_API Check for basic functionality of UVector.
91 //---------------------------------------------------------------------------
92 void UVectorTest::UVector_API() {
94 UErrorCode status
= U_ZERO_ERROR
;
97 a
= new UVector(status
);
98 TEST_CHECK_STATUS(status
);
101 status
= U_ZERO_ERROR
;
102 a
= new UVector(2000, status
);
103 TEST_CHECK_STATUS(status
);
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));
125 void UVectorTest::UStack_API() {
126 UErrorCode status
= U_ZERO_ERROR
;
129 a
= new UStack(status
);
130 TEST_CHECK_STATUS(status
);
133 status
= U_ZERO_ERROR
;
134 a
= new UStack(2000, status
);
135 TEST_CHECK_STATUS(status
);
138 status
= U_ZERO_ERROR
;
139 a
= new UStack(NULL
, NULL
, 2000, status
);
140 TEST_CHECK_STATUS(status
);
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);
164 static UBool U_CALLCONV
neverTRUE(const UElement
/*key1*/, const UElement
/*key2*/) {
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
));
181 /* verify that setValueComparator works */
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
.setValueComparator(uhash_compareLong
);
187 TEST_ASSERT((!a
->equals(b
)));
188 a
->setValueComparator(uhash_compareLong
);
189 TEST_ASSERT((a
->equals(b
)));
190 TEST_ASSERT((a
->equals(*a
))); // This better be reflexive.
192 /* verify that setKeyComparator works */
193 TEST_ASSERT((a
->puti("a", 1, status
) == 0));
194 TEST_ASSERT((a
->find("a") != NULL
));
195 a
->setKeyComparator(neverTRUE
);
196 TEST_ASSERT((a
->find("a") == NULL
));