Browse Source

[Positioner] explicit ReferencePoint(Point3D&)

Make ReferencePoint's Point3D copy constructor explicit. This implied
the following changes:

* Stock::get_reference_point(ReferencePoint&) becomes
  get_reference_point(Point3D&). Reference points were always
  retrieved by their coordinates only in the Stock anyway.

* Stock::find_create_reference_point(Point3D&) was added, since it can
  have a different outcome than its (ReferencePoint&) counterpart.

* The unit tests for Stock were improved.
master
Matteo Cypriani 2 years ago
parent
commit
243dc321b9

+ 2
- 1
owlps-positioner/referencepoint.hh View File

@@ -37,6 +37,7 @@ protected:
37 37
 
38 38
   /** @name Read accessors */
39 39
   //@{
40
+  /// Returns all the measurements of all the calibration #requests
40 41
   std::unordered_map<std::string, Measurement>
41 42
   get_all_measurements(void) const ;
42 43
   /// Returns all the measurements sent by the given mobile
@@ -49,7 +50,7 @@ public:
49 50
                  const float _y = 0,
50 51
                  const float _z = 0): Point3D(_x, _y, _z) {}
51 52
 
52
-  ReferencePoint(const Point3D &p): Point3D(p) {}
53
+  explicit ReferencePoint(const Point3D &p): Point3D(p) {}
53 54
 
54 55
   ReferencePoint(const ReferencePoint &source):
55 56
     Point3D(source), requests(source.requests) {}

+ 8
- 3
owlps-positioner/stock.cc View File

@@ -380,9 +380,14 @@ bool Stock::reference_point_exists(const ReferencePoint &point)
380 380
  * given coordinates.
381 381
  */
382 382
 const ReferencePoint& Stock::
