]>
git.saurik.com Git - wxWidgets.git/blob - tests/streams/fileback.cpp
484c95a8d57201836bca3a79972320136458bb2f
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/streams/backfile.cpp
3 // Purpose: Test wxBackingFile
4 // Author: Mike Wetherell
6 // Copyright: (c) 2006 Mike Wetherell
7 // Licence: wxWidgets licence
8 ///////////////////////////////////////////////////////////////////////////////
16 // for all others, include the necessary headers
21 #include "wx/mstream.h"
22 #include "wx/private/fileback.h"
25 const size_t TESTSIZE
= 256;
26 const size_t BUFSIZE
= 100;
28 ///////////////////////////////////////////////////////////////////////////////
29 // Parent stream for testing
31 class TestStream
: public wxMemoryInputStream
34 TestStream(const void *buf
, size_t size
)
35 : wxMemoryInputStream(buf
, size
) { }
37 wxFileOffset
GetLength() const { return wxInvalidOffset
; }
41 ///////////////////////////////////////////////////////////////////////////////
44 class backStream
: public CppUnit::TestCase
49 CPPUNIT_TEST_SUITE(backStream
);
50 CPPUNIT_TEST(ReadLenSeek
);
51 CPPUNIT_TEST(LenSeekRead
);
52 CPPUNIT_TEST(SeekReadLen
);
53 CPPUNIT_TEST(ReadAll
);
54 CPPUNIT_TEST(ReadTooMuch
);
55 CPPUNIT_TEST(EmptyStream
);
56 CPPUNIT_TEST_SUITE_END();
66 void Read(wxInputStream
& in
, size_t size1
, size_t size2
, size_t size3
);
67 void Len(wxBackedInputStream
& in
);
68 void Seek(wxInputStream
& in
);
70 char m_testdata
[TESTSIZE
];
73 backStream::backStream()
75 for (unsigned i
= 0; i
< TESTSIZE
; i
++)
79 void backStream::ReadLenSeek()
81 wxBackingFile
bf(new TestStream(m_testdata
, TESTSIZE
), BUFSIZE
);
82 wxBackedInputStream
in(bf
);
84 Read(in
, BUFSIZE
, BUFSIZE
/ 2, 2 * BUFSIZE
/ 3);
89 void backStream::LenSeekRead()
91 wxBackingFile
bf(new TestStream(m_testdata
, TESTSIZE
), BUFSIZE
);
92 wxBackedInputStream
in(bf
);
96 Read(in
, BUFSIZE
, BUFSIZE
/ 2, 2 * BUFSIZE
/ 3);
99 void backStream::SeekReadLen()
101 wxBackingFile
bf(new TestStream(m_testdata
, TESTSIZE
), BUFSIZE
);
102 wxBackedInputStream
in(bf
);
105 Read(in
, BUFSIZE
, BUFSIZE
/ 2, 2 * BUFSIZE
/ 3);
109 void backStream::ReadAll()
111 wxBackingFile
bf(new TestStream(m_testdata
, TESTSIZE
), BUFSIZE
);
112 wxBackedInputStream
in(bf
);
114 Read(in
, TESTSIZE
, 0, 0);
117 void backStream::ReadTooMuch()
119 wxBackingFile
bf(new TestStream(m_testdata
, TESTSIZE
), BUFSIZE
);
120 wxBackedInputStream
in(bf
);
122 char buf
[TESTSIZE
* 2];
124 CPPUNIT_ASSERT_EQUAL(TESTSIZE
, in
.Read(buf
, TESTSIZE
* 2).LastRead());
125 CPPUNIT_ASSERT(in
.Eof());
126 CPPUNIT_ASSERT(memcmp(buf
, m_testdata
, TESTSIZE
) == 0);
129 void backStream::EmptyStream()
131 wxBackingFile
bf(new TestStream(m_testdata
, 0), BUFSIZE
);
132 wxBackedInputStream
in(bf
);
136 CPPUNIT_ASSERT_EQUAL(size_t(0), in
.Read(buf
, 1).LastRead());
137 CPPUNIT_ASSERT(in
.Eof());
140 void backStream::Read(wxInputStream
& in
,
145 const size_t remainder
= TESTSIZE
- size1
- size2
- size3
;
147 char *testdata
= m_testdata
;
151 CPPUNIT_ASSERT_EQUAL(size1
, in
.Read(buf
, size1
).LastRead());
152 CPPUNIT_ASSERT(in
.IsOk());
153 CPPUNIT_ASSERT(memcmp(buf
, testdata
, size1
) == 0);
156 CPPUNIT_ASSERT_EQUAL(size2
, in
.Read(buf
, size2
).LastRead());
157 CPPUNIT_ASSERT(in
.IsOk());
158 CPPUNIT_ASSERT(memcmp(buf
, testdata
, size2
) == 0);
161 CPPUNIT_ASSERT_EQUAL(size3
, in
.Read(buf
, size3
).LastRead());
162 CPPUNIT_ASSERT(in
.IsOk());
163 CPPUNIT_ASSERT(memcmp(buf
, testdata
, size3
) == 0);
166 CPPUNIT_ASSERT_EQUAL(remainder
, in
.Read(buf
, TESTSIZE
).LastRead());
167 CPPUNIT_ASSERT(in
.Eof());
168 CPPUNIT_ASSERT(memcmp(buf
, testdata
, remainder
) == 0);
170 CPPUNIT_ASSERT_EQUAL(size_t(0), in
.Read(buf
, TESTSIZE
).LastRead());
171 CPPUNIT_ASSERT(in
.Eof());
174 void backStream::Len(wxBackedInputStream
& in
)
176 CPPUNIT_ASSERT_EQUAL(wxFileOffset(TESTSIZE
), in
.FindLength());
179 void backStream::Seek(wxInputStream
& in
)
181 CPPUNIT_ASSERT_EQUAL(wxFileOffset(TESTSIZE
), in
.SeekI(TESTSIZE
));
183 CPPUNIT_ASSERT_EQUAL(size_t(0), in
.LastRead());
184 CPPUNIT_ASSERT(in
.Eof());
186 for (wxFileOffset i
= TESTSIZE
- 1; i
>= 0; i
--) {
187 CPPUNIT_ASSERT_EQUAL(i
, in
.SeekI(i
));
188 CPPUNIT_ASSERT_EQUAL(i
, in
.TellI());
189 CPPUNIT_ASSERT_EQUAL(int(i
), in
.GetC());
190 CPPUNIT_ASSERT_EQUAL(size_t(1), in
.LastRead());
191 CPPUNIT_ASSERT(in
.IsOk());
195 // Register the stream sub suite, by using some stream helper macro.
196 // Note: Don't forget to connect it to the base suite (See: bstream.cpp => StreamCase::suite())
197 STREAM_TEST_SUBSUITE_NAMED_REGISTRATION(backStream
)