X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/46f4442e9a5a4f3b98b7c1083586332f6a8a99a4..249c4c5ea9376c24572daf9c2effa7484a282f14:/icuSources/test/iotest/stream.cpp diff --git a/icuSources/test/iotest/stream.cpp b/icuSources/test/iotest/stream.cpp index c700437a..0ffc6239 100644 --- a/icuSources/test/iotest/stream.cpp +++ b/icuSources/test/iotest/stream.cpp @@ -1,10 +1,12 @@ +// © 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html /* ********************************************************************** -* Copyright (C) 2002-2006, International Business Machines +* Copyright (C) 2002-2016, International Business Machines * Corporation and others. All Rights Reserved. ********************************************************************** * file name: iotest.cpp -* encoding: US-ASCII +* encoding: UTF-8 * tab size: 8 (not used) * indentation:4 * @@ -18,9 +20,9 @@ #include "unicode/ucnv.h" #include "unicode/ustring.h" #include "ustr_cnv.h" +#include "cmemory.h" #include "iotest.h" -#if U_IOSTREAM_SOURCE >= 199711 #if defined(__GNUC__) && __GNUC__ >= 4 #define USE_SSTREAM 1 #include @@ -29,16 +31,13 @@ #include #endif #include +#include using namespace std; -#elif U_IOSTREAM_SOURCE >= 198506 -#include -#include -#endif #include U_CDECL_BEGIN -#ifdef U_WINDOWS +#if U_PLATFORM_USES_ONLY_WIN32_API const UChar NEW_LINE[] = {0x0d,0x0a,0}; const char C_NEW_LINE[] = {0x0d,0x0a,0}; #define UTF8_NEW_LINE "\x0d\x0a" @@ -52,7 +51,6 @@ U_CDECL_END U_CDECL_BEGIN static void U_CALLCONV TestStream(void) { -#if U_IOSTREAM_SOURCE >= 198506 const UChar thisMu[] = { 0x74, 0x48, 0x69, 0x73, 0x3BC, 0}; const UChar mu[] = { 0x6D, 0x75, 0}; UnicodeString str1 = UNICODE_STRING_SIMPLE("str1"); @@ -78,7 +76,7 @@ static void U_CALLCONV TestStream(void) return; } ucnv_close(defConv); - strncpy(defConvName, ucnv_getDefaultName(), sizeof(defConvName)/sizeof(defConvName[0])); + strncpy(defConvName, ucnv_getDefaultName(), UPRV_LENGTHOF(defConvName)); ucnv_setDefaultName("UTF-8"); static const char * const TESTSTRING = "\x20\x74\x48\x69\x73\xCE\xBC\xE2\x80\x82\x20\x6D\x75\x20\x77\x6F\x72\x6C\x64"; @@ -107,12 +105,12 @@ static void U_CALLCONV TestStream(void) inTestStream >> inStr >> inStr2; if (inStr.compare(thisMu) != 0) { - u_austrncpy(inStrC, inStr.getBuffer(), inStr.length()); + u_austrncpy(inStrC, toUCharPtr(inStr.getBuffer()), inStr.length()); inStrC[inStr.length()] = 0; log_err("Got: \"%s\", Expected: \"tHis\\u03BC\"\n", inStrC); } if (inStr2.compare(mu) != 0) { - u_austrncpy(inStrC, inStr.getBuffer(), inStr.length()); + u_austrncpy(inStrC, toUCharPtr(inStr.getBuffer()), inStr.length()); inStrC[inStr.length()] = 0; log_err("Got: \"%s\", Expected: \"mu\"\n", inStrC); } @@ -124,10 +122,61 @@ static void U_CALLCONV TestStream(void) log_err("Can't get default converter"); return; } - ucnv_close(defConv); + + /* Test formatting when using '<<' and UnicodeString */ +#ifdef USE_SSTREAM + ostringstream outFormatStream; +#else + char testFormatStreamBuf[512]; + memset(testFormatStreamBuf, 0, sizeof(testFormatStreamBuf)); + ostrstream outFormatStream(testFormatStreamBuf, sizeof(testFormatStreamBuf)); +#endif + UnicodeString ustr("string"); + + outFormatStream << "1234567890" << setw(10) << left << ustr << " " << "0123456789"; + +#ifdef USE_SSTREAM + tempStr = outFormatStream.str(); + const char *testFormatStreamBuf = tempStr.c_str(); +#endif + const char *format_test_expected = "1234567890string 0123456789"; + if (strcmp(format_test_expected, testFormatStreamBuf) != 0) { + log_err("UnicodeString format test using << operator Got: '%s' Expected: '%s'\n", testFormatStreamBuf, format_test_expected); + } + + /* Test large buffer (size > 200) when using '<<' and UnicodeString */ +#ifdef USE_SSTREAM + ostringstream outLargeStream; #else - log_info("U_IOSTREAM_SOURCE is disabled\n"); + char testLargeStreamBuf[512]; + memset(testLargeStreamBuf, 0, sizeof(testLargeStreamBuf)); + ostrstream outLargeStream(testLargeStreamBuf, sizeof(testLargeStreamBuf)); #endif + UChar large_array[200]; + int32_t large_array_length = UPRV_LENGTHOF(large_array); + for (int32_t i = 0; i < large_array_length; i++) { + large_array[i] = 0x41; + } + UnicodeString large_array_unistr(large_array, large_array_length); + + outLargeStream << large_array_unistr; + +#ifdef USE_SSTREAM + string tmpString = outLargeStream.str(); + const char *testLargeStreamBuf = tmpString.c_str(); +#endif + char expectedLargeStreamBuf[300]; + int32_t expectedBufLength = sizeof(expectedLargeStreamBuf); + + ucnv_fromUChars(defConv, expectedLargeStreamBuf, expectedBufLength, large_array, large_array_length, &status); + if (U_SUCCESS(status)) { + if (strcmp(testLargeStreamBuf, expectedLargeStreamBuf) != 0) { + log_err("Large UnicodeString operator << output incorrect.\n"); + } + } else { + log_err("Error converting string for large stream buffer testing.\n"); + } + ucnv_close(defConv); } #define IOSTREAM_GOOD_SHIFT 3 @@ -168,10 +217,10 @@ printBits(const iostream& stream) void testString( - UnicodeString& str, + UnicodeString& str, const char* testString, - const char* expectedString, - int32_t expectedStatus) + const UChar* expectedString, + int32_t expectedStatus) { #ifdef USE_SSTREAM stringstream sstrm; @@ -191,13 +240,14 @@ testString( if (getBitStatus(sstrm) != expectedStatus) { printBits(sstrm); - log_err("Expected status %d, Got %d. See verbose output for details\n", getBitStatus(sstrm), expectedStatus); + log_err("Expected status %d, Got %d. See verbose output for details\n", expectedStatus, getBitStatus(sstrm)); } if (str != UnicodeString(expectedString)) { log_err("Did not get expected results from \"%s\", expected \"%s\"\n", testString, expectedString); } } + static void U_CALLCONV TestStreamEOF(void) { UnicodeString dest; @@ -229,14 +279,41 @@ static void U_CALLCONV TestStreamEOF(void) log_verbose("Testing operator >> for UnicodeString...\n"); + /* The test cases needs to be converted to the default codepage. However, the stream operator needs char* so U_STRING_* is called. */ + U_STRING_DECL(testCase1, "", 0); + U_STRING_INIT(testCase1, "", 0); + U_STRING_DECL(testCase2, "foo", 3); + U_STRING_INIT(testCase2, "foo", 3); + U_STRING_DECL(testCase3, " ", 3); + U_STRING_INIT(testCase3, " ", 3); + U_STRING_DECL(testCase4, " bar", 6); + U_STRING_INIT(testCase4, " bar", 6); + U_STRING_DECL(testCase5, "bar ", 6); + U_STRING_INIT(testCase5, "bar ", 6); + U_STRING_DECL(testCase6, " bar ", 9); + U_STRING_INIT(testCase6, " bar ", 9); + + + U_STRING_DECL(expectedResultA, "", 0); + U_STRING_INIT(expectedResultA, "", 0); + U_STRING_DECL(expectedResultB, "foo", 3); + U_STRING_INIT(expectedResultB, "foo", 3); + U_STRING_DECL(expectedResultC, "unchanged", 9); + U_STRING_INIT(expectedResultC, "unchanged", 9); + U_STRING_DECL(expectedResultD, "bar", 3); + U_STRING_INIT(expectedResultD, "bar", 3); + + UnicodeString UStr; - testString(UStr, "", "", IOSTREAM_EOF|IOSTREAM_FAIL); - testString(UStr, "foo", "foo", IOSTREAM_EOF); - UStr = "unchanged"; - testString(UStr, " ", "unchanged", IOSTREAM_EOF|IOSTREAM_FAIL); - testString(UStr, " bar", "bar", IOSTREAM_EOF); - testString(UStr, "bar ", "bar", IOSTREAM_GOOD); - testString(UStr, " bar ", "bar", IOSTREAM_GOOD); + UnicodeString expectedResults; + char testcase[10]; + testString(UStr, u_austrcpy(testcase, testCase1), expectedResultA, IOSTREAM_EOF|IOSTREAM_FAIL); + testString(UStr, u_austrcpy(testcase, testCase2), expectedResultB, IOSTREAM_EOF); + UStr = UnicodeString(expectedResultC); + testString(UStr, u_austrcpy(testcase, testCase3), expectedResultC, IOSTREAM_EOF|IOSTREAM_FAIL); + testString(UStr, u_austrcpy(testcase, testCase4), expectedResultD, IOSTREAM_EOF); + testString(UStr, u_austrcpy(testcase, testCase5), expectedResultD, IOSTREAM_GOOD); + testString(UStr, u_austrcpy(testcase, testCase6), expectedResultD, IOSTREAM_GOOD); } U_CDECL_END @@ -244,4 +321,3 @@ U_CFUNC void addStreamTests(TestNode** root) { addTest(root, &TestStream, "stream/TestStream"); addTest(root, &TestStreamEOF, "stream/TestStreamEOF"); } -