]>
Commit | Line | Data |
---|---|---|
1 | /******************************************************************** | |
2 | * COPYRIGHT: | |
3 | * Copyright (c) 2001-2004, 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, TestUniToHexErrors); | |
47 | //TESTCASE(4, TestHexToUniErrors); | |
48 | // TODO: Add a subclass to test clone(). | |
49 | default: name = ""; break; | |
50 | } | |
51 | } | |
52 | ||
53 | ||
54 | void TransliteratorErrorTest::TestTransliteratorErrors() { | |
55 | UnicodeString trans="Latin-Greek"; | |
56 | UnicodeString bogusID="LATINGREEK-GREEKLATIN"; | |
57 | UnicodeString newID="Bogus-Latin"; | |
58 | UnicodeString newIDRules="zzz > Z; f <> ph"; | |
59 | UnicodeString bogusRules="a } [b-g m-p "; | |
60 | UParseError parseError; | |
61 | UErrorCode status = U_ZERO_ERROR; | |
62 | UnicodeString testString="A quick fox jumped over the lazy dog."; | |
63 | UnicodeString insertString="cats and dogs"; | |
64 | int32_t stoppedAt = 0, len; | |
65 | UTransPosition pos; | |
66 | ||
67 | Transliterator* t= Transliterator::createInstance(trans, UTRANS_FORWARD, parseError, status); | |
68 | if(t==0 || U_FAILURE(status)){ | |
69 | errln("FAIL: construction of Latin-Greek"); | |
70 | return; | |
71 | } | |
72 | pos.contextLimit = 0; | |
73 | pos.contextStart = 0; | |
74 | pos.limit = 0; | |
75 | pos.start = 0; | |
76 | len = testString.length(); | |
77 | stoppedAt = t->transliterate(testString, 0, 100); | |
78 | if (stoppedAt != -1) { | |
79 | errln("FAIL: Out of bounds check failed (1)."); | |
80 | } else if (testString.length() != len) { | |
81 | testString="A quick fox jumped over the lazy dog."; | |
82 | errln("FAIL: Transliterate fails and the target string was modified."); | |
83 | } | |
84 | stoppedAt = t->transliterate(testString, 100, testString.length()-1); | |
85 | if (stoppedAt != -1) | |
86 | errln("FAIL: Out of bounds check failed (2)."); | |
87 | else if (testString.length() != len) { | |
88 | testString="A quick fox jumped over the lazy dog."; | |
89 | errln("FAIL: Transliterate fails and the target string was modified."); | |
90 | } | |
91 | pos.start = 100; | |
92 | pos.limit = testString.length(); | |
93 | t->transliterate(testString, pos, status); | |
94 | if (U_SUCCESS(status)) { | |
95 | errln("FAIL: Start offset is out of bounds, error not reported.\n"); | |
96 | } | |
97 | status = U_ZERO_ERROR; | |
98 | pos.limit = 100; | |
99 | pos.start = 0; | |
100 | t->transliterate(testString, pos, status); | |
101 | if (U_SUCCESS(status)) { | |
102 | errln("FAIL: Limit offset is out of bounds, error not reported.\n"); | |
103 | } | |
104 | status = U_ZERO_ERROR; | |
105 | len = pos.contextLimit = testString.length(); | |
106 | pos.contextStart = 0; | |
107 | pos.limit = len - 1; | |
108 | pos.start = 5; | |
109 | t->transliterate(testString, pos, insertString, status); | |
110 | if (len == pos.limit) { | |
111 | errln("FAIL: Test insertion with string: the transliteration position limit didn't change as expected."); | |
112 | if (U_SUCCESS(status)) { | |
113 | errln("FAIL: Error code wasn't set either."); | |
114 | } | |
115 | } | |
116 | status = U_ZERO_ERROR; | |
117 | pos.contextStart = 0; | |
118 | pos.contextLimit = testString.length(); | |
119 | pos.limit = testString.length() -1; | |
120 | pos.start = 5; | |
121 | t->transliterate(testString, pos, (UChar32)0x0061, status); | |
122 | if (len == pos.limit) { | |
123 | errln("FAIL: Test insertion with character: the transliteration position limit didn't change as expected."); | |
124 | if (U_SUCCESS(status)) { | |
125 | errln("FAIL: Error code wasn't set either."); | |
126 | } | |
127 | } | |
128 | status = U_ZERO_ERROR; | |
129 | len = pos.limit = testString.length(); | |
130 | pos.contextStart = 0; | |
131 | pos.contextLimit = testString.length() - 1; | |
132 | pos.start = 5; | |
133 | t->transliterate(testString, pos, insertString, status); | |
134 | if (U_SUCCESS(status)) { | |
135 | errln("FAIL: Out of bounds check failed (3)."); | |
136 | if (testString.length() != len) | |
137 | errln("FAIL: The input string was modified though the offsets were out of bounds."); | |
138 | } | |
139 | Transliterator* t1= Transliterator::createInstance(bogusID, UTRANS_FORWARD, parseError, status); | |
140 | if(t1!=0 || U_SUCCESS(status)){ | |
141 | delete t1; | |
142 | errln("FAIL: construction of bogus ID \"LATINGREEK-GREEKLATIN\""); | |
143 | } else { | |
144 | delete t1; | |
145 | } | |
146 | status = U_ZERO_ERROR; | |
147 | Transliterator* t2 = new RuleBasedTransliterator(newID, newIDRules, UTRANS_FORWARD, status); | |
148 | if (U_SUCCESS(status)) { | |
149 | Transliterator* t3 = t2->createInverse(status); | |
150 | if (U_SUCCESS(status)) { | |
151 | delete t3; | |
152 | errln("FAIL: The newID transliterator was not registered so createInverse should fail."); | |
153 | } else { | |
154 | delete t3; | |
155 | } | |
156 | } | |
157 | status = U_ZERO_ERROR; | |
158 | Transliterator* t4 = Transliterator::createFromRules(newID, bogusRules, UTRANS_FORWARD, parseError, status); | |
159 | if (t4 != NULL || U_SUCCESS(status)) { | |
160 | errln("FAIL: The rules is malformed but error was not reported."); | |
161 | if (parseError.offset != -1) { | |
162 | errln("FAIL: The parse error offset isn't set correctly when fails."); | |
163 | } else if (parseError.postContext[0] == 0 || parseError.preContext[0] == 0) { | |
164 | errln("FAIL: The parse error pre/post context isn't reset properly."); | |
165 | } | |
166 | delete t4; | |
167 | } | |
168 | delete t; | |
169 | delete t2; | |
170 | } | |
171 | ||
172 | void TransliteratorErrorTest::TestUnicodeSetErrors() { | |
173 | UnicodeString badPattern="[[:L:]-[0x0300-0x0400]"; | |
174 | UnicodeSet set; | |
175 | UErrorCode status = U_ZERO_ERROR; | |
176 | UnicodeString result; | |
177 | ||
178 | if (!set.isEmpty()) { | |
179 | errln("FAIL: The default ctor of UnicodeSet created a non-empty object."); | |
180 | } | |
181 | set.applyPattern(badPattern, status); | |
182 | if (U_SUCCESS(status)) { | |
183 | errln("FAIL: Applied a bad pattern to the UnicodeSet object okay."); | |
184 | } | |
185 | status = U_ZERO_ERROR; | |
186 | UnicodeSet *set1 = new UnicodeSet(badPattern, status); | |
187 | if (U_SUCCESS(status)) { | |
188 | errln("FAIL: Created a UnicodeSet based on bad patterns."); | |
189 | } | |
190 | delete set1; | |
191 | } | |
192 | ||
193 | //void TransliteratorErrorTest::TestUniToHexErrors() { | |
194 | // UErrorCode status = U_ZERO_ERROR; | |
195 | // Transliterator *t = new UnicodeToHexTransliterator("", TRUE, NULL, status); | |
196 | // if (U_SUCCESS(status)) { | |
197 | // errln("FAIL: Created a UnicodeToHexTransliterator with an empty pattern."); | |
198 | // } | |
199 | // delete t; | |
200 | // | |
201 | // status = U_ZERO_ERROR; | |
202 | // t = new UnicodeToHexTransliterator("\\x", TRUE, NULL, status); | |
203 | // if (U_SUCCESS(status)) { | |
204 | // errln("FAIL: Created a UnicodeToHexTransliterator with a bad pattern."); | |
205 | // } | |
206 | // delete t; | |
207 | // | |
208 | // status = U_ZERO_ERROR; | |
209 | // t = new UnicodeToHexTransliterator(); | |
210 | // ((UnicodeToHexTransliterator*)t)->applyPattern("\\x", status); | |
211 | // if (U_SUCCESS(status)) { | |
212 | // errln("FAIL: UnicodeToHexTransliterator::applyPattern succeeded with a bad pattern."); | |
213 | // } | |
214 | // delete t; | |
215 | //} | |
216 | ||
217 | void TransliteratorErrorTest::TestRBTErrors() { | |
218 | ||
219 | UnicodeString rules="ab>y"; | |
220 | UnicodeString id="MyRandom-YReverse"; | |
221 | UnicodeString goodPattern="[[:L:]&[\\u0000-\\uFFFF]]"; /* all BMP letters */ | |
222 | UErrorCode status = U_ZERO_ERROR; | |
223 | UParseError parseErr; | |
224 | UnicodeSet *set = new UnicodeSet(goodPattern, status); | |
225 | if (U_FAILURE(status)) { | |
226 | errln("FAIL: Was not able to create a good UnicodeSet based on valid patterns."); | |
227 | return; | |
228 | } | |
229 | RuleBasedTransliterator *t = new RuleBasedTransliterator(id, rules, UTRANS_REVERSE, set, parseErr, status); | |
230 | if (U_FAILURE(status)) { | |
231 | errln("FAIL: Was not able to create a good RBT to test registration."); | |
232 | delete set; | |
233 | return; | |
234 | } | |
235 | Transliterator::registerInstance(t); | |
236 | Transliterator::unregister(id); | |
237 | status = U_ZERO_ERROR; | |
238 | Transliterator* t1= Transliterator::createInstance(id, UTRANS_REVERSE, parseErr, status); | |
239 | if(U_SUCCESS(status)){ | |
240 | delete t1; | |
241 | errln("FAIL: construction of unregistered ID failed."); | |
242 | } | |
243 | } | |
244 | ||
245 | //void TransliteratorErrorTest::TestHexToUniErrors() { | |
246 | // UErrorCode status = U_ZERO_ERROR; | |
247 | // Transliterator *t = new HexToUnicodeTransliterator("", NULL, status); | |
248 | // if (U_FAILURE(status)) { | |
249 | // errln("FAIL: Could not create a HexToUnicodeTransliterator with an empty pattern."); | |
250 | // } | |
251 | // delete t; | |
252 | // status = U_ZERO_ERROR; | |
253 | // t = new HexToUnicodeTransliterator("\\x", NULL, status); | |
254 | // if (U_SUCCESS(status)) { | |
255 | // errln("FAIL: Created a HexToUnicodeTransliterator with a bad pattern."); | |
256 | // } | |
257 | // delete t; | |
258 | // status = U_ZERO_ERROR; | |
259 | // t = new HexToUnicodeTransliterator(); | |
260 | // ((HexToUnicodeTransliterator*)t)->applyPattern("\\x", status); | |
261 | // if (U_SUCCESS(status)) { | |
262 | // errln("FAIL: HexToUnicodeTransliterator::applyPattern succeeded with a bad pattern."); | |
263 | // } | |
264 | // delete t; | |
265 | //} | |
266 | ||
267 | #endif /* #if !UCONFIG_NO_TRANSLITERATION */ |