Debian Codegraph @

src/test/RVNGOLEStreamTest.cpp

raw symbols SCIP
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 <sstream>
22 #include <string>
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
31 #if !defined RVNG_DATA_DIR
32 #error RVNG_DATA_DIR not defined, cannot test
33 #endif
34
35 namespace test
36 {
37
38 using librevenge::RVNGInputStream;
39 using librevenge::RVNGFileStream;
40
41 using std::string;
42
43 namespace
44 {
45
46 const string makeMessage(const unsigned n, const char *const message)
47 {
48 std::ostringstream out;
49 out << "substream " << n << ' ' << message;
50 return out.str();
51 }
52
53 void assertSubStream(const unsigned n, const char *const message, bool actual)
54 {
55 CPPUNIT_ASSERT_MESSAGE(makeMessage(n, message), actual);
56 }
57
58 template<typename T>
59 void assertSubStream(const unsigned n, const char *const message, const T &expected, const T &actual)
60 {
61 CPPUNIT_ASSERT_EQUAL_MESSAGE(makeMessage(n, message), expected, actual);
62 }
63
64 const string getContent(RVNGInputStream *const input)
65 {
66 input->seek(0, librevenge::RVNG_SEEK_END);
67 const long end = input->tell();
68 input->seek(0, librevenge::RVNG_SEEK_SET);
69
70 unsigned long readBytes = 0;
71 const unsigned char *const bytes = input->read(static_cast<unsigned long>(end), readBytes);
72 CPPUNIT_ASSERT_EQUAL(end, long(readBytes));
73
74 return string(reinterpret_cast<const char *>(bytes), size_t(readBytes));
75 }
76
77 }
78
79 class RVNGOLEStreamTest : public CPPUNIT_NS::TestFixture
80 {
81 public:
82 virtual void setUp();
83 virtual void tearDown();
84
85 private:
86 CPPUNIT_TEST_SUITE(RVNGOLEStreamTest);
87 CPPUNIT_TEST(test);
88 CPPUNIT_TEST_SUITE_END();
89
90 private:
91 void test();
92 };
93
94 void RVNGOLEStreamTest::setUp()
95 {
96 }
97
98 void RVNGOLEStreamTest::tearDown()
99 {
100 }
101
102 void RVNGOLEStreamTest::test()
103 {
104 struct SubStream
105 {
106 const char *name;
107 const char *content;
108 unsigned size;
109 bool dir;
110 };
111
112 const SubStream subStreams[] =
113 {
114 {"/", "", 11, true},
115 {"a", "a\n", 0, false},
116 {"b", "bb\n", 0, false},
117 {"c/", "", 7, true},
118 {"c/h/", "", 3, true},
119 {"c/h/i/", "", 2, true},
120 {"c/h/i/j", "jjjj\n", 0, false},
121 {"c/g", "gg\n", 0, false},
122 {"c/d/", "", 2, true},
123 {"c/d/e", "eee\n", 0, false},
124 {"f", "fff\n", 0, false},
125 };
126
127 RVNGFileStream input(RVNG_DATA_DIR "/test.ole");
128
129 CPPUNIT_ASSERT_MESSAGE("file is structured", input.isStructured());
130 CPPUNIT_ASSERT_EQUAL(RVNG_NUM_ELEMENTS(subStreams), size_t(input.subStreamCount()));
131
132 for (unsigned i = 0; i != RVNG_NUM_ELEMENTS(subStreams); ++i)
133 {
134 assertSubStream(i, "exists", input.existsSubStream(subStreams[i].name));
135 assertSubStream(i, "has matching name", string(subStreams[i].name), string(input.subStreamName(i)));
136
137 const std::unique_ptr<RVNGInputStream> streamById(input.getSubStreamById(i));
138 const std::unique_ptr<RVNGInputStream> streamByName(input.getSubStreamByName(subStreams[i].name));
139
140 assertSubStream(i, "can be retrieved by id", bool(streamById));
141 assertSubStream(i, "can be retrieved by name", bool(streamByName));
142 assertSubStream(i, "get by id is the right type", subStreams[i].dir, streamById->isStructured());
143 assertSubStream(i, "get by name is the right type", subStreams[i].dir, streamByName->isStructured());
144
145 if (subStreams[i].dir)
146 {
147 assertSubStream(i, "get by id has the right number of substreams", subStreams[i].size, streamById->subStreamCount());
148 assertSubStream(i, "get by name has the right number of substreams", subStreams[i].size, streamByName->subStreamCount());
149 }
150 else
151 {
152 assertSubStream(i, "get by id has the right content", string(subStreams[i].content), getContent(streamById.get()));
153 assertSubStream(i, "get by name has the right content", string(subStreams[i].content), getContent(streamByName.get()));
154 }
155 }
156 }
157
158 CPPUNIT_TEST_SUITE_REGISTRATION(RVNGOLEStreamTest);
159
160 }
161
162 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */