blob: 9502ee7f76ef8e648a77dbbfa77823947e31f8d7 [file] [log] [blame]
Frank Tang3e05d9d2021-11-08 14:04:04 -08001// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
3/********************************************************************
4 * COPYRIGHT:
5 * Copyright (c) 2002-2007, International Business Machines Corporation and
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//---------------------------------------------------------------------------
30UVector32Test::UVector32Test()
31{
32}
33
34
35UVector32Test::~UVector32Test()
36{
37}
38
39
40
41void 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//---------------------------------------------------------------------------
60#define TEST_CHECK_STATUS(status) UPRV_BLOCK_MACRO_BEGIN {\
61 if (U_FAILURE(status)) {\
62 errln("UVector32Test failure at line %d. status=%s\n", __LINE__, u_errorName(status));\
63 return;\
64 }\
65} UPRV_BLOCK_MACRO_END
66
67#define TEST_ASSERT(expr) UPRV_BLOCK_MACRO_BEGIN {\
Frank Tang1f164ee2022-11-08 12:31:27 -080068 if ((expr)==false) {\
Frank Tang3e05d9d2021-11-08 14:04:04 -080069 errln("UVector32Test failure at line %d.\n", __LINE__);\
70 }\
71} UPRV_BLOCK_MACRO_END
72
73//---------------------------------------------------------------------------
74//
75// UVector32_API Check for basic functionality of UVector32.
76//
77//---------------------------------------------------------------------------
78void 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);
Frank Tang1f164ee2022-11-08 12:31:27 -0800210 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);
Frank Tang3e05d9d2021-11-08 14:04:04 -0800214 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);
Frank Tang1f164ee2022-11-08 12:31:27 -0800227 TEST_ASSERT(a->containsAll(*b) == true);
Frank Tang3e05d9d2021-11-08 14:04:04 -0800228 b->addElement(2, status);
Frank Tang1f164ee2022-11-08 12:31:27 -0800229 TEST_ASSERT(a->containsAll(*b) == false);
Frank Tang3e05d9d2021-11-08 14:04:04 -0800230 b->setElementAt(10, 0);
Frank Tang1f164ee2022-11-08 12:31:27 -0800231 TEST_ASSERT(a->containsAll(*b) == true);
232 TEST_ASSERT(b->containsAll(*a) == false);
Frank Tang3e05d9d2021-11-08 14:04:04 -0800233 b->addElement(30, status);
234 b->addElement(20, status);
Frank Tang1f164ee2022-11-08 12:31:27 -0800235 TEST_ASSERT(a->containsAll(*b) == true);
236 TEST_ASSERT(b->containsAll(*a) == true);
Frank Tang3e05d9d2021-11-08 14:04:04 -0800237 b->addElement(2, status);
Frank Tang1f164ee2022-11-08 12:31:27 -0800238 TEST_ASSERT(a->containsAll(*b) == false);
239 TEST_ASSERT(b->containsAll(*a) == true);
Frank Tang3e05d9d2021-11-08 14:04:04 -0800240 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);
Frank Tang1f164ee2022-11-08 12:31:27 -0800258 TEST_ASSERT(a->contains(10)==true);
259 TEST_ASSERT(a->contains(30)==true);
Frank Tang3e05d9d2021-11-08 14:04:04 -0800260 b->addElement(10, status);
261 a->removeAll(*b);
262 TEST_ASSERT(a->size() == 1);
Frank Tang1f164ee2022-11-08 12:31:27 -0800263 TEST_ASSERT(a->contains(30) == true);
Frank Tang3e05d9d2021-11-08 14:04:04 -0800264 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);
Frank Tang1f164ee2022-11-08 12:31:27 -0800285 TEST_ASSERT(a->contains(20) == false);
Frank Tang3e05d9d2021-11-08 14:04:04 -0800286 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);
Frank Tang1f164ee2022-11-08 12:31:27 -0800312 TEST_ASSERT(a->isEmpty() == true);
Frank Tang3e05d9d2021-11-08 14:04:04 -0800313 a->addElement(10, status);
Frank Tang1f164ee2022-11-08 12:31:27 -0800314 TEST_ASSERT(a->isEmpty() == false);
Frank Tang3e05d9d2021-11-08 14:04:04 -0800315 a->addElement(20, status);
316 a->removeElementAt(0);
Frank Tang1f164ee2022-11-08 12:31:27 -0800317 TEST_ASSERT(a->isEmpty() == false);
Frank Tang3e05d9d2021-11-08 14:04:04 -0800318 a->removeElementAt(0);
Frank Tang1f164ee2022-11-08 12:31:27 -0800319 TEST_ASSERT(a->isEmpty() == true);
Frank Tang3e05d9d2021-11-08 14:04:04 -0800320 TEST_CHECK_STATUS(status);
321 delete a;
322
323
324 //
325 // ensureCapacity, expandCapacity
326 //
327 status = U_ZERO_ERROR;
328 a = new UVector32(status);
Frank Tang1f164ee2022-11-08 12:31:27 -0800329 TEST_ASSERT(a->isEmpty() == true);
Frank Tang3e05d9d2021-11-08 14:04:04 -0800330 a->addElement(10, status);
Frank Tang1f164ee2022-11-08 12:31:27 -0800331 TEST_ASSERT(a->ensureCapacity(5000, status)== true);
332 TEST_ASSERT(a->expandCapacity(20000, status) == true);
Frank Tang3e05d9d2021-11-08 14:04:04 -0800333 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);
Frank Tang1f164ee2022-11-08 12:31:27 -0800359 TEST_ASSERT(a->empty() == true);
Frank Tang3e05d9d2021-11-08 14:04:04 -0800360 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);
Frank Tang1f164ee2022-11-08 12:31:27 -0800374 TEST_ASSERT(a->containsNone(*b) == true);
Frank Tang3e05d9d2021-11-08 14:04:04 -0800375 b->addElement(5, status);
Frank Tang1f164ee2022-11-08 12:31:27 -0800376 TEST_ASSERT(a->containsNone(*b) == true);
Frank Tang3e05d9d2021-11-08 14:04:04 -0800377 b->addElement(30, status);
Frank Tang1f164ee2022-11-08 12:31:27 -0800378 TEST_ASSERT(a->containsNone(*b) == false);
Frank Tang3e05d9d2021-11-08 14:04:04 -0800379
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();
412 //TEST_ASSERT(buf != resizedBuf); // The buffer might have been realloc'd
413 TEST_ASSERT(resizedBuf[0] == 10);
414 TEST_ASSERT(resizedBuf[1] == 20);
415
416 TEST_CHECK_STATUS(status);
417 delete a;
418
419
420 //
421 // empty
422 //
423 status = U_ZERO_ERROR;
424 a = new UVector32(status);
Frank Tang1f164ee2022-11-08 12:31:27 -0800425 TEST_ASSERT(a->empty() == true);
Frank Tang3e05d9d2021-11-08 14:04:04 -0800426 a->addElement(10, status);
Frank Tang1f164ee2022-11-08 12:31:27 -0800427 TEST_ASSERT(a->empty() == false);
Frank Tang3e05d9d2021-11-08 14:04:04 -0800428 a->addElement(20, status);
429 a->removeElementAt(0);
Frank Tang1f164ee2022-11-08 12:31:27 -0800430 TEST_ASSERT(a->empty() == false);
Frank Tang3e05d9d2021-11-08 14:04:04 -0800431 a->removeElementAt(0);
Frank Tang1f164ee2022-11-08 12:31:27 -0800432 TEST_ASSERT(a->empty() == true);
Frank Tang3e05d9d2021-11-08 14:04:04 -0800433 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
497}
498
499