Avoid potential negative array index access to cached text.
[LibreOffice.git] / basegfx / test / clipstate.cxx
blob245da3b88da35ea8b05b5aef96c0bc46762bbb9d
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/TestAssert.h>
21 #include <cppunit/TestFixture.h>
22 #include <cppunit/extensions/HelperMacros.h>
24 #include <basegfx/utils/b2dclipstate.hxx>
25 #include <basegfx/polygon/b2dpolypolygontools.hxx>
26 #include <basegfx/polygon/b2dpolypolygon.hxx>
28 using namespace ::basegfx;
30 namespace basegfx2d
33 class clipstate : public CppUnit::TestFixture
35 private:
36 utils::B2DClipState aUnion1;
37 utils::B2DClipState aUnion2;
38 utils::B2DClipState aIntersect;
39 utils::B2DClipState aXor;
40 utils::B2DClipState aSubtract;
42 public:
43 void setUp() override
45 B2DRange aCenter(100, 100, -100, -100);
46 B2DRange aNorth(-10, -110, 10, -90);
47 B2DRange aWest(-110, -10, -90, 10);
48 B2DRange aSouth(-10, 110, 10, 90);
49 B2DRange aEast(110, -10, 90, 10);
51 aUnion1.unionRange(aCenter);
52 aUnion1.unionRange(aNorth);
53 aUnion1.unionRange(aWest);
54 aUnion1.unionRange(aSouth);
55 aUnion1.unionRange(aEast);
57 aUnion2.makeNull();
58 aUnion2.unionRange(aCenter);
59 aUnion2.unionRange(aNorth);
60 aUnion2.unionRange(aWest);
61 aUnion2.unionRange(aSouth);
62 aUnion2.unionRange(aEast);
64 aIntersect.intersectRange(aCenter);
65 aIntersect.intersectRange(aNorth);
66 aIntersect.intersectRange(aWest);
67 aIntersect.intersectRange(aSouth);
68 aIntersect.intersectRange(aEast);
70 aXor.makeNull();
71 aXor.xorRange(aCenter);
72 aXor.xorRange(aNorth);
73 aXor.xorRange(aWest);
74 aXor.xorRange(aSouth);
75 aXor.xorRange(aEast);
77 aSubtract.intersectRange(aCenter);
78 aSubtract.subtractRange(aNorth);
79 aSubtract.subtractRange(aWest);
80 aSubtract.subtractRange(aSouth);
81 aSubtract.subtractRange(aEast);
84 void verifyPoly(const char* sName, const char* sSvg, const utils::B2DClipState& toTest) const
86 #if OSL_DEBUG_LEVEL > 2
87 fprintf(stderr, "%s - svg:d=\"%s\"\n",
88 sName, OUStringToOString(
89 basegfx::utils::exportToSvgD(toTest.getClipPoly(), true, true, false),
90 RTL_TEXTENCODING_UTF8).getStr() );
91 #endif
93 B2DPolyPolygon aTmp1;
94 CPPUNIT_ASSERT_MESSAGE(sName,
95 utils::importFromSvgD(
96 aTmp1, OUString::createFromAscii(sSvg), false, nullptr));
98 const OUString aSvg=
99 utils::exportToSvgD(toTest.getClipPoly(), true, true, false);
100 B2DPolyPolygon aTmp2;
101 CPPUNIT_ASSERT_MESSAGE(sName,
102 utils::importFromSvgD(
103 aTmp2, aSvg, false, nullptr));
105 CPPUNIT_ASSERT_EQUAL_MESSAGE(
106 sName,
107 aTmp1, aTmp2);
110 void verifySimpleRange()
112 const char* const unionSvg="m100 10v90h-90v10h-20v-10h-90v-90h-10v-20h10v-90h90v-10h20v10h90v90h10v20z";
113 const char* const intersectSvg="m-100 10v-20h10v20zm90 90v-10h20v10zm0-190v-10h20v10zm100 100v-20h10v20z";
114 const char* const xorSvg="m-100 10h10v-20h-10zm90 90h20v-10h-20zm0-190h20v-10h-20zm100 100h10v-20h-10z"
115 "m10 0v90h-90v10h-20v-10h-90v-90h-10v-20h10v-90h90v-10h20v10h90v90h10v20z";
116 const char* const subtractSvg="m-90 10v-20h-10v-90h90v10h20v-10h90v90h-10v20h10v90h-90v-10h-20v10h-90v-90z";
118 CPPUNIT_ASSERT_MESSAGE("cleared clip stays empty under union operation",
119 aUnion1.isCleared());
120 verifyPoly("union", unionSvg, aUnion2);
121 verifyPoly("intersect", intersectSvg, aIntersect);
122 verifyPoly("xor", xorSvg, aXor);
123 verifyPoly("subtract", subtractSvg, aSubtract);
126 void verifyMixedClips()
128 utils::B2DClipState aMixedClip;
130 static constexpr OUString unionSvg=u"m100 10v90h-90v10h-20v-10h-90v-90h-10v-20h10v-90h90v-10h20v10h90v90h10v20z"_ustr;
132 B2DPolyPolygon aTmp1;
133 CPPUNIT_ASSERT(utils::importFromSvgD(aTmp1, unionSvg, false, nullptr));
135 aMixedClip.intersectPolyPolygon(aTmp1);
136 aMixedClip.subtractRange(B2DRange(-20,-150,20,0));
137 aMixedClip.subtractRange(B2DRange(-150,-20,0,20));
138 aMixedClip.xorRange(B2DRange(-150,-150,150,150));
140 const char* const mixedClipSvg="m0 0v20h-100v80h90v10h20v-10h90v-90h10v-20h-10v-90h-80v100zm-20-20v-80h-80v80zm-130 170v-300h300v300z";
141 verifyPoly("mixed clip", mixedClipSvg, aMixedClip);
144 CPPUNIT_TEST_SUITE(clipstate);
145 CPPUNIT_TEST(verifySimpleRange);
146 CPPUNIT_TEST(verifyMixedClips);
147 CPPUNIT_TEST_SUITE_END();
150 CPPUNIT_TEST_SUITE_REGISTRATION(basegfx2d::clipstate);
151 } // namespace basegfx2d
153 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */