1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/streams/textstreamtest.cpp
3 // Purpose: wxTextXXXStream unit test
4 // Author: Ryan Norton, Vince Harron
6 // Copyright: (c) 2004 Ryan Norton, (c) 2006 Vince Harron
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
23 #include "wx/txtstrm.h"
24 #include "wx/wfstream.h"
27 #include "wx/longlong.h"
31 #include "wx/mstream.h"
32 #endif // wxUSE_UNICODE
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 class TextStreamTestCase
: public CppUnit::TestCase
44 CPPUNIT_TEST_SUITE( TextStreamTestCase
);
45 CPPUNIT_TEST( Endline
);
46 CPPUNIT_TEST( MiscTests
);
49 CPPUNIT_TEST( TestLongLong
);
50 CPPUNIT_TEST( TestLongLong
);
51 #endif // wxUSE_LONGLONG
54 CPPUNIT_TEST( TestUTF8Input
);
55 CPPUNIT_TEST( TestEmbeddedZerosUTF16LEInput
);
56 CPPUNIT_TEST( TestEmbeddedZerosUTF16BEInput
);
57 CPPUNIT_TEST( TestEmbeddedZerosUTF32LEInput
);
58 CPPUNIT_TEST( TestEmbeddedZerosUTF32BEInput
);
59 #endif // wxUSE_UNICODE
60 CPPUNIT_TEST_SUITE_END();
68 #endif // wxUSE_LONGLONG
72 void TestEmbeddedZerosUTF16LEInput();
73 void TestEmbeddedZerosUTF16BEInput();
74 void TestEmbeddedZerosUTF32LEInput();
75 void TestEmbeddedZerosUTF32BEInput();
76 void TestInput(const wxMBConv
& conv
,
77 const void* encodedText
,
79 #endif // wxUSE_UNICODE
82 DECLARE_NO_COPY_CLASS(TextStreamTestCase
)
85 // register in the unnamed registry so that these tests are run by default
86 CPPUNIT_TEST_SUITE_REGISTRATION( TextStreamTestCase
);
88 // also include in its own registry so that these tests can be run alone
89 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TextStreamTestCase
, "TextStreamTestCase" );
91 TextStreamTestCase::TextStreamTestCase()
95 #if defined(__WINDOWS__) || defined(__WXPM__)
96 # define NEWLINE "\r\n"
98 #elif defined(__WXMAC__) && !defined(__DARWIN__)
100 # define NEWLINELEN 1
102 # define NEWLINE "\n"
103 # define NEWLINELEN 1
106 void TextStreamTestCase::Endline()
108 wxFileOutputStream
* pOutFile
= new wxFileOutputStream(wxT("test.txt"));
109 wxTextOutputStream
* pOutText
= new wxTextOutputStream(*pOutFile
);
110 *pOutText
<< wxT("Test text") << endl
111 << wxT("More Testing Text (There should be newline before this)");
116 wxFileInputStream
* pInFile
= new wxFileInputStream(wxT("test.txt"));
118 char szIn
[9 + NEWLINELEN
];
120 pInFile
->Read(szIn
, 9 + NEWLINELEN
);
122 CPPUNIT_ASSERT( memcmp(&szIn
[9], NEWLINE
, NEWLINELEN
) == 0 );
127 void TextStreamTestCase::MiscTests()
129 wxString filename
= wxT("testdata.fc");
130 wxFileInputStream
fsIn(filename
);
136 wxTextInputStream
tis(fsIn
);
137 CPPUNIT_ASSERT_EQUAL("# this is the test data file for wxFileConfig tests", tis
.ReadLine());
138 CPPUNIT_ASSERT_EQUAL("value1=one", tis
.ReadLine());
139 CPPUNIT_ASSERT_EQUAL("# a comment here", tis
.ReadLine());
140 CPPUNIT_ASSERT_EQUAL("value2=two", tis
.ReadLine());
141 CPPUNIT_ASSERT_EQUAL("value\\ with\\ spaces\\ inside\\ it=nothing special", tis
.ReadLine());
142 CPPUNIT_ASSERT_EQUAL("path=$PATH", tis
.ReadLine());
147 template <typename T
>
148 static void DoTestRoundTrip(const T
*values
, size_t numValues
)
151 wxFileOutputStream
fileOut(wxT("test.txt"));
152 wxTextOutputStream
textOut(fileOut
);
154 for ( size_t n
= 0; n
< numValues
; n
++ )
156 textOut
<< values
[n
] << endl
;
161 wxFileInputStream
fileIn(wxT("test.txt"));
162 wxTextInputStream
textIn(fileIn
);
165 for ( size_t n
= 0; n
< numValues
; n
++ )
169 CPPUNIT_ASSERT( value
== values
[n
] );
174 void TextStreamTestCase::TestLongLong()
176 static const wxLongLong llvalues
[] =
183 wxLL(0x123456789abcdef0),
184 wxLL(-0x123456789abcdef0),
187 DoTestRoundTrip(llvalues
, WXSIZEOF(llvalues
));
190 void TextStreamTestCase::TestULongLong()
192 static const wxULongLong ullvalues
[] =
197 wxULL(0x123456789abcdef0),
200 DoTestRoundTrip(ullvalues
, WXSIZEOF(ullvalues
));
203 #endif // wxUSE_LONGLONG
207 static const wchar_t txtWchar
[4] =
209 0x0041, // LATIN CAPITAL LETTER A
210 0x0100, // A WITH BREVE, LATIN SMALL LETTER
211 0x0041, // LATIN CAPITAL LETTER A
212 0x0100, // A WITH BREVE, LATIN SMALL LETTER
215 static const unsigned char txtUtf8
[6] =
217 0x41, 0xc4, 0x80, 0x41, 0xc4, 0x80,
220 static const unsigned char txtUtf16le
[8] =
222 0x41, 0x00, 0x00, 0x01, 0x41, 0x00, 0x00, 0x01,
225 static const unsigned char txtUtf16be
[8] =
227 0x00, 0x41, 0x01, 0x00, 0x00, 0x41, 0x01, 0x00,
230 static const unsigned char txtUtf32le
[16] =
232 0x41, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
233 0x41, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
236 static const unsigned char txtUtf32be
[16] =
238 0x00, 0x00, 0x00, 0x41, 0x00, 0x00, 0x01, 0x00,
239 0x00, 0x00, 0x00, 0x41, 0x00, 0x00, 0x01, 0x00,
242 void TextStreamTestCase::TestUTF8Input()
244 TestInput(wxConvUTF8
, txtUtf8
, sizeof(txtUtf8
));
245 TestInput(wxCSConv(wxFONTENCODING_UTF8
), txtUtf8
, sizeof(txtUtf8
));
248 void TextStreamTestCase::TestEmbeddedZerosUTF16LEInput()
250 TestInput(wxMBConvUTF16LE(), txtUtf16le
, sizeof(txtUtf16le
));
251 TestInput(wxCSConv(wxFONTENCODING_UTF16LE
), txtUtf16le
, sizeof(txtUtf16le
));
254 void TextStreamTestCase::TestEmbeddedZerosUTF16BEInput()
256 TestInput(wxMBConvUTF16BE(), txtUtf16be
, sizeof(txtUtf16be
));
257 TestInput(wxCSConv(wxFONTENCODING_UTF16BE
), txtUtf16be
, sizeof(txtUtf16be
));
260 void TextStreamTestCase::TestEmbeddedZerosUTF32LEInput()
262 TestInput(wxMBConvUTF32LE(), txtUtf32le
, sizeof(txtUtf32le
));
263 TestInput(wxCSConv(wxFONTENCODING_UTF32LE
), txtUtf32le
, sizeof(txtUtf32le
));
266 void TextStreamTestCase::TestEmbeddedZerosUTF32BEInput()
268 TestInput(wxMBConvUTF32BE(), txtUtf32be
, sizeof(txtUtf32be
));
269 TestInput(wxCSConv(wxFONTENCODING_UTF32BE
), txtUtf32be
, sizeof(txtUtf32be
));
272 void TextStreamTestCase::TestInput(const wxMBConv
& conv
,
273 const void *encodedText
,
276 wxMemoryInputStream
byteIn(encodedText
, encodedSize
);
277 wxTextInputStream
textIn(byteIn
, wxT("\n"), conv
);
280 while ( wxChar c
= textIn
.GetChar() )
285 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(txtWchar
), temp
.length() );
287 CPPUNIT_ASSERT_EQUAL( 0, memcmp(txtWchar
, temp
.wc_str(), sizeof(txtWchar
)) );
290 #endif // wxUSE_UNICODE