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