]> git.saurik.com Git - wxWidgets.git/blob - tests/streams/fileback.cpp
Clarify docs about wxSizer::SetSizeHints().
[wxWidgets.git] / tests / streams / fileback.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/streams/backfile.cpp
3 // Purpose: Test wxBackingFile
4 // Author: Mike Wetherell
5 // RCS-ID: $Id$
6 // Copyright: (c) 2006 Mike Wetherell
7 // Licence: wxWidgets licence
8 ///////////////////////////////////////////////////////////////////////////////
9
10 #include "testprec.h"
11
12 #ifdef __BORLANDC__
13 #pragma hdrstop
14 #endif
15
16 // for all others, include the necessary headers
17 #ifndef WX_PRECOMP
18 #include "wx/wx.h"
19 #endif
20
21 #include "wx/mstream.h"
22 #include "wx/fileback.h"
23 #include "bstream.h"
24
25 #if wxUSE_STREAMS
26
27 const size_t TESTSIZE = 256;
28 const size_t BUFSIZE = 100;
29
30
31 ///////////////////////////////////////////////////////////////////////////////
32 // Parent stream for testing
33
34 class TestStream : public wxMemoryInputStream
35 {
36 public:
37 TestStream(const void *buf, size_t size)
38 : wxMemoryInputStream(buf, size) { }
39
40 wxFileOffset GetLength() const { return wxInvalidOffset; }
41 };
42
43
44 ///////////////////////////////////////////////////////////////////////////////
45 // The test case
46
47 class backStream : public CppUnit::TestCase
48 {
49 public:
50 backStream();
51
52 CPPUNIT_TEST_SUITE(backStream);
53 CPPUNIT_TEST(ReadLenSeek);
54 CPPUNIT_TEST(LenSeekRead);
55 CPPUNIT_TEST(SeekReadLen);
56 CPPUNIT_TEST(ReadAll);
57 CPPUNIT_TEST(ReadTooMuch);
58 CPPUNIT_TEST(EmptyStream);
59 CPPUNIT_TEST_SUITE_END();
60
61 void ReadLenSeek();
62 void LenSeekRead();
63 void SeekReadLen();
64 void ReadAll();
65 void ReadTooMuch();
66 void EmptyStream();
67
68 private:
69 void Read(wxInputStream& in, size_t size1, size_t size2, size_t size3);
70 void Len(wxBackedInputStream& in);
71 void Seek(wxInputStream& in);
72
73 char m_testdata[TESTSIZE];
74 };
75
76 backStream::backStream()
77 {
78 for (unsigned i = 0; i < TESTSIZE; i++)
79 m_testdata[i] = i;
80 }
81
82 void backStream::ReadLenSeek()
83 {
84 wxBackingFile bf(new TestStream(m_testdata, TESTSIZE), BUFSIZE);
85 wxBackedInputStream in(bf);
86
87 Read(in, BUFSIZE, BUFSIZE / 2, 2 * BUFSIZE / 3);
88 Len(in);
89 Seek(in);
90 }
91
92 void backStream::LenSeekRead()
93 {
94 wxBackingFile bf(new TestStream(m_testdata, TESTSIZE), BUFSIZE);
95 wxBackedInputStream in(bf);
96
97 Len(in);
98 Seek(in);
99 Read(in, BUFSIZE, BUFSIZE / 2, 2 * BUFSIZE / 3);
100 }
101
102 void backStream::SeekReadLen()
103 {
104 wxBackingFile bf(new TestStream(m_testdata, TESTSIZE), BUFSIZE);
105 wxBackedInputStream in(bf);
106
107 Seek(in);
108 Read(in, BUFSIZE, BUFSIZE / 2, 2 * BUFSIZE / 3);
109 Len(in);
110 }
111
112 void backStream::ReadAll()
113 {
114 wxBackingFile bf(new TestStream(m_testdata, TESTSIZE), BUFSIZE);
115 wxBackedInputStream in(bf);
116
117 Read(in, TESTSIZE, 0, 0);
118 }
119
120 void backStream::ReadTooMuch()
121 {
122 wxBackingFile bf(new TestStream(m_testdata, TESTSIZE), BUFSIZE);
123 wxBackedInputStream in(bf);
124
125 char buf[TESTSIZE * 2];
126
127 CPPUNIT_ASSERT_EQUAL(TESTSIZE, in.Read(buf, TESTSIZE * 2).LastRead());
128 CPPUNIT_ASSERT(in.Eof());
129 CPPUNIT_ASSERT(memcmp(buf, m_testdata, TESTSIZE) == 0);
130 }
131
132 void backStream::EmptyStream()
133 {
134 wxBackingFile bf(new TestStream(m_testdata, 0), BUFSIZE);
135 wxBackedInputStream in(bf);
136
137 char buf[1];
138
139 CPPUNIT_ASSERT_EQUAL(size_t(0), in.Read(buf, 1).LastRead());
140 CPPUNIT_ASSERT(in.Eof());
141 }
142
143 void backStream::Read(wxInputStream& in,
144 size_t size1,
145 size_t size2,
146 size_t size3)
147 {
148 const size_t remainder = TESTSIZE - size1 - size2 - size3;
149 char buf[TESTSIZE];
150 char *testdata = m_testdata;
151
152 in.SeekI(0);
153
154 CPPUNIT_ASSERT_EQUAL(size1, in.Read(buf, size1).LastRead());
155 CPPUNIT_ASSERT(in.IsOk());
156 CPPUNIT_ASSERT(memcmp(buf, testdata, size1) == 0);
157 testdata += size1;
158
159 CPPUNIT_ASSERT_EQUAL(size2, in.Read(buf, size2).LastRead());
160 CPPUNIT_ASSERT(in.IsOk());
161 CPPUNIT_ASSERT(memcmp(buf, testdata, size2) == 0);
162 testdata += size2;
163
164 CPPUNIT_ASSERT_EQUAL(size3, in.Read(buf, size3).LastRead());
165 CPPUNIT_ASSERT(in.IsOk());
166 CPPUNIT_ASSERT(memcmp(buf, testdata, size3) == 0);
167 testdata += size3;
168
169 CPPUNIT_ASSERT_EQUAL(remainder, in.Read(buf, TESTSIZE).LastRead());
170 CPPUNIT_ASSERT(in.Eof());
171 CPPUNIT_ASSERT(memcmp(buf, testdata, remainder) == 0);
172
173 CPPUNIT_ASSERT_EQUAL(size_t(0), in.Read(buf, TESTSIZE).LastRead());
174 CPPUNIT_ASSERT(in.Eof());
175 }
176
177 void backStream::Len(wxBackedInputStream& in)
178 {
179 CPPUNIT_ASSERT_EQUAL(wxFileOffset(TESTSIZE), in.FindLength());
180 }
181
182 void backStream::Seek(wxInputStream& in)
183 {
184 CPPUNIT_ASSERT_EQUAL(wxFileOffset(TESTSIZE), in.SeekI(TESTSIZE));
185 in.GetC();
186 CPPUNIT_ASSERT_EQUAL(size_t(0), in.LastRead());
187 CPPUNIT_ASSERT(in.Eof());
188
189 for (wxFileOffset i = TESTSIZE - 1; i >= 0; i--) {
190 CPPUNIT_ASSERT_EQUAL(i, in.SeekI(i));
191 CPPUNIT_ASSERT_EQUAL(i, in.TellI());
192 CPPUNIT_ASSERT_EQUAL(char(i), in.GetC());
193 CPPUNIT_ASSERT_EQUAL(size_t(1), in.LastRead());
194 CPPUNIT_ASSERT(in.IsOk());
195 }
196 }
197
198 // Register the stream sub suite, by using some stream helper macro.
199 // Note: Don't forget to connect it to the base suite (See: bstream.cpp => StreamCase::suite())
200 STREAM_TEST_SUBSUITE_NAMED_REGISTRATION(backStream)
201
202 #endif // wxUSE_STREAMS