1/* 2********************************************************************** 3* Copyright (C) 2002-2011, International Business Machines 4* Corporation and others. All Rights Reserved. 5********************************************************************** 6* file name: iotest.cpp 7* encoding: US-ASCII 8* tab size: 8 (not used) 9* indentation:4 10* 11* created on: 2002feb21 12* created by: George Rhoten 13*/ 14 15 16#include "unicode/ustream.h" 17 18#include "unicode/ucnv.h" 19#include "unicode/ustring.h" 20#include "ustr_cnv.h" 21#include "iotest.h" 22 23#if U_IOSTREAM_SOURCE >= 199711 24#if defined(__GNUC__) && __GNUC__ >= 4 25#define USE_SSTREAM 1 26#include <sstream> 27#else 28// <strstream> is deprecated on some platforms, and the compiler complains very loudly if you use it. 29#include <strstream> 30#endif 31#include <fstream> 32#include <iomanip> 33using namespace std; 34 35#include <string.h> 36 37U_CDECL_BEGIN 38#if U_PLATFORM_USES_ONLY_WIN32_API 39const UChar NEW_LINE[] = {0x0d,0x0a,0}; 40const char C_NEW_LINE[] = {0x0d,0x0a,0}; 41#define UTF8_NEW_LINE "\x0d\x0a" 42#else 43const UChar NEW_LINE[] = {0x0a,0}; 44const char C_NEW_LINE[] = {'\n',0}; 45#define UTF8_NEW_LINE "\x0a" 46#endif 47U_CDECL_END 48 49U_CDECL_BEGIN 50static void U_CALLCONV TestStream(void) 51{ 52 const UChar thisMu[] = { 0x74, 0x48, 0x69, 0x73, 0x3BC, 0}; 53 const UChar mu[] = { 0x6D, 0x75, 0}; 54 UnicodeString str1 = UNICODE_STRING_SIMPLE("str1"); 55 UnicodeString str2 = UNICODE_STRING_SIMPLE(" <<"); 56 UnicodeString str3 = UNICODE_STRING_SIMPLE("2"); 57 UnicodeString str4 = UNICODE_STRING_SIMPLE(" UTF-8 "); 58 UnicodeString inStr = UNICODE_STRING_SIMPLE(" UTF-8 "); 59 UnicodeString inStr2; 60 char defConvName[UCNV_MAX_CONVERTER_NAME_LENGTH*2]; 61 char inStrC[128]; 62 UErrorCode status = U_ZERO_ERROR; 63 UConverter *defConv; 64 static const char testStr[] = "\x42\x65\x67\x69\x6E\x6E\x69\x6E\x67\x20\x6F\x66\x20\x74\x65\x73\x74\x20\x73\x74\x72\x31\x20\x20\x20\x3C\x3C\x32\x31\x20" UTF8_NEW_LINE "\x20\x55\x54\x46\x2D\x38\x20\xCE\xBC\xF0\x90\x80\x81\xF0\x90\x80\x82"; 65 66 str4.append((UChar32)0x03BC); /* mu */ 67 str4.append((UChar32)0x10001); 68 str4.append((UChar32)0x10002); 69 70 /* release the default converter and use utf-8 for a bit */ 71 defConv = u_getDefaultConverter(&status); 72 if (U_FAILURE(status)) { 73 log_err("Can't get default converter\n"); 74 return; 75 } 76 ucnv_close(defConv); 77 strncpy(defConvName, ucnv_getDefaultName(), sizeof(defConvName)/sizeof(defConvName[0])); 78 ucnv_setDefaultName("UTF-8"); 79 80 static const char * const TESTSTRING = "\x20\x74\x48\x69\x73\xCE\xBC\xE2\x80\x82\x20\x6D\x75\x20\x77\x6F\x72\x6C\x64"; 81#ifdef USE_SSTREAM 82 ostringstream outTestStream; 83 istringstream inTestStream(TESTSTRING); 84#else 85 char testStreamBuf[512]; 86 ostrstream outTestStream(testStreamBuf, sizeof(testStreamBuf)); 87 istrstream inTestStream(TESTSTRING, 0); 88 89 /* initialize testStreamBuf */ 90 memset(testStreamBuf, '*', sizeof(testStreamBuf)); 91 testStreamBuf[sizeof(testStreamBuf)-1] = 0; 92#endif 93 94 outTestStream << "\x42\x65\x67\x69\x6E\x6E\x69\x6E\x67\x20\x6F\x66\x20\x74\x65\x73\x74\x20"; 95 outTestStream << str1 << "\x20\x20" << str2 << str3 << "\x31\x20" << UTF8_NEW_LINE << str4 << ends; 96#ifdef USE_SSTREAM 97 string tempStr = outTestStream.str(); 98 const char *testStreamBuf = tempStr.c_str(); 99#endif 100 if (strcmp(testStreamBuf, testStr) != 0) { 101 log_err("Got: \"%s\", Expected: \"%s\"\n", testStreamBuf, testStr); 102 } 103 104 inTestStream >> inStr >> inStr2; 105 if (inStr.compare(thisMu) != 0) { 106 u_austrncpy(inStrC, inStr.getBuffer(), inStr.length()); 107 inStrC[inStr.length()] = 0; 108 log_err("Got: \"%s\", Expected: \"tHis\\u03BC\"\n", inStrC); 109 } 110 if (inStr2.compare(mu) != 0) { 111 u_austrncpy(inStrC, inStr.getBuffer(), inStr.length()); 112 inStrC[inStr.length()] = 0; 113 log_err("Got: \"%s\", Expected: \"mu\"\n", inStrC); 114 } 115 116 /* return the default converter to the original state. */ 117 ucnv_setDefaultName(defConvName); 118 defConv = u_getDefaultConverter(&status); 119 if (U_FAILURE(status)) { 120 log_err("Can't get default converter"); 121 return; 122 } 123 124 /* Test formatting when using '<<' and UnicodeString */ 125#ifdef USE_SSTREAM 126 ostringstream outFormatStream; 127#else 128 char testFormatStreamBuf[512]; 129 memset(testFormatStreamBuf, 0, sizeof(testFormatStreamBuf)); 130 ostrstream outFormatStream(testFormatStreamBuf, sizeof(testFormatStreamBuf)); 131#endif 132 UnicodeString ustr("string"); 133 134 outFormatStream << "1234567890" << setw(10) << left << ustr << " " << "0123456789"; 135 136#ifdef USE_SSTREAM 137 tempStr = outFormatStream.str(); 138 const char *testFormatStreamBuf = tempStr.c_str(); 139#endif 140 const char *format_test_expected = "1234567890string 0123456789"; 141 if (strcmp(format_test_expected, testFormatStreamBuf) != 0) { 142 log_err("UnicodeString format test using << operator Got: '%s' Expected: '%s'\n", testFormatStreamBuf, format_test_expected); 143 } 144 145 /* Test large buffer (size > 200) when using '<<' and UnicodeString */ 146#ifdef USE_SSTREAM 147 ostringstream outLargeStream; 148#else 149 char testLargeStreamBuf[512]; 150 memset(testLargeStreamBuf, 0, sizeof(testLargeStreamBuf)); 151 ostrstream outLargeStream(testLargeStreamBuf, sizeof(testLargeStreamBuf)); 152#endif 153 UChar large_array[200]; 154 int32_t large_array_length = sizeof(large_array)/sizeof(UChar); 155 for (int32_t i = 0; i < large_array_length; i++) { 156 large_array[i] = 0x41; 157 } 158 UnicodeString large_array_unistr(large_array, large_array_length); 159 160 outLargeStream << large_array_unistr; 161 162#ifdef USE_SSTREAM 163 string tmpString = outLargeStream.str(); 164 const char *testLargeStreamBuf = tmpString.c_str(); 165#endif 166 char expectedLargeStreamBuf[300]; 167 int32_t expectedBufLength = sizeof(expectedLargeStreamBuf); 168 169 ucnv_fromUChars(defConv, expectedLargeStreamBuf, expectedBufLength, large_array, large_array_length, &status); 170 if (U_SUCCESS(status)) { 171 if (strcmp(testLargeStreamBuf, expectedLargeStreamBuf) != 0) { 172 log_err("Large UnicodeString operator << output incorrect.\n"); 173 } 174 } else { 175 log_err("Error converting string for large stream buffer testing.\n"); 176 } 177 ucnv_close(defConv); 178} 179 180#define IOSTREAM_GOOD_SHIFT 3 181#define IOSTREAM_GOOD (1<<IOSTREAM_GOOD_SHIFT) 182#define IOSTREAM_BAD_SHIFT 2 183#define IOSTREAM_BAD (1<<IOSTREAM_BAD_SHIFT) 184#define IOSTREAM_EOF_SHIFT 1 185#define IOSTREAM_EOF (1<<IOSTREAM_EOF_SHIFT) 186#define IOSTREAM_FAIL_SHIFT 0 187#define IOSTREAM_FAIL (1<<IOSTREAM_FAIL_SHIFT) 188 189static int32_t getBitStatus(const iostream& stream) { 190 return (stream.good()<<IOSTREAM_GOOD_SHIFT) 191 | (stream.bad()<<IOSTREAM_BAD_SHIFT) 192 | (stream.eof()<<IOSTREAM_EOF_SHIFT) 193 | (stream.fail()<<IOSTREAM_FAIL_SHIFT); 194} 195 196void 197printBits(const iostream& stream) 198{ 199 int32_t status = getBitStatus(stream); 200 log_verbose("status 0x%02X (", status); 201 if (status & IOSTREAM_GOOD) { 202 log_verbose("good"); 203 } 204 if (status & IOSTREAM_BAD) { 205 log_verbose("bad"); 206 } 207 if (status & IOSTREAM_EOF) { 208 log_verbose("eof"); 209 } 210 if (status & IOSTREAM_FAIL) { 211 log_verbose("fail"); 212 } 213 log_verbose(")\n"); 214} 215 216void 217testString( 218 UnicodeString& str, 219 const char* testString, 220 const UChar* expectedString, 221 int32_t expectedStatus) 222{ 223#ifdef USE_SSTREAM 224 stringstream sstrm; 225#else 226 strstream sstrm; 227#endif 228 229 sstrm << testString; 230 231 /*log_verbose("iostream before operator::>>() call \"%s\" ", testString); 232 printBits(sstrm);*/ 233 234 sstrm >> str; 235 236 log_verbose("iostream after operator::>>() call \"%s\" ", testString); 237 printBits(sstrm); 238 239 if (getBitStatus(sstrm) != expectedStatus) { 240 printBits(sstrm); 241 log_err("Expected status %d, Got %d. See verbose output for details\n", expectedStatus, getBitStatus(sstrm)); 242 } 243 if (str != UnicodeString(expectedString)) { 244 log_err("Did not get expected results from \"%s\", expected \"%s\"\n", testString, expectedString); 245 } 246} 247 248 249static void U_CALLCONV TestStreamEOF(void) 250{ 251 UnicodeString dest; 252 fstream fs(STANDARD_TEST_FILE, fstream::in | fstream::out | fstream::trunc); 253#ifdef USE_SSTREAM 254 stringstream ss; 255#else 256 strstream ss; 257#endif 258 259 fs << "EXAMPLE"; 260 fs.seekg(0); 261 ss << "EXAMPLE"; 262 263 if (!(fs >> dest)) { 264 log_err("Reading of file did not return expected status result\n"); 265 } 266 if (dest != "EXAMPLE") { 267 log_err("Reading of file did not return expected string\n"); 268 } 269 270 if (!(ss >> dest)) { 271 log_err("Reading of string did not return expected status result\n"); 272 } 273 if (dest != "EXAMPLE") { 274 log_err("Reading of string did not return expected string\n"); 275 } 276 fs.close(); 277 278 log_verbose("Testing operator >> for UnicodeString...\n"); 279 280 /* The test cases needs to be converted to the default codepage. However, the stream operator needs char* so U_STRING_* is called. */ 281 U_STRING_DECL(testCase1, "", 0); 282 U_STRING_INIT(testCase1, "", 0); 283 U_STRING_DECL(testCase2, "foo", 3); 284 U_STRING_INIT(testCase2, "foo", 3); 285 U_STRING_DECL(testCase3, " ", 3); 286 U_STRING_INIT(testCase3, " ", 3); 287 U_STRING_DECL(testCase4, " bar", 6); 288 U_STRING_INIT(testCase4, " bar", 6); 289 U_STRING_DECL(testCase5, "bar ", 6); 290 U_STRING_INIT(testCase5, "bar ", 6); 291 U_STRING_DECL(testCase6, " bar ", 9); 292 U_STRING_INIT(testCase6, " bar ", 9); 293 294 295 U_STRING_DECL(expectedResultA, "", 0); 296 U_STRING_INIT(expectedResultA, "", 0); 297 U_STRING_DECL(expectedResultB, "foo", 3); 298 U_STRING_INIT(expectedResultB, "foo", 3); 299 U_STRING_DECL(expectedResultC, "unchanged", 9); 300 U_STRING_INIT(expectedResultC, "unchanged", 9); 301 U_STRING_DECL(expectedResultD, "bar", 3); 302 U_STRING_INIT(expectedResultD, "bar", 3); 303 304 305 UnicodeString UStr; 306 UnicodeString expectedResults; 307 char testcase[10]; 308 testString(UStr, u_austrcpy(testcase, testCase1), expectedResultA, IOSTREAM_EOF|IOSTREAM_FAIL); 309 testString(UStr, u_austrcpy(testcase, testCase2), expectedResultB, IOSTREAM_EOF); 310 UStr = UnicodeString(expectedResultC); 311 testString(UStr, u_austrcpy(testcase, testCase3), expectedResultC, IOSTREAM_EOF|IOSTREAM_FAIL); 312 testString(UStr, u_austrcpy(testcase, testCase4), expectedResultD, IOSTREAM_EOF); 313 testString(UStr, u_austrcpy(testcase, testCase5), expectedResultD, IOSTREAM_GOOD); 314 testString(UStr, u_austrcpy(testcase, testCase6), expectedResultD, IOSTREAM_GOOD); 315} 316U_CDECL_END 317 318U_CFUNC void addStreamTests(TestNode** root) { 319 addTest(root, &TestStream, "stream/TestStream"); 320 addTest(root, &TestStreamEOF, "stream/TestStreamEOF"); 321} 322#endif 323