xmscore  1.0
functors.cpp
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
6 //------------------------------------------------------------------------------
7 
8 //----- Included files ---------------------------------------------------------
9 
11 
12 //----- Function definitions ---------------------------------------------------
13 
14 #if CXX_TEST
15 // TESTS
18 
19 #include <set>
22 
23 typedef std::set<xms::Pt3d, xms::ltPt3d> SET_WITH_LTPT3D;
24 
25 namespace // unnamed namespace
26 {
27 //------------------------------------------------------------------------------
29 //------------------------------------------------------------------------------
30 void iTestPassingMptFunctorTypes(SET_WITH_LTPT3D& a_set)
31 {
32  SET_WITH_LTPT3D::iterator iter;
33  iter = a_set.begin();
34  iter = a_set.find(xms::Pt3d(0, 0, 0));
35  TS_ASSERT_EQUALS(iter, a_set.end());
36 }
37 } // unnamed namespace
38 //------------------------------------------------------------------------------
40 //------------------------------------------------------------------------------
42 {
43  // Test xms::ltPt3d functor by itself
44  {
45  // Some points from lowest to highest
46  xms::Pt3d pt1(0, 0, 0);
47  xms::Pt3d pt2(0.0001, 0.0001, 0.0001);
48  xms::Pt3d pt3(0.001, 0.001, 0.001);
49  xms::Pt3d pt4(0.01, 0.01, 0.01);
50 
51  xms::ltPt3d functor1(0.0001, 0.0001);
52 
53  TS_ASSERT_EQUALS(functor1(pt1, pt2), false); // given our tol, these are =
54  TS_ASSERT_EQUALS(functor1(pt1, pt3), true);
55  TS_ASSERT_EQUALS(functor1(pt1, pt4), true);
56  TS_ASSERT_EQUALS(functor1(pt2, pt1), false); // given our tol, these are =
57  TS_ASSERT_EQUALS(functor1(pt2, pt3), true);
58  TS_ASSERT_EQUALS(functor1(pt2, pt4), true);
59  TS_ASSERT_EQUALS(functor1(pt3, pt1), false);
60  TS_ASSERT_EQUALS(functor1(pt3, pt2), false);
61  TS_ASSERT_EQUALS(functor1(pt3, pt4), true);
62  TS_ASSERT_EQUALS(functor1(pt4, pt1), false);
63  TS_ASSERT_EQUALS(functor1(pt4, pt2), false);
64  TS_ASSERT_EQUALS(functor1(pt4, pt3), false);
65  }
66 
67  // Test xms::ltPt3d_2D functor by itself
68  {
69  xms::Pt3d pt1(0, 0, 1);
70  xms::Pt3d pt2(0, 0, 2);
71 
72  xms::ltPt3d_2D functor1(0.001);
73 
74  // Neither point should be less than the other because we don't compare z
75  TS_ASSERT_EQUALS(functor1(pt1, pt2), false);
76  TS_ASSERT_EQUALS(functor1(pt2, pt1), false);
77  }
78 
79  // Test xms::ltPt2d functor by itself
80  {
81  xms::Pt2d pt1(0, 0);
82  xms::Pt2d pt2(0.0001, 0.0001);
83  xms::Pt2d pt3(0.001, 0.001);
84  xms::Pt2d pt4(0.01, 0.01);
85 
86  xms::ltPt2d functor1(0.0001);
87 
88  TS_ASSERT_EQUALS(functor1(pt1, pt2), false); // given our tol, these are =
89  TS_ASSERT_EQUALS(functor1(pt1, pt3), true);
90  TS_ASSERT_EQUALS(functor1(pt1, pt4), true);
91  TS_ASSERT_EQUALS(functor1(pt2, pt1), false); // given our tol, these are =
92  TS_ASSERT_EQUALS(functor1(pt2, pt3), true);
93  TS_ASSERT_EQUALS(functor1(pt2, pt4), true);
94  TS_ASSERT_EQUALS(functor1(pt3, pt1), false);
95  TS_ASSERT_EQUALS(functor1(pt3, pt2), false);
96  TS_ASSERT_EQUALS(functor1(pt3, pt4), true);
97  TS_ASSERT_EQUALS(functor1(pt4, pt1), false);
98  TS_ASSERT_EQUALS(functor1(pt4, pt2), false);
99  TS_ASSERT_EQUALS(functor1(pt4, pt3), false);
100  }
101 
102  // Test the functor as part of a set
103  {
104  // Some points from lowest to highest
105  xms::Pt3d pt1(0, 0, 0);
106  xms::Pt3d pt2(0.0001, 0.0001, 0.0001);
107  xms::Pt3d pt3(0.001, 0.001, 0.001);
108  xms::Pt3d pt4(0.01, 0.01, 0.01);
109 
110  xms::ltPt3d functor1(0.0001, 0.0001);
111  std::set<xms::Pt3d, xms::ltPt3d> set_pt3d(functor1);
112 
113  // Add the points in descending order
114  set_pt3d.insert(pt4);
115  set_pt3d.insert(pt3);
116  set_pt3d.insert(pt2);
117  set_pt3d.insert(pt1); // this one will be ignored given our tolerance
118 
119  TS_ASSERT_EQUALS(set_pt3d.size(), 3); // given our tol, pt1 and pt2 are =
120 
121  // Iterate through the set and see that they are in the right order
122  // even though we put them in in the wrong order
123  std::set<xms::Pt3d, xms::ltPt3d>::iterator iter;
124  iter = set_pt3d.begin();
125  TS_ASSERT_EQUALS(*iter, pt2);
126  ++iter;
127  TS_ASSERT_EQUALS(*iter, pt3);
128  ++iter;
129  TS_ASSERT_EQUALS(*iter, pt4);
130  }
131 
132  // Test the functor as part of a set
133  {
134  // Some points from lowest to highest
135  xms::Pt3d pt1(0, 0, 0);
136  xms::Pt3d pt2(0.0001, 0.0001, 0.0001);
137  xms::Pt3d pt3(0.001, 0.001, 0.001);
138  xms::Pt3d pt4(0.01, 0.01, 0.01);
139 
140  std::set<xms::Pt3d, xms::ltPt3d> set_pt3d(xms::ltPt3d(0.0001, 0.0001));
141 
142  // Add the points in descending order
143  set_pt3d.insert(pt4);
144  set_pt3d.insert(pt3);
145  set_pt3d.insert(pt2);
146  set_pt3d.insert(pt1); // this one will be ignored given our tolerance
147 
148  TS_ASSERT_EQUALS(set_pt3d.size(), 3); // given our tol, pt1 and pt2 are =
149 
150  // Iterate through the set and see that they are in the right order
151  // even though we put them in in the wrong order
152  std::set<xms::Pt3d, xms::ltPt3d>::iterator iter;
153  iter = set_pt3d.begin();
154  TS_ASSERT_EQUALS(*iter, pt2);
155  ++iter;
156  TS_ASSERT_EQUALS(*iter, pt3);
157  ++iter;
158  TS_ASSERT_EQUALS(*iter, pt4);
159  }
160 
161  // Test passing a set that is based of a typedef and uses a functor
162  {
163  xms::ltPt3d functor1(0.0001, 0.0001);
164  SET_WITH_LTPT3D my_set(functor1);
165  SET_WITH_LTPT3D::iterator iter;
166 
167  iter = my_set.find(xms::Pt3d(0, 0, 0));
168  TS_ASSERT_EQUALS(iter, my_set.end());
169  iTestPassingMptFunctorTypes(my_set);
170  }
171 
172  // Test ptTruncate() for Pt2d
173  {
174  xms::Pt2d in(1.2, -1.2);
175  in = xms::ptTruncate(in);
176  TS_ASSERT_DELTA(in.x, 1.0, 0.001);
177  TS_ASSERT_DELTA(in.y, -1.0, 0.001);
178  }
179 
180  // Test ptTruncate() for Pt3d
181  {
182  xms::Pt3d in(1.2, -1.2, 4.0);
183  in = xms::ptTruncate(in);
184  TS_ASSERT_DELTA(in.x, 1.0, 0.001);
185  TS_ASSERT_DELTA(in.y, -1.0, 0.001);
186  TS_ASSERT_DELTA(in.z, 4.0, 0.001);
187  }
188 
189  // TS_FAIL("PtTest::TestIt");
190 } // MptTester::testLessThanFunctors
191 #endif
T z
z coordinate
Definition: pt.h:799
T x
x coordinate
Definition: pt.h:797
less than Pt3 comparing in 2D, or, only x and y
Definition: functors.h:74
void TestIt()
test functors
Definition: functors.cpp:41
T x
x coordinate
Definition: pt.h:88
std::set< xms::Pt3d, xms::ltPt3d > SET_WITH_LTPT3D
meant for testing only
Definition: functors.cpp:23
2D Point template class
Definition: pt.h:82
3d point template class
Definition: pt.h:791
T y
y coordinate
Definition: pt.h:89
T y
y coordinate
Definition: pt.h:798
less than Pt2
Definition: functors.h:118
less than Pt3
Definition: functors.h:20