OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkFunction.h" | 8 #include "SkFunction.h" |
9 #include "Test.h" | 9 #include "Test.h" |
10 | 10 |
11 static void test_add_five(skiatest::Reporter* r, SkFunction<int(int)>&& f) { | 11 static void test_add_five(skiatest::Reporter* r, SkFunction<int(int)>&& f) { |
12 REPORTER_ASSERT(r, f(3) == 8); | 12 REPORTER_ASSERT(r, f(3) == 8); |
13 REPORTER_ASSERT(r, f(4) == 9); | 13 REPORTER_ASSERT(r, f(4) == 9); |
14 } | 14 } |
15 | 15 |
16 static int add_five(int x) { return x + 5; } | 16 static int add_five(int x) { return x + 5; } |
17 | 17 |
18 struct AddFive { | 18 struct AddFive { |
19 int operator()(int x) { return x + 5; }; | 19 int operator()(int x) { return x + 5; }; |
20 }; | 20 }; |
21 | 21 |
| 22 class MoveOnlyAdd5 : SkNoncopyable { |
| 23 public: |
| 24 MoveOnlyAdd5() {} |
| 25 MoveOnlyAdd5(MoveOnlyAdd5&&) {} |
| 26 MoveOnlyAdd5& operator=(MoveOnlyAdd5&&) { return *this; } |
| 27 |
| 28 int operator()(int x) { return x + 5; } |
| 29 }; |
| 30 |
22 DEF_TEST(Function, r) { | 31 DEF_TEST(Function, r) { |
23 // We should be able to turn a static function, an explicit functor, or a la
mbda | 32 // We should be able to turn a static function, an explicit functor, or a la
mbda |
24 // all into an SkFunction equally well. | 33 // all into an SkFunction equally well. |
25 test_add_five(r, SkFunction<int(int)>(&add_five)); | 34 test_add_five(r, &add_five); |
26 test_add_five(r, SkFunction<int(int)>(AddFive())); | 35 test_add_five(r, AddFive()); |
27 test_add_five(r, SkFunction<int(int)>([](int x) { return x + 5; })); | 36 test_add_five(r, [](int x) { return x + 5; }); |
28 | 37 |
29 // AddFive and the lambda above are both small enough to test small-object o
ptimization. | 38 // AddFive and the lambda above are both small enough to test small-object o
ptimization. |
30 // Now test a lambda that's much too large for the small-object optimization
. | 39 // Now test a lambda that's much too large for the small-object optimization
. |
31 int a = 1, b = 1, c = 1, d = 1, e = 1; | 40 int a = 1, b = 1, c = 1, d = 1, e = 1; |
32 test_add_five(r, SkFunction<int(int)>([&](int x) { return x + a + b + c + d
+ e; })); | 41 test_add_five(r, [&](int x) { return x + a + b + c + d + e; }); |
33 } | |
34 | |
35 DEF_TEST(Function_forwarding, r) { | |
36 class MoveOnlyAdd5 : SkNoncopyable { | |
37 public: | |
38 MoveOnlyAdd5() {} | |
39 MoveOnlyAdd5(MoveOnlyAdd5&&) {} | |
40 MoveOnlyAdd5& operator=(MoveOnlyAdd5&&) { return *this; } | |
41 | |
42 int operator()(int x) { return x + 5; } | |
43 }; | |
44 | 42 |
45 // Makes sure we forward the functor when constructing SkFunction. | 43 // Makes sure we forward the functor when constructing SkFunction. |
46 test_add_five(r, SkFunction<int(int)>(MoveOnlyAdd5())); | 44 test_add_five(r, MoveOnlyAdd5()); |
47 | 45 |
48 // Makes sure we forward arguments when calling SkFunction. | 46 // Makes sure we forward arguments when calling SkFunction. |
49 SkFunction<int(int, MoveOnlyAdd5&&, int)> b([](int x, MoveOnlyAdd5&& f, int
y) { | 47 REPORTER_ASSERT(r, [](int x, MoveOnlyAdd5&& f, int y) { |
50 return x * f(y); | 48 return x * f(y); |
51 }); | 49 }(2, MoveOnlyAdd5(), 4) == 18); |
52 REPORTER_ASSERT(r, b(2, MoveOnlyAdd5(), 4) == 18); | |
53 } | 50 } |
OLD | NEW |