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

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

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