Browse Source

Adds unit tests

develop
Erwin Nindl 1 year ago
parent
commit
09638b8f90
  1. 5
      CMakeLists.txt
  2. 18
      src/CMakeLists.txt
  3. 16
      tests/CMakeLists.txt
  4. 124
      tests/test_calc.cpp
  5. 13
      tests/test_spatial_grid.cpp

5
CMakeLists.txt

@ -22,3 +22,8 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR}) @@ -22,3 +22,8 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR})
add_subdirectory(protocol_buffers_definitions)
include_directories(src)
add_subdirectory(src)
if (BUILD_TESTING)
enable_testing()
add_subdirectory(tests)
endif()

18
src/CMakeLists.txt

@ -1,10 +1,20 @@ @@ -1,10 +1,20 @@
set(assignment_HEADERS
calc.hpp
json_output.hpp
proto_loader.hpp
spatial_grid.hpp)
set(assignment_SOURCES
main.cpp
calc.cpp
json_output.cpp
proto_loader.cpp)
add_executable(main ${assignment_SOURCES})
target_link_libraries(main PRIVATE proto ${PROTOBUF_LIBRARY})
target_link_libraries(main PRIVATE nlohmann_json::nlohmann_json)
add_library(assignment ${assignment_SOURCES} ${assignment_HEADERS})
set_target_properties(assignment PROPERTIES PUBLIC_HEADER "${assignment_HEADERS}")
target_link_libraries(assignment PRIVATE proto ${PROTOBUF_LIBRARY} )
target_link_libraries(assignment PUBLIC nlohmann_json::nlohmann_json)
add_executable(main main.cpp)
target_link_libraries(main assignment)

16
tests/CMakeLists.txt

@ -0,0 +1,16 @@ @@ -0,0 +1,16 @@
include(FetchContent)
FetchContent_Declare(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v2.13.6)
FetchContent_MakeAvailable(Catch2)
add_executable(test_calc test_calc.cpp)
target_link_libraries(test_calc PRIVATE Catch2::Catch2 assignment)
add_test(NAME test_calc COMMAND test_calc)
add_executable(test_spatial_grid test_spatial_grid.cpp)
target_link_libraries(test_spatial_grid PRIVATE Catch2::Catch2 assignment)
add_test(NAME test_spatial_grid COMMAND test_spatial_grid)

124
tests/test_calc.cpp

@ -0,0 +1,124 @@ @@ -0,0 +1,124 @@
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
#include "calc.hpp"
#include <vector>
TEST_CASE("norm", "[calc]")
{
std::vector<double> const v_0{0, 0, 0};
REQUIRE(calc::norm(v_0.cbegin(), v_0.cend()) == 0);
std::vector<double> const v_empty{};
REQUIRE(calc::norm(v_empty.cbegin(), v_empty.cend()) == 0);
std::vector<double> const v{1, 1, 1, 1, 1, 1, 1, 1, 1};
REQUIRE(calc::norm(v.cbegin(), v.cend()) == Approx(3) );
}
TEST_CASE("position_segment", "[calc]")
{
Positions const pos{
Position(0, 0, 0),
Position(0.5, 2, 1),
Position(2.0, 8, 4)
};
SECTION("center") {
auto const [first, second] = calc::position_segment(pos, 0.23);
REQUIRE(first.time == pos[0].time);
REQUIRE(second.time == pos[1].time);
}
SECTION("begin") {
auto const [first, second] = calc::position_segment(pos, 0);
REQUIRE(first.time == pos[0].time);
REQUIRE(second.time == pos[1].time);
}
SECTION("corner") {
auto const [first, second] = calc::position_segment(pos, 0.5);
REQUIRE(first.time == pos[0].time);
REQUIRE(second.time == pos[1].time);
}
SECTION("end") {
auto const [first, second] = calc::position_segment(pos, 2.0);
REQUIRE(first.time == pos[1].time);
REQUIRE(second.time == pos[2].time);
}
}
TEST_CASE("interpolation", "[calc]")
{
Positions const groundtruth{
Position(0.0, 0, 0),
Position(1.0, 1, 1),
Position(2.0, 3, 1)
};
Magnetics magnetics{
Magnetic(0.0, 0, 0, 0),
Magnetic(0.5, 0, 0, 0),
Magnetic(1.0, 0, 0, 0),
Magnetic(1.5, 0, 0, 0),
Magnetic(2.0, 0, 0, 0)
};
calc::interpolate_positions(magnetics, groundtruth);
REQUIRE(magnetics[0].position[0] == Approx(0));
REQUIRE(magnetics[0].position[1] == Approx(0));
REQUIRE(magnetics[1].position[0] == Approx(0.5));
REQUIRE(magnetics[1].position[1] == Approx(0.5));
REQUIRE(magnetics[3].position[0] == Approx(2));
REQUIRE(magnetics[3].position[1] == Approx(1));
REQUIRE(magnetics[4].position[0] == Approx(3));
REQUIRE(magnetics[4].position[1] == Approx(1));
}
TEST_CASE("crop_temporal_inersection", "[calc]")
{
Positions const positions{
Position(1.0, 8, 4),
Position(1.5, 8, 4),
Position(2.0, 8, 4)
};
SECTION("right-corner") {
Magnetics magnetics{
Magnetic(0.0, 1, 1),
Magnetic(0.5, 2, 2),
Magnetic(1.0, 3, 3),
Magnetic(1.5, 4, 4),
Magnetic(2.0, 5, 5)
};
calc::crop_temporal_inersection(magnetics, positions);
REQUIRE(magnetics.size() == 3);
REQUIRE(magnetics[0].position[0] == 3);
REQUIRE(magnetics[2].position[0] == 5);
}
SECTION("left-corner") {
Magnetics magnetics{
Magnetic(1.0, 2, 2),
Magnetic(2.0, 3, 3),
Magnetic(3.0, 4, 4),
Magnetic(4.0, 5, 5)
};
calc::crop_temporal_inersection(magnetics, positions);
REQUIRE(magnetics.size() == 2);
REQUIRE(magnetics[0].position[0] == 2);
REQUIRE(magnetics[1].position[0] == 3);
}
}
TEST_CASE("spatial_extent", "[calc]")
{
Magnetics const mag{
Magnetic(0, 1, 2),
Magnetic(0.5, 2, 1),
Magnetic(1.0, 8, 23),
Magnetic(1.5, 9, 4)
};
auto const [x_min, y_min, x_max, y_max] = calc::spatial_extent(mag);
REQUIRE(x_min == mag[0].position[0]);
REQUIRE(y_min == mag[1].position[1]);
REQUIRE(x_max == mag[3].position[0]);
REQUIRE(y_max == mag[2].position[1]);
}

13
tests/test_spatial_grid.cpp

@ -0,0 +1,13 @@ @@ -0,0 +1,13 @@
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
#include "spatial_grid.hpp"
TEST_CASE("metadata", "[spatial_grid]")
{
SpatialGrid<int> grid(2, 1, 10, 20);
REQUIRE(grid.start_x() == 0);
REQUIRE(grid.start_y() == 0);
REQUIRE(grid.size_x() == (10 / grid.spacing));
REQUIRE(grid.size_y() == (20 / grid.spacing));
}
Loading…
Cancel
Save