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