mtklein | 4a9426f | 2015-03-31 14:24:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkFunction.h" |
| 9 | #include "Test.h" |
| 10 | |
mtklein | 0d992db | 2015-05-06 07:40:25 -0700 | [diff] [blame] | 11 | static void test_add_five(skiatest::Reporter* r, SkFunction<int(int)>& f) { |
mtklein | 97312d0 | 2015-04-01 08:11:16 -0700 | [diff] [blame] | 12 | REPORTER_ASSERT(r, f(3) == 8); |
mtklein | 4a9426f | 2015-03-31 14:24:27 -0700 | [diff] [blame] | 13 | REPORTER_ASSERT(r, f(4) == 9); |
| 14 | } |
| 15 | |
mtklein | 0d992db | 2015-05-06 07:40:25 -0700 | [diff] [blame] | 16 | static void test_add_five(skiatest::Reporter* r, SkFunction<int(int)>&& f) { test_add_five(r, f); } |
| 17 | |
mtklein | 4a9426f | 2015-03-31 14:24:27 -0700 | [diff] [blame] | 18 | static int add_five(int x) { return x + 5; } |
| 19 | |
| 20 | struct AddFive { |
mtklein | 0d992db | 2015-05-06 07:40:25 -0700 | [diff] [blame] | 21 | int operator()(int x) const { return x + 5; }; |
mtklein | 4a9426f | 2015-03-31 14:24:27 -0700 | [diff] [blame] | 22 | }; |
| 23 | |
mtklein | 0d992db | 2015-05-06 07:40:25 -0700 | [diff] [blame] | 24 | class MoveOnlyThree : SkNoncopyable { |
mtklein | 03e5161 | 2015-04-01 13:08:50 -0700 | [diff] [blame] | 25 | public: |
mtklein | 0d992db | 2015-05-06 07:40:25 -0700 | [diff] [blame] | 26 | MoveOnlyThree() {} |
| 27 | MoveOnlyThree(MoveOnlyThree&&) {} |
| 28 | MoveOnlyThree& operator=(MoveOnlyThree&&) { return *this; } |
mtklein | 03e5161 | 2015-04-01 13:08:50 -0700 | [diff] [blame] | 29 | |
mtklein | 0d992db | 2015-05-06 07:40:25 -0700 | [diff] [blame] | 30 | int val() { return 3; } |
mtklein | 03e5161 | 2015-04-01 13:08:50 -0700 | [diff] [blame] | 31 | }; |
| 32 | |
mtklein | 4a9426f | 2015-03-31 14:24:27 -0700 | [diff] [blame] | 33 | DEF_TEST(Function, r) { |
mtklein | 0d992db | 2015-05-06 07:40:25 -0700 | [diff] [blame] | 34 | // We should be able to turn a function pointer, an explicit functor, or a |
| 35 | // lambda into an SkFunction all equally well. |
mtklein | 03e5161 | 2015-04-01 13:08:50 -0700 | [diff] [blame] | 36 | test_add_five(r, &add_five); |
| 37 | test_add_five(r, AddFive()); |
| 38 | test_add_five(r, [](int x) { return x + 5; }); |
mtklein | 97312d0 | 2015-04-01 08:11:16 -0700 | [diff] [blame] | 39 | |
| 40 | // AddFive and the lambda above are both small enough to test small-object optimization. |
| 41 | // Now test a lambda that's much too large for the small-object optimization. |
| 42 | int a = 1, b = 1, c = 1, d = 1, e = 1; |
mtklein | 03e5161 | 2015-04-01 13:08:50 -0700 | [diff] [blame] | 43 | test_add_five(r, [&](int x) { return x + a + b + c + d + e; }); |
mtklein | 7441527 | 2015-04-01 11:26:31 -0700 | [diff] [blame] | 44 | |
mtklein | 7441527 | 2015-04-01 11:26:31 -0700 | [diff] [blame] | 45 | // Makes sure we forward arguments when calling SkFunction. |
mtklein | 0d992db | 2015-05-06 07:40:25 -0700 | [diff] [blame] | 46 | SkFunction<int(int, MoveOnlyThree&&, int)> f([](int x, MoveOnlyThree&& three, int y) { |
| 47 | return x * three.val() + y; |
mtklein | b41f057 | 2015-04-01 13:36:23 -0700 | [diff] [blame] | 48 | }); |
mtklein | 0d992db | 2015-05-06 07:40:25 -0700 | [diff] [blame] | 49 | REPORTER_ASSERT(r, f(2, MoveOnlyThree(), 4) == 10); |
| 50 | |
| 51 | // SkFunctions can go in containers. |
| 52 | SkTArray<SkFunction<int(int)>> add_fivers; |
| 53 | add_fivers.push_back(&add_five); |
| 54 | add_fivers.push_back(AddFive()); |
| 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; |
mtklein | 7441527 | 2015-04-01 11:26:31 -0700 | [diff] [blame] | 70 | } |