[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.
This commit is contained in:
Matteo Cypriani 2010-02-11 11:15:57 +01:00
parent c341f852b7
commit 0ddef4c747
7 changed files with 68 additions and 43 deletions

View File

@ -1,5 +1,5 @@
doc
csv/*.csv
tests/*.cc
tests/tests.cc
tests/tests
owlps-positioning

View File

@ -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)

View File

@ -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<string>.
- Réorganisation du dépôt ?
owlps-positioning/src
owlps-positioning/include

View File

@ -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<std::string>::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() ;

View File

@ -0,0 +1,34 @@
#include "testutil.hh"
#include <cxxtest/TestSuite.h>
#include <fstream>
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<string> 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<std::string>::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 +"`!") ;
}

View File

@ -0,0 +1,15 @@
#ifndef _OWLPS_POSITIONING_TESTS_TESTUTIL_HH_
#define _OWLPS_POSITIONING_TESTS_TESTUTIL_HH_
#include <string>
#include <vector>
class TestUtil
{
public:
static void fill_file(const std::string &output_file_name,
const std::vector<std::string> output_lines) ;
static void remove_file(const std::string &file_name) ;
} ;
#endif // _OWLPS_POSITIONING_TESTS_TESTUTIL_HH_

View File

@ -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<std::string>::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) ;
}