/********************************************************************
* COPYRIGHT:
- * Copyright (c) 1997-2010, International Business Machines Corporation and
+ * Copyright (c) 1997-2012, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
#include "ustrtest.h"
+#include "unicode/appendable.h"
#include "unicode/std_string.h"
#include "unicode/unistr.h"
#include "unicode/uchar.h"
#include "unicode/locid.h"
#include "unicode/ucnv.h"
#include "unicode/uenum.h"
+#include "unicode/utf16.h"
#include "cmemory.h"
#include "charstr.h"
#if 0
#include "unicode/ustream.h"
-#if U_IOSTREAM_SOURCE >= 199711
#include <iostream>
using namespace std;
-#elif U_IOSTREAM_SOURCE >= 198506
-#include <iostream.h>
-#endif
#endif
case 17: name = "TestUTF32"; if (exec) TestUTF32(); break;
case 18: name = "TestUTF8"; if (exec) TestUTF8(); break;
case 19: name = "TestReadOnlyAlias"; if (exec) TestReadOnlyAlias(); break;
+ case 20: name = "TestAppendable"; if (exec) TestAppendable(); break;
+ case 21: name = "TestUnicodeStringImplementsAppendable"; if (exec) TestUnicodeStringImplementsAppendable(); break;
+ case 22: name = "TestSizeofUnicodeString"; if (exec) TestSizeofUnicodeString(); break;
+ case 23: name = "TestStartsWithAndEndsWithNulTerminated"; if (exec) TestStartsWithAndEndsWithNulTerminated(); break;
default: name = ""; break; //needed to end loop
}
errln("operator+=() failed: expected \"" + expectedValue + "\"\n,got \"" + test2 + "\"");
if (test1.length() != 70)
- errln("length() failed: expected 70, got " + test1.length());
+ errln(UnicodeString("length() failed: expected 70, got ") + test1.length());
if (test2.length() != 30)
- errln("length() failed: expected 30, got " + test2.length());
+ errln(UnicodeString("length() failed: expected 30, got ") + test2.length());
UnicodeString test3;
test3.append((UChar32)0x20402);
errln((UnicodeString)"append failed for UChar32, expected \"\\\\ud841\\\\udc02\", got " + prettify(test3));
}
if(test3.length() != 2){
- errln("append or length failed for UChar32, expected 2, got " + test3.length());
+ errln(UnicodeString("append or length failed for UChar32, expected 2, got ") + test3.length());
}
test3.append((UChar32)0x0074);
if(test3 != CharsToUnicodeString("\\uD841\\uDC02t")){
(startPos = test1.indexOf(test2, startPos)) != -1 ? (++occurrences, startPos += 4) : 0)
;
if (occurrences != 6)
- errln("indexOf failed: expected to find 6 occurrences, found " + occurrences);
-
+ errln(UnicodeString("indexOf failed: expected to find 6 occurrences, found ") + occurrences);
+
for ( occurrences = 0, startPos = 10;
startPos != -1 && startPos < test1.length();
(startPos = test1.indexOf(test2, startPos)) != -1 ? (++occurrences, startPos += 4) : 0)
;
if (occurrences != 4)
- errln("indexOf with starting offset failed: expected to find 4 occurrences, found " + occurrences);
+ errln(UnicodeString("indexOf with starting offset failed: "
+ "expected to find 4 occurrences, found ") + occurrences);
int32_t endPos = 28;
for ( occurrences = 0, startPos = 5;
(startPos = test1.indexOf(test2, startPos, endPos - startPos)) != -1 ? (++occurrences, startPos += 4) : 0)
;
if (occurrences != 4)
- errln("indexOf with starting and ending offsets failed: expected to find 4 occurrences, found " + occurrences);
+ errln(UnicodeString("indexOf with starting and ending offsets failed: "
+ "expected to find 4 occurrences, found ") + occurrences);
//using UChar32 string
for ( startPos=0, occurrences=0;
(startPos = test3.indexOf(test4, startPos)) != -1 ? (++occurrences, startPos += 2) : 0)
;
if (occurrences != 2)
- errln("indexOf failed: expected to find 2 occurrences, found " + occurrences);
+ errln(UnicodeString("indexOf failed: expected to find 2 occurrences, found ") + occurrences);
//---
for ( occurrences = 0, startPos = 0;
(startPos = test1.indexOf(testChar, startPos)) != -1 ? (++occurrences, startPos += 1) : 0)
;
if (occurrences != 16)
- errln("indexOf with character failed: expected to find 16 occurrences, found " + occurrences);
+ errln(UnicodeString("indexOf with character failed: "
+ "expected to find 16 occurrences, found ") + occurrences);
for ( occurrences = 0, startPos = 10;
startPos != -1 && startPos < test1.length();
(startPos = test1.indexOf(testChar, startPos)) != -1 ? (++occurrences, startPos += 1) : 0)
;
if (occurrences != 12)
- errln("indexOf with character & start offset failed: expected to find 12 occurrences, found " + occurrences);
+ errln(UnicodeString("indexOf with character & start offset failed: "
+ "expected to find 12 occurrences, found ") + occurrences);
for ( occurrences = 0, startPos = 5, endPos = 28;
startPos != -1 && startPos < test1.length();
(startPos = test1.indexOf(testChar, startPos, endPos - startPos)) != -1 ? (++occurrences, startPos += 1) : 0)
;
if (occurrences != 10)
- errln("indexOf with character & start & end offsets failed: expected to find 10 occurrences, found " + occurrences);
+ errln(UnicodeString("indexOf with character & start & end offsets failed: "
+ "expected to find 10 occurrences, found ") + occurrences);
//testing for UChar32
UnicodeString subString;
(startPos = test1.lastIndexOf(test2, 5, startPos - 5)) != -1 ? ++occurrences : 0)
;
if (occurrences != 4)
- errln("lastIndexOf with starting and ending offsets failed: expected to find 4 occurrences, found " + occurrences);
+ errln(UnicodeString("lastIndexOf with starting and ending offsets failed: "
+ "expected to find 4 occurrences, found ") + occurrences);
for ( occurrences = 0, startPos = 32;
startPos != -1;
(startPos = test1.lastIndexOf(testChar, 5, startPos - 5)) != -1 ? ++occurrences : 0)
;
if (occurrences != 11)
- errln("lastIndexOf with character & start & end offsets failed: expected to find 11 occurrences, found " + occurrences);
+ errln(UnicodeString("lastIndexOf with character & start & end offsets failed: "
+ "expected to find 11 occurrences, found ") + occurrences);
//testing UChar32
startPos=test3.length();
}
}
+void
+UnicodeStringTest::TestStartsWithAndEndsWithNulTerminated() {
+ UnicodeString test("abcde");
+ const UChar ab[] = { 0x61, 0x62, 0 };
+ const UChar de[] = { 0x64, 0x65, 0 };
+ assertTrue("abcde.startsWith(ab, -1)", test.startsWith(ab, -1));
+ assertTrue("abcde.startsWith(ab, 0, -1)", test.startsWith(ab, 0, -1));
+ assertTrue("abcde.endsWith(de, -1)", test.endsWith(de, -1));
+ assertTrue("abcde.endsWith(de, 0, -1)", test.endsWith(de, 0, -1));
+}
+
void
UnicodeStringTest::TestFindAndReplace()
{
// test the UChar32 constructor
UnicodeString c32Test((UChar32)0x10ff2a);
- if( c32Test.length() != UTF_CHAR_LENGTH(0x10ff2a) ||
+ if( c32Test.length() != U16_LENGTH(0x10ff2a) ||
c32Test.char32At(c32Test.length() - 1) != 0x10ff2a
) {
errln("The UnicodeString(UChar32) constructor does not work with a 0x10ff2a filler");
// test the (new) capacity constructor
UnicodeString capTest(5, (UChar32)0x2a, 5);
- if( capTest.length() != 5 * UTF_CHAR_LENGTH(0x2a) ||
+ if( capTest.length() != 5 * U16_LENGTH(0x2a) ||
capTest.char32At(0) != 0x2a ||
capTest.char32At(4) != 0x2a
) {
}
capTest = UnicodeString(5, (UChar32)0x10ff2a, 5);
- if( capTest.length() != 5 * UTF_CHAR_LENGTH(0x10ff2a) ||
+ if( capTest.length() != 5 * U16_LENGTH(0x10ff2a) ||
capTest.char32At(0) != 0x10ff2a ||
capTest.char32At(4) != 0x10ff2a
) {
*
* Define a (bogus) UnicodeString class in another namespace and check for ambiguity.
*/
-#if U_HAVE_NAMESPACE
namespace bogus {
class UnicodeString {
public:
int32_t i;
};
}
-#endif
void
UnicodeStringTest::TestNameSpace() {
-#if U_HAVE_NAMESPACE
// Provoke name collision unless the UnicodeString macros properly
// qualify the icu::UnicodeString class.
using namespace bogus;
if(s4.length()!=9) {
errln("Something wrong with UnicodeString::operator+().");
}
-#endif
}
void
errln("UnicodeString::fromUTF8(StringPiece) did not create the expected string.");
}
#if U_HAVE_STD_STRING
- U_STD_NSQ string utf8_string((const char *)utf8, sizeof(utf8));
+ std::string utf8_string((const char *)utf8, sizeof(utf8));
UnicodeString from8b = UnicodeString::fromUTF8(utf8_string);
if(from8b != expected) {
errln("UnicodeString::fromUTF8(std::string) did not create the expected string.");
}
#if U_HAVE_STD_STRING
// Initial contents for testing that toUTF8String() appends.
- U_STD_NSQ string result8 = "-->";
- U_STD_NSQ string expected8 = "-->" + U_STD_NSQ string((const char *)expected_utf8, sizeof(expected_utf8));
+ std::string result8 = "-->";
+ std::string expected8 = "-->" + std::string((const char *)expected_utf8, sizeof(expected_utf8));
// Use the return value just for testing.
- U_STD_NSQ string &result8r = us.toUTF8String(result8);
+ std::string &result8r = us.toUTF8String(result8);
if(result8r != expected8 || &result8r != &result8) {
errln("UnicodeString::toUTF8String() did not create the expected string.");
}
errln("UnicodeString.setToBogus().tempSubStringBetween(8, 18) failed");
}
}
+
+void
+UnicodeStringTest::doTestAppendable(UnicodeString &dest, Appendable &app) {
+ static const UChar cde[3]={ 0x63, 0x64, 0x65 };
+ static const UChar fg[3]={ 0x66, 0x67, 0 };
+ if(!app.reserveAppendCapacity(12)) {
+ errln("Appendable.reserve(12) failed");
+ }
+ app.appendCodeUnit(0x61);
+ app.appendCodePoint(0x62);
+ app.appendCodePoint(0x50000);
+ app.appendString(cde, 3);
+ app.appendString(fg, -1);
+ UChar scratch[3];
+ int32_t capacity=-1;
+ UChar *buffer=app.getAppendBuffer(3, 3, scratch, 3, &capacity);
+ if(capacity<3) {
+ errln("Appendable.getAppendBuffer(min=3) returned capacity=%d<3", (int)capacity);
+ return;
+ }
+ static const UChar hij[3]={ 0x68, 0x69, 0x6a };
+ u_memcpy(buffer, hij, 3);
+ app.appendString(buffer, 3);
+ if(dest!=UNICODE_STRING_SIMPLE("ab\\U00050000cdefghij").unescape()) {
+ errln("Appendable.append(...) failed");
+ }
+ buffer=app.getAppendBuffer(0, 3, scratch, 3, &capacity);
+ if(buffer!=NULL || capacity!=0) {
+ errln("Appendable.getAppendBuffer(min=0) failed");
+ }
+ capacity=1;
+ buffer=app.getAppendBuffer(3, 3, scratch, 2, &capacity);
+ if(buffer!=NULL || capacity!=0) {
+ errln("Appendable.getAppendBuffer(scratch<min) failed");
+ }
+}
+
+class SimpleAppendable : public Appendable {
+public:
+ explicit SimpleAppendable(UnicodeString &dest) : str(dest) {}
+ virtual UBool appendCodeUnit(UChar c) { str.append(c); return TRUE; }
+ SimpleAppendable &reset() { str.remove(); return *this; }
+private:
+ UnicodeString &str;
+};
+
+void
+UnicodeStringTest::TestAppendable() {
+ UnicodeString dest;
+ SimpleAppendable app(dest);
+ doTestAppendable(dest, app);
+}
+
+void
+UnicodeStringTest::TestUnicodeStringImplementsAppendable() {
+ UnicodeString dest;
+ UnicodeStringAppendable app(dest);
+ doTestAppendable(dest, app);
+}
+
+void
+UnicodeStringTest::TestSizeofUnicodeString() {
+ // See the comments in unistr.h near the declaration of UnicodeString's fields.
+ size_t sizeofUniStr=sizeof(UnicodeString);
+ size_t expected;
+ switch(sizeof(void *)) {
+ case 4:
+ expected=32;
+ break;
+ case 8:
+ expected=40;
+ break;
+ default:
+ logln("This platform has neither 32-bit nor 64-bit pointers.");
+ return;
+ }
+ if(expected!=sizeofUniStr) {
+ errln("sizeof(UnicodeString)=%d, expected %d", (int)sizeofUniStr, (int)expected);
+ }
+}