]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: tests/streams/textstreamtest.cpp | |
3 | // Purpose: wxTextXXXStream unit test | |
4 | // Author: Ryan Norton, Vince Harron | |
5 | // Created: 2004-08-14 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2004 Ryan Norton, (c) 2006 Vince Harron | |
8 | /////////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // ---------------------------------------------------------------------------- | |
11 | // headers | |
12 | // ---------------------------------------------------------------------------- | |
13 | ||
14 | #include "testprec.h" | |
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 | ||
27 | #if wxUSE_LONGLONG | |
28 | #include "wx/longlong.h" | |
29 | #endif | |
30 | ||
31 | #if wxUSE_UNICODE | |
32 | #include "wx/mstream.h" | |
33 | #endif // wxUSE_UNICODE | |
34 | ||
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 ); | |
47 | CPPUNIT_TEST( MiscTests ); | |
48 | ||
49 | #if wxUSE_LONGLONG | |
50 | CPPUNIT_TEST( TestLongLong ); | |
51 | CPPUNIT_TEST( TestLongLong ); | |
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 | |
61 | CPPUNIT_TEST_SUITE_END(); | |
62 | ||
63 | void Endline(); | |
64 | void MiscTests(); | |
65 | ||
66 | #if wxUSE_LONGLONG | |
67 | void TestLongLong(); | |
68 | void TestULongLong(); | |
69 | #endif // wxUSE_LONGLONG | |
70 | ||
71 | #if wxUSE_UNICODE | |
72 | void TestUTF8Input(); | |
73 | void TestEmbeddedZerosUTF16LEInput(); | |
74 | void TestEmbeddedZerosUTF16BEInput(); | |
75 | void TestEmbeddedZerosUTF32LEInput(); | |
76 | void TestEmbeddedZerosUTF32BEInput(); | |
77 | void TestInput(const wxMBConv& conv, | |
78 | const void* encodedText, | |
79 | size_t encodedSize ); | |
80 | #endif // wxUSE_UNICODE | |
81 | ||
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 its 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(__WINDOWS__) || 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 | { | |
109 | wxFileOutputStream* pOutFile = new wxFileOutputStream(wxT("test.txt")); | |
110 | wxTextOutputStream* pOutText = new wxTextOutputStream(*pOutFile); | |
111 | *pOutText << wxT("Test text") << endl | |
112 | << wxT("More Testing Text (There should be newline before this)"); | |
113 | ||
114 | delete pOutText; | |
115 | delete pOutFile; | |
116 | ||
117 | wxFileInputStream* pInFile = new wxFileInputStream(wxT("test.txt")); | |
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 | } | |
127 | ||
128 | void TextStreamTestCase::MiscTests() | |
129 | { | |
130 | wxString filename = wxT("testdata.fc"); | |
131 | wxFileInputStream fsIn(filename); | |
132 | if ( !fsIn.IsOk() ) | |
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 | ||
146 | #if wxUSE_LONGLONG | |
147 | ||
148 | template <typename T> | |
149 | static void DoTestRoundTrip(const T *values, size_t numValues) | |
150 | { | |
151 | { | |
152 | wxFileOutputStream fileOut(wxT("test.txt")); | |
153 | wxTextOutputStream textOut(fileOut); | |
154 | ||
155 | for ( size_t n = 0; n < numValues; n++ ) | |
156 | { | |
157 | textOut << values[n] << endl; | |
158 | } | |
159 | } | |
160 | ||
161 | { | |
162 | wxFileInputStream fileIn(wxT("test.txt")); | |
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 | ||
206 | #if wxUSE_UNICODE | |
207 | ||
208 | static const wchar_t txtWchar[4] = | |
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 | ||
216 | static const unsigned char txtUtf8[6] = | |
217 | { | |
218 | 0x41, 0xc4, 0x80, 0x41, 0xc4, 0x80, | |
219 | }; | |
220 | ||
221 | static const unsigned char txtUtf16le[8] = | |
222 | { | |
223 | 0x41, 0x00, 0x00, 0x01, 0x41, 0x00, 0x00, 0x01, | |
224 | }; | |
225 | ||
226 | static const unsigned char txtUtf16be[8] = | |
227 | { | |
228 | 0x00, 0x41, 0x01, 0x00, 0x00, 0x41, 0x01, 0x00, | |
229 | }; | |
230 | ||
231 | static const unsigned char txtUtf32le[16] = | |
232 | { | |
233 | 0x41, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, | |
234 | 0x41, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, | |
235 | }; | |
236 | ||
237 | static const unsigned char txtUtf32be[16] = | |
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 | { | |
245 | TestInput(wxConvUTF8, txtUtf8, sizeof(txtUtf8)); | |
246 | TestInput(wxCSConv(wxFONTENCODING_UTF8), txtUtf8, sizeof(txtUtf8)); | |
247 | } | |
248 | ||
249 | void TextStreamTestCase::TestEmbeddedZerosUTF16LEInput() | |
250 | { | |
251 | TestInput(wxMBConvUTF16LE(), txtUtf16le, sizeof(txtUtf16le)); | |
252 | TestInput(wxCSConv(wxFONTENCODING_UTF16LE), txtUtf16le, sizeof(txtUtf16le)); | |
253 | } | |
254 | ||
255 | void TextStreamTestCase::TestEmbeddedZerosUTF16BEInput() | |
256 | { | |
257 | TestInput(wxMBConvUTF16BE(), txtUtf16be, sizeof(txtUtf16be)); | |
258 | TestInput(wxCSConv(wxFONTENCODING_UTF16BE), txtUtf16be, sizeof(txtUtf16be)); | |
259 | } | |
260 | ||
261 | void TextStreamTestCase::TestEmbeddedZerosUTF32LEInput() | |
262 | { | |
263 | TestInput(wxMBConvUTF32LE(), txtUtf32le, sizeof(txtUtf32le)); | |
264 | TestInput(wxCSConv(wxFONTENCODING_UTF32LE), txtUtf32le, sizeof(txtUtf32le)); | |
265 | } | |
266 | ||
267 | void TextStreamTestCase::TestEmbeddedZerosUTF32BEInput() | |
268 | { | |
269 | TestInput(wxMBConvUTF32BE(), txtUtf32be, sizeof(txtUtf32be)); | |
270 | TestInput(wxCSConv(wxFONTENCODING_UTF32BE), txtUtf32be, sizeof(txtUtf32be)); | |
271 | } | |
272 | ||
273 | void TextStreamTestCase::TestInput(const wxMBConv& conv, | |
274 | const void *encodedText, | |
275 | size_t encodedSize) | |
276 | { | |
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 | ||
288 | CPPUNIT_ASSERT_EQUAL( 0, memcmp(txtWchar, temp.wc_str(), sizeof(txtWchar)) ); | |
289 | } | |
290 | ||
291 | #endif // wxUSE_UNICODE |