]>
git.saurik.com Git - wxWidgets.git/blob - tests/streams/fileback.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/streams/backfile.cpp
3 // Purpose: Test wxBackingFile
4 // Author: Mike Wetherell
5 // Copyright: (c) 2006 Mike Wetherell
6 // Licence: wxWindows licence
7 ///////////////////////////////////////////////////////////////////////////////
15 // for all others, include the necessary headers
20 #include "wx/mstream.h"
21 #include "wx/private/fileback.h"
24 const size_t TESTSIZE
= 256;
25 const size_t BUFSIZE
= 100;
27 ///////////////////////////////////////////////////////////////////////////////
28 // Parent stream for testing
30 class TestStream
: public wxMemoryInputStream
33 TestStream(const void *buf
, size_t size
)
34 : wxMemoryInputStream(buf
, size
) { }
36 wxFileOffset
GetLength() const { return wxInvalidOffset
; }
40 ///////////////////////////////////////////////////////////////////////////////
43 class backStream
: public CppUnit::TestCase
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();
65 void Read(wxInputStream
& in
, size_t size1
, size_t size2
, size_t size3
);
66 void Len(wxBackedInputStream
& in
);
67 void Seek(wxInputStream
& in
);
69 char m_testdata
[TESTSIZE
];
72 backStream::backStream()
74 for (unsigned i
= 0; i
< TESTSIZE
; i
++)
78 void backStream::ReadLenSeek()
80 wxBackingFile
bf(new TestStream(m_testdata
, TESTSIZE
), BUFSIZE
);
81 wxBackedInputStream
in(bf
);
83 Read(in
, BUFSIZE
, BUFSIZE
/ 2, 2 * BUFSIZE
/ 3);
88 void backStream::LenSeekRead()
90 wxBackingFile
bf(new TestStream(m_testdata
, TESTSIZE
), BUFSIZE
);
91 wxBackedInputStream
in(bf
);
95 Read(in
, BUFSIZE
, BUFSIZE
/ 2, 2 * BUFSIZE
/ 3);
98 void backStream::SeekReadLen()
100 wxBackingFile
bf(new TestStream(m_testdata
, TESTSIZE
), BUFSIZE
);
101 wxBackedInputStream
in(bf
);
104 Read(in
, BUFSIZE
, BUFSIZE
/ 2, 2 * BUFSIZE
/ 3);
108 void backStream::ReadAll()
110 wxBackingFile
bf(new TestStream(m_testdata
, TESTSIZE
), BUFSIZE
);
111 wxBackedInputStream
in(bf
);
113 Read(in
, TESTSIZE
, 0, 0);
116 void backStream::ReadTooMuch()
118 wxBackingFile
bf(new TestStream(m_testdata
, TESTSIZE
), BUFSIZE
);
119 wxBackedInputStream
in(bf
);
121 char buf
[TESTSIZE
* 2];
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);
128 void backStream::EmptyStream()
130 wxBackingFile
bf(new TestStream(m_testdata
, 0), BUFSIZE
);
131 wxBackedInputStream
in(bf
);
135 CPPUNIT_ASSERT_EQUAL(size_t(0), in
.Read(buf
, 1).LastRead());
136 CPPUNIT_ASSERT(in
.Eof());
139 void backStream::Read(wxInputStream
& in
,
144 const size_t remainder
= TESTSIZE
- size1
- size2
- size3
;
146 char *testdata
= m_testdata
;
150 CPPUNIT_ASSERT_EQUAL(size1
, in
.Read(buf
, size1
).LastRead());
151 CPPUNIT_ASSERT(in
.IsOk());
152 CPPUNIT_ASSERT(memcmp(buf
, testdata
, size1
) == 0);
155 CPPUNIT_ASSERT_EQUAL(size2
, in
.Read(buf
, size2
).LastRead());
156 CPPUNIT_ASSERT(in
.IsOk());
157 CPPUNIT_ASSERT(memcmp(buf
, testdata
, size2
) == 0);
160 CPPUNIT_ASSERT_EQUAL(size3
, in
.Read(buf
, size3
).LastRead());
161 CPPUNIT_ASSERT(in
.IsOk());
162 CPPUNIT_ASSERT(memcmp(buf
, testdata
, size3
) == 0);
165 CPPUNIT_ASSERT_EQUAL(remainder
, in
.Read(buf
, TESTSIZE
).LastRead());
166 CPPUNIT_ASSERT(in
.Eof());
167 CPPUNIT_ASSERT(memcmp(buf
, testdata
, remainder
) == 0);
169 CPPUNIT_ASSERT_EQUAL(size_t(0), in
.Read(buf
, TESTSIZE
).LastRead());
170 CPPUNIT_ASSERT(in
.Eof());
173 void backStream::Len(wxBackedInputStream
& in
)
175 CPPUNIT_ASSERT_EQUAL(wxFileOffset(TESTSIZE
), in
.FindLength());
178 void backStream::Seek(wxInputStream
& in
)
180 CPPUNIT_ASSERT_EQUAL(wxFileOffset(TESTSIZE
), in
.SeekI(TESTSIZE
));
182 CPPUNIT_ASSERT_EQUAL(size_t(0), in
.LastRead());
183 CPPUNIT_ASSERT(in
.Eof());
185 for (wxFileOffset i
= TESTSIZE
- 1; i
>= 0; i
--) {
186 CPPUNIT_ASSERT_EQUAL(i
, in
.SeekI(i
));
187 CPPUNIT_ASSERT_EQUAL(i
, in
.TellI());
188 CPPUNIT_ASSERT_EQUAL(int(i
), in
.GetC());
189 CPPUNIT_ASSERT_EQUAL(size_t(1), in
.LastRead());
190 CPPUNIT_ASSERT(in
.IsOk());
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())
196 STREAM_TEST_SUBSUITE_NAMED_REGISTRATION(backStream
)