1 ///////////////////////////////////////////////////////////////////////////////
2 // Name:        tests/textfile/textfile.cpp
3 // Purpose:     wxTextFile unit test
4 // Author:      Vadim Zeitlin
5 // Created:     2006-03-31
6 // Copyright:   (c) 2006 Vadim Zeitlin
7 ///////////////////////////////////////////////////////////////////////////////
8 
9 // ----------------------------------------------------------------------------
10 // headers
11 // ----------------------------------------------------------------------------
12 
13 #include "testprec.h"
14 
15 #ifdef __BORLANDC__
16     #pragma hdrstop
17 #endif
18 
19 #if wxUSE_TEXTFILE
20 
21 #ifndef WX_PRECOMP
22 #endif // WX_PRECOMP
23 
24 #include "wx/ffile.h"
25 #include "wx/textfile.h"
26 
27 #ifdef __VISUALC__
28     #define unlink _unlink
29 #endif
30 
31 // ----------------------------------------------------------------------------
32 // test class
33 // ----------------------------------------------------------------------------
34 
35 class TextFileTestCase : public CppUnit::TestCase
36 {
37 public:
TextFileTestCase()38     TextFileTestCase()
39     {
40         srand((unsigned)time(NULL));
41     }
42 
tearDown()43     virtual void tearDown() { unlink(GetTestFileName()); }
44 
45 private:
46     CPPUNIT_TEST_SUITE( TextFileTestCase );
47         CPPUNIT_TEST( ReadEmpty );
48         CPPUNIT_TEST( ReadDOS );
49         CPPUNIT_TEST( ReadDOSLast );
50         CPPUNIT_TEST( ReadUnix );
51         CPPUNIT_TEST( ReadUnixLast );
52         CPPUNIT_TEST( ReadMac );
53         CPPUNIT_TEST( ReadMacLast );
54         CPPUNIT_TEST( ReadMixed );
55         CPPUNIT_TEST( ReadMixedWithFuzzing );
56         CPPUNIT_TEST( ReadCRCRLF );
57 #if wxUSE_UNICODE
58         CPPUNIT_TEST( ReadUTF8 );
59         CPPUNIT_TEST( ReadUTF16 );
60 #endif // wxUSE_UNICODE
61         CPPUNIT_TEST( ReadBig );
62     CPPUNIT_TEST_SUITE_END();
63 
64     void ReadEmpty();
65     void ReadDOS();
66     void ReadDOSLast();
67     void ReadUnix();
68     void ReadUnixLast();
69     void ReadMac();
70     void ReadMacLast();
71     void ReadMixed();
72     void ReadMixedWithFuzzing();
73     void ReadCRCRLF();
74 #if wxUSE_UNICODE
75     void ReadUTF8();
76     void ReadUTF16();
77 #endif // wxUSE_UNICODE
78     void ReadBig();
79 
80     // return the name of the test file we use
GetTestFileName()81     static const char *GetTestFileName() { return "textfiletest.txt"; }
82 
83     // create the test file with the given contents
CreateTestFile(const char * contents)84     static void CreateTestFile(const char *contents)
85     {
86         CreateTestFile(strlen(contents), contents);
87     }
88 
89     // create the test file with the given contents (version must be used if
90     // contents contains NULs)
91     static void CreateTestFile(size_t len, const char *contents);
92 
93 
94     DECLARE_NO_COPY_CLASS(TextFileTestCase)
95 };
96 
97 // register in the unnamed registry so that these tests are run by default
98 CPPUNIT_TEST_SUITE_REGISTRATION( TextFileTestCase );
99 
100 // also include in its own registry so that these tests can be run alone
101 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TextFileTestCase, "TextFileTestCase" );
102 
CreateTestFile(size_t len,const char * contents)103 void TextFileTestCase::CreateTestFile(size_t len, const char *contents)
104 {
105     FILE *f = fopen(GetTestFileName(), "wb");
106     CPPUNIT_ASSERT( f );
107 
108     CPPUNIT_ASSERT_EQUAL( len, fwrite(contents, 1, len, f) );
109     CPPUNIT_ASSERT_EQUAL( 0, fclose(f) );
110 }
111 
ReadEmpty()112 void TextFileTestCase::ReadEmpty()
113 {
114     CreateTestFile("");
115 
116     wxTextFile f;
117     CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
118 
119     CPPUNIT_ASSERT_EQUAL( (size_t)0, f.GetLineCount() );
120 }
121 
ReadDOS()122 void TextFileTestCase::ReadDOS()
123 {
124     CreateTestFile("foo\r\nbar\r\nbaz");
125 
126     wxTextFile f;
127     CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
128 
129     CPPUNIT_ASSERT_EQUAL( (size_t)3, f.GetLineCount() );
130     CPPUNIT_ASSERT_EQUAL( wxTextFileType_Dos, f.GetLineType(0) );
131     CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(2) );
132     CPPUNIT_ASSERT_EQUAL( wxString(wxT("bar")), f.GetLine(1) );
133     CPPUNIT_ASSERT_EQUAL( wxString(wxT("baz")), f.GetLastLine() );
134 }
135 
ReadDOSLast()136 void TextFileTestCase::ReadDOSLast()
137 {
138     CreateTestFile("foo\r\n");
139 
140     wxTextFile f;
141     CPPUNIT_ASSERT( f.Open(GetTestFileName()) );
142 
143     CPPUNIT_ASSERT_EQUAL( 1, f.GetLineCount() );
144     CPPUNIT_ASSERT_EQUAL( wxTextFileType_Dos, f.GetLineType(0) );
145     CPPUNIT_ASSERT_EQUAL( "foo", f.GetFirstLine() );
146 }
147 
ReadUnix()148 void TextFileTestCase::ReadUnix()
149 {
150     CreateTestFile("foo\nbar\nbaz");
151 
152     wxTextFile f;
153     CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
154 
155     CPPUNIT_ASSERT_EQUAL( (size_t)3, f.GetLineCount() );
156     CPPUNIT_ASSERT_EQUAL( wxTextFileType_Unix, f.GetLineType(0) );
157     CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(2) );
158     CPPUNIT_ASSERT_EQUAL( wxString(wxT("bar")), f.GetLine(1) );
159     CPPUNIT_ASSERT_EQUAL( wxString(wxT("baz")), f.GetLastLine() );
160 }
161 
ReadUnixLast()162 void TextFileTestCase::ReadUnixLast()
163 {
164     CreateTestFile("foo\n");
165 
166     wxTextFile f;
167     CPPUNIT_ASSERT( f.Open(GetTestFileName()) );
168 
169     CPPUNIT_ASSERT_EQUAL( 1, f.GetLineCount() );
170     CPPUNIT_ASSERT_EQUAL( wxTextFileType_Unix, f.GetLineType(0) );
171     CPPUNIT_ASSERT_EQUAL( "foo", f.GetFirstLine() );
172 }
173 
ReadMac()174 void TextFileTestCase::ReadMac()
175 {
176     CreateTestFile("foo\rbar\r\rbaz");
177 
178     wxTextFile f;
179     CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
180 
181     CPPUNIT_ASSERT_EQUAL( (size_t)4, f.GetLineCount() );
182     CPPUNIT_ASSERT_EQUAL( wxTextFileType_Mac, f.GetLineType(0) );
183     CPPUNIT_ASSERT_EQUAL( wxTextFileType_Mac, f.GetLineType(1) );
184     CPPUNIT_ASSERT_EQUAL( wxTextFileType_Mac, f.GetLineType(2) );
185     CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(3) );
186     CPPUNIT_ASSERT_EQUAL( wxString(wxT("foo")), f.GetLine(0) );
187     CPPUNIT_ASSERT_EQUAL( wxString(wxT("bar")), f.GetLine(1) );
188     CPPUNIT_ASSERT_EQUAL( wxString(wxT("")), f.GetLine(2) );
189     CPPUNIT_ASSERT_EQUAL( wxString(wxT("baz")), f.GetLastLine() );
190 }
191 
ReadMacLast()192 void TextFileTestCase::ReadMacLast()
193 {
194     CreateTestFile("foo\r");
195 
196     wxTextFile f;
197     CPPUNIT_ASSERT( f.Open(GetTestFileName()) );
198 
199     CPPUNIT_ASSERT_EQUAL( 1, f.GetLineCount() );
200     CPPUNIT_ASSERT_EQUAL( wxTextFileType_Mac, f.GetLineType(0) );
201     CPPUNIT_ASSERT_EQUAL( "foo", f.GetFirstLine() );
202 }
203 
ReadMixed()204 void TextFileTestCase::ReadMixed()
205 {
206     CreateTestFile("foo\rbar\r\nbaz\n");
207 
208     wxTextFile f;
209     CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
210 
211     CPPUNIT_ASSERT_EQUAL( (size_t)3, f.GetLineCount() );
212     CPPUNIT_ASSERT_EQUAL( wxTextFileType_Mac, f.GetLineType(0) );
213     CPPUNIT_ASSERT_EQUAL( wxTextFileType_Dos, f.GetLineType(1) );
214     CPPUNIT_ASSERT_EQUAL( wxTextFileType_Unix, f.GetLineType(2) );
215     CPPUNIT_ASSERT_EQUAL( wxString(wxT("foo")), f.GetFirstLine() );
216     CPPUNIT_ASSERT_EQUAL( wxString(wxT("bar")), f.GetLine(1) );
217     CPPUNIT_ASSERT_EQUAL( wxString(wxT("baz")), f.GetLastLine() );
218 }
219 
ReadMixedWithFuzzing()220 void TextFileTestCase::ReadMixedWithFuzzing()
221 {
222     for ( int iteration = 0; iteration < 100; iteration++)
223     {
224         // Create a random buffer with lots of newlines. This is intended to catch
225         // bad parsing in unexpected situations such as the one from ReadCRCRLF()
226         // (which is so common it deserves a test of its own).
227         static const char CHOICES[] = {'\r', '\n', 'X'};
228 
229         const size_t BUF_LEN = 100;
230         char data[BUF_LEN + 1];
231         data[0] = 'X';
232         data[BUF_LEN] = '\0';
233         unsigned linesCnt = 0;
234         for ( size_t i = 1; i < BUF_LEN; i++ )
235         {
236             char ch = CHOICES[rand() % WXSIZEOF(CHOICES)];
237             data[i] = ch;
238             if ( ch == '\r' || (ch == '\n' && data[i-1] != '\r') )
239                 linesCnt++;
240         }
241         if (data[BUF_LEN-1] != '\r' && data[BUF_LEN-1] != '\n')
242             linesCnt++; // last line was unterminated
243 
244         CreateTestFile(data);
245 
246         wxTextFile f;
247         CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
248         CPPUNIT_ASSERT_EQUAL( (size_t)linesCnt, f.GetLineCount() );
249     }
250 }
251 
ReadCRCRLF()252 void TextFileTestCase::ReadCRCRLF()
253 {
254     // Notepad may create files with CRCRLF line endings (see
255     // http://stackoverflow.com/questions/6998506/text-file-with-0d-0d-0a-line-breaks).
256     // Older versions of wx would loose all data when reading such files.
257     // Test that the data are read, but don't worry about empty lines in between or
258     // line endings. Also include a longer streak of CRs, because they can
259     // happen as well.
260     CreateTestFile("foo\r\r\nbar\r\r\r\nbaz\r\r\n");
261 
262     wxTextFile f;
263     CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
264 
265     wxString all;
266     for ( wxString str = f.GetFirstLine(); !f.Eof(); str = f.GetNextLine() )
267         all += str;
268 
269     CPPUNIT_ASSERT_EQUAL( "foobarbaz", all );
270 }
271 
272 #if wxUSE_UNICODE
273 
ReadUTF8()274 void TextFileTestCase::ReadUTF8()
275 {
276     CreateTestFile("\xd0\x9f\n"
277                    "\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82");
278 
279     wxTextFile f;
280     CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName()), wxConvUTF8) );
281 
282     CPPUNIT_ASSERT_EQUAL( (size_t)2, f.GetLineCount() );
283     CPPUNIT_ASSERT_EQUAL( wxTextFileType_Unix, f.GetLineType(0) );
284     CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(1) );
285 #ifdef wxHAVE_U_ESCAPE
286     CPPUNIT_ASSERT_EQUAL( wxString(L"\u041f"), f.GetFirstLine() );
287     CPPUNIT_ASSERT_EQUAL( wxString(L"\u0440\u0438\u0432\u0435\u0442"),
288                           f.GetLastLine() );
289 #endif // wxHAVE_U_ESCAPE
290 }
291 
ReadUTF16()292 void TextFileTestCase::ReadUTF16()
293 {
294     CreateTestFile(16,
295                    "\x1f\x04\x0d\x00\x0a\x00"
296                    "\x40\x04\x38\x04\x32\x04\x35\x04\x42\x04");
297 
298     wxTextFile f;
299     wxMBConvUTF16LE conv;
300     CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName()), conv) );
301 
302     CPPUNIT_ASSERT_EQUAL( (size_t)2, f.GetLineCount() );
303     CPPUNIT_ASSERT_EQUAL( wxTextFileType_Dos, f.GetLineType(0) );
304     CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(1) );
305 
306 #ifdef wxHAVE_U_ESCAPE
307     CPPUNIT_ASSERT_EQUAL( wxString(L"\u041f"), f.GetFirstLine() );
308     CPPUNIT_ASSERT_EQUAL( wxString(L"\u0440\u0438\u0432\u0435\u0442"),
309                           f.GetLastLine() );
310 #endif // wxHAVE_U_ESCAPE
311 }
312 
313 #endif // wxUSE_UNICODE
314 
ReadBig()315 void TextFileTestCase::ReadBig()
316 {
317     static const size_t NUM_LINES = 10000;
318 
319     {
320         wxFFile f(GetTestFileName(), "w");
321         for ( size_t n = 0; n < NUM_LINES; n++ )
322         {
323             fprintf(f.fp(), "Line %lu\n", (unsigned long)n + 1);
324         }
325     }
326 
327     wxTextFile f;
328     CPPUNIT_ASSERT( f.Open(GetTestFileName()) );
329 
330     CPPUNIT_ASSERT_EQUAL( NUM_LINES, f.GetLineCount() );
331     CPPUNIT_ASSERT_EQUAL( wxString("Line 1"), f[0] );
332     CPPUNIT_ASSERT_EQUAL( wxString("Line 999"), f[998] );
333     CPPUNIT_ASSERT_EQUAL( wxString("Line 1000"), f[999] );
334     CPPUNIT_ASSERT_EQUAL( wxString::Format("Line %lu", (unsigned long)NUM_LINES),
335                           f[NUM_LINES - 1] );
336 }
337 
338 #endif // wxUSE_TEXTFILE
339 
340