]> git.saurik.com Git - wxWidgets.git/blame - tests/streams/textstreamtest.cpp
Fixed capitalisation
[wxWidgets.git] / tests / streams / textstreamtest.cpp
CommitLineData
ba854691 1///////////////////////////////////////////////////////////////////////////////
3dea816b 2// Name: tests/streams/textstreamtest.cpp
ba854691 3// Purpose: wxTextXXXStream unit test
6f3e46c2 4// Author: Ryan Norton, Vince Harron
ba854691 5// Created: 2004-08-14
6f3e46c2 6// Copyright: (c) 2004 Ryan Norton, (c) 2006 Vince Harron
ba854691
RN
7///////////////////////////////////////////////////////////////////////////////
8
9// ----------------------------------------------------------------------------
10// headers
11// ----------------------------------------------------------------------------
12
8899b155 13#include "testprec.h"
ba854691
RN
14
15#ifdef __BORLANDC__
16 #pragma hdrstop
17#endif
18
19#ifndef WX_PRECOMP
20 #include "wx/wx.h"
21#endif // WX_PRECOMP
22
23#include "wx/txtstrm.h"
24#include "wx/wfstream.h"
25
216a72f3 26#if wxUSE_LONGLONG
6f3e46c2 27 #include "wx/longlong.h"
216a72f3
VZ
28#endif
29
6f3e46c2
VZ
30#if wxUSE_UNICODE
31 #include "wx/mstream.h"
32#endif // wxUSE_UNICODE
33
ba854691
RN
34// ----------------------------------------------------------------------------
35// test class
36// ----------------------------------------------------------------------------
37
38class TextStreamTestCase : public CppUnit::TestCase
39{
40public:
41 TextStreamTestCase();
42
43private:
44 CPPUNIT_TEST_SUITE( TextStreamTestCase );
45 CPPUNIT_TEST( Endline );
3dea816b 46 CPPUNIT_TEST( MiscTests );
6f3e46c2 47
216a72f3
VZ
48#if wxUSE_LONGLONG
49 CPPUNIT_TEST( TestLongLong );
50 CPPUNIT_TEST( TestLongLong );
6f3e46c2
VZ
51#endif // wxUSE_LONGLONG
52
53#if wxUSE_UNICODE
54 CPPUNIT_TEST( TestUTF8Input );
55 CPPUNIT_TEST( TestEmbeddedZerosUTF16LEInput );
56 CPPUNIT_TEST( TestEmbeddedZerosUTF16BEInput );
57 CPPUNIT_TEST( TestEmbeddedZerosUTF32LEInput );
58 CPPUNIT_TEST( TestEmbeddedZerosUTF32BEInput );
59#endif // wxUSE_UNICODE
ba854691
RN
60 CPPUNIT_TEST_SUITE_END();
61
62 void Endline();
3dea816b 63 void MiscTests();
6f3e46c2 64
216a72f3
VZ
65#if wxUSE_LONGLONG
66 void TestLongLong();
67 void TestULongLong();
68#endif // wxUSE_LONGLONG
ba854691 69
6f3e46c2
VZ
70#if wxUSE_UNICODE
71 void TestUTF8Input();
72 void TestEmbeddedZerosUTF16LEInput();
73 void TestEmbeddedZerosUTF16BEInput();
74 void TestEmbeddedZerosUTF32LEInput();
75 void TestEmbeddedZerosUTF32BEInput();
0646b219 76 void TestInput(const wxMBConv& conv,
6f3e46c2
VZ
77 const void* encodedText,
78 size_t encodedSize );
79#endif // wxUSE_UNICODE
80
ba854691
RN
81
82 DECLARE_NO_COPY_CLASS(TextStreamTestCase)
83};
84
85// register in the unnamed registry so that these tests are run by default
86CPPUNIT_TEST_SUITE_REGISTRATION( TextStreamTestCase );
87
e3778b4d 88// also include in its own registry so that these tests can be run alone
ba854691
RN
89CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TextStreamTestCase, "TextStreamTestCase" );
90
91TextStreamTestCase::TextStreamTestCase()
92{
93}
94
bb5a9514 95#if defined(__WINDOWS__) || defined(__WXPM__)
ba854691
RN
96# define NEWLINE "\r\n"
97# define NEWLINELEN 2
98#elif defined(__WXMAC__) && !defined(__DARWIN__)
99# define NEWLINE "\r"
100# define NEWLINELEN 1
101#else
102# define NEWLINE "\n"
103# define NEWLINELEN 1
104#endif
105
106void TextStreamTestCase::Endline()
107{
9a83f860 108 wxFileOutputStream* pOutFile = new wxFileOutputStream(wxT("test.txt"));
ba854691 109 wxTextOutputStream* pOutText = new wxTextOutputStream(*pOutFile);
9a83f860
VZ
110 *pOutText << wxT("Test text") << endl
111 << wxT("More Testing Text (There should be newline before this)");
ba854691
RN
112
113 delete pOutText;
114 delete pOutFile;
115
9a83f860 116 wxFileInputStream* pInFile = new wxFileInputStream(wxT("test.txt"));
ba854691
RN
117
118 char szIn[9 + NEWLINELEN];
119
120 pInFile->Read(szIn, 9 + NEWLINELEN);
121
122 CPPUNIT_ASSERT( memcmp(&szIn[9], NEWLINE, NEWLINELEN) == 0 );
123
124 delete pInFile;
125}
216a72f3 126
3dea816b
FM
127void TextStreamTestCase::MiscTests()
128{
129 wxString filename = wxT("testdata.fc");
130 wxFileInputStream fsIn(filename);
a1b806b9 131 if ( !fsIn.IsOk() )
3dea816b
FM
132 {
133 return;
134 }
135
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());
143}
144
216a72f3
VZ
145#if wxUSE_LONGLONG
146
147template <typename T>
148static void DoTestRoundTrip(const T *values, size_t numValues)
149{
150 {
9a83f860 151 wxFileOutputStream fileOut(wxT("test.txt"));
216a72f3
VZ
152 wxTextOutputStream textOut(fileOut);
153
154 for ( size_t n = 0; n < numValues; n++ )
155 {
156 textOut << values[n] << endl;
157 }
158 }
159
160 {
9a83f860 161 wxFileInputStream fileIn(wxT("test.txt"));
216a72f3
VZ
162 wxTextInputStream textIn(fileIn);
163
164 T value;
165 for ( size_t n = 0; n < numValues; n++ )
166 {
167 textIn >> value;
168
169 CPPUNIT_ASSERT( value == values[n] );
170 }
171 }
172}
173
174void TextStreamTestCase::TestLongLong()
175{
176 static const wxLongLong llvalues[] =
177 {
178 0,
179 1,
180 -1,
181 0x12345678l,
182 -0x12345678l,
183 wxLL(0x123456789abcdef0),
184 wxLL(-0x123456789abcdef0),
185 };
186
187 DoTestRoundTrip(llvalues, WXSIZEOF(llvalues));
188}
189
190void TextStreamTestCase::TestULongLong()
191{
192 static const wxULongLong ullvalues[] =
193 {
194 0,
195 1,
196 0x12345678l,
197 wxULL(0x123456789abcdef0),
198 };
199
200 DoTestRoundTrip(ullvalues, WXSIZEOF(ullvalues));
201}
202
203#endif // wxUSE_LONGLONG
204
6f3e46c2
VZ
205#if wxUSE_UNICODE
206
93a800a9 207static const wchar_t txtWchar[4] =
6f3e46c2
VZ
208{
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
213};
214
93a800a9 215static const unsigned char txtUtf8[6] =
6f3e46c2
VZ
216{
217 0x41, 0xc4, 0x80, 0x41, 0xc4, 0x80,
218};
219
93a800a9 220static const unsigned char txtUtf16le[8] =
6f3e46c2
VZ
221{
222 0x41, 0x00, 0x00, 0x01, 0x41, 0x00, 0x00, 0x01,
223};
224
93a800a9 225static const unsigned char txtUtf16be[8] =
6f3e46c2
VZ
226{
227 0x00, 0x41, 0x01, 0x00, 0x00, 0x41, 0x01, 0x00,
228};
229
93a800a9 230static const unsigned char txtUtf32le[16] =
6f3e46c2
VZ
231{
232 0x41, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
233 0x41, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
234};
235
93a800a9 236static const unsigned char txtUtf32be[16] =
6f3e46c2
VZ
237{
238 0x00, 0x00, 0x00, 0x41, 0x00, 0x00, 0x01, 0x00,
239 0x00, 0x00, 0x00, 0x41, 0x00, 0x00, 0x01, 0x00,
240};
241
242void TextStreamTestCase::TestUTF8Input()
243{
0646b219
VZ
244 TestInput(wxConvUTF8, txtUtf8, sizeof(txtUtf8));
245 TestInput(wxCSConv(wxFONTENCODING_UTF8), txtUtf8, sizeof(txtUtf8));
6f3e46c2
VZ
246}
247
248void TextStreamTestCase::TestEmbeddedZerosUTF16LEInput()
249{
0646b219
VZ
250 TestInput(wxMBConvUTF16LE(), txtUtf16le, sizeof(txtUtf16le));
251 TestInput(wxCSConv(wxFONTENCODING_UTF16LE), txtUtf16le, sizeof(txtUtf16le));
6f3e46c2
VZ
252}
253
254void TextStreamTestCase::TestEmbeddedZerosUTF16BEInput()
255{
0646b219
VZ
256 TestInput(wxMBConvUTF16BE(), txtUtf16be, sizeof(txtUtf16be));
257 TestInput(wxCSConv(wxFONTENCODING_UTF16BE), txtUtf16be, sizeof(txtUtf16be));
6f3e46c2
VZ
258}
259
260void TextStreamTestCase::TestEmbeddedZerosUTF32LEInput()
261{
0646b219
VZ
262 TestInput(wxMBConvUTF32LE(), txtUtf32le, sizeof(txtUtf32le));
263 TestInput(wxCSConv(wxFONTENCODING_UTF32LE), txtUtf32le, sizeof(txtUtf32le));
6f3e46c2
VZ
264}
265
266void TextStreamTestCase::TestEmbeddedZerosUTF32BEInput()
267{
0646b219
VZ
268 TestInput(wxMBConvUTF32BE(), txtUtf32be, sizeof(txtUtf32be));
269 TestInput(wxCSConv(wxFONTENCODING_UTF32BE), txtUtf32be, sizeof(txtUtf32be));
6f3e46c2
VZ
270}
271
0646b219 272void TextStreamTestCase::TestInput(const wxMBConv& conv,
6f3e46c2
VZ
273 const void *encodedText,
274 size_t encodedSize)
275{
6f3e46c2
VZ
276 wxMemoryInputStream byteIn(encodedText, encodedSize);
277 wxTextInputStream textIn(byteIn, wxT("\n"), conv);
278
279 wxString temp;
280 while ( wxChar c = textIn.GetChar() )
281 {
282 temp.Append(c);
283 }
284
285 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(txtWchar), temp.length() );
286
11aac4ba 287 CPPUNIT_ASSERT_EQUAL( 0, memcmp(txtWchar, temp.wc_str(), sizeof(txtWchar)) );
6f3e46c2
VZ
288}
289
290#endif // wxUSE_UNICODE