1/*
2 * Copyright (C) 2011 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27
28#include "wtf/RefCounted.h"
29#include "wtf/Functional.h"
30#include <gtest/gtest.h>
31
32namespace {
33
34static int returnFortyTwo()
35{
36    return 42;
37}
38
39TEST(FunctionalTest, Basic)
40{
41    Function<int()> emptyFunction;
42    EXPECT_TRUE(emptyFunction.isNull());
43
44    Function<int()> returnFortyTwoFunction = bind(returnFortyTwo);
45    EXPECT_FALSE(returnFortyTwoFunction.isNull());
46    EXPECT_EQ(42, returnFortyTwoFunction());
47}
48
49static int multiplyByTwo(int n)
50{
51    return n * 2;
52}
53
54static double multiplyByOneAndAHalf(double d)
55{
56    return d * 1.5;
57}
58
59TEST(FunctionalTest, UnaryBind)
60{
61    Function<int()> multiplyFourByTwoFunction = bind(multiplyByTwo, 4);
62    EXPECT_EQ(8, multiplyFourByTwoFunction());
63
64    Function<double()> multiplyByOneAndAHalfFunction = bind(multiplyByOneAndAHalf, 3);
65    EXPECT_EQ(4.5, multiplyByOneAndAHalfFunction());
66}
67
68TEST(FunctionalTest, UnaryPartBind)
69{
70    Function<int(int)> multiplyByTwoFunction = bind<int>(multiplyByTwo);
71    EXPECT_EQ(8, multiplyByTwoFunction(4));
72
73    Function<double(double)> multiplyByOneAndAHalfFunction = bind<double>(multiplyByOneAndAHalf);
74    EXPECT_EQ(4.5, multiplyByOneAndAHalfFunction(3));
75}
76
77static int multiply(int x, int y)
78{
79    return x * y;
80}
81
82static int subtract(int x, int y)
83{
84    return x - y;
85}
86
87TEST(FunctionalTest, BinaryBind)
88{
89    Function<int()> multiplyFourByTwoFunction = bind(multiply, 4, 2);
90    EXPECT_EQ(8, multiplyFourByTwoFunction());
91
92    Function<int()> subtractTwoFromFourFunction = bind(subtract, 4, 2);
93    EXPECT_EQ(2, subtractTwoFromFourFunction());
94}
95
96TEST(FunctionalTest, BinaryPartBind)
97{
98    Function<int(int)> multiplyFourFunction = bind<int>(multiply, 4);
99    EXPECT_EQ(8, multiplyFourFunction(2));
100    Function<int(int, int)> multiplyFunction = bind<int, int>(multiply);
101    EXPECT_EQ(8, multiplyFunction(4, 2));
102
103    Function<int(int)> subtractFromFourFunction = bind<int>(subtract, 4);
104    EXPECT_EQ(2, subtractFromFourFunction(2));
105    Function<int(int, int)> subtractFunction = bind<int, int>(subtract);
106    EXPECT_EQ(2, subtractFunction(4, 2));
107}
108
109static void sixArgFunc(int a, double b, char c, int* d, double* e, char* f)
110{
111    *d = a;
112    *e = b;
113    *f = c;
114}
115
116static void assertArgs(int actualInt, double actualDouble, char actualChar, int expectedInt, double expectedDouble, char expectedChar)
117{
118    EXPECT_EQ(expectedInt, actualInt);
119    EXPECT_EQ(expectedDouble, actualDouble);
120    EXPECT_EQ(expectedChar, actualChar);
121}
122
123TEST(FunctionalTest, MultiPartBind)
124{
125    int a = 0;
126    double b = 0.5;
127    char c = 'a';
128
129    Function<void(int, double, char, int*, double*, char*)> unbound =
130        bind<int, double, char, int*, double*, char*>(sixArgFunc);
131    unbound(1, 1.5, 'b', &a, &b, &c);
132    assertArgs(a, b, c, 1, 1.5, 'b');
133
134    Function<void(double, char, int*, double*, char*)> oneBound =
135        bind<double, char, int*, double*, char*>(sixArgFunc, 2);
136    oneBound(2.5, 'c', &a, &b, &c);
137    assertArgs(a, b, c, 2, 2.5, 'c');
138
139    Function<void(char, int*, double*, char*)> twoBound =
140        bind<char, int*, double*, char*>(sixArgFunc, 3, 3.5);
141    twoBound('d', &a, &b, &c);
142    assertArgs(a, b, c, 3, 3.5, 'd');
143
144    Function<void(int*, double*, char*)> threeBound =
145        bind<int*, double*, char*>(sixArgFunc, 4, 4.5, 'e');
146    threeBound(&a, &b, &c);
147    assertArgs(a, b, c, 4, 4.5, 'e');
148
149    Function<void(double*, char*)> fourBound =
150        bind<double*, char*>(sixArgFunc, 5, 5.5, 'f', &a);
151    fourBound(&b, &c);
152    assertArgs(a, b, c, 5, 5.5, 'f');
153
154    Function<void(char*)> fiveBound =
155        bind<char*>(sixArgFunc, 6, 6.5, 'g', &a, &b);
156    fiveBound(&c);
157    assertArgs(a, b, c, 6, 6.5, 'g');
158
159    Function<void()> sixBound =
160        bind(sixArgFunc, 7, 7.5, 'h', &a, &b, &c);
161    sixBound();
162    assertArgs(a, b, c, 7, 7.5, 'h');
163}
164
165class A {
166public:
167    explicit A(int i)
168        : m_i(i)
169    {
170    }
171
172    int f() { return m_i; }
173    int addF(int j) { return m_i + j; }
174
175private:
176    int m_i;
177};
178
179TEST(FunctionalTest, MemberFunctionBind)
180{
181    A a(10);
182    Function<int()> function1 = bind(&A::f, &a);
183    EXPECT_EQ(10, function1());
184
185    Function<int()> function2 = bind(&A::addF, &a, 15);
186    EXPECT_EQ(25, function2());
187}
188
189TEST(FunctionalTest, MemberFunctionPartBind)
190{
191    A a(10);
192    Function<int(class A*)> function1 = bind<class A*>(&A::f);
193    EXPECT_EQ(10, function1(&a));
194
195    Function<int(class A*, int)> unboundFunction2 =
196        bind<class A*, int>(&A::addF);
197    EXPECT_EQ(25, unboundFunction2(&a, 15));
198    Function<int(int)> objectBoundFunction2 =
199        bind<int>(&A::addF, &a);
200    EXPECT_EQ(25, objectBoundFunction2(15));
201}
202
203class Number : public RefCounted<Number> {
204public:
205    static PassRefPtr<Number> create(int value)
206    {
207        return adoptRef(new Number(value));
208    }
209
210    ~Number()
211    {
212        m_value = 0;
213    }
214
215    int value() const { return m_value; }
216
217private:
218    explicit Number(int value)
219        : m_value(value)
220    {
221    }
222
223    int m_value;
224};
225
226static int multiplyNumberByTwo(Number* number)
227{
228    return number->value() * 2;
229}
230
231TEST(FunctionalTest, RefCountedStorage)
232{
233    RefPtr<Number> five = Number::create(5);
234    Function<int()> multiplyFiveByTwoFunction = bind(multiplyNumberByTwo, five);
235    EXPECT_EQ(10, multiplyFiveByTwoFunction());
236
237    Function<int()> multiplyFourByTwoFunction = bind(multiplyNumberByTwo, Number::create(4));
238    EXPECT_EQ(8, multiplyFourByTwoFunction());
239
240    RefPtr<Number> six = Number::create(6);
241    Function<int()> multiplySixByTwoFunction = bind(multiplyNumberByTwo, six.release());
242    EXPECT_FALSE(six);
243    EXPECT_EQ(12, multiplySixByTwoFunction());
244}
245
246} // namespace
247