1/********************************************************************
2* COPYRIGHT:
3* Copyright (c) 1997-2006, International Business Machines Corporation and
4* others. All Rights Reserved.
5********************************************************************/
6
7#include "unicode/utypes.h"
8
9#if !UCONFIG_NO_COLLATION
10
11#include "cntabcol.h"
12
13U_NAMESPACE_USE
14
15ContractionTableTest::ContractionTableTest() {
16    testMapping = utrie_open(NULL, NULL, 0, 0, 0, TRUE);
17}
18
19ContractionTableTest::~ContractionTableTest() {
20    utrie_close(testMapping);
21}
22
23void ContractionTableTest::TestGrowTable(/* char* par */) {
24    UErrorCode status = U_ZERO_ERROR;
25    uint32_t i = 0, res = 0;
26    testTable = uprv_cnttab_open(testMapping, &status);
27
28    // fill up one contraction so that it has to expand
29    for(i = 0; i<65536; i++) {
30        uprv_cnttab_addContraction(testTable, 0, (UChar)i, i, &status);
31        if(U_FAILURE(status)) {
32            errln("Error occurred at position %i, error = %i (%s)\n", i, status, u_errorName(status));
33            break;
34        }
35    }
36    // test whether the filled up contraction really contains the data we input
37    if(U_SUCCESS(status)) {
38        for(i = 0; i<65536; i++) {
39            res = uprv_cnttab_getCE(testTable, 0, i, &status);
40            if(U_FAILURE(status)) {
41                errln("Error occurred at position %i, error = %i (%s)\n", i, status, u_errorName(status));
42                break;
43            }
44            if(res != i) {
45                errln("Error: expected %i, got %i\n", i, res);
46                break;
47            }
48        }
49    }
50    uprv_cnttab_close(testTable);
51}
52
53void ContractionTableTest::TestSetContraction(){
54    UErrorCode status = U_ZERO_ERROR;
55    testTable = uprv_cnttab_open(testMapping, &status);
56    // This should make a new contraction
57    uprv_cnttab_setContraction(testTable, 1, 0, 0x41, 0x41, &status);
58    if(U_FAILURE(status)) {
59        errln("Error setting a non existing contraction error = %i (%s)\n", status, u_errorName(status));
60    }
61    // if we try to change the non existing offset, we should get an error
62    status = U_ZERO_ERROR;
63    // currently this tests whether there is enough space, maybe it should test whether the element is actually in
64    // range. Also, maybe a silent growing should take place....
65    uprv_cnttab_setContraction(testTable, 1, 0x401, 0x41, 0x41, &status);
66    if(status != U_INDEX_OUTOFBOUNDS_ERROR) {
67        errln("changing a non-existing offset should have resulted in an error\n");
68    }
69    status = U_ZERO_ERROR;
70    uprv_cnttab_close(testTable);
71}
72
73void ContractionTableTest::TestAddATableElement(){
74    UErrorCode status = U_ZERO_ERROR;
75    testTable = uprv_cnttab_open(testMapping, &status);
76    uint32_t i = 0, res = 0;
77
78    // fill up one contraction so that it has to expand
79    for(i = 0; i<0x1000; i++) {
80        uprv_cnttab_addContraction(testTable, i, (UChar)i, i, &status);
81        if(U_FAILURE(status)) {
82            errln("Error occurred at position %i, error = %i (%s)\n", i, status, u_errorName(status));
83            break;
84        }
85    }
86    // test whether the filled up contraction really contains the data we input
87    if(U_SUCCESS(status)) {
88        for(i = 0; i<0x1000; i++) {
89            res = uprv_cnttab_getCE(testTable, i, 0, &status);
90            if(U_FAILURE(status)) {
91                errln("Error occurred at position %i, error = %i (%s)\n", i, status, u_errorName(status));
92                break;
93            }
94            if(res != i) {
95                errln("Error: expected %i, got %i\n", i, res);
96                break;
97            }
98        }
99    }
100    uprv_cnttab_close(testTable);
101}
102
103void ContractionTableTest::TestClone(){
104    UErrorCode status = U_ZERO_ERROR;
105    testTable = uprv_cnttab_open(testMapping, &status);
106    int32_t i = 0, res = 0;
107    // we must construct table in order to copy codepoints and CEs
108    // fill up one contraction so that it has to expand
109    for(i = 0; i<0x500; i++) {
110        uprv_cnttab_addContraction(testTable, i, (UChar)i, i, &status);
111        if(U_FAILURE(status)) {
112            errln("Error occurred at position %i, error = %i (%s)\n", i, status, u_errorName(status));
113            break;
114        }
115    }
116    uprv_cnttab_constructTable(testTable, 0, &status);
117    if(U_FAILURE(status)) {
118        errln("Error constructing table error = %i (%s)\n", status, u_errorName(status));
119    } else {
120        testClone = uprv_cnttab_clone(testTable, &status);
121        if(U_SUCCESS(status)) {
122            for(i = 0; i<0x500; i++) {
123                res = uprv_cnttab_getCE(testTable, i, 0, &status);
124                if(U_FAILURE(status)) {
125                    errln("Error occurred at position %i, error = %i (%s)\n", i, status, u_errorName(status));
126                    break;
127                }
128                if(res != i) {
129                    errln("Error: expected %i, got %i\n", i, res);
130                    break;
131                }
132            }
133        }
134        uprv_cnttab_close(testClone);
135    }
136    uprv_cnttab_close(testTable);
137    testTable = uprv_cnttab_open(testMapping, &status);
138    if(U_FAILURE(status)) {
139        errln("Error opening table error = %i (%s)\n", status, u_errorName(status));
140    }
141    uprv_cnttab_close(testTable);
142}
143
144void ContractionTableTest::TestChangeContraction(){
145    UErrorCode status = U_ZERO_ERROR;
146    testTable = uprv_cnttab_open(testMapping, &status);
147    uint32_t i = 0, res = 0;
148    res = uprv_cnttab_changeContraction(testTable, 0, 0x41, 0xAB, &status);
149    if(res != 0) {
150        errln("found a non existing contraction!\n");
151    }
152
153    for(i = 0; i < 0x20; i+=2) {
154        uprv_cnttab_addContraction(testTable, 0, (UChar)i, i, &status);
155    }
156
157    res = uprv_cnttab_changeContraction(testTable, 0, 0x41, 0xAB, &status);
158    if(res != UCOL_NOT_FOUND) {
159        errln("managed to change a non existing contraction!\n");
160    }
161
162    for(i = 1; i < 0x20; i+=2) {
163        res = uprv_cnttab_changeContraction(testTable, 0, (UChar)i, 0xAB, &status);
164        if(res != UCOL_NOT_FOUND) {
165            errln("managed to change a non existing contraction!\n");
166        }
167    }
168    uprv_cnttab_close(testTable);
169}
170
171void ContractionTableTest::TestChangeLastCE(){
172    UErrorCode status = U_ZERO_ERROR;
173    testTable = uprv_cnttab_open(testMapping, &status);
174    uint32_t res = uprv_cnttab_changeLastCE(testTable, 1, 0xABCD, &status);
175    if(res!=0) {
176        errln("managed to change the last CE in an non-existing contraction!\n");
177    }
178    uprv_cnttab_close(testTable);
179}
180
181void ContractionTableTest::TestErrorCodeChecking(){
182    UErrorCode status = U_REGEX_SET_CONTAINS_STRING;
183
184    uprv_cnttab_open(NULL, &status);
185    if (status != U_REGEX_SET_CONTAINS_STRING) {
186        errln("Status was incorrectly modified to %s\n", u_errorName(status));
187    }
188    uprv_cnttab_clone(NULL, &status);
189    if (status != U_REGEX_SET_CONTAINS_STRING) {
190        errln("Status was incorrectly modified to %s\n", u_errorName(status));
191    }
192    uprv_cnttab_changeLastCE(NULL, 0, 0, &status);
193    if (status != U_REGEX_SET_CONTAINS_STRING) {
194        errln("Status was incorrectly modified to %s\n", u_errorName(status));
195    }
196    uprv_cnttab_insertContraction(NULL, 0, 0, 0, &status);
197    if (status != U_REGEX_SET_CONTAINS_STRING) {
198        errln("Status was incorrectly modified to %s\n", u_errorName(status));
199    }
200    uprv_cnttab_addContraction(NULL, 0, 0, 0, &status);
201    if (status != U_REGEX_SET_CONTAINS_STRING) {
202        errln("Status was incorrectly modified to %s\n", u_errorName(status));
203    }
204    uprv_cnttab_setContraction(NULL, 0, 0, 0, 0, &status);
205    if (status != U_REGEX_SET_CONTAINS_STRING) {
206        errln("Status was incorrectly modified to %s\n", u_errorName(status));
207    }
208    uprv_cnttab_findCP(NULL, 0, 0, &status);
209    if (status != U_REGEX_SET_CONTAINS_STRING) {
210        errln("Status was incorrectly modified to %s\n", u_errorName(status));
211    }
212    if (uprv_cnttab_getCE(NULL, 0, 0, &status) != UCOL_NOT_FOUND) {
213        errln("uprv_cnttab_getCE didn't return UCOL_NOT_FOUND\n");
214    }
215    if (status != U_REGEX_SET_CONTAINS_STRING) {
216        errln("Status was incorrectly modified to %s\n", u_errorName(status));
217    }
218    if (uprv_cnttab_findCE(NULL, 0, 0, &status) != UCOL_NOT_FOUND) {
219        errln("uprv_cnttab_findCE didn't return UCOL_NOT_FOUND\n");
220    }
221    if (status != U_REGEX_SET_CONTAINS_STRING) {
222        errln("Status was incorrectly modified to %s\n", u_errorName(status));
223    }
224    uprv_cnttab_isTailored(NULL, 0, NULL, &status);
225    if (status != U_REGEX_SET_CONTAINS_STRING) {
226        errln("Status was incorrectly modified to %s\n", u_errorName(status));
227    }
228    uprv_cnttab_changeContraction(NULL, 0, 0, 0, &status);
229    if (status != U_REGEX_SET_CONTAINS_STRING) {
230        errln("Status was incorrectly modified to %s\n", u_errorName(status));
231    }
232}
233
234void ContractionTableTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
235{
236    if (exec) logln("TestSuite ContractionTableTest: ");
237    switch (index) {
238        case 0: name = "TestGrowTable";         if (exec)   TestGrowTable(/* par */); break;
239        case 1: name = "TestSetContraction";    if (exec)   TestSetContraction(/* par */); break;
240        case 2: name = "TestAddATableElement";  if (exec)   TestAddATableElement(/* par */); break;
241        case 3: name = "TestClone";             if (exec)   TestClone(/* par */); break;
242        case 4: name = "TestChangeContraction"; if (exec)   TestChangeContraction(/* par */); break;
243        case 5: name = "TestChangeLastCE";      if (exec)   TestChangeLastCE(/* par */); break;
244        case 6: name = "TestErrorCodeChecking"; if (exec)   TestErrorCodeChecking(/* par */); break;
245        default: name = ""; break;
246    }
247}
248
249#endif /* #if !UCONFIG_NO_COLLATION */
250