add unit tests support

you need cxxtest or they will be skipped
one bug found using those tests!
This commit is contained in:
boyska 2017-01-12 17:10:52 +01:00
parent 9fdd4ebbb1
commit 3f9b48c377
4 changed files with 68 additions and 5 deletions

4
.gitignore vendored
View file

@ -19,5 +19,9 @@ compile_commands.json
CTestTestfile.cmake CTestTestfile.cmake
/build/ /build/
# generated code (unit tests)
Test*.cpp
# executables # executables
/lines /lines
unittest_*

View file

@ -36,6 +36,18 @@ else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DHAS_OPENCV3") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DHAS_OPENCV3")
endif (OpenCV_VERSION VERSION_LESS "3.0") endif (OpenCV_VERSION VERSION_LESS "3.0")
#CxxTest
find_package(CxxTest)
if(CXXTEST_FOUND)
include_directories(${CXXTEST_INCLUDE_DIR})
enable_testing()
CXXTEST_ADD_TEST(unittest_changecoord TestChangeCoord.cpp ${CMAKE_CURRENT_SOURCE_DIR}/TestChangeCoord.h
geometry.cpp)
target_link_libraries(unittest_changecoord ${OpenCV_LIBS})
else()
message(STATUS "Warning: Can't find CxxTest; testing will be disabled")
endif()
add_executable( lines lines.cpp geometry.cpp cvutils.cpp) add_executable( lines lines.cpp geometry.cpp cvutils.cpp)
target_link_libraries( lines ${OpenCV_LIBS} ) target_link_libraries( lines ${OpenCV_LIBS} )

48
TestChangeCoord.h Normal file
View file

@ -0,0 +1,48 @@
#include <math.h>
#ifdef HAS_OPENCV3
#include <opencv2/core.hpp> //Any OPENCV3 code
#else
#include <opencv2/core/core.hpp> //Any Opencv2 code
#endif
using namespace cv;
#include <cxxtest/TestSuite.h>
#include "geometry.h"
class TestChangeCoord : public CxxTest::TestSuite
{
public:
void testOrigin(void)
{
Point origin = Point(1,2);
//the angle here is actually irrelevant
Point p = map_point(M_PI/2, origin, origin);
TS_ASSERT_EQUALS(p.x, 0);
TS_ASSERT_EQUALS(p.y, 0);
}
void testOnlyTranslation(void)
{
Point origin = Point(1,2);
Point p = map_point(0, origin, Point(0,0));
TS_ASSERT_EQUALS(p.x, -1);
TS_ASSERT_EQUALS(p.y, -2);
}
void testOnlyRotationOrigin(void)
{
Point origin = Point(0,0);
Point p = map_point(M_PI, origin, origin);
TS_ASSERT_EQUALS(p.x, 0);
TS_ASSERT_EQUALS(p.y, 0);
}
void testOnlyRotation(void)
{
Point origin = Point(0,0);
Point p = map_point(M_PI, origin, Point(1,2));
TS_ASSERT_EQUALS(p.x, -1);
TS_ASSERT_EQUALS(p.y, -2);
}
};

View file

@ -1,3 +1,5 @@
#include <iostream>
#ifdef HAS_OPENCV3 #ifdef HAS_OPENCV3
#include <opencv2/core.hpp> //Any OPENCV3 code #include <opencv2/core.hpp> //Any OPENCV3 code
#else #else
@ -24,19 +26,16 @@ map_point(double theta, cv::Point origin, cv::Point tomap)
//that's the algorithm: res = Rt*(tomap - origin) //that's the algorithm: res = Rt*(tomap - origin)
cv::Vec<double,2> p_as_vector; cv::Vec<double,2> p_as_vector;
cv::Point p; cv::Point p;
cv::Point res;
cv::Matx<double,2,2> Rt; cv::Matx<double,2,2> Rt;
p = tomap - origin; p = tomap - origin;
p_as_vector[0] = p.x; p_as_vector[0] = p.x;
p_as_vector[1] = p.y; p_as_vector[1] = p.y;
//TODO: fai matrice
Rt(0,0) = cos(theta); Rt(0,0) = cos(theta);
Rt(0,1) = sin(theta); Rt(0,1) = sin(theta);
Rt(1,1) = Rt(0,0); Rt(1,1) = Rt(0,0);
Rt(1,0) = -Rt(0,1); Rt(1,0) = -Rt(0,1);
cv::Vec<double,2> res_as_vector = Rt*p_as_vector; cv::Vec<double,2> res_as_vector = Rt*p_as_vector;
res.x = res_as_vector[0]; return cv::Point(res_as_vector);
res.y = res_as_vector[1];
return res;
} }