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