3 * Copyright 2004--2011, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #include "talk/base/bind.h"
29 #include "talk/base/callback.h"
30 #include "talk/base/gunit.h"
37 int g() { return 42; }
38 int h(int x) { return x * x; }
39 void i(int& x) { x *= x; } // NOLINT: Testing refs
42 int a() { return 24; }
43 int b(int x) const { return x * x; }
48 TEST(CallbackTest, VoidReturn) {
50 EXPECT_TRUE(cb.empty());
51 cb(); // Executing an empty callback should not crash.
52 cb = Callback0<void>(&f);
53 EXPECT_FALSE(cb.empty());
57 TEST(CallbackTest, IntReturn) {
59 EXPECT_TRUE(cb.empty());
60 cb = Callback0<int>(&g);
61 EXPECT_FALSE(cb.empty());
66 TEST(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));
72 // Try clearing a callback.
73 cb1 = Callback1<int, int>();
74 EXPECT_TRUE(cb1.empty());
76 // Try a callback with a ref parameter.
77 Callback1<void, int&> cb2(&i);
85 TEST(CallbackTest, WithBind) {
87 Callback0<int> cb1 = Bind(&BindTester::a, &t);
90 cb1 = Bind(&BindTester::b, &t, 10);
91 EXPECT_EQ(100, cb1());
92 EXPECT_EQ(100, cb1());
93 cb1 = Bind(&BindTester::b, &t, 5);
98 } // namespace talk_base