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