]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/trnserr.cpp
ICU-8.11.4.tar.gz
[apple/icu.git] / icuSources / test / intltest / trnserr.cpp
1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 2001-2005, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6 /************************************************************************
7 * This test program is intended for testing error conditions of the
8 * transliterator APIs to make sure the exceptions are raised where
9 * necessary.
10 *
11 * Date Name Description
12 * 11/14/2001 hshih Creation.
13 *
14 ************************************************************************/
15
16 #include "unicode/utypes.h"
17
18 #if !UCONFIG_NO_TRANSLITERATION
19
20 #include "ittrans.h"
21 #include "trnserr.h"
22 #include "unicode/utypes.h"
23 #include "unicode/translit.h"
24 #include "unicode/uniset.h"
25 #include "rbt.h"
26 #include "unicode/unifilt.h"
27 #include "cpdtrans.h"
28 #include "nultrans.h"
29 #include <string.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include "unicode/rep.h"
33 #include "unicode/locid.h"
34
35 //---------------------------------------------
36 // runIndexedTest
37 //---------------------------------------------
38
39 void
40 TransliteratorErrorTest::runIndexedTest(int32_t index, UBool exec,
41 const char* &name, char* /*par*/) {
42 switch (index) {
43 TESTCASE(0,TestTransliteratorErrors);
44 TESTCASE(1, TestUnicodeSetErrors);
45 TESTCASE(2, TestRBTErrors);
46 TESTCASE(3, TestCoverage);
47 //TESTCASE(3, TestUniToHexErrors);
48 //TESTCASE(4, TestHexToUniErrors);
49 // TODO: Add a subclass to test clone().
50 default: name = ""; break;
51 }
52 }
53
54
55 void TransliteratorErrorTest::TestTransliteratorErrors() {
56 UnicodeString trans="Latin-Greek";
57 UnicodeString bogusID="LATINGREEK-GREEKLATIN";
58 UnicodeString newID="Bogus-Latin";
59 UnicodeString newIDRules="zzz > Z; f <> ph";
60 UnicodeString bogusRules="a } [b-g m-p ";
61 UParseError parseError;
62 UErrorCode status = U_ZERO_ERROR;
63 UnicodeString testString="A quick fox jumped over the lazy dog.";
64 UnicodeString insertString="cats and dogs";
65 int32_t stoppedAt = 0, len;
66 UTransPosition pos;
67
68 Transliterator* t= Transliterator::createInstance(trans, UTRANS_FORWARD, parseError, status);
69 if(t==0 || U_FAILURE(status)){
70 errln("FAIL: construction of Latin-Greek");
71 return;
72 }
73 pos.contextLimit = 0;
74 pos.contextStart = 0;
75 pos.limit = 0;
76 pos.start = 0;
77 len = testString.length();
78 stoppedAt = t->transliterate(testString, 0, 100);
79 if (stoppedAt != -1) {
80 errln("FAIL: Out of bounds check failed (1).");
81 } else if (testString.length() != len) {
82 testString="A quick fox jumped over the lazy dog.";
83 errln("FAIL: Transliterate fails and the target string was modified.");
84 }
85 stoppedAt = t->transliterate(testString, 100, testString.length()-1);
86 if (stoppedAt != -1)
87 errln("FAIL: Out of bounds check failed (2).");
88 else if (testString.length() != len) {
89 testString="A quick fox jumped over the lazy dog.";
90 errln("FAIL: Transliterate fails and the target string was modified.");
91 }
92 pos.start = 100;
93 pos.limit = testString.length();
94 t->transliterate(testString, pos, status);
95 if (U_SUCCESS(status)) {
96 errln("FAIL: Start offset is out of bounds, error not reported.\n");
97 }
98 status = U_ZERO_ERROR;
99 pos.limit = 100;
100 pos.start = 0;
101 t->transliterate(testString, pos, status);
102 if (U_SUCCESS(status)) {
103 errln("FAIL: Limit offset is out of bounds, error not reported.\n");
104 }
105 status = U_ZERO_ERROR;
106 len = pos.contextLimit = testString.length();
107 pos.contextStart = 0;
108 pos.limit = len - 1;
109 pos.start = 5;
110 t->transliterate(testString, pos, insertString, status);
111 if (len == pos.limit) {
112 errln("FAIL: Test insertion with string: the transliteration position limit didn't change as expected.");
113 if (U_SUCCESS(status)) {
114 errln("FAIL: Error code wasn't set either.");
115 }
116 }
117 status = U_ZERO_ERROR;
118 pos.contextStart = 0;
119 pos.contextLimit = testString.length();
120 pos.limit = testString.length() -1;
121 pos.start = 5;
122 t->transliterate(testString, pos, (UChar32)0x0061, status);
123 if (len == pos.limit) {
124 errln("FAIL: Test insertion with character: the transliteration position limit didn't change as expected.");
125 if (U_SUCCESS(status)) {
126 errln("FAIL: Error code wasn't set either.");
127 }
128 }
129 status = U_ZERO_ERROR;
130 len = pos.limit = testString.length();
131 pos.contextStart = 0;
132 pos.contextLimit = testString.length() - 1;
133 pos.start = 5;
134 t->transliterate(testString, pos, insertString, status);
135 if (U_SUCCESS(status)) {
136 errln("FAIL: Out of bounds check failed (3).");
137 if (testString.length() != len)
138 errln("FAIL: The input string was modified though the offsets were out of bounds.");
139 }
140 Transliterator* t1= Transliterator::createInstance(bogusID, UTRANS_FORWARD, parseError, status);
141 if(t1!=0 || U_SUCCESS(status)){
142 delete t1;
143 errln("FAIL: construction of bogus ID \"LATINGREEK-GREEKLATIN\"");
144 } else {
145 delete t1;
146 }
147 status = U_ZERO_ERROR;
148 Transliterator* t2 = new RuleBasedTransliterator(newID, newIDRules, UTRANS_FORWARD, status);
149 if (U_SUCCESS(status)) {
150 Transliterator* t3 = t2->createInverse(status);
151 if (U_SUCCESS(status)) {
152 delete t3;
153 errln("FAIL: The newID transliterator was not registered so createInverse should fail.");
154 } else {
155 delete t3;
156 }
157 }
158 status = U_ZERO_ERROR;
159 Transliterator* t4 = Transliterator::createFromRules(newID, bogusRules, UTRANS_FORWARD, parseError, status);
160 if (t4 != NULL || U_SUCCESS(status)) {
161 errln("FAIL: The rules is malformed but error was not reported.");
162 if (parseError.offset != -1) {
163 errln("FAIL: The parse error offset isn't set correctly when fails.");
164 } else if (parseError.postContext[0] == 0 || parseError.preContext[0] == 0) {
165 errln("FAIL: The parse error pre/post context isn't reset properly.");
166 }
167 delete t4;
168 }
169 delete t;
170 delete t2;
171 }
172
173 void TransliteratorErrorTest::TestUnicodeSetErrors() {
174 UnicodeString badPattern="[[:L:]-[0x0300-0x0400]";
175 UnicodeSet set;
176 UErrorCode status = U_ZERO_ERROR;
177 UnicodeString result;
178
179 if (!set.isEmpty()) {
180 errln("FAIL: The default ctor of UnicodeSet created a non-empty object.");
181 }
182 set.applyPattern(badPattern, status);
183 if (U_SUCCESS(status)) {
184 errln("FAIL: Applied a bad pattern to the UnicodeSet object okay.");
185 }
186 status = U_ZERO_ERROR;
187 UnicodeSet *set1 = new UnicodeSet(badPattern, status);
188 if (U_SUCCESS(status)) {
189 errln("FAIL: Created a UnicodeSet based on bad patterns.");
190 }
191 delete set1;
192 }
193
194 //void TransliteratorErrorTest::TestUniToHexErrors() {
195 // UErrorCode status = U_ZERO_ERROR;
196 // Transliterator *t = new UnicodeToHexTransliterator("", TRUE, NULL, status);
197 // if (U_SUCCESS(status)) {
198 // errln("FAIL: Created a UnicodeToHexTransliterator with an empty pattern.");
199 // }
200 // delete t;
201 //
202 // status = U_ZERO_ERROR;
203 // t = new UnicodeToHexTransliterator("\\x", TRUE, NULL, status);
204 // if (U_SUCCESS(status)) {
205 // errln("FAIL: Created a UnicodeToHexTransliterator with a bad pattern.");
206 // }
207 // delete t;
208 //
209 // status = U_ZERO_ERROR;
210 // t = new UnicodeToHexTransliterator();
211 // ((UnicodeToHexTransliterator*)t)->applyPattern("\\x", status);
212 // if (U_SUCCESS(status)) {
213 // errln("FAIL: UnicodeToHexTransliterator::applyPattern succeeded with a bad pattern.");
214 // }
215 // delete t;
216 //}
217
218 void TransliteratorErrorTest::TestRBTErrors() {
219
220 UnicodeString rules="ab>y";
221 UnicodeString id="MyRandom-YReverse";
222 UnicodeString goodPattern="[[:L:]&[\\u0000-\\uFFFF]]"; /* all BMP letters */
223 UErrorCode status = U_ZERO_ERROR;
224 UParseError parseErr;
225 UnicodeSet *set = new UnicodeSet(goodPattern, status);
226 if (U_FAILURE(status)) {
227 errln("FAIL: Was not able to create a good UnicodeSet based on valid patterns.");
228 return;
229 }
230 RuleBasedTransliterator *t = new RuleBasedTransliterator(id, rules, UTRANS_REVERSE, set, parseErr, status);
231 if (U_FAILURE(status)) {
232 errln("FAIL: Was not able to create a good RBT to test registration.");
233 delete set;
234 return;
235 }
236 Transliterator::registerInstance(t);
237 Transliterator::unregister(id);
238 status = U_ZERO_ERROR;
239 Transliterator* t1= Transliterator::createInstance(id, UTRANS_REVERSE, parseErr, status);
240 if(U_SUCCESS(status)){
241 delete t1;
242 errln("FAIL: construction of unregistered ID failed.");
243 }
244 }
245
246 //void TransliteratorErrorTest::TestHexToUniErrors() {
247 // UErrorCode status = U_ZERO_ERROR;
248 // Transliterator *t = new HexToUnicodeTransliterator("", NULL, status);
249 // if (U_FAILURE(status)) {
250 // errln("FAIL: Could not create a HexToUnicodeTransliterator with an empty pattern.");
251 // }
252 // delete t;
253 // status = U_ZERO_ERROR;
254 // t = new HexToUnicodeTransliterator("\\x", NULL, status);
255 // if (U_SUCCESS(status)) {
256 // errln("FAIL: Created a HexToUnicodeTransliterator with a bad pattern.");
257 // }
258 // delete t;
259 // status = U_ZERO_ERROR;
260 // t = new HexToUnicodeTransliterator();
261 // ((HexToUnicodeTransliterator*)t)->applyPattern("\\x", status);
262 // if (U_SUCCESS(status)) {
263 // errln("FAIL: HexToUnicodeTransliterator::applyPattern succeeded with a bad pattern.");
264 // }
265 // delete t;
266 //}
267
268 class StubTransliterator: public Transliterator{
269 public:
270 StubTransliterator(): Transliterator(UNICODE_STRING_SIMPLE("Any-Null"), 0) {}
271 virtual void handleTransliterate(Replaceable& ,UTransPosition& offsets,UBool) const {
272 offsets.start = offsets.limit;
273 }
274
275 virtual UClassID getDynamicClassID() const{
276 static char classID = 0;
277 return (UClassID)&classID;
278 }
279 };
280
281 void TransliteratorErrorTest::TestCoverage() {
282 StubTransliterator stub;
283
284 if (stub.clone() != NULL){
285 errln("FAIL: default Transliterator::clone() should return NULL");
286 }
287 }
288
289 #endif /* #if !UCONFIG_NO_TRANSLITERATION */