template_util_unittest.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #include "base/template_util.h"
  5. #include <string>
  6. #include <type_traits>
  7. #include "base/containers/flat_tree.h"
  8. #include "base/test/move_only_int.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. namespace base {
  11. namespace {
  12. enum SimpleEnum { SIMPLE_ENUM };
  13. enum EnumWithExplicitType : uint64_t { ENUM_WITH_EXPLICIT_TYPE };
  14. enum class ScopedEnum { SCOPED_ENUM };
  15. enum class ScopedEnumWithOperator { SCOPED_ENUM_WITH_OPERATOR };
  16. std::ostream& operator<<(std::ostream& os, ScopedEnumWithOperator v) {
  17. return os;
  18. }
  19. struct SimpleStruct {};
  20. struct StructWithOperator {};
  21. std::ostream& operator<<(std::ostream& os, const StructWithOperator& v) {
  22. return os;
  23. }
  24. struct StructWithToString {
  25. std::string ToString() const { return ""; }
  26. };
  27. // A few standard types that definitely support printing.
  28. static_assert(internal::SupportsOstreamOperator<int>::value,
  29. "ints should be printable");
  30. static_assert(internal::SupportsOstreamOperator<const char*>::value,
  31. "C strings should be printable");
  32. static_assert(internal::SupportsOstreamOperator<std::string>::value,
  33. "std::string should be printable");
  34. // Various kinds of enums operator<< support.
  35. static_assert(internal::SupportsOstreamOperator<SimpleEnum>::value,
  36. "simple enum should be printable by value");
  37. static_assert(internal::SupportsOstreamOperator<const SimpleEnum&>::value,
  38. "simple enum should be printable by const ref");
  39. static_assert(internal::SupportsOstreamOperator<EnumWithExplicitType>::value,
  40. "enum with explicit type should be printable by value");
  41. static_assert(
  42. internal::SupportsOstreamOperator<const EnumWithExplicitType&>::value,
  43. "enum with explicit type should be printable by const ref");
  44. static_assert(!internal::SupportsOstreamOperator<ScopedEnum>::value,
  45. "scoped enum should not be printable by value");
  46. static_assert(!internal::SupportsOstreamOperator<const ScopedEnum&>::value,
  47. "simple enum should not be printable by const ref");
  48. static_assert(internal::SupportsOstreamOperator<ScopedEnumWithOperator>::value,
  49. "scoped enum with operator<< should be printable by value");
  50. static_assert(
  51. internal::SupportsOstreamOperator<const ScopedEnumWithOperator&>::value,
  52. "scoped enum with operator<< should be printable by const ref");
  53. // operator<< support on structs.
  54. static_assert(!internal::SupportsOstreamOperator<SimpleStruct>::value,
  55. "simple struct should not be printable by value");
  56. static_assert(!internal::SupportsOstreamOperator<const SimpleStruct&>::value,
  57. "simple struct should not be printable by const ref");
  58. static_assert(internal::SupportsOstreamOperator<StructWithOperator>::value,
  59. "struct with operator<< should be printable by value");
  60. static_assert(
  61. internal::SupportsOstreamOperator<const StructWithOperator&>::value,
  62. "struct with operator<< should be printable by const ref");
  63. // .ToString() support on structs.
  64. static_assert(!internal::SupportsToString<SimpleStruct>::value,
  65. "simple struct value doesn't support .ToString()");
  66. static_assert(!internal::SupportsToString<const SimpleStruct&>::value,
  67. "simple struct const ref doesn't support .ToString()");
  68. static_assert(internal::SupportsToString<StructWithToString>::value,
  69. "struct with .ToString() should be printable by value");
  70. static_assert(internal::SupportsToString<const StructWithToString&>::value,
  71. "struct with .ToString() should be printable by const ref");
  72. // is_scoped_enum
  73. TEST(TemplateUtil, IsScopedEnum) {
  74. static_assert(!is_scoped_enum<int>::value, "");
  75. static_assert(!is_scoped_enum<SimpleEnum>::value, "");
  76. static_assert(!is_scoped_enum<EnumWithExplicitType>::value, "");
  77. static_assert(is_scoped_enum<ScopedEnum>::value, "");
  78. }
  79. TEST(TemplateUtil, RemoveCvRefT) {
  80. static_assert(std::is_same<int, remove_cvref_t<const int>>::value, "");
  81. static_assert(std::is_same<int, remove_cvref_t<const volatile int>>::value,
  82. "");
  83. static_assert(std::is_same<int, remove_cvref_t<int&>>::value, "");
  84. static_assert(std::is_same<int, remove_cvref_t<const int&>>::value, "");
  85. static_assert(std::is_same<int, remove_cvref_t<const volatile int&>>::value,
  86. "");
  87. static_assert(std::is_same<int, remove_cvref_t<int&&>>::value, "");
  88. static_assert(
  89. std::is_same<SimpleStruct, remove_cvref_t<const SimpleStruct&>>::value,
  90. "");
  91. static_assert(std::is_same<int*, remove_cvref_t<int*>>::value, "");
  92. // Test references and pointers to arrays.
  93. static_assert(std::is_same<int[3], remove_cvref_t<int[3]>>::value, "");
  94. static_assert(std::is_same<int[3], remove_cvref_t<int(&)[3]>>::value, "");
  95. static_assert(std::is_same<int(*)[3], remove_cvref_t<int(*)[3]>>::value, "");
  96. // Test references and pointers to functions.
  97. static_assert(std::is_same<void(int), remove_cvref_t<void(int)>>::value, "");
  98. static_assert(std::is_same<void(int), remove_cvref_t<void (&)(int)>>::value,
  99. "");
  100. static_assert(
  101. std::is_same<void (*)(int), remove_cvref_t<void (*)(int)>>::value, "");
  102. }
  103. TEST(TemplateUtil, IsConstantEvaluated) {
  104. // base::is_constant_evaluated() should return whether it is evaluated as part
  105. // of a constant expression.
  106. static_assert(is_constant_evaluated(), "");
  107. EXPECT_FALSE(is_constant_evaluated());
  108. }
  109. } // namespace
  110. } // namespace base