]>
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
6 // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
25 // ----------------------------------------------------------------------------
27 // ----------------------------------------------------------------------------
29 class FileTestCase
: public CppUnit::TestCase
35 CPPUNIT_TEST_SUITE( FileTestCase
);
36 CPPUNIT_TEST( ReadAll
);
38 CPPUNIT_TEST( RoundTripUTF8
);
39 CPPUNIT_TEST( RoundTripUTF16
);
40 CPPUNIT_TEST( RoundTripUTF32
);
41 #endif // wxUSE_UNICODE
42 CPPUNIT_TEST( TempFile
);
43 CPPUNIT_TEST_SUITE_END();
47 void RoundTripUTF8() { DoRoundTripTest(wxConvUTF8
); }
48 void RoundTripUTF16() { DoRoundTripTest(wxMBConvUTF16()); }
49 void RoundTripUTF32() { DoRoundTripTest(wxMBConvUTF32()); }
50 #endif // wxUSE_UNICODE
52 void DoRoundTripTest(const wxMBConv
& conv
);
55 wxDECLARE_NO_COPY_CLASS(FileTestCase
);
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 CPPUNIT_TEST_SUITE_REGISTRATION( FileTestCase
);
63 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileTestCase
, "FileTestCase" );
65 // ----------------------------------------------------------------------------
66 // tests implementation
67 // ----------------------------------------------------------------------------
69 void FileTestCase::ReadAll()
73 const char* text
= "Ream\nde";
76 wxFile
fout(tf
.GetName(), wxFile::write
);
77 CPPUNIT_ASSERT( fout
.IsOpened() );
78 fout
.Write(text
, strlen(text
));
79 CPPUNIT_ASSERT( fout
.Close() );
83 wxFile
fin(tf
.GetName(), wxFile::read
);
84 CPPUNIT_ASSERT( fin
.IsOpened() );
87 CPPUNIT_ASSERT( fin
.ReadAll(&s
) );
88 CPPUNIT_ASSERT_EQUAL( text
, s
);
94 void FileTestCase::DoRoundTripTest(const wxMBConv
& conv
)
98 // Explicit length is needed because of the embedded NUL.
99 const wxString
data("Hello\0UTF!", 10);
102 wxFile
fout(tf
.GetName(), wxFile::write
);
103 CPPUNIT_ASSERT( fout
.IsOpened() );
105 CPPUNIT_ASSERT( fout
.Write(data
, conv
) );
109 wxFile
fin(tf
.GetName(), wxFile::read
);
110 CPPUNIT_ASSERT( fin
.IsOpened() );
112 const ssize_t len
= fin
.Length();
113 wxCharBuffer
buf(len
);
114 CPPUNIT_ASSERT_EQUAL( len
, fin
.Read(buf
.data(), len
) );
116 wxString
dataReadBack(buf
, conv
, len
);
117 CPPUNIT_ASSERT_EQUAL( data
, dataReadBack
);
121 #endif // wxUSE_UNICODE
123 void FileTestCase::TempFile()
126 CPPUNIT_ASSERT( tmpFile
.Open(wxT("test2")) && tmpFile
.Write(wxT("the answer is 42")) );
127 CPPUNIT_ASSERT( tmpFile
.Commit() );
128 CPPUNIT_ASSERT( wxRemoveFile(wxT("test2")) );