Skip to content

Commit f0faea6

Browse files
add trait 'tests'
1 parent d3216ea commit f0faea6

File tree

2 files changed

+110
-1
lines changed

2 files changed

+110
-1
lines changed

data_tamer_cpp/tests/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ include(GoogleTest)
44
add_executable(datatamer_test
55
dt_tests.cpp
66
custom_types_tests.cpp
7-
parser_tests.cpp)
7+
parser_tests.cpp
8+
trait_tests.cpp)
89
gtest_discover_tests(datatamer_test DISCOVERY_MODE PRE_TEST)
910

1011
target_include_directories(datatamer_test
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#include "data_tamer/contrib/SerializeMe.hpp"
2+
#include <gtest/gtest.h>
3+
#include <vector>
4+
#include <array>
5+
6+
using namespace SerializeMe;
7+
8+
// non-template type with TypeDefinition
9+
struct CustomWithTypeDef
10+
{
11+
int a;
12+
double b;
13+
};
14+
15+
template <typename AddField>
16+
std::string_view TypeDefinition(CustomWithTypeDef&, AddField&)
17+
{
18+
return "CustomWithTypeDef";
19+
}
20+
21+
// non-template type without TypeDefinition
22+
struct CustomNoTypeDef
23+
{
24+
int x;
25+
};
26+
27+
// a template but non-variadic custom type
28+
template <typename T>
29+
struct TemplateCustomType
30+
{
31+
T type;
32+
};
33+
34+
template <typename T, typename AddField>
35+
std::string_view TypeDefinition(TemplateCustomType<T>&, AddField&)
36+
{
37+
return "TemplateCustomType";
38+
}
39+
40+
// type with variadic parameters but *not* a container type
41+
template <typename... Args>
42+
struct VariadicCustomType
43+
{
44+
int id;
45+
std::tuple<Args...> values;
46+
};
47+
48+
template <typename... Args, typename AddField>
49+
std::string_view TypeDefinition(VariadicCustomType<Args...>&, AddField&)
50+
{
51+
return "VariadicCustomType";
52+
}
53+
54+
// check that all the traits work as expected at compile time
55+
// the below tests run, but are here primarily to ensure compilation works as intended for user-defined types
56+
// the static asserts are the crux of the test
57+
static_assert(has_TypeDefinition<CustomWithTypeDef>::value, "CustomWithTypeDef should "
58+
"have TypeDefinition");
59+
static_assert(!has_TypeDefinition<CustomNoTypeDef>::value, "CustomNoTypeDef should not "
60+
"have TypeDefinition");
61+
static_assert(!has_TypeDefinition<std::vector<int>>::value, "std::vector<int> should not "
62+
"have TypeDefinition");
63+
static_assert(!has_TypeDefinition<std::array<double, 3>>::value, "std::array<double, 3> "
64+
"should not have "
65+
"TypeDefinition");
66+
static_assert(has_TypeDefinition<VariadicCustomType<int, double>>::value, "MyCustomType<"
67+
"int, "
68+
"double> "
69+
"should have "
70+
"TypeDefinitio"
71+
"n");
72+
73+
static_assert(has_TypeDefinition<TemplateCustomType<int>>::value, "TemplateCustomType<"
74+
"int> should have "
75+
"TypeDefinition");
76+
77+
// check that BufferSize works on the types
78+
TEST(CustomTypeTrait, BufferSize)
79+
{
80+
// container of custom type works because it uses the container overload even though the contained type has no TypeDefinition
81+
std::vector<CustomNoTypeDef> c{ { 1 }, { 2 }, { 3 } };
82+
EXPECT_NO_THROW(BufferSize(c));
83+
84+
// std containers of std types
85+
std::vector<int> v{ 1, 2, 3 };
86+
EXPECT_NO_THROW((BufferSize(v)));
87+
std::array<double, 2> arr{ { 4.0, 5.0 } };
88+
EXPECT_NO_THROW((BufferSize(arr)));
89+
90+
// template custom type
91+
TemplateCustomType<int> template_type_def_type{ 31415 };
92+
EXPECT_NO_THROW((BufferSize(template_type_def_type)));
93+
94+
// variadic custom type
95+
VariadicCustomType<int, double> variadic_type_def_type{ 1, std::make_tuple(2, 3.) };
96+
EXPECT_NO_THROW((BufferSize(variadic_type_def_type)));
97+
98+
// container overload for custom type with TypeDefinition
99+
std::vector<CustomWithTypeDef> vector_of_type_def{ { 1, 2. }, { 3, 4. } };
100+
EXPECT_NO_THROW((BufferSize(vector_of_type_def)));
101+
102+
// non-variadic custom types
103+
CustomWithTypeDef type_def_type{};
104+
EXPECT_NO_THROW((BufferSize(type_def_type)));
105+
CustomNoTypeDef no_type_def{};
106+
// this wouldn't compile since it isn't in a container and has no type def
107+
// BufferSize(no_type_def);
108+
}

0 commit comments

Comments
 (0)