Avoid potential negative array index access to cached text.
[LibreOffice.git] / basegfx / test / B2DTupleTest.cxx
blob100808fe8a78a387c5dcac080d17141d2f68d7ab
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <cppunit/TestFixture.h>
21 #include <cppunit/extensions/HelperMacros.h>
23 #include <basegfx/tuple/b2dtuple.hxx>
24 #include <basegfx/tuple/b2ituple.hxx>
25 #include <basegfx/tuple/b2i64tuple.hxx>
27 namespace basegfx
29 class B2DTupleTest : public CppUnit::TestFixture
31 public:
32 void testEmpty()
34 B2DTuple aTuple;
35 CPPUNIT_ASSERT_EQUAL(true, aTuple.equalZero());
37 B2ITuple aTupleInteger;
38 CPPUNIT_ASSERT_EQUAL(true, aTupleInteger.equalZero());
40 B2I64Tuple aTupleLong;
41 CPPUNIT_ASSERT_EQUAL(true, aTupleLong.equalZero());
44 void testEquals()
46 B2DTuple aTuple(1.0, 1.0);
47 CPPUNIT_ASSERT_EQUAL(true, aTuple.equal({ 1.0, 1.0 }));
48 CPPUNIT_ASSERT_EQUAL(false, aTuple.equal({ 0.99, 0.99 }));
50 B2ITuple aTupleInteger(1, 1);
51 CPPUNIT_ASSERT_EQUAL(true, aTupleInteger.equal({ 1, 1 }));
52 CPPUNIT_ASSERT_EQUAL(false, aTupleInteger.equal({ 1, 0 }));
53 CPPUNIT_ASSERT_EQUAL(false, aTupleInteger.equal({ 0, 1 }));
55 B2I64Tuple aTupleLong(1, 1);
56 CPPUNIT_ASSERT_EQUAL(true, aTupleLong.equal({ 1, 1 }));
57 CPPUNIT_ASSERT_EQUAL(false, aTupleLong.equal({ 1, 0 }));
58 CPPUNIT_ASSERT_EQUAL(false, aTupleLong.equal({ 0, 1 }));
61 void testOperatorAddition()
63 B2DTuple aTuple(4.0, 8.0);
64 aTuple += { 2.0, 3.0 };
66 CPPUNIT_ASSERT_DOUBLES_EQUAL(6.0, aTuple.getX(), 1e-2);
67 CPPUNIT_ASSERT_DOUBLES_EQUAL(11.0, aTuple.getY(), 1e-2);
69 B2ITuple aTupleInt(4, 8);
70 aTupleInt += { 2, 3 };
72 CPPUNIT_ASSERT_EQUAL(sal_Int32(6), aTupleInt.getX());
73 CPPUNIT_ASSERT_EQUAL(sal_Int32(11), aTupleInt.getY());
75 B2I64Tuple aTuple64(4, 8);
76 aTuple64 += { 2, 3 };
78 CPPUNIT_ASSERT_EQUAL(sal_Int64(6), aTuple64.getX());
79 CPPUNIT_ASSERT_EQUAL(sal_Int64(11), aTuple64.getY());
82 void testOperatorSubstraction()
84 B2DTuple aTuple(4.0, 8.0);
85 aTuple -= { 2.0, 3.0 };
87 CPPUNIT_ASSERT_DOUBLES_EQUAL(2.0, aTuple.getX(), 1e-2);
88 CPPUNIT_ASSERT_DOUBLES_EQUAL(5.0, aTuple.getY(), 1e-2);
90 B2ITuple aTupleInt(4, 8);
91 aTupleInt -= { 2, 3 };
93 CPPUNIT_ASSERT_EQUAL(sal_Int32(2), aTupleInt.getX());
94 CPPUNIT_ASSERT_EQUAL(sal_Int32(5), aTupleInt.getY());
96 B2I64Tuple aTuple64(4, 8);
97 aTuple64 -= { 2, 3 };
99 CPPUNIT_ASSERT_EQUAL(sal_Int64(2), aTuple64.getX());
100 CPPUNIT_ASSERT_EQUAL(sal_Int64(5), aTuple64.getY());
103 void testOperatorMultiply()
105 B2DTuple aTuple(4.0, 8.0);
106 aTuple *= { 2.0, 3.0 };
108 CPPUNIT_ASSERT_DOUBLES_EQUAL(8.0, aTuple.getX(), 1e-2);
109 CPPUNIT_ASSERT_DOUBLES_EQUAL(24.0, aTuple.getY(), 1e-2);
111 aTuple *= 2.0;
113 CPPUNIT_ASSERT_DOUBLES_EQUAL(16.0, aTuple.getX(), 1e-2);
114 CPPUNIT_ASSERT_DOUBLES_EQUAL(48.0, aTuple.getY(), 1e-2);
116 B2ITuple aTupleInt(4, 8);
117 aTupleInt *= { 2, 3 };
119 CPPUNIT_ASSERT_EQUAL(sal_Int32(8), aTupleInt.getX());
120 CPPUNIT_ASSERT_EQUAL(sal_Int32(24), aTupleInt.getY());
122 aTupleInt *= 2.0;
124 CPPUNIT_ASSERT_EQUAL(sal_Int32(16), aTupleInt.getX());
125 CPPUNIT_ASSERT_EQUAL(sal_Int32(48), aTupleInt.getY());
127 B2I64Tuple aTuple64(4, 8);
128 aTuple64 *= { 2, 3 };
130 CPPUNIT_ASSERT_EQUAL(sal_Int64(8), aTuple64.getX());
131 CPPUNIT_ASSERT_EQUAL(sal_Int64(24), aTuple64.getY());
133 aTuple64 *= 2.0;
135 CPPUNIT_ASSERT_EQUAL(sal_Int64(16), aTuple64.getX());
136 CPPUNIT_ASSERT_EQUAL(sal_Int64(48), aTuple64.getY());
139 void testOperatorDivide()
141 B2DTuple aTuple(4.0, 8.0);
142 aTuple /= { 2.0, 8.0 };
144 CPPUNIT_ASSERT_DOUBLES_EQUAL(2.0, aTuple.getX(), 1e-2);
145 CPPUNIT_ASSERT_DOUBLES_EQUAL(1.0, aTuple.getY(), 1e-2);
147 B2ITuple aTupleInt(4, 8);
148 aTupleInt /= { 2, 8 };
150 CPPUNIT_ASSERT_EQUAL(sal_Int32(2), aTupleInt.getX());
151 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), aTupleInt.getY());
153 B2I64Tuple aTuple64(4, 8);
154 aTuple64 /= { 2, 8 };
156 CPPUNIT_ASSERT_EQUAL(sal_Int64(2), aTuple64.getX());
157 CPPUNIT_ASSERT_EQUAL(sal_Int64(1), aTuple64.getY());
160 void testOperatorEqualUnequal()
162 B2DTuple aTuple(4.0, 8.0);
163 B2DTuple aTuple2 = aTuple;
165 CPPUNIT_ASSERT_EQUAL(true, aTuple == aTuple);
166 CPPUNIT_ASSERT_EQUAL(true, aTuple == aTuple2);
167 CPPUNIT_ASSERT_EQUAL(true, aTuple == B2DTuple(4.0, 8.0));
168 CPPUNIT_ASSERT_EQUAL(false, aTuple == B2DTuple(4.0, 7.99));
169 CPPUNIT_ASSERT_EQUAL(false, aTuple == B2DTuple(3.99, 8.0));
171 B2ITuple aTupleInt(4, 8);
172 B2ITuple aTupleInt2 = aTupleInt;
174 CPPUNIT_ASSERT_EQUAL(true, aTupleInt == aTupleInt);
175 CPPUNIT_ASSERT_EQUAL(true, aTupleInt == aTupleInt2);
176 CPPUNIT_ASSERT_EQUAL(true, aTupleInt == B2ITuple(4, 8));
177 CPPUNIT_ASSERT_EQUAL(false, aTupleInt == B2ITuple(4, 7));
178 CPPUNIT_ASSERT_EQUAL(false, aTupleInt == B2ITuple(3, 8));
180 B2I64Tuple aTuple64(4, 8);
181 B2I64Tuple aTuple64_2 = aTuple64;
183 CPPUNIT_ASSERT_EQUAL(true, aTuple64 == aTuple64);
184 CPPUNIT_ASSERT_EQUAL(true, aTuple64 == aTuple64_2);
185 CPPUNIT_ASSERT_EQUAL(true, aTuple64 == B2I64Tuple(4, 8));
186 CPPUNIT_ASSERT_EQUAL(false, aTuple64 == B2I64Tuple(4, 7));
187 CPPUNIT_ASSERT_EQUAL(false, aTuple64 == B2I64Tuple(3, 8));
190 void testOperatorMinus()
192 B2DTuple aTupleMinus = -B2DTuple(4.0, 8.0);
193 CPPUNIT_ASSERT_DOUBLES_EQUAL(-4.0, aTupleMinus.getX(), 1e-2);
194 CPPUNIT_ASSERT_DOUBLES_EQUAL(-8.0, aTupleMinus.getY(), 1e-2);
195 B2DTuple aTupleZero = -B2DTuple(0.0, 0.0);
196 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, aTupleZero.getX(), 1e-2);
197 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, aTupleZero.getY(), 1e-2);
199 B2ITuple aTupleIntMinus = -B2ITuple(4, 8);
200 CPPUNIT_ASSERT_EQUAL(sal_Int32(-4), aTupleIntMinus.getX());
201 CPPUNIT_ASSERT_EQUAL(sal_Int32(-8), aTupleIntMinus.getY());
202 B2ITuple aTupleIntZero = -B2ITuple(0, 0);
203 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), aTupleIntZero.getX());
204 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), aTupleIntZero.getY());
206 B2I64Tuple aTuple64Minus = -B2I64Tuple(4, 8);
207 CPPUNIT_ASSERT_EQUAL(sal_Int64(-4), aTuple64Minus.getX());
208 CPPUNIT_ASSERT_EQUAL(sal_Int64(-8), aTuple64Minus.getY());
209 B2I64Tuple aTuple64Zero = -B2I64Tuple(0, 0);
210 CPPUNIT_ASSERT_EQUAL(sal_Int64(0), aTuple64Zero.getX());
211 CPPUNIT_ASSERT_EQUAL(sal_Int64(0), aTuple64Zero.getY());
214 CPPUNIT_TEST_SUITE(B2DTupleTest);
215 CPPUNIT_TEST(testEmpty);
216 CPPUNIT_TEST(testEquals);
217 CPPUNIT_TEST(testOperatorAddition);
218 CPPUNIT_TEST(testOperatorSubstraction);
219 CPPUNIT_TEST(testOperatorMultiply);
220 CPPUNIT_TEST(testOperatorDivide);
221 CPPUNIT_TEST(testOperatorEqualUnequal);
222 CPPUNIT_TEST(testOperatorMinus);
223 CPPUNIT_TEST_SUITE_END();
226 } // namespace basegfx
228 CPPUNIT_TEST_SUITE_REGISTRATION(basegfx::B2DTupleTest);
230 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */