1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #include <cppunit/TestAssert.h>
11 #include <cppunit/TestFixture.h>
12 #include <cppunit/extensions/HelperMacros.h>
13 #include <tools/gen.hxx>
17 class RectangleTest
: public CppUnit::TestFixture
20 void testConstruction();
21 void testOpenClosedSize();
22 void testUnitConvesion();
23 void testSetOperators();
24 void test_rectnormalize_alreadynormal();
25 void test_rectnormalize_zerorect();
26 void test_rectnormalize_reverse_topleft_bottomright();
27 void test_rectnormalize_topright_bottomleft();
28 void test_rectnormalize_bottomleft_topright();
29 void test_rectnormalize_zerowidth_top_bottom_reversed();
30 void test_rectnormalize_zeroheight_left_right_reversed();
32 CPPUNIT_TEST_SUITE(RectangleTest
);
33 CPPUNIT_TEST(testConstruction
);
34 CPPUNIT_TEST(testOpenClosedSize
);
35 CPPUNIT_TEST(testUnitConvesion
);
36 CPPUNIT_TEST(testSetOperators
);
37 CPPUNIT_TEST(test_rectnormalize_zerorect
);
38 CPPUNIT_TEST(test_rectnormalize_alreadynormal
);
39 CPPUNIT_TEST(test_rectnormalize_reverse_topleft_bottomright
);
40 CPPUNIT_TEST(test_rectnormalize_topright_bottomleft
);
41 CPPUNIT_TEST(test_rectnormalize_bottomleft_topright
);
42 CPPUNIT_TEST(test_rectnormalize_zerowidth_top_bottom_reversed
);
43 CPPUNIT_TEST(test_rectnormalize_zeroheight_left_right_reversed
);
44 CPPUNIT_TEST_SUITE_END();
47 void RectangleTest::testConstruction()
50 tools::Rectangle
aRect1(Point(), Size(0, 20));
51 CPPUNIT_ASSERT_EQUAL(true, aRect1
.IsEmpty());
52 CPPUNIT_ASSERT_EQUAL(tools::Long(0), aRect1
.getOpenWidth());
54 tools::Rectangle aRect2
{ Point(), Point(0, 20) };
55 CPPUNIT_ASSERT_EQUAL(false, aRect2
.IsEmpty());
56 CPPUNIT_ASSERT_EQUAL(tools::Long(0), aRect2
.getOpenWidth());
58 tools::Rectangle
aRect3(0, 0, 0, 20);
59 CPPUNIT_ASSERT_EQUAL(false, aRect3
.IsEmpty());
60 CPPUNIT_ASSERT_EQUAL(tools::Long(0), aRect3
.getOpenWidth());
63 static constexpr tools::Rectangle
aRect(Point(), Size(-1, -2));
64 static_assert(!aRect
.IsEmpty());
65 static_assert(aRect
.Right() == 0);
66 static_assert(aRect
.Bottom() == -1);
68 tools::Rectangle aRect2
;
69 aRect2
.SetSize(Size(-1, -2));
70 CPPUNIT_ASSERT_EQUAL(aRect
, aRect2
);
72 static constexpr tools::Rectangle
aRect3(Point(), Size(0, 0));
73 static_assert(aRect3
.IsEmpty());
74 static_assert(aRect3
.Right() == 0);
75 static_assert(aRect3
.Bottom() == 0);
77 static constexpr tools::Rectangle
aRect4(Point(), Size(1, 1));
78 static_assert(!aRect4
.IsEmpty());
79 static_assert(aRect4
.Right() == 0);
80 static_assert(aRect4
.Bottom() == 0);
82 static constexpr tools::Rectangle
aRect5(Point(), Size(-1, -1));
83 static_assert(!aRect5
.IsEmpty());
84 static_assert(aRect5
.Right() == 0);
85 static_assert(aRect5
.Bottom() == 0);
89 void RectangleTest::testOpenClosedSize()
92 tools::Rectangle
aRect(1, 1, 1, 1);
94 CPPUNIT_ASSERT_EQUAL(tools::Long(1), aRect
.GetWidth());
95 CPPUNIT_ASSERT_EQUAL(tools::Long(1), aRect
.GetHeight());
97 CPPUNIT_ASSERT_EQUAL(tools::Long(0), aRect
.getOpenWidth());
98 CPPUNIT_ASSERT_EQUAL(tools::Long(0), aRect
.getOpenHeight());
102 tools::Rectangle
aRect(Point(), Size(1, 1));
104 CPPUNIT_ASSERT_EQUAL(tools::Long(0), aRect
.Left());
105 CPPUNIT_ASSERT_EQUAL(tools::Long(0), aRect
.Top());
106 CPPUNIT_ASSERT_EQUAL(tools::Long(0), aRect
.Right());
107 CPPUNIT_ASSERT_EQUAL(tools::Long(0), aRect
.Bottom());
109 CPPUNIT_ASSERT_EQUAL(tools::Long(1), aRect
.GetWidth());
110 CPPUNIT_ASSERT_EQUAL(tools::Long(1), aRect
.GetHeight());
112 // getOpenWidth and getOpenHeight return the size that excludes one of the bounds,
113 // unlike the ctor and GetWidth / GetHeight that operate on inclusive size
114 CPPUNIT_ASSERT_EQUAL(tools::Long(0), aRect
.getOpenWidth());
115 CPPUNIT_ASSERT_EQUAL(tools::Long(0), aRect
.getOpenHeight());
118 CPPUNIT_ASSERT_EQUAL(tools::Long(1), aRect
.GetHeight());
120 CPPUNIT_ASSERT_EQUAL(tools::Long(1), aRect
.GetWidth());
124 void RectangleTest::testUnitConvesion()
127 static constexpr tools::Rectangle
aRectTwip(100, 100, 100, 100);
128 static constexpr tools::Rectangle
aRectMm100(
129 o3tl::convert(aRectTwip
, o3tl::Length::twip
, o3tl::Length::mm100
));
130 static_assert(!aRectMm100
.IsEmpty());
131 // Make sure that we use coordinates for conversion, not width/height:
132 // the latter is ambiguous, and e.g. GetWidth(aRectTwip) gives 1, which
133 // would had been converted to 2, resulting in different LR coordinates
134 static_assert(aRectMm100
.Left() == aRectMm100
.Right());
135 static_assert(aRectMm100
.Top() == aRectMm100
.Bottom());
139 static constexpr tools::Rectangle
aRectTwip(1, 1);
140 static constexpr tools::Rectangle
aRectMm100(
141 o3tl::convert(aRectTwip
, o3tl::Length::twip
, o3tl::Length::mm100
));
142 // Make sure that result keeps the empty flag
143 static_assert(aRectMm100
.IsEmpty());
144 static_assert(aRectMm100
.IsWidthEmpty());
145 static_assert(aRectMm100
.IsHeightEmpty());
146 static_assert(aRectMm100
.GetWidth() == 0);
147 static_assert(aRectMm100
.GetHeight() == 0);
151 void RectangleTest::testSetOperators()
153 static constexpr tools::Rectangle
rect(Point(0, 0), Size(20, 20));
154 static constexpr tools::Rectangle
inside(Point(10, 10), Size(10, 10));
155 static constexpr tools::Rectangle
overlap(Point(10, 10), Size(20, 20));
156 static constexpr tools::Rectangle
outside(Point(20, 20), Size(10, 10));
157 CPPUNIT_ASSERT(rect
.Contains(inside
));
158 CPPUNIT_ASSERT(rect
.Contains(rect
));
159 CPPUNIT_ASSERT(!rect
.Contains(overlap
));
160 CPPUNIT_ASSERT(!rect
.Contains(outside
));
161 CPPUNIT_ASSERT(rect
.Overlaps(inside
));
162 CPPUNIT_ASSERT(rect
.Overlaps(rect
));
163 CPPUNIT_ASSERT(rect
.Overlaps(overlap
));
164 CPPUNIT_ASSERT(!rect
.Overlaps(outside
));
167 void RectangleTest::test_rectnormalize_alreadynormal()
169 Point
aTopLeft(0, 0);
170 Point
aBottomRight(1, 1);
172 tools::Rectangle
aRect(aTopLeft
, aBottomRight
);
175 CPPUNIT_ASSERT_EQUAL(aRect
.TopLeft(), aTopLeft
);
176 CPPUNIT_ASSERT_EQUAL(aRect
.BottomRight(), aBottomRight
);
179 void RectangleTest::test_rectnormalize_zerorect()
181 Point
aTopLeft(53, 53);
182 Point
aBottomRight(53, 53);
184 tools::Rectangle
aRect(aTopLeft
, aBottomRight
);
187 CPPUNIT_ASSERT_EQUAL(aRect
.TopLeft(), aTopLeft
);
188 CPPUNIT_ASSERT_EQUAL(aRect
.BottomRight(), aBottomRight
);
191 void RectangleTest::test_rectnormalize_reverse_topleft_bottomright()
196 tools::Rectangle
aRect(aPoint1
, aPoint2
);
199 CPPUNIT_ASSERT_EQUAL_MESSAGE("TopLeft() is wrong", Point(0, 0), aRect
.TopLeft());
200 CPPUNIT_ASSERT_EQUAL_MESSAGE("BottomRight() is wrong", Point(1, 1), aRect
.BottomRight());
203 void RectangleTest::test_rectnormalize_topright_bottomleft()
208 tools::Rectangle
aRect(aPoint1
, aPoint2
);
211 CPPUNIT_ASSERT_EQUAL_MESSAGE("TopLeft() is wrong", Point(0, 0), aRect
.TopLeft());
212 CPPUNIT_ASSERT_EQUAL_MESSAGE("BottomRight() is wrong", Point(1, 1), aRect
.BottomRight());
215 void RectangleTest::test_rectnormalize_bottomleft_topright()
220 tools::Rectangle
aRect(aPoint1
, aPoint2
);
223 CPPUNIT_ASSERT_EQUAL_MESSAGE("TopLeft() is wrong", Point(0, 0), aRect
.TopLeft());
224 CPPUNIT_ASSERT_EQUAL_MESSAGE("BottomRight() is wrong", Point(1, 1), aRect
.BottomRight());
227 void RectangleTest::test_rectnormalize_zerowidth_top_bottom_reversed()
232 tools::Rectangle
aRect(aPoint1
, aPoint2
);
235 CPPUNIT_ASSERT_EQUAL_MESSAGE("TopLeft() is wrong", Point(0, 0), aRect
.TopLeft());
236 CPPUNIT_ASSERT_EQUAL_MESSAGE("BottomRight() is wrong", Point(0, 1), aRect
.BottomRight());
239 void RectangleTest::test_rectnormalize_zeroheight_left_right_reversed()
244 tools::Rectangle
aRect(aPoint1
, aPoint2
);
247 CPPUNIT_ASSERT_EQUAL_MESSAGE("TopLeft() is wrong", Point(0, 0), aRect
.TopLeft());
248 CPPUNIT_ASSERT_EQUAL_MESSAGE("BottomRight() is wrong", Point(1, 0), aRect
.BottomRight());
251 CPPUNIT_TEST_SUITE_REGISTRATION(RectangleTest
);
254 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */