2015-03-31 21:24:27 +00:00
|
|
|
/*
|
|
|
|
* 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"
|
|
|
|
|
2015-05-06 14:40:25 +00:00
|
|
|
static void test_add_five(skiatest::Reporter* r, SkFunction<int(int)>& f) {
|
2015-04-01 15:11:16 +00:00
|
|
|
REPORTER_ASSERT(r, f(3) == 8);
|
2015-03-31 21:24:27 +00:00
|
|
|
REPORTER_ASSERT(r, f(4) == 9);
|
|
|
|
}
|
|
|
|
|
2015-05-06 14:40:25 +00:00
|
|
|
static void test_add_five(skiatest::Reporter* r, SkFunction<int(int)>&& f) { test_add_five(r, f); }
|
|
|
|
|
2015-03-31 21:24:27 +00:00
|
|
|
static int add_five(int x) { return x + 5; }
|
|
|
|
|
|
|
|
struct AddFive {
|
2015-05-06 14:40:25 +00:00
|
|
|
int operator()(int x) const { return x + 5; };
|
2015-03-31 21:24:27 +00:00
|
|
|
};
|
|
|
|
|
2015-05-06 14:40:25 +00:00
|
|
|
class MoveOnlyThree : SkNoncopyable {
|
2015-04-01 20:08:50 +00:00
|
|
|
public:
|
2015-05-06 14:40:25 +00:00
|
|
|
MoveOnlyThree() {}
|
|
|
|
MoveOnlyThree(MoveOnlyThree&&) {}
|
|
|
|
MoveOnlyThree& operator=(MoveOnlyThree&&) { return *this; }
|
2015-04-01 20:08:50 +00:00
|
|
|
|
2015-05-06 14:40:25 +00:00
|
|
|
int val() { return 3; }
|
2015-04-01 20:08:50 +00:00
|
|
|
};
|
|
|
|
|
2015-03-31 21:24:27 +00:00
|
|
|
DEF_TEST(Function, r) {
|
2015-05-06 14:40:25 +00:00
|
|
|
// We should be able to turn a function pointer, an explicit functor, or a
|
|
|
|
// lambda into an SkFunction all equally well.
|
2015-04-01 20:08:50 +00:00
|
|
|
test_add_five(r, &add_five);
|
|
|
|
test_add_five(r, AddFive());
|
|
|
|
test_add_five(r, [](int x) { return x + 5; });
|
2015-04-01 15:11:16 +00:00
|
|
|
|
|
|
|
// 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;
|
2015-04-01 20:08:50 +00:00
|
|
|
test_add_five(r, [&](int x) { return x + a + b + c + d + e; });
|
2015-04-01 18:26:31 +00:00
|
|
|
|
|
|
|
// Makes sure we forward arguments when calling SkFunction.
|
2015-05-06 14:40:25 +00:00
|
|
|
SkFunction<int(int, MoveOnlyThree&&, int)> f([](int x, MoveOnlyThree&& three, int y) {
|
|
|
|
return x * three.val() + y;
|
2015-04-01 20:36:23 +00:00
|
|
|
});
|
2015-05-06 14:40:25 +00:00
|
|
|
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;
|
2015-04-01 18:26:31 +00:00
|
|
|
}
|