Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(156)

Side by Side Diff: webrtc/base/function_view_unittest.cc

Issue 2375023004: rtc::FunctionView improvements: accept function pointers and nullptr (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« webrtc/base/function_view.h ('K') | « webrtc/base/function_view.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include <memory> 11 #include <memory>
12 #include <utility> 12 #include <utility>
13 13
14 #include "webrtc/base/function_view.h" 14 #include "webrtc/base/function_view.h"
15 #include "webrtc/base/gunit.h" 15 #include "webrtc/base/gunit.h"
16 16
17 namespace rtc { 17 namespace rtc {
18 18
19 namespace { 19 namespace {
20 20
21 int CallWith33(rtc::FunctionView<int(int)> fv) { 21 int CallWith33(rtc::FunctionView<int(int)> fv) {
22 return fv(33); 22 return fv ? fv(33) : -1;
23 }
24
25 int Add33(int x) {
26 return x + 33;
23 } 27 }
24 28
25 } // namespace 29 } // namespace
26 30
27 // Test the main use case of FunctionView: implicitly converting a lambda 31 // Test the main use case of FunctionView: implicitly converting a callable
28 // function argument. 32 // argument.
29 TEST(FunctionViewTest, ImplicitConversion) { 33 TEST(FunctionViewTest, ImplicitConversion) {
30 EXPECT_EQ(38, CallWith33([](int x) { return x + 5; })); 34 EXPECT_EQ(38, CallWith33([](int x) { return x + 5; }));
35 EXPECT_EQ(66, CallWith33(Add33));
36 EXPECT_EQ(-1, CallWith33(nullptr));
31 } 37 }
32 38
33 TEST(FunctionViewTest, IntIntLambdaWithoutState) { 39 TEST(FunctionViewTest, IntIntLambdaWithoutState) {
34 auto f = [](int x) { return x + 1; }; 40 auto f = [](int x) { return x + 1; };
35 EXPECT_EQ(18, f(17)); 41 EXPECT_EQ(18, f(17));
36 rtc::FunctionView<int(int)> fv(f); 42 rtc::FunctionView<int(int)> fv(f);
43 EXPECT_TRUE(fv);
37 EXPECT_EQ(18, fv(17)); 44 EXPECT_EQ(18, fv(17));
38 } 45 }
39 46
40 TEST(FunctionViewTest, IntVoidLambdaWithState) { 47 TEST(FunctionViewTest, IntVoidLambdaWithState) {
41 int x = 13; 48 int x = 13;
42 auto f = [x]() mutable { return ++x; }; 49 auto f = [x]() mutable { return ++x; };
43 rtc::FunctionView<int()> fv(f); 50 rtc::FunctionView<int()> fv(f);
51 EXPECT_TRUE(fv);
44 EXPECT_EQ(14, f()); 52 EXPECT_EQ(14, f());
45 EXPECT_EQ(15, fv()); 53 EXPECT_EQ(15, fv());
46 EXPECT_EQ(16, f()); 54 EXPECT_EQ(16, f());
47 EXPECT_EQ(17, fv()); 55 EXPECT_EQ(17, fv());
48 } 56 }
49 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
50 // Ensure that FunctionView handles move-only arguments and return values. 79 // Ensure that FunctionView handles move-only arguments and return values.
51 TEST(FunctionViewTest, UniquePtrPassthrough) { 80 TEST(FunctionViewTest, UniquePtrPassthrough) {
52 auto f = [](std::unique_ptr<int> x) { return x; }; 81 auto f = [](std::unique_ptr<int> x) { return x; };
53 rtc::FunctionView<std::unique_ptr<int>(std::unique_ptr<int>)> fv(f); 82 rtc::FunctionView<std::unique_ptr<int>(std::unique_ptr<int>)> fv(f);
54 std::unique_ptr<int> x(new int); 83 std::unique_ptr<int> x(new int);
55 int* x_addr = x.get(); 84 int* x_addr = x.get();
56 auto y = fv(std::move(x)); 85 auto y = fv(std::move(x));
57 EXPECT_EQ(x_addr, y.get()); 86 EXPECT_EQ(x_addr, y.get());
58 } 87 }
59 88
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 rtc::FunctionView<int()> fv2(f23); 133 rtc::FunctionView<int()> fv2(f23);
105 EXPECT_EQ(17, fv1()); 134 EXPECT_EQ(17, fv1());
106 EXPECT_EQ(23, fv2()); 135 EXPECT_EQ(23, fv2());
107 using std::swap; 136 using std::swap;
108 swap(fv1, fv2); 137 swap(fv1, fv2);
109 EXPECT_EQ(23, fv1()); 138 EXPECT_EQ(23, fv1());
110 EXPECT_EQ(17, fv2()); 139 EXPECT_EQ(17, fv2());
111 } 140 }
112 141
113 // Ensure that when you copy-construct a FunctionView, the new object points to 142 // 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 143 // the same function as the old one (as opposed to the new object pointing to
115 // the old one. 144 // the old one).
116 TEST(FunctionViewTest, CopyConstructorChaining) { 145 TEST(FunctionViewTest, CopyConstructorChaining) {
117 auto f17 = [] { return 17; }; 146 auto f17 = [] { return 17; };
118 rtc::FunctionView<int()> fv1(f17); 147 rtc::FunctionView<int()> fv1(f17);
119 rtc::FunctionView<int()> fv2(fv1); 148 rtc::FunctionView<int()> fv2(fv1);
120 EXPECT_EQ(17, fv1()); 149 EXPECT_EQ(17, fv1());
121 EXPECT_EQ(17, fv2()); 150 EXPECT_EQ(17, fv2());
122 auto f23 = [] { return 23; }; 151 auto f23 = [] { return 23; };
123 fv1 = f23; 152 fv1 = f23;
124 EXPECT_EQ(23, fv1()); 153 EXPECT_EQ(23, fv1());
125 EXPECT_EQ(17, fv2()); 154 EXPECT_EQ(17, fv2());
126 } 155 }
127 156
128 // Ensure that when you assign one FunctionView to another, we actually make a 157 // 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. 158 // copy (as opposed to making the second FunctionView point to the first one).
130 TEST(FunctionViewTest, CopyAssignmentChaining) { 159 TEST(FunctionViewTest, CopyAssignmentChaining) {
131 auto f17 = [] { return 17; }; 160 auto f17 = [] { return 17; };
132 rtc::FunctionView<int()> fv1(f17); 161 rtc::FunctionView<int()> fv1(f17);
133 auto f3 = [] { return 3; }; 162 rtc::FunctionView<int()> fv2;
134 rtc::FunctionView<int()> fv2(f3); 163 EXPECT_TRUE(fv1);
135 EXPECT_EQ(17, fv1()); 164 EXPECT_EQ(17, fv1());
136 EXPECT_EQ(3, fv2()); 165 EXPECT_FALSE(fv2);
137 fv2 = fv1; 166 fv2 = fv1;
138 EXPECT_EQ(17, fv1()); 167 EXPECT_EQ(17, fv1());
139 EXPECT_EQ(17, fv2()); 168 EXPECT_EQ(17, fv2());
140 auto f23 = [] { return 23; }; 169 auto f23 = [] { return 23; };
141 fv1 = f23; 170 fv1 = f23;
142 EXPECT_EQ(23, fv1()); 171 EXPECT_EQ(23, fv1());
143 EXPECT_EQ(17, fv2()); 172 EXPECT_EQ(17, fv2());
144 } 173 }
145 174
146 } // namespace rtc 175 } // namespace rtc
OLDNEW
« webrtc/base/function_view.h ('K') | « webrtc/base/function_view.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698