-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathTypeTraits.cpp
More file actions
152 lines (136 loc) · 5.14 KB
/
Copy pathTypeTraits.cpp
File metadata and controls
152 lines (136 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//
// Copyright (c) 2016-2017 Kris Jusiak (kris at jusiak dot net)
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include "GUnit/Detail/TypeTraits.h"
#include <gtest/gtest.h>
#include <algorithm>
#include <string>
struct a {};
namespace testing {
inline namespace v1 {
namespace detail {
TEST(TypeTraits, ShouldReturnTrueIfIsComplete) {
struct a {};
class b;
EXPECT_TRUE(is_complete<int>::value);
EXPECT_TRUE(is_complete<a>::value);
EXPECT_FALSE(is_complete<class B>::value);
EXPECT_FALSE(is_complete<b>::value);
}
TEST(TypeTraits, ShouldReturnTrueIfIsSharedPtr) {
EXPECT_FALSE(is_shared_ptr<int>::value);
EXPECT_TRUE(is_shared_ptr<std::shared_ptr<int>>::value);
EXPECT_TRUE(is_shared_ptr<const std::shared_ptr<int>>::value);
EXPECT_TRUE(is_shared_ptr<const volatile std::shared_ptr<int>>::value);
}
TEST(TypeTraits, ShouldReturnTrueIfIsBaseOf) {
struct b {};
struct a : b {};
EXPECT_TRUE((is_complete_base_of<b, a>::value));
EXPECT_FALSE((is_complete_base_of<int, double>::value));
EXPECT_FALSE((is_complete_base_of<class d, class e>::value));
}
TEST(TypeTraits, ShouldReturnTrueIfIsCallable) {
struct a {
void operator()(int) {}
};
struct b {
void operator()() const {}
};
EXPECT_TRUE(is_callable<a(int)>::value);
EXPECT_FALSE(is_callable<a()>::value);
EXPECT_TRUE(is_callable<b()>::value);
EXPECT_FALSE(is_callable<b(int, double)>::value);
}
TEST(TypeTraits, ShouldReturnUniqueTypeId) {
struct a {};
struct b {};
EXPECT_TRUE(type_id<int>() == type_id<int>());
EXPECT_TRUE(type_id<const int>() == type_id<int>());
EXPECT_TRUE(type_id<const int>() == type_id<int const volatile>());
EXPECT_FALSE(type_id<int>() == type_id<double>());
EXPECT_FALSE(type_id<a>() == type_id<b>());
}
void f1() {}
int f2(int) { return {}; }
int f3(int, const double&) { return {}; }
struct c1 {
void f1() {}
int f2(int) { return {}; }
int f3(int, const double&) { return {}; }
};
struct c2 {
void f1() const {}
int f2(int) const { return {}; }
int f3(int, const double&) const { return {}; }
};
TEST(TypeTraits, ShouldReturnFunctionArguments) {
EXPECT_TRUE(
(std::is_same<
void, typename function_traits<decltype(&f1)>::result_type>::value));
EXPECT_TRUE(
(std::is_same<type_list<>,
typename function_traits<decltype(&f1)>::args>::value));
EXPECT_TRUE(
(std::is_same<
int, typename function_traits<decltype(&f2)>::result_type>::value));
EXPECT_TRUE(
(std::is_same<type_list<int>,
typename function_traits<decltype(&f2)>::args>::value));
EXPECT_TRUE(
(std::is_same<
int, typename function_traits<decltype(&f3)>::result_type>::value));
EXPECT_TRUE(
(std::is_same<type_list<int, const double&>,
typename function_traits<decltype(&f3)>::args>::value));
EXPECT_TRUE((std::is_same<void, typename function_traits<decltype(
&c1::f1)>::result_type>::value));
EXPECT_TRUE(
(std::is_same<type_list<>,
typename function_traits<decltype(&c1::f1)>::args>::value));
EXPECT_TRUE((std::is_same<int, typename function_traits<decltype(
&c1::f2)>::result_type>::value));
EXPECT_TRUE(
(std::is_same<type_list<int>,
typename function_traits<decltype(&c1::f2)>::args>::value));
EXPECT_TRUE((std::is_same<int, typename function_traits<decltype(
&c1::f3)>::result_type>::value));
EXPECT_TRUE(
(std::is_same<type_list<int, const double&>,
typename function_traits<decltype(&c1::f3)>::args>::value));
EXPECT_TRUE((std::is_same<void, typename function_traits<decltype(
&c2::f1)>::result_type>::value));
EXPECT_TRUE(
(std::is_same<type_list<>,
typename function_traits<decltype(&c2::f1)>::args>::value));
EXPECT_TRUE((std::is_same<int, typename function_traits<decltype(
&c2::f2)>::result_type>::value));
EXPECT_TRUE(
(std::is_same<type_list<int>,
typename function_traits<decltype(&c2::f2)>::args>::value));
EXPECT_TRUE((std::is_same<int, typename function_traits<decltype(
&c2::f3)>::result_type>::value));
EXPECT_TRUE(
(std::is_same<type_list<int, const double&>,
typename function_traits<decltype(&c2::f3)>::args>::value));
}
struct n {};
TEST(TypeTraits, ShouldGetTypeName) {
EXPECT_STREQ("int", get_type_name<int>());
EXPECT_STREQ("const double", get_type_name<const double>());
EXPECT_STREQ("a", get_type_name<a>());
#if defined(__clang__)
std::vector<std::string> expected = {"testing::v1::detail::n","testing::detail::n"}; // get_type_name result may not contain v1::
EXPECT_TRUE( std::find(expected.begin(), expected.end(), get_type_name<n>()) != expected.end() );
#elif defined(__GNUC__)
EXPECT_TRUE(std::string{get_type_name<n>()}.find("n") != std::string::npos);
#endif
// EXPECT_STREQ("a", "b");
}
} // detail
} // v1
} // testing