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