]> git.saurik.com Git - wxWidgets.git/blob - tests/textfile/textfiletest.cpp
define wxHAS_ATOMIC_OPS only if native implementation is available; use fewer loop...
[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 // ----------------------------------------------------------------------------
29 // test class
30 // ----------------------------------------------------------------------------
31
32 class TextFileTestCase : public CppUnit::TestCase
33 {
34 public:
35 TextFileTestCase() { }
36
37 virtual void tearDown() { unlink(GetTestFileName()); }
38
39 private:
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 );
46 #if wxUSE_UNICODE
47 CPPUNIT_TEST( ReadUTF8 );
48 CPPUNIT_TEST( ReadUTF16 );
49 #endif // wxUSE_UNICODE
50 CPPUNIT_TEST( ReadBig );
51 CPPUNIT_TEST_SUITE_END();
52
53 void ReadEmpty();
54 void ReadDOS();
55 void ReadUnix();
56 void ReadMac();
57 void ReadMixed();
58 #if wxUSE_UNICODE
59 void ReadUTF8();
60 void ReadUTF16();
61 #endif // wxUSE_UNICODE
62 void ReadBig();
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
68 static void CreateTestFile(const char *contents)
69 {
70 CreateTestFile(strlen(contents), contents);
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);
76
77
78 DECLARE_NO_COPY_CLASS(TextFileTestCase)
79 };
80
81 // register in the unnamed registry so that these tests are run by default
82 CPPUNIT_TEST_SUITE_REGISTRATION( TextFileTestCase );
83
84 // also include in it's own registry so that these tests can be run alone
85 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TextFileTestCase, "TextFileTestCase" );
86
87 void TextFileTestCase::CreateTestFile(size_t len, const char *contents)
88 {
89 FILE *f = fopen(GetTestFileName(), "wb");
90 CPPUNIT_ASSERT( f );
91
92 CPPUNIT_ASSERT( fwrite(contents, 1, len, f) >= 0 );
93 CPPUNIT_ASSERT( fclose(f) == 0 );
94 }
95
96 void TextFileTestCase::ReadEmpty()
97 {
98 CreateTestFile("");
99
100 wxTextFile f;
101 CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
102
103 CPPUNIT_ASSERT_EQUAL( (size_t)0, f.GetLineCount() );
104 }
105
106 void TextFileTestCase::ReadDOS()
107 {
108 CreateTestFile("foo\r\nbar\r\nbaz");
109
110 wxTextFile f;
111 CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
112
113 CPPUNIT_ASSERT_EQUAL( (size_t)3, f.GetLineCount() );
114 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Dos, f.GetLineType(0) );
115 CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(2) );
116 CPPUNIT_ASSERT_EQUAL( wxString(_T("bar")), f.GetLine(1) );
117 CPPUNIT_ASSERT_EQUAL( wxString(_T("baz")), f.GetLastLine() );
118 }
119
120 void TextFileTestCase::ReadUnix()
121 {
122 CreateTestFile("foo\nbar\nbaz");
123
124 wxTextFile f;
125 CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
126
127 CPPUNIT_ASSERT_EQUAL( (size_t)3, f.GetLineCount() );
128 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Unix, f.GetLineType(0) );
129 CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(2) );
130 CPPUNIT_ASSERT_EQUAL( wxString(_T("bar")), f.GetLine(1) );
131 CPPUNIT_ASSERT_EQUAL( wxString(_T("baz")), f.GetLastLine() );
132 }
133
134 void TextFileTestCase::ReadMac()
135 {
136 CreateTestFile("foo\rbar\rbaz");
137
138 wxTextFile f;
139 CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
140
141 CPPUNIT_ASSERT_EQUAL( (size_t)3, f.GetLineCount() );
142 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Mac, f.GetLineType(0) );
143 CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(2) );
144 CPPUNIT_ASSERT_EQUAL( wxString(_T("bar")), f.GetLine(1) );
145 CPPUNIT_ASSERT_EQUAL( wxString(_T("baz")), f.GetLastLine() );
146 }
147
148 void TextFileTestCase::ReadMixed()
149 {
150 CreateTestFile("foo\rbar\r\nbaz\n");
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_Mac, f.GetLineType(0) );
157 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Dos, f.GetLineType(1) );
158 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Unix, f.GetLineType(2) );
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() );
162 }
163
164 #if wxUSE_UNICODE
165
166 void 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
174 CPPUNIT_ASSERT_EQUAL( (size_t)2, f.GetLineCount() );
175 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Unix, f.GetLineType(0) );
176 CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(1) );
177 #ifdef wxHAVE_U_ESCAPE
178 CPPUNIT_ASSERT_EQUAL( wxString(L"\u041f"), f.GetFirstLine() );
179 CPPUNIT_ASSERT_EQUAL( wxString(L"\u0440\u0438\u0432\u0435\u0442"),
180 f.GetLastLine() );
181 #endif // wxHAVE_U_ESCAPE
182 }
183
184 void 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;
191 wxMBConvUTF16LE conv;
192 CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName()), conv) );
193
194 CPPUNIT_ASSERT_EQUAL( (size_t)2, f.GetLineCount() );
195 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Dos, f.GetLineType(0) );
196 CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(1) );
197
198 #ifdef wxHAVE_U_ESCAPE
199 CPPUNIT_ASSERT_EQUAL( wxString(L"\u041f"), f.GetFirstLine() );
200 CPPUNIT_ASSERT_EQUAL( wxString(L"\u0440\u0438\u0432\u0435\u0442"),
201 f.GetLastLine() );
202 #endif // wxHAVE_U_ESCAPE
203 }
204
205 #endif // wxUSE_UNICODE
206
207 void 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
230 #endif // wxUSE_TEXTFILE
231