• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 **********************************************************************
3 *   Copyright (C) 2002-2008, 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 using namespace std;
33 #elif U_IOSTREAM_SOURCE >= 198506
34 #define USE_OLD_IOSTREAM 1
35 #include <strstream.h>
36 #include <fstream.h>
37 #endif
38 
39 #include <string.h>
40 
41 U_CDECL_BEGIN
42 #ifdef U_WINDOWS
43 const UChar NEW_LINE[] = {0x0d,0x0a,0};
44 const char C_NEW_LINE[] = {0x0d,0x0a,0};
45 #define UTF8_NEW_LINE "\x0d\x0a"
46 #else
47 const UChar NEW_LINE[] = {0x0a,0};
48 const char C_NEW_LINE[] = {'\n',0};
49 #define UTF8_NEW_LINE "\x0a"
50 #endif
51 U_CDECL_END
52 
53 #if U_IOSTREAM_SOURCE
54 U_CDECL_BEGIN
TestStream(void)55 static void U_CALLCONV TestStream(void)
56 {
57 #if U_IOSTREAM_SOURCE >= 198506
58     const UChar thisMu[] = { 0x74, 0x48, 0x69, 0x73, 0x3BC, 0};
59     const UChar mu[] = { 0x6D, 0x75, 0};
60     UnicodeString str1 = UNICODE_STRING_SIMPLE("str1");
61     UnicodeString str2 = UNICODE_STRING_SIMPLE(" <<");
62     UnicodeString str3 = UNICODE_STRING_SIMPLE("2");
63     UnicodeString str4 = UNICODE_STRING_SIMPLE(" UTF-8 ");
64     UnicodeString inStr = UNICODE_STRING_SIMPLE(" UTF-8 ");
65     UnicodeString inStr2;
66     char defConvName[UCNV_MAX_CONVERTER_NAME_LENGTH*2];
67     char inStrC[128];
68     UErrorCode status = U_ZERO_ERROR;
69     UConverter *defConv;
70     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";
71 
72     str4.append((UChar32)0x03BC);   /* mu */
73     str4.append((UChar32)0x10001);
74     str4.append((UChar32)0x10002);
75 
76     /* release the default converter and use utf-8 for a bit */
77     defConv = u_getDefaultConverter(&status);
78     if (U_FAILURE(status)) {
79         log_err("Can't get default converter\n");
80         return;
81     }
82     ucnv_close(defConv);
83     strncpy(defConvName, ucnv_getDefaultName(), sizeof(defConvName)/sizeof(defConvName[0]));
84     ucnv_setDefaultName("UTF-8");
85 
86     static const char * const TESTSTRING = "\x20\x74\x48\x69\x73\xCE\xBC\xE2\x80\x82\x20\x6D\x75\x20\x77\x6F\x72\x6C\x64";
87 #ifdef USE_SSTREAM
88     ostringstream outTestStream;
89     istringstream inTestStream(TESTSTRING);
90 #else
91     char testStreamBuf[512];
92     ostrstream outTestStream(testStreamBuf, sizeof(testStreamBuf));
93     istrstream inTestStream(TESTSTRING, 0);
94 
95     /* initialize testStreamBuf */
96     memset(testStreamBuf, '*', sizeof(testStreamBuf));
97     testStreamBuf[sizeof(testStreamBuf)-1] = 0;
98 #endif
99 
100     outTestStream << "\x42\x65\x67\x69\x6E\x6E\x69\x6E\x67\x20\x6F\x66\x20\x74\x65\x73\x74\x20";
101     outTestStream << str1 << "\x20\x20" << str2 << str3 << "\x31\x20" << UTF8_NEW_LINE << str4 << ends;
102 #ifdef USE_SSTREAM
103     string tempStr = outTestStream.str();
104     const char *testStreamBuf = tempStr.c_str();
105 #endif
106     if (strcmp(testStreamBuf, testStr) != 0) {
107         log_err("Got: \"%s\", Expected: \"%s\"\n", testStreamBuf, testStr);
108     }
109 
110     inTestStream >> inStr >> inStr2;
111     if (inStr.compare(thisMu) != 0) {
112         u_austrncpy(inStrC, inStr.getBuffer(), inStr.length());
113         inStrC[inStr.length()] = 0;
114         log_err("Got: \"%s\", Expected: \"tHis\\u03BC\"\n", inStrC);
115     }
116     if (inStr2.compare(mu) != 0) {
117         u_austrncpy(inStrC, inStr.getBuffer(), inStr.length());
118         inStrC[inStr.length()] = 0;
119         log_err("Got: \"%s\", Expected: \"mu\"\n", inStrC);
120     }
121 
122     /* return the default converter to the original state. */
123     ucnv_setDefaultName(defConvName);
124     defConv = u_getDefaultConverter(&status);
125     if (U_FAILURE(status)) {
126         log_err("Can't get default converter");
127         return;
128     }
129     ucnv_close(defConv);
130 #else
131     log_info("U_IOSTREAM_SOURCE is disabled\n");
132 #endif
133 }
134 
135 #define IOSTREAM_GOOD_SHIFT 3
136 #define IOSTREAM_GOOD (1<<IOSTREAM_GOOD_SHIFT)
137 #define IOSTREAM_BAD_SHIFT 2
138 #define IOSTREAM_BAD (1<<IOSTREAM_BAD_SHIFT)
139 #define IOSTREAM_EOF_SHIFT 1
140 #define IOSTREAM_EOF (1<<IOSTREAM_EOF_SHIFT)
141 #define IOSTREAM_FAIL_SHIFT 0
142 #define IOSTREAM_FAIL (1<<IOSTREAM_FAIL_SHIFT)
143 
getBitStatus(const iostream & stream)144 static int32_t getBitStatus(const iostream&  stream) {
145     return (stream.good()<<IOSTREAM_GOOD_SHIFT)
146         | (stream.bad()<<IOSTREAM_BAD_SHIFT)
147         | (stream.eof()<<IOSTREAM_EOF_SHIFT)
148         | (stream.fail()<<IOSTREAM_FAIL_SHIFT);
149 }
150 
151 void
printBits(const iostream & stream)152 printBits(const iostream&  stream)
153 {
154     int32_t status = getBitStatus(stream);
155     log_verbose("status 0x%02X (", status);
156     if (status & IOSTREAM_GOOD) {
157         log_verbose("good");
158     }
159     if (status & IOSTREAM_BAD) {
160         log_verbose("bad");
161     }
162     if (status & IOSTREAM_EOF) {
163         log_verbose("eof");
164     }
165     if (status & IOSTREAM_FAIL) {
166         log_verbose("fail");
167     }
168     log_verbose(")\n");
169 }
170 
171 void
testString(UnicodeString & str,const char * testString,const UChar * expectedString,int32_t expectedStatus)172 testString(
173             UnicodeString&  str,
174             const char*     testString,
175             const UChar*    expectedString,
176             int32_t         expectedStatus)
177 {
178 #ifdef USE_SSTREAM
179     stringstream sstrm;
180 #else
181     strstream sstrm;
182 #endif
183 
184     sstrm << testString;
185 
186     /*log_verbose("iostream before operator::>>() call \"%s\" ", testString);
187     printBits(sstrm);*/
188 
189     sstrm >> str;
190 
191     log_verbose("iostream after operator::>>() call \"%s\" ", testString);
192     printBits(sstrm);
193 
194     if (getBitStatus(sstrm) != expectedStatus) {
195         printBits(sstrm);
196 #ifdef USE_OLD_IOSTREAM
197         log_info("Warning. Expected status %d, Got %d. This maybe caused by the fact that the non-standardized iostream is being used.\n", expectedStatus, getBitStatus(sstrm));
198         log_info("See verbose output for details.\n");
199 #else
200         log_err("Expected status %d, Got %d. See verbose output for details\n", expectedStatus, getBitStatus(sstrm));
201 #endif
202     }
203     if (str != UnicodeString(expectedString)) {
204         log_err("Did not get expected results from \"%s\", expected \"%s\"\n", testString, expectedString);
205     }
206 }
207 
208 
TestStreamEOF(void)209 static void U_CALLCONV TestStreamEOF(void)
210 {
211     UnicodeString dest;
212     fstream fs(STANDARD_TEST_FILE, fstream::in | fstream::out | fstream::trunc);
213 #ifdef USE_SSTREAM
214     stringstream ss;
215 #else
216     strstream ss;
217 #endif
218 
219 #ifdef USE_OLD_IOSTREAM
220     log_info("Old non-standardized iostream being used. This may result in inconsistent state flag settings. (e.g. failbit may not be set properly)\n");
221     log_info("In such a case, warnings will be issued instead of errors.\n");
222 #endif
223 
224     fs << "EXAMPLE";
225     fs.seekg(0);
226     ss << "EXAMPLE";
227 
228     if (!(fs >> dest)) {
229         log_err("Reading of file did not return expected status result\n");
230     }
231     if (dest != "EXAMPLE") {
232         log_err("Reading of file did not return expected string\n");
233     }
234 
235     if (!(ss >> dest)) {
236         log_err("Reading of string did not return expected status result\n");
237     }
238     if (dest != "EXAMPLE") {
239         log_err("Reading of string did not return expected string\n");
240     }
241     fs.close();
242 
243     log_verbose("Testing operator >> for UnicodeString...\n");
244 
245     /* The test cases needs to be converted to the default codepage.  However, the stream operator needs char* so U_STRING_* is called. */
246     U_STRING_DECL(testCase1, "", 0);
247     U_STRING_INIT(testCase1, "", 0);
248     U_STRING_DECL(testCase2, "foo", 3);
249     U_STRING_INIT(testCase2, "foo", 3);
250     U_STRING_DECL(testCase3, "   ", 3);
251     U_STRING_INIT(testCase3, "   ", 3);
252     U_STRING_DECL(testCase4, "   bar", 6);
253     U_STRING_INIT(testCase4, "   bar", 6);
254     U_STRING_DECL(testCase5, "bar   ", 6);
255     U_STRING_INIT(testCase5, "bar   ", 6);
256     U_STRING_DECL(testCase6, "   bar   ", 9);
257     U_STRING_INIT(testCase6, "   bar   ", 9);
258 
259 
260     U_STRING_DECL(expectedResultA, "", 0);
261     U_STRING_INIT(expectedResultA, "", 0);
262     U_STRING_DECL(expectedResultB, "foo", 3);
263     U_STRING_INIT(expectedResultB, "foo", 3);
264     U_STRING_DECL(expectedResultC, "unchanged", 9);
265     U_STRING_INIT(expectedResultC, "unchanged", 9);
266     U_STRING_DECL(expectedResultD, "bar", 3);
267     U_STRING_INIT(expectedResultD, "bar", 3);
268 
269 
270     UnicodeString UStr;
271     UnicodeString expectedResults;
272     char testcase[10];
273     testString(UStr, u_austrcpy(testcase, testCase1), expectedResultA, IOSTREAM_EOF|IOSTREAM_FAIL);
274     testString(UStr, u_austrcpy(testcase, testCase2), expectedResultB, IOSTREAM_EOF);
275     UStr = UnicodeString(expectedResultC);
276     testString(UStr, u_austrcpy(testcase, testCase3), expectedResultC, IOSTREAM_EOF|IOSTREAM_FAIL);
277     testString(UStr, u_austrcpy(testcase, testCase4), expectedResultD, IOSTREAM_EOF);
278     testString(UStr, u_austrcpy(testcase, testCase5), expectedResultD, IOSTREAM_GOOD);
279     testString(UStr, u_austrcpy(testcase, testCase6), expectedResultD, IOSTREAM_GOOD);
280 }
281 U_CDECL_END
282 
addStreamTests(TestNode ** root)283 U_CFUNC void addStreamTests(TestNode** root) {
284     addTest(root, &TestStream, "stream/TestStream");
285     addTest(root, &TestStreamEOF, "stream/TestStreamEOF");
286 }
287 #endif
288