cxx17_backports_unittest.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright 2021 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/cxx17_backports.h"
  5. #include <array>
  6. #include <memory>
  7. #include <tuple>
  8. #include <type_traits>
  9. #include <utility>
  10. #include <vector>
  11. #include "base/test/gtest_util.h"
  12. #include "testing/gmock/include/gmock/gmock.h"
  13. #include "testing/gtest/include/gtest/gtest.h"
  14. namespace base {
  15. namespace {
  16. struct OneType {
  17. int some_int;
  18. };
  19. bool operator<(const OneType& lhs, const OneType& rhs) {
  20. return lhs.some_int < rhs.some_int;
  21. }
  22. bool operator==(const OneType& lhs, const OneType& rhs) {
  23. return lhs.some_int == rhs.some_int;
  24. }
  25. struct AnotherType {
  26. int some_other_int;
  27. };
  28. bool operator==(const AnotherType& lhs, const AnotherType& rhs) {
  29. return lhs.some_other_int == rhs.some_other_int;
  30. }
  31. TEST(Cxx17BackportTest, Clamp) {
  32. EXPECT_EQ(0, base::clamp(-5, 0, 10));
  33. EXPECT_EQ(0, base::clamp(0, 0, 10));
  34. EXPECT_EQ(3, base::clamp(3, 0, 10));
  35. EXPECT_EQ(10, base::clamp(10, 0, 10));
  36. EXPECT_EQ(10, base::clamp(15, 0, 10));
  37. EXPECT_EQ(0.0, base::clamp(-5.0, 0.0, 10.0));
  38. EXPECT_EQ(0.0, base::clamp(0.0, 0.0, 10.0));
  39. EXPECT_EQ(3.0, base::clamp(3.0, 0.0, 10.0));
  40. EXPECT_EQ(10.0, base::clamp(10.0, 0.0, 10.0));
  41. EXPECT_EQ(10.0, base::clamp(15.0, 0.0, 10.0));
  42. EXPECT_EQ(0, base::clamp(-5, 0, 0));
  43. EXPECT_EQ(0, base::clamp(0, 0, 0));
  44. EXPECT_EQ(0, base::clamp(3, 0, 0));
  45. OneType one_type_neg5{-5};
  46. OneType one_type_0{0};
  47. OneType one_type_3{3};
  48. OneType one_type_10{10};
  49. OneType one_type_15{15};
  50. EXPECT_EQ(one_type_0, base::clamp(one_type_neg5, one_type_0, one_type_10));
  51. EXPECT_EQ(one_type_0, base::clamp(one_type_0, one_type_0, one_type_10));
  52. EXPECT_EQ(one_type_3, base::clamp(one_type_3, one_type_0, one_type_10));
  53. EXPECT_EQ(one_type_10, base::clamp(one_type_10, one_type_0, one_type_10));
  54. EXPECT_EQ(one_type_10, base::clamp(one_type_15, one_type_0, one_type_10));
  55. AnotherType another_type_neg5{-5};
  56. AnotherType another_type_0{0};
  57. AnotherType another_type_3{3};
  58. AnotherType another_type_10{10};
  59. AnotherType another_type_15{15};
  60. auto compare_another_type = [](const auto& lhs, const auto& rhs) {
  61. return lhs.some_other_int < rhs.some_other_int;
  62. };
  63. EXPECT_EQ(another_type_0, base::clamp(another_type_neg5, another_type_0,
  64. another_type_10, compare_another_type));
  65. EXPECT_EQ(another_type_0, base::clamp(another_type_0, another_type_0,
  66. another_type_10, compare_another_type));
  67. EXPECT_EQ(another_type_3, base::clamp(another_type_3, another_type_0,
  68. another_type_10, compare_another_type));
  69. EXPECT_EQ(another_type_10,
  70. base::clamp(another_type_10, another_type_0, another_type_10,
  71. compare_another_type));
  72. EXPECT_EQ(another_type_10,
  73. base::clamp(another_type_15, another_type_0, another_type_10,
  74. compare_another_type));
  75. EXPECT_CHECK_DEATH(base::clamp(3, 10, 0));
  76. EXPECT_CHECK_DEATH(base::clamp(3.0, 10.0, 0.0));
  77. EXPECT_CHECK_DEATH(base::clamp(one_type_3, one_type_10, one_type_0));
  78. EXPECT_CHECK_DEATH(base::clamp(another_type_3, another_type_10,
  79. another_type_0, compare_another_type));
  80. }
  81. constexpr int Subtract(int a, int b) {
  82. return a - b;
  83. }
  84. int SubtractUnique(std::unique_ptr<int> a, std::unique_ptr<int> b) {
  85. return *a - *b;
  86. }
  87. TEST(Cxx17BackportTest, Apply) {
  88. // Function
  89. constexpr std::tuple<int, int> tuple1(3, 2);
  90. static_assert(base::apply(&Subtract, tuple1) == 1,
  91. "base::apply() can invoke functions as constexpr");
  92. // Function with move-only types
  93. std::tuple<std::unique_ptr<int>, std::unique_ptr<int>> tuple2(
  94. std::make_unique<int>(3), std::make_unique<int>(2));
  95. EXPECT_EQ(1, base::apply(&SubtractUnique, std::move(tuple2)));
  96. // Lambda
  97. const auto subtract_lambda = [](int a, int b) { return a - b; };
  98. EXPECT_EQ(1, base::apply(subtract_lambda, tuple1));
  99. // Member function
  100. class Foo {
  101. public:
  102. constexpr Foo(int a) : a_(a) {}
  103. constexpr int Bar(int b) const { return a_ - b; }
  104. private:
  105. int a_;
  106. };
  107. static constexpr Foo f(3);
  108. constexpr std::tuple<const Foo*, int> tuple3(&f, 2);
  109. static_assert(base::apply(&Foo::Bar, tuple3) == 1,
  110. "base::apply() can invoke member functions as constexpr");
  111. }
  112. } // namespace
  113. } // namespace base