OLD | NEW |
(Empty) | |
| 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 |
| 11 #include <memory> |
| 12 #include <utility> |
| 13 |
| 14 #include "webrtc/base/function_view.h" |
| 15 #include "webrtc/base/gunit.h" |
| 16 |
| 17 namespace rtc { |
| 18 |
| 19 namespace { |
| 20 |
| 21 int CallWith33(rtc::FunctionView<int(int)> fv) { |
| 22 return fv(33); |
| 23 } |
| 24 |
| 25 } // namespace |
| 26 |
| 27 // Test the main use case of FunctionView: implicitly converting a lambda |
| 28 // function argument. |
| 29 TEST(FunctionViewTest, ImplicitConversion) { |
| 30 EXPECT_EQ(38, CallWith33([](int x) { return x + 5; })); |
| 31 } |
| 32 |
| 33 TEST(FunctionViewTest, IntIntLambdaWithoutState) { |
| 34 auto f = [](int x) { return x + 1; }; |
| 35 EXPECT_EQ(18, f(17)); |
| 36 rtc::FunctionView<int(int)> fv(f); |
| 37 EXPECT_EQ(18, fv(17)); |
| 38 } |
| 39 |
| 40 TEST(FunctionViewTest, IntVoidLambdaWithState) { |
| 41 int x = 13; |
| 42 auto f = [x]() mutable { return ++x; }; |
| 43 rtc::FunctionView<int()> fv(f); |
| 44 EXPECT_EQ(14, f()); |
| 45 EXPECT_EQ(15, fv()); |
| 46 EXPECT_EQ(16, f()); |
| 47 EXPECT_EQ(17, fv()); |
| 48 } |
| 49 |
| 50 // Ensure that FunctionView handles move-only arguments and return values. |
| 51 TEST(FunctionViewTest, UniquePtrPassthrough) { |
| 52 auto f = [](std::unique_ptr<int> x) { return x; }; |
| 53 rtc::FunctionView<std::unique_ptr<int>(std::unique_ptr<int>)> fv(f); |
| 54 std::unique_ptr<int> x(new int); |
| 55 int* x_addr = x.get(); |
| 56 auto y = fv(std::move(x)); |
| 57 EXPECT_EQ(x_addr, y.get()); |
| 58 } |
| 59 |
| 60 TEST(FunctionViewTest, CopyConstructor) { |
| 61 auto f17 = [] { return 17; }; |
| 62 rtc::FunctionView<int()> fv1(f17); |
| 63 rtc::FunctionView<int()> fv2(fv1); |
| 64 EXPECT_EQ(17, fv1()); |
| 65 EXPECT_EQ(17, fv2()); |
| 66 } |
| 67 |
| 68 TEST(FunctionViewTest, MoveConstructorIsCopy) { |
| 69 auto f17 = [] { return 17; }; |
| 70 rtc::FunctionView<int()> fv1(f17); |
| 71 rtc::FunctionView<int()> fv2(std::move(fv1)); |
| 72 EXPECT_EQ(17, fv1()); |
| 73 EXPECT_EQ(17, fv2()); |
| 74 } |
| 75 |
| 76 TEST(FunctionViewTest, CopyAssignment) { |
| 77 auto f17 = [] { return 17; }; |
| 78 rtc::FunctionView<int()> fv1(f17); |
| 79 auto f23 = [] { return 23; }; |
| 80 rtc::FunctionView<int()> fv2(f23); |
| 81 EXPECT_EQ(17, fv1()); |
| 82 EXPECT_EQ(23, fv2()); |
| 83 fv2 = fv1; |
| 84 EXPECT_EQ(17, fv1()); |
| 85 EXPECT_EQ(17, fv2()); |
| 86 } |
| 87 |
| 88 TEST(FunctionViewTest, MoveAssignmentIsCopy) { |
| 89 auto f17 = [] { return 17; }; |
| 90 rtc::FunctionView<int()> fv1(f17); |
| 91 auto f23 = [] { return 23; }; |
| 92 rtc::FunctionView<int()> fv2(f23); |
| 93 EXPECT_EQ(17, fv1()); |
| 94 EXPECT_EQ(23, fv2()); |
| 95 fv2 = std::move(fv1); |
| 96 EXPECT_EQ(17, fv1()); |
| 97 EXPECT_EQ(17, fv2()); |
| 98 } |
| 99 |
| 100 TEST(FunctionViewTest, Swap) { |
| 101 auto f17 = [] { return 17; }; |
| 102 rtc::FunctionView<int()> fv1(f17); |
| 103 auto f23 = [] { return 23; }; |
| 104 rtc::FunctionView<int()> fv2(f23); |
| 105 EXPECT_EQ(17, fv1()); |
| 106 EXPECT_EQ(23, fv2()); |
| 107 using std::swap; |
| 108 swap(fv1, fv2); |
| 109 EXPECT_EQ(23, fv1()); |
| 110 EXPECT_EQ(17, fv2()); |
| 111 } |
| 112 |
| 113 // Ensure that when you copy-construct a FunctionView, the new object points to |
| 114 // the same function as the old one, as opposed to the new object pointing to |
| 115 // the old one. |
| 116 TEST(FunctionViewTest, CopyConstructorChaining) { |
| 117 auto f17 = [] { return 17; }; |
| 118 rtc::FunctionView<int()> fv1(f17); |
| 119 rtc::FunctionView<int()> fv2(fv1); |
| 120 EXPECT_EQ(17, fv1()); |
| 121 EXPECT_EQ(17, fv2()); |
| 122 auto f23 = [] { return 23; }; |
| 123 fv1 = f23; |
| 124 EXPECT_EQ(23, fv1()); |
| 125 EXPECT_EQ(17, fv2()); |
| 126 } |
| 127 |
| 128 // Ensure that when you assign one FunctionView to another, we actually make a |
| 129 // copy as opposed to making the second FunctionView point to the first one. |
| 130 TEST(FunctionViewTest, CopyAssignmentChaining) { |
| 131 auto f17 = [] { return 17; }; |
| 132 rtc::FunctionView<int()> fv1(f17); |
| 133 auto f3 = [] { return 3; }; |
| 134 rtc::FunctionView<int()> fv2(f3); |
| 135 EXPECT_EQ(17, fv1()); |
| 136 EXPECT_EQ(3, fv2()); |
| 137 fv2 = fv1; |
| 138 EXPECT_EQ(17, fv1()); |
| 139 EXPECT_EQ(17, fv2()); |
| 140 auto f23 = [] { return 23; }; |
| 141 fv1 = f23; |
| 142 EXPECT_EQ(23, fv1()); |
| 143 EXPECT_EQ(17, fv2()); |
| 144 } |
| 145 |
| 146 } // namespace rtc |
OLD | NEW |