]> git.saurik.com Git - wxWidgets.git/blame - tests/textfile/textfiletest.cpp
added wxICON_NONE and implement support for it in wxGTK (closes #2897)
[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
50a73bb1
VZ
28#ifdef __VISUALC__
29 #define unlink _unlink
30#endif
31
468c5a97
VZ
32// ----------------------------------------------------------------------------
33// test class
34// ----------------------------------------------------------------------------
35
36class TextFileTestCase : public CppUnit::TestCase
37{
38public:
39 TextFileTestCase() { }
40
41 virtual void tearDown() { unlink(GetTestFileName()); }
42
43private:
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 );
93a57d19
VZ
50#if wxUSE_UNICODE
51 CPPUNIT_TEST( ReadUTF8 );
52 CPPUNIT_TEST( ReadUTF16 );
53#endif // wxUSE_UNICODE
c6d36593 54 CPPUNIT_TEST( ReadBig );
468c5a97
VZ
55 CPPUNIT_TEST_SUITE_END();
56
57 void ReadEmpty();
58 void ReadDOS();
59 void ReadUnix();
60 void ReadMac();
61 void ReadMixed();
93a57d19
VZ
62#if wxUSE_UNICODE
63 void ReadUTF8();
64 void ReadUTF16();
65#endif // wxUSE_UNICODE
c6d36593 66 void ReadBig();
468c5a97
VZ
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
93a57d19
VZ
72 static void CreateTestFile(const char *contents)
73 {
5098c258 74 CreateTestFile(strlen(contents), contents);
93a57d19
VZ
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);
468c5a97
VZ
80
81
82 DECLARE_NO_COPY_CLASS(TextFileTestCase)
83};
84
85// register in the unnamed registry so that these tests are run by default
86CPPUNIT_TEST_SUITE_REGISTRATION( TextFileTestCase );
87
88// also include in it's own registry so that these tests can be run alone
89CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TextFileTestCase, "TextFileTestCase" );
90
93a57d19 91void TextFileTestCase::CreateTestFile(size_t len, const char *contents)
468c5a97
VZ
92{
93 FILE *f = fopen(GetTestFileName(), "wb");
94 CPPUNIT_ASSERT( f );
95
93a800a9
VZ
96 CPPUNIT_ASSERT_EQUAL( len, fwrite(contents, 1, len, f) );
97 CPPUNIT_ASSERT_EQUAL( 0, fclose(f) );
468c5a97
VZ
98}
99
100void TextFileTestCase::ReadEmpty()
101{
102 CreateTestFile("");
103
104 wxTextFile f;
18230eb6 105 CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
468c5a97 106
0739e2ee 107 CPPUNIT_ASSERT_EQUAL( (size_t)0, f.GetLineCount() );
468c5a97
VZ
108}
109
110void TextFileTestCase::ReadDOS()
111{
112 CreateTestFile("foo\r\nbar\r\nbaz");
113
114 wxTextFile f;
18230eb6 115 CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
468c5a97 116
0739e2ee 117 CPPUNIT_ASSERT_EQUAL( (size_t)3, f.GetLineCount() );
468c5a97
VZ
118 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Dos, f.GetLineType(0) );
119 CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(2) );
18230eb6
VZ
120 CPPUNIT_ASSERT_EQUAL( wxString(_T("bar")), f.GetLine(1) );
121 CPPUNIT_ASSERT_EQUAL( wxString(_T("baz")), f.GetLastLine() );
468c5a97
VZ
122}
123
124void TextFileTestCase::ReadUnix()
125{
126 CreateTestFile("foo\nbar\nbaz");
127
128 wxTextFile f;
18230eb6 129 CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
468c5a97 130
0739e2ee 131 CPPUNIT_ASSERT_EQUAL( (size_t)3, f.GetLineCount() );
468c5a97
VZ
132 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Unix, f.GetLineType(0) );
133 CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(2) );
18230eb6
VZ
134 CPPUNIT_ASSERT_EQUAL( wxString(_T("bar")), f.GetLine(1) );
135 CPPUNIT_ASSERT_EQUAL( wxString(_T("baz")), f.GetLastLine() );
468c5a97
VZ
136}
137
138void TextFileTestCase::ReadMac()
139{
140 CreateTestFile("foo\rbar\rbaz");
141
142 wxTextFile f;
18230eb6 143 CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
468c5a97 144
0739e2ee 145 CPPUNIT_ASSERT_EQUAL( (size_t)3, f.GetLineCount() );
468c5a97
VZ
146 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Mac, f.GetLineType(0) );
147 CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(2) );
18230eb6
VZ
148 CPPUNIT_ASSERT_EQUAL( wxString(_T("bar")), f.GetLine(1) );
149 CPPUNIT_ASSERT_EQUAL( wxString(_T("baz")), f.GetLastLine() );
468c5a97
VZ
150}
151
152void TextFileTestCase::ReadMixed()
153{
154 CreateTestFile("foo\rbar\r\nbaz\n");
155
156 wxTextFile f;
18230eb6 157 CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
468c5a97 158
0739e2ee 159 CPPUNIT_ASSERT_EQUAL( (size_t)3, f.GetLineCount() );
468c5a97
VZ
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) );
18230eb6
VZ
163 CPPUNIT_ASSERT_EQUAL( wxString(_T("foo")), f.GetFirstLine() );
164 CPPUNIT_ASSERT_EQUAL( wxString(_T("bar")), f.GetLine(1) );
165 CPPUNIT_ASSERT_EQUAL( wxString(_T("baz")), f.GetLastLine() );
468c5a97
VZ
166}
167
93a57d19
VZ
168#if wxUSE_UNICODE
169
170void 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
0739e2ee 178 CPPUNIT_ASSERT_EQUAL( (size_t)2, f.GetLineCount() );
93a57d19
VZ
179 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Unix, f.GetLineType(0) );
180 CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(1) );
6ef5a7a3 181#ifdef wxHAVE_U_ESCAPE
93a57d19
VZ
182 CPPUNIT_ASSERT_EQUAL( wxString(L"\u041f"), f.GetFirstLine() );
183 CPPUNIT_ASSERT_EQUAL( wxString(L"\u0440\u0438\u0432\u0435\u0442"),
184 f.GetLastLine() );
ed177375 185#endif // wxHAVE_U_ESCAPE
93a57d19
VZ
186}
187
188void 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;
5c763f5a 195 wxMBConvUTF16LE conv;
93a57d19
VZ
196 CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName()), conv) );
197
0739e2ee 198 CPPUNIT_ASSERT_EQUAL( (size_t)2, f.GetLineCount() );
93a57d19
VZ
199 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Dos, f.GetLineType(0) );
200 CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(1) );
ed177375 201
6ef5a7a3 202#ifdef wxHAVE_U_ESCAPE
93a57d19
VZ
203 CPPUNIT_ASSERT_EQUAL( wxString(L"\u041f"), f.GetFirstLine() );
204 CPPUNIT_ASSERT_EQUAL( wxString(L"\u0440\u0438\u0432\u0435\u0442"),
205 f.GetLastLine() );
ed177375 206#endif // wxHAVE_U_ESCAPE
93a57d19
VZ
207}
208
14b4f6fc
VZ
209#endif // wxUSE_UNICODE
210
c6d36593
VZ
211void 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
468c5a97
VZ
234#endif // wxUSE_TEXTFILE
235