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