webrtc_m130/webrtc/rtc_base/function_view_unittest.cc
Henrik Kjellander 6776518bea Move webrtc/{base => rtc_base}
This refactoring takes a careful approach to avoid rushing the change:
* stub headers are left in all the old locations of webrtc/base
* existing GN targets are kept and now just forward to the moved ones
  using public_deps.
The only exception to the above is the base_java target and its .java files,
which were moved to webrtc/rtc_base right away since it's not possible
to use public_deps for android_library.
To avoid breaking builds, a temporary Dummy.java file was added to
the new intermediate target in webrtc/rtc_base:base_java as well to avoid
hitting a GN assert in the android_library template.

The above approach should make the transition smooth without breaking
downstream.

A helper script was created (https://codereview.webrtc.org/2879203002/)
and was run like this:
stub-headers.py -s webrtc/base -d webrtc/rtc_base -i 7634
stub-headers.py -s webrtc/base/numerics -d webrtc/rtc_base/numerics -i 7634

Fixed invalid header guards in the following files:
webrtc/base/base64.h
webrtc/base/cryptstring.h
webrtc/base/event.h
webrtc/base/flags.h
webrtc/base/httpbase.h
webrtc/base/httpcommon-inl.h
webrtc/base/httpcommon.h
webrtc/base/httpserver.h
webrtc/base/logsinks.h
webrtc/base/macutils.h
webrtc/base/nattypes.h
webrtc/base/openssladapter.h
webrtc/base/opensslstreamadapter.h
webrtc/base/pathutils.h
webrtc/base/physicalsocketserver.h
webrtc/base/proxyinfo.h
webrtc/base/sigslot.h
webrtc/base/sigslotrepeater.h
webrtc/base/socket.h
webrtc/base/socketaddresspair.h
webrtc/base/socketfactory.h
webrtc/base/stringutils.h
webrtc/base/testbase64.h
webrtc/base/testutils.h
webrtc/base/transformadapter.h
webrtc/base/win32filesystem.h

Added new header guards to:
sslroots.h
testbase64.h

BUG=webrtc:7634
NOTRY=True
NOPRESUBMIT=True
R=kwiberg@webrtc.org

Review-Url: https://codereview.webrtc.org/2877023002 .
Cr-Commit-Position: refs/heads/master@{#18816}
2017-06-28 18:58:10 +00:00

176 lines
4.6 KiB
C++

/*
* 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 ? fv(33) : -1;
}
int Add33(int x) {
return x + 33;
}
} // namespace
// Test the main use case of FunctionView: implicitly converting a callable
// argument.
TEST(FunctionViewTest, ImplicitConversion) {
EXPECT_EQ(38, CallWith33([](int x) { return x + 5; }));
EXPECT_EQ(66, CallWith33(Add33));
EXPECT_EQ(-1, CallWith33(nullptr));
}
TEST(FunctionViewTest, IntIntLambdaWithoutState) {
auto f = [](int x) { return x + 1; };
EXPECT_EQ(18, f(17));
rtc::FunctionView<int(int)> fv(f);
EXPECT_TRUE(fv);
EXPECT_EQ(18, fv(17));
}
TEST(FunctionViewTest, IntVoidLambdaWithState) {
int x = 13;
auto f = [x]() mutable { return ++x; };
rtc::FunctionView<int()> fv(f);
EXPECT_TRUE(fv);
EXPECT_EQ(14, f());
EXPECT_EQ(15, fv());
EXPECT_EQ(16, f());
EXPECT_EQ(17, fv());
}
TEST(FunctionViewTest, IntIntFunction) {
rtc::FunctionView<int(int)> fv(Add33);
EXPECT_TRUE(fv);
EXPECT_EQ(50, fv(17));
}
TEST(FunctionViewTest, IntIntFunctionPointer) {
rtc::FunctionView<int(int)> fv(&Add33);
EXPECT_TRUE(fv);
EXPECT_EQ(50, fv(17));
}
TEST(FunctionViewTest, Null) {
// These two call constructors that statically construct null FunctionViews.
EXPECT_FALSE(rtc::FunctionView<int()>());
EXPECT_FALSE(rtc::FunctionView<int()>(nullptr));
// This calls the constructor for function pointers.
EXPECT_FALSE(rtc::FunctionView<int()>(reinterpret_cast<int(*)()>(0)));
}
// 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);
rtc::FunctionView<int()> fv2;
EXPECT_TRUE(fv1);
EXPECT_EQ(17, fv1());
EXPECT_FALSE(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