]> git.saurik.com Git - wxWidgets.git/blame - tests/textfile/textfiletest.cpp
rename ARRAYSIZE to CELLSARRAYSIZE to avoid conflict with a macro with the same name...
[wxWidgets.git] / tests / textfile / textfiletest.cpp
CommitLineData
468c5a97
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: tests/textfile/textfile.cpp
3// Purpose: wxTextFile unit test
4// Author: Vadim Zeitlin
5// Created: 2006-03-31
6// RCS-ID: $Id$
7// Copyright: (c) 2006 Vadim Zeitlin
8///////////////////////////////////////////////////////////////////////////////
9
10// ----------------------------------------------------------------------------
11// headers
12// ----------------------------------------------------------------------------
13
14#include "testprec.h"
15
16#ifdef __BORLANDC__
17 #pragma hdrstop
18#endif
19
20#if wxUSE_TEXTFILE
21
22#ifndef WX_PRECOMP
23#endif // WX_PRECOMP
24
c6d36593 25#include "wx/ffile.h"
468c5a97
VZ
26#include "wx/textfile.h"
27
28// ----------------------------------------------------------------------------
29// test class
30// ----------------------------------------------------------------------------
31
32class TextFileTestCase : public CppUnit::TestCase
33{
34public:
35 TextFileTestCase() { }
36
37 virtual void tearDown() { unlink(GetTestFileName()); }
38
39private:
40 CPPUNIT_TEST_SUITE( TextFileTestCase );
41 CPPUNIT_TEST( ReadEmpty );
42 CPPUNIT_TEST( ReadDOS );
43 CPPUNIT_TEST( ReadUnix );
44 CPPUNIT_TEST( ReadMac );
45 CPPUNIT_TEST( ReadMixed );
93a57d19
VZ
46#if wxUSE_UNICODE
47 CPPUNIT_TEST( ReadUTF8 );
48 CPPUNIT_TEST( ReadUTF16 );
49#endif // wxUSE_UNICODE
c6d36593 50 CPPUNIT_TEST( ReadBig );
468c5a97
VZ
51 CPPUNIT_TEST_SUITE_END();
52
53 void ReadEmpty();
54 void ReadDOS();
55 void ReadUnix();
56 void ReadMac();
57 void ReadMixed();
93a57d19
VZ
58#if wxUSE_UNICODE
59 void ReadUTF8();
60 void ReadUTF16();
61#endif // wxUSE_UNICODE
c6d36593 62 void ReadBig();
468c5a97
VZ
63
64 // return the name of the test file we use
65 static const char *GetTestFileName() { return "textfiletest.txt"; }
66
67 // create the test file with the given contents
93a57d19
VZ
68 static void CreateTestFile(const char *contents)
69 {
5098c258 70 CreateTestFile(strlen(contents), contents);
93a57d19
VZ
71 }
72
73 // create the test file with the given contents (version must be used if
74 // contents contains NULs)
75 static void CreateTestFile(size_t len, const char *contents);
468c5a97
VZ
76
77
78 DECLARE_NO_COPY_CLASS(TextFileTestCase)
79};
80
81// register in the unnamed registry so that these tests are run by default
82CPPUNIT_TEST_SUITE_REGISTRATION( TextFileTestCase );
83
84// also include in it's own registry so that these tests can be run alone
85CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TextFileTestCase, "TextFileTestCase" );
86
93a57d19 87void TextFileTestCase::CreateTestFile(size_t len, const char *contents)
468c5a97
VZ
88{
89 FILE *f = fopen(GetTestFileName(), "wb");
90 CPPUNIT_ASSERT( f );
91
93a800a9
VZ
92 CPPUNIT_ASSERT_EQUAL( len, fwrite(contents, 1, len, f) );
93 CPPUNIT_ASSERT_EQUAL( 0, fclose(f) );
468c5a97
VZ
94}
95
96void TextFileTestCase::ReadEmpty()
97{
98 CreateTestFile("");
99
100 wxTextFile f;
18230eb6 101 CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
468c5a97 102
0739e2ee 103 CPPUNIT_ASSERT_EQUAL( (size_t)0, f.GetLineCount() );
468c5a97
VZ
104}
105
106void TextFileTestCase::ReadDOS()
107{
108 CreateTestFile("foo\r\nbar\r\nbaz");
109
110 wxTextFile f;
18230eb6 111 CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
468c5a97 112
0739e2ee 113 CPPUNIT_ASSERT_EQUAL( (size_t)3, f.GetLineCount() );
468c5a97
VZ
114 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Dos, f.GetLineType(0) );
115 CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(2) );
18230eb6
VZ
116 CPPUNIT_ASSERT_EQUAL( wxString(_T("bar")), f.GetLine(1) );
117 CPPUNIT_ASSERT_EQUAL( wxString(_T("baz")), f.GetLastLine() );
468c5a97
VZ
118}
119
120void TextFileTestCase::ReadUnix()
121{
122 CreateTestFile("foo\nbar\nbaz");
123
124 wxTextFile f;
18230eb6 125 CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
468c5a97 126
0739e2ee 127 CPPUNIT_ASSERT_EQUAL( (size_t)3, f.GetLineCount() );
468c5a97
VZ
128 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Unix, f.GetLineType(0) );
129 CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(2) );
18230eb6
VZ
130 CPPUNIT_ASSERT_EQUAL( wxString(_T("bar")), f.GetLine(1) );
131 CPPUNIT_ASSERT_EQUAL( wxString(_T("baz")), f.GetLastLine() );
468c5a97
VZ
132}
133
134void TextFileTestCase::ReadMac()
135{
136 CreateTestFile("foo\rbar\rbaz");
137
138 wxTextFile f;
18230eb6 139 CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
468c5a97 140
0739e2ee 141 CPPUNIT_ASSERT_EQUAL( (size_t)3, f.GetLineCount() );
468c5a97
VZ
142 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Mac, f.GetLineType(0) );
143 CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(2) );
18230eb6
VZ
144 CPPUNIT_ASSERT_EQUAL( wxString(_T("bar")), f.GetLine(1) );
145 CPPUNIT_ASSERT_EQUAL( wxString(_T("baz")), f.GetLastLine() );
468c5a97
VZ
146}
147
148void TextFileTestCase::ReadMixed()
149{
150 CreateTestFile("foo\rbar\r\nbaz\n");
151
152 wxTextFile f;
18230eb6 153 CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
468c5a97 154
0739e2ee 155 CPPUNIT_ASSERT_EQUAL( (size_t)3, f.GetLineCount() );
468c5a97
VZ
156 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Mac, f.GetLineType(0) );
157 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Dos, f.GetLineType(1) );
158 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Unix, f.GetLineType(2) );
18230eb6
VZ
159 CPPUNIT_ASSERT_EQUAL( wxString(_T("foo")), f.GetFirstLine() );
160 CPPUNIT_ASSERT_EQUAL( wxString(_T("bar")), f.GetLine(1) );
161 CPPUNIT_ASSERT_EQUAL( wxString(_T("baz")), f.GetLastLine() );
468c5a97
VZ
162}
163
93a57d19
VZ
164#if wxUSE_UNICODE
165
166void TextFileTestCase::ReadUTF8()
167{
168 CreateTestFile("\xd0\x9f\n"
169 "\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82");
170
171 wxTextFile f;
172 CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName()), wxConvUTF8) );
173
0739e2ee 174 CPPUNIT_ASSERT_EQUAL( (size_t)2, f.GetLineCount() );
93a57d19
VZ
175 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Unix, f.GetLineType(0) );
176 CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(1) );
6ef5a7a3 177#ifdef wxHAVE_U_ESCAPE
93a57d19
VZ
178 CPPUNIT_ASSERT_EQUAL( wxString(L"\u041f"), f.GetFirstLine() );
179 CPPUNIT_ASSERT_EQUAL( wxString(L"\u0440\u0438\u0432\u0435\u0442"),
180 f.GetLastLine() );
ed177375 181#endif // wxHAVE_U_ESCAPE
93a57d19
VZ
182}
183
184void TextFileTestCase::ReadUTF16()
185{
186 CreateTestFile(16,
187 "\x1f\x04\x0d\x00\x0a\x00"
188 "\x40\x04\x38\x04\x32\x04\x35\x04\x42\x04");
189
190 wxTextFile f;
5c763f5a 191 wxMBConvUTF16LE conv;
93a57d19
VZ
192 CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName()), conv) );
193
0739e2ee 194 CPPUNIT_ASSERT_EQUAL( (size_t)2, f.GetLineCount() );
93a57d19
VZ
195 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Dos, f.GetLineType(0) );
196 CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(1) );
ed177375 197
6ef5a7a3 198#ifdef wxHAVE_U_ESCAPE
93a57d19
VZ
199 CPPUNIT_ASSERT_EQUAL( wxString(L"\u041f"), f.GetFirstLine() );
200 CPPUNIT_ASSERT_EQUAL( wxString(L"\u0440\u0438\u0432\u0435\u0442"),
201 f.GetLastLine() );
ed177375 202#endif // wxHAVE_U_ESCAPE
93a57d19
VZ
203}
204
14b4f6fc
VZ
205#endif // wxUSE_UNICODE
206
c6d36593
VZ
207void TextFileTestCase::ReadBig()
208{
209 static const size_t NUM_LINES = 10000;
210
211 {
212 wxFFile f(GetTestFileName(), "w");
213 for ( size_t n = 0; n < NUM_LINES; n++ )
214 {
215 fprintf(f.fp(), "Line %lu\n", (unsigned long)n + 1);
216 }
217 }
218
219 wxTextFile f;
220 CPPUNIT_ASSERT( f.Open(GetTestFileName()) );
221
222 CPPUNIT_ASSERT_EQUAL( NUM_LINES, f.GetLineCount() );
223 CPPUNIT_ASSERT_EQUAL( wxString("Line 1"), f[0] );
224 CPPUNIT_ASSERT_EQUAL( wxString("Line 999"), f[998] );
225 CPPUNIT_ASSERT_EQUAL( wxString("Line 1000"), f[999] );
226 CPPUNIT_ASSERT_EQUAL( wxString::Format("Line %lu", (unsigned long)NUM_LINES),
227 f[NUM_LINES - 1] );
228}
229
468c5a97
VZ
230#endif // wxUSE_TEXTFILE
231