]> git.saurik.com Git - wxWidgets.git/blob - tests/streams/textstreamtest.cpp
Fix install_name_tool calls in OS X "make install".
[wxWidgets.git] / tests / streams / textstreamtest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/streams/textstreamtest.cpp
3 // Purpose: wxTextXXXStream unit test
4 // Author: Ryan Norton, Vince Harron
5 // Created: 2004-08-14
6 // Copyright: (c) 2004 Ryan Norton, (c) 2006 Vince Harron
7 ///////////////////////////////////////////////////////////////////////////////
8
9 // ----------------------------------------------------------------------------
10 // headers
11 // ----------------------------------------------------------------------------
12
13 #include "testprec.h"
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
26 #if wxUSE_LONGLONG
27 #include "wx/longlong.h"
28 #endif
29
30 #if wxUSE_UNICODE
31 #include "wx/mstream.h"
32 #endif // wxUSE_UNICODE
33
34 // ----------------------------------------------------------------------------
35 // test class
36 // ----------------------------------------------------------------------------
37
38 class TextStreamTestCase : public CppUnit::TestCase
39 {
40 public:
41 TextStreamTestCase();
42
43 private:
44 CPPUNIT_TEST_SUITE( TextStreamTestCase );
45 CPPUNIT_TEST( Endline );
46 CPPUNIT_TEST( MiscTests );
47
48 #if wxUSE_LONGLONG
49 CPPUNIT_TEST( TestLongLong );
50 CPPUNIT_TEST( TestLongLong );
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
60 CPPUNIT_TEST_SUITE_END();
61
62 void Endline();
63 void MiscTests();
64
65 #if wxUSE_LONGLONG
66 void TestLongLong();
67 void TestULongLong();
68 #endif // wxUSE_LONGLONG
69
70 #if wxUSE_UNICODE
71 void TestUTF8Input();
72 void TestEmbeddedZerosUTF16LEInput();
73 void TestEmbeddedZerosUTF16BEInput();
74 void TestEmbeddedZerosUTF32LEInput();
75 void TestEmbeddedZerosUTF32BEInput();
76 void TestInput(const wxMBConv& conv,
77 const void* encodedText,
78 size_t encodedSize );
79 #endif // wxUSE_UNICODE
80
81
82 DECLARE_NO_COPY_CLASS(TextStreamTestCase)
83 };
84
85 // register in the unnamed registry so that these tests are run by default
86 CPPUNIT_TEST_SUITE_REGISTRATION( TextStreamTestCase );
87
88 // also include in its own registry so that these tests can be run alone
89 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TextStreamTestCase, "TextStreamTestCase" );
90
91 TextStreamTestCase::TextStreamTestCase()
92 {
93 }
94
95 #if defined(__WINDOWS__) || defined(__WXPM__)
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
106 void TextStreamTestCase::Endline()
107 {
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)");
112
113 delete pOutText;
114 delete pOutFile;
115
116 wxFileInputStream* pInFile = new wxFileInputStream(wxT("test.txt"));
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 }
126
127 void TextStreamTestCase::MiscTests()
128 {
129 wxString filename = wxT("testdata.fc");
130 wxFileInputStream fsIn(filename);
131 if ( !fsIn.IsOk() )
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
145 #if wxUSE_LONGLONG
146
147 template <typename T>
148 static void DoTestRoundTrip(const T *values, size_t numValues)
149 {
150 {
151 wxFileOutputStream fileOut(wxT("test.txt"));
152 wxTextOutputStream textOut(fileOut);
153
154 for ( size_t n = 0; n < numValues; n++ )
155 {
156 textOut << values[n] << endl;
157 }
158 }
159
160 {
161 wxFileInputStream fileIn(wxT("test.txt"));
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
174 void 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
190 void 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
205 #if wxUSE_UNICODE
206
207 static const wchar_t txtWchar[4] =
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
215 static const unsigned char txtUtf8[6] =
216 {
217 0x41, 0xc4, 0x80, 0x41, 0xc4, 0x80,
218 };
219
220 static const unsigned char txtUtf16le[8] =
221 {
222 0x41, 0x00, 0x00, 0x01, 0x41, 0x00, 0x00, 0x01,
223 };
224
225 static const unsigned char txtUtf16be[8] =
226 {
227 0x00, 0x41, 0x01, 0x00, 0x00, 0x41, 0x01, 0x00,
228 };
229
230 static const unsigned char txtUtf32le[16] =
231 {
232 0x41, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
233 0x41, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
234 };
235
236 static const unsigned char txtUtf32be[16] =
237 {
238 0x00, 0x00, 0x00, 0x41, 0x00, 0x00, 0x01, 0x00,
239 0x00, 0x00, 0x00, 0x41, 0x00, 0x00, 0x01, 0x00,
240 };
241
242 void TextStreamTestCase::TestUTF8Input()
243 {
244 TestInput(wxConvUTF8, txtUtf8, sizeof(txtUtf8));
245 TestInput(wxCSConv(wxFONTENCODING_UTF8), txtUtf8, sizeof(txtUtf8));
246 }
247
248 void TextStreamTestCase::TestEmbeddedZerosUTF16LEInput()
249 {
250 TestInput(wxMBConvUTF16LE(), txtUtf16le, sizeof(txtUtf16le));
251 TestInput(wxCSConv(wxFONTENCODING_UTF16LE), txtUtf16le, sizeof(txtUtf16le));
252 }
253
254 void TextStreamTestCase::TestEmbeddedZerosUTF16BEInput()
255 {
256 TestInput(wxMBConvUTF16BE(), txtUtf16be, sizeof(txtUtf16be));
257 TestInput(wxCSConv(wxFONTENCODING_UTF16BE), txtUtf16be, sizeof(txtUtf16be));
258 }
259
260 void TextStreamTestCase::TestEmbeddedZerosUTF32LEInput()
261 {
262 TestInput(wxMBConvUTF32LE(), txtUtf32le, sizeof(txtUtf32le));
263 TestInput(wxCSConv(wxFONTENCODING_UTF32LE), txtUtf32le, sizeof(txtUtf32le));
264 }
265
266 void TextStreamTestCase::TestEmbeddedZerosUTF32BEInput()
267 {
268 TestInput(wxMBConvUTF32BE(), txtUtf32be, sizeof(txtUtf32be));
269 TestInput(wxCSConv(wxFONTENCODING_UTF32BE), txtUtf32be, sizeof(txtUtf32be));
270 }
271
272 void TextStreamTestCase::TestInput(const wxMBConv& conv,
273 const void *encodedText,
274 size_t encodedSize)
275 {
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
287 CPPUNIT_ASSERT_EQUAL( 0, memcmp(txtWchar, temp.wc_str(), sizeof(txtWchar)) );
288 }
289
290 #endif // wxUSE_UNICODE