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