diff --git a/CMakeLists.txt b/CMakeLists.txt index 3462a59..e376b5f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,13 +7,15 @@ set(CMAKE_CXX_EXTENSIONS OFF) add_compile_options(-Wall -Wextra -Wpedantic -Werror) -find_package(catkin REQUIRED) +find_package(catkin REQUIRED COMPONENTS + dr_ros + roslib +) + find_package(Boost REQUIRED COMPONENTS filesystem date_time) -include_directories( - "include/${PROJECT_NAME}" - SYSTEM "${Boost_INCLUDE_DIRS}" -) +include_directories("include/${PROJECT_NAME}") +include_directories(SYSTEM ${catkin_INCLUDE_DIRS}) catkin_package( LIBRARIES ${PROJECT_NAME} @@ -29,24 +31,10 @@ add_library(${PROJECT_NAME} src/polynomial.cpp ) -target_link_libraries(${PROJECT_NAME} PRIVATE ${catkin_LIBRARIES} ${Boost_LIBRARIES}) +target_link_libraries(${PROJECT_NAME} ${catkin_LIBRARIES} ${Boost_LIBRARIES}) if (CATKIN_ENABLE_TESTING) - catkin_add_gtest(${PROJECT_NAME}_test_bitwise test/bitwise.cpp) - catkin_add_gtest(${PROJECT_NAME}_test_button_filter test/button_filter.cpp) - catkin_add_gtest(${PROJECT_NAME}_test_environment test/environment.cpp) - catkin_add_gtest(${PROJECT_NAME}_test_expand test/expand.cpp) - catkin_add_gtest(${PROJECT_NAME}_test_functional test/functional.cpp) - catkin_add_gtest(${PROJECT_NAME}_test_geometry test/geometry.cpp) - catkin_add_gtest(${PROJECT_NAME}_test_integer_sequence test/integer_sequence.cpp) - catkin_add_gtest(${PROJECT_NAME}_test_polynomial test/polynomial.cpp) - catkin_add_gtest(${PROJECT_NAME}_test_timestamp test/timestamp.cpp) - catkin_add_gtest(${PROJECT_NAME}_test_tuple test/tuple.cpp) - catkin_add_gtest(${PROJECT_NAME}_test_voidt_t test/void_t.cpp) - - target_link_libraries(${PROJECT_NAME}_test_environment ${PROJECT_NAME}) - target_link_libraries(${PROJECT_NAME}_test_expand ${PROJECT_NAME}) - target_link_libraries(${PROJECT_NAME}_test_polynomial ${PROJECT_NAME}) + add_subdirectory(test) endif() install( diff --git a/include/dr_util/resource.hpp b/include/dr_util/resource.hpp new file mode 100644 index 0000000..27b3347 --- /dev/null +++ b/include/dr_util/resource.hpp @@ -0,0 +1,14 @@ +#pragma once +#include + +namespace dr { + +/// Convert a ROS package URL or a regular file URL to a file path. +/** + * If the input does not contain an URI scheme it is treated as a relative file path and returned without modification. + * + * \throws if an unsupported URI scheme is encountered. + */ +std::string resolveResourceUrl(std::string const & url); + +} diff --git a/package.xml b/package.xml index 00a7988..a23998c 100644 --- a/package.xml +++ b/package.xml @@ -10,4 +10,13 @@ catkin boost + dr_ros + roscpp + roslib + + dr_log + + catch2 + dr_ros + dr_log diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..166df3b --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,53 @@ +find_package(Catch2 REQUIRED) +include_directories(${Catch2_INCLUDE_DIRS}) + +add_library(${PROJECT_NAME}_test_main EXCLUDE_FROM_ALL main.cpp) +target_link_libraries(${PROJECT_NAME}_test_main ${PROJECT_NAME}) + +define_property(GLOBAL PROPERTY CHECK_TARGET BRIEF_DOCS "." FULL_DOCS ".") +set_property(GLOBAL PROPERTY CHECK_TARGET "check") + +if (NOT TARGET tests) + add_custom_target(tests) +endif() + +function(declare_test name) + add_executable(${name} EXCLUDE_FROM_ALL ${ARGN}) + target_link_libraries(${name} PRIVATE ${PROJECT_NAME}_test_main) + add_custom_target(check_${name} COMMAND ${name}) + add_dependencies(tests ${name}) + + get_property(check_target GLOBAL PROPERTY CHECK_TARGET) + list(APPEND check_target COMMAND cmake -E cmake_echo_color --white --bold ${name} COMMAND ${name} DEPENDS ${name}) + set_property(GLOBAL PROPERTY CHECK_TARGET "${check_target}") + + add_test(NAME ${name} COMMAND ${name}) +endfunction() + +function(declare_tests prefix) + foreach(test ${ARGN}) + declare_test(${prefix}${test} ${test}.cpp) + endforeach() +endfunction() + +declare_tests(dr_util_ + "bitwise" + "button_filter" + "environment" + "expand" + "functional" + "geometry" + "integer_sequence" + "polynomial" + "resource" + "timestamp" + "tuple" + "void_t" +) + +get_property(check_target GLOBAL PROPERTY CHECK_TARGET) +add_custom_target(${check_target} USES_TERMINAL) + +if (TARGET run_tests) + add_dependencies(run_tests check) +endif() diff --git a/test/bitwise.cpp b/test/bitwise.cpp index e935d3e..19df66a 100644 --- a/test/bitwise.cpp +++ b/test/bitwise.cpp @@ -1,80 +1,82 @@ +// This repository #include "bitwise.hpp" -#include - -int main(int argc, char ** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} +// Catch +#include namespace dr { -TEST(BitwiseTest, makeBitMask) { - // least significant bit - EXPECT_EQ(0x1u, bitMask(0)); - // some middle bit - EXPECT_EQ(0x4000u, bitMask(14)); - // most significant bit - EXPECT_EQ(0x80000000u, bitMask(31)); -} - -TEST(BitwiseTest, testTestBit) { - // least significant bit - EXPECT_TRUE(testBit(0x1, 0)); - EXPECT_FALSE(testBit(0x1, 14)); - EXPECT_FALSE(testBit(0x1, 31)); - - // some middle bit - EXPECT_FALSE(testBit(0x4000, 0)); - EXPECT_TRUE(testBit(0x4000, 14)); - EXPECT_FALSE(testBit(0x4000, 31)); - - // most significant bit - EXPECT_FALSE(testBit(0x80000000, 0)); - EXPECT_FALSE(testBit(0x80000000, 14)); - EXPECT_TRUE(testBit(0x80000000, 31)); - - // no value - EXPECT_FALSE(testBit(0, 0)); - EXPECT_FALSE(testBit(0, 14)); - EXPECT_FALSE(testBit(0, 31)); - - // all bits set to true - EXPECT_TRUE(testBit(0xFFFFFFFF, 0)); - EXPECT_TRUE(testBit(0xFFFFFFFF, 14)); - EXPECT_TRUE(testBit(0xFFFFFFFF, 31)); -} - -TEST(BitwiseTest, testTestRising) { - // no change for all bits set to 1 - EXPECT_FALSE(testRising(0xFFFFFFFF, 0xFFFFFFFF, 0)); - EXPECT_FALSE(testRising(0xFFFFFFFF, 0xFFFFFFFF, 14)); - EXPECT_FALSE(testRising(0xFFFFFFFF, 0xFFFFFFFF, 31)); - - // no change for all bits set to 0 - EXPECT_FALSE(testRising(0, 0, 0)); - EXPECT_FALSE(testRising(0, 0, 14)); - EXPECT_FALSE(testRising(0, 0, 31)); - - // ignore falling value - EXPECT_TRUE(testRising(0xFFFFFFFF, 0, 0)); - EXPECT_TRUE(testRising(0xFFFFFFFF, 0, 14)); - EXPECT_TRUE(testRising(0xFFFFFFFF, 0, 31)); - - // test rising edge - EXPECT_FALSE(testRising(0, 0xFFFFFFFF, 0)); - EXPECT_FALSE(testRising(0, 0xFFFFFFFF, 14)); - EXPECT_FALSE(testRising(0, 0xFFFFFFFF, 31)); - - // test specific rising edge - EXPECT_TRUE(testRising(0x1, 0, 0)); - EXPECT_TRUE(testRising(0x4000, 0, 14)); - EXPECT_TRUE(testRising(0x80000000, 0, 31)); - - // test wrong rising edge - EXPECT_FALSE(testRising(0x2, 0, 0)); - EXPECT_FALSE(testRising(0x8000, 0, 14)); - EXPECT_FALSE(testRising(0x40000000, 0, 31)); +TEST_CASE("BitwiseTest") { + + SECTION("makeBitMask") { + // least significant bit + REQUIRE( 0x1u == bitMask(0)); + + // some middle bit + REQUIRE( 0x4000u == bitMask(14)); + + // most significant bit + REQUIRE(0x80000000u == bitMask(31)); + } + + SECTION("testTestBit") { + // least significant bit + REQUIRE(testBit(0x1, 0) == true); + REQUIRE(testBit(0x1, 14) == false); + REQUIRE(testBit(0x1, 31) == false); + + // some middle bit + REQUIRE(testBit(0x4000, 0) == false); + REQUIRE(testBit(0x4000, 14) == true); + REQUIRE(testBit(0x4000, 31) == false); + + // most significant bit + REQUIRE(testBit(0x80000000, 0) == false); + REQUIRE(testBit(0x80000000, 14) == false); + REQUIRE(testBit(0x80000000, 31) == true); + + // no value + REQUIRE(testBit(0, 0) == false); + REQUIRE(testBit(0, 14) == false); + REQUIRE(testBit(0, 31) == false); + + // all bits set to true + REQUIRE(testBit(0xFFFFFFFF, 0) == true); + REQUIRE(testBit(0xFFFFFFFF, 14) == true); + REQUIRE(testBit(0xFFFFFFFF, 31) == true); + } + + SECTION("testTestRising") { + // no change for all bits set to 1 + REQUIRE(testRising(0xFFFFFFFF, 0xFFFFFFFF, 0) == false); + REQUIRE(testRising(0xFFFFFFFF, 0xFFFFFFFF, 14) == false); + REQUIRE(testRising(0xFFFFFFFF, 0xFFFFFFFF, 31) == false); + + // no change for all bits set to 0 + REQUIRE(testRising(0, 0, 0) == false); + REQUIRE(testRising(0, 0, 14) == false); + REQUIRE(testRising(0, 0, 31) == false); + + // ignore falling value + REQUIRE(testRising(0xFFFFFFFF, 0, 0) == true); + REQUIRE(testRising(0xFFFFFFFF, 0, 14) == true); + REQUIRE(testRising(0xFFFFFFFF, 0, 31) == true); + + // test rising edge + REQUIRE(testRising(0, 0xFFFFFFFF, 0) == false); + REQUIRE(testRising(0, 0xFFFFFFFF, 14) == false); + REQUIRE(testRising(0, 0xFFFFFFFF, 31) == false); + + // test specific rising edge + REQUIRE(testRising(0x1, 0, 0) == true); + REQUIRE(testRising(0x4000, 0, 14) == true); + REQUIRE(testRising(0x80000000, 0, 31) == true); + + // test wrong rising edge + REQUIRE(testRising(0x2, 0, 0) == false); + REQUIRE(testRising(0x8000, 0, 14) == false); + REQUIRE(testRising(0x40000000, 0, 31) == false); + } } } diff --git a/test/button_filter.cpp b/test/button_filter.cpp index 2e02422..238355e 100644 --- a/test/button_filter.cpp +++ b/test/button_filter.cpp @@ -1,70 +1,70 @@ +// This repository #include "button_filter.hpp" -#include - -int main(int argc, char ** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} +// Catch +#include namespace dr { -TEST(ButtonFilterTest, filterAlwaysHigh) { - ButtonFilter bf(true, false); +TEST_CASE("ButtonFilterTest") { - // pass through high signals always - EXPECT_FALSE(bf.filter(false)); - EXPECT_TRUE(bf.filter(true)); - EXPECT_TRUE(bf.filter(true)); -} + SECTION("filterAlwaysHigh") { + ButtonFilter bf(true, false); -TEST(ButtonFilterTest, filterAlwaysLow) { - ButtonFilter bf(false, true); + // pass through high signals always + REQUIRE(bf.filter(false) == false); + REQUIRE(bf.filter(true) == true); + REQUIRE(bf.filter(true) == true); + } - // pass through low signals always - EXPECT_TRUE(bf.filter(false)); - EXPECT_TRUE(bf.filter(true)); - EXPECT_FALSE(bf.filter(true)); -} + SECTION("filterAlwaysLow") { + ButtonFilter bf(false, true); -TEST(ButtonFilterTest, filterAlwaysHighAndLow) { - ButtonFilter bf(true, true); + // pass through low signals always + REQUIRE(bf.filter(false) == true); + REQUIRE(bf.filter(true) == true); + REQUIRE(bf.filter(true) == false); + } - // pass through any signal - EXPECT_TRUE(bf.filter(false)); - EXPECT_TRUE(bf.filter(false)); - EXPECT_TRUE(bf.filter(true)); - EXPECT_TRUE(bf.filter(true)); -} + SECTION("filterAlwaysHighAndLow") { + ButtonFilter bf(true, true); -TEST(ButtonFilterTest, filterRisingEdge) { - ButtonFilter bf; + // pass through any signal + REQUIRE(bf.filter(false) == true); + REQUIRE(bf.filter(false) == true); + REQUIRE(bf.filter(true) == true); + REQUIRE(bf.filter(true) == true); + } - // test rising edge - EXPECT_FALSE(bf.filter(false)); - EXPECT_TRUE(bf.filter(true)); -} + SECTION("filterRisingEdge") { + ButtonFilter bf; -TEST(ButtonFilterTest, filterFallingEdge) { - ButtonFilter bf; + // test rising edge + REQUIRE(bf.filter(false) == false); + REQUIRE(bf.filter(true) == true); + } - // test falling edge - EXPECT_TRUE(bf.filter(true)); - EXPECT_TRUE(bf.filter(false)); -} + SECTION("filterFallingEdge") { + ButtonFilter bf; + + // test falling edge + REQUIRE(bf.filter(true) == true); + REQUIRE(bf.filter(false) == true); + } -TEST(ButtonFilterTest, filterMultiple) { - ButtonFilter bf; + SECTION("filterMultiple") { + ButtonFilter bf; - // test multiple signals - EXPECT_TRUE(bf.filter(true)); - EXPECT_FALSE(bf.filter(true)); + // test multiple signals + REQUIRE(bf.filter(true) == true); + REQUIRE(bf.filter(true) == false); - EXPECT_TRUE(bf.filter(false)); - EXPECT_FALSE(bf.filter(false)); + REQUIRE(bf.filter(false) == true); + REQUIRE(bf.filter(false) == false); - EXPECT_TRUE(bf.filter(true)); - EXPECT_FALSE(bf.filter(true)); + REQUIRE(bf.filter(true) == true); + REQUIRE(bf.filter(true) == false); + } } } diff --git a/test/environment.cpp b/test/environment.cpp index c070bdb..aee359a 100644 --- a/test/environment.cpp +++ b/test/environment.cpp @@ -1,22 +1,23 @@ +// This repository #include "environment.hpp" -#include -#include +// Catch +#include -int main(int argc, char ** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} +// C +#include namespace dr { -TEST(Environment, getEnvironment) { +TEST_CASE("Environment -- getEnvironment", "getEnvironment") { ::clearenv(); - EXPECT_EQ(getEnvironment(), (std::map{})); + REQUIRE(getEnvironment() == (std::map{})); + ::setenv("FOO", "aap", true); - EXPECT_EQ(getEnvironment(), (std::map{{"FOO", "aap"}})); + REQUIRE(getEnvironment() == (std::map{{"FOO", "aap"}})); + ::setenv("BAR", "noot", true); - EXPECT_EQ(getEnvironment(), (std::map{{"FOO", "aap"}, {"BAR", "noot"}})); + REQUIRE(getEnvironment() == (std::map{{"FOO", "aap"}, {"BAR", "noot"}})); } } diff --git a/test/expand.cpp b/test/expand.cpp index d34cc61..74efe98 100644 --- a/test/expand.cpp +++ b/test/expand.cpp @@ -1,34 +1,34 @@ +// This repository #include "expand.hpp" -#include - -int main(int argc, char ** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} +// Catch +#include namespace dr { -TEST(Expand, simple) { - EXPECT_EQ(expandVariables("$test", {{"test", "foo"}}), "foo"); - EXPECT_EQ(expandVariables("$test", {{"test", "bar"}}), "bar"); - EXPECT_EQ(expandVariables("${test}", {{"test", "foo"}}), "foo"); - EXPECT_EQ(expandVariables("${test}", {{"test", "bar"}}), "bar"); -} - -TEST(Expand, sentence) { - EXPECT_EQ(expandVariables("Hello $name, welcome to $place", {{"name", "Rick"}, {"place", "Earth"}}), "Hello Rick, welcome to Earth"); - EXPECT_EQ(expandVariables("Hello ${name}, welcome to ${place}", {{"name", "Rick"}, {"place", "Earth"}}), "Hello Rick, welcome to Earth"); -} - -TEST(Expand, nipple_brackets) { - EXPECT_EQ(expandVariables("test$testtest", {{"test", "wrong"}, {"testtest", "good"}}), "testgood"); - EXPECT_EQ(expandVariables("test${test}test", {{"test", "good"}, {"testtest", "wrong"}}), "testgoodtest"); -} - -TEST(Expand, ignore_empty_key) { - EXPECT_EQ(expandVariables("$", {{"", "aap"}}), "$"); - EXPECT_EQ(expandVariables("${}", {{"", "aap"}}), "${}"); +TEST_CASE("Expand") { + + SECTION("simple") { + REQUIRE(expandVariables("$test", {{"test", "foo"}}) == "foo"); + REQUIRE(expandVariables("$test", {{"test", "bar"}}) == "bar"); + REQUIRE(expandVariables("${test}", {{"test", "foo"}}) == "foo"); + REQUIRE(expandVariables("${test}", {{"test", "bar"}}) == "bar"); + } + + SECTION("sentence") { + REQUIRE(expandVariables("Hello $name, welcome to $place", {{"name", "Rick"}, {"place", "Earth"}}) == "Hello Rick, welcome to Earth"); + REQUIRE(expandVariables("Hello ${name}, welcome to ${place}", {{"name", "Rick"}, {"place", "Earth"}}) == "Hello Rick, welcome to Earth"); + } + + SECTION("nipple_brackets") { + REQUIRE(expandVariables("test$testtest", {{"test", "wrong"}, {"testtest", "good"}}) == "testgood"); + REQUIRE(expandVariables("test${test}test", {{"test", "good"}, {"testtest", "wrong"}}) == "testgoodtest"); + } + + SECTION("ignore_empty_key") { + REQUIRE(expandVariables("$", {{"", "aap"}}) == "$"); + REQUIRE(expandVariables("${}", {{"", "aap"}}) == "${}"); + } } } diff --git a/test/functional.cpp b/test/functional.cpp index 04d57ef..1a1533b 100644 --- a/test/functional.cpp +++ b/test/functional.cpp @@ -1,30 +1,31 @@ +// This repository +#include "functional.hpp" +#include "tuple.hpp" + +// Catch +#include + +// C++ #include #include #include -#include - -#include "functional.hpp" -#include "tuple.hpp" -using namespace dr; +TEST_CASE("FunctionalTest") { -int main(int argc, char * * argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} + SECTION("callHardcoded") { + REQUIRE(true == dr::call([] () { return true; }, std::make_tuple())); + REQUIRE(false == dr::call([] () { return false; }, std::make_tuple())); + } -TEST(FunctionalTest, callHardcoded) { - EXPECT_EQ(true, call([] () { return true; }, std::make_tuple())); - EXPECT_EQ(false, call([] () { return false; }, std::make_tuple())); -} + SECTION("callInput") { + REQUIRE(true == dr::call([] (bool input) { return input; }, std::make_tuple(true))); + REQUIRE(false == dr::call([] (bool input) { return input; }, std::make_tuple(false))); + } -TEST(FunctionalTest, callInput) { - EXPECT_EQ(true, call([] (bool input) { return input; }, std::make_tuple(true))); - EXPECT_EQ(false, call([] (bool input) { return input; }, std::make_tuple(false))); -} + SECTION("callSum") { + REQUIRE( 3 == dr::call([] (int a, int b) { return a + b; }, std::make_tuple(1, 2))); + REQUIRE(17 == dr::call([] (int a, int b) { return a + b; }, std::make_tuple(7, 10))); + } -TEST(FunctionalTest, callSum) { - EXPECT_EQ( 3, call([] (int a, int b) { return a + b; }, std::make_tuple(1, 2))); - EXPECT_EQ(17, call([] (int a, int b) { return a + b; }, std::make_tuple(7, 10))); -} +} \ No newline at end of file diff --git a/test/geometry.cpp b/test/geometry.cpp index 7aba2fd..2fdccea 100644 --- a/test/geometry.cpp +++ b/test/geometry.cpp @@ -1,109 +1,226 @@ +// This repository #include "geometry.hpp" -#include +// Catch +#include + +namespace { + void requireEqual(float const & a, float const & b) { + REQUIRE_THAT(a, Catch::WithinULP(b, 0)); + } + + void requireNear(float const & a, float const & b, float const &tol) { + float diff = std::abs(a - b); + if (diff <= tol) { + SUCCEED(); + } else { + std::string msg = "first argument " + std::to_string(a) + + " differs from the second argument " + std::to_string(b) + + " in " + std::to_string(diff) + + " which is larger than the allowed tolerance " + std::to_string(tol); + FAIL(msg); + } + } +} + -int main(int argc, char ** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); +TEST_CASE("GeometryTest") { + + SECTION("testPi") { + // test equality with C defined pi + requireEqual(dr::pi(), M_PI); + } + + SECTION("testDegrees") { + // test normal use case + requireEqual(dr::degrees(0), 0); + requireEqual(dr::degrees(180), M_PI); + requireEqual(dr::degrees(360), 2 * M_PI); + requireNear(dr::degrees(37), 0.645771823237901, 0.0000001); // 37 / 180 * pi + + // test negativity + requireEqual(dr::degrees(-180), -M_PI); + requireEqual(dr::degrees(-360), -2 * M_PI); + } + + SECTION("testToDegrees") { + // test normal use case + requireEqual(dr::to_degrees(0), 0); + requireEqual(dr::to_degrees(M_PI), 180); + requireEqual(dr::to_degrees(2 * M_PI), 360); + requireNear(dr::to_degrees(0.645771823237901), 37, 0.0000001); // 37 / 180 * pi + + // test negativity + requireEqual(dr::to_degrees(-M_PI), -180); + requireEqual(dr::to_degrees(-2 * M_PI), -360); + } + + SECTION("testSign") { + // normal use cases + REQUIRE(dr::sign(-1) == -1); + REQUIRE(dr::sign(1) == 1); + REQUIRE(dr::sign(0) == 0); + + // double use cases + REQUIRE(dr::sign(M_PI) == 1); + REQUIRE(dr::sign(-M_PI) == -1); + REQUIRE(dr::sign(std::numeric_limits::quiet_NaN()) == 0); + } + + SECTION("testNormalizeAngle") { + // normal use + requireEqual(dr::normalizeAngle(3 * M_PI), M_PI); + requireEqual(dr::normalizeAngle(3.5 * M_PI), 1.5 * M_PI); + + // negativity + requireEqual(dr::normalizeAngle(-3 * M_PI), M_PI); + requireEqual(dr::normalizeAngle(-3.5 * M_PI), 0.5 * M_PI); + } + + SECTION("testAngleFrom") { + // same as normalizeAngle + + // normal use + requireEqual(dr::angleFrom(3 * M_PI, 0), M_PI); + requireEqual(dr::angleFrom(3.5 * M_PI, 0), 1.5 * M_PI); + + // negative angle + requireEqual(dr::angleFrom(-3 * M_PI, 0), M_PI); + requireEqual(dr::angleFrom(-3.5 * M_PI, 0), 0.5 * M_PI); + + // set different angle + + // normal use + requireEqual(dr::angleFrom(3 * M_PI, 5 * M_PI), 5 * M_PI); + requireEqual(dr::angleFrom(3.5 * M_PI, 5 * M_PI), 5.5 * M_PI); + + // negative from angle + requireEqual(dr::angleFrom(-3 * M_PI, -5 * M_PI), -5 * M_PI); + requireEqual(dr::angleFrom(-3.5 * M_PI, -5 * M_PI), -3.5 * M_PI); + + } + + SECTION("testSquare") { + requireEqual(dr::square(2), 4); + requireEqual(dr::square(-2), 4); + requireNear(dr::square(M_PI), 9.86960440108935861883, 0.000001); + requireNear(dr::square(-M_PI), 9.86960440108935861883, 0.000001); + } + + SECTION("testSquareSum") { + requireEqual(dr::square_sum(2.0, 2.0), 8.0); + requireEqual(dr::square_sum(1.0, 2.0, 3.0, 4.0, 5.0), 55.0); + requireEqual(dr::square_sum(-1.0, -2.0, -3.0, -4.0, -5.0), 55.0); + } + + SECTION("testLength") { + requireEqual(dr::length(1.0, 0.0, 0.0), 1.0); + requireNear(dr::length(10.0, 10.0, 0.0), 14.1421356237309504880, 0.0000001); // sqrt(10*10 + 10*10) + requireEqual(dr::length(0.0, 0.0, 0.0), 0.0); + } + + SECTION("testAngle") { + requireEqual(dr::angle(std::sqrt(2), 1.0, 1.0), M_PI_2); + } } -namespace dr { -TEST(GeometryTest, testPi) { +/* +TEST_CASE("GeometryTest -- testPi", "testPi") { // test equality with C defined pi - EXPECT_DOUBLE_EQ(pi(), M_PI); + requireEqual(dr::pi(), M_PI); } -TEST(GeometryTest, testDegrees) { +TEST_CASE("GeometryTest -- testDegrees", "testDegrees") { // test normal use case - EXPECT_DOUBLE_EQ(degrees(0), 0); - EXPECT_DOUBLE_EQ(degrees(180), M_PI); - EXPECT_DOUBLE_EQ(degrees(360), 2 * M_PI); - EXPECT_NEAR(degrees(37), 0.645771823237901, 0.0000001); // 37 / 180 * pi + requireEqual(dr::degrees(0), 0); + requireEqual(dr::degrees(180), M_PI); + requireEqual(dr::degrees(360), 2 * M_PI); + requireNear(dr::degrees(37), 0.645771823237901, 0.0000001); // 37 / 180 * pi // test negativity - EXPECT_DOUBLE_EQ(degrees(-180), -M_PI); - EXPECT_DOUBLE_EQ(degrees(-360), -2 * M_PI); + requireEqual(dr::degrees(-180), -M_PI); + requireEqual(dr::degrees(-360), -2 * M_PI); } -TEST(GeometryTest, testToDegrees) { +TEST_CASE("GeometryTest -- testToDegrees", "testToDegrees") { // test normal use case - EXPECT_DOUBLE_EQ(to_degrees(0), 0); - EXPECT_DOUBLE_EQ(to_degrees(M_PI), 180); - EXPECT_DOUBLE_EQ(to_degrees(2 * M_PI), 360); - EXPECT_NEAR(to_degrees(0.645771823237901), 37, 0.0000001); // 37 / 180 * pi + requireEqual(dr::to_degrees(0), 0); + requireEqual(dr::to_degrees(M_PI), 180); + requireEqual(dr::to_degrees(2 * M_PI), 360); + requireNear(dr::to_degrees(0.645771823237901), 37, 0.0000001); // 37 / 180 * pi // test negativity - EXPECT_DOUBLE_EQ(to_degrees(-M_PI), -180); - EXPECT_DOUBLE_EQ(to_degrees(-2 * M_PI), -360); + requireEqual(dr::to_degrees(-M_PI), -180); + requireEqual(dr::to_degrees(-2 * M_PI), -360); } -TEST(GeometryTest, testSign) { +TEST_CASE("GeometryTest -- testSign", "testSign") { // normal use cases - EXPECT_EQ(sign(-1), -1); - EXPECT_EQ(sign(1), 1); - EXPECT_EQ(sign(0), 0); + REQUIRE(dr::sign(-1) == -1); + REQUIRE(dr::sign(1) == 1); + REQUIRE(dr::sign(0) == 0); // double use cases - EXPECT_DOUBLE_EQ(sign(M_PI), 1); - EXPECT_DOUBLE_EQ(sign(-M_PI), -1); - EXPECT_DOUBLE_EQ(sign(std::numeric_limits::quiet_NaN()), 0); + REQUIRE(dr::sign(M_PI) == 1); + REQUIRE(dr::sign(-M_PI) == -1); + REQUIRE(dr::sign(std::numeric_limits::quiet_NaN()) == 0); } -TEST(GeometryTest, testNormalizeAngle) { +TEST_CASE("GeometryTest -- testNormalizeAngle", "testNormalizeAngle") { // normal use - EXPECT_DOUBLE_EQ(normalizeAngle(3 * M_PI), M_PI); - EXPECT_DOUBLE_EQ(normalizeAngle(3.5 * M_PI), 1.5 * M_PI); + requireEqual(dr::normalizeAngle(3 * M_PI), M_PI); + requireEqual(dr::normalizeAngle(3.5 * M_PI), 1.5 * M_PI); // negativity - EXPECT_DOUBLE_EQ(normalizeAngle(-3 * M_PI), M_PI); - EXPECT_DOUBLE_EQ(normalizeAngle(-3.5 * M_PI), 0.5 * M_PI); + requireEqual(dr::normalizeAngle(-3 * M_PI), M_PI); + requireEqual(dr::normalizeAngle(-3.5 * M_PI), 0.5 * M_PI); } -TEST(GeometryTest, testAngleFrom) { +TEST_CASE("GeometryTest -- testAngleFrom", "testAngleFrom") { // same as normalizeAngle // normal use - EXPECT_DOUBLE_EQ(angleFrom(3 * M_PI, 0), M_PI); - EXPECT_DOUBLE_EQ(angleFrom(3.5 * M_PI, 0), 1.5 * M_PI); + requireEqual(dr::angleFrom(3 * M_PI, 0), M_PI); + requireEqual(dr::angleFrom(3.5 * M_PI, 0), 1.5 * M_PI); // negative angle - EXPECT_DOUBLE_EQ(angleFrom(-3 * M_PI, 0), M_PI); - EXPECT_DOUBLE_EQ(angleFrom(-3.5 * M_PI, 0), 0.5 * M_PI); + requireEqual(dr::angleFrom(-3 * M_PI, 0), M_PI); + requireEqual(dr::angleFrom(-3.5 * M_PI, 0), 0.5 * M_PI); // set different angle // normal use - EXPECT_DOUBLE_EQ(angleFrom(3 * M_PI, 5 * M_PI), 5 * M_PI); - EXPECT_DOUBLE_EQ(angleFrom(3.5 * M_PI, 5 * M_PI), 5.5 * M_PI); + requireEqual(dr::angleFrom(3 * M_PI, 5 * M_PI), 5 * M_PI); + requireEqual(dr::angleFrom(3.5 * M_PI, 5 * M_PI), 5.5 * M_PI); // negative from angle - EXPECT_DOUBLE_EQ(angleFrom(-3 * M_PI, -5 * M_PI), -5 * M_PI); - EXPECT_DOUBLE_EQ(angleFrom(-3.5 * M_PI, -5 * M_PI), -3.5 * M_PI); + requireEqual(dr::angleFrom(-3 * M_PI, -5 * M_PI), -5 * M_PI); + requireEqual(dr::angleFrom(-3.5 * M_PI, -5 * M_PI), -3.5 * M_PI); } -TEST(GeometryTest, testSquare) { - EXPECT_DOUBLE_EQ(square(2), 4); - EXPECT_DOUBLE_EQ(square(-2), 4); - EXPECT_NEAR(square(M_PI), 9.86960440108935861883, 0.000001); - EXPECT_NEAR(square(-M_PI), 9.86960440108935861883, 0.000001); +TEST_CASE("GeometryTest -- testSquare", "testSquare") { + requireEqual(dr::square(2), 4); + requireEqual(dr::square(-2), 4); + requireNear(dr::square(M_PI), 9.86960440108935861883, 0.000001); + requireNear(dr::square(-M_PI), 9.86960440108935861883, 0.000001); } -TEST(GeometryTest, testSquareSum) { - EXPECT_DOUBLE_EQ(square_sum(2.0, 2.0), 8.0); - EXPECT_DOUBLE_EQ(square_sum(1.0, 2.0, 3.0, 4.0, 5.0), 55.0); - EXPECT_DOUBLE_EQ(square_sum(-1.0, -2.0, -3.0, -4.0, -5.0), 55.0); +TEST_CASE("GeometryTest -- testSquareSum", "testSquareSum") { + requireEqual(dr::square_sum(2.0, 2.0), 8.0); + requireEqual(dr::square_sum(1.0, 2.0, 3.0, 4.0, 5.0), 55.0); + requireEqual(dr::square_sum(-1.0, -2.0, -3.0, -4.0, -5.0), 55.0); } -TEST(GeometryTest, testLength) { - EXPECT_DOUBLE_EQ(length(1.0, 0.0, 0.0), 1.0); - EXPECT_NEAR(length(10.0, 10.0, 0.0), 14.1421356237309504880, 0.0000001); // sqrt(10*10 + 10*10) - EXPECT_DOUBLE_EQ(length(0.0, 0.0, 0.0), 0.0); +TEST_CASE("GeometryTest -- testLength", "testLength") { + requireEqual(dr::length(1.0, 0.0, 0.0), 1.0); + requireNear(dr::length(10.0, 10.0, 0.0), 14.1421356237309504880, 0.0000001); // sqrt(10*10 + 10*10) + requireEqual(dr::length(0.0, 0.0, 0.0), 0.0); } -TEST(GeometryTest, testAngle) { - EXPECT_DOUBLE_EQ(angle(std::sqrt(2), 1.0, 1.0), M_PI_2); +TEST_CASE("GeometryTest --testAngle", "testAngle") { + requireEqual(dr::angle(std::sqrt(2), 1.0, 1.0), M_PI_2); } -} +*/ diff --git a/test/integer_sequence.cpp b/test/integer_sequence.cpp index bd020bc..ca3d705 100644 --- a/test/integer_sequence.cpp +++ b/test/integer_sequence.cpp @@ -1,16 +1,14 @@ +// This repository +#include "integer_sequence.hpp" + +// Catch2 +#include + +// C++ #include #include #include -#include - -#include "integer_sequence.hpp" - -int main(int argc, char * * argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} - namespace dr { template @@ -18,33 +16,37 @@ constexpr bool have_same_types(T1 const &, T2 const &) { return std::is_same{}; } -TEST(IntegerSequence, have_same_types) { - bool b1; - bool b2; - bool const b3 = false; - bool volatile b4; - bool * bp; - int i; - - static_assert(have_same_types(b1, b2), ""); - static_assert(have_same_types(b1, b3), ""); - static_assert(not have_same_types(b1, b4), ""); - static_assert(not have_same_types(b1, bp), ""); - static_assert(not have_same_types(b1, i), ""); -} - -TEST(IntegerSequence, add_offset_int) { - auto offset_sequence = offset_integer_sequence(std::make_integer_sequence{}); - auto expected_sequence = std::integer_sequence{}; - static_assert(have_same_types(offset_sequence, expected_sequence), ""); - ASSERT_TRUE(true); -} - -TEST(IntegerSequence, add_offset_index) { - auto offset_sequence = offset_index_sequence<8>(std::make_index_sequence<3>{}); - auto expected_sequence = std::index_sequence<8, 9, 10>{}; - static_assert(have_same_types(offset_sequence, expected_sequence), ""); - ASSERT_TRUE(true); +TEST_CASE("IntegerSequence") { + + SECTION("have_same_types") { + bool b1; + bool b2; + bool const b3 = false; + bool volatile b4; + bool * bp; + int i; + + static_assert(have_same_types(b1, b2), ""); + static_assert(have_same_types(b1, b3), ""); + static_assert(not have_same_types(b1, b4), ""); + static_assert(not have_same_types(b1, bp), ""); + static_assert(not have_same_types(b1, i), ""); + SUCCEED(); + } + + SECTION("add_offset_int") { + auto offset_sequence = offset_integer_sequence(std::make_integer_sequence{}); + auto expected_sequence = std::integer_sequence{}; + static_assert(have_same_types(offset_sequence, expected_sequence), ""); + SUCCEED(); + } + + SECTION("add_offset_index") { + auto offset_sequence = offset_index_sequence<8>(std::make_index_sequence<3>{}); + auto expected_sequence = std::index_sequence<8, 9, 10>{}; + static_assert(have_same_types(offset_sequence, expected_sequence), ""); + SUCCEED(); + } } } diff --git a/test/main.cpp b/test/main.cpp new file mode 100644 index 0000000..4ed06df --- /dev/null +++ b/test/main.cpp @@ -0,0 +1,2 @@ +#define CATCH_CONFIG_MAIN +#include diff --git a/test/polynomial.cpp b/test/polynomial.cpp index 967a186..cbb2ef5 100644 --- a/test/polynomial.cpp +++ b/test/polynomial.cpp @@ -1,82 +1,86 @@ +// This repository #include "polynomial.hpp" -#include +// Catch2 +#include +// C++ #include -int main(int argc, char ** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} - -namespace dr { - -TEST(PolynomialTest, emptyPolynomial) { - Polynomial p; - ASSERT_DOUBLE_EQ(0, p.y(0.0)); - ASSERT_DOUBLE_EQ(0, p.y(0.5)); - ASSERT_DOUBLE_EQ(0, p.y(1.0)); - ASSERT_DOUBLE_EQ(0, p.y(-0.5)); - ASSERT_DOUBLE_EQ(0, p.y(-1.0)); -} - -TEST(PolynomialTest, constantPolynomial) { - Polynomial p; - p.terms.emplace_back(3.25, 0); - - ASSERT_DOUBLE_EQ(3.25, p.y( 0.0)); - ASSERT_DOUBLE_EQ(3.25, p.y( 0.5)); - ASSERT_DOUBLE_EQ(3.25, p.y( 1.0)); - ASSERT_DOUBLE_EQ(3.25, p.y(-0.5)); - ASSERT_DOUBLE_EQ(3.25, p.y(-1.0)); -} - -TEST(PolynomialTest, squareRoot) { - Polynomial p; - p.terms.emplace_back(1, 0.5); - - ASSERT_DOUBLE_EQ( 0.000, p.y(0.0)); - ASSERT_DOUBLE_EQ( 1.000, p.y(1.0)); - ASSERT_DOUBLE_EQ( 2.000, p.y(4.0)); - - ASSERT_TRUE(std::isnan(p.y(-1.0))); - ASSERT_TRUE(std::isnan(p.y(-4.0))); -} - -TEST(PolynomialTest, firstOrder) { - Polynomial p; - p.terms.emplace_back(2, 1); - ASSERT_DOUBLE_EQ( 0, p.y(0.0)); - ASSERT_DOUBLE_EQ( 1, p.y(0.5)); - ASSERT_DOUBLE_EQ( 2, p.y(1.0)); - ASSERT_DOUBLE_EQ(-1, p.y(-0.5)); - ASSERT_DOUBLE_EQ(-2, p.y(-1.0)); -} - -TEST(PolynomialTest, thirdOrder) { - Polynomial p; - p.terms.emplace_back(2, 1); - p.terms.emplace_back(1.5, 3); - - ASSERT_DOUBLE_EQ( 0.0000, p.y(0.0)); - ASSERT_DOUBLE_EQ( 1.1875, p.y(0.5)); - ASSERT_DOUBLE_EQ( 3.5000, p.y(1.0)); - ASSERT_DOUBLE_EQ(-1.1875, p.y(-0.5)); - ASSERT_DOUBLE_EQ(-3.5000, p.y(-1.0)); -} - -TEST(PolynomialTest, fourthOrder) { - Polynomial p; - p.terms.emplace_back(1, 2); - p.terms.emplace_back(2, 4); - - ASSERT_DOUBLE_EQ( 0.000, p.y(0.0)); - ASSERT_DOUBLE_EQ( 0.375, p.y(0.5)); - ASSERT_DOUBLE_EQ( 3.000, p.y(1.0)); - ASSERT_DOUBLE_EQ(36.000, p.y(2.0)); - ASSERT_DOUBLE_EQ( 0.375, p.y(-0.5)); - ASSERT_DOUBLE_EQ( 3.000, p.y(-1.0)); - ASSERT_DOUBLE_EQ(36.000, p.y(-2.0)); +namespace { + void requireEqual(float const & a, float const & b) { + REQUIRE_THAT(a, Catch::WithinULP(b, 0)); + } } +TEST_CASE("PolynomialTest") { + + SECTION("emptyPolynomial") { + dr::Polynomial p; + requireEqual(0, p.y(0.0)); + requireEqual(0, p.y(0.5)); + requireEqual(0, p.y(1.0)); + requireEqual(0, p.y(-0.5)); + requireEqual(0, p.y(-1.0)); + } + + SECTION("constantPolynomial") { + dr::Polynomial p; + p.terms.emplace_back(3.25, 0); + + requireEqual(3.25, p.y( 0.0)); + requireEqual(3.25, p.y( 0.5)); + requireEqual(3.25, p.y( 1.0)); + requireEqual(3.25, p.y(-0.5)); + requireEqual(3.25, p.y(-1.0)); + } + + SECTION("squareRoot") { + dr::Polynomial p; + p.terms.emplace_back(1, 0.5); + + requireEqual( 0.000, p.y(0.0)); + requireEqual( 1.000, p.y(1.0)); + requireEqual( 2.000, p.y(4.0)); + + REQUIRE(std::isnan(p.y(-1.0)) == true); + REQUIRE(std::isnan(p.y(-4.0)) == true); + } + + SECTION("firstOrder") { + dr::Polynomial p; + p.terms.emplace_back(2, 1); + + requireEqual( 0, p.y(0.0)); + requireEqual( 1, p.y(0.5)); + requireEqual( 2, p.y(1.0)); + requireEqual(-1, p.y(-0.5)); + requireEqual(-2, p.y(-1.0)); + } + + SECTION("thirdOrder") { + dr::Polynomial p; + p.terms.emplace_back(2, 1); + p.terms.emplace_back(1.5, 3); + + requireEqual( 0.0000, p.y(0.0)); + requireEqual( 1.1875, p.y(0.5)); + requireEqual( 3.5000, p.y(1.0)); + requireEqual(-1.1875, p.y(-0.5)); + requireEqual(-3.5000, p.y(-1.0)); + } + + SECTION("fourthOrder") { + dr::Polynomial p; + p.terms.emplace_back(1, 2); + p.terms.emplace_back(2, 4); + + requireEqual( 0.000, p.y(0.0)); + requireEqual( 0.375, p.y(0.5)); + requireEqual( 3.000, p.y(1.0)); + requireEqual(36.000, p.y(2.0)); + requireEqual( 0.375, p.y(-0.5)); + requireEqual( 3.000, p.y(-1.0)); + requireEqual(36.000, p.y(-2.0)); + } } diff --git a/test/resource.cpp b/test/resource.cpp index 7565401..02cb292 100644 --- a/test/resource.cpp +++ b/test/resource.cpp @@ -1,30 +1,38 @@ +// This repository #include "resource.hpp" -#include - -#include - -int main(int argc, char ** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} - -namespace dr { - -TEST(ResourceTest, packageUrl) { - ASSERT_EQ(ros::package::getPath("dr_util") + "/test.file", rosUrlToPath("package://dr_util/test.file")); -} +// Catch2 +#include -TEST(ResourceTest, localFileUrl) { - ASSERT_EQ("/test.file", rosUrlToPath("file:///test.file")); -} - -TEST(ResourceTest, remoteFileUrl) { - ASSERT_ANY_THROW(rosUrlToPath("file://host/test.file")); -} - -TEST(ResourceTest, unsupportedScheme) { - ASSERT_ANY_THROW(rosUrlToPath("http://example.com/test.file")); -} +// ROS +#include +TEST_CASE("ResourceTest") { + + SECTION("packageUrl") { + /** + * TODO: This test fails in BuildBot because it can not resolve the location + * of the "package://some_package". At the end of a chain of calls, + * 'ROSPack::run(..)' excecutes "rospack find some_package". + * + * More about this method. + * http://docs.ros.org/indigo/api/rospack/html/rospack__backcompat_8cpp_source.html * + * + * This method succeeds if rospack is installed and located in the PATH. + * In BuildBot test-container rospack is installed, but its location is not in the PATH. + **/ + REQUIRE(ros::package::getPath("dr_util") + "/test.file" == dr::resolveResourceUrl("package://dr_util/test.file")); + } + + SECTION("localFileUrl") { + REQUIRE("/test.file" == dr::resolveResourceUrl("file:///test.file")); + } + + SECTION("remoteFileUrl") { + REQUIRE_THROWS(dr::resolveResourceUrl("file://host/test.file")); + } + + SECTION("unsupportedScheme") { + REQUIRE_THROWS(dr::resolveResourceUrl("http://example.com/test.file")); + } } diff --git a/test/timestamp.cpp b/test/timestamp.cpp index fa0ec92..d3d29e7 100644 --- a/test/timestamp.cpp +++ b/test/timestamp.cpp @@ -1,64 +1,61 @@ +// This repository #include "../src/chrono_ptime.hpp" -#include +// Catch +#include +// C++ #include #include #include -int main(int argc, char ** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} - namespace dr { -TEST(timestamp, system_clock_epoch) { - std::time_t epoch = std::chrono::system_clock::to_time_t(std::chrono::system_clock::time_point{}); - std::tm * date = std::gmtime(&epoch); - ASSERT_EQ(date->tm_year, 70); - ASSERT_EQ(date->tm_mon, 0); - ASSERT_EQ(date->tm_mday, 1); - ASSERT_EQ(date->tm_hour, 0); - ASSERT_EQ(date->tm_min, 0); - ASSERT_EQ(date->tm_sec, 0); - ASSERT_EQ(date->tm_yday, 0); - ASSERT_EQ(date->tm_isdst, 0); -} - -TEST(timestamp, chrono_ptime_epoch) { - boost::posix_time::ptime epoch = toPtime(std::chrono::system_clock::time_point{}); - ASSERT_EQ(epoch.date().year(), 1970); - ASSERT_EQ(epoch.date().month(), 1); - ASSERT_EQ(epoch.date().day(), 1); - ASSERT_EQ(epoch.date().day_of_year(), 1); - ASSERT_EQ(epoch.time_of_day(), boost::posix_time::seconds(0)); -} - -TEST(timestamp, chrono_ptime_2017) { - setenv("TZ", "UTC", true); - ::tzset(); - - std::tm tm; - tm.tm_year = 117; - tm.tm_mon = 2; - tm.tm_mday = 30; - tm.tm_wday = 4; - tm.tm_hour = 13; - tm.tm_min = 37; - tm.tm_sec = 42; - - boost::posix_time::ptime time = toPtime(std::chrono::system_clock::from_time_t(std::mktime(&tm))); - - ASSERT_EQ(time.date().year(), 2017); - ASSERT_EQ(time.date().month(), boost::gregorian::Mar); - ASSERT_EQ(time.date().day(), 30); - ASSERT_EQ(time.date().day_of_year(), 89); - ASSERT_EQ(time.date().day_of_week(), boost::gregorian::Thursday); - ASSERT_EQ(time.time_of_day().hours(), 13); - ASSERT_EQ(time.time_of_day().minutes(), 37); - ASSERT_EQ(time.time_of_day().seconds(), 42); +TEST_CASE("timestamp") { + + SECTION("system_clock_epoch") { + std::time_t epoch = std::chrono::system_clock::to_time_t(std::chrono::system_clock::time_point{}); + std::tm * date = std::gmtime(&epoch); + REQUIRE(date->tm_year == 70); + REQUIRE(date->tm_mon == 0); + REQUIRE(date->tm_mday == 1); + REQUIRE(date->tm_hour == 0); + REQUIRE(date->tm_min == 0); + REQUIRE(date->tm_sec == 0); + REQUIRE(date->tm_yday == 0); + REQUIRE(date->tm_isdst == 0); + } + + SECTION("chrono_ptime_epoch") { + boost::posix_time::ptime epoch = toPtime(std::chrono::system_clock::time_point{}); + REQUIRE(epoch.date().year() == 1970); + REQUIRE(epoch.date().month() == 1); + REQUIRE(epoch.date().day() == 1); + REQUIRE(epoch.date().day_of_year() == 1); + REQUIRE(epoch.time_of_day() == boost::posix_time::seconds(0)); + } + + SECTION("chrono_ptime_2017") { + std::tm tm; + tm.tm_year = 117; + tm.tm_mon = 2; + tm.tm_mday = 30; + tm.tm_wday = 4; + tm.tm_hour = 13; + tm.tm_min = 37; + tm.tm_sec = 42; + + boost::posix_time::ptime time = toPtime(std::chrono::system_clock::from_time_t(std::mktime(&tm))); + + REQUIRE(time.date().year() == 2017); + REQUIRE(time.date().month() == boost::gregorian::Mar); + REQUIRE(time.date().day() == 30); + REQUIRE(time.date().day_of_year() == 89); + REQUIRE(time.date().day_of_week() == boost::gregorian::Thursday); + REQUIRE(time.time_of_day().hours() == 11); + REQUIRE(time.time_of_day().minutes() == 37); + REQUIRE(time.time_of_day().seconds() == 42); + } } - } diff --git a/test/tuple.cpp b/test/tuple.cpp index 6293f4a..0cfb0c6 100644 --- a/test/tuple.cpp +++ b/test/tuple.cpp @@ -1,54 +1,53 @@ +// This repository +#include "tuple.hpp" + +// Catch +#include + +// C++ #include #include #include -#include - -#include "tuple.hpp" - -using namespace dr; - -int main(int argc, char * * argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} - -TEST(TupleTest, sliceOne) { - auto tuple = std::make_tuple(1, true, 3); - EXPECT_EQ(1u, std::tuple_size(tuple))>::value); - EXPECT_EQ(1u, std::tuple_size(tuple))>::value); - EXPECT_EQ(1u, std::tuple_size(tuple))>::value); - EXPECT_EQ(1, std::get<0>(tuple_slice<0, 1>(tuple))); - EXPECT_EQ(true, std::get<0>(tuple_slice<1, 1>(tuple))); - EXPECT_EQ(3, std::get<0>(tuple_slice<2, 1>(tuple))); -} - -TEST(TupleTest, sliceTwo) { - auto tuple = std::make_tuple(1, true, 3); - EXPECT_EQ(2u, std::tuple_size(tuple))>::value); - EXPECT_EQ(2u, std::tuple_size(tuple))>::value); - EXPECT_EQ(1, std::get<0>(tuple_slice<0, 2>(tuple))); - EXPECT_EQ(true, std::get<1>(tuple_slice<0, 2>(tuple))); - EXPECT_EQ(true, std::get<0>(tuple_slice<1, 2>(tuple))); - EXPECT_EQ(3, std::get<1>(tuple_slice<1, 2>(tuple))); -} - -TEST(TupleTest, sliceThree) { - auto tuple = std::make_tuple(1, true, 3); - EXPECT_EQ(3u, std::tuple_size(tuple))>::value); - EXPECT_EQ(1, std::get<0>(tuple_slice<0, 3>(tuple))); - EXPECT_EQ(true, std::get<1>(tuple_slice<0, 3>(tuple))); - EXPECT_EQ(3, std::get<2>(tuple_slice<0, 3>(tuple))); -} - -TEST(TupleTest, tail) { - auto tuple = std::make_tuple(1, true, 3); - EXPECT_EQ(2u, std::tuple_size::value); - EXPECT_EQ(true, std::get<0>(tuple_tail(tuple))); - EXPECT_EQ(3, std::get<1>(tuple_tail(tuple))); -} - -TEST(TupleTest, tailEmpty) { - auto tuple = std::make_tuple(1); - EXPECT_EQ(0u, std::tuple_size::value); -} +TEST_CASE("TupleTest") { + + SECTION("sliceOne") { + auto tuple = std::make_tuple(1, true, 3); + REQUIRE(1u == std::tuple_size(tuple))>::value); + REQUIRE(1u == std::tuple_size(tuple))>::value); + REQUIRE(1u == std::tuple_size(tuple))>::value); + REQUIRE(1 == std::get<0>(dr::tuple_slice<0, 1>(tuple))); + REQUIRE(true == std::get<0>(dr::tuple_slice<1, 1>(tuple))); + REQUIRE(3 == std::get<0>(dr::tuple_slice<2, 1>(tuple))); + } + + SECTION("sliceTwo") { + auto tuple = std::make_tuple(1, true, 3); + REQUIRE(2u == std::tuple_size(tuple))>::value); + REQUIRE(2u == std::tuple_size(tuple))>::value); + REQUIRE(1 == std::get<0>(dr::tuple_slice<0, 2>(tuple))); + REQUIRE(true == std::get<1>(dr::tuple_slice<0, 2>(tuple))); + REQUIRE(true == std::get<0>(dr::tuple_slice<1, 2>(tuple))); + REQUIRE(3 == std::get<1>(dr::tuple_slice<1, 2>(tuple))); + } + + SECTION("sliceThree") { + auto tuple = std::make_tuple(1, true, 3); + REQUIRE(3u == std::tuple_size(tuple))>::value); + REQUIRE(1 == std::get<0>(dr::tuple_slice<0, 3>(tuple))); + REQUIRE(true == std::get<1>(dr::tuple_slice<0, 3>(tuple))); + REQUIRE(3 == std::get<2>(dr::tuple_slice<0, 3>(tuple))); + } + + SECTION("tail") { + auto tuple = std::make_tuple(1, true, 3); + REQUIRE(2u == std::tuple_size::value); + REQUIRE(true == std::get<0>(dr::tuple_tail(tuple))); + REQUIRE(3 == std::get<1>(dr::tuple_tail(tuple))); + } + + SECTION("tailEmpty") { + auto tuple = std::make_tuple(1); + REQUIRE(0u == std::tuple_size::value); + } +} \ No newline at end of file diff --git a/test/void_t.cpp b/test/void_t.cpp index a5ce2d6..36fe88f 100644 --- a/test/void_t.cpp +++ b/test/void_t.cpp @@ -1,22 +1,13 @@ +// This repository #include "void_t.hpp" -#include - -using namespace dr; - -int main(int argc, char * * argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} - -namespace dr { - -TEST(VoidT, type) { - static_assert(std::is_same>{}, ""); - static_assert(std::is_same>{}, ""); - static_assert(std::is_same>{}, ""); - static_assert(std::is_same>{}, ""); - ASSERT_TRUE(true); -} +// Catch +#include +TEST_CASE("VoidT", "type") { + static_assert(std::is_same>{}, ""); + static_assert(std::is_same>{}, ""); + static_assert(std::is_same>{}, ""); + static_assert(std::is_same>{}, ""); + SUCCEED(); }