[Positioning] Finish unit tests for Area & Point3D

Area:
- Unit test: test inversion of points.
- Add reorder_coordinates().
- Remove all set_{x,y,z}_{min,max}() accessors.
- Remove set_p_min() and set_p_max().
- Add set_coordinates().
- The constructor now takes 2 Point3D instead of 6 float coordinates.

Point3D:
- Unit test: finish to test distances, test new cast operator.
- Add a cast to string operator.

Unit tests:
- Add valuetraits.hh with class to display correctly a Point3D in a
  wrong assertion.
This commit is contained in:
Matteo Cypriani 2010-02-04 18:46:23 +01:00
parent b921263795
commit a9a2331cfa
8 changed files with 173 additions and 148 deletions

View File

@ -13,7 +13,7 @@ INSTALL_DIR = $(PREFIX)/bin
# Commandes d'installation et de désinstallation
RM = rm -fv
RM_RECURSIVE = $(RM) -r
RM_RECURSIVE = \rm -fr
CP = cp -v
# Autres outils
@ -72,8 +72,10 @@ obj: $(OBJ)
$(TARGET): $(OBJ)
$(TEST_XX): $(OBJ:%.o=$(TEST_DIR)/%_test.hh)
$(TEST_DIR)/cxxtestgen.pl --error-printer -o $@ $^
$(TEST_XX): $(OBJ:%.o=$(TEST_DIR)/%_test.hh) $(TEST_DIR)/valuetraits.hh
$(TEST_DIR)/cxxtestgen.pl --error-printer \
--include=$(TEST_DIR)/valuetraits.hh \
-o $@ $^
$(TEST_OBJ): $(TEST_XX) $(OBJ)
$(GXX) $(GXXFLAGS) $(TESTSGXXFLAGS) -o $@ -c $<

View File

@ -9,63 +9,11 @@ using namespace std ;
Area::Area(const Building *_building, const string &_name,
const float &_x1, const float &_y1, const float &_z1,
const float &_x2, const float &_y2, const float &_z2)
const Point3D &p1, const Point3D &p2)
{
building = const_cast<Building*>(_building) ;
name = _name ;
float x_min, x_max, y_min, y_max, z_min, z_max ;
if (_x1 < _x2 && _y1 > _y2) // Le premier point de la pièce
{
// est en haut à gauche
x_min = _x1 ;
x_max = _x2 ;
y_min = _y2 ; // on inverse les Y
y_max = _y1 ;
}
else if (_x1 > _x2 && _y1 < _y2) // Le premier point de la pièce
{
// est en bas à droite
x_min = _x2 ; // on inverse les X
x_max = _x1 ;
y_min = _y1 ;
y_max = _y2 ;
}
else if (_x1 > _x2 && _z1 > _z2) // Le premier point de la pièce
{
// est en haut à droite
x_min = _x2 ; // on inverse les X
x_max = _x1 ;
z_min = _z2 ; // on inverse les Y
z_max = _z1 ;
}
else // Le premier point de la pièce est en bas à gauche
{
// ou autres cas (par ex. coordonnées nulles)
x_min = _x1 ;
x_max = _x2 ;
y_min = _y1 ;
y_max = _y2 ;
}
if (_z1 < _z2)
{
z_min = _z1 ;
z_max = _z2 ;
}
else
{
z_min = _z2 ;
z_max = _z1 ;
}
p_min = Point3D(x_min, y_min, z_min) ;
p_max = Point3D(x_max, y_max, z_max) ;
set_coordinates(p1, p2) ;
}
@ -98,6 +46,84 @@ bool Area::contains_point(const Point3D &p) const
}
/**
* Changes the Area coordinates so that #p_min is the South-West-lower
* point and #p_max the North-East-higher point.
*/
void Area::reorder_coordinates()
{
float
x1 = p_min.get_x(),
y1 = p_min.get_y(),
z1 = p_min.get_z(),
x2 = p_max.get_x(),
y2 = p_max.get_y(),
z2 = p_max.get_z(),
x_min, x_max, y_min,
y_max, z_min, z_max ;
// 2-D coordinates (X and Y) //
// First point is North-West
if (x1 < x2 && y1 > y2)
{
x_min = x1 ;
x_max = x2 ;
y_min = y2 ; // We swap the two Y
y_max = y1 ;
}
// First point is South-East
else if (x1 > x2 && y1 < y2)
{
x_min = x2 ; // We swap the two X
x_max = x1 ;
y_min = y1 ;
y_max = y2 ;
}
// First point is North-East
else if (x1 > x2 && y1 > y2)
{
x_min = x2 ; // We swap the two X
x_max = x1 ;
y_min = y2 ; // and we swap the two Y
y_max = y1 ;
}
// First point is South-West
// (or other cases such as null coordinates)
else
{
// We swap nothing
x_min = x1 ;
x_max = x2 ;
y_min = y1 ;
y_max = y2 ;
}
// Vertical coordinate (Z) //
// First point is lower
if (z1 < z2)
{
z_min = z1 ;
z_max = z2 ;
}
// First point is higher
else
{
// We swap the two Z
z_min = z2 ;
z_max = z1 ;
}
p_min = Point3D(x_min, y_min, z_min) ;
p_max = Point3D(x_max, y_max, z_max) ;
}
/* *** Operators *** */

