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.
4
5 #include "base/template_util.h"
6
7 #include <stdint.h>
8
9 #include <string>
10 #include <type_traits>
11
12 #include "base/containers/flat_tree.h"
13 #include "base/test/move_only_int.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace base {
17 namespace {
18
19 enum SimpleEnum { SIMPLE_ENUM };
20 enum EnumWithExplicitType : uint64_t { ENUM_WITH_EXPLICIT_TYPE };
21 enum class ScopedEnum { SCOPED_ENUM };
22 struct SimpleStruct {};
23 struct StructWithToString {
ToStringbase::__anonb19876460111::StructWithToString24 std::string ToString() const { return ""; }
25 };
26
27 // .ToString() support on structs.
28 static_assert(!internal::SupportsToString<SimpleStruct>::value,
29 "simple struct value doesn't support .ToString()");
30 static_assert(!internal::SupportsToString<const SimpleStruct&>::value,
31 "simple struct const ref doesn't support .ToString()");
32 static_assert(internal::SupportsToString<StructWithToString>::value,
33 "struct with .ToString() should be printable by value");
34 static_assert(internal::SupportsToString<const StructWithToString&>::value,
35 "struct with .ToString() should be printable by const ref");
36
37 // is_scoped_enum
TEST(TemplateUtil,IsScopedEnum)38 TEST(TemplateUtil, IsScopedEnum) {
39 static_assert(!is_scoped_enum<int>::value, "");
40 static_assert(!is_scoped_enum<SimpleEnum>::value, "");
41 static_assert(!is_scoped_enum<EnumWithExplicitType>::value, "");
42 static_assert(is_scoped_enum<ScopedEnum>::value, "");
43 }
44
TEST(TemplateUtil,RemoveCvRefT)45 TEST(TemplateUtil, RemoveCvRefT) {
46 static_assert(std::is_same<int, remove_cvref_t<const int>>::value, "");
47 static_assert(std::is_same<int, remove_cvref_t<const volatile int>>::value,
48 "");
49 static_assert(std::is_same<int, remove_cvref_t<int&>>::value, "");
50 static_assert(std::is_same<int, remove_cvref_t<const int&>>::value, "");
51 static_assert(std::is_same<int, remove_cvref_t<const volatile int&>>::value,
52 "");
53 static_assert(std::is_same<int, remove_cvref_t<int&&>>::value, "");
54 static_assert(
55 std::is_same<SimpleStruct, remove_cvref_t<const SimpleStruct&>>::value,
56 "");
57 static_assert(std::is_same<int*, remove_cvref_t<int*>>::value, "");
58
59 // Test references and pointers to arrays.
60 static_assert(std::is_same<int[3], remove_cvref_t<int[3]>>::value, "");
61 static_assert(std::is_same<int[3], remove_cvref_t<int(&)[3]>>::value, "");
62 static_assert(std::is_same<int(*)[3], remove_cvref_t<int(*)[3]>>::value, "");
63
64 // Test references and pointers to functions.
65 static_assert(std::is_same<void(int), remove_cvref_t<void(int)>>::value, "");
66 static_assert(std::is_same<void(int), remove_cvref_t<void (&)(int)>>::value,
67 "");
68 static_assert(
69 std::is_same<void (*)(int), remove_cvref_t<void (*)(int)>>::value, "");
70 }
71
72 } // namespace
73
74 } // namespace base
75