From 1eac9bb71891ba74e86021576717d14e094a56fa Mon Sep 17 00:00:00 2001 From: Matteo Cypriani Date: Sat, 22 Jun 2013 17:10:38 -0400 Subject: [PATCH] [Positioner] tests: review and update (100% pass) Review and fix all the tests so that 100% of the tests pass. --- CMakeLists.txt | 8 ++ INSTALL.t2t | 9 +++ TODO.t2t | 40 ++++++++-- owlps-positioner/CMakeLists.txt | 1 - .../tests/calibrationrequest_test.hh | 56 +++++++++++--- owlps-positioner/tests/capturepoint_test.hh | 16 ++-- owlps-positioner/tests/csvfilereader_test.hh | 6 +- owlps-positioner/tests/direction_test.hh | 9 ++- owlps-positioner/tests/inputcsv_test.hh | 33 ++------- owlps-positioner/tests/inputlogcsv_test.hh | 7 +- .../tests/interlinknetworks_test.hh | 20 +++-- owlps-positioner/tests/measurement_test.hh | 27 ++++--- owlps-positioner/tests/outputcsv_test.hh | 18 +---- owlps-positioner/tests/point3d_test.hh | 19 +++++ owlps-positioner/tests/realposition_test.hh | 16 ++-- owlps-positioner/tests/referencepoint_test.hh | 42 +++-------- owlps-positioner/tests/request_test.hh | 52 +++++++++---- owlps-positioner/tests/result_test.hh | 24 +++++- owlps-positioner/tests/stock_test.hh | 12 +-- owlps-positioner/tests/testsetup.cc | 2 + owlps-positioner/tests/testutil.cc | 55 +++++++++++++- owlps-positioner/tests/testutil.hh | 1 + owlps-positioner/tests/textfilereader_test.hh | 2 +- owlps-positioner/tests/textfilewriter_test.hh | 2 +- owlps-positioner/tests/timestamp_test.hh | 74 ++++++++++++------- .../tests/topologyreadercsv_test.hh | 11 +-- owlps-positioner/tests/userinterface_test.hh | 2 +- 27 files changed, 368 insertions(+), 196 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 76b0e24..1f3bdff 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -222,3 +222,11 @@ add_custom_target(semistatic DEPENDS ${OWLPS_SEMISTATIC_TARGETS}) # The OWLPS_STATIC_TARGET is defined in subdirectories add_custom_target(static DEPENDS ${OWLPS_STATIC_TARGETS}) + + + +### Tests ### + +add_custom_target(test + COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_positioner + DEPENDS test_positioner) diff --git a/INSTALL.t2t b/INSTALL.t2t index d07d8a8..54dd21d 100644 --- a/INSTALL.t2t +++ b/INSTALL.t2t @@ -132,6 +132,15 @@ To display all the available targets, type: ``` make help += Running the tests = + +To run the unit tests, use the ``test`` target: + +``` make test + +Currently, only OwlPS Positioner has unit tests. + + = Compiling for OpenWrt = == The OpenWrt toolchain == diff --git a/TODO.t2t b/TODO.t2t index 9284fd8..ceb9505 100644 --- a/TODO.t2t +++ b/TODO.t2t @@ -183,8 +183,6 @@ Work to do in OwlPS - With autocalibration on, all the CPs declared in listeners.csv must be in coverage or the reference points will not be generated. -- Unit tests are currently unmaintained. Do not try to run them. - == Algorithms == @@ -295,16 +293,44 @@ Work to do in OwlPS == Unit tests == -- Update (rewrite?) tests. +- Classes with incomplete tests: + - InterlinkNetworks: compute() + - Measurement: average, variance, standard deviation, operations + - MinMax: trilaterate_2d() + - OutputCSV: write(ResultList) + - Point3D: distance & angle operations + - PosUtil: most functions + - ReferencePoint: operations + - Stock: a lot of functions -- Unfinished tests: - - InputDataReader +- Classes without tests or with mock test files (possibly untestable or + not worth testing, to be checked): + - Autocalibration + - CapturePointsReaderCSV + - CartographyAlgorithm + - CSVStringReader + - FBCM + - FRBHMBasic - Input + - InputDataReader + - InputLogMedium + - InputMedium + - InputUDPSocket + - MobilesReaderCSV + - NSS - Output + - OutputMedium + - OutputNetworkSocket + - OutputTCPSocketEvAAL + - OutputUDPSocket - Positioning + - PositioningAlgorithm + - ResultList + - TrilaterationAlgorithm + - TrilaterationMethod -- Test InterlinkNetworks::compute() ? - Timestamp: there is a probability of 10^-6 that the value in nanoseconds and the rounded value in milliseconds are identical, in - which case some tests can fail. + which case one of the tests can fail. This could be fixed by retrying + the same test a couple of times. diff --git a/owlps-positioner/CMakeLists.txt b/owlps-positioner/CMakeLists.txt index 355b0b1..66f8e2e 100644 --- a/owlps-positioner/CMakeLists.txt +++ b/owlps-positioner/CMakeLists.txt @@ -161,7 +161,6 @@ set(TESTS_SRC_FILES ${TESTS_DIR}/textfilewriter_test.hh ${TESTS_DIR}/timestamp_test.hh ${TESTS_DIR}/topologyreadercsv_test.hh - ${TESTS_DIR}/userinterface_test.hh ${TESTS_DIR}/waypoint_test.hh ${TESTS_DIR}/wifidevice_test.hh ) diff --git a/owlps-positioner/tests/calibrationrequest_test.hh b/owlps-positioner/tests/calibrationrequest_test.hh index 16fe58b..1e4ede5 100644 --- a/owlps-positioner/tests/calibrationrequest_test.hh +++ b/owlps-positioner/tests/calibrationrequest_test.hh @@ -8,11 +8,9 @@ public: void test_constructors(void) { - // Default constructor - CalibrationRequest calibrationrequest00 ; - std::vector vi1 ; - CalibrationRequest calibrationrequest01(Request(), NULL) ; - TS_ASSERT_EQUALS(calibrationrequest00, calibrationrequest01) ; + // We can't test the default constructor with two different objects + // like we usually do because the reception time is initialised by + // the Request constructor. // Copy constructor Mobile mobile1 ; @@ -25,7 +23,14 @@ public: TS_ASSERT_EQUALS(calibrationrequest1, calibrationrequest2) ; // Request copy constructor - CalibrationRequest calibrationrequest3(request1) ; + /* We must force the request type to be the same in the Request and + * in the CalibrationRequest, because by default it is set to + * OWL_REQUEST_UNDEFINED for the Request and to + * OWL_REQUEST_AUTOCALIBRATION for the CalibrationRequest. */ + request1.set_type(OWL_REQUEST_CALIBRATION) ; + CalibrationRequest + calibrationrequest3(request1, &referencepoint1, Direction(1), + OWL_REQUEST_CALIBRATION) ; TS_ASSERT_EQUALS(request1, calibrationrequest3) ; } @@ -40,10 +45,13 @@ public: Timestamp timestamp1 ; timestamp1.now() ; Request request1(&mobile1, timestamp1) ; + Direction direction1(1) ; ReferencePoint referencepoint1(1,2,3) ; - CalibrationRequest calibrationrequest1(request1, &referencepoint1) ; + CalibrationRequest + calibrationrequest1(request1, &referencepoint1, direction1) ; TS_ASSERT_EQUALS(calibrationrequest1.get_mobile(), &mobile1) ; TS_ASSERT_EQUALS(calibrationrequest1.get_time_sent(), timestamp1) ; + TS_ASSERT_EQUALS(calibrationrequest1.get_direction(), direction1) ; TS_ASSERT_EQUALS(calibrationrequest1.get_reference_point(), &referencepoint1) ; @@ -56,6 +64,10 @@ public: calibrationrequest1.set_time_sent(timestamp1) ; TS_ASSERT_EQUALS(calibrationrequest1.get_time_sent(), timestamp1) ; + Direction direction2(2) ; + calibrationrequest1.set_direction(direction2) ; + TS_ASSERT_EQUALS(calibrationrequest1.get_direction(), direction2) ; + ReferencePoint referencepoint2(4,3,2) ; calibrationrequest1.set_reference_point(&referencepoint2) ; TS_ASSERT_EQUALS(calibrationrequest1.get_reference_point(), @@ -63,6 +75,7 @@ public: calibrationrequest1.clear() ; CalibrationRequest calibrationrequest2 ; + calibrationrequest2.clear() ; TS_ASSERT_EQUALS(calibrationrequest1, calibrationrequest2) ; } @@ -74,16 +87,37 @@ public: Timestamp timestamp1 ; timestamp1.now() ; Request request1(&mobile1, timestamp1) ; + Direction direction1(1) ; ReferencePoint referencepoint1 ; - CalibrationRequest calibrationrequest1(request1, &referencepoint1) ; - CalibrationRequest calibrationrequest2(request1, &referencepoint1) ; + CalibrationRequest + calibrationrequest1(request1, &referencepoint1, + direction1, OWL_REQUEST_CALIBRATION) ; + CalibrationRequest + calibrationrequest2(request1, &referencepoint1, + direction1, OWL_REQUEST_CALIBRATION) ; + TS_ASSERT_EQUALS(calibrationrequest1, calibrationrequest2) ; + + // != and == + Direction direction2(2) ; + calibrationrequest2.set_direction(direction2) ; + TS_ASSERT(calibrationrequest1 != calibrationrequest2) ; + + calibrationrequest2.set_direction(direction1) ; + TS_ASSERT_EQUALS(calibrationrequest1, calibrationrequest2) ; + + calibrationrequest2.set_type(OWL_REQUEST_AUTOCALIBRATION) ; + TS_ASSERT(calibrationrequest1 != calibrationrequest2) ; + + calibrationrequest2.set_type(OWL_REQUEST_CALIBRATION) ; TS_ASSERT_EQUALS(calibrationrequest1, calibrationrequest2) ; - // != timestamp1.now() ; Request request2(&mobile1, timestamp1) ; ReferencePoint referencepoint2 ; - CalibrationRequest calibrationrequest3(request2, &referencepoint2) ; + Direction direction3(3) ; + CalibrationRequest + calibrationrequest3(request2, &referencepoint2, + direction3, OWL_REQUEST_AUTOCALIBRATION) ; TS_ASSERT(calibrationrequest1 != calibrationrequest3) ; // = diff --git a/owlps-positioner/tests/capturepoint_test.hh b/owlps-positioner/tests/capturepoint_test.hh index cc887ee..cb5e457 100644 --- a/owlps-positioner/tests/capturepoint_test.hh +++ b/owlps-positioner/tests/capturepoint_test.hh @@ -11,11 +11,13 @@ public: // Default constructor CapturePoint capturepoint1 ; CapturePoint capturepoint2(Point3D(), "", "", - CP_DEFAULT_ANTENNA_GAIN, - WIFIDEVICE_DEFAULT_TRX_POWER, - CP_DEFAULT_CHANNEL) ; + CP_DEFAULT_ANTENNA_GAIN, + WIFIDEVICE_DEFAULT_TRX_POWER, + CP_DEFAULT_CHANNEL) ; TS_ASSERT_EQUALS(capturepoint1, capturepoint2) ; + // TODO: WifiDevice copy constructors + // Copy constructor CapturePoint capturepoint3(Point3D(4,3,5), "192.168.0.1", "AA:BB:CC:DD:EE:FF", 5, 32, 11) ; @@ -29,7 +31,7 @@ public: // Simple read accessors Point3D point3d1(78,23,4) ; CapturePoint capturepoint1(point3d1, "192.168.0.1", - "AA:BB:CC:DD:EE:FF", 6, 38, 10) ; + "AA:BB:CC:DD:EE:FF", 6, 38, 10) ; TS_ASSERT_EQUALS(capturepoint1.get_coordinates(), point3d1) ; TS_ASSERT_EQUALS(capturepoint1.get_ip_addr(), "192.168.0.1") ; TS_ASSERT_EQUALS(capturepoint1.get_mac_addr(), "AA:BB:CC:DD:EE:FF") ; @@ -63,15 +65,15 @@ public: // == Point3D point3d1(78,23,4) ; CapturePoint capturepoint1(point3d1, "192.168.0.1", - "AA:BB:CC:DD:EE:FF", 6, 38, 10) ; + "AA:BB:CC:DD:EE:FF", 6, 38, 10) ; CapturePoint capturepoint2(point3d1, "192.168.0.1", - "AA:BB:CC:DD:EE:FF", 6, 38, 10) ; + "AA:BB:CC:DD:EE:FF", 6, 38, 10) ; TS_ASSERT_EQUALS(capturepoint1, capturepoint2) ; // != Point3D point3d2(7,3,24) ; CapturePoint capturepoint3(point3d1, "10.0.0.1", - "AA:BB:CC:DD:EE:FF", 6, 38, 10) ; + "AA:BB:CC:DD:EE:FF", 6, 38, 10) ; TS_ASSERT(capturepoint2 != capturepoint3) ; // = diff --git a/owlps-positioner/tests/csvfilereader_test.hh b/owlps-positioner/tests/csvfilereader_test.hh index 2e9c66b..2e7764a 100644 --- a/owlps-positioner/tests/csvfilereader_test.hh +++ b/owlps-positioner/tests/csvfilereader_test.hh @@ -85,7 +85,11 @@ public: std::ofstream output_file(test_file_name.c_str()) ; assert(output_file) ; for (unsigned int i = 0 ; i < nlines ; ++i) - output_file << "aa:bb:cc:dd:ee:ff;1265120910725;0;0;0;0;11:22:33:44:55:01;-58;11:22:33:44:55:03;-50;11:22:33:44:55:02;-42;11:22:33:44:55:01;-55;11:22:33:44:55:02;-37\n" ; + output_file + << "aa:bb:cc:dd:ee:ff;1265120910725;0;0;0;0;" + << "11:22:33:44:55:01;-58;11:22:33:44:55:03;-50;" + << "11:22:33:44:55:02;-42;11:22:33:44:55:01;-55;" + << "11:22:33:44:55:02;-37\n" ; output_file.close() ; CSVFileReader *csvfilereader ; diff --git a/owlps-positioner/tests/direction_test.hh b/owlps-positioner/tests/direction_test.hh index 13d7250..fc7b84a 100644 --- a/owlps-positioner/tests/direction_test.hh +++ b/owlps-positioner/tests/direction_test.hh @@ -11,11 +11,10 @@ public: { // Default constructor TS_ASSERT_THROWS_NOTHING(Direction()) ; - Direction direction0 ; - TS_ASSERT(! direction0) ; - // char constructor - TS_ASSERT_THROWS(Direction(0), bad_direction) ; + // int constructor + // 0 is accepted, although it doesn't create a valid Direction + TS_ASSERT_THROWS_NOTHING(Direction(0)) ; TS_ASSERT_THROWS(Direction(5), bad_direction) ; TS_ASSERT_THROWS_NOTHING(Direction(north)) ; @@ -43,6 +42,7 @@ public: void test_operators(void) { + Direction direction0 ; Direction direction1(Direction::north) ; Direction direction2(Direction::east) ; Direction direction3(Direction::south) ; @@ -61,6 +61,7 @@ public: TS_ASSERT(direction5 != direction6) ; // bool + TS_ASSERT(! direction0) ; TS_ASSERT(direction1) ; TS_ASSERT(direction2) ; TS_ASSERT(direction3) ; diff --git a/owlps-positioner/tests/inputcsv_test.hh b/owlps-positioner/tests/inputcsv_test.hh index 51a126e..5f7fc0e 100644 --- a/owlps-positioner/tests/inputcsv_test.hh +++ b/owlps-positioner/tests/inputcsv_test.hh @@ -20,9 +20,6 @@ public: // If we cannot open the file, we want to stop the test CxxTest::setAbortTestOnFail(true) ; - // Clear the stock - Stock::clear() ; - csv_file_name = "/tmp/InputCSV_test_csv_file.csv" ; TestUtil::create_test_csv_file(csv_file_name, true) ; @@ -35,9 +32,6 @@ public: { // Delete the test CSV file TestUtil::remove_file(csv_file_name) ; - - // Clear the stock - Stock::clear() ; } @@ -66,27 +60,22 @@ public: request1 = inputcsv1.get_next_request() ; TS_ASSERT(request1) ; - /* assert(request1.get_mobile()) ; TS_ASSERT_EQUALS(request1.get_mobile()->get_mac_addr(), TestUtil::mobiles[0].get_mac_addr()) ; TS_ASSERT_EQUALS(request1.get_time_sent(), TestUtil::requests.at(0)->get_time_sent()) ; - for (std::vector::const_iterator i = TestUtil::aps.begin() ; - i != TestUtil::aps.end() ; ++i) + for (auto i = TestUtil::aps.begin() ; i != TestUtil::aps.end() ; ++i) { std::unordered_map::const_iterator measurement_it1 = request1.get_measurements().find(i->get_mac_addr()) ; TS_ASSERT(request1.get_measurements().end() != measurement_it1) ; - // TS_ASSERT_EQUALS(TestUtil::requests.at(0)->get_measurements(). - // at(i->get_mac_addr()).get_ss_list(), - // measurement_it1->second.get_ss_list()) ; - + // We cannot compare the complete ss_list since there is not a + // Measurement::get_ss_list() any more. } - */ TS_ASSERT(inputcsv1) ; TS_ASSERT(! inputcsv1.eof()) ; @@ -95,31 +84,24 @@ public: request1 = inputcsv1.get_next_request() ; TS_ASSERT(request1) ; - /* assert(request1.get_mobile()) ; TS_ASSERT_EQUALS(request1.get_mobile()->get_mac_addr(), TestUtil::mobiles[1].get_mac_addr()) ; TS_ASSERT_EQUALS(request1.get_time_sent(), TestUtil::requests.at(1)->get_time_sent()) ; - for (std::vector::const_iterator i = TestUtil::aps.begin() ; - i != TestUtil::aps.end() ; ++i) + for (auto i = TestUtil::aps.begin() ; i != TestUtil::aps.end() ; ++i) { std::unordered_map::const_iterator measurement_it1 = request1.get_measurements().find(i->get_mac_addr()) ; TS_ASSERT(request1.get_measurements().end() != measurement_it1) ; - // TS_ASSERT_EQUALS(TestUtil::requests.at(1)->get_measurements(). - // at(i->get_mac_addr()).get_ss_list(), - // measurement_it1->second.get_ss_list()) ; } - */ // Third Request // TS_ASSERT(inputcsv1) ; TS_ASSERT(request1) ; - /* assert(request1.get_mobile()) ; TS_ASSERT(! inputcsv1.eof()) ; @@ -129,18 +111,13 @@ public: TS_ASSERT_EQUALS(request1.get_time_sent(), TestUtil::requests.at(2)->get_time_sent()) ; - for (std::vector::const_iterator i = TestUtil::aps.begin() ; - i != TestUtil::aps.end() ; ++i) + for (auto i = TestUtil::aps.begin() ; i != TestUtil::aps.end() ; ++i) { std::unordered_map::const_iterator measurement_it1 = request1.get_measurements().find(i->get_mac_addr()) ; TS_ASSERT(request1.get_measurements().end() != measurement_it1) ; - // TS_ASSERT_EQUALS(TestUtil::requests.at(2)->get_measurements(). - // at(i->get_mac_addr()).get_ss_list(), - // measurement_it1->second.get_ss_list()) ; } - */ // End of file diff --git a/owlps-positioner/tests/inputlogcsv_test.hh b/owlps-positioner/tests/inputlogcsv_test.hh index 938e449..fef4d00 100644 --- a/owlps-positioner/tests/inputlogcsv_test.hh +++ b/owlps-positioner/tests/inputlogcsv_test.hh @@ -18,9 +18,6 @@ public: // If we cannot open the file, we want to stop the test CxxTest::setAbortTestOnFail(true) ; - // Clear the stock - Stock::clear() ; - csv_file_name = "/tmp/InputLogCSV_test_csv_file.csv" ; TestUtil::create_test_csv_file(csv_file_name) ; @@ -35,7 +32,6 @@ public: { TestUtil::remove_file(csv_file_name) ; TestUtil::remove_file(log_file_name) ; - Stock::clear() ; } @@ -70,8 +66,7 @@ public: // Read the two files and compare to the reference Request list InputCSV inputcsv_csv(csv_file_name) ; InputCSV inputcsv_log(log_file_name) ; - for (std::vector::const_iterator i = - TestUtil::requests.begin() ; + for (auto i = TestUtil::requests.begin() ; i != TestUtil::requests.end() ; ++i) { Stock::clear() ; diff --git a/owlps-positioner/tests/interlinknetworks_test.hh b/owlps-positioner/tests/interlinknetworks_test.hh index b01189f..344e29b 100644 --- a/owlps-positioner/tests/interlinknetworks_test.hh +++ b/owlps-positioner/tests/interlinknetworks_test.hh @@ -11,12 +11,13 @@ public: TrilaterationAlgorithm *algo = new InterlinkNetworks() ; // Initialise algo->request - char mobile_gain = 3, mobile_power = 15 ; + float mobile_gain = 3, mobile_power = 15 ; Mobile mobile("", "", mobile_gain, mobile_power) ; Request request(&mobile) ; algo->compute(request) ; // yes, that's ugly - char ap_gain = 5, ap_power = 20, ap_channel = 1 ; + float ap_gain = 5, ap_power = 20 ; + unsigned int ap_channel = 1 ; CapturePoint ap(Point3D(), "", "", ap_gain, ap_power, ap_channel) ; std::map ss_list ; @@ -24,12 +25,17 @@ public: Measurement measurement(&ap) ; measurement.add_ss_list(ss_list) ; - /* ** How to check ref_distance in Scilab? ** - * C = 5 + 20 + 20 * log10(299792458 / 2412000000 / (4 * PI)) + 3 - * L = -42 - * ref_distance = 10^((C-L)/35) + /* To check ref_distance in Scilab: + mobile_gain = 3 + mobile_power = 15 + ap_gain = 5 + celerity = 299792458 + frequency = 2412000000 + C = mobile_gain + mobile_power + ap_gain + 20 * log10(celerity / frequency / (4 * %pi)) + L = -42 + ref_distance = 10^((C-L)/35) */ - float ref_distance = 7.1519 ; + float ref_distance = 5.1470931 ; float computed_distance = algo->estimate_distance(measurement) ; TS_ASSERT_DELTA(computed_distance, ref_distance, 0.0001) ; diff --git a/owlps-positioner/tests/measurement_test.hh b/owlps-positioner/tests/measurement_test.hh index 36ae196..d1289c8 100644 --- a/owlps-positioner/tests/measurement_test.hh +++ b/owlps-positioner/tests/measurement_test.hh @@ -42,6 +42,7 @@ public: m1.add_ss(1, -33) ; m1.add_ss(2, -78) ; m1.add_ss(3, -21) ; + TS_ASSERT_EQUALS(m1.get_nb_ss(), 3) ; TS_ASSERT_EQUALS(m1.get_ss(1), -33) ; TS_ASSERT_EQUALS(m1.get_ss(2), -78) ; TS_ASSERT_EQUALS(m1.get_ss(3), -21) ; @@ -64,18 +65,26 @@ public: */ TS_ASSERT_DELTA(m1.get_average_dbm(), -25.505481, 0.0001) ; - m1.clear() ; + Measurement m2(&ap2) ; std::map vi2 ; - vi2[1] = -54 ; - vi2[2] = -1 ; - m1.add_ss_list(vi2) ; - TS_ASSERT_EQUALS(m1.get_ss(1), -54) ; - TS_ASSERT_EQUALS(m1.get_ss(2), -1) ; - TS_ASSERT_DELTA(m1.get_average_dbm(), -4.0102782, 0.0001) ; + vi2[3] = -54 ; + vi2[4] = -1 ; + m2.add_ss_list(vi2) ; + TS_ASSERT_EQUALS(m2.get_nb_ss(), vi2.size()) ; + TS_ASSERT_EQUALS(m2.get_ss(3), -54) ; + TS_ASSERT_EQUALS(m2.get_ss(4), -1) ; + TS_ASSERT_DELTA(m2.get_average_dbm(), -4.0102782, 0.0001) ; + + m1.merge(m2) ; + TS_ASSERT_EQUALS(m1.get_nb_ss(), 4) ; + TS_ASSERT_EQUALS(m1.get_ss(1), -33) ; + TS_ASSERT_EQUALS(m1.get_ss(2), -78) ; + TS_ASSERT_EQUALS(m1.get_ss(3), -54) ; + TS_ASSERT_EQUALS(m1.get_ss(4), -1) ; m1.clear() ; - Measurement m2 ; - TS_ASSERT_EQUALS(m1, m2) ; + Measurement m3 ; + TS_ASSERT_EQUALS(m1, m3) ; } diff --git a/owlps-positioner/tests/outputcsv_test.hh b/owlps-positioner/tests/outputcsv_test.hh index c44fa95..5dbb7c8 100644 --- a/owlps-positioner/tests/outputcsv_test.hh +++ b/owlps-positioner/tests/outputcsv_test.hh @@ -11,7 +11,6 @@ public: OutputCSV_test(void) { - Stock::clear() ; csv_file_name = "/tmp/OutputCSV_test_csv_file.csv" ; } @@ -19,7 +18,6 @@ public: ~OutputCSV_test(void) { TestUtil::remove_file(csv_file_name) ; - Stock::clear() ; } @@ -35,11 +33,10 @@ public: } - void test_outputcsv(void) + void test_write_result(void) { OutputCSV *outputcsv1 = new OutputCSV(csv_file_name) ; - for (std::vector::const_iterator i = - TestUtil::results.begin() ; + for (auto i = TestUtil::results.begin() ; i != TestUtil::results.end() ; ++i) outputcsv1->write(*i) ; delete outputcsv1 ; @@ -52,15 +49,8 @@ public: getline(inputcsv, line) ; const Result &result = TestUtil::results.at(result_count) ; - std::ostringstream result_csv ; - result_csv - << result.get_request()->get_mobile()->get_mac_addr() << ';' - << result.get_request()->get_time_sent() << ';' - << result.get_position().get_x() << ';' - << result.get_position().get_y() << ';' - << result.get_position().get_z() ; - - TS_ASSERT_EQUALS(line, result_csv.str()) ; + std::string result_csv = result.to_csv() ; + TS_ASSERT_EQUALS(line, result_csv) ; if (++result_count >= TestUtil::results.size()) break ; diff --git a/owlps-positioner/tests/point3d_test.hh b/owlps-positioner/tests/point3d_test.hh index 904e54e..3bb4314 100644 --- a/owlps-positioner/tests/point3d_test.hh +++ b/owlps-positioner/tests/point3d_test.hh @@ -40,6 +40,23 @@ public: TS_ASSERT_EQUALS(p1.get_y(), 321) ; p1.set_z(98) ; TS_ASSERT_EQUALS(p1.get_z(), 98) ; + + p1.set_coordinates(78, 79, 80) ; + TS_ASSERT_EQUALS(p1.get_x(), 78) ; + TS_ASSERT_EQUALS(p1.get_y(), 79) ; + TS_ASSERT_EQUALS(p1.get_z(), 80) ; + + Point3D p2(4, 5, 6) ; + p1.set_coordinates(p2) ; + TS_ASSERT_EQUALS(p1.get_x(), 4) ; + TS_ASSERT_EQUALS(p1.get_y(), 5) ; + TS_ASSERT_EQUALS(p1.get_z(), 6) ; + + float p3[3] = {7, 8, 9} ; + p1.set_coordinates(p3) ; + TS_ASSERT_EQUALS(p1.get_x(), 7) ; + TS_ASSERT_EQUALS(p1.get_y(), 8) ; + TS_ASSERT_EQUALS(p1.get_z(), 9) ; } @@ -58,6 +75,8 @@ public: Point3D p5(23, 2.4, 0.4) ; TS_ASSERT_DELTA(p4.distance(p5), 43.588, 0.001) ; TS_ASSERT_DELTA(p4.square_distance(p5), 1900, 0.001) ; + + // TODO: complete this. } diff --git a/owlps-positioner/tests/realposition_test.hh b/owlps-positioner/tests/realposition_test.hh index df9261e..da25eb5 100644 --- a/owlps-positioner/tests/realposition_test.hh +++ b/owlps-positioner/tests/realposition_test.hh @@ -18,18 +18,22 @@ public: PositioningAlgorithm *algo = new RealPosition() ; Result res ; + // Empty request res = algo->compute(request) ; TS_ASSERT_EQUALS(res.get_position(), null_point) ; + // Non-empty request + Point3D point(3,2,1) ; + request.set_real_position(point) ; + res = algo->compute(request) ; + TS_ASSERT_EQUALS(res.get_position(), point) ; + + // Calibration request res = algo->compute(calibration_request) ; TS_ASSERT_EQUALS(res.get_position(), reference_point) ; - Request *request_ptr ; - request_ptr = &request ; - res = algo->compute(*request_ptr) ; - TS_ASSERT_EQUALS(res.get_position(), null_point) ; - - request_ptr = static_cast(&calibration_request) ; + // CalibrationRequest casted to Request + Request *request_ptr = static_cast(&calibration_request) ; res = algo->compute(*request_ptr) ; TS_ASSERT_EQUALS(res.get_position(), reference_point) ; diff --git a/owlps-positioner/tests/referencepoint_test.hh b/owlps-positioner/tests/referencepoint_test.hh index e14a9ca..11fe4e5 100644 --- a/owlps-positioner/tests/referencepoint_test.hh +++ b/owlps-positioner/tests/referencepoint_test.hh @@ -7,6 +7,7 @@ class ReferencePoint_test: public CxxTest::TestSuite { public: + void test_constructors(void) { // Default constructor @@ -40,7 +41,8 @@ public: TS_ASSERT_EQUALS(rp1.get_z(), 5) ; // Write & read accessors - CalibrationRequest calibrationrequest1, calibrationrequest2, calibrationrequest3 ; + CalibrationRequest + calibrationrequest1, calibrationrequest2, calibrationrequest3 ; rp1.add_request(&calibrationrequest1) ; rp1.add_request(&calibrationrequest2) ; rp1.add_request(&calibrationrequest3) ; @@ -55,37 +57,15 @@ public: TS_ASSERT_EQUALS(rp1.get_y(), 321) ; rp1.set_z(98) ; TS_ASSERT_EQUALS(rp1.get_z(), 98) ; - } - - void test_ss_square_distance(void) - { - CapturePoint ap1(Point3D(1,2,3), "aa:bb:cc:dd:ee:ff") ; - CalibrationRequest calibrationrequest1 ; - Measurement measurement1(&ap1) ; - measurement1.add_ss(1, -78) ; - std::unordered_map measurements ; - measurements["aa:bb:cc:dd:ee:ff"] = measurement1 ; - calibrationrequest1.set_measurements(measurements) ; - ReferencePoint referencepoint1 ; - referencepoint1.add_request(&calibrationrequest1) ; - /* - TS_ASSERT_EQUALS(0, referencepoint1.ss_square_distance( - calibrationrequest1)) ; - */ - - /* Distance computation: - * (-42 - (-78))^2 == 1296 - */ - CalibrationRequest calibrationrequest2 ; - measurement1.clear() ; - measurement1.add_ss(1, -42) ; - measurements["aa:bb:cc:dd:ee:ff"] = measurement1 ; - calibrationrequest2.set_measurements(measurements) ; - /* - TS_ASSERT_EQUALS(1296, referencepoint1.ss_square_distance( - calibrationrequest2)) ; - */ + // delete_requests() + /* Note: as delete_requests() deletes the requests from the Stock, + * we must store them there first. */ + Stock::find_create_calibration_request(calibrationrequest1) ; + Stock::find_create_calibration_request(calibrationrequest2) ; + Stock::find_create_calibration_request(calibrationrequest3) ; + rp1.delete_requests() ; + TS_ASSERT(rp1.get_requests().empty()) ; } diff --git a/owlps-positioner/tests/request_test.hh b/owlps-positioner/tests/request_test.hh index dc6f3b6..fd77590 100644 --- a/owlps-positioner/tests/request_test.hh +++ b/owlps-positioner/tests/request_test.hh @@ -2,6 +2,7 @@ #include "request.hh" #include "mobile.hh" +#include "timestamp.hh" class Request_test: public CxxTest::TestSuite { @@ -9,17 +10,16 @@ public: void test_constructors(void) { - // Default constructor - Request r1 ; - std::unordered_map measurements ; - Request r2(measurements) ; - TS_ASSERT_EQUALS(r1, r2) ; + // We can't test the default constructor with two different objects + // like we usually do because the reception time is initialised by + // the Request constructor. // Copy constructor - struct timespec current_time ; + std::unordered_map measurements ; Measurement meas1 ; - measurements["aa:bb:cc:dd:ee:ff"] = meas1 ; - clock_gettime(CLOCK_REALTIME, ¤t_time) ; + measurements["AA:BB:CC:DD:EE:FF"] = meas1 ; + Timestamp current_time ; + current_time.now() ; Mobile mob1 ; Request r3(&mob1, current_time, measurements) ; Request r4(r3) ; @@ -35,7 +35,7 @@ public: CapturePoint ap1(Point3D(1,2,3), "192.168.0.1", "AA:BB:CC:DD:EE:FF") ; Measurement meas1(&ap1) ; std::unordered_map measurements ; - measurements["aa:bb:cc:dd:ee:ff"] = meas1 ; + measurements["AA:BB:CC:DD:EE:FF"] = meas1 ; Mobile mob1 ; Request r1(&mob1, current_time, measurements) ; TS_ASSERT_EQUALS(r1.get_mobile(), &mob1) ; @@ -43,6 +43,12 @@ public: TS_ASSERT_EQUALS(r1.get_measurements(), measurements) ; // Write & read accessors + r1.set_type(OWL_REQUEST_GENERATED) ; + TS_ASSERT_EQUALS(r1.get_type(), OWL_REQUEST_GENERATED) ; + + r1.set_nb_packets(42) ; + TS_ASSERT_EQUALS(r1.get_nb_packets(), 42) ; + Mobile mob2 ; r1.set_mobile(&mob2) ; TS_ASSERT_EQUALS(r1.get_mobile(), &mob2) ; @@ -51,39 +57,53 @@ public: r1.set_time_sent(current_time) ; TS_ASSERT_EQUALS(r1.get_time_sent(), current_time) ; + current_time.now() ; + r1.received_now() ; + TS_ASSERT_DELTA(static_cast(r1.get_time_received()), + static_cast(current_time), + 100) ; + CapturePoint ap2(Point3D(3,2,1), "192.168.0.2", "AA:BB:CC:DD:EE:02") ; Measurement meas2(&ap2) ; - measurements["aa:bb:cc:dd:ee:02"] = meas2 ; + measurements["AA:BB:CC:DD:EE:02"] = meas2 ; r1.set_measurements(measurements) ; TS_ASSERT_EQUALS(r1.get_measurements(), measurements) ; // clear() r1.clear() ; Request r2 ; + r2.clear() ; TS_ASSERT_EQUALS(r1, r2) ; } + /* + * Operations: + * We don't test similarity() because it's just a wrapper around + * PosUtil functions. + */ + + void test_operators(void) { // == - struct timespec current_time ; + Timestamp current_time ; + current_time.now() ; std::unordered_map measurements ; Measurement meas1 ; - measurements["aa:bb:cc:dd:ee:00"] = meas1 ; - clock_gettime(CLOCK_REALTIME, ¤t_time) ; + measurements["AA:BB:CC:DD:EE:00"] = meas1 ; Request r1(current_time, measurements) ; - Request r2(current_time, measurements) ; + Request r2(r1) ; TS_ASSERT_EQUALS(r1, r2) ; Mobile mob1 ; Request r3(&mob1, current_time, measurements) ; - Request r4(&mob1, current_time, measurements) ; + Request r4(r3) ; TS_ASSERT_EQUALS(r3, r4) ; // != Measurement meas2 ; - measurements["aa:bb:cc:dd:ee:01"] = meas2 ; + measurements["AA:BB:CC:DD:EE:01"] = meas2 ; Request r5(&mob1, current_time, measurements) ; TS_ASSERT(r4 != r5) ; diff --git a/owlps-positioner/tests/result_test.hh b/owlps-positioner/tests/result_test.hh index 85a91cf..fe0898e 100644 --- a/owlps-positioner/tests/result_test.hh +++ b/owlps-positioner/tests/result_test.hh @@ -7,12 +7,12 @@ class Result_test: public CxxTest::TestSuite { public: - void test_constructor(void) + void test_constructors(void) { // Default constructor Result result1 ; Result result2(NULL) ; - Result result3(NULL, Point3D()) ; + Result result3(NULL, "UnknownAlgorithm") ; TS_ASSERT_EQUALS(result1, result2) ; TS_ASSERT_EQUALS(result1, result3) ; @@ -23,14 +23,32 @@ public: void test_accessors(void) { + // We cannot test easily in_which_area() without a complete + // configuration. + Timestamp timestamp1 ; timestamp1.now() ; Request request1(timestamp1) ; Point3D point3d1(3,4,2) ; - Result result1(&request1, point3d1) ; + Result result1(&request1, "MyAlgorithm", point3d1) ; TS_ASSERT_EQUALS(result1.get_position(), point3d1) ; TS_ASSERT_EQUALS(result1.get_request(), &request1) ; + TS_ASSERT_EQUALS(result1.get_error(), -1) ; + } + + void test_error(void) + { + Timestamp timestamp1 ; + timestamp1.now() ; + Request request1(timestamp1) ; + Point3D position(1, 1, 1), real_position(1, 5, 1) ; + Result result1(&request1, "MyAlgorithm", position, real_position) ; + TS_ASSERT_EQUALS(result1.get_error(), 4) ; + + Point3D real_position2(1, 3.5, 1) ; + result1.compute_error(real_position2) ; + TS_ASSERT_EQUALS(result1.get_error(), 2.5) ; } void test_operators(void) diff --git a/owlps-positioner/tests/stock_test.hh b/owlps-positioner/tests/stock_test.hh index 66d3788..7ef3689 100644 --- a/owlps-positioner/tests/stock_test.hh +++ b/owlps-positioner/tests/stock_test.hh @@ -7,11 +7,13 @@ class Stock_test: public CxxTest::TestSuite { public: + // Run before each test void setUp(void) { Stock::clear() ; } + // Run after each test void tearDown(void) { Stock::clear() ; @@ -87,7 +89,7 @@ public: TS_ASSERT_EQUALS(Stock::getw_cp("00:00:00:00:02:01"), ap1) ; CapturePoint ap2(Point3D(1,2,3), "192.168.2.1", "00:00:00:00:02:02", - 8.5, 2.1, 11) ; + 8.5, 2.1, 11) ; Stock::getw_cp("00:00:00:00:02:02") = ap2 ; TS_ASSERT_EQUALS(Stock::get_cp("00:00:00:00:02:02"), ap2) ; TS_ASSERT_EQUALS(Stock::getw_cp("00:00:00:00:02:02"), ap2) ; @@ -130,7 +132,7 @@ public: Measurement measurement1(&ap1) ; measurement1.add_ss(1, -23) ; std::unordered_map measurements ; - measurements["aa:bb:cc:dd:ee:ff"] = measurement1 ; + measurements["AA:BB:CC:DD:EE:FF"] = measurement1 ; calibrationrequest1.set_measurements(measurements) ; TS_ASSERT_THROWS(Stock::closest_reference_point(calibrationrequest1), element_not_found) ; @@ -147,7 +149,7 @@ public: Measurement measurement2(&ap1) ; measurement2.add_ss(1, -78) ; measurements.clear() ; - measurements["aa:bb:cc:dd:ee:ff"] = measurement2 ; + measurements["AA:BB:CC:DD:EE:FF"] = measurement2 ; calibrationrequest2.set_measurements(measurements) ; ReferencePoint referencepoint2(88,99,111) ; referencepoint2.add_request(&calibrationrequest2) ; @@ -164,7 +166,7 @@ public: Measurement measurement3(&ap1) ; measurement3.add_ss(1, -75) ; measurements.clear() ; - measurements["aa:bb:cc:dd:ee:ff"] = measurement3 ; + measurements["AA:BB:CC:DD:EE:FF"] = measurement3 ; request3.set_measurements(measurements) ; TS_ASSERT_EQUALS( &Stock::closest_reference_point(request3), @@ -174,7 +176,7 @@ public: Measurement measurement4(&ap1) ; measurement4.add_ss(1, -8) ; measurements.clear() ; - measurements["aa:bb:cc:dd:ee:ff"] = measurement4 ; + measurements["AA:BB:CC:DD:EE:FF"] = measurement4 ; request4.set_measurements(measurements) ; TS_ASSERT_EQUALS( &Stock::closest_reference_point(request4), diff --git a/owlps-positioner/tests/testsetup.cc b/owlps-positioner/tests/testsetup.cc index 2f113f9..3a3d761 100644 --- a/owlps-positioner/tests/testsetup.cc +++ b/owlps-positioner/tests/testsetup.cc @@ -5,6 +5,7 @@ static TestSetUp testsetup ; +// This is called before each test suite bool TestSetUp::setUpWorld(void) { TestUtil::set_up() ; @@ -12,6 +13,7 @@ bool TestSetUp::setUpWorld(void) } +// This is called after each test suite bool TestSetUp::tearDownWorld(void) { TestUtil::tear_down() ; diff --git a/owlps-positioner/tests/testutil.cc b/owlps-positioner/tests/testutil.cc index 3bbc913..241f287 100644 --- a/owlps-positioner/tests/testutil.cc +++ b/owlps-positioner/tests/testutil.cc @@ -1,6 +1,8 @@ #include "testutil.hh" #include +#include "stock.hh" +#include "userinterface.hh" #include "calibrationrequest.hh" #include "realposition.hh" @@ -19,6 +21,8 @@ vector TestUtil::reference_points ; void TestUtil::tear_down() { + Stock::clear() ; + aps.clear() ; mobiles.clear() ; @@ -39,6 +43,7 @@ void TestUtil::set_up() create_cp_list() ; create_request_list() ; create_result_list() ; + setup_configuration() ; } void TestUtil::create_mobile_list() @@ -154,6 +159,23 @@ void TestUtil::create_result_list() } } +void TestUtil::setup_configuration() +{ + const std::string config_file_name("/dev/null") ; + const char *argv[] = + { + "owlps-positionerd", // program name + "--config-file", config_file_name.c_str(), + "--positioning.area-start", "0;0;0", + "--positioning.area-stop", "10;15;3", + "--positioning.accept-new-mobiles", "true", + "--positioning.accept-new-cps", "true" + } ; + int argc = 11 ; + + UserInterface ui(argc, argv) ; +} + // Create the file output_file_name and fill it with the contents of // output_lines @@ -189,53 +211,72 @@ create_test_csv_file(const string &file_name, bool with_spaces) // Fill name and contents of the test CSV file vector csv_lines ; ostringstream line ; + constexpr unsigned int csv_format = OWL_LATEST_AGGREGATION_CSV_FORMAT ; + + /* The CSV format to use is documented in the header of InputCSV */ if (with_spaces) line << "\n \n " ; - line << mobiles[0].get_mac_addr() ; + line << csv_format ; + line << ';' << mobiles[0].get_mac_addr() ; + line << ';' << static_cast(requests.at(0)->get_type()) ; + line << ';' << requests.at(0)->get_nb_packets() ; line << ';' << requests.at(0)->get_time_sent() ; line << ";0;0;0;0;" ; line << aps[0].get_mac_addr() ; + line << ';' << 1 ; line << ';' << requests.at(0)->get_measurements() .at(aps[0].get_mac_addr()).get_ss(1) ; line << ';' << aps[2].get_mac_addr() ; + line << ';' << 1 ; line << ';' << requests.at(0)->get_measurements() .at(aps[2].get_mac_addr()).get_ss(1) ; line << ';' << aps[1].get_mac_addr() ; + line << ';' << 1 ; line << ';' << requests.at(0)->get_measurements() .at(aps[1].get_mac_addr()).get_ss(1) ; line << ';' << aps[0].get_mac_addr() ; + line << ';' << 2 ; line << ';' << requests.at(0)->get_measurements() .at(aps[0].get_mac_addr()).get_ss(2) ; line << ';' << aps[1].get_mac_addr() ; + line << ';' << 2 ; line << ';' << requests.at(0)->get_measurements() .at(aps[1].get_mac_addr()).get_ss(2) ; line << '\n' ; csv_lines.push_back(line.str()) ; line.str("") ; - line << mobiles[1].get_mac_addr() ; + line << csv_format ; + line << ';' << mobiles[1].get_mac_addr() ; CalibrationRequest *calibration_request = dynamic_cast(requests.at(1)) ; assert(calibration_request) ; + line << ';' << static_cast(calibration_request->get_type()) ; + line << ';' << calibration_request->get_nb_packets() ; line << ';' << calibration_request->get_time_sent() ; line << ';' << reference_points.at(0).get_x() ; line << ';' << reference_points.at(0).get_y() ; line << ';' << reference_points.at(0).get_z() ; line << ';' << static_cast(calibration_request->get_direction()) ; line << ';' << aps[2].get_mac_addr() ; + line << ';' << 1 ; line << ';' << calibration_request->get_measurements() .at(aps[2].get_mac_addr()).get_ss(1) ; line << ';' << aps[0].get_mac_addr() ; + line << ';' << 1 ; line << ';' << calibration_request->get_measurements() .at(aps[0].get_mac_addr()).get_ss(1) ; line << ';' << aps[1].get_mac_addr() ; + line << ';' << 1 ; line << ';' << calibration_request->get_measurements() .at(aps[1].get_mac_addr()).get_ss(1) ; line << ';' << aps[1].get_mac_addr() ; + line << ';' << 2 ; line << ';' << calibration_request->get_measurements() .at(aps[1].get_mac_addr()).get_ss(2) ; line << ';' << aps[0].get_mac_addr() ; + line << ';' << 2 ; line << ';' << calibration_request->get_measurements() .at(aps[0].get_mac_addr()).get_ss(2) ; line << '\n' ; @@ -249,22 +290,30 @@ create_test_csv_file(const string &file_name, bool with_spaces) line.str("") ; if (with_spaces) line << '\t' ; - line << mobiles[0].get_mac_addr() ; + line << csv_format ; + line << ';' << mobiles[0].get_mac_addr() ; + line << ';' << static_cast(requests.at(2)->get_type()) ; + line << ';' << requests.at(2)->get_nb_packets() ; line << ';' << requests.at(2)->get_time_sent() ; line << ";0;0;0;0;" ; line << aps[2].get_mac_addr() ; + line << ';' << 1 ; line << ';' << requests.at(2)->get_measurements() .at(aps[2].get_mac_addr()).get_ss(1) ; line << ';' << aps[1].get_mac_addr() ; + line << ';' << 1 ; line << ';' << requests.at(2)->get_measurements() .at(aps[1].get_mac_addr()).get_ss(1) ; line << ';' << aps[0].get_mac_addr() ; + line << ';' << 1 ; line << ';' << requests.at(2)->get_measurements() .at(aps[0].get_mac_addr()).get_ss(1) ; line << ';' << aps[1].get_mac_addr() ; + line << ';' << 2 ; line << ';' << requests.at(2)->get_measurements() .at(aps[1].get_mac_addr()).get_ss(2) ; line << ';' << aps[0].get_mac_addr() ; + line << ';' << 2 ; line << ';' << requests.at(2)->get_measurements() .at(aps[0].get_mac_addr()).get_ss(2) ; line << '\n' ; diff --git a/owlps-positioner/tests/testutil.hh b/owlps-positioner/tests/testutil.hh index 3f7cc80..3ee9142 100644 --- a/owlps-positioner/tests/testutil.hh +++ b/owlps-positioner/tests/testutil.hh @@ -19,6 +19,7 @@ private: static void create_cp_list(void) ; static void create_request_list(void) ; static void create_result_list(void) ; + static void setup_configuration(void) ; public: static std::vector aps ; diff --git a/owlps-positioner/tests/textfilereader_test.hh b/owlps-positioner/tests/textfilereader_test.hh index f291e8d..971f7a2 100644 --- a/owlps-positioner/tests/textfilereader_test.hh +++ b/owlps-positioner/tests/textfilereader_test.hh @@ -41,7 +41,7 @@ public: output_file.close() ; std::string line ; - TextFileReader *textfilereader ; + TextFileReader *textfilereader = NULL ; TS_ASSERT_THROWS_NOTHING( textfilereader = new TextFileReader(test_file_name)) ; TS_ASSERT(textfilereader->read_line(line)) ; diff --git a/owlps-positioner/tests/textfilewriter_test.hh b/owlps-positioner/tests/textfilewriter_test.hh index 16f368d..64055d0 100644 --- a/owlps-positioner/tests/textfilewriter_test.hh +++ b/owlps-positioner/tests/textfilewriter_test.hh @@ -35,7 +35,7 @@ public: void test_textfilewriter(void) { - TextFileWriter *textfilewriter ; + TextFileWriter *textfilewriter = NULL ; TS_ASSERT_THROWS_NOTHING( textfilewriter = new TextFileWriter(test_file_name)) ; TS_ASSERT(textfilewriter->write_text("Hello\nWorld\n")) ; diff --git a/owlps-positioner/tests/timestamp_test.hh b/owlps-positioner/tests/timestamp_test.hh index 325b11d..55ae4d2 100644 --- a/owlps-positioner/tests/timestamp_test.hh +++ b/owlps-positioner/tests/timestamp_test.hh @@ -39,7 +39,8 @@ public: TS_ASSERT_EQUALS(timestamp1, timestamp4) ; // Equality - TS_ASSERT_EQUALS(static_cast(timestamp1), timespec1) ; + TS_ASSERT_EQUALS(static_cast(timestamp1), + timespec1) ; TS_ASSERT_EQUALS(timestamp1, timespec1) ; TS_ASSERT_EQUALS(static_cast(timestamp1), msec) ; TS_ASSERT_EQUALS(timestamp1, msec) ; @@ -48,7 +49,7 @@ public: void test_non_zero(void) { uint64_t sec = 1234567 ; - uint64_t nsec = 891234567 ; // Do not put a value >= 1 sec. for the test! + uint64_t nsec = 891234567 ; // Don't put a value >= 1 sec. for the test! uint64_t msec = sec * 1000 + nsec / 1000000 ; struct timespec timespec1 ; timespec1.tv_sec = sec ; @@ -56,19 +57,23 @@ public: // struct timespec constructor Timestamp timestamp1(timespec1) ; - timespec1.tv_nsec = nsec / 1000000 * 1000000 ; // Round to ms - TS_ASSERT_EQUALS(static_cast(timestamp1), timespec1) ; + TS_ASSERT_EQUALS(static_cast(timestamp1), + timespec1) ; TS_ASSERT_EQUALS(timestamp1, timespec1) ; TS_ASSERT_EQUALS(static_cast(timestamp1), msec) ; TS_ASSERT_EQUALS(timestamp1, msec) ; - // ms constructor + // ms constructor (deprecated) Timestamp timestamp2(msec) ; - TS_ASSERT_EQUALS(static_cast(timestamp2), timespec1) ; + timespec1.tv_nsec = nsec / 1000000 * 1000000 ; // Round to ms + TS_ASSERT_EQUALS(static_cast(timestamp2), + timespec1) ; TS_ASSERT_EQUALS(timestamp2, timespec1) ; TS_ASSERT_EQUALS(static_cast(timestamp2), msec) ; TS_ASSERT_EQUALS(timestamp2, msec) ; - TS_ASSERT_EQUALS(timestamp1, timestamp2) ; + + // ms and ns give different values + TS_ASSERT_DIFFERS(timestamp1, timestamp2) ; // Copy constructor Timestamp timestamp3(timestamp1) ; @@ -77,6 +82,8 @@ public: void test_current_time(void) { + // Note: we could test update_current_time() in replay mode. + Timestamp timestamp1 ; struct timespec current_time ; @@ -86,18 +93,28 @@ public: TS_FAIL("Error getting current time!") ; return ; } - + const Timestamp &now = Timestamp::get_current_time() ; Timestamp timestamp2(current_time) ; - TS_ASSERT_DELTA(static_cast(timestamp1), static_cast(timestamp2), 100) ; + TS_ASSERT_DELTA(static_cast(timestamp2), + static_cast(now), + 100) ; - // The following tests may fail if the ns value is a ms - TS_ASSERT_DIFFERS(static_cast(timestamp1), current_time) ; - TS_ASSERT(timestamp1 != current_time) ; - TS_ASSERT_DIFFERS(static_cast(timestamp2), current_time) ; - TS_ASSERT(timestamp2 != current_time) ; + TS_ASSERT_DIFFERS(static_cast(timestamp1), + current_time) ; + TS_ASSERT_EQUALS(static_cast(timestamp2), + current_time) ; + + // elapsed() + Timestamp elapsed11(timestamp1.elapsed(timestamp1)) ; + TS_ASSERT_EQUALS(static_cast(elapsed11), 0) ; + TS_ASSERT(! elapsed11) ; + Timestamp elapsed12(timestamp1.elapsed(timestamp2)) ; + TS_ASSERT(static_cast(elapsed12) < 100) ; + Timestamp elapsed21(timestamp2.elapsed(timestamp1)) ; + TS_ASSERT_EQUALS(elapsed12, elapsed21) ; } void test_affectation(void) @@ -111,26 +128,28 @@ public: // struct timespec constructor Timestamp timestamp1(current_time) ; - TS_ASSERT_DIFFERS(static_cast(timestamp1), current_time) ; - TS_ASSERT(timestamp1 != current_time) ; + TS_ASSERT_EQUALS(static_cast(timestamp1), + current_time) ; - // ms constructor + // ms constructor (deprecated) current_time.tv_nsec = current_time.tv_nsec / 1000000 * 1000000 ; uint64_t msec = timestamp1 ; Timestamp timestamp2(msec) ; TS_ASSERT_EQUALS(static_cast(timestamp2), msec) ; TS_ASSERT_EQUALS(timestamp2, msec) ; - TS_ASSERT_EQUALS(static_cast(timestamp2), current_time) ; + TS_ASSERT_EQUALS(static_cast(timestamp2), + current_time) ; TS_ASSERT_EQUALS(timestamp2, current_time) ; // Affectation ++current_time.tv_sec ; timestamp2 = current_time ; - TS_ASSERT(timestamp1 != timestamp2) ; + TS_ASSERT_DIFFERS(timestamp1, timestamp2) ; msec = timestamp2 ; TS_ASSERT_EQUALS(static_cast(timestamp2), msec) ; TS_ASSERT_EQUALS(timestamp2, msec) ; - TS_ASSERT_EQUALS(static_cast(timestamp2), current_time) ; + TS_ASSERT_EQUALS(static_cast(timestamp2), + current_time) ; TS_ASSERT_EQUALS(timestamp2, current_time) ; msec = 1234567891234ull ; @@ -138,7 +157,8 @@ public: TS_ASSERT_EQUALS(static_cast(timestamp2), msec) ; TS_ASSERT_EQUALS(timestamp2, msec) ; current_time = timestamp2 ; - TS_ASSERT_EQUALS(static_cast(timestamp2), current_time) ; + TS_ASSERT_EQUALS(static_cast(timestamp2), + current_time) ; TS_ASSERT_EQUALS(timestamp2, current_time) ; } @@ -170,17 +190,17 @@ public: TS_ASSERT(! (today > today_timespec)) ; TS_ASSERT(! (today < today_timespec)) ; - TS_ASSERT(today != yesterday) ; + TS_ASSERT_DIFFERS(today, yesterday) ; TS_ASSERT_LESS_THAN(yesterday, today) ; TS_ASSERT_LESS_THAN_EQUALS(yesterday, today) ; TS_ASSERT(! (yesterday > today)) ; TS_ASSERT(! (yesterday >= today)) ; - TS_ASSERT(today != yesterday_int) ; + TS_ASSERT_DIFFERS(today, yesterday_int) ; TS_ASSERT_LESS_THAN(yesterday, today_int) ; TS_ASSERT_LESS_THAN_EQUALS(yesterday, today_int) ; TS_ASSERT(! (yesterday > today_int)) ; TS_ASSERT(! (yesterday >= today_int)) ; - TS_ASSERT(today != yesterday_timespec) ; + TS_ASSERT_DIFFERS(today, yesterday_timespec) ; TS_ASSERT_LESS_THAN(yesterday, today_timespec) ; TS_ASSERT_LESS_THAN_EQUALS(yesterday, today_timespec) ; TS_ASSERT(! (yesterday > today_timespec)) ; @@ -198,9 +218,9 @@ public: Timestamp today ; today.now() ; - TS_ASSERT(today != zero) ; - TS_ASSERT(today != zero_timespec) ; - TS_ASSERT(today != zero_timestamp) ; + TS_ASSERT_DIFFERS(today, zero) ; + TS_ASSERT_DIFFERS(today, zero_timespec) ; + TS_ASSERT_DIFFERS(today, zero_timestamp) ; today.clear() ; TS_ASSERT_EQUALS(today, zero) ; diff --git a/owlps-positioner/tests/topologyreadercsv_test.hh b/owlps-positioner/tests/topologyreadercsv_test.hh index 95b9863..203c187 100644 --- a/owlps-positioner/tests/topologyreadercsv_test.hh +++ b/owlps-positioner/tests/topologyreadercsv_test.hh @@ -14,8 +14,6 @@ public: areas_file_name("/tmp/TopologyReaderCSV_areas_file.csv"), waypoints_file_name("/tmp/TopologyReaderCSV_waypoints_file.csv") { - Stock::clear() ; - std::ofstream areas_file(areas_file_name.c_str()) ; areas_file << "My building;My room #1;1;2;3;9;8;7\n" @@ -43,7 +41,6 @@ public: ~TopologyReaderCSV_test(void) { - Stock::clear() ; TestUtil::remove_file(areas_file_name) ; TestUtil::remove_file(waypoints_file_name) ; } @@ -63,11 +60,11 @@ public: void test_areas(void) { - Building *building1 ; + Building *building1 = NULL ; TS_ASSERT_THROWS_NOTHING( building1 = const_cast( &Stock::get_building("My building"))) ; - Building *building2 ; + Building *building2 = NULL ; TS_ASSERT_THROWS_NOTHING( building2 = const_cast( &Stock::get_building("Building #2"))) ; @@ -97,11 +94,11 @@ public: void test_waypoints(void) { - Building *building1 ; + Building *building1 = NULL ; TS_ASSERT_THROWS_NOTHING( building1 = const_cast( &Stock::get_building("My building"))) ; - Building *building2 ; + Building *building2 = NULL ; TS_ASSERT_THROWS_NOTHING( building2 = const_cast( &Stock::get_building("Building #2"))) ; diff --git a/owlps-positioner/tests/userinterface_test.hh b/owlps-positioner/tests/userinterface_test.hh index 6ef6795..456ab8d 100644 --- a/owlps-positioner/tests/userinterface_test.hh +++ b/owlps-positioner/tests/userinterface_test.hh @@ -62,7 +62,7 @@ public: } ; int argc = 3 ; - UserInterface ui(argc, const_cast(argv)) ; + UserInterface ui(argc, argv) ; TS_ASSERT(Configuration::is_configured("input.udp-port")) ; TS_ASSERT(Configuration::is_configured("config-file")) ;