]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/test/intltest/citrtest.cpp
ICU-551.24.tar.gz
[apple/icu.git] / icuSources / test / intltest / citrtest.cpp
index 5a7e1049da0cd576666d4471e98eeb18b1cf3d3f..80a0cf71042edaf6db23de2dc0b27d9d67a6dd9d 100644 (file)
@@ -1,21 +1,26 @@
-/********************************************************************
+/****************************************************************************************
  * COPYRIGHT: 
- * Copyright (c) 1997-2002, International Business Machines Corporation and
+ * Copyright (c) 1997-2014, International Business Machines Corporation and
  * others. All Rights Reserved.
  * Modification History:
  *
  *   Date          Name        Description
  *   05/22/2000    Madhu       Added tests for testing new API for utf16 support and more
- **********************************************************************/
+ ****************************************************************************************/
 
 #include <string.h>
+#include "utypeinfo.h"  // for 'typeid' to work
+
 #include "unicode/chariter.h"
 #include "unicode/ustring.h"
 #include "unicode/unistr.h"
 #include "unicode/schriter.h"
 #include "unicode/uchriter.h"
 #include "unicode/uiter.h"
+#include "unicode/putil.h"
+#include "unicode/utf16.h"
 #include "citrtest.h"
+#include "cmemory.h"
 
 
 class  SCharacterIterator : public CharacterIterator {
@@ -36,16 +41,16 @@ public:
         text = newText;
     }
 
-    virtual void getText(UnicodeString& result){
+    virtual void getText(UnicodeString& result) {
         text.extract(0,text.length(),result);
     }
+    static UClassID getStaticClassID(void){ 
+        return (UClassID)(&fgClassID); 
+    }
     virtual UClassID getDynamicClassID(void) const{ 
         return getStaticClassID(); 
     }
 
-    static UClassID getStaticClassID(void){ 
-        return (UClassID)(&fgClassID); 
-    }
     virtual UBool operator==(const ForwardCharacterIterator& /*that*/) const{
         return TRUE;
     }
@@ -58,7 +63,7 @@ public:
     }
     virtual UChar nextPostInc(void){ return text.charAt(pos++);}
     virtual UChar32 next32PostInc(void){return text.char32At(pos++);}
-    virtual UBool hasNext(){ return TRUE;};
+    virtual UBool hasNext() { return TRUE;};
     virtual UChar first(){return DONE;};
     virtual UChar32 first32(){return DONE;};
     virtual UChar last(){return DONE;};
@@ -99,20 +104,20 @@ public:
         case kStart:
             pos = begin;
             if(delta > 0) {
-                UTF_FWD_N(text, pos, end, delta);
+                U16_FWD_N(text, pos, end, delta);
             }
             break;
         case kCurrent:
             if(delta > 0) {
-                UTF_FWD_N(text, pos, end, delta);
+                U16_FWD_N(text, pos, end, delta);
             } else {
-                UTF_BACK_N(text, begin, pos, -delta);
+                U16_BACK_N(text, begin, pos, -delta);
             }
             break;
         case kEnd:
             pos = end;
             if(delta < 0) {
-                UTF_BACK_N(text, begin, pos, -delta);
+                U16_BACK_N(text, begin, pos, -delta);
             }
             break;
         default:
@@ -121,7 +126,7 @@ public:
 
         return pos;
     };
-    virtual UBool hasPrevious(){return TRUE;};
+    virtual UBool hasPrevious() {return TRUE;};
 
   SCharacterIterator&  operator=(const SCharacterIterator&    that){
      text = that.text;
@@ -135,14 +140,12 @@ private:
 };
 const char SCharacterIterator::fgClassID=0;
 
