Write correct number of bytes in wxFile::Write(wxString).
[wxWidgets.git] / tests / file / filetest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/file/filetest.cpp
3 // Purpose: wxFile unit test
4 // Author: Vadim Zeitlin
5 // Created: 2009-09-12
6 // RCS-ID: $Id$
7 // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #if wxUSE_FILE
21
22 #include "wx/file.h"
23
24 #include "testfile.h"
25
26 // ----------------------------------------------------------------------------
27 // test class
28 // ----------------------------------------------------------------------------
29
30 class FileTestCase : public CppUnit::TestCase
31 {
32 public:
33 FileTestCase() { }
34
35 private:
36 CPPUNIT_TEST_SUITE( FileTestCase );
37 CPPUNIT_TEST( RoundTripUTF8 );
38 CPPUNIT_TEST( RoundTripUTF16 );
39 CPPUNIT_TEST( RoundTripUTF32 );
40 CPPUNIT_TEST_SUITE_END();
41
42 void RoundTripUTF8() { DoRoundTripTest(wxConvUTF8); }
43 void RoundTripUTF16() { DoRoundTripTest(wxMBConvUTF16()); }
44 void RoundTripUTF32() { DoRoundTripTest(wxMBConvUTF32()); }
45
46 void DoRoundTripTest(const wxMBConv& conv);
47
48 wxDECLARE_NO_COPY_CLASS(FileTestCase);
49 };
50
51 // ----------------------------------------------------------------------------
52 // CppUnit macros
53 // ----------------------------------------------------------------------------
54
55 CPPUNIT_TEST_SUITE_REGISTRATION( FileTestCase );
56 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileTestCase, "FileTestCase" );
57
58 // ----------------------------------------------------------------------------
59 // tests implementation
60 // ----------------------------------------------------------------------------
61
62 void FileTestCase::DoRoundTripTest(const wxMBConv& conv)
63 {
64 TestFile tf;
65
66 const wxString data = "Hello\0UTF";
67
68 {
69 wxFile fout(tf.GetName(), wxFile::write);
70 CPPUNIT_ASSERT( fout.IsOpened() );
71
72 CPPUNIT_ASSERT( fout.Write(data, conv) );
73 }
74
75 {
76 wxFile fin(tf.GetName(), wxFile::read);
77 CPPUNIT_ASSERT( fin.IsOpened() );
78
79 const wxFileOffset len = fin.Length();
80 wxCharBuffer buf(len);
81 CPPUNIT_ASSERT_EQUAL( len, fin.Read(buf.data(), len) );
82
83 wxWCharBuffer wbuf(conv.cMB2WC(buf));
84 #if wxUSE_UNICODE
85 CPPUNIT_ASSERT_EQUAL( data, wbuf );
86 #else // !wxUSE_UNICODE
87 CPPUNIT_ASSERT
88 (
89 memcmp(wbuf, L"Hello\0UTF", data.length()*sizeof(wchar_t)) == 0
90 );
91 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
92 }
93 }
94
95 #endif // wxUSE_FILE