Blender V4.3
BLI_function_ref_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "BLI_function_ref.hh"
6
7#include "testing/testing.h"
8
9namespace blender::tests {
10
11static int perform_binary_operation(int a, int b, FunctionRef<int(int, int)> operation)
12{
13 return operation(a, b);
14}
15
16TEST(function_ref, StatelessLambda)
17{
18 const int result = perform_binary_operation(4, 6, [](int a, int b) { return a - b; });
19 EXPECT_EQ(result, -2);
20}
21
22TEST(function_ref, StatefullLambda)
23{
24 const int factor = 10;
25 const int result = perform_binary_operation(
26 2, 3, [&](int a, int b) { return factor * (a + b); });
27 EXPECT_EQ(result, 50);
28}
29
30static int add_two_numbers(int a, int b)
31{
32 return a + b;
33}
34
35TEST(function_ref, StandaloneFunction)
36{
37 const int result = perform_binary_operation(10, 5, add_two_numbers);
38 EXPECT_EQ(result, 15);
39}
40
41TEST(function_ref, ConstantFunction)
42{
43 auto f = []() { return 42; };
44 FunctionRef<int()> ref = f;
45 EXPECT_EQ(ref(), 42);
46}
47
48TEST(function_ref, MutableStatefullLambda)
49{
50 int counter = 0;
51 auto f = [&]() mutable { return counter++; };
52 FunctionRef<int()> ref = f;
53 EXPECT_EQ(ref(), 0);
54 EXPECT_EQ(ref(), 1);
55 EXPECT_EQ(ref(), 2);
56}
57
58TEST(function_ref, Null)
59{
60 FunctionRef<int()> ref;
61 EXPECT_FALSE(ref);
62
63 auto f = []() { return 1; };
64 ref = f;
65 EXPECT_TRUE(ref);
66
67 ref = {};
68 EXPECT_FALSE(ref);
69}
70
71TEST(function_ref, CopyDoesNotReferenceFunctionRef)
72{
73 auto f1 = []() { return 1; };
74 auto f2 = []() { return 2; };
75 FunctionRef<int()> x = f1;
76 FunctionRef<int()> y = x;
77 x = f2;
78 EXPECT_EQ(y(), 1);
79}
80
81TEST(function_ref, CopyDoesNotReferenceFunctionRef2)
82{
83 auto f = []() { return 1; };
84 FunctionRef<int()> x;
85 FunctionRef<int()> y = f;
86 FunctionRef<int()> z = static_cast<const FunctionRef<int()> &&>(y);
87 x = z;
88 y = {};
89 EXPECT_EQ(x(), 1);
90}
91
92TEST(function_ref, ReferenceAnotherFunctionRef)
93{
94 auto f1 = []() { return 1; };
95 auto f2 = []() { return 2; };
96 FunctionRef<int()> x = f1;
97 auto f3 = [&]() { return x(); };
98 FunctionRef<int()> y = f3;
99 EXPECT_EQ(y(), 1);
100 x = f2;
101 EXPECT_EQ(y(), 2);
102}
103
104TEST(function_ref, InitializeWithNull)
105{
106 FunctionRef<int(int, int)> f{nullptr};
107 EXPECT_FALSE(f);
108}
109
110static int overload_test(const FunctionRef<void(std::string)> /*fn*/)
111{
112 return 1;
113}
114
115static int overload_test(const FunctionRef<void(int)> /*fn*/)
116{
117 return 2;
118}
119
120TEST(function_ref, OverloadSelection)
121{
122 const auto fn_1 = [](std::string /*x*/) {};
123 const auto fn_2 = [](int /*x*/) {};
124
125 EXPECT_EQ(overload_test(fn_1), 1);
126 EXPECT_EQ(overload_test(fn_2), 2);
127}
128
129} // namespace blender::tests
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
local_group_size(16, 16) .push_constant(Type b
draw_view push_constant(Type::INT, "radiance_src") .push_constant(Type capture_info_buf storage_buf(1, Qualifier::READ, "ObjectBounds", "bounds_buf[]") .push_constant(Type draw_view int
static int add_two_numbers(int a, int b)
static int perform_binary_operation(int a, int b, FunctionRef< int(int, int)> operation)
TEST(any, DefaultConstructor)
static int overload_test(const FunctionRef< void(std::string)>)