Unverified Commit fcf0ea0a authored by Justin Carpentier's avatar Justin Carpentier Committed by GitHub
Browse files

Merge pull request #1383 from jmirabel/devel

[Minor] Fix message in argument checking macros.
parents 0856656a 812245f0
Pipeline #12981 passed with stage
in 237 minutes and 35 seconds
......@@ -106,15 +106,16 @@ namespace pinocchio
/// \brief Generic macro to throw an exception in Pinocchio if the condition is not met with a given input message.
#if !defined(PINOCCHIO_NO_THROW)
#define PINOCCHIO_THROW(condition,exception_type,message) \
if (!(condition)) { throw exception_type(PINOCCHIO_STRING_LITERAL(message)); }
if (!(condition)) { throw exception_type(message); }
#else
#define PINOCCHIO_THROW(condition,exception_type,message)
#endif
#define _PINOCCHIO_EXPAND(x) x
#define _PINOCCHIO_GET_OVERRIDE_FOR_CHECK_INPUT_ARGUMENT(_1, _2, MACRO_NAME, ...) MACRO_NAME
#define _PINOCCHIO_CHECK_INPUT_ARGUMENT_2(condition, message) \
PINOCCHIO_THROW(condition,std::invalid_argument,PINOCCHIO_STRING_LITERAL(message))
PINOCCHIO_THROW(condition,std::invalid_argument,message)
#define _PINOCCHIO_CHECK_INPUT_ARGUMENT_1(condition) \
_PINOCCHIO_CHECK_INPUT_ARGUMENT_2(condition,\
......@@ -124,35 +125,28 @@ namespace pinocchio
/// \brief Macro to check an assert-like condition and throw a std::invalid_argument exception (with a message) if violated.
#define PINOCCHIO_CHECK_INPUT_ARGUMENT(...) \
_PINOCCHIO_GET_OVERRIDE_FOR_CHECK_INPUT_ARGUMENT(__VA_ARGS__,_PINOCCHIO_CHECK_INPUT_ARGUMENT_2,\
_PINOCCHIO_CHECK_INPUT_ARGUMENT_1,_PINOCCHIO_CHECK_INPUT_ARGUMENT_0)(__VA_ARGS__)
_PINOCCHIO_EXPAND(_PINOCCHIO_EXPAND(_PINOCCHIO_GET_OVERRIDE_FOR_CHECK_INPUT_ARGUMENT(__VA_ARGS__,_PINOCCHIO_CHECK_INPUT_ARGUMENT_2,\
_PINOCCHIO_CHECK_INPUT_ARGUMENT_1,_PINOCCHIO_CHECK_INPUT_ARGUMENT_0))(__VA_ARGS__))
#define _PINOCCHIO_GET_OVERRIDE_FOR_CHECK_ARGUMENT_SIZE(_1, _2, _3, MACRO_NAME, ...) MACRO_NAME
#define _PINOCCHIO_CHECK_ARGUMENT_SIZE_5(size, expected_size, size_literal, expected_size_literal, message) \
#define _PINOCCHIO_CHECK_ARGUMENT_SIZE_3(size, expected_size, message) \
if (size != expected_size) { \
std::ostringstream oss; \
oss << "wrong argument size: expected " << expected_size << ", got " << size << std::endl; \
oss << "hint: "; \
if(!std::string(message).empty()) \
oss << message << std::endl; \
else \
oss << size_literal << " is different from " << expected_size_literal << std::endl; \
oss << "hint: " << message << std::endl; \
PINOCCHIO_THROW(false, std::invalid_argument, oss.str()); \
}
#define _PINOCCHIO_CHECK_ARGUMENT_SIZE_3(size, expected_size, message) \
_PINOCCHIO_CHECK_ARGUMENT_SIZE_5(size, expected_size, PINOCCHIO_STRING_LITERAL(size), PINOCCHIO_STRING_LITERAL(expected_size), PINOCCHIO_STRING_LITERAL(message))
#define _PINOCCHIO_CHECK_ARGUMENT_SIZE_2(size, expected_size) \
_PINOCCHIO_CHECK_ARGUMENT_SIZE_5(size, expected_size, PINOCCHIO_STRING_LITERAL(size), PINOCCHIO_STRING_LITERAL(expected_size), "")
_PINOCCHIO_CHECK_ARGUMENT_SIZE_3(size, expected_size, PINOCCHIO_STRING_LITERAL(size) " is different from " PINOCCHIO_STRING_LITERAL(expected_size))
#define _PINOCCHIO_CHECK_ARGUMENT_SIZE_1
/// \brief Macro to check if the size of an element is equal to the expected size.
#define PINOCCHIO_CHECK_ARGUMENT_SIZE(...) \
_PINOCCHIO_GET_OVERRIDE_FOR_CHECK_ARGUMENT_SIZE(__VA_ARGS__,_PINOCCHIO_CHECK_ARGUMENT_SIZE_3, \
_PINOCCHIO_CHECK_ARGUMENT_SIZE_2, _PINOCCHIO_CHECK_ARGUMENT_SIZE_1)(__VA_ARGS__)
_PINOCCHIO_EXPAND(_PINOCCHIO_EXPAND(_PINOCCHIO_GET_OVERRIDE_FOR_CHECK_ARGUMENT_SIZE(__VA_ARGS__,_PINOCCHIO_CHECK_ARGUMENT_SIZE_3, \
_PINOCCHIO_CHECK_ARGUMENT_SIZE_2, _PINOCCHIO_CHECK_ARGUMENT_SIZE_1))(__VA_ARGS__))
#if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop
......
......@@ -12,7 +12,13 @@ MACRO(ADD_TEST_CFLAGS target flag)
ENDMACRO(ADD_TEST_CFLAGS)
MACRO(ADD_PINOCCHIO_UNIT_TEST NAME)
SET(PKGS ${ARGN}) # Optional arguments
SET(options HEADER_ONLY)
SET(oneValueArgs)
SET(multiValueArgs PACKAGES)
CMAKE_PARSE_ARGUMENTS(unit_test "${options}" "${oneValueArgs}"
"${multiValueArgs}" ${ARGN} )
SET(PKGS ${unit_test_PACKAGES})
SET(TEST_NAME "test-cpp-${NAME}")
ADD_UNIT_TEST(${TEST_NAME} ${NAME}.cpp)
......@@ -27,8 +33,10 @@ MACRO(ADD_PINOCCHIO_UNIT_TEST NAME)
IF(WIN32)
ADD_TEST_CFLAGS(${NAME} "-DNOMINMAX -D_USE_MATH_DEFINES")
ENDIF(WIN32)
TARGET_LINK_LIBRARIES(${TEST_NAME} PUBLIC ${PROJECT_NAME})
IF(NOT unit_test_HEADER_ONLY)
TARGET_LINK_LIBRARIES(${TEST_NAME} PUBLIC ${PROJECT_NAME})
ENDIF(NOT unit_test_HEADER_ONLY)
TARGET_LINK_LIBRARIES(${TEST_NAME} PRIVATE ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY})
ENDMACRO(ADD_PINOCCHIO_UNIT_TEST)
......@@ -40,6 +48,9 @@ ENDMACRO()
# Find Boost.UnitTestFramework
FIND_PACKAGE(Boost COMPONENTS unit_test_framework)
# Header only
ADD_PINOCCHIO_UNIT_TEST(macros HEADER_ONLY)
# Math components
ADD_PINOCCHIO_UNIT_TEST(eigen-basic-op)
ADD_PINOCCHIO_UNIT_TEST(eigen-tensor)
......@@ -74,7 +85,7 @@ IF(urdfdom_FOUND)
ENDIF(urdfdom_FOUND)
IF(BUILD_PYTHON_INTERFACE)
ADD_PINOCCHIO_UNIT_TEST(python_parser eigenpy)
ADD_PINOCCHIO_UNIT_TEST(python_parser PACKAGES eigenpy)
TARGET_INCLUDE_DIRECTORIES(test-cpp-python_parser SYSTEM PUBLIC ${PYTHON_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(test-cpp-python_parser PUBLIC ${PYWRAP})
......@@ -156,7 +167,7 @@ ENDIF(BUILD_ADVANCED_TESTING)
# Automatic differentiation
MACRO(ADD_CPPAD_UNIT_TEST name)
INCLUDE_DIRECTORIES(SYSTEM ${cppad_INCLUDE_DIR})
ADD_PINOCCHIO_UNIT_TEST(${name} cppad)
ADD_PINOCCHIO_UNIT_TEST(${name} PACKAGES cppad)
ADD_DEPENDENCIES(test-cppad test-cpp-${name})
TARGET_LINK_LIBRARIES(test-cpp-${name} PUBLIC ${cppad_LIBRARY})
ENDMACRO()
......@@ -164,7 +175,7 @@ ENDMACRO()
MACRO(ADD_CPPADCG_UNIT_TEST name)
INCLUDE_DIRECTORIES(SYSTEM ${cppad_INCLUDE_DIR})
INCLUDE_DIRECTORIES(SYSTEM ${cppadcodegen_INCLUDE_DIR})
ADD_PINOCCHIO_UNIT_TEST(${name} cppadcg)
ADD_PINOCCHIO_UNIT_TEST(${name} PACKAGES cppadcg)
ADD_DEPENDENCIES(test-cppadcg test-cpp-${name})
SET_PROPERTY(TARGET test-cpp-${name} PROPERTY CXX_STANDARD 11)
TARGET_LINK_LIBRARIES(test-cpp-${name} PUBLIC ${cppad_LIBRARY} ${CMAKE_DL_LIBS})
......@@ -193,7 +204,7 @@ IF(BUILD_WITH_AUTODIFF_SUPPORT)
ENDIF(BUILD_WITH_AUTODIFF_SUPPORT)
MACRO(ADD_CASADI_UNIT_TEST name)
ADD_PINOCCHIO_UNIT_TEST(${name} casadi)
ADD_PINOCCHIO_UNIT_TEST(${name} PACKAGES casadi)
SET_PROPERTY(TARGET test-cpp-${name} PROPERTY CXX_STANDARD 11)
TARGET_LINK_LIBRARIES(test-cpp-${name} PUBLIC casadi)
ADD_DEPENDENCIES(test-casadi test-cpp-${name})
......
//
// Copyright (c) 2021 CNRS
//
#include "pinocchio/macros.hpp"
#include <boost/test/unit_test.hpp>
#include <boost/utility/binary.hpp>
using namespace pinocchio;
std::string expected_msg;
bool check_exception_msg (const std::exception& exception)
{
BOOST_CHECK_EQUAL(expected_msg, exception.what());
return expected_msg == exception.what();
}
BOOST_AUTO_TEST_SUITE ( BOOST_TEST_MODULE )
void function_1(std::vector<int> v, int size)
{
PINOCCHIO_CHECK_ARGUMENT_SIZE(v.size(), size);
//"size of input vector should be " << size)
}
void function_2(std::vector<int> v, int size)
{
PINOCCHIO_CHECK_ARGUMENT_SIZE(v.size(), size,
"custom message " << "with stream");
}
BOOST_AUTO_TEST_CASE(test_check_arguments)
{
expected_msg = "wrong argument size: expected 2, got 3\n"
"hint: v.size() is different from size\n";
BOOST_CHECK_EXCEPTION(function_1(std::vector<int>(3), 2),
std::invalid_argument,
check_exception_msg);
expected_msg = "wrong argument size: expected 2, got 3\n"
"hint: custom message with stream\n";
BOOST_CHECK_EXCEPTION(function_2(std::vector<int>(3), 2),
std::invalid_argument,
check_exception_msg);
}
BOOST_AUTO_TEST_SUITE_END()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment