File: testGeometry.cxx

package info (click to toggle)
codequery 1.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,860 kB
  • sloc: cpp: 151,420; xml: 16,576; python: 5,602; ansic: 5,487; makefile: 559; perl: 496; ruby: 209; sql: 194; sh: 106; php: 53; vhdl: 51; erlang: 47; objc: 22; lisp: 18; cobol: 18; modula3: 17; asm: 14; fortran: 12; ml: 11; tcl: 6
file content (246 lines) | stat: -rw-r--r-- 7,563 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/** @file testGeometry.cxx
 ** Unit Tests for Scintilla internal data structures
 **/

#include <cstdint>

#include "Geometry.h"

#include "catch.hpp"

using namespace Scintilla;
using namespace Scintilla::Internal;

// Test Geometry.

TEST_CASE("Point") {

	SECTION("Point") {
		constexpr Point pt(1.0, 2.0);
		REQUIRE(pt.x == 1.0);
		REQUIRE(pt.y == 2.0);

		constexpr Point pti = Point::FromInts(1, 2);
		REQUIRE(pt == pti);

		constexpr Point pt2 = pt + pt;
		REQUIRE(pt != pt2);

		constexpr Point ptBack = pt2 - pt;
		REQUIRE(pt == ptBack);
	}

}

TEST_CASE("Interval") {

	SECTION("Interval") {
		constexpr Interval interval { 1.0, 2.0 };
		REQUIRE(interval.left == 1.0);
		REQUIRE(interval.right == 2.0);
		REQUIRE(interval.Width() == 1.0);
		REQUIRE(!interval.Empty());

		constexpr Interval empty { 4.0, 4.0 };
		REQUIRE(empty.Empty());
		REQUIRE(empty.Width() == 0.0);
		REQUIRE(!(interval == empty));
		REQUIRE(!(interval.Intersects(empty)));

		constexpr Interval inside { 1.7, 1.8 };
		REQUIRE(interval.Intersects(inside));
		constexpr Interval straddles { 1.7, 2.8 };
		REQUIRE(interval.Intersects(straddles));
	}

	SECTION("Interval") {
		constexpr Interval interval1{ 1.0, 3.0 };
		constexpr Interval interval2{ 2.0, 4.0 };
		REQUIRE(Intersection(interval1, interval2) == Interval{ 2.0, 3.0 });
	}
}

