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