You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
4.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2016 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "api/function_view.h"
  11. #include <memory>
  12. #include <utility>
  13. #include "test/gtest.h"
  14. namespace rtc {
  15. namespace {
  16. int CallWith33(rtc::FunctionView<int(int)> fv) {
  17. return fv ? fv(33) : -1;
  18. }
  19. int Add33(int x) {
  20. return x + 33;
  21. }
  22. } // namespace
  23. // Test the main use case of FunctionView: implicitly converting a callable
  24. // argument.
  25. TEST(FunctionViewTest, ImplicitConversion) {
  26. EXPECT_EQ(38, CallWith33([](int x) { return x + 5; }));
  27. EXPECT_EQ(66, CallWith33(Add33));
  28. EXPECT_EQ(-1, CallWith33(nullptr));
  29. }
  30. TEST(FunctionViewTest, IntIntLambdaWithoutState) {
  31. auto f = [](int x) { return x + 1; };
  32. EXPECT_EQ(18, f(17));
  33. rtc::FunctionView<int(int)> fv(f);
  34. EXPECT_TRUE(fv);
  35. EXPECT_EQ(18, fv(17));
  36. }
  37. TEST(FunctionViewTest, IntVoidLambdaWithState) {
  38. int x = 13;
  39. auto f = [x]() mutable { return ++x; };
  40. rtc::FunctionView<int()> fv(f);
  41. EXPECT_TRUE(fv);
  42. EXPECT_EQ(14, f());
  43. EXPECT_EQ(15, fv());
  44. EXPECT_EQ(16, f());
  45. EXPECT_EQ(17, fv());
  46. }
  47. TEST(FunctionViewTest, IntIntFunction) {
  48. rtc::FunctionView<int(int)> fv(Add33);
  49. EXPECT_TRUE(fv);
  50. EXPECT_EQ(50, fv(17));
  51. }
  52. TEST(FunctionViewTest, IntIntFunctionPointer) {
  53. rtc::FunctionView<int(int)> fv(&Add33);
  54. EXPECT_TRUE(fv);
  55. EXPECT_EQ(50, fv(17));
  56. }
  57. TEST(FunctionViewTest, Null) {
  58. // These two call constructors that statically construct null FunctionViews.
  59. EXPECT_FALSE(rtc::FunctionView<int()>());
  60. EXPECT_FALSE(rtc::FunctionView<int()>(nullptr));
  61. // This calls the constructor for function pointers.
  62. EXPECT_FALSE(rtc::FunctionView<int()>(reinterpret_cast<int (*)()>(0)));
  63. }
  64. // Ensure that FunctionView handles move-only arguments and return values.
  65. TEST(FunctionViewTest, UniquePtrPassthrough) {
  66. auto f = [](std::unique_ptr<int> x) { return x; };
  67. rtc::FunctionView<std::unique_ptr<int>(std::unique_ptr<int>)> fv(f);
  68. std::unique_ptr<int> x(new int);
  69. int* x_addr = x.get();
  70. auto y = fv(std::move(x));
  71. EXPECT_EQ(x_addr, y.get());
  72. }
  73. TEST(FunctionViewTest, CopyConstructor) {
  74. auto f17 = [] { return 17; };
  75. rtc::FunctionView<int()> fv1(f17);
  76. rtc::FunctionView<int()> fv2(fv1);
  77. EXPECT_EQ(17, fv1());
  78. EXPECT_EQ(17, fv2());
  79. }
  80. TEST(FunctionViewTest, MoveConstructorIsCopy) {
  81. auto f17 = [] { return 17; };
  82. rtc::FunctionView<int()> fv1(f17);
  83. rtc::FunctionView<int()> fv2(std::move(fv1)); // NOLINT
  84. EXPECT_EQ(17, fv1());
  85. EXPECT_EQ(17, fv2());
  86. }
  87. TEST(FunctionViewTest, CopyAssignment) {
  88. auto f17 = [] { return 17; };
  89. rtc::FunctionView<int()> fv1(f17);
  90. auto f23 = [] { return 23; };
  91. rtc::FunctionView<int()> fv2(f23);
  92. EXPECT_EQ(17, fv1());
  93. EXPECT_EQ(23, fv2());
  94. fv2 = fv1;
  95. EXPECT_EQ(17, fv1());
  96. EXPECT_EQ(17, fv2());
  97. }
  98. TEST(FunctionViewTest, MoveAssignmentIsCopy) {
  99. auto f17 = [] { return 17; };
  100. rtc::FunctionView<int()> fv1(f17);
  101. auto f23 = [] { return 23; };
  102. rtc::FunctionView<int()> fv2(f23);
  103. EXPECT_EQ(17, fv1());
  104. EXPECT_EQ(23, fv2());
  105. fv2 = std::move(fv1); // NOLINT
  106. EXPECT_EQ(17, fv1());
  107. EXPECT_EQ(17, fv2());
  108. }
  109. TEST(FunctionViewTest, Swap) {
  110. auto f17 = [] { return 17; };
  111. rtc::FunctionView<int()> fv1(f17);
  112. auto f23 = [] { return 23; };
  113. rtc::FunctionView<int()> fv2(f23);
  114. EXPECT_EQ(17, fv1());
  115. EXPECT_EQ(23, fv2());
  116. using std::swap;
  117. swap(fv1, fv2);
  118. EXPECT_EQ(23, fv1());
  119. EXPECT_EQ(17, fv2());
  120. }
  121. // Ensure that when you copy-construct a FunctionView, the new object points to
  122. // the same function as the old one (as opposed to the new object pointing to
  123. // the old one).
  124. TEST(FunctionViewTest, CopyConstructorChaining) {
  125. auto f17 = [] { return 17; };
  126. rtc::FunctionView<int()> fv1(f17);
  127. rtc::FunctionView<int()> fv2(fv1);
  128. EXPECT_EQ(17, fv1());
  129. EXPECT_EQ(17, fv2());
  130. auto f23 = [] { return 23; };
  131. fv1 = f23;
  132. EXPECT_EQ(23, fv1());
  133. EXPECT_EQ(17, fv2());
  134. }
  135. // Ensure that when you assign one FunctionView to another, we actually make a
  136. // copy (as opposed to making the second FunctionView point to the first one).
  137. TEST(FunctionViewTest, CopyAssignmentChaining) {
  138. auto f17 = [] { return 17; };
  139. rtc::FunctionView<int()> fv1(f17);
  140. rtc::FunctionView<int()> fv2;
  141. EXPECT_TRUE(fv1);
  142. EXPECT_EQ(17, fv1());
  143. EXPECT_FALSE(fv2);
  144. fv2 = fv1;
  145. EXPECT_EQ(17, fv1());
  146. EXPECT_EQ(17, fv2());
  147. auto f23 = [] { return 23; };
  148. fv1 = f23;
  149. EXPECT_EQ(23, fv1());
  150. EXPECT_EQ(17, fv2());
  151. }
  152. } // namespace rtc