1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/file/filetest.cpp
3 // Purpose: wxFile unit test
4 // Author: Vadim Zeitlin
5 // Created: 2009-09-12
6 // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
7 ///////////////////////////////////////////////////////////////////////////////
8
9 // ----------------------------------------------------------------------------
10 // headers
11 // ----------------------------------------------------------------------------
12
13 #include "testprec.h"
14
15
16 #if wxUSE_FILE
17
18 #include "wx/file.h"
19
20 #include "testfile.h"
21
22 // ----------------------------------------------------------------------------
23 // test class
24 // ----------------------------------------------------------------------------
25
26 class FileTestCase : public CppUnit::TestCase
27 {
28 public:
FileTestCase()29 FileTestCase() { }
30
31 private:
32 CPPUNIT_TEST_SUITE( FileTestCase );
33 CPPUNIT_TEST( ReadAll );
34 #if wxUSE_UNICODE
35 CPPUNIT_TEST( RoundTripUTF8 );
36 CPPUNIT_TEST( RoundTripUTF16 );
37 CPPUNIT_TEST( RoundTripUTF32 );
38 #endif // wxUSE_UNICODE
39 CPPUNIT_TEST( TempFile );
40 CPPUNIT_TEST_SUITE_END();
41
42 void ReadAll();
43 #if wxUSE_UNICODE
RoundTripUTF8()44 void RoundTripUTF8() { DoRoundTripTest(wxConvUTF8); }
RoundTripUTF16()45 void RoundTripUTF16() { DoRoundTripTest(wxMBConvUTF16()); }
RoundTripUTF32()46 void RoundTripUTF32() { DoRoundTripTest(wxMBConvUTF32()); }
47 #endif // wxUSE_UNICODE
48
49 void DoRoundTripTest(const wxMBConv& conv);
50 void TempFile();
51
52 wxDECLARE_NO_COPY_CLASS(FileTestCase);
53 };
54
55 // ----------------------------------------------------------------------------
56 // CppUnit macros
57 // ----------------------------------------------------------------------------
58
59 CPPUNIT_TEST_SUITE_REGISTRATION( FileTestCase );
60 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileTestCase, "FileTestCase" );
61
62 // ----------------------------------------------------------------------------
63 // tests implementation
64 // ----------------------------------------------------------------------------
65
ReadAll()66 void FileTestCase::ReadAll()
67 {
68 TestFile tf;
69
70 const char* text = "Ream\nde";
71
72 {
73 wxFile fout(tf.GetName(), wxFile::write);
74 CPPUNIT_ASSERT( fout.IsOpened() );
75 fout.Write(text, strlen(text));
76 CPPUNIT_ASSERT( fout.Close() );
77 }
78
79 {
80 wxFile fin(tf.GetName(), wxFile::read);
81 CPPUNIT_ASSERT( fin.IsOpened() );
82
83 wxString s;
84 CPPUNIT_ASSERT( fin.ReadAll(&s) );
85 CPPUNIT_ASSERT_EQUAL( text, s );
86 }
87 }
88
89 #if wxUSE_UNICODE
90
DoRoundTripTest(const wxMBConv & conv)91 void FileTestCase::DoRoundTripTest(const wxMBConv& conv)
92 {
93 TestFile tf;
94
95 // Explicit length is needed because of the embedded NUL.
96 const wxString data("Hello\0UTF!", 10);
97
98 {
99 wxFile fout(tf.GetName(), wxFile::write);
100 CPPUNIT_ASSERT( fout.IsOpened() );
101
102 CPPUNIT_ASSERT( fout.Write(data, conv) );
103 }
104
105 {
106 wxFile fin(tf.GetName(), wxFile::read);
107 CPPUNIT_ASSERT( fin.IsOpened() );
108
109 const ssize_t len = fin.Length();
110 wxCharBuffer buf(len);
111 CPPUNIT_ASSERT_EQUAL( len, fin.Read(buf.data(), len) );
112
113 wxString dataReadBack(buf, conv, len);
114 CPPUNIT_ASSERT_EQUAL( data, dataReadBack );
115 }
116
117 {
118 wxFile fin(tf.GetName(), wxFile::read);
119 CPPUNIT_ASSERT( fin.IsOpened() );
120
121 wxString dataReadBack;
122 CPPUNIT_ASSERT( fin.ReadAll(&dataReadBack, conv) );
123
124 CPPUNIT_ASSERT_EQUAL( data, dataReadBack );
125 }
126 }
127
128 #endif // wxUSE_UNICODE
129
TempFile()130 void FileTestCase::TempFile()
131 {
132 wxTempFile tmpFile;
133 CPPUNIT_ASSERT( tmpFile.Open(wxT("test2")) );
134 CPPUNIT_ASSERT( tmpFile.Write(wxT("the answer is 42")) );
135 CPPUNIT_ASSERT( tmpFile.Commit() );
136 CPPUNIT_ASSERT( wxRemoveFile(wxT("test2")) );
137 }
138
139 #ifdef __LINUX__
140
141 // Check that GetSize() works correctly for special files.
142 TEST_CASE("wxFile::Special", "[file][linux][special-file]")
143 {
144 // LXC containers don't (always) populate /proc and /sys, so skip these
145 // tests there.
146 if ( IsRunningInLXC() )
147 return;
148
149 // We can't test /proc/kcore here, unlike in the similar
150 // wxFileName::GetSize() test, as wxFile must be able to open it (at least
151 // for reading) and usually we don't have the permissions to do it.
152
153 // This file is not seekable and has 0 size, but can still be read.
154 wxFile fileProc("/proc/cpuinfo");
155 CHECK( fileProc.IsOpened() );
156
157 wxString s;
158 CHECK( fileProc.ReadAll(&s) );
159 CHECK( !s.empty() );
160
161 // All files in /sys have the size of one kernel page, even if they don't
162 // have that much data in them.
163 const long pageSize = sysconf(_SC_PAGESIZE);
164
165 wxFile fileSys("/sys/power/state");
166 CHECK( fileSys.Length() == pageSize );
167 CHECK( fileSys.IsOpened() );
168 CHECK( fileSys.ReadAll(&s) );
169 CHECK( !s.empty() );
170 CHECK( s.length() < pageSize );
171 }
172
173 #endif // __LINUX__
174
175 #endif // wxUSE_FILE
176