From ec10ed71a104c90bfa747d59e27c147ba68bda45 Mon Sep 17 00:00:00 2001 From: Matteo Cypriani Date: Tue, 2 Feb 2010 17:34:58 +0100 Subject: [PATCH] [Positioning] Add test for InputCSV - Add unit test for InputCSV. - Request: Add get_measurements(). - Stock: - Add clear(). - Update tests to use it. --- owlps-positioning/TODO | 5 +- owlps-positioning/request.hh | 10 ++ owlps-positioning/stock.cc | 11 ++ owlps-positioning/stock.hh | 3 + owlps-positioning/tests/inputcsv_test.hh | 206 +++++++++++++++++++++++ owlps-positioning/tests/stock_test.hh | 18 ++ 6 files changed, 251 insertions(+), 2 deletions(-) diff --git a/owlps-positioning/TODO b/owlps-positioning/TODO index f8b4370..4d7c29d 100644 --- a/owlps-positioning/TODO +++ b/owlps-positioning/TODO @@ -6,8 +6,9 @@ ° Différencier une requête normale d'une requête de calibration, en utilisant les champs de direction. ° Lire la direction en tant qu'entier plutôt que float ? - -- Tests unitaires + ° Fermer le fichier lorsqu'on a atteint la fin. + ° Faire en sorte que eof() soit vrai dès qu'on a lu la dernière + ligne (skip blanks, etc.). - Affichage de debug Garder (generaliser) ? Supprimer ? diff --git a/owlps-positioning/request.hh b/owlps-positioning/request.hh index b64c3e7..d2bc551 100644 --- a/owlps-positioning/request.hh +++ b/owlps-positioning/request.hh @@ -46,6 +46,9 @@ public: Mobile* get_mobile(void) const ; /// #timestamp read accessor const struct timespec& get_timestamp(void) const ; + /// #measurements read accessor + const std::tr1::unordered_map& + get_measurements(void) const ; //@} /** @name Write accessors */ @@ -90,6 +93,13 @@ inline const struct timespec& Request::get_timestamp() const } +inline const std::tr1::unordered_map& +Request::get_measurements(void) const +{ + return measurements ; +} + + /* *** Write accessors *** */ diff --git a/owlps-positioning/stock.cc b/owlps-positioning/stock.cc index 5d592ff..fbd4e1b 100644 --- a/owlps-positioning/stock.cc +++ b/owlps-positioning/stock.cc @@ -50,3 +50,14 @@ const AccessPoint& Stock::get_ap(const string &mac) return i->second ; throw out_of_range("No AccessPoint with MAC address « " + mac + " »!") ; } + + + +/* *** Write accessors *** */ + + +void Stock::clear() +{ + mobiles.clear() ; + aps.clear() ; +} diff --git a/owlps-positioning/stock.hh b/owlps-positioning/stock.hh index b43ef5d..5a8a1c8 100644 --- a/owlps-positioning/stock.hh +++ b/owlps-positioning/stock.hh @@ -26,6 +26,9 @@ public: static const AccessPoint& get_ap(const std::string &mac) ; /// Get a reference to the AccessPoint corresponding to a given MAC address static AccessPoint& getw_ap(const std::string &mac) ; + + /// Deletes all elements in all attributes + static void clear(void) ; } ; diff --git a/owlps-positioning/tests/inputcsv_test.hh b/owlps-positioning/tests/inputcsv_test.hh index e69de29..0b9df8a 100644 --- a/owlps-positioning/tests/inputcsv_test.hh +++ b/owlps-positioning/tests/inputcsv_test.hh @@ -0,0 +1,206 @@ +#include + +#include "inputcsv.hh" +#include "stock.hh" + +#include +#include +#include + +class InputCSV_test: public CxxTest::TestSuite +{ +private: + std::string csv_file_name ; // Test CSV file name + std::vector csv_lines ; // Test CSV file contents + std::vector aps ; // List of test AccessPoint + + +public: + InputCSV_test(void) + { + // If we cannot open the file, we want to stop the test + CxxTest::setAbortTestOnFail(true) ; + + // Clear the stock + Stock::clear() ; + + // Creating AP list + aps.push_back(AccessPoint(Point3D(1,2,3), "11:22:33:44:55:01")) ; + aps.push_back(AccessPoint(Point3D(4,5,6), "11:22:33:44:55:02")) ; + aps.push_back(AccessPoint(Point3D(7,8,9), "11:22:33:44:55:03")) ; + + // Filling name and contents of the CSV test file + csv_file_name = "/tmp/InputCSV_test_csv_file.csv" ; + csv_lines.push_back("\ +aa:bb:cc:dd:ee:ff;1265120910725;0.00;0.00;0.00;0\ +;" + aps[0].get_mac_addr() + ";-58\ +;" + aps[2].get_mac_addr() + ";-50\ +;" + aps[1].get_mac_addr() + ";-42\ +;" + aps[0].get_mac_addr() + ";-55\ +;" + aps[1].get_mac_addr() + ";-37\ +\n") ; + csv_lines.push_back("\ +aa:bb:cc:dd:ee:77;1265120911234;0.00;0.00;0.00;0\ +;" + aps[2].get_mac_addr() + ";-59\ +;" + aps[0].get_mac_addr() + ";-51\ +;" + aps[1].get_mac_addr() + ";-70\ +;" + aps[1].get_mac_addr() + ";-21\ +;" + aps[0].get_mac_addr() + ";-19\ +\n") ; + csv_lines.push_back("\ +aa:bb:cc:dd:ee:ff;1265120912345;0.00;0.00;0.00;0\ +;" + aps[2].get_mac_addr() + ";-56\ +;" + aps[1].get_mac_addr() + ";-45\ +;" + aps[0].get_mac_addr() + ";-54\ +;" + aps[1].get_mac_addr() + ";-23\ +;" + aps[0].get_mac_addr() + ";-32\ +\n") ; + + // Opening the file + std::ofstream csv_file ; + csv_file.open(csv_file_name.c_str()) ; + if (! csv_file) + TS_FAIL("Cannot open test CSV file for creation!") ; + + // Writing contents to the file + for (std::vector::const_iterator i = csv_lines.begin() ; + i != csv_lines.end() ; ++i) + csv_file << *i ; + + csv_file.close() ; + + // Back to the normal behaviour (i.e. do not abort on fail) + CxxTest::setAbortTestOnFail(false) ; + } + + + ~InputCSV_test(void) + { + // Deleting the test CSV file + if (remove(csv_file_name.c_str()) == -1) + TS_WARN("Cannot remove test CSV file!") ; + + // Clear the stock + Stock::clear() ; + } + + + static InputCSV_test* createSuite(void) + { + return new InputCSV_test() ; + } + + + static void destroySuite(InputCSV_test *suite) + { + delete suite ; + } + + + void test_inputcsv(void) + { + // Opening file + InputCSV inputcsv1(csv_file_name) ; + TS_ASSERT(inputcsv1) ; + TS_ASSERT(! inputcsv1.eof()) ; + + Request request1 ; + std::tr1::unordered_map measurements ; + + // First Request // + + request1 = inputcsv1.get_next_request() ; + TS_ASSERT_EQUALS(request1.get_mobile()->get_mac_addr(), + "aa:bb:cc:dd:ee:ff") ; + TS_ASSERT_EQUALS(PosUtil::timespec_to_ms(request1.get_timestamp()), + 1265120910725llu) ; + + measurements[aps[0].get_mac_addr()].add_ss(-58) ; + measurements[aps[2].get_mac_addr()].add_ss(-50) ; + measurements[aps[1].get_mac_addr()].add_ss(-42) ; + measurements[aps[0].get_mac_addr()].add_ss(-55) ; + measurements[aps[1].get_mac_addr()].add_ss(-37) ; + + for (std::vector::const_iterator i = aps.begin() ; + i != aps.end() ; ++i) + { + std::tr1::unordered_map::const_iterator + measurement_it1 = + request1.get_measurements().find(i->get_mac_addr()) ; + TS_ASSERT_DIFFERS(request1.get_measurements().end(), measurement_it1) ; + TS_ASSERT_EQUALS(measurements[i->get_mac_addr()].get_ss_list(), + measurement_it1->second.get_ss_list()) ; + } + + TS_ASSERT(inputcsv1) ; + TS_ASSERT(! inputcsv1.eof()) ; + + // Second Request // + + request1 = inputcsv1.get_next_request() ; + TS_ASSERT_EQUALS(request1.get_mobile()->get_mac_addr(), + "aa:bb:cc:dd:ee:77") ; + TS_ASSERT_EQUALS(PosUtil::timespec_to_ms(request1.get_timestamp()), + 1265120911234llu) ; + + measurements.clear() ; + measurements[aps[2].get_mac_addr()].add_ss(-59) ; + measurements[aps[0].get_mac_addr()].add_ss(-51) ; + measurements[aps[1].get_mac_addr()].add_ss(-70) ; + measurements[aps[1].get_mac_addr()].add_ss(-21) ; + measurements[aps[0].get_mac_addr()].add_ss(-19) ; + + for (std::vector::const_iterator i = aps.begin() ; + i != aps.end() ; ++i) + { + std::tr1::unordered_map::const_iterator + measurement_it1 = + request1.get_measurements().find(i->get_mac_addr()) ; + TS_ASSERT_DIFFERS(request1.get_measurements().end(), measurement_it1) ; + TS_ASSERT_EQUALS(measurements[i->get_mac_addr()].get_ss_list(), + measurement_it1->second.get_ss_list()) ; + } + + // Third Request // + + TS_ASSERT(inputcsv1) ; + TS_ASSERT(! inputcsv1.eof()) ; + + request1 = inputcsv1.get_next_request() ; + TS_ASSERT_EQUALS(request1.get_mobile()->get_mac_addr(), + "aa:bb:cc:dd:ee:ff") ; + TS_ASSERT_EQUALS(PosUtil::timespec_to_ms(request1.get_timestamp()), + 1265120912345llu) ; + + measurements.clear() ; + measurements[aps[2].get_mac_addr()].add_ss(-56) ; + measurements[aps[1].get_mac_addr()].add_ss(-45) ; + measurements[aps[0].get_mac_addr()].add_ss(-54) ; + measurements[aps[1].get_mac_addr()].add_ss(-23) ; + measurements[aps[0].get_mac_addr()].add_ss(-32) ; + + for (std::vector::const_iterator i = aps.begin() ; + i != aps.end() ; ++i) + { + std::tr1::unordered_map::const_iterator + measurement_it1 = + request1.get_measurements().find(i->get_mac_addr()) ; + TS_ASSERT_DIFFERS(request1.get_measurements().end(), measurement_it1) ; + TS_ASSERT_EQUALS(measurements[i->get_mac_addr()].get_ss_list(), + measurement_it1->second.get_ss_list()) ; + } + + // End of file + + TS_WARN("TODO: eof() should be true even before we try to read another request.") ; + TS_ASSERT(inputcsv1) ; + TS_ASSERT(! inputcsv1.eof()) ; + + request1 = inputcsv1.get_next_request() ; + TS_ASSERT(! request1) ; + + TS_ASSERT(! inputcsv1) ; + TS_ASSERT(inputcsv1.eof()) ; + } + +} ; diff --git a/owlps-positioning/tests/stock_test.hh b/owlps-positioning/tests/stock_test.hh index 3650406..4e8ebe5 100644 --- a/owlps-positioning/tests/stock_test.hh +++ b/owlps-positioning/tests/stock_test.hh @@ -8,6 +8,9 @@ public: void test_accessors(void) { + // Clear the stock, in case other test classes filled it + Stock::clear() ; + // Non-existing elements TS_ASSERT_THROWS(Stock::get_mobile("aa:bb:cc:dd:ee:ff"), std::out_of_range) ; @@ -31,6 +34,21 @@ public: Stock::getw_ap("00:00:00:00:02:02") = ap2 ; TS_ASSERT_EQUALS(Stock::get_ap("00:00:00:00:02:02"), ap2) ; TS_ASSERT_EQUALS(Stock::getw_ap("00:00:00:00:02:02"), ap2) ; + + // clear() + Stock::clear() ; + TS_ASSERT_THROWS(Stock::get_mobile("aa:bb:cc:dd:ee:ff"), + std::out_of_range) ; + TS_ASSERT_THROWS(Stock::get_mobile("00:00:00:00:01:01"), + std::out_of_range) ; + TS_ASSERT_THROWS(Stock::get_mobile("00:00:00:00:01:02"), + std::out_of_range) ; + TS_ASSERT_THROWS(Stock::get_ap("aa:bb:cc:dd:ee:ff"), + std::out_of_range) ; + TS_ASSERT_THROWS(Stock::get_ap("00:00:00:00:02:01"), + std::out_of_range) ; + TS_ASSERT_THROWS(Stock::get_ap("00:00:00:00:02:02"), + std::out_of_range) ; } } ;