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, "");