From 0f2ceda1e2a79396ae6911e054871fc9dbcbb426 Mon Sep 17 00:00:00 2001 From: Helena Chapman Date: Fri, 16 Nov 2001 23:31:46 +0000 Subject: [PATCH] ICU-1533 Added coverage test for error conditions. X-SVN-Rev: 6961 --- icu4c/source/test/intltest/intltest.dsp | 8 + icu4c/source/test/intltest/ittrans.cpp | 2 + icu4c/source/test/intltest/trnserr.cpp | 256 ++++++++++++++++++++++++ icu4c/source/test/intltest/trnserr.h | 48 +++++ 4 files changed, 314 insertions(+) create mode 100644 icu4c/source/test/intltest/trnserr.cpp create mode 100644 icu4c/source/test/intltest/trnserr.h diff --git a/icu4c/source/test/intltest/intltest.dsp b/icu4c/source/test/intltest/intltest.dsp index a62a275d5fd..e42c1ee5863 100644 --- a/icu4c/source/test/intltest/intltest.dsp +++ b/icu4c/source/test/intltest/intltest.dsp @@ -361,6 +361,10 @@ SOURCE=.\trcoll.cpp # End Source File # Begin Source File +SOURCE=.\trnserr.cpp +# End Source File +# Begin Source File + SOURCE=.\tscoll.cpp # End Source File # Begin Source File @@ -697,6 +701,10 @@ SOURCE=.\trcoll.h # End Source File # Begin Source File +SOURCE=.\trnserr.h +# End Source File +# Begin Source File + SOURCE=.\tscoll.h # End Source File # Begin Source File diff --git a/icu4c/source/test/intltest/ittrans.cpp b/icu4c/source/test/intltest/ittrans.cpp index e947685ace9..4d5d1254b23 100644 --- a/icu4c/source/test/intltest/ittrans.cpp +++ b/icu4c/source/test/intltest/ittrans.cpp @@ -29,6 +29,7 @@ #include "transrt.h" #include "usettest.h" #include "jamotest.h" +#include "trnserr.h" #define CASE(id,test) case id: \ name = #test; \ @@ -52,6 +53,7 @@ void IntlTestTransliterator::runIndexedTest( int32_t index, UBool exec, const ch CASE(6, TransliteratorRoundTripTest); CASE(7, UnicodeSetTest); CASE(8, JamoTest); + CASE(9, TransliteratorErrorTest); default: name=""; break; } } diff --git a/icu4c/source/test/intltest/trnserr.cpp b/icu4c/source/test/intltest/trnserr.cpp new file mode 100644 index 00000000000..d2a86fdb530 --- /dev/null +++ b/icu4c/source/test/intltest/trnserr.cpp @@ -0,0 +1,256 @@ +/******************************************************************** + * COPYRIGHT: + * Copyright (c) 2001, 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 "ittrans.h" +#include "trnserr.h" +#include "unicode/utypes.h" +#include "unicode/translit.h" +#include "unicode/uniset.h" +#include "unicode/rbt.h" +#include "unicode/unitohex.h" +#include "unicode/hextouni.h" +#include "unicode/unifilt.h" +#include "unicode/cpdtrans.h" +#include "unicode/nultrans.h" +#include +#include +#include +#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, TestUniToHexErrors); + TESTCASE(3, TestRBTErrors); + 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==0 || U_FAILURE(status)){ + errln("FAIL: construction of Latin-Greek"); + return; + } + len = testString.length(); + stoppedAt = t->transliterate(testString, 0, 100); + if (stoppedAt != 0) { + errln("FAIL: Out of bounds check failed."); + } 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 != 0) + errln("FAIL: Out of bounds check failed."); + 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, (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."); + 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!=0 || U_SUCCESS(status)){ + delete t1; + errln("FAIL: construction of bogus ID \"LATINGREEK-GREEKLATIN\""); + } + status = U_ZERO_ERROR; + Transliterator* t2 = new RuleBasedTransliterator(newID, newIDRules, UTRANS_FORWARD, 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 != NULL || 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)) { + delete set1; + errln("FAIL: Created a UnicodeSet based on bad patterns."); + } +} + +void TransliteratorErrorTest::TestUniToHexErrors() { + UErrorCode status = U_ZERO_ERROR; + Transliterator *t = new UnicodeToHexTransliterator("", TRUE, NULL, status); + if (U_SUCCESS(status)) { + delete t; + errln("FAIL: Created a UnicodeToHexTransliterator with an empty pattern."); + } + status = U_ZERO_ERROR; + t = new UnicodeToHexTransliterator("\\x", TRUE, NULL, status); + if (U_SUCCESS(status)) { + delete t; + errln("FAIL: Created a UnicodeToHexTransliterator with a bad pattern."); + } + 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; + } + RuleBasedTransliterator *t = new RuleBasedTransliterator(id, rules, UTRANS_REVERSE, set, parseErr, status); + if (U_FAILURE(status)) { + errln("FAIL: Was not able to create a good RBT to test registration."); + 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("", NULL, status); + if (U_SUCCESS(status)) { + delete t; + errln("FAIL: Created a HexToUnicodeTransliterator with an empty pattern."); + } + status = U_ZERO_ERROR; + t = new HexToUnicodeTransliterator("\\x", NULL, status); + if (U_SUCCESS(status)) { + delete t; + errln("FAIL: Created a HexToUnicodeTransliterator with a bad pattern."); + } + 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; +} + + diff --git a/icu4c/source/test/intltest/trnserr.h b/icu4c/source/test/intltest/trnserr.h new file mode 100644 index 00000000000..e8eda6ca4e9 --- /dev/null +++ b/icu4c/source/test/intltest/trnserr.h @@ -0,0 +1,48 @@ +/******************************************************************** + * COPYRIGHT: + * Copyright (c) 2001, 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. +* +************************************************************************/ + + +#ifndef TRNSERR_H +#define TRNSERR_H + +#include "unicode/translit.h" +#include "intltest.h" + +/** + * @test + * @summary Error condition tests of Transliterator + */ +class TransliteratorErrorTest : public IntlTest { +public: + void runIndexedTest(int32_t index, UBool exec, const char* &name, char* par=NULL); + + /*Tests the returned error codes on all the APIs according to the API documentation. */ + void TestTransliteratorErrors(void); + + void TestUnicodeSetErrors(void); + + void TestUniToHexErrors(void); + + void TestRBTErrors(void); + + void TestHexToUniErrors(void); + +}; + +#endif + + + +