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