2 **********************************************************************
3 * Copyright (C) 2002-2006, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 * file name: iotest.cpp
8 * tab size: 8 (not used)
11 * created on: 2002feb21
12 * created by: George Rhoten
16 #include "unicode/ustream.h"
18 #include "unicode/ucnv.h"
19 #include "unicode/ustring.h"
23 #if U_IOSTREAM_SOURCE >= 199711
24 #if defined(__GNUC__) && __GNUC__ >= 4
28 // <strstream> is deprecated on some platforms, and the compiler complains very loudly if you use it.
33 #elif U_IOSTREAM_SOURCE >= 198506
34 #include <strstream.h>
42 const UChar NEW_LINE
[] = {0x0d,0x0a,0};
43 const char C_NEW_LINE
[] = {0x0d,0x0a,0};
44 #define UTF8_NEW_LINE "\x0d\x0a"
46 const UChar NEW_LINE
[] = {0x0a,0};
47 const char C_NEW_LINE
[] = {'\n',0};
48 #define UTF8_NEW_LINE "\x0a"
53 static void U_CALLCONV
TestStream(void)
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 ");
64 char defConvName
[UCNV_MAX_CONVERTER_NAME_LENGTH
*2];
66 UErrorCode status
= U_ZERO_ERROR
;
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";
70 str4
.append((UChar32
)0x03BC); /* mu */
71 str4
.append((UChar32
)0x10001);
72 str4
.append((UChar32
)0x10002);
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");
81 strncpy(defConvName
, ucnv_getDefaultName(), sizeof(defConvName
)/sizeof(defConvName
[0]));
82 ucnv_setDefaultName("UTF-8");
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";
86 ostringstream outTestStream
;
87 istringstream
inTestStream(TESTSTRING
);
89 char testStreamBuf
[512];
90 ostrstream
outTestStream(testStreamBuf
, sizeof(testStreamBuf
));
91 istrstream
inTestStream(TESTSTRING
, 0);
93 /* initialize testStreamBuf */
94 memset(testStreamBuf
, '*', sizeof(testStreamBuf
));
95 testStreamBuf
[sizeof(testStreamBuf
)-1] = 0;
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
;
101 string tempStr
= outTestStream
.str();
102 const char *testStreamBuf
= tempStr
.c_str();
104 if (strcmp(testStreamBuf
, testStr
) != 0) {
105 log_err("Got: \"%s\", Expected: \"%s\"\n", testStreamBuf
, testStr
);
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
);
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
);
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");
129 log_info("U_IOSTREAM_SOURCE is disabled\n");
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)
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
);
150 printBits(const iostream
& stream
)
152 int32_t status
= getBitStatus(stream
);
153 log_verbose("status 0x%02X (", status
);
154 if (status
& IOSTREAM_GOOD
) {
157 if (status
& IOSTREAM_BAD
) {
160 if (status
& IOSTREAM_EOF
) {
163 if (status
& IOSTREAM_FAIL
) {
172 const char* testString
,
173 const char* expectedString
,
174 int32_t expectedStatus
)
184 /*log_verbose("iostream before operator::>>() call \"%s\" ", testString);
189 log_verbose("iostream after operator::>>() call \"%s\" ", testString
);
192 if (getBitStatus(sstrm
) != expectedStatus
) {
194 log_err("Expected status %d, Got %d. See verbose output for details\n", getBitStatus(sstrm
), expectedStatus
);
196 if (str
!= UnicodeString(expectedString
)) {
197 log_err("Did not get expected results from \"%s\", expected \"%s\"\n", testString
, expectedString
);
201 static void U_CALLCONV
TestStreamEOF(void)
204 fstream
fs(STANDARD_TEST_FILE
, fstream::in
| fstream::out
| fstream::trunc
);
216 log_err("Reading of file did not return expected status result\n");
218 if (dest
!= "EXAMPLE") {
219 log_err("Reading of file did not return expected string\n");
223 log_err("Reading of string did not return expected status result\n");
225 if (dest
!= "EXAMPLE") {
226 log_err("Reading of string did not return expected string\n");
230 log_verbose("Testing operator >> for UnicodeString...\n");
233 testString(UStr
, "", "", IOSTREAM_EOF
|IOSTREAM_FAIL
);
234 testString(UStr
, "foo", "foo", IOSTREAM_EOF
);
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
);
243 U_CFUNC
void addStreamTests(TestNode
** root
) {
244 addTest(root
, &TestStream
, "stream/TestStream");
245 addTest(root
, &TestStreamEOF
, "stream/TestStreamEOF");