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

Side by Side Diff: third_party/WebKit/Source/wtf/FunctionalTest.cpp

Issue 1611343002: wtf reformat test Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: pydent Created 4 years, 11 months 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 | « third_party/WebKit/Source/wtf/Functional.h ('k') | third_party/WebKit/Source/wtf/GetPtr.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2011 Apple Inc. All rights reserved. 2 * Copyright (C) 2011 Apple Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 14 matching lines...) Expand all
25 25
26 #include "wtf/Functional.h" 26 #include "wtf/Functional.h"
27 27
28 #include "testing/gtest/include/gtest/gtest.h" 28 #include "testing/gtest/include/gtest/gtest.h"
29 #include "wtf/OwnPtr.h" 29 #include "wtf/OwnPtr.h"
30 #include "wtf/RefCounted.h" 30 #include "wtf/RefCounted.h"
31 31
32 namespace WTF { 32 namespace WTF {
33 33
34 class UnwrappedClass { 34 class UnwrappedClass {
35 public: 35 public:
36 explicit UnwrappedClass(int value) 36 explicit UnwrappedClass(int value) : m_value(value) {}
37 : m_value(value) 37
38 { 38 int value() const { return m_value; }
39 } 39
40 40 private:
41 int value() const { return m_value; } 41 int m_value;
42
43 private:
44 int m_value;
45 }; 42 };
46 43
47 class WrappedClass { 44 class WrappedClass {
48 public: 45 public:
49 explicit WrappedClass(int value) 46 explicit WrappedClass(int value) : m_value(value) {}
50 : m_value(value) 47
51 { 48 UnwrappedClass unwrap() const { return UnwrappedClass(m_value); }
52 } 49
53 50 private:
54 UnwrappedClass unwrap() const { return UnwrappedClass(m_value); } 51 int m_value;
55
56 private:
57 int m_value;
58 }; 52 };
59 53
60 // This class must be wrapped in bind() and unwrapped in closure execution. 54 // This class must be wrapped in bind() and unwrapped in closure execution.
61 class ClassToBeWrapped { 55 class ClassToBeWrapped {
62 WTF_MAKE_NONCOPYABLE(ClassToBeWrapped); 56 WTF_MAKE_NONCOPYABLE(ClassToBeWrapped);
63 public: 57
64 explicit ClassToBeWrapped(int value) 58 public:
65 : m_value(value) 59 explicit ClassToBeWrapped(int value) : m_value(value) {}
66 { 60
67 } 61 WrappedClass wrap() const { return WrappedClass(m_value); }
68 62
69 WrappedClass wrap() const { return WrappedClass(m_value); } 63 private:
70 64 int m_value;
71 private: 65 };
72 int m_value; 66
73 }; 67 template <>
74 68 struct ParamStorageTraits<ClassToBeWrapped> {
75 template<> struct ParamStorageTraits<ClassToBeWrapped> { 69 using StorageType = WrappedClass;
76 using StorageType = WrappedClass; 70 static StorageType wrap(const ClassToBeWrapped& value) {
77 static StorageType wrap(const ClassToBeWrapped& value) { return value.wrap() ; } 71 return value.wrap();
78 static UnwrappedClass unwrap(const StorageType& value) { return value.unwrap (); } 72 }
73 static UnwrappedClass unwrap(const StorageType& value) {
74 return value.unwrap();
75 }
79 }; 76 };
80 77
81 namespace { 78 namespace {
82 79
83 int returnFortyTwo() 80 int returnFortyTwo() {
84 { 81 return 42;
85 return 42; 82 }
86 } 83
87 84 TEST(FunctionalTest, Basic) {
88 TEST(FunctionalTest, Basic) 85 OwnPtr<Function<int()>> returnFortyTwoFunction = bind(returnFortyTwo);
89 { 86 EXPECT_EQ(42, (*returnFortyTwoFunction)());
90 OwnPtr<Function<int()>> returnFortyTwoFunction = bind(returnFortyTwo); 87 }
91 EXPECT_EQ(42, (*returnFortyTwoFunction)()); 88
92 } 89 int multiplyByTwo(int n) {
93 90 return n * 2;
94 int multiplyByTwo(int n) 91 }
95 { 92
96 return n * 2; 93 double multiplyByOneAndAHalf(double d) {
97 } 94 return d * 1.5;
98 95 }
99 double multiplyByOneAndAHalf(double d) 96
100 { 97 TEST(FunctionalTest, UnaryBind) {
101 return d * 1.5; 98 OwnPtr<Function<int()>> multiplyFourByTwoFunction = bind(multiplyByTwo, 4);
102 } 99 EXPECT_EQ(8, (*multiplyFourByTwoFunction)());
103 100
104 TEST(FunctionalTest, UnaryBind) 101 OwnPtr<Function<double()>> multiplyByOneAndAHalfFunction =
105 { 102 bind(multiplyByOneAndAHalf, 3);
106 OwnPtr<Function<int()>> multiplyFourByTwoFunction = bind(multiplyByTwo, 4); 103 EXPECT_EQ(4.5, (*multiplyByOneAndAHalfFunction)());
107 EXPECT_EQ(8, (*multiplyFourByTwoFunction)()); 104 }
108 105
109 OwnPtr<Function<double()>> multiplyByOneAndAHalfFunction = bind(multiplyByOn eAndAHalf, 3); 106 TEST(FunctionalTest, UnaryPartBind) {
110 EXPECT_EQ(4.5, (*multiplyByOneAndAHalfFunction)()); 107 OwnPtr<Function<int(int)>> multiplyByTwoFunction = bind<int>(multiplyByTwo);
111 } 108 EXPECT_EQ(8, (*multiplyByTwoFunction)(4));
112 109
113 TEST(FunctionalTest, UnaryPartBind) 110 OwnPtr<Function<double(double)>> multiplyByOneAndAHalfFunction =
114 { 111 bind<double>(multiplyByOneAndAHalf);
115 OwnPtr<Function<int(int)>> multiplyByTwoFunction = bind<int>(multiplyByTwo); 112 EXPECT_EQ(4.5, (*multiplyByOneAndAHalfFunction)(3));
116 EXPECT_EQ(8, (*multiplyByTwoFunction)(4)); 113 }
117 114
118 OwnPtr<Function<double(double)>> multiplyByOneAndAHalfFunction = bind<double >(multiplyByOneAndAHalf); 115 int multiply(int x, int y) {
119 EXPECT_EQ(4.5, (*multiplyByOneAndAHalfFunction)(3)); 116 return x * y;
120 } 117 }
121 118
122 int multiply(int x, int y) 119 int subtract(int x, int y) {
123 { 120 return x - y;
124 return x * y; 121 }
125 } 122
126 123 TEST(FunctionalTest, BinaryBind) {
127 int subtract(int x, int y) 124 OwnPtr<Function<int()>> multiplyFourByTwoFunction = bind(multiply, 4, 2);
128 { 125 EXPECT_EQ(8, (*multiplyFourByTwoFunction)());
129 return x - y; 126
130 } 127 OwnPtr<Function<int()>> subtractTwoFromFourFunction = bind(subtract, 4, 2);
131 128 EXPECT_EQ(2, (*subtractTwoFromFourFunction)());
132 TEST(FunctionalTest, BinaryBind) 129 }
133 { 130
134 OwnPtr<Function<int()>> multiplyFourByTwoFunction = bind(multiply, 4, 2); 131 TEST(FunctionalTest, BinaryPartBind) {
135 EXPECT_EQ(8, (*multiplyFourByTwoFunction)()); 132 OwnPtr<Function<int(int)>> multiplyFourFunction = bind<int>(multiply, 4);
136 133 EXPECT_EQ(8, (*multiplyFourFunction)(2));
137 OwnPtr<Function<int()>> subtractTwoFromFourFunction = bind(subtract, 4, 2); 134 OwnPtr<Function<int(int, int)>> multiplyFunction = bind<int, int>(multiply);
138 EXPECT_EQ(2, (*subtractTwoFromFourFunction)()); 135 EXPECT_EQ(8, (*multiplyFunction)(4, 2));
139 } 136
140 137 OwnPtr<Function<int(int)>> subtractFromFourFunction = bind<int>(subtract, 4);
141 TEST(FunctionalTest, BinaryPartBind) 138 EXPECT_EQ(2, (*subtractFromFourFunction)(2));
142 { 139 OwnPtr<Function<int(int, int)>> subtractFunction = bind<int, int>(subtract);
143 OwnPtr<Function<int(int)>> multiplyFourFunction = bind<int>(multiply, 4); 140 EXPECT_EQ(2, (*subtractFunction)(4, 2));
144 EXPECT_EQ(8, (*multiplyFourFunction)(2)); 141 }
145 OwnPtr<Function<int(int, int)>> multiplyFunction = bind<int, int>(multiply); 142
146 EXPECT_EQ(8, (*multiplyFunction)(4, 2)); 143 void sixArgFunc(int a, double b, char c, int* d, double* e, char* f) {
147 144 *d = a;
148 OwnPtr<Function<int(int)>> subtractFromFourFunction = bind<int>(subtract, 4) ; 145 *e = b;
149 EXPECT_EQ(2, (*subtractFromFourFunction)(2)); 146 *f = c;
150 OwnPtr<Function<int(int, int)>> subtractFunction = bind<int, int>(subtract); 147 }
151 EXPECT_EQ(2, (*subtractFunction)(4, 2)); 148
152 } 149 void assertArgs(int actualInt,
153 150 double actualDouble,
154 void sixArgFunc(int a, double b, char c, int* d, double* e, char* f) 151 char actualChar,
155 { 152 int expectedInt,
156 *d = a; 153 double expectedDouble,
157 *e = b; 154 char expectedChar) {
158 *f = c; 155 EXPECT_EQ(expectedInt, actualInt);
159 } 156 EXPECT_EQ(expectedDouble, actualDouble);
160 157 EXPECT_EQ(expectedChar, actualChar);
161 void assertArgs(int actualInt, double actualDouble, char actualChar, int expecte dInt, double expectedDouble, char expectedChar) 158 }
162 { 159
163 EXPECT_EQ(expectedInt, actualInt); 160 TEST(FunctionalTest, MultiPartBind) {
164 EXPECT_EQ(expectedDouble, actualDouble); 161 int a = 0;
165 EXPECT_EQ(expectedChar, actualChar); 162 double b = 0.5;
166 } 163 char c = 'a';
167 164
168 TEST(FunctionalTest, MultiPartBind) 165 OwnPtr<Function<void(int, double, char, int*, double*, char*)>> unbound =
169 { 166 bind<int, double, char, int*, double*, char*>(sixArgFunc);
170 int a = 0; 167 (*unbound)(1, 1.5, 'b', &a, &b, &c);
171 double b = 0.5; 168 assertArgs(a, b, c, 1, 1.5, 'b');
172 char c = 'a'; 169
173 170 OwnPtr<Function<void(double, char, int*, double*, char*)>> oneBound =
174 OwnPtr<Function<void(int, double, char, int*, double*, char*)>> unbound = 171 bind<double, char, int*, double*, char*>(sixArgFunc, 2);
175 bind<int, double, char, int*, double*, char*>(sixArgFunc); 172 (*oneBound)(2.5, 'c', &a, &b, &c);
176 (*unbound)(1, 1.5, 'b', &a, &b, &c); 173 assertArgs(a, b, c, 2, 2.5, 'c');
177 assertArgs(a, b, c, 1, 1.5, 'b'); 174
178 175 OwnPtr<Function<void(char, int*, double*, char*)>> twoBound =
179 OwnPtr<Function<void(double, char, int*, double*, char*)>> oneBound = 176 bind<char, int*, double*, char*>(sixArgFunc, 3, 3.5);
180 bind<double, char, int*, double*, char*>(sixArgFunc, 2); 177 (*twoBound)('d', &a, &b, &c);
181 (*oneBound)(2.5, 'c', &a, &b, &c); 178 assertArgs(a, b, c, 3, 3.5, 'd');
182 assertArgs(a, b, c, 2, 2.5, 'c'); 179
183 180 OwnPtr<Function<void(int*, double*, char*)>> threeBound =
184 OwnPtr<Function<void(char, int*, double*, char*)>> twoBound = 181 bind<int*, double*, char*>(sixArgFunc, 4, 4.5, 'e');
185 bind<char, int*, double*, char*>(sixArgFunc, 3, 3.5); 182 (*threeBound)(&a, &b, &c);
186 (*twoBound)('d', &a, &b, &c); 183 assertArgs(a, b, c, 4, 4.5, 'e');
187 assertArgs(a, b, c, 3, 3.5, 'd'); 184
188 185 OwnPtr<Function<void(double*, char*)>> fourBound =
189 OwnPtr<Function<void(int*, double*, char*)>> threeBound = 186 bind<double*, char*>(sixArgFunc, 5, 5.5, 'f', &a);
190 bind<int*, double*, char*>(sixArgFunc, 4, 4.5, 'e'); 187 (*fourBound)(&b, &c);
191 (*threeBound)(&a, &b, &c); 188 assertArgs(a, b, c, 5, 5.5, 'f');
192 assertArgs(a, b, c, 4, 4.5, 'e'); 189
193 190 OwnPtr<Function<void(char*)>> fiveBound =
194 OwnPtr<Function<void(double*, char*)>> fourBound = 191 bind<char*>(sixArgFunc, 6, 6.5, 'g', &a, &b);
195 bind<double*, char*>(sixArgFunc, 5, 5.5, 'f', &a); 192 (*fiveBound)(&c);
196 (*fourBound)(&b, &c); 193 assertArgs(a, b, c, 6, 6.5, 'g');
197 assertArgs(a, b, c, 5, 5.5, 'f'); 194
198 195 OwnPtr<Function<void()>> sixBound = bind(sixArgFunc, 7, 7.5, 'h', &a, &b, &c);
199 OwnPtr<Function<void(char*)>> fiveBound = 196 (*sixBound)();
200 bind<char*>(sixArgFunc, 6, 6.5, 'g', &a, &b); 197 assertArgs(a, b, c, 7, 7.5, 'h');
201 (*fiveBound)(&c);
202 assertArgs(a, b, c, 6, 6.5, 'g');
203
204 OwnPtr<Function<void()>> sixBound =
205 bind(sixArgFunc, 7, 7.5, 'h', &a, &b, &c);
206 (*sixBound)();
207 assertArgs(a, b, c, 7, 7.5, 'h');
208 } 198 }
209 199
210 class A { 200 class A {
211 public: 201 public:
212 explicit A(int i) 202 explicit A(int i) : m_i(i) {}
213 : m_i(i) 203
214 { 204 int f() { return m_i; }
215 } 205 int addF(int j) { return m_i + j; }
216 206
217 int f() { return m_i; } 207 private:
218 int addF(int j) { return m_i + j; } 208 int m_i;
219 209 };
220 private: 210
221 int m_i; 211 TEST(FunctionalTest, MemberFunctionBind) {
222 }; 212 A a(10);
223 213 OwnPtr<Function<int()>> function1 = bind(&A::f, &a);
224 TEST(FunctionalTest, MemberFunctionBind) 214 EXPECT_EQ(10, (*function1)());
225 { 215
226 A a(10); 216 OwnPtr<Function<int()>> function2 = bind(&A::addF, &a, 15);
227 OwnPtr<Function<int()>> function1 = bind(&A::f, &a); 217 EXPECT_EQ(25, (*function2)());
228 EXPECT_EQ(10, (*function1)()); 218 }
229 219
230 OwnPtr<Function<int()>> function2 = bind(&A::addF, &a, 15); 220 TEST(FunctionalTest, MemberFunctionPartBind) {
231 EXPECT_EQ(25, (*function2)()); 221 A a(10);
232 } 222 OwnPtr<Function<int(class A*)>> function1 = bind<class A*>(&A::f);
233 223 EXPECT_EQ(10, (*function1)(&a));
234 TEST(FunctionalTest, MemberFunctionPartBind) 224
235 { 225 OwnPtr<Function<int(class A*, int)>> unboundFunction2 =
236 A a(10); 226 bind<class A*, int>(&A::addF);
237 OwnPtr<Function<int(class A*)>> function1 = bind<class A*>(&A::f); 227 EXPECT_EQ(25, (*unboundFunction2)(&a, 15));
238 EXPECT_EQ(10, (*function1)(&a)); 228 OwnPtr<Function<int(int)>> objectBoundFunction2 = bind<int>(&A::addF, &a);
239 229 EXPECT_EQ(25, (*objectBoundFunction2)(15));
240 OwnPtr<Function<int(class A*, int)>> unboundFunction2 =
241 bind<class A*, int>(&A::addF);
242 EXPECT_EQ(25, (*unboundFunction2)(&a, 15));
243 OwnPtr<Function<int(int)>> objectBoundFunction2 =
244 bind<int>(&A::addF, &a);
245 EXPECT_EQ(25, (*objectBoundFunction2)(15));
246 } 230 }
247 231
248 class Number : public RefCounted<Number> { 232 class Number : public RefCounted<Number> {
249 public: 233 public:
250 static PassRefPtr<Number> create(int value) 234 static PassRefPtr<Number> create(int value) {
251 { 235 return adoptRef(new Number(value));
252 return adoptRef(new Number(value)); 236 }
253 } 237
254 238 ~Number() { m_value = 0; }
255 ~Number() 239
256 { 240 int value() const { return m_value; }
257 m_value = 0; 241
258 } 242 private:
259 243 explicit Number(int value) : m_value(value) {}
260 int value() const { return m_value; } 244
261 245 int m_value;
262 private: 246 };
263 explicit Number(int value) 247
264 : m_value(value) 248 int multiplyNumberByTwo(Number* number) {
265 { 249 return number->value() * 2;
266 } 250 }
267 251
268 int m_value; 252 TEST(FunctionalTest, RefCountedStorage) {
269 }; 253 RefPtr<Number> five = Number::create(5);
270 254 OwnPtr<Function<int()>> multiplyFiveByTwoFunction =
271 int multiplyNumberByTwo(Number* number) 255 bind(multiplyNumberByTwo, five);
272 { 256 EXPECT_EQ(10, (*multiplyFiveByTwoFunction)());
273 return number->value() * 2; 257
274 } 258 OwnPtr<Function<int()>> multiplyFourByTwoFunction =
275 259 bind(multiplyNumberByTwo, Number::create(4));
276 TEST(FunctionalTest, RefCountedStorage) 260 EXPECT_EQ(8, (*multiplyFourByTwoFunction)());
277 { 261
278 RefPtr<Number> five = Number::create(5); 262 RefPtr<Number> six = Number::create(6);
279 OwnPtr<Function<int()>> multiplyFiveByTwoFunction = bind(multiplyNumberByTwo , five); 263 OwnPtr<Function<int()>> multiplySixByTwoFunction =
280 EXPECT_EQ(10, (*multiplyFiveByTwoFunction)()); 264 bind(multiplyNumberByTwo, six.release());
281 265 EXPECT_FALSE(six);
282 OwnPtr<Function<int()>> multiplyFourByTwoFunction = bind(multiplyNumberByTwo , Number::create(4)); 266 EXPECT_EQ(12, (*multiplySixByTwoFunction)());
283 EXPECT_EQ(8, (*multiplyFourByTwoFunction)()); 267 }
284 268
285 RefPtr<Number> six = Number::create(6); 269 int processUnwrappedClass(const UnwrappedClass& u, int v) {
286 OwnPtr<Function<int()>> multiplySixByTwoFunction = bind(multiplyNumberByTwo, six.release()); 270 return u.value() * v;
287 EXPECT_FALSE(six);
288 EXPECT_EQ(12, (*multiplySixByTwoFunction)());
289 }
290
291 int processUnwrappedClass(const UnwrappedClass& u, int v)
292 {
293 return u.value() * v;
294 } 271 }
295 272
296 // Tests that: 273 // Tests that:
297 // - ParamStorageTraits's wrap()/unwrap() are called, and 274 // - ParamStorageTraits's wrap()/unwrap() are called, and
298 // - bind()'s arguments are passed as references to ParamStorageTraits::wrap() 275 // - bind()'s arguments are passed as references to ParamStorageTraits::wrap()
299 // with no additional copies. 276 // with no additional copies.
300 // This test would fail in compile if something is wrong, 277 // This test would fail in compile if something is wrong,
301 // rather than in runtime. 278 // rather than in runtime.
302 TEST(FunctionalTest, WrapUnwrap) 279 TEST(FunctionalTest, WrapUnwrap) {
303 { 280 OwnPtr<Function<int()>> function =
304 OwnPtr<Function<int()>> function = bind(processUnwrappedClass, ClassToBeWrap ped(3), 7); 281 bind(processUnwrappedClass, ClassToBeWrapped(3), 7);
305 EXPECT_EQ(21, (*function)()); 282 EXPECT_EQ(21, (*function)());
306 } 283 }
307 284
308 TEST(FunctionalTest, WrapUnwrapInPartialBind) 285 TEST(FunctionalTest, WrapUnwrapInPartialBind) {
309 { 286 OwnPtr<Function<int(int)>> partiallyBoundFunction =
310 OwnPtr<Function<int(int)>> partiallyBoundFunction = bind<int>(processUnwrapp edClass, ClassToBeWrapped(3)); 287 bind<int>(processUnwrappedClass, ClassToBeWrapped(3));
311 EXPECT_EQ(21, (*partiallyBoundFunction)(7)); 288 EXPECT_EQ(21, (*partiallyBoundFunction)(7));
312 } 289 }
313 290
314 } // anonymous namespace 291 } // anonymous namespace
315 292
316 } // namespace WTF 293 } // namespace WTF
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/Functional.h ('k') | third_party/WebKit/Source/wtf/GetPtr.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698