| 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 void test_add_five(skiatest::Reporter* r, SkFunction<int(int)>&& f) { tes
t_add_five(r, f); } |
| 17 |
| 16 static int add_five(int x) { return x + 5; } | 18 static int add_five(int x) { return x + 5; } |
| 17 | 19 |
| 18 struct AddFive { | 20 struct AddFive { |
| 19 int operator()(int x) { return x + 5; }; | 21 int operator()(int x) const { return x + 5; }; |
| 20 }; | 22 }; |
| 21 | 23 |
| 22 class MoveOnlyAdd5 : SkNoncopyable { | 24 class MoveOnlyThree : SkNoncopyable { |
| 23 public: | 25 public: |
| 24 MoveOnlyAdd5() {} | 26 MoveOnlyThree() {} |
| 25 MoveOnlyAdd5(MoveOnlyAdd5&&) {} | 27 MoveOnlyThree(MoveOnlyThree&&) {} |
| 26 MoveOnlyAdd5& operator=(MoveOnlyAdd5&&) { return *this; } | 28 MoveOnlyThree& operator=(MoveOnlyThree&&) { return *this; } |
| 27 | 29 |
| 28 int operator()(int x) { return x + 5; } | 30 int val() { return 3; } |
| 29 }; | 31 }; |
| 30 | 32 |
| 31 DEF_TEST(Function, r) { | 33 DEF_TEST(Function, r) { |
| 32 // We should be able to turn a static function, an explicit functor, or a la
mbda | 34 // We should be able to turn a function pointer, an explicit functor, or a |
| 33 // all into an SkFunction equally well. | 35 // lambda into an SkFunction all equally well. |
| 34 test_add_five(r, &add_five); | 36 test_add_five(r, &add_five); |
| 35 test_add_five(r, AddFive()); | 37 test_add_five(r, AddFive()); |
| 36 test_add_five(r, [](int x) { return x + 5; }); | 38 test_add_five(r, [](int x) { return x + 5; }); |
| 37 | 39 |
| 38 // AddFive and the lambda above are both small enough to test small-object o
ptimization. | 40 // AddFive and the lambda above are both small enough to test small-object o
ptimization. |
| 39 // Now test a lambda that's much too large for the small-object optimization
. | 41 // Now test a lambda that's much too large for the small-object optimization
. |
| 40 int a = 1, b = 1, c = 1, d = 1, e = 1; | 42 int a = 1, b = 1, c = 1, d = 1, e = 1; |
| 41 test_add_five(r, [&](int x) { return x + a + b + c + d + e; }); | 43 test_add_five(r, [&](int x) { return x + a + b + c + d + e; }); |
| 42 | 44 |
| 43 // Makes sure we forward the functor when constructing SkFunction. | 45 // Makes sure we forward arguments when calling SkFunction. |
| 44 test_add_five(r, MoveOnlyAdd5()); | 46 SkFunction<int(int, MoveOnlyThree&&, int)> f([](int x, MoveOnlyThree&& three
, int y) { |
| 47 return x * three.val() + y; |
| 48 }); |
| 49 REPORTER_ASSERT(r, f(2, MoveOnlyThree(), 4) == 10); |
| 45 | 50 |
| 46 // Makes sure we forward arguments when calling SkFunction. | 51 // SkFunctions can go in containers. |
| 47 SkFunction<int(int, MoveOnlyAdd5&&, int)> f([](int x, MoveOnlyAdd5&& addFive
, int y) { | 52 SkTArray<SkFunction<int(int)>> add_fivers; |
| 48 return x * addFive(y); | 53 add_fivers.push_back(&add_five); |
| 49 }); | 54 add_fivers.push_back(AddFive()); |
| 50 REPORTER_ASSERT(r, f(2, MoveOnlyAdd5(), 4) == 18); | 55 add_fivers.push_back([](int x) { return x + 5; }); |
| 56 add_fivers.push_back([&](int x) { return x + a + b + c + d + e; }); |
| 57 for (auto& f : add_fivers) { |
| 58 test_add_five(r, f); |
| 59 } |
| 60 |
| 61 // SkFunctions are assignable. |
| 62 SkFunction<int(int)> empty; |
| 63 empty = [](int x) { return x + 5; }; |
| 64 test_add_five(r, empty); |
| 65 |
| 66 // This all is silly acrobatics, but it should at least work correctly. |
| 67 SkFunction<int(int)> emptyA, emptyB(emptyA); |
| 68 emptyA = emptyB; |
| 69 emptyA = emptyA; |
| 51 } | 70 } |
| OLD | NEW |