]>
git.saurik.com Git - wxWidgets.git/blob - tests/textfile/textfiletest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/textfile/textfile.cpp
3 // Purpose: wxTextFile unit test
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2006 Vadim Zeitlin
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
26 #include "wx/textfile.h"
29 #define unlink _unlink
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
36 class TextFileTestCase
: public CppUnit::TestCase
39 TextFileTestCase() { }
41 virtual void tearDown() { unlink(GetTestFileName()); }
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
);
51 CPPUNIT_TEST( ReadUTF8
);
52 CPPUNIT_TEST( ReadUTF16
);
53 #endif // wxUSE_UNICODE
54 CPPUNIT_TEST( ReadBig
);
55 CPPUNIT_TEST_SUITE_END();
65 #endif // wxUSE_UNICODE
68 // return the name of the test file we use
69 static const char *GetTestFileName() { return "textfiletest.txt"; }
71 // create the test file with the given contents
72 static void CreateTestFile(const char *contents
)
74 CreateTestFile(strlen(contents
), contents
);
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
);
82 DECLARE_NO_COPY_CLASS(TextFileTestCase
)
85 // register in the unnamed registry so that these tests are run by default
86 CPPUNIT_TEST_SUITE_REGISTRATION( TextFileTestCase
);
88 // also include in it's own registry so that these tests can be run alone
89 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TextFileTestCase
, "TextFileTestCase" );
91 void TextFileTestCase::CreateTestFile(size_t len
, const char *contents
)
93 FILE *f
= fopen(GetTestFileName(), "wb");
96 CPPUNIT_ASSERT_EQUAL( len
, fwrite(contents
, 1, len
, f
) );
97 CPPUNIT_ASSERT_EQUAL( 0, fclose(f
) );
100 void TextFileTestCase::ReadEmpty()
105 CPPUNIT_ASSERT( f
.Open(wxString::FromAscii(GetTestFileName())) );
107 CPPUNIT_ASSERT_EQUAL( (size_t)0, f
.GetLineCount() );
110 void TextFileTestCase::ReadDOS()
112 CreateTestFile("foo\r\nbar\r\nbaz");
115 CPPUNIT_ASSERT( f
.Open(wxString::FromAscii(GetTestFileName())) );
117 CPPUNIT_ASSERT_EQUAL( (size_t)3, f
.GetLineCount() );
118 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Dos
, f
.GetLineType(0) );
119 CPPUNIT_ASSERT_EQUAL( wxTextFileType_None
, f
.GetLineType(2) );
120 CPPUNIT_ASSERT_EQUAL( wxString(wxT("bar")), f
.GetLine(1) );
121 CPPUNIT_ASSERT_EQUAL( wxString(wxT("baz")), f
.GetLastLine() );
124 void TextFileTestCase::ReadUnix()
126 CreateTestFile("foo\nbar\nbaz");
129 CPPUNIT_ASSERT( f
.Open(wxString::FromAscii(GetTestFileName())) );
131 CPPUNIT_ASSERT_EQUAL( (size_t)3, f
.GetLineCount() );
132 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Unix
, f
.GetLineType(0) );
133 CPPUNIT_ASSERT_EQUAL( wxTextFileType_None
, f
.GetLineType(2) );
134 CPPUNIT_ASSERT_EQUAL( wxString(wxT("bar")), f
.GetLine(1) );
135 CPPUNIT_ASSERT_EQUAL( wxString(wxT("baz")), f
.GetLastLine() );
138 void TextFileTestCase::ReadMac()
140 CreateTestFile("foo\rbar\rbaz");
143 CPPUNIT_ASSERT( f
.Open(wxString::FromAscii(GetTestFileName())) );
145 CPPUNIT_ASSERT_EQUAL( (size_t)3, f
.GetLineCount() );
146 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Mac
, f
.GetLineType(0) );
147 CPPUNIT_ASSERT_EQUAL( wxTextFileType_None
, f
.GetLineType(2) );
148 CPPUNIT_ASSERT_EQUAL( wxString(wxT("bar")), f
.GetLine(1) );
149 CPPUNIT_ASSERT_EQUAL( wxString(wxT("baz")), f
.GetLastLine() );
152 void TextFileTestCase::ReadMixed()
154 CreateTestFile("foo\rbar\r\nbaz\n");
157 CPPUNIT_ASSERT( f
.Open(wxString::FromAscii(GetTestFileName())) );
159 CPPUNIT_ASSERT_EQUAL( (size_t)3, f
.GetLineCount() );
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) );
163 CPPUNIT_ASSERT_EQUAL( wxString(wxT("foo")), f
.GetFirstLine() );
164 CPPUNIT_ASSERT_EQUAL( wxString(wxT("bar")), f
.GetLine(1) );
165 CPPUNIT_ASSERT_EQUAL( wxString(wxT("baz")), f
.GetLastLine() );
170 void TextFileTestCase::ReadUTF8()
172 CreateTestFile("\xd0\x9f\n"
173 "\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82");
176 CPPUNIT_ASSERT( f
.Open(wxString::FromAscii(GetTestFileName()), wxConvUTF8
) );
178 CPPUNIT_ASSERT_EQUAL( (size_t)2, f
.GetLineCount() );
179 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Unix
, f
.GetLineType(0) );
180 CPPUNIT_ASSERT_EQUAL( wxTextFileType_None
, f
.GetLineType(1) );
181 #ifdef wxHAVE_U_ESCAPE
182 CPPUNIT_ASSERT_EQUAL( wxString(L
"\u041f"), f
.GetFirstLine() );
183 CPPUNIT_ASSERT_EQUAL( wxString(L
"\u0440\u0438\u0432\u0435\u0442"),
185 #endif // wxHAVE_U_ESCAPE
188 void TextFileTestCase::ReadUTF16()
191 "\x1f\x04\x0d\x00\x0a\x00"
192 "\x40\x04\x38\x04\x32\x04\x35\x04\x42\x04");
195 wxMBConvUTF16LE conv
;
196 CPPUNIT_ASSERT( f
.Open(wxString::FromAscii(GetTestFileName()), conv
) );
198 CPPUNIT_ASSERT_EQUAL( (size_t)2, f
.GetLineCount() );
199 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Dos
, f
.GetLineType(0) );
200 CPPUNIT_ASSERT_EQUAL( wxTextFileType_None
, f
.GetLineType(1) );
202 #ifdef wxHAVE_U_ESCAPE
203 CPPUNIT_ASSERT_EQUAL( wxString(L
"\u041f"), f
.GetFirstLine() );
204 CPPUNIT_ASSERT_EQUAL( wxString(L
"\u0440\u0438\u0432\u0435\u0442"),
206 #endif // wxHAVE_U_ESCAPE
209 #endif // wxUSE_UNICODE
211 void TextFileTestCase::ReadBig()
213 static const size_t NUM_LINES
= 10000;
216 wxFFile
f(GetTestFileName(), "w");
217 for ( size_t n
= 0; n
< NUM_LINES
; n
++ )
219 fprintf(f
.fp(), "Line %lu\n", (unsigned long)n
+ 1);
224 CPPUNIT_ASSERT( f
.Open(GetTestFileName()) );
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
),
234 #endif // wxUSE_TEXTFILE