383
-get_reference_point(const ReferencePoint &point)
384
-{
385
-  auto i = reference_points.find(point) ;
383
+get_reference_point(const Point3D &point)
384
+{
385
+  /* Note that we can look up a ReferencePoint in the unordered_set
386
+   * #reference_points using only its coordinates (Point3D) only because
387
+   * we defined `hash` and `equal_to` to take into consideration only
388
+   * the coordinates of the ReferencePoint (see referencepoint.hh).
389
+   */
390
+  auto i = reference_points.find(ReferencePoint(point));
386 391
   if (i != reference_points.end())
387 392
     return *i ;
388 393
   throw element_not_found("No ReferencePoint with coordinates " +

+ 13
- 1
owlps-positioner/stock.hh View File

@@ -168,10 +168,14 @@ public:
168 168
   static bool reference_point_exists(const ReferencePoint &point) ;
169 169
   /// Returns the ReferencePoint at the given coordinates
170 170
   static const ReferencePoint&
171
-  get_reference_point(const ReferencePoint &point) ;
171
+  get_reference_point(const Point3D &point) ;
172 172
   /// Searches for a ReferencePoint and adds it if it does not exist
173 173
   static const ReferencePoint&
174 174
   find_create_reference_point(const ReferencePoint &point) ;
175
+  /// Searches for a ReferencePoint at the given coordinates and adds it
176
+  /// if it doesn't exist
177
+  static const ReferencePoint&
178
+  find_create_reference_point(const Point3D &point);
175 179
   /// Searches for the closest ReferencePoint (in the signal strength
176 180
   /// space) to a given Request
177 181
   static const ReferencePoint&
@@ -302,6 +306,14 @@ inline unsigned int Stock::nb_reference_points()
302 306
 }
303 307
 
304 308
 
309
+inline const ReferencePoint&
310
+Stock::find_create_reference_point(const Point3D &point)
311
+{
312
+  return find_create_reference_point(ReferencePoint(point));
313
+}
314
+
315
+
316
+
305 317
 
306 318
 /* *** CalibrationRequest operations *** */
307 319
 

+ 1
- 0
owlps-positioner/tests/referencepoint_test.hh View File

@@ -15,6 +15,7 @@
15 15
 
16 16
 #include "referencepoint.hh"
17 17
 #include "calibrationrequest.hh"
18
+#include "stock.hh"
18 19
 
19 20
 class ReferencePoint_test: public CxxTest::TestSuite
20 21
 {

+ 76
- 2
owlps-positioner/tests/stock_test.hh View File

@@ -13,6 +13,7 @@
13 13
 
14 14
 #include <cxxtest/TestSuite.h>
15 15
 
16
+#include "calibrationrequest.hh"
16 17
 #include "stock.hh"
17 18
 #include "posexcept.hh"
18 19
 
@@ -122,9 +123,20 @@ public:
122 123
     // TODO: implement that
123 124
   }
124 125
 
125
-  void test_reference_points(void)
126
+  /*
127
+   * This test creates a ReferencePoint, then retrieves it from its
128
+   * coordinates (Point3D).
129
+   */
130
+  void test_reference_points_1(void)
126 131
   {
127
-    ReferencePoint referencepoint1(3,5,7) ;
132
+    Point3D point1(3,5,7);
133
+    ReferencePoint referencepoint1(point1);
134
+    CalibrationRequest cr1(42); // 42 is obviously not a valid request type
135
+    referencepoint1.add_request(&cr1);
136
+
137
+    TS_ASSERT_THROWS(Stock::get_reference_point(point1), element_not_found);
138
+    TS_ASSERT_THROWS(Stock::get_reference_point(referencepoint1),
139
+                     element_not_found);
128 140
 
129 141
     const ReferencePoint &referencepoint1_ref1 =
130 142
       Stock::find_create_reference_point(referencepoint1) ;
@@ -137,6 +149,68 @@ public:
137 149
     TS_ASSERT_DIFFERS(&referencepoint1, &referencepoint1_ref2) ;
138 150
 
139 151
     TS_ASSERT_EQUALS(&referencepoint1_ref1, &referencepoint1_ref2) ;
152
+
153
+    const ReferencePoint &referencepoint1_ref3 =
154
+      Stock::get_reference_point(referencepoint1);
155
+    TS_ASSERT_EQUALS(referencepoint1, referencepoint1_ref3) ;
156
+    TS_ASSERT_DIFFERS(&referencepoint1, &referencepoint1_ref3) ;
157
+
158
+    /* Now let's use the Point3D to look up the ReferencePoint */
159
+
160
+    const ReferencePoint &referencepoint1_ref4 =
161
+      Stock::get_reference_point(point1);
162
+    TS_ASSERT_EQUALS(referencepoint1, referencepoint1_ref4);
163
+
164
+    const ReferencePoint &referencepoint1_ref5 =
165
+      Stock::find_create_reference_point(point1);
166
+    TS_ASSERT_EQUALS(referencepoint1, referencepoint1_ref5);
167
+  }
168
+
169
+  /*
170
+   * This test creates a ReferencePoint using a Point3D in the first
171
+   * place, and compares to a ReferencePoint created from a
172
+   * ReferencePoint.
173
+   */
174
+  void test_reference_points_2(void)
175
+  {
176
+    // Create with Point3D, retrieve with ReferencePoint
177
+    Point3D p1(1,2,3);
178
+    const ReferencePoint &rp1_ref1 = Stock::find_create_reference_point(p1);
179
+    ReferencePoint rp1(1,2,3);
180
+    const ReferencePoint &rp1_ref2 = Stock::get_reference_point(rp1);
181
+    const ReferencePoint &rp1_ref3 = Stock::find_create_reference_point(rp1);
182
+    TS_ASSERT_EQUALS(&rp1_ref1, &rp1_ref2);
183
+    TS_ASSERT_EQUALS(&rp1_ref1, &rp1_ref3);
184
+    TS_ASSERT_EQUALS(rp1, rp1_ref1);
185
+
186
+    // Create with ReferencePoint, retrieve with Point3D
187
+    ReferencePoint rp2(4,5,6);
188
+    const ReferencePoint &rp2_ref1 = Stock::find_create_reference_point(rp2);
189
+    Point3D p2(4,5,6);
190
+    const ReferencePoint &rp2_ref2 = Stock::get_reference_point(p2);
191
+    const ReferencePoint &rp2_ref3 = Stock::find_create_reference_point(p2);
192
+    TS_ASSERT_EQUALS(&rp2_ref1, &rp2_ref2);
193
+    TS_ASSERT_EQUALS(&rp2_ref1, &rp2_ref3);
194
+    TS_ASSERT_EQUALS(rp2, rp2_ref1);
195
+
196
+    /* Now with a request in the reference point */
197
+
198
+    // Create with Point3D, retrieve with ReferencePoint
199
+    Point3D p3(7,8,9);
200
+    const ReferencePoint &rp3_ref1 = Stock::find_create_reference_point(p3);
201
+    ReferencePoint rp3(7,8,9);
202
+    CalibrationRequest cr3(42); // 42 is obviously not a valid request type
203
+    rp3.add_request(&cr3);
204
+    const ReferencePoint &rp3_ref2 = Stock::get_reference_point(rp3);
205
+    const ReferencePoint &rp3_ref3 = Stock::find_create_reference_point(rp3);
206
+    TS_ASSERT_EQUALS(&rp3_ref1, &rp3_ref2);
207
+    TS_ASSERT_EQUALS(&rp3_ref1, &rp3_ref3);
208
+    TS_ASSERT_DIFFERS(rp3, rp3_ref1); /* the ReferencePoint in the stock doesn't
209
+                                       * have the CalibrationRequest */
210
+    ReferencePoint &rp3_ref_rw =
211
+      const_cast<ReferencePoint&>(Stock::get_reference_point(rp3));
212
+    rp3_ref_rw.add_request(&cr3);
213
+    TS_ASSERT_EQUALS(rp3, rp3_ref1);
140 214
   }
141 215
 
142 216
   void test_closest_reference_point(void)

Loading…
Cancel
Save