View File

@ -17,13 +17,15 @@ protected:
Point3D p_min ; ///< First coordinate of the Area
Point3D p_max ; ///< Second coordinate of the Area
void reorder_coordinates(void) ; ///< Reorders #p_min and #p_max
public:
/// \brief Constructs an Area from a Building, a name and two 3-float
/// defined points (or default constructor)
Area(const Building *_building = NULL,
const std::string &_name = "Unnamed area",
const float &_x1 = 0, const float &_y1 = 0, const float &_z1 = 0,
const float &_x2 = 0, const float &_y2 = 0, const float &_z2 = 0) ;
const Point3D &p1 = Point3D(0,0,0),
const Point3D &p2 = Point3D(0,0,0)) ;
/// Copy constructor
Area(const Area &a) ;
@ -43,22 +45,8 @@ public:
void set_building(const Building *_building) ;
/// #name write accessor
void set_name(const std::string &_name) ;
/// Sets the X of #p_min
void set_x_min(const float &v) ;
/// Sets the Y of #p_min
void set_y_min(const float &v) ;
/// Sets the Z of #p_min
void set_z_min(const float &v) ;
/// Sets the X of #p_max
void set_x_max(const float &v) ;
/// Sets the Y of #p_max
void set_y_max(const float &v) ;
/// Sets the Z of #p_max
void set_z_max(const float &v) ;
/// #p_min write accessor
void set_p_min(const Point3D &p) ;
/// #p_max write accessor
void set_p_max(const Point3D &p) ;
/// Sets the coordinates of the Area and reorder them
void set_coordinates(const Point3D &p1, const Point3D &p2) ;
//@}
/** @name Operations */
@ -123,51 +111,11 @@ inline void Area::set_name(const std::string &_name)
}
inline void Area::set_x_min(const float &v)
inline void Area::set_coordinates(const Point3D &p1, const Point3D &p2)
{
p_min.set_x(v) ;
}
inline void Area::set_y_min(const float &v)
{
p_min.set_y(v) ;
}
inline void Area::set_z_min(const float &v)
{
p_min.set_z(v) ;
}
inline void Area::set_x_max(const float &v)
{
p_max.set_x(v) ;
}
inline void Area::set_y_max(const float &v)
{
p_max.set_y(v) ;
}
inline void Area::set_z_max(const float &v)
{
p_max.set_z(v) ;
}
inline void Area::set_p_min(const Point3D &p)
{
p_min = p ;
}
inline void Area::set_p_max(const Point3D &p)
{
p_max = p ;
p_min = p1 ;
p_max = p2 ;
reorder_coordinates() ;
}

View File

