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