]>
git.saurik.com Git - wxWidgets.git/blob - tests/file/filetest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/file/filetest.cpp
3 // Purpose: wxFile unit test
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
26 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
30 class FileTestCase
: public CppUnit::TestCase
36 CPPUNIT_TEST_SUITE( FileTestCase
);
37 CPPUNIT_TEST( ReadAll
);
39 CPPUNIT_TEST( RoundTripUTF8
);
40 CPPUNIT_TEST( RoundTripUTF16
);
41 CPPUNIT_TEST( RoundTripUTF32
);
42 #endif // wxUSE_UNICODE
43 CPPUNIT_TEST( TempFile
);
44 CPPUNIT_TEST_SUITE_END();
48 void RoundTripUTF8() { DoRoundTripTest(wxConvUTF8
); }
49 void RoundTripUTF16() { DoRoundTripTest(wxMBConvUTF16()); }
50 void RoundTripUTF32() { DoRoundTripTest(wxMBConvUTF32()); }
51 #endif // wxUSE_UNICODE
53 void DoRoundTripTest(const wxMBConv
& conv
);
56 wxDECLARE_NO_COPY_CLASS(FileTestCase
);
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 CPPUNIT_TEST_SUITE_REGISTRATION( FileTestCase
);
64 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileTestCase
, "FileTestCase" );
66 // ----------------------------------------------------------------------------
67 // tests implementation
68 // ----------------------------------------------------------------------------
70 void FileTestCase::ReadAll()
74 const char* text
= "Ream\nde";
77 wxFile
fout(tf
.GetName(), wxFile::write
);
78 CPPUNIT_ASSERT( fout
.IsOpened() );
79 fout
.Write(text
, strlen(text
));
80 CPPUNIT_ASSERT( fout
.Close() );
84 wxFile
fin(tf
.GetName(), wxFile::read
);
85 CPPUNIT_ASSERT( fin
.IsOpened() );
88 CPPUNIT_ASSERT( fin
.ReadAll(&s
) );
89 CPPUNIT_ASSERT_EQUAL( text
, s
);
95 void FileTestCase::DoRoundTripTest(const wxMBConv
& conv
)
99 // Explicit length is needed because of the embedded NUL.
100 const wxString
data("Hello\0UTF!", 10);
103 wxFile
fout(tf
.GetName(), wxFile::write
);
104 CPPUNIT_ASSERT( fout
.IsOpened() );
106 CPPUNIT_ASSERT( fout
.Write(data
, conv
) );
110 wxFile
fin(tf
.GetName(), wxFile::read
);
111 CPPUNIT_ASSERT( fin
.IsOpened() );
113 const ssize_t len
= fin
.Length();
114 wxCharBuffer
buf(len
);
115 CPPUNIT_ASSERT_EQUAL( len
, fin
.Read(buf
.data(), len
) );
117 wxString
dataReadBack(buf
, conv
, len
);
118 CPPUNIT_ASSERT_EQUAL( data
, dataReadBack
);
122 #endif // wxUSE_UNICODE
124 void FileTestCase::TempFile()
127 CPPUNIT_ASSERT( tmpFile
.Open(wxT("test2")) && tmpFile
.Write(wxT("the answer is 42")) );
128 CPPUNIT_ASSERT( tmpFile
.Commit() );
129 CPPUNIT_ASSERT( wxRemoveFile(wxT("test2")) );