Micro template library A library for building device drivers
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Tinvoke.cpp 2.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*!
  2. * \file Tinvoke.cpp
  3. *
  4. * Copyright (C) 2018-2019 Christos Choutouridis
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as
  8. * published by the Free Software Foundation, either version 3
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include <utl/utility/invoke.h>
  21. #include <gtest/gtest.h>
  22. #include <type_traits>
  23. namespace test_meta {
  24. using namespace utl;
  25. /*
  26. * Types to behave like Fixtures
  27. */
  28. int Ifun(int i) { return i; }
  29. struct Ifoo {
  30. int operator() (int i) {
  31. return i;
  32. }
  33. };
  34. struct Ibar {
  35. Ibar(int num) : num_(num) {}
  36. int echo(int i) const { return i; }
  37. int get() const { return num_; }
  38. int num_;
  39. };
  40. /*
  41. * Test invoke()
  42. */
  43. TEST(Tinvoke, Invoke) {
  44. Ifoo ifoo{};
  45. const Ibar ibar{42};
  46. EXPECT_EQ (42, invoke(Ifun, 42));
  47. EXPECT_EQ (42, invoke([] () { return Ifun(42); }));
  48. EXPECT_EQ (42, invoke(Ifoo{}, 42));
  49. EXPECT_EQ (42, invoke(ifoo, 42));
  50. EXPECT_EQ (42, invoke(&Ibar::echo, ibar, 42));
  51. EXPECT_EQ (42, invoke(&Ibar::get, ibar));
  52. EXPECT_EQ (true, (is_invocable<Ifoo, int>::value));
  53. EXPECT_EQ (false, (is_invocable<Ifoo>::value));
  54. EXPECT_EQ (false, (is_invocable<Ifoo, int, double>::value));
  55. EXPECT_EQ (false, (is_invocable<Ibar, int>::value));
  56. EXPECT_EQ (true, (is_invocable_r<int, Ifoo, int>::value));
  57. EXPECT_EQ (false, (is_invocable_r<int*, Ifoo, int>::value));
  58. EXPECT_EQ (false, (is_invocable_r<int, Ifoo>::value));
  59. EXPECT_EQ (true, (std::is_same<int, invoke_result_t<Ifoo, int>>()));
  60. EXPECT_EQ (true, (std::is_same<meta::nil_, invoke_result_t<Ifoo>>()));
  61. EXPECT_EQ (true, (std::is_same<meta::nil_, invoke_result_t<Ifoo, int, double>>()));
  62. }
  63. }