Debian Codegraph @

src/test/UtilsTest.cpp

raw
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3 * This file is part of the libqxp 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 <memory>
11
12 #include <cppunit/TestFixture.h>
13 #include <cppunit/extensions/HelperMacros.h>
14
15 #include <librevenge-stream/librevenge-stream.h>
16
17 #include "libqxp_utils.h"
18
19 namespace test
20 {
21
22 using libqxp::readFloat16;
23 using libqxp::readFraction;
24
25 using librevenge::RVNGStringStream;
26 using std::make_shared;
27
28 class UtilsTest : public CPPUNIT_NS::TestFixture
29 {
30 public:
31 virtual void setUp() override;
32 virtual void tearDown() override;
33
34 private:
35 CPPUNIT_TEST_SUITE(UtilsTest);
36 CPPUNIT_TEST(testFloat16);
37 CPPUNIT_TEST(testReadFraction);
38 CPPUNIT_TEST(testGetRemainingLength);
39 CPPUNIT_TEST_SUITE_END();
40
41 private:
42 void testFloat16();
43 void testReadFraction();
44 void testGetRemainingLength();
45 };
46
47 void UtilsTest::setUp()
48 {
49 }
50
51 void UtilsTest::tearDown()
52 {
53 }
54
55 void UtilsTest::testFloat16()
56 {
57 uint8_t data[] =
58 {
59 0x0, 0x0,
60 0xff, 0xff,
61 0x88, 0xc6,
62 0xc6, 0x88
63 };
64 auto stream = make_shared<RVNGStringStream>(data, sizeof(data));
65
66 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, readFloat16(stream), 0.001);
67 CPPUNIT_ASSERT_DOUBLES_EQUAL(1.0, readFloat16(stream), 0.001);
68 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.776, readFloat16(stream, false), 0.001);
69 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.776, readFloat16(stream, true), 0.001);
70 }
71
72 void UtilsTest::testReadFraction()
73 {
74 uint8_t data[] =
75 {
76 0x0, 0x0, 0x0, 0x0,
77 0x0, 0x0, 0x24, 0x0,
78 0x0, 0x24, 0x0, 0x0,
79 0x0, 0x80, 0xb0, 0x01,
80 0x01, 0xb0, 0x80, 0x0,
81 0x0, 0x80, 0xf7, 0xff,
82 0xff, 0xf7, 0x80, 0x0,
83 };
84 auto stream = make_shared<RVNGStringStream>(data, sizeof(data));
85
86 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, readFraction(stream), 0.001);
87 CPPUNIT_ASSERT_DOUBLES_EQUAL(36.0, readFraction(stream, false), 0.001);
88 CPPUNIT_ASSERT_DOUBLES_EQUAL(36.0, readFraction(stream, true), 0.001);
89 CPPUNIT_ASSERT_DOUBLES_EQUAL(432.5, readFraction(stream, false), 0.001);
90 CPPUNIT_ASSERT_DOUBLES_EQUAL(432.5, readFraction(stream, true), 0.001);
91 CPPUNIT_ASSERT_DOUBLES_EQUAL(-8.5, readFraction(stream, false), 0.001);
92 CPPUNIT_ASSERT_DOUBLES_EQUAL(-8.5, readFraction(stream, true), 0.001);
93 }
94
95 void UtilsTest::testGetRemainingLength()
96 {
97 uint8_t data[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
98 auto stream = make_shared<RVNGStringStream>(data, sizeof(data));
99
100 using libqxp::getRemainingLength;
101 CPPUNIT_ASSERT_EQUAL(sizeof(data), size_t(getRemainingLength(stream)));
102 stream->seek(2, librevenge::RVNG_SEEK_CUR);
103 CPPUNIT_ASSERT_EQUAL(sizeof(data) - 2, size_t(getRemainingLength(stream)));
104 stream->seek(2, librevenge::RVNG_SEEK_CUR);
105 CPPUNIT_ASSERT_EQUAL(sizeof(data) - 4, size_t(getRemainingLength(stream)));
106 stream->seek(0, librevenge::RVNG_SEEK_END);
107 CPPUNIT_ASSERT_EQUAL(0ul, getRemainingLength(stream));
108 }
109
110 CPPUNIT_TEST_SUITE_REGISTRATION(UtilsTest);
111
112 }
113
114 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */