| 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 | /* |
| 3 | * This file is part of the libcdr project. |
| 4 | * |
| 5 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 | */ |
| 9 | |
| 10 | #include <algorithm> |
| 11 | |
| 12 | #include <cppunit/TestFixture.h> |
| 13 | #include <cppunit/extensions/HelperMacros.h> |
| 14 | |
| 15 | #include <librevenge-stream/librevenge-stream.h> |
| 16 | |
| 17 | #include "CDRInternalStream.h" |
| 18 | |
| 19 | namespace test |
| 20 | { |
| 21 | |
| 22 | using libcdr::CDRInternalStream; |
| 23 | |
| 24 | class CDRInternalStreamTest : public CPPUNIT_NS::TestFixture |
| 25 | { |
| 26 | public: |
| 27 | virtual void setUp(); |
| 28 | virtual void tearDown(); |
| 29 | |
| 30 | private: |
| 31 | CPPUNIT_TEST_SUITE(CDRInternalStreamTest); |
| 32 | CPPUNIT_TEST(testRead); |
| 33 | CPPUNIT_TEST(testSeek); |
| 34 | CPPUNIT_TEST_SUITE_END(); |
| 35 | |
| 36 | private: |
| 37 | void testRead(); |
| 38 | void testSeek(); |
| 39 | }; |
| 40 | |
| 41 | void CDRInternalStreamTest::setUp() |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | void CDRInternalStreamTest::tearDown() |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | void CDRInternalStreamTest::testRead() |
| 50 | { |
| 51 | const unsigned char data[] = "abc dee fgh"; |
| 52 | CDRInternalStream strm(std::vector<unsigned char>(data, data + sizeof(data))); |
| 53 | |
| 54 | CPPUNIT_ASSERT_MESSAGE("stream is already exhausted before starting to read", !strm.isEnd()); |
| 55 | |
| 56 | for (int i = 0; sizeof(data) != i; ++i) |
| 57 | { |
| 58 | unsigned long readBytes = 0; |
| 59 | const unsigned char *s = strm.read(1, readBytes); |
| 60 | |
| 61 | CPPUNIT_ASSERT(1 == readBytes); |
| 62 | CPPUNIT_ASSERT_EQUAL(data[i], s[0]); |
| 63 | CPPUNIT_ASSERT(((sizeof(data) - 1) == i) || !strm.isEnd()); |
| 64 | } |
| 65 | |
| 66 | CPPUNIT_ASSERT_MESSAGE("reading did not exhaust the stream", strm.isEnd()); |
| 67 | |
| 68 | strm.seek(0, librevenge::RVNG_SEEK_SET); |
| 69 | |
| 70 | unsigned long readBytes = 0; |
| 71 | const unsigned char *s = strm.read(sizeof(data), readBytes); |
| 72 | CPPUNIT_ASSERT(sizeof(data) == readBytes); |
| 73 | CPPUNIT_ASSERT(std::equal(data, data + sizeof(data), s)); |
| 74 | } |
| 75 | |
| 76 | void CDRInternalStreamTest::testSeek() |
| 77 | { |
| 78 | const unsigned char data[] = "abc dee fgh"; |
| 79 | CDRInternalStream strm(std::vector<unsigned char>(data, data + sizeof(data))); |
| 80 | |
| 81 | strm.seek(0, librevenge::RVNG_SEEK_SET); |
| 82 | CPPUNIT_ASSERT(0 == strm.tell()); |
| 83 | strm.seek(2, librevenge::RVNG_SEEK_SET); |
| 84 | CPPUNIT_ASSERT(2 == strm.tell()); |
| 85 | |
| 86 | strm.seek(1, librevenge::RVNG_SEEK_CUR); |
| 87 | CPPUNIT_ASSERT(3 == strm.tell()); |
| 88 | strm.seek(-2, librevenge::RVNG_SEEK_CUR); |
| 89 | CPPUNIT_ASSERT(1 == strm.tell()); |
| 90 | |
| 91 | CPPUNIT_ASSERT(!strm.isEnd()); |
| 92 | CPPUNIT_ASSERT(0 == strm.seek(0, librevenge::RVNG_SEEK_END)); |
| 93 | CPPUNIT_ASSERT(strm.isEnd()); |
| 94 | CPPUNIT_ASSERT(sizeof(data) == strm.tell()); |
| 95 | CPPUNIT_ASSERT(0 != strm.seek(1, librevenge::RVNG_SEEK_END)); // cannot seek after the end |
| 96 | CPPUNIT_ASSERT(strm.isEnd()); |
| 97 | CPPUNIT_ASSERT(0 == strm.seek(-1, librevenge::RVNG_SEEK_END)); // but can seek before it |
| 98 | CPPUNIT_ASSERT(!strm.isEnd()); |
| 99 | CPPUNIT_ASSERT((sizeof(data) - 1) == strm.tell()); |
| 100 | } |
| 101 | |
| 102 | CPPUNIT_TEST_SUITE_REGISTRATION(CDRInternalStreamTest); |
| 103 | |
| 104 | } |
| 105 | |
| 106 | /* vim:set shiftwidth=2 softtabstop=2 expandtab: */ |