]>
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
6 // Copyright: (c) 2006 Vadim Zeitlin
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
25 #include "wx/textfile.h"
28 #define unlink _unlink
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
35 class TextFileTestCase
: public CppUnit::TestCase
38 TextFileTestCase() { }
40 virtual void tearDown() { unlink(GetTestFileName()); }
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
);
50 CPPUNIT_TEST( ReadUTF8
);
51 CPPUNIT_TEST( ReadUTF16
);
52 #endif // wxUSE_UNICODE
53 CPPUNIT_TEST( ReadBig
);
54 CPPUNIT_TEST_SUITE_END();
64 #endif // wxUSE_UNICODE
67 // return the name of the test file we use
68 static const char *GetTestFileName() { return "textfiletest.txt"; }
70 // create the test file with the given contents
71 static void CreateTestFile(const char *contents
)
73 CreateTestFile(strlen(contents
), contents
);
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
);
81 DECLARE_NO_COPY_CLASS(TextFileTestCase
)
84 // register in the unnamed registry so that these tests are run by default
85 CPPUNIT_TEST_SUITE_REGISTRATION( TextFileTestCase
);
87 // also include in its own registry so that these tests can be run alone
88 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TextFileTestCase
, "TextFileTestCase" );
90 void TextFileTestCase::CreateTestFile(size_t len
, const char *contents
)
92 FILE *f
= fopen(GetTestFileName(), "wb");
95 CPPUNIT_ASSERT_EQUAL( len
, fwrite(contents
, 1, len
, f
) );
96 CPPUNIT_ASSERT_EQUAL( 0, fclose(f
) );
99 void TextFileTestCase::ReadEmpty()
104 CPPUNIT_ASSERT( f
.Open(wxString::FromAscii(GetTestFileName())) );
106 CPPUNIT_ASSERT_EQUAL( (size_t)0, f
.GetLineCount() );
109 void TextFileTestCase::ReadDOS()
111 CreateTestFile("foo\r\nbar\r\nbaz");
114 CPPUNIT_ASSERT( f
.Open(wxString::FromAscii(GetTestFileName())) );
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() );
123 void TextFileTestCase::ReadUnix()
125 CreateTestFile("foo\nbar\nbaz");
128 CPPUNIT_ASSERT( f
.Open(wxString::FromAscii(GetTestFileName())) );
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() );
137 void TextFileTestCase::ReadMac()
139 CreateTestFile("foo\rbar\rbaz");
142 CPPUNIT_ASSERT( f
.Open(wxString::FromAscii(GetTestFileName())) );
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() );
151 void TextFileTestCase::ReadMixed()
153 CreateTestFile("foo\rbar\r\nbaz\n");
156 CPPUNIT_ASSERT( f
.Open(wxString::FromAscii(GetTestFileName())) );
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() );
169 void TextFileTestCase::ReadUTF8()
171 CreateTestFile("\xd0\x9f\n"
172 "\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82");
175 CPPUNIT_ASSERT( f
.Open(wxString::FromAscii(GetTestFileName()), wxConvUTF8
) );
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"),
184 #endif // wxHAVE_U_ESCAPE
187 void TextFileTestCase::ReadUTF16()
190 "\x1f\x04\x0d\x00\x0a\x00"
191 "\x40\x04\x38\x04\x32\x04\x35\x04\x42\x04");
194 wxMBConvUTF16LE conv
;
195 CPPUNIT_ASSERT( f
.Open(wxString::FromAscii(GetTestFileName()), conv
) );
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) );
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"),
205 #endif // wxHAVE_U_ESCAPE
208 #endif // wxUSE_UNICODE
210 void TextFileTestCase::ReadBig()
212 static const size_t NUM_LINES
= 10000;
215 wxFFile
f(GetTestFileName(), "w");
216 for ( size_t n
= 0; n
< NUM_LINES
; n
++ )
218 fprintf(f
.fp(), "Line %lu\n", (unsigned long)n
+ 1);
223 CPPUNIT_ASSERT( f
.Open(GetTestFileName()) );
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
),
233 #endif // wxUSE_TEXTFILE