aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/FunctionTest.cpp
blob: cf6b78905f5007a194a931e925a604b4a16c81c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkFunction.h"
#include "Test.h"

static void test_add_five(skiatest::Reporter* r, SkFunction<int(int)>& f) {
    REPORTER_ASSERT(r, f(3) == 8);
    REPORTER_ASSERT(r, f(4) == 9);
}

static void test_add_five(skiatest::Reporter* r, SkFunction<int(int)>&& f) { test_add_five(r, f); }

static int add_five(int x) { return x + 5; }

struct AddFive {
    int operator()(int x) const { return x + 5; };
};

class MoveOnlyThree : SkNoncopyable {
public:
    MoveOnlyThree() {}
    MoveOnlyThree(MoveOnlyThree&&) {}
    MoveOnlyThree& operator=(MoveOnlyThree&&) { return *this; }

    int val() { return 3; }
};

DEF_TEST(Function, r) {
    // We should be able to turn a function pointer, an explicit functor, or a
    // lambda into an SkFunction all equally well.
    test_add_five(r, &add_five);
    test_add_five(r, AddFive());
    test_add_five(r, [](int x) { return x + 5; });

    // AddFive and the lambda above are both small enough to test small-object optimization.
    // Now test a lambda that's much too large for the small-object optimization.
    int a = 1, b = 1, c = 1, d = 1, e = 1;
    test_add_five(r, [&](int x) { return x + a + b + c + d + e; });

    // Makes sure we forward arguments when calling SkFunction.
    SkFunction<int(int, MoveOnlyThree&&, int)> f([](int x, MoveOnlyThree&& three, int y) {
            return x * three.val() + y;
    });
    REPORTER_ASSERT(r, f(2, MoveOnlyThree(), 4) == 10);

    // SkFunctions can go in containers.
    SkTArray<SkFunction<int(int)>> add_fivers;
    add_fivers.push_back(&add_five);
    add_fivers.push_back(AddFive());
    add_fivers.push_back([](int x) { return x + 5; });
    add_fivers.push_back([&](int x) { return x + a + b + c + d + e; });
    for (auto& f : add_fivers) {
        test_add_five(r, f);
    }

    // SkFunctions are assignable.
    SkFunction<int(int)> empty;
    empty = [](int x) { return x + 5; };
    test_add_five(r, empty);

    // This all is silly acrobatics, but it should at least work correctly.
    SkFunction<int(int)> emptyA, emptyB(emptyA);
    emptyA = emptyB;
    emptyA = emptyA;
}