Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
segment_test.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
42#include <gtest/gtest.h>
43#include <point.H>
44#include <cmath>
45
46using namespace std;
47
48// =============================================================================
49// Test Fixture
50// =============================================================================
51
52class SegmentTest : public ::testing::Test
53{
54protected:
55 // Common test points
56 Point origin{0, 0};
57 Point p1{10, 0};
58 Point p2{0, 10};
59 Point p3{10, 10};
60 Point p4{5, 5};
61
62 // Helper to compare Geom_Number with tolerance
63 static bool near_equal(const Geom_Number& a, const Geom_Number& b,
64 double epsilon = 1e-9)
65 {
66 return std::abs(geom_number_to_double(a - b)) < epsilon;
67 }
68
69 static bool near_equal(double a, double b, double epsilon = 1e-9)
70 {
71 return std::abs(a - b) < epsilon;
72 }
73};
74
75// =============================================================================
76// Constructor Tests
77// =============================================================================
78
79TEST_F(SegmentTest, DefaultConstructor)
80{
81 Segment s;
82 // Default constructed segment has uninitialized points
83 // Just verify it doesn't crash
84 SUCCEED();
85}
86
87TEST_F(SegmentTest, TwoPointConstructor)
88{
89 Segment s(origin, p3);
90 EXPECT_EQ(s.get_src_point(), origin);
91 EXPECT_EQ(s.get_tgt_point(), p3);
92}
93
94TEST_F(SegmentTest, CopyConstructor)
95{
96 Segment s1(origin, p3);
97 Segment s2(s1);
98 EXPECT_EQ(s2.get_src_point(), s1.get_src_point());
99 EXPECT_EQ(s2.get_tgt_point(), s1.get_tgt_point());
100}
101
102TEST_F(SegmentTest, SlopeLengthConstructor)
103{
104 // Segment from origin with slope 1 and length sqrt(2)
105 Segment s(origin, Geom_Number(1), Geom_Number(std::sqrt(2.0)));
106 // Target should be approximately (1, 1)
107 EXPECT_TRUE(near_equal(s.get_tgt_point().get_x(), 1, 0.01));
108 EXPECT_TRUE(near_equal(s.get_tgt_point().get_y(), 1, 0.01));
109}
110
111// =============================================================================
112// Endpoint Access Tests
113// =============================================================================
114
115TEST_F(SegmentTest, GetEndpoints)
116{
117 Segment s(origin, p3);
118 EXPECT_EQ(s.get_src_point(), origin);
119 EXPECT_EQ(s.get_tgt_point(), p3);
120}
121
122TEST_F(SegmentTest, HighestPoint)
123{
124 Segment s1(origin, p2); // (0,0) to (0,10)
125 EXPECT_EQ(s1.highest_point(), p2);
126
127 Segment s2(p2, origin); // reverse order
128 EXPECT_EQ(s2.highest_point(), p2);
129}
130
131TEST_F(SegmentTest, LowestPoint)
132{
133 Segment s1(origin, p2); // (0,0) to (0,10)
134 EXPECT_EQ(s1.lowest_point(), origin);
135
136 Segment s2(p2, origin); // reverse order
137 EXPECT_EQ(s2.lowest_point(), origin);
138}
139
140TEST_F(SegmentTest, LeftmostPoint)
141{
142 Segment s1(origin, p1); // (0,0) to (10,0)
143 EXPECT_EQ(s1.leftmost_point(), origin);
144
145 Segment s2(p1, origin); // reverse order
146 EXPECT_EQ(s2.leftmost_point(), origin);
147}
148
149TEST_F(SegmentTest, RightmostPoint)
150{
151 Segment s1(origin, p1); // (0,0) to (10,0)
152 EXPECT_EQ(s1.rightmost_point(), p1);
153
154 Segment s2(p1, origin); // reverse order
155 EXPECT_EQ(s2.rightmost_point(), p1);
156}
157
158// =============================================================================
159// Geometric Properties Tests
160// =============================================================================
161
163{
164 Segment horizontal(origin, p1); // length 10
165 EXPECT_TRUE(near_equal(horizontal.size(), 10));
166
167 Segment vertical(origin, p2); // length 10
168 EXPECT_TRUE(near_equal(vertical.size(), 10));
169
170 Segment diagonal(origin, p3); // length sqrt(200) ≈ 14.14
171 EXPECT_TRUE(near_equal(diagonal.size(), std::sqrt(200.0), 0.01));
172}
173
175{
176 Segment s(origin, p3); // (0,0) to (10,10)
177 Point mid = s.mid_point();
178 EXPECT_TRUE(near_equal(mid.get_x(), 5));
179 EXPECT_TRUE(near_equal(mid.get_y(), 5));
180}
181
182TEST_F(SegmentTest, MidPointNegative)
183{
184 Point n1(-10, -10);
185 Point n2(10, 10);
186 Segment s(n1, n2);
187 Point mid = s.mid_point();
188 EXPECT_TRUE(near_equal(mid.get_x(), 0));
189 EXPECT_TRUE(near_equal(mid.get_y(), 0));
190}
191
193{
194 Segment s45(origin, p3); // 45 degrees, slope = 1
195 EXPECT_TRUE(near_equal(s45.slope(), 1.0));
196
197 Segment horizontal(origin, p1); // slope = 0
198 EXPECT_TRUE(near_equal(horizontal.slope(), 0.0));
199
200 Point neg(-10, 10);
201 Segment negative(origin, neg); // slope = -1
202 EXPECT_TRUE(near_equal(negative.slope(), -1.0));
203}
204
205TEST_F(SegmentTest, SlopeVertical)
206{
207 Segment vertical(origin, p2); // vertical segment
208 // Vertical segments return max/min double for slope
209 double s = vertical.slope();
210 EXPECT_TRUE(s > 1e10 || s < -1e10);
211}
212
213// =============================================================================
214// Equality Tests
215// =============================================================================
216
217TEST_F(SegmentTest, EqualitySameOrder)
218{
219 Segment s1(origin, p3);
220 Segment s2(origin, p3);
221 EXPECT_TRUE(s1 == s2);
222 EXPECT_FALSE(s1 != s2);
223}
224
225TEST_F(SegmentTest, EqualityReversedOrder)
226{
227 Segment s1(origin, p3);
228 Segment s2(p3, origin);
229 // Note: In point.H Segment, equality checks exact src/tgt match
230 EXPECT_FALSE(s1 == s2); // Different src/tgt
231}
232
233TEST_F(SegmentTest, InequalityDifferentEndpoints)
234{
235 Segment s1(origin, p1);
236 Segment s2(origin, p2);
237 EXPECT_FALSE(s1 == s2);
238 EXPECT_TRUE(s1 != s2);
239}
240
241// =============================================================================
242// Colinearity Tests
243// =============================================================================
244
245TEST_F(SegmentTest, IsColinearWith)
246{
247 Segment s(origin, p3); // diagonal
248 EXPECT_TRUE(s.is_colinear_with(p4)); // (5,5) is on the line
249
250 Point off(5, 6); // not collinear
251 EXPECT_FALSE(s.is_colinear_with(off));
252}
253
254// =============================================================================
255// Left/Right Tests
256// =============================================================================
257
258TEST_F(SegmentTest, IsToLeftFrom)
259{
260 Segment s(origin, p1); // horizontal at y=0
261 Point above(5, 5);
262 Point below(5, -5);
263
264 // Segment is to the left of points above it (depending on orientation)
265 // This depends on the specific implementation
266 (void)s.is_to_left_from(above);
267 (void)s.is_to_right_from(below);
268
269 // Just verify the methods work without crashing
270 SUCCEED();
271}
272
273// =============================================================================
274// Angle Tests
275// =============================================================================
276
277TEST_F(SegmentTest, CounterclockwiseAngle)
278{
279 Segment s(origin, p1); // along x-axis
280 double angle = s.counterclockwise_angle();
281 // Should be close to 0 or 2*PI
282 EXPECT_TRUE(near_equal(angle, 0.0, 0.1) || near_equal(angle, 2*PI, 0.1));
283}
284
285TEST_F(SegmentTest, CounterclockwiseAngleWith)
286{
287 Segment s1(origin, p1); // along x-axis
288 Segment s2(origin, p2); // along y-axis
289
290 double angle = s1.counterclockwise_angle_with(s2);
291 // 90 degrees counterclockwise from x to y
292 EXPECT_TRUE(near_equal(angle, PI_2, 0.1) ||
293 near_equal(angle, 2*PI - PI_2, 0.1));
294}
295
296// =============================================================================
297// Parallel Segment Tests
298// =============================================================================
299
300TEST_F(SegmentTest, ParallelSegmentConstructor)
301{
302 Segment original(origin, p1); // horizontal at y=0
303 Segment parallel(original, Geom_Number(5)); // 5 units away
304
305 // The parallel segment should have the same length
306 EXPECT_TRUE(near_equal(parallel.size(), original.size(), 0.01));
307
308 // The parallel segment should have a different y coordinate
309 // (shifted by 5 units perpendicular to the original)
310}
311
312// =============================================================================
313// Edge Cases
314// =============================================================================
315
316TEST_F(SegmentTest, NegativeCoordinates)
317{
318 Point n1(-10, -10);
319 Point n2(-5, -5);
320 Segment s(n1, n2);
321
322 EXPECT_TRUE(near_equal(s.size(), std::sqrt(50.0), 0.01));
323 Point mid = s.mid_point();
324 EXPECT_TRUE(near_equal(mid.get_x(), -7.5, 0.1));
325 EXPECT_TRUE(near_equal(mid.get_y(), -7.5, 0.1));
326}
327
328TEST_F(SegmentTest, DegenerateSegment)
329{
330 Point same(5, 5);
331 Segment s(same, same);
332 EXPECT_TRUE(near_equal(s.size(), 0));
333}
334
335TEST_F(SegmentTest, LargeCoordinates)
336{
337 Point a(1000000, 1000000);
338 Point b(1000010, 1000010);
339 Segment s(a, b);
340
341 EXPECT_TRUE(near_equal(s.size(), std::sqrt(200.0), 0.01));
342}
343
344int main(int argc, char **argv)
345{
346 ::testing::InitGoogleTest(&argc, argv);
347 return RUN_ALL_TESTS();
348}
int main()
Rectangular point in the plane.
Definition point.H:156
const Geom_Number & get_y() const
Returns y value.
Definition point.H:226
const Geom_Number & get_x() const
Returns x value.
Definition point.H:221
static bool near_equal(double a, double b, double epsilon=1e-9)
static bool near_equal(const Geom_Number &a, const Geom_Number &b, double epsilon=1e-9)
Fundamental segment defined by two points.
Definition point.H:417
const Point & get_tgt_point() const
Definition point.H:470
bool is_colinear_with(const Point &p) const
Return true if p is colinear with this segment.
Definition point.H:584
double counterclockwise_angle_with(const Segment &s) const
Compute the counterclockwise wise angle respect another segment s.
Definition point.H:550
double counterclockwise_angle() const
Compute the counterclockwise angle of this respect x-axis.
Definition point.H:571
const Point & rightmost_point() const
Definition point.H:463
Geom_Number size() const
Return the Euclidean length of the segment (distance between endpoints).
Definition point.H:578
const Point & lowest_point() const
Definition point.H:453
bool is_to_left_from(const Point &p) const
Return true if this segment is to the left of point p.
Definition point.H:590
Point mid_point() const
Return the midpoint of this segment.
Definition point.H:602
const Point & highest_point() const
Definition point.H:448
bool is_to_right_from(const Point &p) const
Return true if this segment is to the right of point p.
Definition point.H:596
const Point & leftmost_point() const
Definition point.H:458
const Point & get_src_point() const
Definition point.H:468
double slope() const
Definition point.H:544
STL namespace.
2D point and geometric utilities.
mpq_class Geom_Number
Numeric type used by the geometry module.
Definition point.H:68
constexpr double PI
Definition point.H:85
constexpr double PI_2
Definition point.H:86
double geom_number_to_double(const Geom_Number &n)
Definition point.H:70
TEST_F(SegmentTest, DefaultConstructor)