1 /********************************************************************
3 * Copyright (c) 1997-2006, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
7 #include "unicode/utypes.h"
9 #if !UCONFIG_NO_COLLATION
15 ContractionTableTest::ContractionTableTest() {
16 testMapping
= utrie_open(NULL
, NULL
, 0, 0, 0, TRUE
);
19 ContractionTableTest::~ContractionTableTest() {
20 utrie_close(testMapping
);
23 void ContractionTableTest::TestGrowTable(/* char* par */) {
24 UErrorCode status
= U_ZERO_ERROR
;
25 uint32_t i
= 0, res
= 0;
26 testTable
= uprv_cnttab_open(testMapping
, &status
);
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
));
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
));
45 errln("Error: expected %i, got %i\n", i
, res
);
50 uprv_cnttab_close(testTable
);
53 void 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
));
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");
69 status
= U_ZERO_ERROR
;
70 uprv_cnttab_close(testTable
);
73 void ContractionTableTest::TestAddATableElement(){
74 UErrorCode status
= U_ZERO_ERROR
;
75 testTable
= uprv_cnttab_open(testMapping
, &status
);
76 uint32_t i
= 0, res
= 0;
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
));
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
));
95 errln("Error: expected %i, got %i\n", i
, res
);
100 uprv_cnttab_close(testTable
);
103 void 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
));
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
));
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
));
129 errln("Error: expected %i, got %i\n", i
, res
);
134 uprv_cnttab_close(testClone
);
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
));
141 uprv_cnttab_close(testTable
);
144 void 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
);
150 errln("found a non existing contraction!\n");
153 for(i
= 0; i
< 0x20; i
+=2) {
154 uprv_cnttab_addContraction(testTable
, 0, (UChar
)i
, i
, &status
);
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");
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");
168 uprv_cnttab_close(testTable
);
171 void 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
);
176 errln("managed to change the last CE in an non-existing contraction!\n");
178 uprv_cnttab_close(testTable
);
181 void ContractionTableTest::TestErrorCodeChecking(){
182 UErrorCode status
= U_REGEX_SET_CONTAINS_STRING
;
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
));
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
));
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
));
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
));
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
));
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
));
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
));
212 if (uprv_cnttab_getCE(NULL
, 0, 0, &status
) != UCOL_NOT_FOUND
) {
213 errln("uprv_cnttab_getCE didn't return UCOL_NOT_FOUND\n");
215 if (status
!= U_REGEX_SET_CONTAINS_STRING
) {
216 errln("Status was incorrectly modified to %s\n", u_errorName(status
));
218 if (uprv_cnttab_findCE(NULL
, 0, 0, &status
) != UCOL_NOT_FOUND
) {
219 errln("uprv_cnttab_findCE didn't return UCOL_NOT_FOUND\n");
221 if (status
!= U_REGEX_SET_CONTAINS_STRING
) {
222 errln("Status was incorrectly modified to %s\n", u_errorName(status
));
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
));
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
));
234 void ContractionTableTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
236 if (exec
) logln("TestSuite ContractionTableTest: ");
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;
249 #endif /* #if !UCONFIG_NO_COLLATION */