| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ |
| 2 | /* librevenge |
| 3 | * Version: MPL 2.0 / LGPLv2.1+ |
| 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 | * Major Contributor(s): |
| 10 | * Copyright (C) 2006 Andrew Ziem |
| 11 | * |
| 12 | * For minor contributions see the git repository. |
| 13 | * |
| 14 | * Alternatively, the contents of this file may be used under the terms |
| 15 | * of the GNU Lesser General Public License Version 2.1 or later |
| 16 | * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are |
| 17 | * applicable instead of those above. |
| 18 | */ |
| 19 | |
| 20 | #include <memory> |
| 21 | #include <stdio.h> |
| 22 | #include <unistd.h> |
| 23 | |
| 24 | #include <cppunit/TestFixture.h> |
| 25 | #include <cppunit/extensions/HelperMacros.h> |
| 26 | |
| 27 | #include <librevenge/librevenge.h> |
| 28 | |
| 29 | #include "librevenge_internal.h" |
| 30 | #include "RVNGMemoryStream.h" |
| 31 | |
| 32 | using namespace librevenge; |
| 33 | |
| 34 | #define TMP_FILENAME "librevenge-unittest.tmp" |
| 35 | |
| 36 | namespace test |
| 37 | { |
| 38 | |
| 39 | class RVNGStreamTest : public CPPUNIT_NS::TestFixture |
| 40 | { |
| 41 | public: |
| 42 | virtual void setUp(); |
| 43 | virtual void tearDown(); |
| 44 | |
| 45 | private: |
| 46 | CPPUNIT_TEST_SUITE(RVNGStreamTest); |
| 47 | CPPUNIT_TEST(testFileStream); |
| 48 | CPPUNIT_TEST(testStringStream); |
| 49 | CPPUNIT_TEST_SUITE_END(); |
| 50 | |
| 51 | private: |
| 52 | void testFileStream(); |
| 53 | void testMemoryInputStream(); |
| 54 | void testStringStream(); |
| 55 | }; |
| 56 | |
| 57 | void RVNGStreamTest::setUp() |
| 58 | { |
| 59 | FILE *f = fopen(TMP_FILENAME, "w"); |
| 60 | fprintf(f, "%c%c%c%c",1,2,3,4); |
| 61 | fprintf(f, "%c%c%c%c",0,5,6,7); |
| 62 | fclose(f); |
| 63 | } |
| 64 | |
| 65 | void RVNGStreamTest::tearDown() |
| 66 | { |
| 67 | unlink(TMP_FILENAME); |
| 68 | } |
| 69 | |
| 70 | void RVNGStreamTest::testFileStream() |
| 71 | { |
| 72 | std::unique_ptr<RVNGInputStream> input(new RVNGFileStream(TMP_FILENAME)); |
| 73 | unsigned long numBytesRead = 0; |
| 74 | |
| 75 | CPPUNIT_ASSERT_EQUAL(false, input->isStructured()); |
| 76 | CPPUNIT_ASSERT_EQUAL((RVNGInputStream *) NULL, input->getSubStreamByName("foo")); |
| 77 | |
| 78 | // test read() |
| 79 | input->seek(0, RVNG_SEEK_SET); |
| 80 | CPPUNIT_ASSERT(NULL == input->read(0, numBytesRead)); |
| 81 | CPPUNIT_ASSERT_EQUAL((unsigned long)0, numBytesRead); |
| 82 | CPPUNIT_ASSERT_EQUAL((long) 0, input->tell()); |
| 83 | CPPUNIT_ASSERT(NULL != input->read(1, numBytesRead)); |
| 84 | CPPUNIT_ASSERT_EQUAL((long) 1, input->tell()); |
| 85 | |
| 86 | input->seek(0, RVNG_SEEK_SET); |
| 87 | CPPUNIT_ASSERT(NULL != input->read(50, numBytesRead)); |
| 88 | CPPUNIT_ASSERT_EQUAL((long) 8, input->tell()); |
| 89 | |
| 90 | // test seek(), tell(), isEnd() |
| 91 | input->seek(1, RVNG_SEEK_SET); |
| 92 | CPPUNIT_ASSERT_EQUAL((long) 1, input->tell()); |
| 93 | |
| 94 | input->seek(0, RVNG_SEEK_SET); |
| 95 | CPPUNIT_ASSERT_EQUAL((long) 0, input->tell()); |
| 96 | |
| 97 | input->seek(8, RVNG_SEEK_SET); |
| 98 | CPPUNIT_ASSERT_EQUAL((long) 8, input->tell()); |
| 99 | |
| 100 | input->seek(-1, RVNG_SEEK_SET); |
| 101 | CPPUNIT_ASSERT_EQUAL((long) 0, input->tell()); |
| 102 | |
| 103 | input->seek(8, RVNG_SEEK_SET); |
| 104 | CPPUNIT_ASSERT_EQUAL(true, input->isEnd()); |
| 105 | |
| 106 | input->seek(10000, RVNG_SEEK_SET); |
| 107 | CPPUNIT_ASSERT(10000 != input->tell()); |
| 108 | CPPUNIT_ASSERT(input->isEnd()); |
| 109 | } |
| 110 | |
| 111 | void RVNGStreamTest::testStringStream() |
| 112 | { |
| 113 | std::unique_ptr<RVNGInputStream> input(new RVNGStringStream((const unsigned char *)"\1\2\3\4\0\5\6\7", 8)); |
| 114 | unsigned long numBytesRead = 0; |
| 115 | |
| 116 | CPPUNIT_ASSERT_EQUAL(false, input->isStructured()); |
| 117 | CPPUNIT_ASSERT_EQUAL((RVNGInputStream *) NULL, input->getSubStreamByName("foo")); |
| 118 | |
| 119 | // test read() |
| 120 | input->seek(0, RVNG_SEEK_SET); |
| 121 | CPPUNIT_ASSERT(NULL == input->read(0, numBytesRead)); |
| 122 | CPPUNIT_ASSERT_EQUAL((unsigned long) 0, numBytesRead); |
| 123 | CPPUNIT_ASSERT_EQUAL((long) 0, input->tell()); |
| 124 | CPPUNIT_ASSERT(NULL != input->read(1, numBytesRead)); |
| 125 | CPPUNIT_ASSERT_EQUAL((long) 1, input->tell()); |
| 126 | |
| 127 | input->seek(0, RVNG_SEEK_SET); |
| 128 | CPPUNIT_ASSERT(NULL != input->read(50, numBytesRead)); |
| 129 | CPPUNIT_ASSERT_EQUAL((long) 8, input->tell()); |
| 130 | |
| 131 | // test seek(), tell(), isEnd() |
| 132 | input->seek(1, RVNG_SEEK_SET); |
| 133 | CPPUNIT_ASSERT_EQUAL((long) 1, input->tell()); |
| 134 | |
| 135 | input->seek(0, RVNG_SEEK_SET); |
| 136 | CPPUNIT_ASSERT_EQUAL((long) 0, input->tell()); |
| 137 | |
| 138 | input->seek(8, RVNG_SEEK_SET); |
| 139 | CPPUNIT_ASSERT_EQUAL((long) 8, input->tell()); |
| 140 | |
| 141 | input->seek(-1, RVNG_SEEK_SET); |
| 142 | CPPUNIT_ASSERT_EQUAL((long) 0, input->tell()); |
| 143 | |
| 144 | input->seek(8, RVNG_SEEK_SET); |
| 145 | CPPUNIT_ASSERT_EQUAL(true, input->isEnd()); |
| 146 | |
| 147 | input->seek(10000, RVNG_SEEK_SET); |
| 148 | CPPUNIT_ASSERT(10000 != input->tell()); |
| 149 | CPPUNIT_ASSERT(input->isEnd()); |
| 150 | } |
| 151 | |
| 152 | CPPUNIT_TEST_SUITE_REGISTRATION(RVNGStreamTest); |
| 153 | |
| 154 | } |
| 155 | |
| 156 | /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ |