]> git.saurik.com Git - wxWidgets.git/blame - tests/file/filetest.cpp
wxMessageBox off the main thread lost result code.
[wxWidgets.git] / tests / file / filetest.cpp
CommitLineData
227989f3
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: tests/file/filetest.cpp
3// Purpose: wxFile unit test
4// Author: Vadim Zeitlin
5// Created: 2009-09-12
227989f3
VZ
6// Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
7///////////////////////////////////////////////////////////////////////////////
8
9// ----------------------------------------------------------------------------
10// headers
11// ----------------------------------------------------------------------------
12
13#include "testprec.h"
14
15#ifdef __BORLANDC__
16 #pragma hdrstop
17#endif
18
19#if wxUSE_FILE
20
21#include "wx/file.h"
22
23#include "testfile.h"
24
25// ----------------------------------------------------------------------------
26// test class
27// ----------------------------------------------------------------------------
28
29class FileTestCase : public CppUnit::TestCase
30{
31public:
32 FileTestCase() { }
33
34private:
35 CPPUNIT_TEST_SUITE( FileTestCase );
06a086e8 36 CPPUNIT_TEST( ReadAll );
a9f3fb07 37#if wxUSE_UNICODE
227989f3
VZ
38 CPPUNIT_TEST( RoundTripUTF8 );
39 CPPUNIT_TEST( RoundTripUTF16 );
40 CPPUNIT_TEST( RoundTripUTF32 );
a9f3fb07 41#endif // wxUSE_UNICODE
69fc8587 42 CPPUNIT_TEST( TempFile );
227989f3
VZ
43 CPPUNIT_TEST_SUITE_END();
44
06a086e8 45 void ReadAll();
a9f3fb07 46#if wxUSE_UNICODE
227989f3
VZ
47 void RoundTripUTF8() { DoRoundTripTest(wxConvUTF8); }
48 void RoundTripUTF16() { DoRoundTripTest(wxMBConvUTF16()); }
49 void RoundTripUTF32() { DoRoundTripTest(wxMBConvUTF32()); }
a9f3fb07 50#endif // wxUSE_UNICODE
227989f3
VZ
51
52 void DoRoundTripTest(const wxMBConv& conv);
69fc8587 53 void TempFile();
227989f3
VZ
54
55 wxDECLARE_NO_COPY_CLASS(FileTestCase);
56};
57
58// ----------------------------------------------------------------------------
59// CppUnit macros
60// ----------------------------------------------------------------------------
61
62CPPUNIT_TEST_SUITE_REGISTRATION( FileTestCase );
63CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileTestCase, "FileTestCase" );
64
65// ----------------------------------------------------------------------------
66// tests implementation
67// ----------------------------------------------------------------------------
68
06a086e8
VZ
69void FileTestCase::ReadAll()
70{
71 TestFile tf;
72
73 const char* text = "Ream\nde";
74
75 {
76 wxFile fout(tf.GetName(), wxFile::write);
77 CPPUNIT_ASSERT( fout.IsOpened() );
78 fout.Write(text, strlen(text));
79 CPPUNIT_ASSERT( fout.Close() );
80 }
81
82 {
83 wxFile fin(tf.GetName(), wxFile::read);
84 CPPUNIT_ASSERT( fin.IsOpened() );
85
86 wxString s;
87 CPPUNIT_ASSERT( fin.ReadAll(&s) );
88 CPPUNIT_ASSERT_EQUAL( text, s );
89 }
90}
91
a9f3fb07
VZ
92#if wxUSE_UNICODE
93
227989f3
VZ
94void FileTestCase::DoRoundTripTest(const wxMBConv& conv)
95{
96 TestFile tf;
97
24b4db9b
VZ
98 // Explicit length is needed because of the embedded NUL.
99 const wxString data("Hello\0UTF!", 10);
227989f3
VZ
100
101 {
102 wxFile fout(tf.GetName(), wxFile::write);
103 CPPUNIT_ASSERT( fout.IsOpened() );
104
105 CPPUNIT_ASSERT( fout.Write(data, conv) );
106 }
107
108 {
109 wxFile fin(tf.GetName(), wxFile::read);
110 CPPUNIT_ASSERT( fin.IsOpened() );
111
bf24fcdd 112 const ssize_t len = fin.Length();
227989f3
VZ
113 wxCharBuffer buf(len);
114 CPPUNIT_ASSERT_EQUAL( len, fin.Read(buf.data(), len) );
115
24b4db9b
VZ
116 wxString dataReadBack(buf, conv, len);
117 CPPUNIT_ASSERT_EQUAL( data, dataReadBack );
227989f3
VZ
118 }
119}
120
a9f3fb07
VZ
121#endif // wxUSE_UNICODE
122
69fc8587
FM
123void FileTestCase::TempFile()
124{
125 wxTempFile tmpFile;
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")) );
129}
130
227989f3 131#endif // wxUSE_FILE