From 0ddef4c7473899467b15668e8425d24f2c05933e Mon Sep 17 00:00:00 2001 From: Matteo Cypriani Date: Thu, 11 Feb 2010 11:15:57 +0100 Subject: [PATCH] [Positioning] Add TestUtil, utilitary test class Add the class TestUtil, that provides functions used in several unit tests. For the moment, it contains two function: - fill_file(), that creates and fills a test file. - remove_file(), that deletes a test file. --- owlps-positioning/.gitignore | 2 +- owlps-positioning/Makefile | 11 ++++-- owlps-positioning/TODO | 5 --- owlps-positioning/tests/inputcsv_test.hh | 23 ++++--------- owlps-positioning/tests/testutil.cc | 34 +++++++++++++++++++ owlps-positioning/tests/testutil.hh | 15 ++++++++ owlps-positioning/tests/userinterface_test.hh | 21 +++--------- 7 files changed, 68 insertions(+), 43 deletions(-) create mode 100644 owlps-positioning/tests/testutil.cc create mode 100644 owlps-positioning/tests/testutil.hh diff --git a/owlps-positioning/.gitignore b/owlps-positioning/.gitignore index 9e8be5e..eed7d3b 100644 --- a/owlps-positioning/.gitignore +++ b/owlps-positioning/.gitignore @@ -1,5 +1,5 @@ doc csv/*.csv -tests/*.cc +tests/tests.cc tests/tests owlps-positioning diff --git a/owlps-positioning/Makefile b/owlps-positioning/Makefile index 86e92d7..58cdd0c 100644 --- a/owlps-positioning/Makefile +++ b/owlps-positioning/Makefile @@ -3,6 +3,7 @@ TEST_DIR = tests TEST_XX = $(TEST_DIR)/tests.cc TEST_OBJ = $(TEST_DIR)/tests.o +TESTUTIL_OBJ = $(TEST_DIR)/testutil.o TEST_TARGET = $(TEST_DIR)/tests DOXYGEN_DIR = doc DOXYFILE = Doxyfile @@ -64,15 +65,19 @@ inputcsv.o: inputmedium.hh request.o stock.o # Specific targets $(TARGET): $(OBJ) -$(TEST_XX): $(OBJ:%.o=$(TEST_DIR)/%_test.hh) $(TEST_DIR)/valuetraits.hh +$(TEST_XX): $(OBJ:%.o=$(TEST_DIR)/%_test.hh) $(TEST_DIR)/cxxtestgen.pl --error-printer \ --include=$(TEST_DIR)/valuetraits.hh \ + --include=$(TESTUTIL_OBJ:.o=.hh) \ -o $@ $^ -$(TEST_OBJ): $(TEST_XX) $(OBJ) +$(TEST_OBJ): $(TEST_XX) $(OBJ) $(TESTUTIL_OBJ) $(TEST_DIR)/valuetraits.hh $(GXX) $(GXXFLAGS) $(TESTSGXXFLAGS) -o $@ -c $< -$(TEST_TARGET): $(TEST_OBJ) +$(TESTUTIL_OBJ): $(TESTUTIL_OBJ:.o=.cc) $(TESTUTIL_OBJ:.o=.hh) + $(GXX) $(GXXFLAGS) $(TESTSGXXFLAGS) -o $@ -c $< + +$(TEST_TARGET): $(TEST_OBJ) $(TESTUTIL_OBJ) $(LD) $(LDFLAGS) -o $@ $^ $(OBJ) test: $(TEST_TARGET) diff --git a/owlps-positioning/TODO b/owlps-positioning/TODO index 6b5fe18..b4d4516 100644 --- a/owlps-positioning/TODO +++ b/owlps-positioning/TODO @@ -4,11 +4,6 @@ utilisant les champs de direction. ° Lire la direction en tant qu'entier plutôt que float ? -- Tests unitaires - Écrire une classe utilitaire pour les tests contenant par exemple - une fonction pour écrire un fichier de test à partir d'un - vector. - - Réorganisation du dépôt ? owlps-positioning/src owlps-positioning/include diff --git a/owlps-positioning/tests/inputcsv_test.hh b/owlps-positioning/tests/inputcsv_test.hh index 1095b37..3063417 100644 --- a/owlps-positioning/tests/inputcsv_test.hh +++ b/owlps-positioning/tests/inputcsv_test.hh @@ -24,12 +24,12 @@ public: // Clear the stock Stock::clear() ; - // Creating AP list + // Create 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 + // Fill name and contents of the test CSV file csv_file_name = "/tmp/InputCSV_test_csv_file.csv" ; csv_lines.push_back("\ \n \n\ @@ -60,18 +60,8 @@ aa:bb:cc:dd:ee:77;1265120911234;0.00;0.00;0.00;0\ ;" + aps[0].get_mac_addr() + ";-32\ \n\n\t\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() ; + // Create and fill the test CSV file + TestUtil::fill_file(csv_file_name, csv_lines) ; // Back to the normal behaviour (i.e. do not abort on fail) CxxTest::setAbortTestOnFail(false) ; @@ -80,9 +70,8 @@ aa:bb:cc:dd:ee:77;1265120911234;0.00;0.00;0.00;0\ ~InputCSV_test(void) { - // Deleting the test CSV file - if (remove(csv_file_name.c_str()) == -1) - TS_WARN("Cannot remove test CSV file!") ; + // Delete the test CSV file + TestUtil::remove_file(csv_file_name) ; // Clear the stock Stock::clear() ; diff --git a/owlps-positioning/tests/testutil.cc b/owlps-positioning/tests/testutil.cc new file mode 100644 index 0000000..66656e9 --- /dev/null +++ b/owlps-positioning/tests/testutil.cc @@ -0,0 +1,34 @@ +#include "testutil.hh" +#include + +#include + +using namespace std ; + + +// Create the file output_file_name and fill it with the contents of +// output_lines +void TestUtil::fill_file(const string &output_file_name, + const vector output_lines) +{ + // Open the file + std::ofstream output_file ; + output_file.open(output_file_name.c_str()) ; + if (! output_file) + TS_FAIL("Cannot open test file `"+ output_file_name + +"` for creation!") ; + + // Write contents to the file + for (std::vector::const_iterator i = output_lines.begin() ; + i != output_lines.end() ; ++i) + output_file << *i ; + + output_file.close() ; +} + + +void TestUtil::remove_file(const string &file_name) +{ + if (remove(file_name.c_str()) == -1) + TS_WARN("Cannot remove test file `"+ file_name +"`!") ; +} diff --git a/owlps-positioning/tests/testutil.hh b/owlps-positioning/tests/testutil.hh new file mode 100644 index 0000000..64fa526 --- /dev/null +++ b/owlps-positioning/tests/testutil.hh @@ -0,0 +1,15 @@ +#ifndef _OWLPS_POSITIONING_TESTS_TESTUTIL_HH_ +#define _OWLPS_POSITIONING_TESTS_TESTUTIL_HH_ + +#include +#include + +class TestUtil +{ +public: + static void fill_file(const std::string &output_file_name, + const std::vector output_lines) ; + static void remove_file(const std::string &file_name) ; +} ; + +#endif // _OWLPS_POSITIONING_TESTS_TESTUTIL_HH_ diff --git a/owlps-positioning/tests/userinterface_test.hh b/owlps-positioning/tests/userinterface_test.hh index b46f601..269b04e 100644 --- a/owlps-positioning/tests/userinterface_test.hh +++ b/owlps-positioning/tests/userinterface_test.hh @@ -18,26 +18,14 @@ public: // If we cannot open the file, we want to stop the test CxxTest::setAbortTestOnFail(true) ; - // Filling name and contents of the config test file + // Fill name and contents of the test config file config_file_name = "/tmp/UserInterface_test_config_file.csv" ; config_lines.push_back("\n") ; config_lines.push_back("[server]\n") ; config_lines.push_back("\n") ; config_lines.push_back("port = 42\n") ; config_lines.push_back("\n") ; - - // Opening the file - std::ofstream config_file ; - config_file.open(config_file_name.c_str()) ; - if (! config_file) - TS_FAIL("Cannot open test config file for creation!") ; - - // Writing contents to the file - for (std::vector::const_iterator i = config_lines.begin() ; - i != config_lines.end() ; ++i) - config_file << *i ; - - config_file.close() ; + TestUtil::fill_file(config_file_name, config_lines) ; // Back to the normal behaviour (i.e. do not abort on fail) CxxTest::setAbortTestOnFail(false) ; @@ -46,9 +34,8 @@ public: ~UserInterface_test(void) { - // Deleting the test config file - if (remove(config_file_name.c_str()) == -1) - TS_WARN("Cannot remove test config file!") ; + // Delete the test config file + TestUtil::remove_file(config_file_name) ; }