-#define LENGTHOF(array) ((int32_t)(sizeof(array)/sizeof((array)[0])))
-
 CharIterTest::CharIterTest()
 {
 }
 void CharIterTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
 {
-    if (exec) logln("TestSuite LocaleTest: ");
+    if (exec) logln("TestSuite CharIterTest: ");
     switch (index) {
         case 0: name = "TestConstructionAndEquality"; if (exec) TestConstructionAndEquality(); break;
         case 1: name = "TestConstructionAndEqualityUChariter"; if (exec) TestConstructionAndEqualityUChariter(); break;
@@ -222,9 +225,9 @@ void CharIterTest::TestConstructionAndEquality() {
     if (test1->hashCode() != test5->hashCode())
         errln("hashCode() failed:  identical objects have different hash codes");
 
-       if(test1->getLength() != testText.length()){
-               errln("getLength of CharacterIterator failed");
-       }
+    if(test1->getLength() != testText.length()){
+        errln("getLength of CharacterIterator failed");
+    }
     test1->getText(result1);
     test1b->getText(result2);
     test1c->getText(result3);
@@ -663,7 +666,7 @@ void CharIterTest::TestIterationUChar32() {
         c=iter.first32PostInc();
         if(c != text.char32At(i))
             errln("first32PostInc failed.  Expected->%X Got->%X", text.char32At(i), c);
-        if(iter.getIndex() != UTF16_CHAR_LENGTH(c) + i)
+        if(iter.getIndex() != U16_LENGTH(c) + i)
             errln((UnicodeString)"getIndex() after first32PostInc() failed");
 
         iter.setToStart();
@@ -910,36 +913,51 @@ void CharIterTest::TestUCharIterator() {
 
     if(cIter.getIndex(&cIter, (enum UCharIteratorOrigin)-1) != -1)
     {
-      errln("error: UCharIterator(char iter).getIndex did not return error value");
+        errln("error: UCharIterator(char iter).getIndex did not return error value");
     }
 
     if(cIter.move(&cIter, 0, (enum UCharIteratorOrigin)-1) != -1)
     {
-      errln("error: UCharIterator(char iter).move did not return error value");
+        errln("error: UCharIterator(char iter).move did not return error value");
     }
 
 
     if(rIter.getIndex(&rIter, (enum UCharIteratorOrigin)-1) != -1)
     {
-      errln("error: UCharIterator(repl iter).getIndex did not return error value");
+        errln("error: UCharIterator(repl iter).getIndex did not return error value");
     }
 
     if(rIter.move(&rIter, 0, (enum UCharIteratorOrigin)-1) != -1)
     {
-      errln("error: UCharIterator(repl iter).move did not return error value");
+        errln("error: UCharIterator(repl iter).move did not return error value");
     }
 
 
     if(sIter.getIndex(&sIter, (enum UCharIteratorOrigin)-1) != -1)
     {
-      errln("error: UCharIterator(string iter).getIndex did not return error value");
+        errln("error: UCharIterator(string iter).getIndex did not return error value");
     }
 
     if(sIter.move(&sIter, 0, (enum UCharIteratorOrigin)-1) != -1)
     {
-      errln("error: UCharIterator(string iter).move did not return error value");
+        errln("error: UCharIterator(string iter).move did not return error value");
     }
 
+    /* Testing function coverage on bad input */
+    UErrorCode status = U_ZERO_ERROR;
+    uiter_setString(&sIter, NULL, 1);
+    uiter_setState(&sIter, 1, &status);
+    if (status != U_UNSUPPORTED_ERROR) {
+        errln("error: uiter_setState returned %s instead of U_UNSUPPORTED_ERROR", u_errorName(status));
+    }
+    status = U_ZERO_ERROR;
+    uiter_setState(NULL, 1, &status);
+    if (status != U_ILLEGAL_ARGUMENT_ERROR) {
+        errln("error: uiter_setState returned %s instead of U_ILLEGAL_ARGUMENT_ERROR", u_errorName(status));
+    }
+    if (uiter_getState(&sIter) != UITER_NO_STATE) {
+        errln("error: uiter_getState did not return UITER_NO_STATE on bad input");
+    }
 }
 
 // subclass test, and completing API coverage -------------------------------
@@ -948,7 +966,7 @@ class SubCharIter : public CharacterIterator {
 public:
     // public default constructor, to get coverage of CharacterIterator()
     SubCharIter() : CharacterIterator() {
-        textLength=end=LENGTHOF(s);
+        textLength=end=UPRV_LENGTHOF(s);
         s[0]=0x61;      // 'a'
         s[1]=0xd900;    // U+50400
         s[2]=0xdd00;
@@ -957,7 +975,7 @@ public:
 
     // useful stuff, mostly dummy but testing coverage and subclassability
     virtual UChar nextPostInc() {
-        if(pos<LENGTHOF(s)) {
+        if(pos<UPRV_LENGTHOF(s)) {
             return s[pos++];
         } else {
             return DONE;
@@ -965,9 +983,9 @@ public:
     }
 
     virtual UChar32 next32PostInc() {
-        if(pos<LENGTHOF(s)) {
+        if(pos<UPRV_LENGTHOF(s)) {
             UChar32 c;
-            U16_NEXT(s, pos, LENGTHOF(s), c);
+            U16_NEXT(s, pos, UPRV_LENGTHOF(s), c);
             return c;
         } else {
             return DONE;
@@ -975,7 +993,7 @@ public:
     }
 
     virtual UBool hasNext() {
-        return pos<LENGTHOF(s);
+        return pos<UPRV_LENGTHOF(s);
     }
 
     virtual UChar first() {
@@ -986,15 +1004,15 @@ public:
     virtual UChar32 first32() {
         UChar32 c;
         pos=0;
-        U16_NEXT(s, pos, LENGTHOF(s), c);
+        U16_NEXT(s, pos, UPRV_LENGTHOF(s), c);
         pos=0;
         return c;
     }
 
     virtual UChar setIndex(int32_t position) {
-        if(0<=position && position<=LENGTHOF(s)) {
+        if(0<=position && position<=UPRV_LENGTHOF(s)) {
             pos=position;
-            if(pos<LENGTHOF(s)) {
+            if(pos<UPRV_LENGTHOF(s)) {
                 return s[pos];
             }
         }
@@ -1002,11 +1020,11 @@ public:
     }
 
     virtual UChar32 setIndex32(int32_t position) {
-        if(0<=position && position<=LENGTHOF(s)) {
+        if(0<=position && position<=UPRV_LENGTHOF(s)) {
             pos=position;
-            if(pos<LENGTHOF(s)) {
+            if(pos<UPRV_LENGTHOF(s)) {
                 UChar32 c;
-                U16_GET(s, 0, pos, LENGTHOF(s), c);
+                U16_GET(s, 0, pos, UPRV_LENGTHOF(s), c);
                 return c;
             }
         }
@@ -1014,7 +1032,7 @@ public:
     }
 
     virtual UChar current() const {
-        if(pos<LENGTHOF(s)) {
+        if(pos<UPRV_LENGTHOF(s)) {
             return s[pos];
         } else {
             return DONE;
@@ -1022,9 +1040,9 @@ public:
     }
 
     virtual UChar32 current32() const {
-        if(pos<LENGTHOF(s)) {
+        if(pos<UPRV_LENGTHOF(s)) {
             UChar32 c;
-            U16_GET(s, 0, pos, LENGTHOF(s), c);
+            U16_GET(s, 0, pos, UPRV_LENGTHOF(s), c);
             return c;
         } else {
             return DONE;
@@ -1032,7 +1050,7 @@ public:
     }
 
     virtual UChar next() {
-        if(pos<LENGTHOF(s) && ++pos<LENGTHOF(s)) {
+        if(pos<UPRV_LENGTHOF(s) && ++pos<UPRV_LENGTHOF(s)) {
             return s[pos];
         } else {
             return DONE;
@@ -1040,13 +1058,13 @@ public:
     }
 
     virtual UChar32 next32() {
-        if(pos<LENGTHOF(s)) {
-            U16_FWD_1(s, pos, LENGTHOF(s));
+        if(pos<UPRV_LENGTHOF(s)) {
+            U16_FWD_1(s, pos, UPRV_LENGTHOF(s));
         }
-        if(pos<LENGTHOF(s)) {
+        if(pos<UPRV_LENGTHOF(s)) {
             UChar32 c;
             int32_t i=pos;
-            U16_NEXT(s, i, LENGTHOF(s), c);
+            U16_NEXT(s, i, UPRV_LENGTHOF(s), c);
             return c;
         } else {
             return DONE;
@@ -1058,14 +1076,14 @@ public:
     }
 
     virtual void getText(UnicodeString &result) {
-        result.setTo(s, LENGTHOF(s));
+        result.setTo(s, UPRV_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;
+            (typeid(*this)==typeid(that) && pos==((SubCharIter &)that).pos);
     }
 
     virtual int32_t hashCode() const {
@@ -1101,14 +1119,14 @@ public:
     }
 
     // RTTI
-    virtual UClassID getDynamicClassID() const {
-        return getStaticClassID();
-    }
-
     static UClassID getStaticClassID() {
         return (UClassID)(&fgClassID);
     }
 
+    virtual UClassID getDynamicClassID() const {
+        return getStaticClassID();
+    }
+
 private:
     // dummy string data
     UChar s[4];