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.
32 #elif U_IOSTREAM_SOURCE >= 198506
33 #include <strstream.h>
40 const UChar NEW_LINE
[] = {0x0d,0x0a,0};
41 const char C_NEW_LINE
[] = {0x0d,0x0a,0};
42 #define UTF8_NEW_LINE "\x0d\x0a"
44 const UChar NEW_LINE
[] = {0x0a,0};
45 const char C_NEW_LINE
[] = {'\n',0};
46 #define UTF8_NEW_LINE "\x0a"
51 static void U_CALLCONV
TestStream(void)
53 #if U_IOSTREAM_SOURCE >= 198506
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 ");
62 char defConvName
[UCNV_MAX_CONVERTER_NAME_LENGTH
*2];
64 UErrorCode status
= U_ZERO_ERROR
;
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";
68 str4
.append((UChar32
)0x03BC); /* mu */
69 str4
.append((UChar32
)0x10001);
70 str4
.append((UChar32
)0x10002);
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");
79 strncpy(defConvName
, ucnv_getDefaultName(), sizeof(defConvName
)/sizeof(defConvName
[0]));
80 ucnv_setDefaultName("UTF-8");
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";
84 ostringstream outTestStream
;
85 istringstream
inTestStream(TESTSTRING
);
87 char testStreamBuf
[512];
88 ostrstream
outTestStream(testStreamBuf
, sizeof(testStreamBuf
));
89 istrstream
inTestStream(TESTSTRING
, 0);
91 /* initialize testStreamBuf */
92 memset(testStreamBuf
, '*', sizeof(testStreamBuf
));
93 testStreamBuf
[sizeof(testStreamBuf
)-1] = 0;
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
;
99 string tempStr
= outTestStream
.str();
100 const char *testStreamBuf
= tempStr
.c_str();
102 if (strcmp(testStreamBuf
, testStr
) != 0) {
103 log_err("Got: \"%s\", Expected: \"%s\"\n", testStreamBuf
, testStr
);
106 inTestStream
>> inStr
>> inStr2
;
107 if (inStr
.compare(thisMu
) != 0) {
108 u_austrncpy(inStrC
, inStr
.getBuffer(), inStr
.length());
109 inStrC
[inStr
.length()] = 0;
110 log_err("Got: \"%s\", Expected: \"tHis\\u03BC\"\n", inStrC
);
112 if (inStr2
.compare(mu
) != 0) {
113 u_austrncpy(inStrC
, inStr
.getBuffer(), inStr
.length());
114 inStrC
[inStr
.length()] = 0;
115 log_err("Got: \"%s\", Expected: \"mu\"\n", inStrC
);
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");
127 log_info("U_IOSTREAM_SOURCE is disabled\n");
132 U_CFUNC
void addStreamTests(TestNode
** root
) {
133 addTest(root
, &TestStream
, "stream/TestStream");