TEST_CASE("PRectangle") {

	SECTION("PRectangle") {
		constexpr PRectangle rc(1.0, 2.0, 3.0, 4.0);
		REQUIRE(rc.left == 1.0);
		REQUIRE(rc.top == 2.0);
		REQUIRE(rc.right == 3.0);
		REQUIRE(rc.bottom == 4.0);
		REQUIRE(rc.Width() == 2.0);
		REQUIRE(rc.Height() == 2.0);
		REQUIRE(!rc.Empty());

		constexpr PRectangle rci = PRectangle::FromInts(1, 2, 3, 4);
		REQUIRE(rc == rci);

		constexpr PRectangle rcEmpty;
		REQUIRE(rcEmpty.Empty());
	}

	SECTION("Contains") {
		constexpr PRectangle rc(1.0, 2.0, 3.0, 4.0);
		constexpr Point pt(1.5, 2.5);
		REQUIRE(rc.Contains(pt));
		REQUIRE(rc.ContainsWholePixel(pt));
		constexpr Point ptNearEdge(2.9, 2.5);
		REQUIRE(rc.Contains(ptNearEdge));
		REQUIRE(!rc.ContainsWholePixel(ptNearEdge));
		constexpr Point ptOutside(1.5, 20.5);
		REQUIRE(!rc.Contains(ptOutside));
		REQUIRE(!rc.ContainsWholePixel(ptOutside));

		constexpr PRectangle rcInside(1.5, 2.0, 2.5, 4.0);
		REQUIRE(rc.Contains(rcInside));
		REQUIRE(rc.Intersects(rcInside));

		constexpr PRectangle rcIntersects(1.5, 2.0, 3.5, 4.0);
		REQUIRE(!rc.Contains(rcIntersects));
		REQUIRE(rc.Intersects(rcIntersects));

		constexpr PRectangle rcSeparate(11.0, 12.0, 13.0, 14.0);
		REQUIRE(!rc.Contains(rcSeparate));
		REQUIRE(!rc.Intersects(rcSeparate));

		constexpr Point ptCentre = rc.Centre();
		REQUIRE(ptCentre == Point(2.0, 3.0));
	}

	SECTION("Move") {
		PRectangle rc(1.0, 2.0, 3.0, 4.0);
		rc.Move(1.0, 10.0);
		REQUIRE(rc == PRectangle(2.0, 12.0, 4.0, 14.0));
	}

	SECTION("Inset") {
		constexpr PRectangle rc(1.0, 2.0, 3.0, 4.0);
		constexpr PRectangle rcInset = rc.Inset(0.5);
		REQUIRE(rcInset == PRectangle(1.5, 2.5, 2.5, 3.5));

		constexpr PRectangle rcInsetPt = rc.Inset(Point(0.25, 0.5));
		REQUIRE(rcInsetPt == PRectangle(1.25, 2.5, 2.75, 3.5));
	}

	SECTION("Clamp") {
		constexpr PRectangle rc(1.0, 2.0, 3.0, 4.0);

		const PRectangle cutBottom = Clamp(rc, Edge::bottom, 3.5);
		REQUIRE(cutBottom == PRectangle(1.0, 2.0, 3.0, 3.5));

		const PRectangle justBottom = Side(rc, Edge::bottom, 0.5);
		REQUIRE(justBottom == PRectangle(1.0, 3.5, 3.0, 4.0));

		constexpr PRectangle rcInset = rc.Inset(0.5);
		REQUIRE(rcInset == PRectangle(1.5, 2.5, 2.5, 3.5));

		constexpr PRectangle rcInsetPt = rc.Inset(Point(0.25, 0.5));
		REQUIRE(rcInsetPt == PRectangle(1.25, 2.5, 2.75, 3.5));

		const Interval bounds = HorizontalBounds(rcInsetPt);
		REQUIRE(bounds == Interval{ 1.25, 2.75 });

		const PRectangle applyBounds = Intersection(rc, bounds);
		REQUIRE(applyBounds == PRectangle(1.25, 2.0, 2.75, 4.0));
	}

	SECTION("PixelAlign") {
		// Whole pixels
		REQUIRE(PixelAlign(1.0, 1) == 1.0);
		REQUIRE(PixelAlignFloor(1.0, 1) == 1.0);
		REQUIRE(PixelAlignCeil(1.0, 1) == 1.0);

		REQUIRE(PixelAlign(1.25, 1) == 1.0);
		REQUIRE(PixelAlignFloor(1.25, 1) == 1.0);
		REQUIRE(PixelAlignCeil(1.25, 1) == 2.0);

		REQUIRE(PixelAlign(1.5, 1) == 2.0);
		REQUIRE(PixelAlignFloor(1.5, 1) == 1.0);
		REQUIRE(PixelAlignCeil(1.5, 1) == 2.0);

		REQUIRE(PixelAlign(1.75, 1) == 2.0);
		REQUIRE(PixelAlignFloor(1.75, 1) == 1.0);
		REQUIRE(PixelAlignCeil(1.75, 1) == 2.0);

		REQUIRE(PixelAlign(Point(1.75, 1.25), 1) == Point(2.0, 1.0));
		REQUIRE(PixelAlign(Point(1.5, 1.0), 1) == Point(2.0, 1.0));

		// Half pixels
		REQUIRE(PixelAlign(1.0, 2) == 1.0);
		REQUIRE(PixelAlignFloor(1.0, 2) == 1.0);
		REQUIRE(PixelAlignCeil(1.0, 2) == 1.0);

		REQUIRE(PixelAlign(1.25, 2) == 1.5);
		REQUIRE(PixelAlignFloor(1.25, 2) == 1.0);
		REQUIRE(PixelAlignCeil(1.25, 2) == 1.5);

		REQUIRE(PixelAlign(1.5, 2) == 1.5);
		REQUIRE(PixelAlignFloor(1.5, 2) == 1.5);
		REQUIRE(PixelAlignCeil(1.5, 2) == 1.5);

		REQUIRE(PixelAlign(1.75, 2) == 2.0);
		REQUIRE(PixelAlignFloor(1.75, 2) == 1.5);
		REQUIRE(PixelAlignCeil(1.75, 2) == 2.0);

		REQUIRE(PixelAlign(Point(1.75, 1.25), 2) == Point(2.0, 1.5));
		REQUIRE(PixelAlign(Point(1.5, 1.0), 2) == Point(1.5, 1.0));

		// x->round, y->floored
		REQUIRE(PixelAlign(PRectangle(1.0, 1.25, 1.5, 1.75), 1) == PRectangle(1.0, 1.0, 2.0, 1.0));
		REQUIRE(PixelAlign(PRectangle(1.0, 1.25, 1.5, 1.75), 2) == PRectangle(1.0, 1.0, 1.5, 1.5));

		// x->outside(floor left, ceil right), y->floored
		REQUIRE(PixelAlignOutside(PRectangle(1.1, 1.25, 1.6, 1.75), 1) == PRectangle(1.0, 1.0, 2.0, 1.0));
		REQUIRE(PixelAlignOutside(PRectangle(1.1, 1.25, 1.6, 1.75), 2) == PRectangle(1.0, 1.0, 2.0, 1.5));
	}

}

