invoke_unittest.cc 981 B

12345678910111213141516171819202122232425262728293031323334
  1. // Copyright 2020 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/functional/invoke.h"
  5. #include <functional>
  6. #include "testing/gtest/include/gtest/gtest.h"
  7. namespace base {
  8. TEST(FunctionalTest, Invoke) {
  9. struct S {
  10. int i;
  11. constexpr int add(int x) const { return i + x; }
  12. };
  13. constexpr S s = {1};
  14. // Note: The tests involving a std::reference_wrapper are not static_asserts,
  15. // since std::reference_wrapper is not constexpr prior to C++20.
  16. static_assert(base::invoke(&S::add, s, 2) == 3, "");
  17. EXPECT_EQ(base::invoke(&S::add, std::ref(s), 2), 3);
  18. static_assert(base::invoke(&S::add, &s, 3) == 4, "");
  19. static_assert(base::invoke(&S::i, s) == 1, "");
  20. EXPECT_EQ(base::invoke(&S::i, std::ref(s)), 1);
  21. static_assert(base::invoke(&S::i, &s) == 1, "");
  22. static_assert(base::invoke(std::plus<>(), 1, 2) == 3, "");
  23. }
  24. } // namespace base