]>
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( RoundTripUTF8
);
38 CPPUNIT_TEST( RoundTripUTF16
);
39 CPPUNIT_TEST( RoundTripUTF32
);
40 CPPUNIT_TEST( TempFile
);
41 CPPUNIT_TEST_SUITE_END();
43 void RoundTripUTF8() { DoRoundTripTest(wxConvUTF8
); }
44 void RoundTripUTF16() { DoRoundTripTest(wxMBConvUTF16()); }
45 void RoundTripUTF32() { DoRoundTripTest(wxMBConvUTF32()); }
47 void DoRoundTripTest(const wxMBConv
& conv
);
50 wxDECLARE_NO_COPY_CLASS(FileTestCase
);
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 CPPUNIT_TEST_SUITE_REGISTRATION( FileTestCase
);
58 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileTestCase
, "FileTestCase" );
60 // ----------------------------------------------------------------------------
61 // tests implementation
62 // ----------------------------------------------------------------------------
64 void FileTestCase::DoRoundTripTest(const wxMBConv
& conv
)
68 const wxString data
= "Hello\0UTF";
71 wxFile
fout(tf
.GetName(), wxFile::write
);
72 CPPUNIT_ASSERT( fout
.IsOpened() );
74 CPPUNIT_ASSERT( fout
.Write(data
, conv
) );
78 wxFile
fin(tf
.GetName(), wxFile::read
);
79 CPPUNIT_ASSERT( fin
.IsOpened() );
81 const ssize_t len
= fin
.Length();
82 wxCharBuffer
buf(len
);
83 CPPUNIT_ASSERT_EQUAL( len
, fin
.Read(buf
.data(), len
) );
85 wxWCharBuffer
wbuf(conv
.cMB2WC(buf
));
87 CPPUNIT_ASSERT_EQUAL( data
, wbuf
);
88 #else // !wxUSE_UNICODE
91 memcmp(wbuf
, L
"Hello\0UTF", data
.length()*sizeof(wchar_t)) == 0
93 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
97 void FileTestCase::TempFile()
100 CPPUNIT_ASSERT( tmpFile
.Open(wxT("test2")) && tmpFile
.Write(wxT("the answer is 42")) );
101 CPPUNIT_ASSERT( tmpFile
.Commit() );
102 CPPUNIT_ASSERT( wxRemoveFile(wxT("test2")) );