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

Unified Diff: webrtc/base/function_view_unittest.cc

Issue 2380463003: Move FunctionView from AudioCodingModule to the rtc namespace (Closed)
Patch Set: git cl format Created 4 years, 3 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « webrtc/base/function_view.h ('k') | webrtc/modules/audio_coding/acm2/audio_coding_module.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webrtc/base/function_view_unittest.cc
diff --git a/webrtc/base/function_view_unittest.cc b/webrtc/base/function_view_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..3ca2df456e0eb26850eb00fce16d0ceb59f6a91b
--- /dev/null
+++ b/webrtc/base/function_view_unittest.cc
@@ -0,0 +1,146 @@
+/*
+ * Copyright 2016 The WebRTC Project Authors. All rights reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include <memory>
+#include <utility>
+
+#include "webrtc/base/function_view.h"
+#include "webrtc/base/gunit.h"
+
+namespace rtc {
+
+namespace {
+
+int CallWith33(rtc::FunctionView<int(int)> fv) {
+ return fv(33);
+}
+
+} // namespace
+
+// Test the main use case of FunctionView: implicitly converting a lambda
+// function argument.
+TEST(FunctionViewTest, ImplicitConversion) {
+ EXPECT_EQ(38, CallWith33([](int x) { return x + 5; }));
+}
+
+TEST(FunctionViewTest, IntIntLambdaWithoutState) {
+ auto f = [](int x) { return x + 1; };
+ EXPECT_EQ(18, f(17));
+ rtc::FunctionView<int(int)> fv(f);
+ EXPECT_EQ(18, fv(17));
+}
+
+TEST(FunctionViewTest, IntVoidLambdaWithState) {
+ int x = 13;
+ auto f = [x]() mutable { return ++x; };
+ rtc::FunctionView<int()> fv(f);
+ EXPECT_EQ(14, f());
+ EXPECT_EQ(15, fv());
+ EXPECT_EQ(16, f());
+ EXPECT_EQ(17, fv());
+}
+
+// Ensure that FunctionView handles move-only arguments and return values.
+TEST(FunctionViewTest, UniquePtrPassthrough) {
+ auto f = [](std::unique_ptr<int> x) { return x; };
+ rtc::FunctionView<std::unique_ptr<int>(std::unique_ptr<int>)> fv(f);
+ std::unique_ptr<int> x(new int);
+ int* x_addr = x.get();
+ auto y = fv(std::move(x));
+ EXPECT_EQ(x_addr, y.get());
+}
+
+TEST(FunctionViewTest, CopyConstructor) {
+ auto f17 = [] { return 17; };
+ rtc::FunctionView<int()> fv1(f17);
+ rtc::FunctionView<int()> fv2(fv1);
+ EXPECT_EQ(17, fv1());
+ EXPECT_EQ(17, fv2());
+}
+
+TEST(FunctionViewTest, MoveConstructorIsCopy) {
+ auto f17 = [] { return 17; };
+ rtc::FunctionView<int()> fv1(f17);
+ rtc::FunctionView<int()> fv2(std::move(fv1));
+ EXPECT_EQ(17, fv1());
+ EXPECT_EQ(17, fv2());
+}
+
+TEST(FunctionViewTest, CopyAssignment) {
+ auto f17 = [] { return 17; };
+ rtc::FunctionView<int()> fv1(f17);
+ auto f23 = [] { return 23; };
+ rtc::FunctionView<int()> fv2(f23);
+ EXPECT_EQ(17, fv1());
+ EXPECT_EQ(23, fv2());
+ fv2 = fv1;
+ EXPECT_EQ(17, fv1());
+ EXPECT_EQ(17, fv2());
+}
+
+TEST(FunctionViewTest, MoveAssignmentIsCopy) {
+ auto f17 = [] { return 17; };
+ rtc::FunctionView<int()> fv1(f17);
+ auto f23 = [] { return 23; };
+ rtc::FunctionView<int()> fv2(f23);
+ EXPECT_EQ(17, fv1());
+ EXPECT_EQ(23, fv2());
+ fv2 = std::move(fv1);
+ EXPECT_EQ(17, fv1());
+ EXPECT_EQ(17, fv2());
+}
+
+TEST(FunctionViewTest, Swap) {
+ auto f17 = [] { return 17; };
+ rtc::FunctionView<int()> fv1(f17);
+ auto f23 = [] { return 23; };
+ rtc::FunctionView<int()> fv2(f23);
+ EXPECT_EQ(17, fv1());
+ EXPECT_EQ(23, fv2());
+ using std::swap;
+ swap(fv1, fv2);
+ EXPECT_EQ(23, fv1());
+ EXPECT_EQ(17, fv2());
+}
+
+// Ensure that when you copy-construct a FunctionView, the new object points to
+// the same function as the old one, as opposed to the new object pointing to
+// the old one.
+TEST(FunctionViewTest, CopyConstructorChaining) {
+ auto f17 = [] { return 17; };
+ rtc::FunctionView<int()> fv1(f17);
+ rtc::FunctionView<int()> fv2(fv1);
+ EXPECT_EQ(17, fv1());
+ EXPECT_EQ(17, fv2());
+ auto f23 = [] { return 23; };
+ fv1 = f23;
+ EXPECT_EQ(23, fv1());
+ EXPECT_EQ(17, fv2());
+}
+
+// Ensure that when you assign one FunctionView to another, we actually make a
+// copy as opposed to making the second FunctionView point to the first one.
+TEST(FunctionViewTest, CopyAssignmentChaining) {
+ auto f17 = [] { return 17; };
+ rtc::FunctionView<int()> fv1(f17);
+ auto f3 = [] { return 3; };
+ rtc::FunctionView<int()> fv2(f3);
+ EXPECT_EQ(17, fv1());
+ EXPECT_EQ(3, fv2());
+ fv2 = fv1;
+ EXPECT_EQ(17, fv1());
+ EXPECT_EQ(17, fv2());
+ auto f23 = [] { return 23; };
+ fv1 = f23;
+ EXPECT_EQ(23, fv1());
+ EXPECT_EQ(17, fv2());
+}
+
+} // namespace rtc
« no previous file with comments | « webrtc/base/function_view.h ('k') | webrtc/modules/audio_coding/acm2/audio_coding_module.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698