]>
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"
28 // ----------------------------------------------------------------------------
30 // ----------------------------------------------------------------------------
32 class TextFileTestCase
: public CppUnit::TestCase
35 TextFileTestCase() { }
37 virtual void tearDown() { unlink(GetTestFileName()); }
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
);
47 CPPUNIT_TEST( ReadUTF8
);
48 CPPUNIT_TEST( ReadUTF16
);
49 #endif // wxUSE_UNICODE
50 CPPUNIT_TEST( ReadBig
);
51 CPPUNIT_TEST_SUITE_END();
61 #endif // wxUSE_UNICODE
64 // return the name of the test file we use
65 static const char *GetTestFileName() { return "textfiletest.txt"; }
67 // create the test file with the given contents
68 static void CreateTestFile(const char *contents
)
70 CreateTestFile(strlen(contents
), contents
);
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
);
78 DECLARE_NO_COPY_CLASS(TextFileTestCase
)
81 // register in the unnamed registry so that these tests are run by default
82 CPPUNIT_TEST_SUITE_REGISTRATION( TextFileTestCase
);
84 // also include in it's own registry so that these tests can be run alone
85 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TextFileTestCase
, "TextFileTestCase" );
87 void TextFileTestCase::CreateTestFile(size_t len
, const char *contents
)
89 FILE *f
= fopen(GetTestFileName(), "wb");
92 CPPUNIT_ASSERT( fwrite(contents
, 1, len
, f
) >= 0 );
93 CPPUNIT_ASSERT( fclose(f
) == 0 );
96 void TextFileTestCase::ReadEmpty()
101 CPPUNIT_ASSERT( f
.Open(wxString::FromAscii(GetTestFileName())) );
103 CPPUNIT_ASSERT_EQUAL( (size_t)0, f
.GetLineCount() );
106 void TextFileTestCase::ReadDOS()
108 CreateTestFile("foo\r\nbar\r\nbaz");
111 CPPUNIT_ASSERT( f
.Open(wxString::FromAscii(GetTestFileName())) );
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() );
120 void TextFileTestCase::ReadUnix()
122 CreateTestFile("foo\nbar\nbaz");
125 CPPUNIT_ASSERT( f
.Open(wxString::FromAscii(GetTestFileName())) );
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() );
134 void TextFileTestCase::ReadMac()
136 CreateTestFile("foo\rbar\rbaz");
139 CPPUNIT_ASSERT( f
.Open(wxString::FromAscii(GetTestFileName())) );
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() );
148 void TextFileTestCase::ReadMixed()
150 CreateTestFile("foo\rbar\r\nbaz\n");
153 CPPUNIT_ASSERT( f
.Open(wxString::FromAscii(GetTestFileName())) );
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() );
166 void TextFileTestCase::ReadUTF8()
168 CreateTestFile("\xd0\x9f\n"
169 "\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82");
172 CPPUNIT_ASSERT( f
.Open(wxString::FromAscii(GetTestFileName()), wxConvUTF8
) );
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"),
181 #endif // wxHAVE_U_ESCAPE
184 void TextFileTestCase::ReadUTF16()
187 "\x1f\x04\x0d\x00\x0a\x00"
188 "\x40\x04\x38\x04\x32\x04\x35\x04\x42\x04");
191 wxMBConvUTF16LE conv
;
192 CPPUNIT_ASSERT( f
.Open(wxString::FromAscii(GetTestFileName()), conv
) );
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) );
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"),
202 #endif // wxHAVE_U_ESCAPE
205 void TextFileTestCase::ReadBig()
207 static const size_t NUM_LINES
= 10000;
210 wxFFile
f(GetTestFileName(), "w");
211 for ( size_t n
= 0; n
< NUM_LINES
; n
++ )
213 fprintf(f
.fp(), "Line %lu\n", (unsigned long)n
+ 1);
218 CPPUNIT_ASSERT( f
.Open(GetTestFileName()) );
220 CPPUNIT_ASSERT_EQUAL( NUM_LINES
, f
.GetLineCount() );
221 CPPUNIT_ASSERT_EQUAL( wxString("Line 1"), f
[0] );
222 CPPUNIT_ASSERT_EQUAL( wxString("Line 999"), f
[998] );
223 CPPUNIT_ASSERT_EQUAL( wxString("Line 1000"), f
[999] );
224 CPPUNIT_ASSERT_EQUAL( wxString::Format("Line %lu", (unsigned long)NUM_LINES
),
228 #endif // wxUSE_UNICODE
230 #endif // wxUSE_TEXTFILE