Blender  V3.3
BLI_function_ref_test.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0 */
2 
3 #include "BLI_function_ref.hh"
4 
5 #include "testing/testing.h"
6 
7 namespace blender::tests {
8 
9 static int perform_binary_operation(int a, int b, FunctionRef<int(int, int)> operation)
10 {
11  return operation(a, b);
12 }
13 
14 TEST(function_ref, StatelessLambda)
15 {
16  const int result = perform_binary_operation(4, 6, [](int a, int b) { return a - b; });
17  EXPECT_EQ(result, -2);
18 }
19 
20 TEST(function_ref, StatefullLambda)
21 {
22  const int factor = 10;
23  const int result = perform_binary_operation(
24  2, 3, [&](int a, int b) { return factor * (a + b); });
25  EXPECT_EQ(result, 50);
26 }
27 
28 static int add_two_numbers(int a, int b)
29 {
30  return a + b;
31 }
32 
33 TEST(function_ref, StandaloneFunction)
34 {
36  EXPECT_EQ(result, 15);
37 }
38 
39 TEST(function_ref, ConstantFunction)
40 {
41  auto f = []() { return 42; };
42  FunctionRef<int()> ref = f;
43  EXPECT_EQ(ref(), 42);
44 }
45 
46 TEST(function_ref, MutableStatefullLambda)
47 {
48  int counter = 0;
49  auto f = [&]() mutable { return counter++; };
50  FunctionRef<int()> ref = f;
51  EXPECT_EQ(ref(), 0);
52  EXPECT_EQ(ref(), 1);
53  EXPECT_EQ(ref(), 2);
54 }
55 
56 TEST(function_ref, Null)
57 {
58  FunctionRef<int()> ref;
59  EXPECT_FALSE(ref);
60 
61  auto f = []() { return 1; };
62  ref = f;
63  EXPECT_TRUE(ref);
64 
65  ref = {};
66  EXPECT_FALSE(ref);
67 }
68 
69 TEST(function_ref, CopyDoesNotReferenceFunctionRef)
70 {
71  auto f1 = []() { return 1; };
72  auto f2 = []() { return 2; };
73  FunctionRef<int()> x = f1;
74  FunctionRef<int()> y = x;
75  x = f2;
76  EXPECT_EQ(y(), 1);
77 }
78 
79 TEST(function_ref, CopyDoesNotReferenceFunctionRef2)
80 {
81  auto f = []() { return 1; };
82  FunctionRef<int()> x;
83  FunctionRef<int()> y = f;
84  FunctionRef<int()> z = static_cast<const FunctionRef<int()> &&>(y);
85  x = z;
86  y = {};
87  EXPECT_EQ(x(), 1);
88 }
89 
90 TEST(function_ref, ReferenceAnotherFunctionRef)
91 {
92  auto f1 = []() { return 1; };
93  auto f2 = []() { return 2; };
94  FunctionRef<int()> x = f1;
95  auto f3 = [&]() { return x(); };
96  FunctionRef<int()> y = f3;
97  EXPECT_EQ(y(), 1);
98  x = f2;
99  EXPECT_EQ(y(), 2);
100 }
101 
102 TEST(function_ref, CallSafe)
103 {
104  FunctionRef<int()> f;
105  EXPECT_FALSE(f.call_safe().has_value());
106  auto func = []() { return 10; };
107  f = func;
108  EXPECT_TRUE(f.call_safe().has_value());
109  EXPECT_EQ(*f.call_safe(), 10);
110  f = {};
111  EXPECT_FALSE(f.call_safe().has_value());
112  BLI_STATIC_ASSERT((std::is_same_v<decltype(f.call_safe()), std::optional<int>>), "");
113 }
114 
115 TEST(function_ref, CallSafeVoid)
116 {
117  FunctionRef<void()> f;
118  BLI_STATIC_ASSERT((std::is_same_v<decltype(f.call_safe()), void>), "");
119  f.call_safe();
120  int value = 0;
121  auto func = [&]() { value++; };
122  f = func;
123  f.call_safe();
124  EXPECT_EQ(value, 1);
125 }
126 
127 TEST(function_ref, InitializeWithNull)
128 {
129  FunctionRef<int(int, int)> f{nullptr};
130  EXPECT_FALSE(f);
131 }
132 
133 } // namespace blender::tests
#define BLI_STATIC_ASSERT(a, msg)
Definition: BLI_assert.h:83
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble z
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
SyclQueue void void size_t num_bytes void
ccl_gpu_kernel_postfix ccl_global int * counter
static unsigned a[3]
Definition: RandGen.cpp:78
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)
Definition: BLI_any_test.cc:10
static const pxr::TfToken b("b", pxr::TfToken::Immortal)