]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/v32test.cpp
ICU-491.11.1.tar.gz
[apple/icu.git] / icuSources / test / intltest / v32test.cpp
1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 2002-2007, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6
7 //
8 // regextst.cpp
9 //
10 // ICU Regular Expressions test, part of intltest.
11 //
12
13 #include "intltest.h"
14
15 #include "v32test.h"
16 #include "uvectr32.h"
17 #include "uvector.h"
18 #include "util.h"
19 #include <stdlib.h>
20 #include <stdio.h>
21
22
23 //---------------------------------------------------------------------------
24 //
25 // Test class boilerplate
26 //
27 //---------------------------------------------------------------------------
28 UVector32Test::UVector32Test()
29 {
30 }
31
32
33 UVector32Test::~UVector32Test()
34 {
35 }
36
37
38
39 void UVector32Test::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
40 {
41 if (exec) logln("TestSuite UVector32Test: ");
42 switch (index) {
43
44 case 0: name = "UVector32_API";
45 if (exec) UVector32_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("UVector32Test 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("UVector32Test failure at line %d.\n", __LINE__);\
67 }
68
69 //---------------------------------------------------------------------------
70 //
71 // UVector32_API Check for basic functionality of UVector32.
72 //
73 //---------------------------------------------------------------------------
74 void UVector32Test::UVector32_API() {
75
76 UErrorCode status = U_ZERO_ERROR;
77 UVector32 *a;
78 UVector32 *b;
79
80 a = new UVector32(status);
81 TEST_CHECK_STATUS(status);
82 delete a;
83
84 status = U_ZERO_ERROR;
85 a = new UVector32(2000, status);
86 TEST_CHECK_STATUS(status);
87 delete a;
88
89 //
90 // assign()
91 //
92 status = U_ZERO_ERROR;
93 a = new UVector32(status);
94 a->addElement(10, status);
95 a->addElement(20, status);
96 a->addElement(30, status);
97 b = new UVector32(status);
98 b->assign(*a, status);
99 TEST_ASSERT(b->size() == 3);
100 TEST_ASSERT(b->elementAti(1) == 20);
101 TEST_CHECK_STATUS(status);
102 delete a;
103 delete b;
104
105 //
106 // operator == and != and equals()
107 //
108 status = U_ZERO_ERROR;
109 a = new UVector32(status);
110 a->addElement(10, status);
111 a->addElement(20, status);
112 a->addElement(30, status);
113 b = new UVector32(status);
114 TEST_ASSERT(*b != *a);
115 TEST_ASSERT(!(*b == *a));
116 TEST_ASSERT(!b->equals(*a));
117 b->assign(*a, status);
118 TEST_ASSERT(*b == *a);
119 TEST_ASSERT(!(*b != *a));
120 TEST_ASSERT(b->equals(*a));
121 b->addElement(666, status);
122 TEST_ASSERT(*b != *a);
123 TEST_ASSERT(!(*b == *a));
124 TEST_ASSERT(!b->equals(*a));
125 TEST_CHECK_STATUS(status);
126 delete b;
127 delete a;
128
129 //
130 // addElement(). Covered by above tests.
131 //
132
133 //
134 // setElementAt()
135 //
136 status = U_ZERO_ERROR;
137 a = new UVector32(status);
138 a->addElement(10, status);
139 a->addElement(20, status);
140 a->addElement(30, status);
141 a->setElementAt(666, 1);
142 TEST_ASSERT(a->elementAti(0) == 10);
143 TEST_ASSERT(a->elementAti(1) == 666);
144 TEST_ASSERT(a->size() == 3);
145 TEST_CHECK_STATUS(status);
146 delete a;
147
148 //
149 // insertElementAt()
150 //
151 status = U_ZERO_ERROR;
152 a = new UVector32(status);
153 a->addElement(10, status);
154 a->addElement(20, status);
155 a->addElement(30, status);
156 a->insertElementAt(666, 1, status);
157 TEST_ASSERT(a->elementAti(0) == 10);
158 TEST_ASSERT(a->elementAti(1) == 666);
159 TEST_ASSERT(a->elementAti(2) == 20);
160 TEST_ASSERT(a->elementAti(3) == 30);
161 TEST_ASSERT(a->size() == 4);
162 TEST_CHECK_STATUS(status);
163 delete a;
164
165 //
166 // elementAti() covered by above tests
167 //
168
169 //
170 // lastElementi
171 //
172 status = U_ZERO_ERROR;
173 a = new UVector32(status);
174 a->addElement(10, status);
175 a->addElement(20, status);
176 a->addElement(30, status);
177 TEST_ASSERT(a->lastElementi() == 30);
178 TEST_CHECK_STATUS(status);
179 delete a;
180
181
182 //
183 // indexOf
184 //
185 status = U_ZERO_ERROR;
186 a = new UVector32(status);
187 a->addElement(10, status);
188 a->addElement(20, status);
189 a->addElement(30, status);
190 TEST_ASSERT(a->indexOf(30, 0) == 2);
191 TEST_ASSERT(a->indexOf(40, 0) == -1);
192 TEST_ASSERT(a->indexOf(10, 0) == 0);
193 TEST_ASSERT(a->indexOf(10, 1) == -1);
194 TEST_CHECK_STATUS(status);
195 delete a;
196
197
198 //
199 // contains
200 //
201 status = U_ZERO_ERROR;
202 a = new UVector32(status);
203 a->addElement(10, status);
204 a->addElement(20, status);
205 a->addElement(30, status);
206 TEST_ASSERT(a->contains(10) == TRUE);
207 TEST_ASSERT(a->contains(11) == FALSE);
208 TEST_ASSERT(a->contains(20) == TRUE);
209 TEST_ASSERT(a->contains(-10) == FALSE);
210 TEST_CHECK_STATUS(status);
211 delete a;
212
213
214 //
215 // containsAll
216 //
217 status = U_ZERO_ERROR;
218 a = new UVector32(status);
219 a->addElement(10, status);
220 a->addElement(20, status);
221 a->addElement(30, status);
222 b = new UVector32(status);
223 TEST_ASSERT(a->containsAll(*b) == TRUE);
224 b->addElement(2, status);
225 TEST_ASSERT(a->containsAll(*b) == FALSE);
226 b->setElementAt(10, 0);
227 TEST_ASSERT(a->containsAll(*b) == TRUE);
228 TEST_ASSERT(b->containsAll(*a) == FALSE);
229 b->addElement(30, status);
230 b->addElement(20, status);
231 TEST_ASSERT(a->containsAll(*b) == TRUE);
232 TEST_ASSERT(b->containsAll(*a) == TRUE);
233 b->addElement(2, status);
234 TEST_ASSERT(a->containsAll(*b) == FALSE);
235 TEST_ASSERT(b->containsAll(*a) == TRUE);
236 TEST_CHECK_STATUS(status);
237 delete a;
238 delete b;
239
240 //
241 // removeAll
242 //
243 status = U_ZERO_ERROR;
244 a = new UVector32(status);
245 a->addElement(10, status);
246 a->addElement(20, status);
247 a->addElement(30, status);
248 b = new UVector32(status);
249 a->removeAll(*b);
250 TEST_ASSERT(a->size() == 3);
251 b->addElement(20, status);
252 a->removeAll(*b);
253 TEST_ASSERT(a->size() == 2);
254 TEST_ASSERT(a->contains(10)==TRUE);
255 TEST_ASSERT(a->contains(30)==TRUE);
256 b->addElement(10, status);
257 a->removeAll(*b);
258 TEST_ASSERT(a->size() == 1);
259 TEST_ASSERT(a->contains(30) == TRUE);
260 TEST_CHECK_STATUS(status);
261 delete a;
262 delete b;
263
264 //
265 // retainAll
266 //
267 status = U_ZERO_ERROR;
268 a = new UVector32(status);
269 a->addElement(10, status);
270 a->addElement(20, status);
271 a->addElement(30, status);
272 b = new UVector32(status);
273 b->addElement(10, status);
274 b->addElement(20, status);
275 b->addElement(30, status);
276 b->addElement(15, status);
277 a->retainAll(*b);
278 TEST_ASSERT(a->size() == 3);
279 b->removeElementAt(1);
280 a->retainAll(*b);
281 TEST_ASSERT(a->contains(20) == FALSE);
282 TEST_ASSERT(a->size() == 2);
283 b->removeAllElements();
284 TEST_ASSERT(b->size() == 0);
285 a->retainAll(*b);
286 TEST_ASSERT(a->size() == 0);
287 TEST_CHECK_STATUS(status);
288 delete a;
289 delete b;
290
291 //
292 // removeElementAt Tested above.
293 //
294
295 //
296 // removeAllElments Tested above
297 //
298
299 //
300 // size() tested above
301 //
302
303 //
304 // isEmpty
305 //
306 status = U_ZERO_ERROR;
307 a = new UVector32(status);
308 TEST_ASSERT(a->isEmpty() == TRUE);
309 a->addElement(10, status);
310 TEST_ASSERT(a->isEmpty() == FALSE);
311 a->addElement(20, status);
312 a->removeElementAt(0);
313 TEST_ASSERT(a->isEmpty() == FALSE);
314 a->removeElementAt(0);
315 TEST_ASSERT(a->isEmpty() == TRUE);
316 TEST_CHECK_STATUS(status);
317 delete a;
318
319
320 //
321 // ensureCapacity, expandCapacity
322 //
323 status = U_ZERO_ERROR;
324 a = new UVector32(status);
325 TEST_ASSERT(a->isEmpty() == TRUE);
326 a->addElement(10, status);
327 TEST_ASSERT(a->ensureCapacity(5000, status)== TRUE);
328 TEST_ASSERT(a->expandCapacity(20000, status) == TRUE);
329 TEST_CHECK_STATUS(status);
330 delete a;
331
332 //
333 // setSize
334 //
335 status = U_ZERO_ERROR;
336 a = new UVector32(status);
337 a->addElement(10, status);
338 a->addElement(20, status);
339 a->addElement(30, status);
340 a->setSize(100);
341 TEST_ASSERT(a->size() == 100);
342 TEST_ASSERT(a->elementAti(0) == 10);
343 TEST_ASSERT(a->elementAti(1) == 20);
344 TEST_ASSERT(a->elementAti(2) == 30);
345 TEST_ASSERT(a->elementAti(3) == 0);
346 a->setElementAt(666, 99);
347 a->setElementAt(777, 100);
348 TEST_ASSERT(a->elementAti(99) == 666);
349 TEST_ASSERT(a->elementAti(100) == 0);
350 a->setSize(2);
351 TEST_ASSERT(a->elementAti(1) == 20);
352 TEST_ASSERT(a->elementAti(2) == 0);
353 TEST_ASSERT(a->size() == 2);
354 a->setSize(0);
355 TEST_ASSERT(a->empty() == TRUE);
356 TEST_ASSERT(a->size() == 0);
357
358 TEST_CHECK_STATUS(status);
359 delete a;
360
361 //
362 // containsNone
363 //
364 status = U_ZERO_ERROR;
365 a = new UVector32(status);
366 a->addElement(10, status);
367 a->addElement(20, status);
368 a->addElement(30, status);
369 b = new UVector32(status);
370 TEST_ASSERT(a->containsNone(*b) == TRUE);
371 b->addElement(5, status);
372 TEST_ASSERT(a->containsNone(*b) == TRUE);
373 b->addElement(30, status);
374 TEST_ASSERT(a->containsNone(*b) == FALSE);
375
376 TEST_CHECK_STATUS(status);
377 delete a;
378 delete b;
379
380 //
381 // sortedInsert
382 //
383 status = U_ZERO_ERROR;
384 a = new UVector32(status);
385 a->sortedInsert(30, status);
386 a->sortedInsert(20, status);
387 a->sortedInsert(10, status);
388 TEST_ASSERT(a->elementAti(0) == 10);
389 TEST_ASSERT(a->elementAti(1) == 20);
390 TEST_ASSERT(a->elementAti(2) == 30);
391
392 TEST_CHECK_STATUS(status);
393 delete a;
394
395 //
396 // getBuffer
397 //
398 status = U_ZERO_ERROR;
399 a = new UVector32(status);
400 a->addElement(10, status);
401 a->addElement(20, status);
402 int32_t *buf = a->getBuffer();
403 TEST_ASSERT(buf[0] == 10);
404 TEST_ASSERT(buf[1] == 20);
405 a->setSize(20000);
406 int32_t *resizedBuf;
407 resizedBuf = a->getBuffer();
408 //TEST_ASSERT(buf != resizedBuf); // The buffer might have been realloc'd
409 TEST_ASSERT(resizedBuf[0] == 10);
410 TEST_ASSERT(resizedBuf[1] == 20);
411
412 TEST_CHECK_STATUS(status);
413 delete a;
414
415
416 //
417 // empty
418 //
419 status = U_ZERO_ERROR;
420 a = new UVector32(status);
421 TEST_ASSERT(a->empty() == TRUE);
422 a->addElement(10, status);
423 TEST_ASSERT(a->empty() == FALSE);
424 a->addElement(20, status);
425 a->removeElementAt(0);
426 TEST_ASSERT(a->empty() == FALSE);
427 a->removeElementAt(0);
428 TEST_ASSERT(a->empty() == TRUE);
429 TEST_CHECK_STATUS(status);
430 delete a;
431
432
433 //
434 // peeki
435 //
436 status = U_ZERO_ERROR;
437 a = new UVector32(status);
438 a->addElement(10, status);
439 TEST_ASSERT(a->peeki() == 10);
440 a->addElement(20, status);
441 TEST_ASSERT(a->peeki() == 20);
442 a->addElement(30, status);
443 TEST_ASSERT(a->peeki() == 30);
444 TEST_CHECK_STATUS(status);
445 delete a;
446
447
448 //
449 // popi
450 //
451 status = U_ZERO_ERROR;
452 a = new UVector32(status);
453 a->addElement(10, status);
454 a->addElement(20, status);
455 a->addElement(30, status);
456 TEST_ASSERT(a->popi() == 30);
457 TEST_ASSERT(a->popi() == 20);
458 TEST_ASSERT(a->popi() == 10);
459 TEST_ASSERT(a->popi() == 0);
460 TEST_ASSERT(a->isEmpty());
461 TEST_CHECK_STATUS(status);
462 delete a;
463
464 //
465 // push
466 //
467 status = U_ZERO_ERROR;
468 a = new UVector32(status);
469 TEST_ASSERT(a->push(10, status) == 10);
470 TEST_ASSERT(a->push(20, status) == 20);
471 TEST_ASSERT(a->push(30, status) == 30);
472 TEST_ASSERT(a->size() == 3);
473 TEST_ASSERT(a->popi() == 30);
474 TEST_ASSERT(a->popi() == 20);
475 TEST_ASSERT(a->popi() == 10);
476 TEST_ASSERT(a->isEmpty());
477 TEST_CHECK_STATUS(status);
478 delete a;
479
480
481 //
482 // reserveBlock
483 //
484 status = U_ZERO_ERROR;
485 a = new UVector32(status);
486 a->ensureCapacity(1000, status);
487
488 // TODO:
489
490 TEST_CHECK_STATUS(status);
491 delete a;
492
493 }
494
495