blob: 6fffcbd43ae92d5699aa7d3194a68135b1b2078c [file] [log] [blame]
mtklein4a9426f2015-03-31 14:24:27 -07001/*
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
11static void test_add_five(skiatest::Reporter* r, SkFunction<int(int)>&& f) {
mtklein97312d02015-04-01 08:11:16 -070012 REPORTER_ASSERT(r, f(3) == 8);
mtklein4a9426f2015-03-31 14:24:27 -070013 REPORTER_ASSERT(r, f(4) == 9);
14}
15
16static int add_five(int x) { return x + 5; }
17
18struct AddFive {
19 int operator()(int x) { return x + 5; };
20};
21
22DEF_TEST(Function, r) {
23 // We should be able to turn a static function, an explicit functor, or a lambda
24 // all into an SkFunction equally well.
25 test_add_five(r, SkFunction<int(int)>(&add_five));
26 test_add_five(r, SkFunction<int(int)>(AddFive()));
27 test_add_five(r, SkFunction<int(int)>([](int x) { return x + 5; }));
mtklein97312d02015-04-01 08:11:16 -070028
29 // AddFive and the lambda above are both small enough to test small-object optimization.
30 // Now test a lambda that's much too large for the small-object optimization.
31 int a = 1, b = 1, c = 1, d = 1, e = 1;
32 test_add_five(r, SkFunction<int(int)>([&](int x) { return x + a + b + c + d + e; }));
mtklein4a9426f2015-03-31 14:24:27 -070033}