KiCad PCB EDA Suite
test_segment.cpp File Reference

Go to the source code of this file.

Classes

struct  SEG_SEG_DISTANCE_CASE
 
struct  SEG_VECTOR_DISTANCE_CASE
 
struct  SEG_SEG_COLLIDE_CASE
 Test cases for collisions (with clearance, for no clearance, it's just a SEG_SEG_DISTANCE_CASE of 0) More...
 
struct  SEG_SEG_BOOLEAN_CASE
 Struct to hold general cases for collinearity, paralellism and perpendicularity. More...
 
struct  SEG_VEC_CASE
 Struct to hold cases for operations with a SEG, and a VECTOR2I. More...
 

Functions

bool SegCollideCorrect (const SEG &aSegA, const SEG &aSegB, int aClearance, bool aExp)
 Predicate to check expected collision between two segments. More...
 
bool SegDistanceCorrect (const SEG &aSegA, const SEG &aSegB, int aExp)
 Predicate to check expected distance between two segments. More...
 
bool SegVecDistanceCorrect (const SEG &aSeg, const VECTOR2I &aVec, int aExp)
 Predicate to check expected distance between a segment and a point. More...
 
bool SegCollinearCorrect (const SEG &aSegA, const SEG &aSegB, bool aExp)
 Predicate to check expected collision between two segments. More...
 
bool SegParallelCorrect (const SEG &aSegA, const SEG &aSegB, bool aExp)
 Predicate to check expected parallelism between two segments. More...
 
bool SegPerpendicularCorrect (const SEG &aSegA, const SEG &aSegB, bool aExp)
 Predicate to check expected perpendicularity between two segments. More...
 
 BOOST_AUTO_TEST_CASE (EndpointCtorMod)
 Checks whether the construction of a segment referencing external points works and that the endpoints can be modified as normal points. More...
 
 BOOST_AUTO_TEST_CASE (SegSegDistance)
 
 BOOST_AUTO_TEST_CASE (SegVecDistance)
 
 BOOST_AUTO_TEST_CASE (SegSegCollision)
 
 BOOST_AUTO_TEST_CASE (SegSegCollinear)
 
 BOOST_AUTO_TEST_CASE (SegSegParallel)
 
 BOOST_AUTO_TEST_CASE (SegSegPerpendicular)
 
 BOOST_AUTO_TEST_CASE (SegCreateParallel)
 
 BOOST_AUTO_TEST_CASE (SegCreatePerpendicular)
 

Variables

static const std::vector< SEG_SEG_DISTANCE_CASEseg_seg_dist_cases
 
static const std::vector< SEG_VECTOR_DISTANCE_CASEseg_vec_dist_cases
 
static const std::vector< SEG_SEG_COLLIDE_CASEseg_seg_coll_cases
 
static const std::vector< SEG_SEG_BOOLEAN_CASEseg_vec_collinear_cases
 Test cases for collinearity. More...
 
static const std::vector< SEG_SEG_BOOLEAN_CASEseg_vec_parallel_cases
 Test cases for paralellism. More...
 
static const std::vector< SEG_SEG_BOOLEAN_CASEseg_vec_perpendicular_cases
 Test cases for perpendicularity. More...
 
static const std::vector< SEG_VEC_CASEsegment_and_point_cases
 Test cases to create segments passing through a point. More...
 

Function Documentation

◆ BOOST_AUTO_TEST_CASE() [1/9]

BOOST_AUTO_TEST_CASE ( EndpointCtorMod  )

Checks whether the construction of a segment referencing external points works and that the endpoints can be modified as normal points.

Definition at line 221 of file test_segment.cpp.

222 {
223  const VECTOR2I pointA{ 10, 20 };
224  const VECTOR2I pointB{ 100, 200 };
225 
226  // Build a segment referencing the previous points
227  SEG segment( pointA, pointB );
228 
229  BOOST_CHECK_EQUAL( pointA, VECTOR2I( 10, 20 ) );
230  BOOST_CHECK_EQUAL( pointB, VECTOR2I( 100, 200 ) );
231 
232  // Modify the ends of the segments
233  segment.A += VECTOR2I( 10, 10 );
234  segment.B += VECTOR2I( 100, 100 );
235 
236  // Check that the ends in segment are modified
237  BOOST_CHECK_EQUAL( segment.A, VECTOR2I( 20, 30 ) );
238  BOOST_CHECK_EQUAL( segment.B, VECTOR2I( 200, 300 ) );
239 }
Define a general 2D-vector/point.
Definition: vector2d.h:61
VECTOR2< int > VECTOR2I
Definition: vector2d.h:623
Definition: seg.h:41

References SEG::A, and SEG::B.

◆ BOOST_AUTO_TEST_CASE() [2/9]

BOOST_AUTO_TEST_CASE ( SegSegDistance  )

Definition at line 292 of file test_segment.cpp.

293 {
294  for( const auto& c : seg_seg_dist_cases )
295  {
296  BOOST_TEST_CONTEXT( c.m_case_name )
297  {
298  BOOST_CHECK_PREDICATE( SegDistanceCorrect, ( c.m_seg_a )( c.m_seg_b )( c.m_exp_dist ) );
299  }
300  }
301 }
#define BOOST_TEST_CONTEXT(A)
static const std::vector< SEG_SEG_DISTANCE_CASE > seg_seg_dist_cases
bool SegDistanceCorrect(const SEG &aSegA, const SEG &aSegB, int aExp)
Predicate to check expected distance between two segments.

References BOOST_TEST_CONTEXT, seg_seg_dist_cases, and SegDistanceCorrect().

◆ BOOST_AUTO_TEST_CASE() [3/9]

BOOST_AUTO_TEST_CASE ( SegVecDistance  )

Definition at line 349 of file test_segment.cpp.

350 {
351  for( const auto& c : seg_vec_dist_cases )
352  {
353  BOOST_TEST_CONTEXT( c.m_case_name )
354  {
355  BOOST_CHECK_PREDICATE( SegVecDistanceCorrect, ( c.m_seg )( c.m_vec )( c.m_exp_dist ) );
356  }
357  }
358 }
bool SegVecDistanceCorrect(const SEG &aSeg, const VECTOR2I &aVec, int aExp)
Predicate to check expected distance between a segment and a point.
#define BOOST_TEST_CONTEXT(A)
static const std::vector< SEG_VECTOR_DISTANCE_CASE > seg_vec_dist_cases

References BOOST_TEST_CONTEXT, seg_vec_dist_cases, and SegVecDistanceCorrect().

◆ BOOST_AUTO_TEST_CASE() [4/9]

BOOST_AUTO_TEST_CASE ( SegSegCollision  )

Definition at line 416 of file test_segment.cpp.

417 {
418  for( const auto& c : seg_seg_coll_cases )
419  {
420  BOOST_TEST_CONTEXT( c.m_case_name )
421  {
422  BOOST_CHECK_PREDICATE( SegCollideCorrect,
423  ( c.m_seg_a )( c.m_seg_b )( c.m_clearance )( c.m_exp_coll ) );
424  }
425  }
426 }
bool SegCollideCorrect(const SEG &aSegA, const SEG &aSegB, int aClearance, bool aExp)
Predicate to check expected collision between two segments.
#define BOOST_TEST_CONTEXT(A)
static const std::vector< SEG_SEG_COLLIDE_CASE > seg_seg_coll_cases

References BOOST_TEST_CONTEXT, seg_seg_coll_cases, and SegCollideCorrect().

◆ BOOST_AUTO_TEST_CASE() [5/9]

BOOST_AUTO_TEST_CASE ( SegSegCollinear  )

Definition at line 479 of file test_segment.cpp.

480 {
481  for( const auto& c : seg_vec_collinear_cases )
482  {
483  BOOST_TEST_CONTEXT( c.m_case_name )
484  {
485  BOOST_CHECK_PREDICATE( SegCollinearCorrect,
486  ( c.m_seg_a )( c.m_seg_b )( c.m_exp_result ) );
487  }
488  }
489 }
static const std::vector< SEG_SEG_BOOLEAN_CASE > seg_vec_collinear_cases
Test cases for collinearity.
#define BOOST_TEST_CONTEXT(A)
bool SegCollinearCorrect(const SEG &aSegA, const SEG &aSegB, bool aExp)
Predicate to check expected collision between two segments.

References BOOST_TEST_CONTEXT, seg_vec_collinear_cases, and SegCollinearCorrect().

◆ BOOST_AUTO_TEST_CASE() [6/9]

BOOST_AUTO_TEST_CASE ( SegSegParallel  )

Definition at line 531 of file test_segment.cpp.

532 {
533  for( const auto& c : seg_vec_parallel_cases )
534  {
535  BOOST_TEST_CONTEXT( c.m_case_name )
536  {
537  BOOST_CHECK_PREDICATE( SegParallelCorrect,
538  ( c.m_seg_a )( c.m_seg_b )( c.m_exp_result ) );
539  }
540  }
541 }
static const std::vector< SEG_SEG_BOOLEAN_CASE > seg_vec_parallel_cases
Test cases for paralellism.
#define BOOST_TEST_CONTEXT(A)
bool SegParallelCorrect(const SEG &aSegA, const SEG &aSegB, bool aExp)
Predicate to check expected parallelism between two segments.

References BOOST_TEST_CONTEXT, seg_vec_parallel_cases, and SegParallelCorrect().

◆ BOOST_AUTO_TEST_CASE() [7/9]

BOOST_AUTO_TEST_CASE ( SegSegPerpendicular  )

Definition at line 607 of file test_segment.cpp.

608 {
609  for( const auto& c : seg_vec_perpendicular_cases )
610  {
611  BOOST_TEST_CONTEXT( c.m_case_name )
612  {
613  BOOST_CHECK_PREDICATE( SegPerpendicularCorrect,
614  ( c.m_seg_a )( c.m_seg_b )( c.m_exp_result ) );
615  }
616  }
617 }
static const std::vector< SEG_SEG_BOOLEAN_CASE > seg_vec_perpendicular_cases
Test cases for perpendicularity.
bool SegPerpendicularCorrect(const SEG &aSegA, const SEG &aSegB, bool aExp)
Predicate to check expected perpendicularity between two segments.
#define BOOST_TEST_CONTEXT(A)

References BOOST_TEST_CONTEXT, seg_vec_perpendicular_cases, and SegPerpendicularCorrect().

◆ BOOST_AUTO_TEST_CASE() [8/9]

BOOST_AUTO_TEST_CASE ( SegCreateParallel  )

Definition at line 670 of file test_segment.cpp.

671 {
672  for( const auto& c : segment_and_point_cases )
673  {
674  BOOST_TEST_CONTEXT( c.m_case_name )
675  {
676  SEG perpendicular = c.m_seg.ParallelSeg( c.m_vec );
677 
678  BOOST_CHECK_PREDICATE( SegParallelCorrect, ( perpendicular )( c.m_seg )( true ) );
679  BOOST_CHECK_PREDICATE( SegVecDistanceCorrect, ( perpendicular )( c.m_vec )( 0 ) );
680  }
681  }
682 }
SEG ParallelSeg(const VECTOR2I &aP) const
Compute a segment parallel to this one, passing through point aP.
Definition: seg.cpp:157
bool SegVecDistanceCorrect(const SEG &aSeg, const VECTOR2I &aVec, int aExp)
Predicate to check expected distance between a segment and a point.
static const std::vector< SEG_VEC_CASE > segment_and_point_cases
Test cases to create segments passing through a point.
#define BOOST_TEST_CONTEXT(A)
Definition: seg.h:41
bool SegParallelCorrect(const SEG &aSegA, const SEG &aSegB, bool aExp)
Predicate to check expected parallelism between two segments.

References BOOST_TEST_CONTEXT, SEG::ParallelSeg(), segment_and_point_cases, SegParallelCorrect(), and SegVecDistanceCorrect().

◆ BOOST_AUTO_TEST_CASE() [9/9]

BOOST_AUTO_TEST_CASE ( SegCreatePerpendicular  )

Definition at line 684 of file test_segment.cpp.

685 {
686  for( const auto& c : segment_and_point_cases )
687  {
688  BOOST_TEST_CONTEXT( c.m_case_name )
689  {
690  SEG perpendicular = c.m_seg.PerpendicularSeg( c.m_vec );
691 
692  BOOST_CHECK_PREDICATE( SegPerpendicularCorrect, ( perpendicular )( c.m_seg )( true ) );
693  BOOST_CHECK_PREDICATE( SegVecDistanceCorrect, ( perpendicular )( c.m_vec )( 0 ) );
694  }
695  }
696 }
SEG PerpendicularSeg(const VECTOR2I &aP) const
Compute a segment perpendicular to this one, passing through point aP.
Definition: seg.cpp:148
bool SegVecDistanceCorrect(const SEG &aSeg, const VECTOR2I &aVec, int aExp)
Predicate to check expected distance between a segment and a point.
static const std::vector< SEG_VEC_CASE > segment_and_point_cases
Test cases to create segments passing through a point.
bool SegPerpendicularCorrect(const SEG &aSegA, const SEG &aSegB, bool aExp)
Predicate to check expected perpendicularity between two segments.
#define BOOST_TEST_CONTEXT(A)
Definition: seg.h:41

References BOOST_TEST_CONTEXT, SEG::PerpendicularSeg(), segment_and_point_cases, SegPerpendicularCorrect(), and SegVecDistanceCorrect().

◆ SegCollideCorrect()

bool SegCollideCorrect ( const SEG aSegA,
const SEG aSegB,
int  aClearance,
bool  aExp 
)

Predicate to check expected collision between two segments.

Parameters
aSegAthe first SEG
aSegBthe second SEG
aClearancethe collision clearance
aExpexpected collision
Returns
does the distance calculated agree?

Definition at line 38 of file test_segment.cpp.

39 {
40  const bool AtoB = aSegA.Collide( aSegB, aClearance );
41  const bool BtoA = aSegB.Collide( aSegA, aClearance );
42 
43  const bool ok = ( AtoB == aExp ) && ( BtoA == aExp );
44 
45  if( AtoB != BtoA )
46  {
47  std::stringstream ss;
48  ss << "Segment collision is not the same in both directions: expected " << aExp << ", got "
49  << AtoB << " & " << BtoA;
50  BOOST_TEST_INFO( ss.str() );
51  }
52  else if( !ok )
53  {
54  std::stringstream ss;
55  ss << "Collision incorrect: expected " << aExp << ", got " << AtoB;
56  BOOST_TEST_INFO( ss.str() );
57  }
58 
59  return ok;
60 }
bool Collide(const SEG &aSeg, int aClearance, int *aActual=nullptr) const
Definition: seg.cpp:172
#define BOOST_TEST_INFO(A)
If HAVE_EXPECTED_FAILURES is defined, this means that boost::unit_test::expected_failures is availabl...

References BOOST_TEST_INFO, and SEG::Collide().

Referenced by BOOST_AUTO_TEST_CASE(), and SegDistanceCorrect().

◆ SegCollinearCorrect()

bool SegCollinearCorrect ( const SEG aSegA,
const SEG aSegB,
bool  aExp 
)

Predicate to check expected collision between two segments.

Parameters
aSegAthe first SEG
sSegBthe second SEG
aExpexpected collinearity
Returns
does the collinearity calculated agree?

Definition at line 127 of file test_segment.cpp.

128 {
129  const bool AtoB = aSegA.Collinear( aSegB );
130  const bool BtoA = aSegB.Collinear( aSegA );
131 
132  const bool ok = ( AtoB == aExp ) && ( BtoA == aExp );
133 
134  if( AtoB != BtoA )
135  {
136  std::stringstream ss;
137  ss << "Segment collinearity is not the same in both directions: expected " << aExp
138  << ", got " << AtoB << " & " << BtoA;
139  BOOST_TEST_INFO( ss.str() );
140  }
141  else if( !ok )
142  {
143  std::stringstream ss;
144  ss << "Collinearity incorrect: expected " << aExp << ", got " << AtoB;
145  BOOST_TEST_INFO( ss.str() );
146  }
147 
148  return ok;
149 }
bool Collinear(const SEG &aSeg) const
Check if segment aSeg lies on the same line as (this).
Definition: seg.h:273
#define BOOST_TEST_INFO(A)
If HAVE_EXPECTED_FAILURES is defined, this means that boost::unit_test::expected_failures is availabl...

References BOOST_TEST_INFO, and SEG::Collinear().

Referenced by BOOST_AUTO_TEST_CASE().

◆ SegDistanceCorrect()

bool SegDistanceCorrect ( const SEG aSegA,
const SEG aSegB,
int  aExp 
)

Predicate to check expected distance between two segments.

Parameters
aSegAthe first SEG
aSegBthe second SEG
aExpexpected distance
Returns
does the distance calculated agree?

Definition at line 70 of file test_segment.cpp.

71 {
72  const int AtoB = aSegA.Distance( aSegB );
73  const int BtoA = aSegB.Distance( aSegA );
74 
75  bool ok = ( AtoB == aExp ) && ( BtoA == aExp );
76 
77  if( AtoB != BtoA )
78  {
79  std::stringstream ss;
80  ss << "Segment distance is not the same in both directions: expected " << aExp << ", got "
81  << AtoB << " & " << BtoA;
82  BOOST_TEST_INFO( ss.str() );
83  }
84  else if( !ok )
85  {
86  std::stringstream ss;
87  ss << "Distance incorrect: expected " << aExp << ", got " << AtoB;
88  BOOST_TEST_INFO( ss.str() );
89  }
90 
91  // Sanity check: the collision should be consistent with the distance
92  ok = ok && SegCollideCorrect( aSegA, aSegB, 0, aExp == 0 );
93 
94  return ok;
95 }
int Distance(const SEG &aSeg) const
Compute minimum Euclidean distance to segment aSeg.
Definition: seg.h:239
bool SegCollideCorrect(const SEG &aSegA, const SEG &aSegB, int aClearance, bool aExp)
Predicate to check expected collision between two segments.
#define BOOST_TEST_INFO(A)
If HAVE_EXPECTED_FAILURES is defined, this means that boost::unit_test::expected_failures is availabl...

References BOOST_TEST_INFO, SEG::Distance(), and SegCollideCorrect().

Referenced by BOOST_AUTO_TEST_CASE().

◆ SegParallelCorrect()

bool SegParallelCorrect ( const SEG aSegA,
const SEG aSegB,
bool  aExp 
)

Predicate to check expected parallelism between two segments.

Parameters
aSegAthe first SEG
sSegBthe second SEG
aExpexpected parallelism: true = segments are parallel false = segments are not parallel
Returns
does the parallelism calculated agree?

Definition at line 159 of file test_segment.cpp.

160 {
161  const bool AtoB = aSegA.ApproxParallel( aSegB );
162  const bool BtoA = aSegB.ApproxParallel( aSegA );
163 
164  const bool ok = ( AtoB == aExp ) && ( BtoA == aExp );
165 
166  if( AtoB != BtoA )
167  {
168  std::stringstream ss;
169  ss << "Segment parallelism is not the same in both directions: expected " << aExp
170  << ", got AtoB: " << AtoB << " BtoA:" << BtoA;
171  BOOST_TEST_INFO( ss.str() );
172  }
173  else if( !ok )
174  {
175  std::stringstream ss;
176  ss << "Parallelism incorrect: expected " << aExp << ", got " << AtoB;
177  BOOST_TEST_INFO( ss.str() );
178  }
179 
180  return ok;
181 }
bool ApproxParallel(const SEG &aSeg) const
Definition: seg.h:295
#define BOOST_TEST_INFO(A)
If HAVE_EXPECTED_FAILURES is defined, this means that boost::unit_test::expected_failures is availabl...

References SEG::ApproxParallel(), and BOOST_TEST_INFO.

Referenced by BOOST_AUTO_TEST_CASE().

◆ SegPerpendicularCorrect()

bool SegPerpendicularCorrect ( const SEG aSegA,
const SEG aSegB,
bool  aExp 
)

Predicate to check expected perpendicularity between two segments.

Parameters
aSegAthe first SEG
sSegBthe second SEG
aExpexpected perpendicularity: true = segments are perpendicular false = segments are not perpendicular
Returns
does the perpendicularity calculated agree?

Definition at line 191 of file test_segment.cpp.

192 {
193  const bool AtoB = aSegA.ApproxPerpendicular( aSegB );
194  const bool BtoA = aSegB.ApproxPerpendicular( aSegA );
195 
196  const bool ok = ( AtoB == aExp ) && ( BtoA == aExp );
197 
198  if( AtoB != BtoA )
199  {
200  std::stringstream ss;
201  ss << "Segment perpendicularity is not the same in both directions: expected " << aExp
202  << ", got AtoB: " << AtoB << " BtoA:" << BtoA;
203  BOOST_TEST_INFO( ss.str() );
204  }
205  else if( !ok )
206  {
207  std::stringstream ss;
208  ss << "Perpendicularity incorrect: expected " << aExp << ", got " << AtoB;
209  BOOST_TEST_INFO( ss.str() );
210  }
211 
212  return ok;
213 }
bool ApproxPerpendicular(const SEG &aSeg) const
Definition: seg.h:306
#define BOOST_TEST_INFO(A)
If HAVE_EXPECTED_FAILURES is defined, this means that boost::unit_test::expected_failures is availabl...

References SEG::ApproxPerpendicular(), and BOOST_TEST_INFO.

Referenced by BOOST_AUTO_TEST_CASE().

◆ SegVecDistanceCorrect()

bool SegVecDistanceCorrect ( const SEG aSeg,
const VECTOR2I aVec,
int  aExp 
)

Predicate to check expected distance between a segment and a point.

Parameters
aSegAthe segment
aVecthe vector (point)
aExpexpected distance
Returns
does the distance calculated agree?

Definition at line 104 of file test_segment.cpp.

105 {
106  const int dist = aSeg.Distance( aVec );
107 
108  bool ok = ( dist == aExp );
109 
110  if( !ok )
111  {
112  std::stringstream ss;
113  ss << "Distance incorrect: expected " << aExp << ", got " << dist;
114  BOOST_TEST_INFO( ss.str() );
115  }
116 
117  return ok;
118 }
int Distance(const SEG &aSeg) const
Compute minimum Euclidean distance to segment aSeg.
Definition: seg.h:239
#define BOOST_TEST_INFO(A)
If HAVE_EXPECTED_FAILURES is defined, this means that boost::unit_test::expected_failures is availabl...

References BOOST_TEST_INFO, and SEG::Distance().

Referenced by BOOST_AUTO_TEST_CASE().

Variable Documentation

◆ seg_seg_coll_cases

const std::vector<SEG_SEG_COLLIDE_CASE> seg_seg_coll_cases
static

Definition at line 376 of file test_segment.cpp.

Referenced by BOOST_AUTO_TEST_CASE().

◆ seg_seg_dist_cases

const std::vector<SEG_SEG_DISTANCE_CASE> seg_seg_dist_cases
static

Definition at line 251 of file test_segment.cpp.

Referenced by BOOST_AUTO_TEST_CASE().

◆ seg_vec_collinear_cases

const std::vector<SEG_SEG_BOOLEAN_CASE> seg_vec_collinear_cases
static

Test cases for collinearity.

Definition at line 444 of file test_segment.cpp.

Referenced by BOOST_AUTO_TEST_CASE().

◆ seg_vec_dist_cases

const std::vector<SEG_VECTOR_DISTANCE_CASE> seg_vec_dist_cases
static

Definition at line 314 of file test_segment.cpp.

Referenced by BOOST_AUTO_TEST_CASE().

◆ seg_vec_parallel_cases

const std::vector<SEG_SEG_BOOLEAN_CASE> seg_vec_parallel_cases
static

Test cases for paralellism.

Definition at line 496 of file test_segment.cpp.

Referenced by BOOST_AUTO_TEST_CASE().

◆ seg_vec_perpendicular_cases

const std::vector<SEG_SEG_BOOLEAN_CASE> seg_vec_perpendicular_cases
static

Test cases for perpendicularity.

Definition at line 548 of file test_segment.cpp.

Referenced by BOOST_AUTO_TEST_CASE().

◆ segment_and_point_cases

const std::vector<SEG_VEC_CASE> segment_and_point_cases
static

Test cases to create segments passing through a point.

Definition at line 635 of file test_segment.cpp.

Referenced by BOOST_AUTO_TEST_CASE().