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