]>
Commit | Line | Data |
---|---|---|
ba854691 RN |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: tests/uris/uris.cpp | |
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 ); | |
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(); | |
6f3e46c2 | 63 | |
216a72f3 VZ |
64 | #if wxUSE_LONGLONG |
65 | void TestLongLong(); | |
66 | void TestULongLong(); | |
67 | #endif // wxUSE_LONGLONG | |
ba854691 | 68 | |
6f3e46c2 VZ |
69 | #if wxUSE_UNICODE |
70 | void TestUTF8Input(); | |
71 | void TestEmbeddedZerosUTF16LEInput(); | |
72 | void TestEmbeddedZerosUTF16BEInput(); | |
73 | void TestEmbeddedZerosUTF32LEInput(); | |
74 | void TestEmbeddedZerosUTF32BEInput(); | |
0646b219 | 75 | void TestInput(const wxMBConv& conv, |
6f3e46c2 VZ |
76 | const void* encodedText, |
77 | size_t encodedSize ); | |
78 | #endif // wxUSE_UNICODE | |
79 | ||
ba854691 RN |
80 | |
81 | DECLARE_NO_COPY_CLASS(TextStreamTestCase) | |
82 | }; | |
83 | ||
84 | // register in the unnamed registry so that these tests are run by default | |
85 | CPPUNIT_TEST_SUITE_REGISTRATION( TextStreamTestCase ); | |
86 | ||
87 | // also include in it's own registry so that these tests can be run alone | |
88 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TextStreamTestCase, "TextStreamTestCase" ); | |
89 | ||
90 | TextStreamTestCase::TextStreamTestCase() | |
91 | { | |
92 | } | |
93 | ||
94 | #if defined(__WXMSW__) || defined(__WXPM__) | |
95 | # define NEWLINE "\r\n" | |
96 | # define NEWLINELEN 2 | |
97 | #elif defined(__WXMAC__) && !defined(__DARWIN__) | |
98 | # define NEWLINE "\r" | |
99 | # define NEWLINELEN 1 | |
100 | #else | |
101 | # define NEWLINE "\n" | |
102 | # define NEWLINELEN 1 | |
103 | #endif | |
104 | ||
105 | void TextStreamTestCase::Endline() | |
106 | { | |
107 | wxFileOutputStream* pOutFile = new wxFileOutputStream(_T("test.txt")); | |
108 | wxTextOutputStream* pOutText = new wxTextOutputStream(*pOutFile); | |
6f3e46c2 | 109 | *pOutText << _T("Test text") << endl |
ba854691 RN |
110 | << _T("More Testing Text (There should be newline before this)"); |
111 | ||
112 | delete pOutText; | |
113 | delete pOutFile; | |
114 | ||
115 | wxFileInputStream* pInFile = new wxFileInputStream(_T("test.txt")); | |
116 | ||
117 | char szIn[9 + NEWLINELEN]; | |
118 | ||
119 | pInFile->Read(szIn, 9 + NEWLINELEN); | |
120 | ||
121 | CPPUNIT_ASSERT( memcmp(&szIn[9], NEWLINE, NEWLINELEN) == 0 ); | |
122 | ||
123 | delete pInFile; | |
124 | } | |
216a72f3 VZ |
125 | |
126 | #if wxUSE_LONGLONG | |
127 | ||
128 | template <typename T> | |
129 | static void DoTestRoundTrip(const T *values, size_t numValues) | |
130 | { | |
131 | { | |
132 | wxFileOutputStream fileOut(_T("test.txt")); | |
133 | wxTextOutputStream textOut(fileOut); | |
134 | ||
135 | for ( size_t n = 0; n < numValues; n++ ) | |
136 | { | |
137 | textOut << values[n] << endl; | |
138 | } | |
139 | } | |
140 | ||
141 | { | |
142 | wxFileInputStream fileIn(_T("test.txt")); | |
143 | wxTextInputStream textIn(fileIn); | |
144 | ||
145 | T value; | |
146 | for ( size_t n = 0; n < numValues; n++ ) | |
147 | { | |
148 | textIn >> value; | |
149 | ||
150 | CPPUNIT_ASSERT( value == values[n] ); | |
151 | } | |
152 | } | |
153 | } | |
154 | ||
155 | void TextStreamTestCase::TestLongLong() | |
156 | { | |
157 | static const wxLongLong llvalues[] = | |
158 | { | |
159 | 0, | |
160 | 1, | |
161 | -1, | |
162 | 0x12345678l, | |
163 | -0x12345678l, | |
164 | wxLL(0x123456789abcdef0), | |
165 | wxLL(-0x123456789abcdef0), | |
166 | }; | |
167 | ||
168 | DoTestRoundTrip(llvalues, WXSIZEOF(llvalues)); | |
169 | } | |
170 | ||
171 | void TextStreamTestCase::TestULongLong() | |
172 | { | |
173 | static const wxULongLong ullvalues[] = | |
174 | { | |
175 | 0, | |
176 | 1, | |
177 | 0x12345678l, | |
178 | wxULL(0x123456789abcdef0), | |
179 | }; | |
180 | ||
181 | DoTestRoundTrip(ullvalues, WXSIZEOF(ullvalues)); | |
182 | } | |
183 | ||
184 | #endif // wxUSE_LONGLONG | |
185 | ||
6f3e46c2 VZ |
186 | #if wxUSE_UNICODE |
187 | ||
188 | const static wchar_t txtWchar[4] = | |
189 | { | |
190 | 0x0041, // LATIN CAPITAL LETTER A | |
191 | 0x0100, // A WITH BREVE, LATIN SMALL LETTER | |
192 | 0x0041, // LATIN CAPITAL LETTER A | |
193 | 0x0100, // A WITH BREVE, LATIN SMALL LETTER | |
194 | }; | |
195 | ||
196 | const static unsigned char txtUtf8[6] = | |
197 | { | |
198 | 0x41, 0xc4, 0x80, 0x41, 0xc4, 0x80, | |
199 | }; | |
200 | ||
201 | const static unsigned char txtUtf16le[8] = | |
202 | { | |
203 | 0x41, 0x00, 0x00, 0x01, 0x41, 0x00, 0x00, 0x01, | |
204 | }; | |
205 | ||
206 | const static unsigned char txtUtf16be[8] = | |
207 | { | |
208 | 0x00, 0x41, 0x01, 0x00, 0x00, 0x41, 0x01, 0x00, | |
209 | }; | |
210 | ||
211 | const static unsigned char txtUtf32le[16] = | |
212 | { | |
213 | 0x41, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, | |
214 | 0x41, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, | |
215 | }; | |
216 | ||
217 | const static unsigned char txtUtf32be[16] = | |
218 | { | |
219 | 0x00, 0x00, 0x00, 0x41, 0x00, 0x00, 0x01, 0x00, | |
220 | 0x00, 0x00, 0x00, 0x41, 0x00, 0x00, 0x01, 0x00, | |
221 | }; | |
222 | ||
223 | void TextStreamTestCase::TestUTF8Input() | |
224 | { | |
0646b219 VZ |
225 | TestInput(wxConvUTF8, txtUtf8, sizeof(txtUtf8)); |
226 | TestInput(wxCSConv(wxFONTENCODING_UTF8), txtUtf8, sizeof(txtUtf8)); | |
6f3e46c2 VZ |
227 | } |
228 | ||
229 | void TextStreamTestCase::TestEmbeddedZerosUTF16LEInput() | |
230 | { | |
0646b219 VZ |
231 | TestInput(wxMBConvUTF16LE(), txtUtf16le, sizeof(txtUtf16le)); |
232 | TestInput(wxCSConv(wxFONTENCODING_UTF16LE), txtUtf16le, sizeof(txtUtf16le)); | |
6f3e46c2 VZ |
233 | } |
234 | ||
235 | void TextStreamTestCase::TestEmbeddedZerosUTF16BEInput() | |
236 | { | |
0646b219 VZ |
237 | TestInput(wxMBConvUTF16BE(), txtUtf16be, sizeof(txtUtf16be)); |
238 | TestInput(wxCSConv(wxFONTENCODING_UTF16BE), txtUtf16be, sizeof(txtUtf16be)); | |
6f3e46c2 VZ |
239 | } |
240 | ||
241 | void TextStreamTestCase::TestEmbeddedZerosUTF32LEInput() | |
242 | { | |
0646b219 VZ |
243 | TestInput(wxMBConvUTF32LE(), txtUtf32le, sizeof(txtUtf32le)); |
244 | TestInput(wxCSConv(wxFONTENCODING_UTF32LE), txtUtf32le, sizeof(txtUtf32le)); | |
6f3e46c2 VZ |
245 | } |
246 | ||
247 | void TextStreamTestCase::TestEmbeddedZerosUTF32BEInput() | |
248 | { | |
0646b219 VZ |
249 | TestInput(wxMBConvUTF32BE(), txtUtf32be, sizeof(txtUtf32be)); |
250 | TestInput(wxCSConv(wxFONTENCODING_UTF32BE), txtUtf32be, sizeof(txtUtf32be)); | |
6f3e46c2 VZ |
251 | } |
252 | ||
0646b219 | 253 | void TextStreamTestCase::TestInput(const wxMBConv& conv, |
6f3e46c2 VZ |
254 | const void *encodedText, |
255 | size_t encodedSize) | |
256 | { | |
6f3e46c2 VZ |
257 | wxMemoryInputStream byteIn(encodedText, encodedSize); |
258 | wxTextInputStream textIn(byteIn, wxT("\n"), conv); | |
259 | ||
260 | wxString temp; | |
261 | while ( wxChar c = textIn.GetChar() ) | |
262 | { | |
263 | temp.Append(c); | |
264 | } | |
265 | ||
266 | CPPUNIT_ASSERT_EQUAL( WXSIZEOF(txtWchar), temp.length() ); | |
267 | ||
11aac4ba | 268 | CPPUNIT_ASSERT_EQUAL( 0, memcmp(txtWchar, temp.wc_str(), sizeof(txtWchar)) ); |
6f3e46c2 VZ |
269 | } |
270 | ||
271 | #endif // wxUSE_UNICODE |