[M85 Dev][EFL] Fix crashes at webview launch
[platform/framework/web/chromium-efl.git] / base / template_util_unittest.cc
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.
4
5 #include "base/template_util.h"
6
7 #include <string>
8 #include <type_traits>
9
10 #include "base/containers/flat_tree.h"
11 #include "base/test/move_only_int.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace base {
15 namespace {
16
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) {
22   return os;
23 }
24 struct SimpleStruct {};
25 struct StructWithOperator {};
26 std::ostream& operator<<(std::ostream& os, const StructWithOperator& v) {
27   return os;
28 }
29 struct StructWithToString {
30   std::string ToString() const { return ""; }
31 };
32
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");
38
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");
46
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");
54 static_assert(
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");
63 static_assert(
64     internal::SupportsOstreamOperator<const ScopedEnumWithOperator&>::value,
65     "scoped enum with operator<< should be printable by const ref");
66
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");
74 static_assert(
75     internal::SupportsOstreamOperator<const StructWithOperator&>::value,
76     "struct with operator<< should be printable by const ref");
77
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");
87
88 // base::is_trivially_copyable
89 class TrivialCopy {
90  public:
91   TrivialCopy(int d) : data_(d) {}
92
93  protected:
94   int data_;
95 };
96
97 class TrivialCopyButWithDestructor : public TrivialCopy {
98  public:
99   TrivialCopyButWithDestructor(int d) : TrivialCopy(d) {}
100   ~TrivialCopyButWithDestructor() { data_ = 0; }
101 };
102
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");
108
109 class NoCopy {
110  public:
111   NoCopy(const NoCopy&) = delete;
112 };
113
114 static_assert(
115     !base::is_trivially_copy_constructible<std::vector<NoCopy>>::value,
116     "is_trivially_copy_constructible<std::vector<T>> must be compiled.");
117
118 using TrueT = std::true_type;
119 using FalseT = std::false_type;
120
121 // bool_constant
122 static_assert(std::is_same<TrueT, bool_constant<true>>::value, "");
123 static_assert(std::is_same<FalseT, bool_constant<false>>::value, "");
124
125 struct True {
126   static constexpr bool value = true;
127 };
128
129 struct False {
130   static constexpr bool value = false;
131 };
132
133 // conjunction
134 static_assert(conjunction<>::value, "");
135 static_assert(conjunction<TrueT>::value, "");
136 static_assert(!conjunction<FalseT>::value, "");
137
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, "");
142
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, "");
151
152 static_assert(conjunction<True>::value, "");
153 static_assert(!conjunction<False>::value, "");
154
155 // disjunction
156 static_assert(!disjunction<>::value, "");
157 static_assert(disjunction<TrueT>::value, "");
158 static_assert(!disjunction<FalseT>::value, "");
159
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, "");
164
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, "");
173
174 static_assert(disjunction<True>::value, "");
175 static_assert(!disjunction<False>::value, "");
176
177 // negation
178 static_assert(!negation<TrueT>::value, "");
179 static_assert(negation<FalseT>::value, "");
180
181 static_assert(!negation<True>::value, "");
182 static_assert(negation<False>::value, "");
183
184 static_assert(negation<negation<TrueT>>::value, "");
185 static_assert(!negation<negation<FalseT>>::value, "");
186
187 }  // namespace
188
189 }  // namespace base