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/.
11 #include <cppunit/TestAssert.h>
12 #include <cppunit/TestFixture.h>
13 #include <cppunit/extensions/HelperMacros.h>
15 #include <swregion.hxx>
16 #include <vcl/region.hxx>
18 class RegionUnittest
: public CppUnit::TestFixture
24 CPPUNIT_TEST_SUITE(RegionUnittest
);
25 CPPUNIT_TEST(testCompress
);
26 CPPUNIT_TEST(testInvert
);
27 CPPUNIT_TEST_SUITE_END();
30 void RegionUnittest::testCompress()
34 // All inside each other, check it'll compress them to the largest one.
35 region
= SwRegionRects();
36 region
+= SwRect(Point(10, 10), Size(10, 10));
37 region
+= SwRect(Point(10, 10), Size(20, 20));
38 region
+= SwRect(Point(10, 10), Size(100, 100));
39 region
+= SwRect(Point(10, 10), Size(50, 50));
40 region
.Compress(SwRegionRects::CompressExact
);
41 CPPUNIT_ASSERT_EQUAL(size_t(1), region
.size());
42 CPPUNIT_ASSERT_EQUAL(SwRect(Point(10, 10), Size(100, 100)), region
[0]);
44 // Check merging of adjacent rectangles. This will merge first two groups
45 // and then those two merged rects only in the next iteration.
46 region
= SwRegionRects();
47 region
+= SwRect(Point(10, 10), Size(10000, 10000));
48 region
+= SwRect(Point(10010, 10), Size(10000, 10000));
49 region
+= SwRect(Point(10, 10010), Size(10000, 10000));
50 region
+= SwRect(Point(10010, 10010), Size(10000, 10000));
51 region
.Compress(SwRegionRects::CompressExact
);
52 CPPUNIT_ASSERT_EQUAL(size_t(1), region
.size());
53 CPPUNIT_ASSERT_EQUAL(SwRect(Point(10, 10), Size(20000, 20000)), region
[0]);
55 // Check fuzzy compress, two almost aligned rects will be compressed to one.
56 region
= SwRegionRects();
57 region
+= SwRect(Point(10, 10), Size(100, 100));
58 region
+= SwRect(Point(110, 10), Size(100, 90));
59 region
.Compress(SwRegionRects::CompressExact
);
60 CPPUNIT_ASSERT_EQUAL(size_t(2), region
.size());
61 region
.Compress(SwRegionRects::CompressFuzzy
);
62 CPPUNIT_ASSERT_EQUAL(size_t(1), region
.size());
63 CPPUNIT_ASSERT_EQUAL(SwRect(Point(10, 10), Size(200, 100)), region
[0]);
65 // Check it doesn't crash because of empty size.
66 region
= SwRegionRects();
67 region
+= SwRect(Point(0, 0), Size(0, 0));
68 region
+= SwRect(Point(10, 10), Size(0, 0));
69 region
+= SwRect(Point(100, 100), Size(0, 0));
70 region
.Compress(SwRegionRects::CompressExact
);
71 region
.Compress(SwRegionRects::CompressFuzzy
);
74 void RegionUnittest::testInvert()
76 // Check that punching holes and inverting has the same result as adding up rects.
77 const SwRect
fullRect(Point(100, 100), Size(1000, 1000));
79 = { { Point(200, 200), Size(200, 200) },
80 { Point(0, 0), Size(200, 200) }, // this one is intentionally partially out of bounds
81 { Point(700, 760), Size(20, 150) },
82 { Point(100, 300), Size(200, 200) },
83 { Point(100, 800), Size(10, 10) }, // these two partially overlap
84 { Point(105, 805), Size(10, 10) } };
85 SwRegionRects
region1(fullRect
);
86 for (const SwRect
& rect
: rects
)
89 region1
.Compress(SwRegionRects::CompressExact
);
90 SwRegionRects region2
;
91 region2
.ChangeOrigin(fullRect
);
92 for (const SwRect
& rect
: rects
)
94 region2
.LimitToOrigin();
95 region2
.Compress(SwRegionRects::CompressExact
);
96 // The regions should be the same area, but not necessarily the same representation.
97 // SwRegionRects cannot compare those easily, but vcl::Region can.
98 vcl::Region vclRegion1
, vclRegion2
;
99 for (const SwRect
& rect
: region1
)
100 vclRegion1
.Union(tools::Rectangle(rect
.Pos(), rect
.SSize()));
101 for (const SwRect
& rect
: region2
)
102 vclRegion2
.Union(tools::Rectangle(rect
.Pos(), rect
.SSize()));
103 CPPUNIT_ASSERT_EQUAL(vclRegion1
, vclRegion2
);
106 CPPUNIT_TEST_SUITE_REGISTRATION(RegionUnittest
);
108 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */