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 ? fv(33) : -1; | |
23 } | |
24 | |
25 int Add33(int x) { | |
26 return x + 33; | |
27 } | |
28 | |
29 } // namespace | |
30 | |
31 // Test the main use case of FunctionView: implicitly converting a callable | |
32 // argument. | |
33 TEST(FunctionViewTest, ImplicitConversion) { | |
34 EXPECT_EQ(38, CallWith33([](int x) { return x + 5; })); | |
35 EXPECT_EQ(66, CallWith33(Add33)); | |
36 EXPECT_EQ(-1, CallWith33(nullptr)); | |
37 } | |
38 | |
39 TEST(FunctionViewTest, IntIntLambdaWithoutState) { | |
40 auto f = [](int x) { return x + 1; }; | |
41 EXPECT_EQ(18, f(17)); | |
42 rtc::FunctionView<int(int)> fv(f); | |
43 EXPECT_TRUE(fv); | |
44 EXPECT_EQ(18, fv(17)); | |
45 } | |
46 | |
47 TEST(FunctionViewTest, IntVoidLambdaWithState) { | |
48 int x = 13; | |
49 auto f = [x]() mutable { return ++x; }; | |
50 rtc::FunctionView<int()> fv(f); | |
51 EXPECT_TRUE(fv); | |
52 EXPECT_EQ(14, f()); | |
53 EXPECT_EQ(15, fv()); | |
54 EXPECT_EQ(16, f()); | |
55 EXPECT_EQ(17, fv()); | |
56 } | |
57 | |
58 TEST(FunctionViewTest, IntIntFunction) { | |
59 rtc::FunctionView<int(int)> fv(Add33); | |
60 EXPECT_TRUE(fv); | |
61 EXPECT_EQ(50, fv(17)); | |
62 } | |
63 | |
64 TEST(FunctionViewTest, IntIntFunctionPointer) { | |
65 rtc::FunctionView<int(int)> fv(&Add33); | |
66 EXPECT_TRUE(fv); | |
67 EXPECT_EQ(50, fv(17)); | |
68 } | |
69 | |
70 TEST(FunctionViewTest, Null) { | |
71 // These two call constructors that statically construct null FunctionViews. | |
72 EXPECT_FALSE(rtc::FunctionView<int()>()); | |
73 EXPECT_FALSE(rtc::FunctionView<int()>(nullptr)); | |
74 | |
75 // This calls the constructor for function pointers. | |
76 EXPECT_FALSE(rtc::FunctionView<int()>(reinterpret_cast<int(*)()>(0))); | |
77 } | |
78 | |
79 // Ensure that FunctionView handles move-only arguments and return values. | |
80 TEST(FunctionViewTest, UniquePtrPassthrough) { | |
81 auto f = [](std::unique_ptr<int> x) { return x; }; | |
82 rtc::FunctionView<std::unique_ptr<int>(std::unique_ptr<int>)> fv(f); | |
83 std::unique_ptr<int> x(new int); | |
84 int* x_addr = x.get(); | |
85 auto y = fv(std::move(x)); | |
86 EXPECT_EQ(x_addr, y.get()); | |
87 } | |
88 | |
89 TEST(FunctionViewTest, CopyConstructor) { | |
90 auto f17 = [] { return 17; }; | |
91 rtc::FunctionView<int()> fv1(f17); | |
92 rtc::FunctionView<int()> fv2(fv1); | |
93 EXPECT_EQ(17, fv1()); | |
94 EXPECT_EQ(17, fv2()); | |
95 } | |
96 | |
97 TEST(FunctionViewTest, MoveConstructorIsCopy) { | |
98 auto f17 = [] { return 17; }; | |
99 rtc::FunctionView<int()> fv1(f17); | |
100 rtc::FunctionView<int()> fv2(std::move(fv1)); | |
101 EXPECT_EQ(17, fv1()); | |
102 EXPECT_EQ(17, fv2()); | |
103 } | |
104 | |
105 TEST(FunctionViewTest, CopyAssignment) { | |
106 auto f17 = [] { return 17; }; | |
107 rtc::FunctionView<int()> fv1(f17); | |
108 auto f23 = [] { return 23; }; | |
109 rtc::FunctionView<int()> fv2(f23); | |
110 EXPECT_EQ(17, fv1()); | |
111 EXPECT_EQ(23, fv2()); | |
112 fv2 = fv1; | |
113 EXPECT_EQ(17, fv1()); | |
114 EXPECT_EQ(17, fv2()); | |
115 } | |
116 | |
117 TEST(FunctionViewTest, MoveAssignmentIsCopy) { | |
118 auto f17 = [] { return 17; }; | |
119 rtc::FunctionView<int()> fv1(f17); | |
120 auto f23 = [] { return 23; }; | |
121 rtc::FunctionView<int()> fv2(f23); | |
122 EXPECT_EQ(17, fv1()); | |
123 EXPECT_EQ(23, fv2()); | |
124 fv2 = std::move(fv1); | |
125 EXPECT_EQ(17, fv1()); | |
126 EXPECT_EQ(17, fv2()); | |
127 } | |
128 | |
129 TEST(FunctionViewTest, Swap) { | |
130 auto f17 = [] { return 17; }; | |
131 rtc::FunctionView<int()> fv1(f17); | |
132 auto f23 = [] { return 23; }; | |
133 rtc::FunctionView<int()> fv2(f23); | |
134 EXPECT_EQ(17, fv1()); | |
135 EXPECT_EQ(23, fv2()); | |
136 using std::swap; | |
137 swap(fv1, fv2); | |
138 EXPECT_EQ(23, fv1()); | |
139 EXPECT_EQ(17, fv2()); | |
140 } | |
141 | |
142 // Ensure that when you copy-construct a FunctionView, the new object points to | |
143 // the same function as the old one (as opposed to the new object pointing to | |
144 // the old one). | |
145 TEST(FunctionViewTest, CopyConstructorChaining) { | |
146 auto f17 = [] { return 17; }; | |
147 rtc::FunctionView<int()> fv1(f17); | |
148 rtc::FunctionView<int()> fv2(fv1); | |
149 EXPECT_EQ(17, fv1()); | |
150 EXPECT_EQ(17, fv2()); | |
151 auto f23 = [] { return 23; }; | |
152 fv1 = f23; | |
153 EXPECT_EQ(23, fv1()); | |
154 EXPECT_EQ(17, fv2()); | |
155 } | |
156 | |
157 // Ensure that when you assign one FunctionView to another, we actually make a | |
158 // copy (as opposed to making the second FunctionView point to the first one). | |
159 TEST(FunctionViewTest, CopyAssignmentChaining) { | |
160 auto f17 = [] { return 17; }; | |
161 rtc::FunctionView<int()> fv1(f17); | |
162 rtc::FunctionView<int()> fv2; | |
163 EXPECT_TRUE(fv1); | |
164 EXPECT_EQ(17, fv1()); | |
165 EXPECT_FALSE(fv2); | |
166 fv2 = fv1; | |
167 EXPECT_EQ(17, fv1()); | |
168 EXPECT_EQ(17, fv2()); | |
169 auto f23 = [] { return 23; }; | |
170 fv1 = f23; | |
171 EXPECT_EQ(23, fv1()); | |
172 EXPECT_EQ(17, fv2()); | |
173 } | |
174 | |
175 } // namespace rtc | |
OLD | NEW |