Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(212)

Side by Side Diff: sky/engine/wtf/FunctionalTest.cpp

Issue 684813003: Remove wtf/Functional.h (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sky/engine/wtf/Functional.h ('k') | sky/engine/wtf/MainThread.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
32 namespace {
33
34 using namespace WTF;
35
36 static int returnFortyTwo()
37 {
38 return 42;
39 }
40
41 TEST(FunctionalTest, Basic)
42 {
43 Function<int()> emptyFunction;
44 EXPECT_TRUE(emptyFunction.isNull());
45
46 Function<int()> returnFortyTwoFunction = bind(returnFortyTwo);
47 EXPECT_FALSE(returnFortyTwoFunction.isNull());
48 EXPECT_EQ(42, returnFortyTwoFunction());
49 }
50
51 static int multiplyByTwo(int n)
52 {
53 return n * 2;
54 }
55
56 static double multiplyByOneAndAHalf(double d)
57 {
58 return d * 1.5;
59 }
60
61 TEST(FunctionalTest, UnaryBind)
62 {
63 Function<int()> multiplyFourByTwoFunction = bind(multiplyByTwo, 4);
64 EXPECT_EQ(8, multiplyFourByTwoFunction());
65
66 Function<double()> multiplyByOneAndAHalfFunction = bind(multiplyByOneAndAHal f, 3);
67 EXPECT_EQ(4.5, multiplyByOneAndAHalfFunction());
68 }
69
70 TEST(FunctionalTest, UnaryPartBind)
71 {
72 Function<int(int)> multiplyByTwoFunction = bind<int>(multiplyByTwo);
73 EXPECT_EQ(8, multiplyByTwoFunction(4));
74
75 Function<double(double)> multiplyByOneAndAHalfFunction = bind<double>(multip lyByOneAndAHalf);
76 EXPECT_EQ(4.5, multiplyByOneAndAHalfFunction(3));
77 }
78
79 static int multiply(int x, int y)
80 {
81 return x * y;
82 }
83
84 static int subtract(int x, int y)
85 {
86 return x - y;
87 }
88
89 TEST(FunctionalTest, BinaryBind)
90 {
91 Function<int()> multiplyFourByTwoFunction = bind(multiply, 4, 2);
92 EXPECT_EQ(8, multiplyFourByTwoFunction());
93
94 Function<int()> subtractTwoFromFourFunction = bind(subtract, 4, 2);
95 EXPECT_EQ(2, subtractTwoFromFourFunction());
96 }
97
98 TEST(FunctionalTest, BinaryPartBind)
99 {
100 Function<int(int)> multiplyFourFunction = bind<int>(multiply, 4);
101 EXPECT_EQ(8, multiplyFourFunction(2));
102 Function<int(int, int)> multiplyFunction = bind<int, int>(multiply);
103 EXPECT_EQ(8, multiplyFunction(4, 2));
104
105 Function<int(int)> subtractFromFourFunction = bind<int>(subtract, 4);
106 EXPECT_EQ(2, subtractFromFourFunction(2));
107 Function<int(int, int)> subtractFunction = bind<int, int>(subtract);
108 EXPECT_EQ(2, subtractFunction(4, 2));
109 }
110
111 static void sixArgFunc(int a, double b, char c, int* d, double* e, char* f)
112 {
113 *d = a;
114 *e = b;
115 *f = c;
116 }
117
118 static void assertArgs(int actualInt, double actualDouble, char actualChar, int expectedInt, double expectedDouble, char expectedChar)
119 {
120 EXPECT_EQ(expectedInt, actualInt);
121 EXPECT_EQ(expectedDouble, actualDouble);
122 EXPECT_EQ(expectedChar, actualChar);
123 }
124
125 TEST(FunctionalTest, MultiPartBind)
126 {
127 int a = 0;
128 double b = 0.5;
129 char c = 'a';
130
131 Function<void(int, double, char, int*, double*, char*)> unbound =
132 bind<int, double, char, int*, double*, char*>(sixArgFunc);
133 unbound(1, 1.5, 'b', &a, &b, &c);
134 assertArgs(a, b, c, 1, 1.5, 'b');
135
136 Function<void(double, char, int*, double*, char*)> oneBound =
137 bind<double, char, int*, double*, char*>(sixArgFunc, 2);
138 oneBound(2.5, 'c', &a, &b, &c);
139 assertArgs(a, b, c, 2, 2.5, 'c');
140
141 Function<void(char, int*, double*, char*)> twoBound =
142 bind<char, int*, double*, char*>(sixArgFunc, 3, 3.5);
143 twoBound('d', &a, &b, &c);
144 assertArgs(a, b, c, 3, 3.5, 'd');
145
146 Function<void(int*, double*, char*)> threeBound =
147 bind<int*, double*, char*>(sixArgFunc, 4, 4.5, 'e');
148 threeBound(&a, &b, &c);
149 assertArgs(a, b, c, 4, 4.5, 'e');
150
151 Function<void(double*, char*)> fourBound =
152 bind<double*, char*>(sixArgFunc, 5, 5.5, 'f', &a);
153 fourBound(&b, &c);
154 assertArgs(a, b, c, 5, 5.5, 'f');
155
156 Function<void(char*)> fiveBound =
157 bind<char*>(sixArgFunc, 6, 6.5, 'g', &a, &b);
158 fiveBound(&c);
159 assertArgs(a, b, c, 6, 6.5, 'g');
160
161 Function<void()> sixBound =
162 bind(sixArgFunc, 7, 7.5, 'h', &a, &b, &c);
163 sixBound();
164 assertArgs(a, b, c, 7, 7.5, 'h');
165 }
166
167 class A {
168 public:
169 explicit A(int i)
170 : m_i(i)
171 {
172 }
173
174 int f() { return m_i; }
175 int addF(int j) { return m_i + j; }
176
177 private:
178 int m_i;
179 };
180
181 TEST(FunctionalTest, MemberFunctionBind)
182 {
183 A a(10);
184 Function<int()> function1 = bind(&A::f, &a);
185 EXPECT_EQ(10, function1());
186
187 Function<int()> function2 = bind(&A::addF, &a, 15);
188 EXPECT_EQ(25, function2());
189 }
190
191 TEST(FunctionalTest, MemberFunctionPartBind)
192 {
193 A a(10);
194 Function<int(class A*)> function1 = bind<class A*>(&A::f);
195 EXPECT_EQ(10, function1(&a));
196
197 Function<int(class A*, int)> unboundFunction2 =
198 bind<class A*, int>(&A::addF);
199 EXPECT_EQ(25, unboundFunction2(&a, 15));
200 Function<int(int)> objectBoundFunction2 =
201 bind<int>(&A::addF, &a);
202 EXPECT_EQ(25, objectBoundFunction2(15));
203 }
204
205 class Number : public RefCounted<Number> {
206 public:
207 static PassRefPtr<Number> create(int value)
208 {
209 return adoptRef(new Number(value));
210 }
211
212 ~Number()
213 {
214 m_value = 0;
215 }
216
217 int value() const { return m_value; }
218
219 private:
220 explicit Number(int value)
221 : m_value(value)
222 {
223 }
224
225 int m_value;
226 };
227
228 static int multiplyNumberByTwo(Number* number)
229 {
230 return number->value() * 2;
231 }
232
233 TEST(FunctionalTest, RefCountedStorage)
234 {
235 RefPtr<Number> five = Number::create(5);
236 Function<int()> multiplyFiveByTwoFunction = bind(multiplyNumberByTwo, five);
237 EXPECT_EQ(10, multiplyFiveByTwoFunction());
238
239 Function<int()> multiplyFourByTwoFunction = bind(multiplyNumberByTwo, Number ::create(4));
240 EXPECT_EQ(8, multiplyFourByTwoFunction());
241
242 RefPtr<Number> six = Number::create(6);
243 Function<int()> multiplySixByTwoFunction = bind(multiplyNumberByTwo, six.rel ease());
244 EXPECT_FALSE(six);
245 EXPECT_EQ(12, multiplySixByTwoFunction());
246 }
247
248 } // namespace
OLDNEW
« no previous file with comments | « sky/engine/wtf/Functional.h ('k') | sky/engine/wtf/MainThread.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698