diff --git a/data_tamer_cpp/tests/CMakeLists.txt b/data_tamer_cpp/tests/CMakeLists.txt index b56949c..92ec5d9 100644 --- a/data_tamer_cpp/tests/CMakeLists.txt +++ b/data_tamer_cpp/tests/CMakeLists.txt @@ -4,7 +4,8 @@ include(GoogleTest) add_executable(datatamer_test dt_tests.cpp custom_types_tests.cpp - parser_tests.cpp) + parser_tests.cpp + trait_tests.cpp) gtest_discover_tests(datatamer_test DISCOVERY_MODE PRE_TEST) target_include_directories(datatamer_test diff --git a/data_tamer_cpp/tests/trait_tests.cpp b/data_tamer_cpp/tests/trait_tests.cpp new file mode 100644 index 0000000..8495fa8 --- /dev/null +++ b/data_tamer_cpp/tests/trait_tests.cpp @@ -0,0 +1,108 @@ +#include "data_tamer/contrib/SerializeMe.hpp" +#include +#include +#include + +using namespace SerializeMe; + +// non-template type with TypeDefinition +struct CustomWithTypeDef +{ + int a; + double b; +}; + +template +std::string_view TypeDefinition(CustomWithTypeDef&, AddField&) +{ + return "CustomWithTypeDef"; +} + +// non-template type without TypeDefinition +struct CustomNoTypeDef +{ + int x; +}; + +// a template but non-variadic custom type +template +struct TemplateCustomType +{ + T type; +}; + +template +std::string_view TypeDefinition(TemplateCustomType&, AddField&) +{ + return "TemplateCustomType"; +} + +// type with variadic parameters but *not* a container type +template +struct VariadicCustomType +{ + int id; + std::tuple values; +}; + +template +std::string_view TypeDefinition(VariadicCustomType&, AddField&) +{ + return "VariadicCustomType"; +} + +// check that all the traits work as expected at compile time +// the below tests run, but are here primarily to ensure compilation works as intended for user-defined types +// the static asserts are the crux of the test +static_assert(has_TypeDefinition::value, "CustomWithTypeDef should " + "have TypeDefinition"); +static_assert(!has_TypeDefinition::value, "CustomNoTypeDef should not " + "have TypeDefinition"); +static_assert(!has_TypeDefinition>::value, "std::vector should not " + "have TypeDefinition"); +static_assert(!has_TypeDefinition>::value, "std::array " + "should not have " + "TypeDefinition"); +static_assert(has_TypeDefinition>::value, "MyCustomType<" + "int, " + "double> " + "should have " + "TypeDefinitio" + "n"); + +static_assert(has_TypeDefinition>::value, "TemplateCustomType<" + "int> should have " + "TypeDefinition"); + +// check that BufferSize works on the types +TEST(CustomTypeTrait, BufferSize) +{ + // container of custom type works because it uses the container overload even though the contained type has no TypeDefinition + std::vector c{ { 1 }, { 2 }, { 3 } }; + EXPECT_NO_THROW(BufferSize(c)); + + // std containers of std types + std::vector v{ 1, 2, 3 }; + EXPECT_NO_THROW((BufferSize(v))); + std::array arr{ { 4.0, 5.0 } }; + EXPECT_NO_THROW((BufferSize(arr))); + + // template custom type + TemplateCustomType template_type_def_type{ 31415 }; + EXPECT_NO_THROW((BufferSize(template_type_def_type))); + + // variadic custom type + VariadicCustomType variadic_type_def_type{ 1, std::make_tuple(2, 3.) }; + EXPECT_NO_THROW((BufferSize(variadic_type_def_type))); + + // container overload for custom type with TypeDefinition + std::vector vector_of_type_def{ { 1, 2. }, { 3, 4. } }; + EXPECT_NO_THROW((BufferSize(vector_of_type_def))); + + // non-variadic custom types + CustomWithTypeDef type_def_type{}; + EXPECT_NO_THROW((BufferSize(type_def_type))); + CustomNoTypeDef no_type_def{}; + // this wouldn't compile since it isn't in a container and has no type def + // BufferSize(no_type_def); +}