From 230d313ee1736ceec3f1fcae1a6f2e82470d80a4 Mon Sep 17 00:00:00 2001 From: Markus Scherer Date: Tue, 26 Nov 2002 21:18:47 +0000 Subject: [PATCH] ICU-2118 improve *CharacterIterator API coverage X-SVN-Rev: 10382 --- icu4c/source/common/chariter.cpp | 6 +- icu4c/source/common/unicode/chariter.h | 4 +- icu4c/source/test/intltest/citrtest.cpp | 231 +++++++++++++++++++++++- icu4c/source/test/intltest/citrtest.h | 4 +- 4 files changed, 240 insertions(+), 5 deletions(-) diff --git a/icu4c/source/common/chariter.cpp b/icu4c/source/common/chariter.cpp index 23711eb8953..064f5332734 100644 --- a/icu4c/source/common/chariter.cpp +++ b/icu4c/source/common/chariter.cpp @@ -1,6 +1,6 @@ /* ********************************************************************** -* Copyright (C) 1999-2001, International Business Machines +* Copyright (C) 1999-2002, International Business Machines * Corporation and others. All Rights Reserved. ********************************************************************** */ @@ -9,6 +9,10 @@ U_NAMESPACE_BEGIN +CharacterIterator::CharacterIterator() +: textLength(0), pos(0), begin(0), end(0) { +} + CharacterIterator::CharacterIterator(int32_t length) : textLength(length), pos(0), begin(0), end(length) { if(textLength < 0) { diff --git a/icu4c/source/common/unicode/chariter.h b/icu4c/source/common/unicode/chariter.h index c2bab79db3e..a2ad41a6127 100644 --- a/icu4c/source/common/unicode/chariter.h +++ b/icu4c/source/common/unicode/chariter.h @@ -1,7 +1,7 @@ /* ******************************************************************** * -* Copyright (C) 1997-2001, International Business Machines +* Copyright (C) 1997-2002, International Business Machines * Corporation and others. All Rights Reserved. * ******************************************************************** @@ -605,7 +605,7 @@ public: virtual void getText(UnicodeString& result) = 0; protected: - CharacterIterator() {} + CharacterIterator(); CharacterIterator(int32_t length); CharacterIterator(int32_t length, int32_t position); CharacterIterator(int32_t length, int32_t textBegin, int32_t textEnd, int32_t position); diff --git a/icu4c/source/test/intltest/citrtest.cpp b/icu4c/source/test/intltest/citrtest.cpp index f12cd57e353..3c1007c7787 100644 --- a/icu4c/source/test/intltest/citrtest.cpp +++ b/icu4c/source/test/intltest/citrtest.cpp @@ -1,6 +1,6 @@ /******************************************************************** * COPYRIGHT: - * Copyright (c) 1997-2001, International Business Machines Corporation and + * Copyright (c) 1997-2002, International Business Machines Corporation and * others. All Rights Reserved. * Modification History: * @@ -17,6 +17,8 @@ #include "unicode/uiter.h" #include "citrtest.h" +#define LENGTHOF(array) (sizeof(array)/sizeof((array)[0])) + CharIterTest::CharIterTest() { } @@ -30,6 +32,7 @@ void CharIterTest::runIndexedTest( int32_t index, UBool exec, const char* &name, case 2: name = "TestIteration"; if (exec) TestIteration(); break; case 3: name = "TestIterationUChar32"; if (exec) TestIterationUChar32(); break; case 4: name = "TestUCharIterator"; if (exec) TestUCharIterator(); break; + case 5: name = "TestCharIteratorSubClasses"; if (exec) TestCharIteratorSubClasses(); break; default: name = ""; break; //needed to end loop } @@ -786,3 +789,229 @@ void CharIterTest::TestUCharIterator() { } } + +// subclass test, and completing API coverage ------------------------------- + +class SubCharIter : public CharacterIterator { +public: + // public default constructor, to get coverage of CharacterIterator() + SubCharIter() : CharacterIterator() { + textLength=end=LENGTHOF(s); + s[0]=0x61; // 'a' + s[1]=0xd900; // U+50400 + s[2]=0xdd00; + s[3]=0x2029; // PS + } + + // useful stuff, mostly dummy but testing coverage and subclassability + virtual UChar nextPostInc() { + if(pos0; + } + + virtual void getText(UnicodeString &result) { + result.setTo(s, LENGTHOF(s)); + } + + // dummy implementations of other pure virtual base class functions + virtual UBool operator==(const ForwardCharacterIterator &that) const { + return + this==&that || + getDynamicClassID()==that.getDynamicClassID() && pos==((SubCharIter &)that).pos; + } + + virtual int32_t hashCode() const { + return 2; + } + + virtual CharacterIterator *clone() const { + return NULL; + } + + virtual UChar last() { + return 0; + } + + virtual UChar32 last32() { + return 0; + } + + virtual UChar previous() { + return 0; + } + + virtual UChar32 previous32() { + return 0; + } + + virtual int32_t move(int32_t delta, EOrigin origin) { + return 0; + } + + virtual int32_t move32(int32_t delta, EOrigin origin) { + return 0; + } + + // RTTI + virtual UClassID getDynamicClassID() const { + return getStaticClassID(); + } + + static UClassID getStaticClassID() { + return (UClassID)(&fgClassID); + } + +private: + // dummy string data + UChar s[4]; + + static const char fgClassID; +}; + +const char SubCharIter::fgClassID = 0; + +class SubStringCharIter : public StringCharacterIterator { +public: + SubStringCharIter() { + setText(UNICODE_STRING("abc", 3)); + } +}; + +class SubUCharCharIter : public UCharCharacterIterator { +public: + SubUCharCharIter() { + setText(u, 3); + } + +private: + static const UChar u[3]; +}; + +const UChar SubUCharCharIter::u[3]={ 0x61, 0x62, 0x63 }; + +void CharIterTest::TestCharIteratorSubClasses() { + SubCharIter *p; + + // coverage - call functions that are not otherwise tested + // first[32]PostInc() are default implementations that are overridden + // in ICU's own CharacterIterator subclasses + p=new SubCharIter; + if(p->firstPostInc()!=0x61) { + errln("SubCharIter.firstPosInc() failed\n"); + } + delete p; + + p=new SubCharIter[2]; + if(p[1].first32PostInc()!=0x61) { + errln("SubCharIter.first32PosInc() failed\n"); + } + delete [] p; + + // coverage: StringCharacterIterator default constructor + SubStringCharIter sci; + if(sci.firstPostInc()!=0x61) { + errln("SubStringCharIter.firstPostInc() failed\n"); + } + + // coverage: UCharCharacterIterator default constructor + SubUCharCharIter uci; + if(uci.firstPostInc()!=0x61) { + errln("SubUCharCharIter.firstPostInc() failed\n"); + } +} diff --git a/icu4c/source/test/intltest/citrtest.h b/icu4c/source/test/intltest/citrtest.h index ff637d6e94c..4faf2bf2004 100644 --- a/icu4c/source/test/intltest/citrtest.h +++ b/icu4c/source/test/intltest/citrtest.h @@ -1,6 +1,6 @@ /******************************************************************** * COPYRIGHT: - * Copyright (c) 1997-2001, International Business Machines Corporation and + * Copyright (c) 1997-2002, International Business Machines Corporation and * others. All Rights Reserved. ********************************************************************/ @@ -38,6 +38,8 @@ public: void TestUCharIterator(); void TestUCharIterator(UCharIterator *iter, CharacterIterator &ci, const char *moves, const char *which); + + void TestCharIteratorSubClasses(); }; #endif