| 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: wxWindows 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/private/fileback.h" |
| 23 | #include "bstream.h" |
| 24 | |
| 25 | const size_t TESTSIZE = 256; |
| 26 | const size_t BUFSIZE = 100; |
| 27 | |
| 28 | /////////////////////////////////////////////////////////////////////////////// |
| 29 | // Parent stream for testing |
| 30 | |
| 31 | class TestStream : public wxMemoryInputStream |
| 32 | { |
| 33 | public: |
| 34 | TestStream(const void *buf, size_t size) |
| 35 | : wxMemoryInputStream(buf, size) { } |
| 36 | |
| 37 | wxFileOffset GetLength() const { return wxInvalidOffset; } |
| 38 | }; |
| 39 | |
| 40 | |
| 41 | /////////////////////////////////////////////////////////////////////////////// |
| 42 | // The test case |
| 43 | |
| 44 | class backStream : public CppUnit::TestCase |
| 45 | { |
| 46 | public: |
| 47 | backStream(); |
| 48 | |
| 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(); |
| 57 | |
| 58 | void ReadLenSeek(); |
| 59 | void LenSeekRead(); |
| 60 | void SeekReadLen(); |
| 61 | void ReadAll(); |
| 62 | void ReadTooMuch(); |
| 63 | void EmptyStream(); |
| 64 | |
| 65 | private: |
| 66 | void Read(wxInputStream& in, size_t size1, size_t size2, size_t size3); |
| 67 | void Len(wxBackedInputStream& in); |
| 68 | void Seek(wxInputStream& in); |
| 69 | |
| 70 | char m_testdata[TESTSIZE]; |
| 71 | }; |
| 72 | |
| 73 | backStream::backStream() |
| 74 | { |
| 75 | for (unsigned i = 0; i < TESTSIZE; i++) |
| 76 | m_testdata[i] = i; |
| 77 | } |
| 78 | |
| 79 | void backStream::ReadLenSeek() |
| 80 | { |
| 81 | wxBackingFile bf(new TestStream(m_testdata, TESTSIZE), BUFSIZE); |
| 82 | wxBackedInputStream in(bf); |
| 83 | |
| 84 | Read(in, BUFSIZE, BUFSIZE / 2, 2 * BUFSIZE / 3); |
| 85 | Len(in); |
| 86 | Seek(in); |
| 87 | } |
| 88 | |
| 89 | void backStream::LenSeekRead() |
| 90 | { |
| 91 | wxBackingFile bf(new TestStream(m_testdata, TESTSIZE), BUFSIZE); |
| 92 | wxBackedInputStream in(bf); |
| 93 | |
| 94 | Len(in); |
| 95 | Seek(in); |
| 96 | Read(in, BUFSIZE, BUFSIZE / 2, 2 * BUFSIZE / 3); |
| 97 | } |
| 98 | |
| 99 | void backStream::SeekReadLen() |
| 100 | { |
| 101 | wxBackingFile bf(new TestStream(m_testdata, TESTSIZE), BUFSIZE); |
| 102 | wxBackedInputStream in(bf); |
| 103 | |
| 104 | Seek(in); |
| 105 | Read(in, BUFSIZE, BUFSIZE / 2, 2 * BUFSIZE / 3); |
| 106 | Len(in); |
| 107 | } |
| 108 | |
| 109 | void backStream::ReadAll() |
| 110 | { |
| 111 | wxBackingFile bf(new TestStream(m_testdata, TESTSIZE), BUFSIZE); |
| 112 | wxBackedInputStream in(bf); |
| 113 | |
| 114 | Read(in, TESTSIZE, 0, 0); |
| 115 | } |
| 116 | |
| 117 | void backStream::ReadTooMuch() |
| 118 | { |
| 119 | wxBackingFile bf(new TestStream(m_testdata, TESTSIZE), BUFSIZE); |
| 120 | wxBackedInputStream in(bf); |
| 121 | |
| 122 | char buf[TESTSIZE * 2]; |
| 123 | |
| 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); |
| 127 | } |
| 128 | |
| 129 | void backStream::EmptyStream() |
| 130 | { |
| 131 | wxBackingFile bf(new TestStream(m_testdata, 0), BUFSIZE); |
| 132 | wxBackedInputStream in(bf); |
| 133 | |
| 134 | char buf[1]; |
| 135 | |
| 136 | CPPUNIT_ASSERT_EQUAL(size_t(0), in.Read(buf, 1).LastRead()); |
| 137 | CPPUNIT_ASSERT(in.Eof()); |
| 138 | } |
| 139 | |
| 140 | void backStream::Read(wxInputStream& in, |
| 141 | size_t size1, |
| 142 | size_t size2, |
| 143 | size_t size3) |
| 144 | { |
| 145 | const size_t remainder = TESTSIZE - size1 - size2 - size3; |
| 146 | char buf[TESTSIZE]; |
| 147 | char *testdata = m_testdata; |
| 148 | |
| 149 | in.SeekI(0); |
| 150 | |
| 151 | CPPUNIT_ASSERT_EQUAL(size1, in.Read(buf, size1).LastRead()); |
| 152 | CPPUNIT_ASSERT(in.IsOk()); |
| 153 | CPPUNIT_ASSERT(memcmp(buf, testdata, size1) == 0); |
| 154 | testdata += size1; |
| 155 | |
| 156 | CPPUNIT_ASSERT_EQUAL(size2, in.Read(buf, size2).LastRead()); |
| 157 | CPPUNIT_ASSERT(in.IsOk()); |
| 158 | CPPUNIT_ASSERT(memcmp(buf, testdata, size2) == 0); |
| 159 | testdata += size2; |
| 160 | |
| 161 | CPPUNIT_ASSERT_EQUAL(size3, in.Read(buf, size3).LastRead()); |
| 162 | CPPUNIT_ASSERT(in.IsOk()); |
| 163 | CPPUNIT_ASSERT(memcmp(buf, testdata, size3) == 0); |
| 164 | testdata += size3; |
| 165 | |
| 166 | CPPUNIT_ASSERT_EQUAL(remainder, in.Read(buf, TESTSIZE).LastRead()); |
| 167 | CPPUNIT_ASSERT(in.Eof()); |
| 168 | CPPUNIT_ASSERT(memcmp(buf, testdata, remainder) == 0); |
| 169 | |
| 170 | CPPUNIT_ASSERT_EQUAL(size_t(0), in.Read(buf, TESTSIZE).LastRead()); |
| 171 | CPPUNIT_ASSERT(in.Eof()); |
| 172 | } |
| 173 | |
| 174 | void backStream::Len(wxBackedInputStream& in) |
| 175 | { |
| 176 | CPPUNIT_ASSERT_EQUAL(wxFileOffset(TESTSIZE), in.FindLength()); |
| 177 | } |
| 178 | |
| 179 | void backStream::Seek(wxInputStream& in) |
| 180 | { |
| 181 | CPPUNIT_ASSERT_EQUAL(wxFileOffset(TESTSIZE), in.SeekI(TESTSIZE)); |
| 182 | in.GetC(); |
| 183 | CPPUNIT_ASSERT_EQUAL(size_t(0), in.LastRead()); |
| 184 | CPPUNIT_ASSERT(in.Eof()); |
| 185 | |
| 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()); |
| 192 | } |
| 193 | } |
| 194 | |
| 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) |