OLD | NEW |
| (Empty) |
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 | |
11 static void test_add_five(skiatest::Reporter* r, SkFunction<int(int)>& f) { | |
12 REPORTER_ASSERT(r, f(3) == 8); | |
13 REPORTER_ASSERT(r, f(4) == 9); | |
14 } | |
15 | |
16 static void test_add_five(skiatest::Reporter* r, SkFunction<int(int)>&& f) { tes
t_add_five(r, f); } | |
17 | |
18 static int add_five(int x) { return x + 5; } | |
19 | |
20 struct AddFive { | |
21 int operator()(int x) const { return x + 5; }; | |
22 }; | |
23 | |
24 class MoveOnlyThree : SkNoncopyable { | |
25 public: | |
26 MoveOnlyThree() {} | |
27 MoveOnlyThree(MoveOnlyThree&&) {} | |
28 MoveOnlyThree& operator=(MoveOnlyThree&&) { return *this; } | |
29 | |
30 int val() { return 3; } | |
31 }; | |
32 | |
33 DEF_TEST(Function, r) { | |
34 // We should be able to turn a function pointer, an explicit functor, or a | |
35 // lambda into an SkFunction all equally well. | |
36 test_add_five(r, &add_five); | |
37 test_add_five(r, AddFive()); | |
38 test_add_five(r, [](int x) { return x + 5; }); | |
39 | |
40 // AddFive and the lambda above are both small enough to test small-object o
ptimization. | |
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; | |
43 test_add_five(r, [&](int x) { return x + a + b + c + d + e; }); | |
44 | |
45 // Makes sure we forward arguments when calling SkFunction. | |
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); | |
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; | |
70 } | |
OLD | NEW |