blob: ed8dd5cf2d226a93b24512069bc9324aa9c91619 [file] [log] [blame]
henrike@webrtc.orgf7795df2014-05-13 18:00:26 +00001/*
2 * Copyright 2004 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 "webrtc/base/bind.h"
12#include "webrtc/base/gunit.h"
13
14namespace rtc {
15
16namespace {
17
18struct MethodBindTester {
19 void NullaryVoid() { ++call_count; }
20 int NullaryInt() { ++call_count; return 1; }
21 int NullaryConst() const { ++call_count; return 2; }
22 void UnaryVoid(int dummy) { ++call_count; }
23 template <class T> T Identity(T value) { ++call_count; return value; }
24 int UnaryByRef(int& value) const { ++call_count; return ++value; } // NOLINT
25 int Multiply(int a, int b) const { ++call_count; return a * b; }
26 mutable int call_count;
27};
28
29int Return42() { return 42; }
30int Negate(int a) { return -a; }
31int Multiply(int a, int b) { return a * b; }
32
33} // namespace
34
35TEST(BindTest, BindToMethod) {
36 MethodBindTester object = {0};
37 EXPECT_EQ(0, object.call_count);
38 Bind(&MethodBindTester::NullaryVoid, &object)();
39 EXPECT_EQ(1, object.call_count);
40 EXPECT_EQ(1, Bind(&MethodBindTester::NullaryInt, &object)());
41 EXPECT_EQ(2, object.call_count);
42 EXPECT_EQ(2, Bind(&MethodBindTester::NullaryConst,
43 static_cast<const MethodBindTester*>(&object))());
44 EXPECT_EQ(3, object.call_count);
45 Bind(&MethodBindTester::UnaryVoid, &object, 5)();
46 EXPECT_EQ(4, object.call_count);
47 EXPECT_EQ(100, Bind(&MethodBindTester::Identity<int>, &object, 100)());
48 EXPECT_EQ(5, object.call_count);
49 const std::string string_value("test string");
50 EXPECT_EQ(string_value, Bind(&MethodBindTester::Identity<std::string>,
51 &object, string_value)());
52 EXPECT_EQ(6, object.call_count);
53 int value = 11;
54 EXPECT_EQ(12, Bind(&MethodBindTester::UnaryByRef, &object, value)());
55 EXPECT_EQ(12, value);
56 EXPECT_EQ(7, object.call_count);
57 EXPECT_EQ(56, Bind(&MethodBindTester::Multiply, &object, 7, 8)());
58 EXPECT_EQ(8, object.call_count);
59}
60
61TEST(BindTest, BindToFunction) {
62 EXPECT_EQ(42, Bind(&Return42)());
63 EXPECT_EQ(3, Bind(&Negate, -3)());
64 EXPECT_EQ(56, Bind(&Multiply, 8, 7)());
65}
66
67} // namespace rtc