]> git.saurik.com Git - wxWidgets.git/blame - tests/file/filetest.cpp
Convert wxFSW_EVENT_{WARNING,ERROR} to string correctly.
[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
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
30class FileTestCase : public CppUnit::TestCase
31{
32public:
33 FileTestCase() { }
34
35private:
36 CPPUNIT_TEST_SUITE( FileTestCase );
06a086e8 37 CPPUNIT_TEST( ReadAll );
a9f3fb07 38#if wxUSE_UNICODE
227989f3
VZ
39 CPPUNIT_TEST( RoundTripUTF8 );
40 CPPUNIT_TEST( RoundTripUTF16 );
41 CPPUNIT_TEST( RoundTripUTF32 );
a9f3fb07 42#endif // wxUSE_UNICODE
69fc8587 43 CPPUNIT_TEST( TempFile );
227989f3
VZ
44 CPPUNIT_TEST_SUITE_END();
45
06a086e8 46 void ReadAll();
a9f3fb07 47#if wxUSE_UNICODE
227989f3
VZ
48 void RoundTripUTF8() { DoRoundTripTest(wxConvUTF8); }
49 void RoundTripUTF16() { DoRoundTripTest(wxMBConvUTF16()); }
50 void RoundTripUTF32() { DoRoundTripTest(wxMBConvUTF32()); }
a9f3fb07 51#endif // wxUSE_UNICODE
227989f3
VZ
52
53 void DoRoundTripTest(const wxMBConv& conv);
69fc8587 54 void TempFile();
227989f3
VZ
55
56 wxDECLARE_NO_COPY_CLASS(FileTestCase);
57};
58
59// ----------------------------------------------------------------------------
60// CppUnit macros
61// ----------------------------------------------------------------------------
62
63CPPUNIT_TEST_SUITE_REGISTRATION( FileTestCase );
64CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileTestCase, "FileTestCase" );
65
66// ----------------------------------------------------------------------------
67// tests implementation
68// ----------------------------------------------------------------------------
69
06a086e8
VZ
70void FileTestCase::ReadAll()
71{
72 TestFile tf;
73
74 const char* text = "Ream\nde";
75
76 {
77 wxFile fout(tf.GetName(), wxFile::write);
78 CPPUNIT_ASSERT( fout.IsOpened() );
79 fout.Write(text, strlen(text));
80 CPPUNIT_ASSERT( fout.Close() );
81 }
82
83 {
84 wxFile fin(tf.GetName(), wxFile::read);
85 CPPUNIT_ASSERT( fin.IsOpened() );
86
87 wxString s;
88 CPPUNIT_ASSERT( fin.ReadAll(&s) );
89 CPPUNIT_ASSERT_EQUAL( text, s );
90 }
91}
92
a9f3fb07
VZ
93#if wxUSE_UNICODE
94
227989f3
VZ
95void FileTestCase::DoRoundTripTest(const wxMBConv& conv)
96{
97 TestFile tf;
98
24b4db9b
VZ
99 // Explicit length is needed because of the embedded NUL.
100 const wxString data("Hello\0UTF!", 10);
227989f3
VZ
101
102 {
103 wxFile fout(tf.GetName(), wxFile::write);
104 CPPUNIT_ASSERT( fout.IsOpened() );
105
106 CPPUNIT_ASSERT( fout.Write(data, conv) );
107 }
108
109 {
110 wxFile fin(tf.GetName(), wxFile::read);
111 CPPUNIT_ASSERT( fin.IsOpened() );
112
bf24fcdd 113 const ssize_t len = fin.Length();
227989f3
VZ
114 wxCharBuffer buf(len);
115 CPPUNIT_ASSERT_EQUAL( len, fin.Read(buf.data(), len) );
116
24b4db9b
VZ
117 wxString dataReadBack(buf, conv, len);
118 CPPUNIT_ASSERT_EQUAL( data, dataReadBack );
227989f3
VZ
119 }
120}
121
a9f3fb07
VZ
122#endif // wxUSE_UNICODE
123
69fc8587
FM
124void FileTestCase::TempFile()
125{
126 wxTempFile tmpFile;
127 CPPUNIT_ASSERT( tmpFile.Open(wxT("test2")) && tmpFile.Write(wxT("the answer is 42")) );
128 CPPUNIT_ASSERT( tmpFile.Commit() );
129 CPPUNIT_ASSERT( wxRemoveFile(wxT("test2")) );
130}
131
227989f3 132#endif // wxUSE_FILE