Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 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" |
[email protected] | a6d6d468 | 2012-06-19 18:34:08 | [diff] [blame] | 6 | |
Peter Kasting | 61152c2 | 2023-04-25 03:27:56 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
jbroman | 6bcfec42 | 2016-05-26 00:28:46 | [diff] [blame] | 9 | #include <string> |
Jan Wilken Dörrie | a33bc99 | 2020-03-24 17:45:31 | [diff] [blame] | 10 | #include <type_traits> |
jbroman | 6bcfec42 | 2016-05-26 00:28:46 | [diff] [blame] | 11 | |
dyaroshev | 99a394d | 2017-07-22 19:10:18 | [diff] [blame] | 12 | #include "base/containers/flat_tree.h" |
| 13 | #include "base/test/move_only_int.h" |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 14 | #include "testing/gtest/include/gtest/gtest.h" |
| 15 | |
| 16 | namespace base { |
| 17 | namespace { |
| 18 | |
jbroman | 6bcfec42 | 2016-05-26 00:28:46 | [diff] [blame] | 19 | enum SimpleEnum { SIMPLE_ENUM }; |
| 20 | enum EnumWithExplicitType : uint64_t { ENUM_WITH_EXPLICIT_TYPE }; |
| 21 | enum class ScopedEnum { SCOPED_ENUM }; |
jbroman | 6bcfec42 | 2016-05-26 00:28:46 | [diff] [blame] | 22 | struct SimpleStruct {}; |
Collin Baker | 89e9e07 | 2019-06-10 22:39:05 | [diff] [blame] | 23 | struct StructWithToString { |
| 24 | std::string ToString() const { return ""; } |
| 25 | }; |
jbroman | 6bcfec42 | 2016-05-26 00:28:46 | [diff] [blame] | 26 | |
Collin Baker | 89e9e07 | 2019-06-10 22:39:05 | [diff] [blame] | 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 | |
Jan Wilken Dörrie | f89fc2ac | 2021-03-05 06:51:22 | [diff] [blame] | 37 | // is_scoped_enum |
| 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 | |
Jan Wilken Dörrie | 94bb5bc | 2021-03-22 18:37:40 | [diff] [blame] | 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 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 72 | } // namespace |
dyaroshev | 99a394d | 2017-07-22 19:10:18 | [diff] [blame] | 73 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 74 | } // namespace base |