]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/iotest/stream.cpp
ICU-8.11.tar.gz
[apple/icu.git] / icuSources / test / iotest / stream.cpp
CommitLineData
73c04bcf
A
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
31using namespace std;
32#elif U_IOSTREAM_SOURCE >= 198506
33#include <strstream.h>
34#endif
35
36#include <string.h>
37
38U_CDECL_BEGIN
39#ifdef U_WINDOWS
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{
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 ");
61 UnicodeString inStr2;
62 char defConvName[UCNV_MAX_CONVERTER_NAME_LENGTH*2];
63 char inStrC[128];
64 UErrorCode status = U_ZERO_ERROR;
65 UConverter *defConv;
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";
67
68 str4.append((UChar32)0x03BC); /* mu */
69 str4.append((UChar32)0x10001);
70 str4.append((UChar32)0x10002);
71
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");
76 return;
77 }
78 ucnv_close(defConv);
79 strncpy(defConvName, ucnv_getDefaultName(), sizeof(defConvName)/sizeof(defConvName[0]));
80 ucnv_setDefaultName("UTF-8");
81
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";
83#ifdef USE_SSTREAM
84 ostringstream outTestStream;
85 istringstream inTestStream(TESTSTRING);
86#else
87 char testStreamBuf[512];
88 ostrstream outTestStream(testStreamBuf, sizeof(testStreamBuf));
89 istrstream inTestStream(TESTSTRING, 0);
90
91 /* initialize testStreamBuf */
92 memset(testStreamBuf, '*', sizeof(testStreamBuf));
93 testStreamBuf[sizeof(testStreamBuf)-1] = 0;
94#endif
95
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;
98#ifdef USE_SSTREAM
99 string tempStr = outTestStream.str();
100 const char *testStreamBuf = tempStr.c_str();
101#endif
102 if (strcmp(testStreamBuf, testStr) != 0) {
103 log_err("Got: \"%s\", Expected: \"%s\"\n", testStreamBuf, testStr);
104 }
105
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);
111 }
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);
116 }
117
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");
123 return;
124 }
125 ucnv_close(defConv);
126#else
127 log_info("U_IOSTREAM_SOURCE is disabled\n");
128#endif
129}
130U_CDECL_END
131
132U_CFUNC void addStreamTests(TestNode** root) {
133 addTest(root, &TestStream, "stream/TestStream");
134}
135