1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/template_util.h"
10 #include "base/containers/flat_tree.h"
11 #include "base/test/move_only_int.h"
12 #include "testing/gtest/include/gtest/gtest.h"
17 enum SimpleEnum { SIMPLE_ENUM };
18 enum EnumWithExplicitType : uint64_t { ENUM_WITH_EXPLICIT_TYPE };
19 enum class ScopedEnum { SCOPED_ENUM };
20 enum class ScopedEnumWithOperator { SCOPED_ENUM_WITH_OPERATOR };
21 std::ostream& operator<<(std::ostream& os, ScopedEnumWithOperator v) {
24 struct SimpleStruct {};
25 struct StructWithOperator {};
26 std::ostream& operator<<(std::ostream& os, const StructWithOperator& v) {
29 struct StructWithToString {
30 std::string ToString() const { return ""; }
33 // is_non_const_reference<Type>
34 static_assert(!is_non_const_reference<int>::value, "IsNonConstReference");
35 static_assert(!is_non_const_reference<const int&>::value,
36 "IsNonConstReference");
37 static_assert(is_non_const_reference<int&>::value, "IsNonConstReference");
39 // A few standard types that definitely support printing.
40 static_assert(internal::SupportsOstreamOperator<int>::value,
41 "ints should be printable");
42 static_assert(internal::SupportsOstreamOperator<const char*>::value,
43 "C strings should be printable");
44 static_assert(internal::SupportsOstreamOperator<std::string>::value,
45 "std::string should be printable");
47 // Various kinds of enums operator<< support.
48 static_assert(internal::SupportsOstreamOperator<SimpleEnum>::value,
49 "simple enum should be printable by value");
50 static_assert(internal::SupportsOstreamOperator<const SimpleEnum&>::value,
51 "simple enum should be printable by const ref");
52 static_assert(internal::SupportsOstreamOperator<EnumWithExplicitType>::value,
53 "enum with explicit type should be printable by value");
55 internal::SupportsOstreamOperator<const EnumWithExplicitType&>::value,
56 "enum with explicit type should be printable by const ref");
57 static_assert(!internal::SupportsOstreamOperator<ScopedEnum>::value,
58 "scoped enum should not be printable by value");
59 static_assert(!internal::SupportsOstreamOperator<const ScopedEnum&>::value,
60 "simple enum should not be printable by const ref");
61 static_assert(internal::SupportsOstreamOperator<ScopedEnumWithOperator>::value,
62 "scoped enum with operator<< should be printable by value");
64 internal::SupportsOstreamOperator<const ScopedEnumWithOperator&>::value,
65 "scoped enum with operator<< should be printable by const ref");
67 // operator<< support on structs.
68 static_assert(!internal::SupportsOstreamOperator<SimpleStruct>::value,
69 "simple struct should not be printable by value");
70 static_assert(!internal::SupportsOstreamOperator<const SimpleStruct&>::value,
71 "simple struct should not be printable by const ref");
72 static_assert(internal::SupportsOstreamOperator<StructWithOperator>::value,
73 "struct with operator<< should be printable by value");
75 internal::SupportsOstreamOperator<const StructWithOperator&>::value,
76 "struct with operator<< should be printable by const ref");
78 // .ToString() support on structs.
79 static_assert(!internal::SupportsToString<SimpleStruct>::value,
80 "simple struct value doesn't support .ToString()");
81 static_assert(!internal::SupportsToString<const SimpleStruct&>::value,
82 "simple struct const ref doesn't support .ToString()");
83 static_assert(internal::SupportsToString<StructWithToString>::value,
84 "struct with .ToString() should be printable by value");
85 static_assert(internal::SupportsToString<const StructWithToString&>::value,
86 "struct with .ToString() should be printable by const ref");
88 // base::is_trivially_copyable
91 TrivialCopy(int d) : data_(d) {}
97 class TrivialCopyButWithDestructor : public TrivialCopy {
99 TrivialCopyButWithDestructor(int d) : TrivialCopy(d) {}
100 ~TrivialCopyButWithDestructor() { data_ = 0; }
103 static_assert(base::is_trivially_copyable<TrivialCopy>::value,
104 "TrivialCopy should be detected as trivially copyable");
105 static_assert(!base::is_trivially_copyable<TrivialCopyButWithDestructor>::value,
106 "TrivialCopyButWithDestructor should not be detected as "
107 "trivially copyable");
111 NoCopy(const NoCopy&) = delete;
115 !base::is_trivially_copy_constructible<std::vector<NoCopy>>::value,
116 "is_trivially_copy_constructible<std::vector<T>> must be compiled.");
118 using TrueT = std::true_type;
119 using FalseT = std::false_type;
122 static_assert(std::is_same<TrueT, bool_constant<true>>::value, "");
123 static_assert(std::is_same<FalseT, bool_constant<false>>::value, "");
126 static constexpr bool value = true;
130 static constexpr bool value = false;
134 static_assert(conjunction<>::value, "");
135 static_assert(conjunction<TrueT>::value, "");
136 static_assert(!conjunction<FalseT>::value, "");
138 static_assert(conjunction<TrueT, TrueT>::value, "");
139 static_assert(!conjunction<TrueT, FalseT>::value, "");
140 static_assert(!conjunction<FalseT, TrueT>::value, "");
141 static_assert(!conjunction<FalseT, FalseT>::value, "");
143 static_assert(conjunction<TrueT, TrueT, TrueT>::value, "");
144 static_assert(!conjunction<TrueT, TrueT, FalseT>::value, "");
145 static_assert(!conjunction<TrueT, FalseT, TrueT>::value, "");
146 static_assert(!conjunction<TrueT, FalseT, FalseT>::value, "");
147 static_assert(!conjunction<FalseT, TrueT, TrueT>::value, "");
148 static_assert(!conjunction<FalseT, TrueT, FalseT>::value, "");
149 static_assert(!conjunction<FalseT, FalseT, TrueT>::value, "");
150 static_assert(!conjunction<FalseT, FalseT, FalseT>::value, "");
152 static_assert(conjunction<True>::value, "");
153 static_assert(!conjunction<False>::value, "");
156 static_assert(!disjunction<>::value, "");
157 static_assert(disjunction<TrueT>::value, "");
158 static_assert(!disjunction<FalseT>::value, "");
160 static_assert(disjunction<TrueT, TrueT>::value, "");
161 static_assert(disjunction<TrueT, FalseT>::value, "");
162 static_assert(disjunction<FalseT, TrueT>::value, "");
163 static_assert(!disjunction<FalseT, FalseT>::value, "");
165 static_assert(disjunction<TrueT, TrueT, TrueT>::value, "");
166 static_assert(disjunction<TrueT, TrueT, FalseT>::value, "");
167 static_assert(disjunction<TrueT, FalseT, TrueT>::value, "");
168 static_assert(disjunction<TrueT, FalseT, FalseT>::value, "");
169 static_assert(disjunction<FalseT, TrueT, TrueT>::value, "");
170 static_assert(disjunction<FalseT, TrueT, FalseT>::value, "");
171 static_assert(disjunction<FalseT, FalseT, TrueT>::value, "");
172 static_assert(!disjunction<FalseT, FalseT, FalseT>::value, "");
174 static_assert(disjunction<True>::value, "");
175 static_assert(!disjunction<False>::value, "");
178 static_assert(!negation<TrueT>::value, "");
179 static_assert(negation<FalseT>::value, "");
181 static_assert(!negation<True>::value, "");
182 static_assert(negation<False>::value, "");
184 static_assert(negation<negation<TrueT>>::value, "");
185 static_assert(!negation<negation<FalseT>>::value, "");
188 TEST(TemplateUtil, IsInvocable) {
190 struct Derived : Base {};
197 explicit Explicit(int) {}
200 struct CallableWithBaseButNotWithInt {
201 int operator()(int) = delete;
202 int operator()(Base) { return 42; }
206 using Fp = void (*)(Base&, int);
207 static_assert(is_invocable<Fp, Base&, int>::value, "");
208 static_assert(is_invocable<Fp, Derived&, int>::value, "");
209 static_assert(!is_invocable<Fp, const Base&, int>::value, "");
210 static_assert(!is_invocable<Fp>::value, "");
211 static_assert(!is_invocable<Fp, Base&>::value, "");
214 // Function reference
215 using Fp = void (&)(Base&, int);
216 static_assert(is_invocable<Fp, Base&, int>::value, "");
217 static_assert(is_invocable<Fp, Derived&, int>::value, "");
218 static_assert(!is_invocable<Fp, const Base&, int>::value, "");
219 static_assert(!is_invocable<Fp>::value, "");
220 static_assert(!is_invocable<Fp, Base&>::value, "");
224 using Fn = CallableWithBaseButNotWithInt;
225 static_assert(is_invocable<Fn, Base>::value, "");
226 static_assert(!is_invocable<Fn, int>::value, "");
229 // Check that the conversion to the return type is properly checked
230 using Fn = int (*)(int);
231 static_assert(is_invocable_r<Implicit, Fn, int>::value, "");
232 static_assert(is_invocable_r<double, Fn, int>::value, "");
233 static_assert(is_invocable_r<const volatile void, Fn, int>::value, "");
234 static_assert(!is_invocable_r<Explicit, Fn, int>::value, "");
236 static_assert(is_invocable_r<Implicit, Fn, double>::value, "");
237 static_assert(!is_invocable_r<double, Fn, std::string>::value, "");
238 static_assert(is_invocable_r<const volatile void, Fn, double>::value, "");
239 static_assert(!is_invocable_r<Explicit, Fn, double>::value, "");
244 TEST(TemplateUtil, IsScopedEnum) {
245 static_assert(!is_scoped_enum<int>::value, "");
246 static_assert(!is_scoped_enum<SimpleEnum>::value, "");
247 static_assert(!is_scoped_enum<EnumWithExplicitType>::value, "");
248 static_assert(is_scoped_enum<ScopedEnum>::value, "");
251 TEST(TemplateUtil, RemoveCvRefT) {
252 static_assert(std::is_same<int, remove_cvref_t<const int>>::value, "");
253 static_assert(std::is_same<int, remove_cvref_t<const volatile int>>::value,
255 static_assert(std::is_same<int, remove_cvref_t<int&>>::value, "");
256 static_assert(std::is_same<int, remove_cvref_t<const int&>>::value, "");
257 static_assert(std::is_same<int, remove_cvref_t<const volatile int&>>::value,
259 static_assert(std::is_same<int, remove_cvref_t<int&&>>::value, "");
261 std::is_same<SimpleStruct, remove_cvref_t<const SimpleStruct&>>::value,
263 static_assert(std::is_same<int*, remove_cvref_t<int*>>::value, "");
265 // Test references and pointers to arrays.
266 static_assert(std::is_same<int[3], remove_cvref_t<int[3]>>::value, "");
267 static_assert(std::is_same<int[3], remove_cvref_t<int(&)[3]>>::value, "");
268 static_assert(std::is_same<int(*)[3], remove_cvref_t<int(*)[3]>>::value, "");
270 // Test references and pointers to functions.
271 static_assert(std::is_same<void(int), remove_cvref_t<void(int)>>::value, "");
272 static_assert(std::is_same<void(int), remove_cvref_t<void (&)(int)>>::value,
275 std::is_same<void (*)(int), remove_cvref_t<void (*)(int)>>::value, "");
278 TEST(TemplateUtil, IsConstantEvaluated) {
279 // base::is_constant_evaluated() should return whether it is evaluated as part
280 // of a constant expression.
281 static_assert(is_constant_evaluated(), "");
282 EXPECT_FALSE(is_constant_evaluated());