@ -1,5 +1,6 @@
#include "point3d.hh"
#include <sstream>
/* *** Constructors *** */
@ -109,6 +110,14 @@ bool Point3D::operator<(const Point3D &source) const
}
Point3D::operator std::string(void) const
{
std::ostringstream oss ;
oss << *this ;
return oss.str() ;
}
std::ostream& operator<<(std::ostream &os, const Point3D &p)
{

View File

@ -62,6 +62,7 @@ public:
bool operator>(const Point3D &source) const ;
bool operator<=(const Point3D &source) const ;
bool operator>=(const Point3D &source) const ;
operator std::string(void) const ;
//@}
/// Displays a Point3D

View File

@ -11,12 +11,12 @@ public:
{
// Default constructor
Area a1 ;
Area a2(NULL, "Unnamed area", 0,0,0, 0,0,0) ;
Area a2(NULL, "Unnamed area", Point3D(0,0,0), Point3D(0,0,0)) ;
TS_ASSERT_EQUALS(a1, a2) ;
// Copy constructor
Building b1 ;
Area a3(&b1, "My Area", 3,2,1, 0,1,2) ;
Area a3(&b1, "My Area", Point3D(3,2,1), Point3D(0,1,2)) ;
Area a4(a3) ;
TS_ASSERT_EQUALS(a3, a4) ;
}
@ -26,15 +26,11 @@ public:
{
// Simple read accessors
Building b1("Numerica") ;
Area a1(&b1, "My Area", 1,3,5, 8,7,4) ;
Area a1(&b1, "My Area", Point3D(1,3,5), Point3D(8,7,4)) ;
TS_ASSERT_EQUALS(a1.get_building(), &b1) ;
TS_ASSERT_EQUALS(a1.get_name(), "My Area") ;
TS_WARN("TODO: test inversion of points.") ;
/*
TS_ASSERT_EQUALS(a1.get_p_min(), Point3D(1,3,5)) ;
TS_ASSERT_EQUALS(a1.get_p_max(), Point3D(8,7,4)) ;
*/
TS_ASSERT_EQUALS(a1.get_p_min(), Point3D(1,3,4)) ;
TS_ASSERT_EQUALS(a1.get_p_max(), Point3D(8,7,5)) ;
// Write & read accessors
Building b2("Numerica 2") ;
@ -43,28 +39,32 @@ public:
a1.set_name("My new Area") ;
TS_ASSERT_EQUALS(a1.get_name(), "My new Area") ;
TS_WARN("TODO: test inversion of points.") ;
a1.set_x_min(56) ;
a1.set_y_min(32) ;
a1.set_z_min(49) ;
TS_ASSERT_EQUALS(a1.get_p_min(), Point3D(56,32,49)) ;
a1.set_x_max(99) ;
a1.set_y_max(44) ;
a1.set_z_max(77) ;
TS_ASSERT_EQUALS(a1.get_p_max(), Point3D(99,44,77)) ;
Point3D p1(9,4,2) ;
a1.set_p_min(p1) ;
TS_ASSERT_EQUALS(a1.get_p_min(), p1) ;
Point3D p2(1,7,1) ;
a1.set_p_max(p2) ;
TS_ASSERT_EQUALS(a1.get_p_max(), p2) ;
Point3D point3d1(56,32,49) ;
Point3D point3d2(99,44,77) ;
a1.set_coordinates(point3d1, point3d2) ;
TS_ASSERT_EQUALS(a1.get_p_min(), point3d1) ;
TS_ASSERT_EQUALS(a1.get_p_max(), point3d2) ;
a1.set_coordinates(point3d2, point3d1) ;
TS_ASSERT_EQUALS(a1.get_p_min(), point3d1) ;
TS_ASSERT_EQUALS(a1.get_p_max(), point3d2) ;
Point3D point3d3(9,4,2) ;
Point3D point3d4(1,7,1) ;
a1.set_coordinates(point3d3, point3d4) ;
TS_ASSERT_EQUALS(a1.get_p_min(), Point3D(1,4,1)) ;
TS_ASSERT_EQUALS(a1.get_p_max(), Point3D(9,7,2)) ;
a1.set_coordinates(point3d4, point3d3) ;
TS_ASSERT_EQUALS(a1.get_p_min(), Point3D(1,4,1)) ;
TS_ASSERT_EQUALS(a1.get_p_max(), Point3D(9,7,2)) ;
}
void test_contains_point(void)
{
Building b1("Numerica") ;
Area a1(&b1, "My Area", 0,0,0, 3,3,3) ;
Area a1(&b1, "My Area", Point3D(0,0,0), Point3D(3,3,3)) ;
Point3D p01(0,0,0) ;
TS_ASSERT(a1.contains_point(p01)) ;
@ -90,12 +90,14 @@ public:
{
// ==
Building b1("Numerica") ;
Area a1(&b1, "My Area", 1,3,5, 8,7,4) ;
Area a2(&b1, "My Area", 1,3,5, 8,7,4) ;
Point3D point3d1(1,3,5) ;
Point3D point3d2(8,7,4) ;
Area a1(&b1, "My Area", point3d1, point3d2) ;
Area a2(&b1, "My Area", point3d1, point3d2) ;
TS_ASSERT_EQUALS(a1, a2) ;
// !=
Area a3(&b1, "My Area", 1,0,5, 8,7,4) ;
Area a3(&b1, "My Area", Point3D(1,0,5), Point3D(8,7,4)) ;
TS_ASSERT_DIFFERS(a1, a3) ;
// =

View File

@ -54,7 +54,10 @@ public:
TS_ASSERT_EQUALS(p1.distance(p3), 2) ;
TS_ASSERT_EQUALS(p1.square_distance(p3), 4) ;
TS_WARN("TODO: some more complex tests.") ;
Point3D p4(5, 42, 3.2) ;
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) ;
}
@ -84,6 +87,9 @@ public:
// >=
TS_ASSERT(p1 >= p2) ;
// string cast operator
TS_ASSERT_EQUALS(static_cast<std::string>(p1), "(42;21;19)") ;
}
} ;

View File

@ -0,0 +1,31 @@
#include <cxxtest/ValueTraits.h>
#include <cstdio>
#include <string>
#include "point3d.hh"
namespace CxxTest
{
CXXTEST_TEMPLATE_INSTANTIATION
class ValueTraits<Point3D>
{
private:
char text_value[50] ;
public:
ValueTraits(const Point3D &source)
{
sprintf(text_value, "Point3D%s",
static_cast<std::string>(source).c_str()) ;
}
const char* asString(void) const
{
return text_value ;
}
} ;
} ;