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