TEST_CASE("ColourRGBA") {

	SECTION("ColourRGBA") {
		constexpr ColourRGBA colour(0x10203040);
		constexpr ColourRGBA colourFromRGB(0x40, 0x30, 0x20, 0x10);

		REQUIRE(colour == colourFromRGB);
		REQUIRE(colour.GetRed() == 0x40);
		REQUIRE(colour.GetGreen() == 0x30);
		REQUIRE(colour.GetBlue() == 0x20);
		REQUIRE(colour.GetAlpha() == 0x10);
		REQUIRE(!colour.IsOpaque());
		REQUIRE(colour.AsInteger() == 0x10203040);

		REQUIRE(ColourRGBA(colour, 0x80) == ColourRGBA(0x40, 0x30, 0x20, 0x80));
		REQUIRE(ColourRGBA::FromRGB(0x203040) == ColourRGBA(0x40, 0x30, 0x20, 0xff));
		REQUIRE(ColourRGBA::FromIpRGB(0x203040) == ColourRGBA(0x40, 0x30, 0x20, 0xff));

		constexpr ColourRGBA colour01(0x00ff00ff);
		REQUIRE(colour01.GetRedComponent() == 1.0);
		REQUIRE(colour01.GetGreenComponent() == 0.0);
		REQUIRE(colour01.GetBlueComponent() == 1.0);
		REQUIRE(colour01.GetAlphaComponent() == 0.0);

		// Opaque colours
		constexpr ColourRGBA colourRGB(0xff203040);
		REQUIRE(colourRGB.IsOpaque());
		constexpr ColourRGBA colourOpaque(0x40, 0x30, 0x20, 0xff);
		REQUIRE(colourRGB == colourOpaque);

		REQUIRE(colourRGB.OpaqueRGB() == 0x203040);
		REQUIRE(colourRGB.WithoutAlpha() == ColourRGBA(0x40, 0x30, 0x20, 0));
	}

	SECTION("Mixing") {
		constexpr ColourRGBA colourMin(0x10, 0x20, 0x30, 0x40);
		constexpr ColourRGBA colourMax(0x30, 0x60, 0x90, 0xC0);

		constexpr ColourRGBA colourMid(0x20, 0x40, 0x60, 0x80);
		REQUIRE(colourMin.MixedWith(colourMax) == colourMid);
		REQUIRE(colourMin.MixedWith(colourMax, 0.5) == colourMid);

		// 3/4 between min and max
		constexpr ColourRGBA colour75(0x28, 0x50, 0x78, 0xA0);
		REQUIRE(colourMin.MixedWith(colourMax, 0.75) == colour75);
	}

}