]>
Commit | Line | Data |
---|---|---|
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: | |
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 ); | |
49 | #if wxUSE_UNICODE | |
50 | CPPUNIT_TEST( ReadUTF8 ); | |
51 | CPPUNIT_TEST( ReadUTF16 ); | |
52 | #endif // wxUSE_UNICODE | |
53 | CPPUNIT_TEST( ReadBig ); | |
54 | CPPUNIT_TEST_SUITE_END(); | |
55 | ||
56 | void ReadEmpty(); | |
57 | void ReadDOS(); | |
58 | void ReadUnix(); | |
59 | void ReadMac(); | |
60 | void ReadMixed(); | |
61 | #if wxUSE_UNICODE | |
62 | void ReadUTF8(); | |
63 | void ReadUTF16(); | |
64 | #endif // wxUSE_UNICODE | |
65 | void ReadBig(); | |
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 | |
71 | static void CreateTestFile(const char *contents) | |
72 | { | |
73 | CreateTestFile(strlen(contents), contents); | |
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); | |
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 | ||
87 | // also include in its own registry so that these tests can be run alone | |
88 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TextFileTestCase, "TextFileTestCase" ); | |
89 | ||
90 | void TextFileTestCase::CreateTestFile(size_t len, const char *contents) | |
91 | { | |
92 | FILE *f = fopen(GetTestFileName(), "wb"); | |
93 | CPPUNIT_ASSERT( f ); | |
94 | ||
95 | CPPUNIT_ASSERT_EQUAL( len, fwrite(contents, 1, len, f) ); | |
96 | CPPUNIT_ASSERT_EQUAL( 0, fclose(f) ); | |
97 | } | |
98 | ||
99 | void TextFileTestCase::ReadEmpty() | |
100 | { | |
101 | CreateTestFile(""); | |
102 | ||
103 | wxTextFile f; | |
104 | CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) ); | |
105 | ||
106 | CPPUNIT_ASSERT_EQUAL( (size_t)0, f.GetLineCount() ); | |
107 | } | |
108 | ||
109 | void TextFileTestCase::ReadDOS() | |
110 | { | |
111 | CreateTestFile("foo\r\nbar\r\nbaz"); | |
112 | ||
113 | wxTextFile f; | |
114 | CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) ); | |
115 | ||
116 | CPPUNIT_ASSERT_EQUAL( (size_t)3, f.GetLineCount() ); | |
117 | CPPUNIT_ASSERT_EQUAL( wxTextFileType_Dos, f.GetLineType(0) ); | |
118 | CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(2) ); | |
119 | CPPUNIT_ASSERT_EQUAL( wxString(wxT("bar")), f.GetLine(1) ); | |
120 | CPPUNIT_ASSERT_EQUAL( wxString(wxT("baz")), f.GetLastLine() ); | |
121 | } | |
122 | ||
123 | void TextFileTestCase::ReadUnix() | |
124 | { | |
125 | CreateTestFile("foo\nbar\nbaz"); | |
126 | ||
127 | wxTextFile f; | |
128 | CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) ); | |
129 | ||
130 | CPPUNIT_ASSERT_EQUAL( (size_t)3, f.GetLineCount() ); | |
131 | CPPUNIT_ASSERT_EQUAL( wxTextFileType_Unix, f.GetLineType(0) ); | |
132 | CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(2) ); | |
133 | CPPUNIT_ASSERT_EQUAL( wxString(wxT("bar")), f.GetLine(1) ); | |
134 | CPPUNIT_ASSERT_EQUAL( wxString(wxT("baz")), f.GetLastLine() ); | |
135 | } | |
136 | ||
137 | void TextFileTestCase::ReadMac() | |
138 | { | |
139 | CreateTestFile("foo\rbar\rbaz"); | |
140 | ||
141 | wxTextFile f; | |
142 | CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) ); | |
143 | ||
144 | CPPUNIT_ASSERT_EQUAL( (size_t)3, f.GetLineCount() ); | |
145 | CPPUNIT_ASSERT_EQUAL( wxTextFileType_Mac, f.GetLineType(0) ); | |
146 | CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(2) ); | |
147 | CPPUNIT_ASSERT_EQUAL( wxString(wxT("bar")), f.GetLine(1) ); | |
148 | CPPUNIT_ASSERT_EQUAL( wxString(wxT("baz")), f.GetLastLine() ); | |
149 | } | |
150 | ||
151 | void TextFileTestCase::ReadMixed() | |
152 | { | |
153 | CreateTestFile("foo\rbar\r\nbaz\n"); | |
154 | ||
155 | wxTextFile f; | |
156 | CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) ); | |
157 | ||
158 | CPPUNIT_ASSERT_EQUAL( (size_t)3, f.GetLineCount() ); | |
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) ); | |
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() ); | |
165 | } | |
166 | ||
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 | ||
177 | CPPUNIT_ASSERT_EQUAL( (size_t)2, f.GetLineCount() ); | |
178 | CPPUNIT_ASSERT_EQUAL( wxTextFileType_Unix, f.GetLineType(0) ); | |
179 | CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(1) ); | |
180 | #ifdef wxHAVE_U_ESCAPE | |
181 | CPPUNIT_ASSERT_EQUAL( wxString(L"\u041f"), f.GetFirstLine() ); | |
182 | CPPUNIT_ASSERT_EQUAL( wxString(L"\u0440\u0438\u0432\u0435\u0442"), | |
183 | f.GetLastLine() ); | |
184 | #endif // wxHAVE_U_ESCAPE | |
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; | |
194 | wxMBConvUTF16LE conv; | |
195 | CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName()), conv) ); | |
196 | ||
197 | CPPUNIT_ASSERT_EQUAL( (size_t)2, f.GetLineCount() ); | |
198 | CPPUNIT_ASSERT_EQUAL( wxTextFileType_Dos, f.GetLineType(0) ); | |
199 | CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(1) ); | |
200 | ||
201 | #ifdef wxHAVE_U_ESCAPE | |
202 | CPPUNIT_ASSERT_EQUAL( wxString(L"\u041f"), f.GetFirstLine() ); | |
203 | CPPUNIT_ASSERT_EQUAL( wxString(L"\u0440\u0438\u0432\u0435\u0442"), | |
204 | f.GetLastLine() ); | |
205 | #endif // wxHAVE_U_ESCAPE | |
206 | } | |
207 | ||
208 | #endif // wxUSE_UNICODE | |
209 | ||
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 | ||
233 | #endif // wxUSE_TEXTFILE | |
234 |