Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration completed of testing unit from GTest to Catch2. #3

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
30 changes: 9 additions & 21 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these added?

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}
Expand All @@ -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(
Expand Down
14 changes: 14 additions & 0 deletions include/dr_util/resource.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once
#include <string>

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

}
9 changes: 9 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,13 @@

<buildtool_depend>catkin</buildtool_depend>
<depend>boost</depend>
<depend>dr_ros</depend>
<depend>roscpp</depend>
<depend>roslib</depend>

<depend>dr_log</depend>

<test_depend>catch2</test_depend>
<test_depend>dr_ros</test_depend>
<test_depend>dr_log</test_depend>
</package>
53 changes: 53 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
146 changes: 74 additions & 72 deletions test/bitwise.cpp
Original file line number Diff line number Diff line change
@@ -1,80 +1,82 @@
// This repository
#include "bitwise.hpp"

#include <gtest/gtest.h>

int main(int argc, char ** argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
// Catch
#include <catch2/catch.hpp>

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);
}
}

}
Loading