]>
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 // ----------------------------------------------------------------------------
25 #include "wx/textfile.h"
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
31 class TextFileTestCase
: public CppUnit::TestCase
34 TextFileTestCase() { }
36 virtual void tearDown() { unlink(GetTestFileName()); }
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
);
46 CPPUNIT_TEST( ReadUTF8
);
47 CPPUNIT_TEST( ReadUTF16
);
48 #endif // wxUSE_UNICODE
49 CPPUNIT_TEST_SUITE_END();
59 #endif // wxUSE_UNICODE
61 // return the name of the test file we use
62 static const char *GetTestFileName() { return "textfiletest.txt"; }
64 // create the test file with the given contents
65 static void CreateTestFile(const char *contents
)
67 CreateTestFile(strlen(contents
), contents
);
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
);
75 DECLARE_NO_COPY_CLASS(TextFileTestCase
)
78 // register in the unnamed registry so that these tests are run by default
79 CPPUNIT_TEST_SUITE_REGISTRATION( TextFileTestCase
);
81 // also include in it's own registry so that these tests can be run alone
82 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TextFileTestCase
, "TextFileTestCase" );
84 void TextFileTestCase::CreateTestFile(size_t len
, const char *contents
)
86 FILE *f
= fopen(GetTestFileName(), "wb");
89 CPPUNIT_ASSERT( fwrite(contents
, 1, len
, f
) >= 0 );
90 CPPUNIT_ASSERT( fclose(f
) == 0 );
93 void TextFileTestCase::ReadEmpty()
98 CPPUNIT_ASSERT( f
.Open(wxString::FromAscii(GetTestFileName())) );
100 CPPUNIT_ASSERT_EQUAL( (size_t)0, f
.GetLineCount() );
103 void TextFileTestCase::ReadDOS()
105 CreateTestFile("foo\r\nbar\r\nbaz");
108 CPPUNIT_ASSERT( f
.Open(wxString::FromAscii(GetTestFileName())) );
110 CPPUNIT_ASSERT_EQUAL( (size_t)3, f
.GetLineCount() );
111 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Dos
, f
.GetLineType(0) );
112 CPPUNIT_ASSERT_EQUAL( wxTextFileType_None
, f
.GetLineType(2) );
113 CPPUNIT_ASSERT_EQUAL( wxString(_T("bar")), f
.GetLine(1) );
114 CPPUNIT_ASSERT_EQUAL( wxString(_T("baz")), f
.GetLastLine() );
117 void TextFileTestCase::ReadUnix()
119 CreateTestFile("foo\nbar\nbaz");
122 CPPUNIT_ASSERT( f
.Open(wxString::FromAscii(GetTestFileName())) );
124 CPPUNIT_ASSERT_EQUAL( (size_t)3, f
.GetLineCount() );
125 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Unix
, f
.GetLineType(0) );
126 CPPUNIT_ASSERT_EQUAL( wxTextFileType_None
, f
.GetLineType(2) );
127 CPPUNIT_ASSERT_EQUAL( wxString(_T("bar")), f
.GetLine(1) );
128 CPPUNIT_ASSERT_EQUAL( wxString(_T("baz")), f
.GetLastLine() );
131 void TextFileTestCase::ReadMac()
133 CreateTestFile("foo\rbar\rbaz");
136 CPPUNIT_ASSERT( f
.Open(wxString::FromAscii(GetTestFileName())) );
138 CPPUNIT_ASSERT_EQUAL( (size_t)3, f
.GetLineCount() );
139 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Mac
, f
.GetLineType(0) );
140 CPPUNIT_ASSERT_EQUAL( wxTextFileType_None
, f
.GetLineType(2) );
141 CPPUNIT_ASSERT_EQUAL( wxString(_T("bar")), f
.GetLine(1) );
142 CPPUNIT_ASSERT_EQUAL( wxString(_T("baz")), f
.GetLastLine() );
145 void TextFileTestCase::ReadMixed()
147 CreateTestFile("foo\rbar\r\nbaz\n");
150 CPPUNIT_ASSERT( f
.Open(wxString::FromAscii(GetTestFileName())) );
152 CPPUNIT_ASSERT_EQUAL( (size_t)3, f
.GetLineCount() );
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) );
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() );
163 void TextFileTestCase::ReadUTF8()
165 CreateTestFile("\xd0\x9f\n"
166 "\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82");
169 CPPUNIT_ASSERT( f
.Open(wxString::FromAscii(GetTestFileName()), wxConvUTF8
) );
171 CPPUNIT_ASSERT_EQUAL( (size_t)2, f
.GetLineCount() );
172 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Unix
, f
.GetLineType(0) );
173 CPPUNIT_ASSERT_EQUAL( wxTextFileType_None
, f
.GetLineType(1) );
174 #ifdef wxHAVE_U_ESCAPE
175 CPPUNIT_ASSERT_EQUAL( wxString(L
"\u041f"), f
.GetFirstLine() );
176 CPPUNIT_ASSERT_EQUAL( wxString(L
"\u0440\u0438\u0432\u0435\u0442"),
178 #endif // wxHAVE_U_ESCAPE
181 void TextFileTestCase::ReadUTF16()
184 "\x1f\x04\x0d\x00\x0a\x00"
185 "\x40\x04\x38\x04\x32\x04\x35\x04\x42\x04");
188 wxMBConvUTF16LE conv
;
189 CPPUNIT_ASSERT( f
.Open(wxString::FromAscii(GetTestFileName()), conv
) );
191 CPPUNIT_ASSERT_EQUAL( (size_t)2, f
.GetLineCount() );
192 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Dos
, f
.GetLineType(0) );
193 CPPUNIT_ASSERT_EQUAL( wxTextFileType_None
, f
.GetLineType(1) );
195 #ifdef wxHAVE_U_ESCAPE
196 CPPUNIT_ASSERT_EQUAL( wxString(L
"\u041f"), f
.GetFirstLine() );
197 CPPUNIT_ASSERT_EQUAL( wxString(L
"\u0440\u0438\u0432\u0435\u0442"),
199 #endif // wxHAVE_U_ESCAPE
202 #endif // wxUSE_UNICODE
204 #endif // wxUSE_TEXTFILE