1 // Copyright 2012 The Chromium Authors
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 // A few standard types that definitely support printing.
34 static_assert(internal::SupportsOstreamOperator<int>::value,
35 "ints should be printable");
36 static_assert(internal::SupportsOstreamOperator<const char*>::value,
37 "C strings should be printable");
38 static_assert(internal::SupportsOstreamOperator<std::string>::value,
39 "std::string should be printable");
41 // Various kinds of enums operator<< support.
42 static_assert(internal::SupportsOstreamOperator<SimpleEnum>::value,
43 "simple enum should be printable by value");
44 static_assert(internal::SupportsOstreamOperator<const SimpleEnum&>::value,
45 "simple enum should be printable by const ref");
46 static_assert(internal::SupportsOstreamOperator<EnumWithExplicitType>::value,
47 "enum with explicit type should be printable by value");
49 internal::SupportsOstreamOperator<const EnumWithExplicitType&>::value,
50 "enum with explicit type should be printable by const ref");
51 static_assert(!internal::SupportsOstreamOperator<ScopedEnum>::value,
52 "scoped enum should not be printable by value");
53 static_assert(!internal::SupportsOstreamOperator<const ScopedEnum&>::value,
54 "simple enum should not be printable by const ref");
55 static_assert(internal::SupportsOstreamOperator<ScopedEnumWithOperator>::value,
56 "scoped enum with operator<< should be printable by value");
58 internal::SupportsOstreamOperator<const ScopedEnumWithOperator&>::value,
59 "scoped enum with operator<< should be printable by const ref");
61 // operator<< support on structs.
62 static_assert(!internal::SupportsOstreamOperator<SimpleStruct>::value,
63 "simple struct should not be printable by value");
64 static_assert(!internal::SupportsOstreamOperator<const SimpleStruct&>::value,
65 "simple struct should not be printable by const ref");
66 static_assert(internal::SupportsOstreamOperator<StructWithOperator>::value,
67 "struct with operator<< should be printable by value");
69 internal::SupportsOstreamOperator<const StructWithOperator&>::value,
70 "struct with operator<< should be printable by const ref");
72 // .ToString() support on structs.
73 static_assert(!internal::SupportsToString<SimpleStruct>::value,
74 "simple struct value doesn't support .ToString()");
75 static_assert(!internal::SupportsToString<const SimpleStruct&>::value,
76 "simple struct const ref doesn't support .ToString()");
77 static_assert(internal::SupportsToString<StructWithToString>::value,
78 "struct with .ToString() should be printable by value");
79 static_assert(internal::SupportsToString<const StructWithToString&>::value,
80 "struct with .ToString() should be printable by const ref");
83 TEST(TemplateUtil, IsScopedEnum) {
84 static_assert(!is_scoped_enum<int>::value, "");
85 static_assert(!is_scoped_enum<SimpleEnum>::value, "");
86 static_assert(!is_scoped_enum<EnumWithExplicitType>::value, "");
87 static_assert(is_scoped_enum<ScopedEnum>::value, "");
90 TEST(TemplateUtil, RemoveCvRefT) {
91 static_assert(std::is_same<int, remove_cvref_t<const int>>::value, "");
92 static_assert(std::is_same<int, remove_cvref_t<const volatile int>>::value,
94 static_assert(std::is_same<int, remove_cvref_t<int&>>::value, "");
95 static_assert(std::is_same<int, remove_cvref_t<const int&>>::value, "");
96 static_assert(std::is_same<int, remove_cvref_t<const volatile int&>>::value,
98 static_assert(std::is_same<int, remove_cvref_t<int&&>>::value, "");
100 std::is_same<SimpleStruct, remove_cvref_t<const SimpleStruct&>>::value,
102 static_assert(std::is_same<int*, remove_cvref_t<int*>>::value, "");
104 // Test references and pointers to arrays.
105 static_assert(std::is_same<int[3], remove_cvref_t<int[3]>>::value, "");
106 static_assert(std::is_same<int[3], remove_cvref_t<int(&)[3]>>::value, "");
107 static_assert(std::is_same<int(*)[3], remove_cvref_t<int(*)[3]>>::value, "");
109 // Test references and pointers to functions.
110 static_assert(std::is_same<void(int), remove_cvref_t<void(int)>>::value, "");
111 static_assert(std::is_same<void(int), remove_cvref_t<void (&)(int)>>::value,
114 std::is_same<void (*)(int), remove_cvref_t<void (*)(int)>>::value, "");