]>
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
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/fileback.h"
27 const size_t TESTSIZE
= 256;
28 const size_t BUFSIZE
= 100;
31 ///////////////////////////////////////////////////////////////////////////////
32 // Parent stream for testing
34 class TestStream
: public wxMemoryInputStream
37 TestStream(const void *buf
, size_t size
)
38 : wxMemoryInputStream(buf
, size
) { }
40 wxFileOffset
GetLength() const { return wxInvalidOffset
; }
44 ///////////////////////////////////////////////////////////////////////////////
47 class backStream
: public CppUnit::TestCase
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();
69 void Read(wxInputStream
& in
, size_t size1
, size_t size2
, size_t size3
);
70 void Len(wxBackedInputStream
& in
);
71 void Seek(wxInputStream
& in
);
73 char m_testdata
[TESTSIZE
];
76 backStream::backStream()
78 for (unsigned i
= 0; i
< TESTSIZE
; i
++)
82 void backStream::ReadLenSeek()
84 wxBackingFile
bf(new TestStream(m_testdata
, TESTSIZE
), BUFSIZE
);
85 wxBackedInputStream
in(bf
);
87 Read(in
, BUFSIZE
, BUFSIZE
/ 2, 2 * BUFSIZE
/ 3);
92 void backStream::LenSeekRead()
94 wxBackingFile
bf(new TestStream(m_testdata
, TESTSIZE
), BUFSIZE
);
95 wxBackedInputStream
in(bf
);
99 Read(in
, BUFSIZE
, BUFSIZE
/ 2, 2 * BUFSIZE
/ 3);
102 void backStream::SeekReadLen()
104 wxBackingFile
bf(new TestStream(m_testdata
, TESTSIZE
), BUFSIZE
);
105 wxBackedInputStream
in(bf
);
108 Read(in
, BUFSIZE
, BUFSIZE
/ 2, 2 * BUFSIZE
/ 3);
112 void backStream::ReadAll()
114 wxBackingFile
bf(new TestStream(m_testdata
, TESTSIZE
), BUFSIZE
);
115 wxBackedInputStream
in(bf
);
117 Read(in
, TESTSIZE
, 0, 0);
120 void backStream::ReadTooMuch()
122 wxBackingFile
bf(new TestStream(m_testdata
, TESTSIZE
), BUFSIZE
);
123 wxBackedInputStream
in(bf
);
125 char buf
[TESTSIZE
* 2];
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);
132 void backStream::EmptyStream()
134 wxBackingFile
bf(new TestStream(m_testdata
, 0), BUFSIZE
);
135 wxBackedInputStream
in(bf
);
139 CPPUNIT_ASSERT_EQUAL(size_t(0), in
.Read(buf
, 1).LastRead());
140 CPPUNIT_ASSERT(in
.Eof());
143 void backStream::Read(wxInputStream
& in
,
148 const size_t remainder
= TESTSIZE
- size1
- size2
- size3
;
150 char *testdata
= m_testdata
;
154 CPPUNIT_ASSERT_EQUAL(size1
, in
.Read(buf
, size1
).LastRead());
155 CPPUNIT_ASSERT(in
.IsOk());
156 CPPUNIT_ASSERT(memcmp(buf
, testdata
, size1
) == 0);
159 CPPUNIT_ASSERT_EQUAL(size2
, in
.Read(buf
, size2
).LastRead());
160 CPPUNIT_ASSERT(in
.IsOk());
161 CPPUNIT_ASSERT(memcmp(buf
, testdata
, size2
) == 0);
164 CPPUNIT_ASSERT_EQUAL(size3
, in
.Read(buf
, size3
).LastRead());
165 CPPUNIT_ASSERT(in
.IsOk());
166 CPPUNIT_ASSERT(memcmp(buf
, testdata
, size3
) == 0);
169 CPPUNIT_ASSERT_EQUAL(remainder
, in
.Read(buf
, TESTSIZE
).LastRead());
170 CPPUNIT_ASSERT(in
.Eof());
171 CPPUNIT_ASSERT(memcmp(buf
, testdata
, remainder
) == 0);
173 CPPUNIT_ASSERT_EQUAL(size_t(0), in
.Read(buf
, TESTSIZE
).LastRead());
174 CPPUNIT_ASSERT(in
.Eof());
177 void backStream::Len(wxBackedInputStream
& in
)
179 CPPUNIT_ASSERT_EQUAL(wxFileOffset(TESTSIZE
), in
.FindLength());
182 void backStream::Seek(wxInputStream
& in
)
184 CPPUNIT_ASSERT_EQUAL(wxFileOffset(TESTSIZE
), in
.SeekI(TESTSIZE
));
186 CPPUNIT_ASSERT_EQUAL(size_t(0), in
.LastRead());
187 CPPUNIT_ASSERT(in
.Eof());
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());
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
)
202 #endif // wxUSE_STREAMS