blob: c7ca00f0fd2197602dbdf86701b09e1b18769bfe [file] [log] [blame]
sergeyu@chromium.org70022fa2014-02-07 19:03:26 +00001/*
2 * libjingle
3 * Copyright 2004--2011, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/base/bind.h"
29#include "talk/base/callback.h"
30#include "talk/base/gunit.h"
31
32namespace talk_base {
33
34namespace {
35
36void f() {}
37int g() { return 42; }
38int h(int x) { return x * x; }
39void i(int& x) { x *= x; } // NOLINT: Testing refs
40
41struct BindTester {
42 int a() { return 24; }
43 int b(int x) const { return x * x; }
44};
45
46} // namespace
47
48TEST(CallbackTest, VoidReturn) {
49 Callback0<void> cb;
50 EXPECT_TRUE(cb.empty());
51 cb(); // Executing an empty callback should not crash.
52 cb = Callback0<void>(&f);
53 EXPECT_FALSE(cb.empty());
54 cb();
55}
56
57TEST(CallbackTest, IntReturn) {
58 Callback0<int> cb;
59 EXPECT_TRUE(cb.empty());
60 cb = Callback0<int>(&g);
61 EXPECT_FALSE(cb.empty());
62 EXPECT_EQ(42, cb());
63 EXPECT_EQ(42, cb());
64}
65
66TEST(CallbackTest, OneParam) {
67 Callback1<int, int> cb1(&h);
68 EXPECT_FALSE(cb1.empty());
69 EXPECT_EQ(9, cb1(-3));
70 EXPECT_EQ(100, cb1(10));
71
72 // Try clearing a callback.
73 cb1 = Callback1<int, int>();
74 EXPECT_TRUE(cb1.empty());
75
76 // Try a callback with a ref parameter.
77 Callback1<void, int&> cb2(&i);
78 int x = 3;
79 cb2(x);
80 EXPECT_EQ(9, x);
81 cb2(x);
82 EXPECT_EQ(81, x);
83}
84
85TEST(CallbackTest, WithBind) {
86 BindTester t;
87 Callback0<int> cb1 = Bind(&BindTester::a, &t);
88 EXPECT_EQ(24, cb1());
89 EXPECT_EQ(24, cb1());
90 cb1 = Bind(&BindTester::b, &t, 10);
91 EXPECT_EQ(100, cb1());
92 EXPECT_EQ(100, cb1());
93 cb1 = Bind(&BindTester::b, &t, 5);
94 EXPECT_EQ(25, cb1());
95 EXPECT_EQ(25, cb1());
96}
97
98} // namespace talk_base