Blender V4.3
BLI_expr_pylike_eval_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 "testing/testing.h"
6
7#include <cstring>
8
10#include "BLI_math_base.h"
11#include "BLI_utildefines.h"
12
13#define TRUE_VAL 1.0
14#define FALSE_VAL 0.0
15
16static void expr_pylike_parse_fail_test(const char *str)
17{
18 ExprPyLike_Parsed *expr = BLI_expr_pylike_parse(str, nullptr, 0);
19
20 EXPECT_FALSE(BLI_expr_pylike_is_valid(expr));
21
23}
24
25static void expr_pylike_const_test(const char *str, double value, bool force_const)
26{
27 ExprPyLike_Parsed *expr = BLI_expr_pylike_parse(str, nullptr, 0);
28
29 if (force_const) {
30 EXPECT_TRUE(BLI_expr_pylike_is_constant(expr));
31 }
32 else {
33 EXPECT_TRUE(BLI_expr_pylike_is_valid(expr));
34 EXPECT_FALSE(BLI_expr_pylike_is_constant(expr));
35 }
36
37 double result;
38 eExprPyLike_EvalStatus status = BLI_expr_pylike_eval(expr, nullptr, 0, &result);
39
41 EXPECT_EQ(result, value);
42
44}
45
46static ExprPyLike_Parsed *parse_for_eval(const char *str, bool nonconst)
47{
48 const char *names[1] = {"x"};
50
51 EXPECT_TRUE(BLI_expr_pylike_is_valid(expr));
52
53 if (nonconst) {
54 EXPECT_FALSE(BLI_expr_pylike_is_constant(expr));
55 }
56
57 return expr;
58}
59
60static void verify_eval_result(ExprPyLike_Parsed *expr, double x, double value)
61{
62 double result;
63 eExprPyLike_EvalStatus status = BLI_expr_pylike_eval(expr, &x, 1, &result);
64
66 EXPECT_EQ(result, value);
67}
68
69static void expr_pylike_eval_test(const char *str, double x, double value)
70{
72 verify_eval_result(expr, x, value);
74}
75
76static void expr_pylike_error_test(const char *str, double x, eExprPyLike_EvalStatus error)
77{
78 ExprPyLike_Parsed *expr = parse_for_eval(str, false);
79
80 double result;
81 eExprPyLike_EvalStatus status = BLI_expr_pylike_eval(expr, &x, 1, &result);
82
83 EXPECT_EQ(status, error);
84
86}
87
88#define TEST_PARSE_FAIL(name, str) \
89 TEST(expr_pylike, ParseFail_##name) \
90 { \
91 expr_pylike_parse_fail_test(str); \
92 }
93
94TEST_PARSE_FAIL(Empty, "")
95TEST_PARSE_FAIL(ConstHex, "0x0")
96TEST_PARSE_FAIL(ConstOctal, "01")
97TEST_PARSE_FAIL(Tail, "0 0")
98TEST_PARSE_FAIL(ConstFloatExp, "0.5e+")
99TEST_PARSE_FAIL(BadId, "Pi")
100TEST_PARSE_FAIL(BadArgCount0, "sqrt")
101TEST_PARSE_FAIL(BadArgCount1, "sqrt()")
102TEST_PARSE_FAIL(BadArgCount2, "sqrt(1,2)")
103TEST_PARSE_FAIL(BadArgCount3, "pi()")
104TEST_PARSE_FAIL(BadArgCount4, "max()")
105TEST_PARSE_FAIL(BadArgCount5, "min()")
106
107TEST_PARSE_FAIL(Truncated1, "(1+2")
108TEST_PARSE_FAIL(Truncated2, "1 if 2")
109TEST_PARSE_FAIL(Truncated3, "1 if 2 else")
110TEST_PARSE_FAIL(Truncated4, "1 < 2 <")
111TEST_PARSE_FAIL(Truncated5, "1 +")
112TEST_PARSE_FAIL(Truncated6, "1 *")
113TEST_PARSE_FAIL(Truncated7, "1 and")
114TEST_PARSE_FAIL(Truncated8, "1 or")
115TEST_PARSE_FAIL(Truncated9, "sqrt(1")
116TEST_PARSE_FAIL(Truncated10, "fmod(1,")
117
118/* Constant expression with working constant folding */
119#define TEST_CONST(name, str, value) \
120 TEST(expr_pylike, Const_##name) \
121 { \
122 expr_pylike_const_test(str, value, true); \
123 }
124
125/* Constant expression but constant folding is not supported */
126#define TEST_RESULT(name, str, value) \
127 TEST(expr_pylike, Result_##name) \
128 { \
129 expr_pylike_const_test(str, value, false); \
130 }
131
132/* Expression with an argument */
133#define TEST_EVAL(name, str, x, value) \
134 TEST(expr_pylike, Eval_##name) \
135 { \
136 expr_pylike_eval_test(str, x, value); \
137 }
138
139TEST_CONST(Zero, "0", 0.0)
140TEST_CONST(Zero2, "00", 0.0)
141TEST_CONST(One, "1", 1.0)
142TEST_CONST(OneF, "1.0", 1.0)
143TEST_CONST(OneF2, "1.", 1.0)
144TEST_CONST(OneE, "1e0", 1.0)
145TEST_CONST(TenE, "1.e+1", 10.0)
146TEST_CONST(Half, ".5", 0.5)
147
148TEST_CONST(Pi, "pi", M_PI)
149TEST_CONST(True, "True", TRUE_VAL)
150TEST_CONST(False, "False", FALSE_VAL)
151
152TEST_CONST(Sqrt, "sqrt(4)", 2.0)
153TEST_EVAL(Sqrt, "sqrt(x)", 4.0, 2.0)
154
155TEST_CONST(FMod, "fmod(3.5, 2)", 1.5)
156TEST_EVAL(FMod, "fmod(x, 2)", 3.5, 1.5)
157
158TEST_CONST(Pow, "pow(4, 0.5)", 2.0)
159TEST_EVAL(Pow, "pow(4, x)", 0.5, 2.0)
160
161TEST_CONST(Log2_1, "log(4, 2)", 2.0)
162
163TEST_CONST(Round1, "round(-0.5)", -1.0)
164TEST_CONST(Round2, "round(-0.4)", 0.0)
165TEST_CONST(Round3, "round(0.4)", 0.0)
166TEST_CONST(Round4, "round(0.5)", 1.0)
167
168TEST_CONST(Clamp1, "clamp(-0.1)", 0.0)
169TEST_CONST(Clamp2, "clamp(0.5)", 0.5)
170TEST_CONST(Clamp3, "clamp(1.5)", 1.0)
171TEST_CONST(Clamp4, "clamp(0.5, 0.2, 0.3)", 0.3)
172TEST_CONST(Clamp5, "clamp(0.0, 0.2, 0.3)", 0.2)
173
174TEST_CONST(Lerp1, "lerp(-10,10,-1)", -30.0)
175TEST_CONST(Lerp2, "lerp(-10,10,0.25)", -5.0)
176TEST_CONST(Lerp3, "lerp(-10,10,1)", 10.0)
177TEST_EVAL(Lerp1, "lerp(-10,10,x)", 0, -10.0)
178TEST_EVAL(Lerp2, "lerp(-10,10,x)", 0.75, 5.0)
179
180TEST_CONST(Smoothstep1, "smoothstep(-10,10,-20)", 0.0)
181TEST_CONST(Smoothstep2, "smoothstep(-10,10,-10)", 0.0)
182TEST_CONST(Smoothstep3, "smoothstep(-10,10,10)", 1.0)
183TEST_CONST(Smoothstep4, "smoothstep(-10,10,20)", 1.0)
184TEST_CONST(Smoothstep5, "smoothstep(-10,10,-5)", 0.15625)
185TEST_EVAL(Smoothstep1, "smoothstep(-10,10,x)", 5, 0.84375)
186
187TEST_RESULT(Min1, "min(3,1,2)", 1.0)
188TEST_RESULT(Max1, "max(3,1,2)", 3.0)
189TEST_RESULT(Min2, "min(1,2,3)", 1.0)
190TEST_RESULT(Max2, "max(1,2,3)", 3.0)
191TEST_RESULT(Min3, "min(2,3,1)", 1.0)
192TEST_RESULT(Max3, "max(2,3,1)", 3.0)
193
194TEST_CONST(UnaryPlus, "+1", 1.0)
195
196TEST_CONST(UnaryMinus, "-1", -1.0)
197TEST_EVAL(UnaryMinus, "-x", 1.0, -1.0)
198
199TEST_CONST(BinaryPlus, "1+2", 3.0)
200TEST_EVAL(BinaryPlus, "x+2", 1, 3.0)
201
202TEST_CONST(BinaryMinus, "1-2", -1.0)
203TEST_EVAL(BinaryMinus, "1-x", 2, -1.0)
204
205TEST_CONST(BinaryMul, "2*3", 6.0)
206TEST_EVAL(BinaryMul, "x*3", 2, 6.0)
207
208TEST_CONST(BinaryDiv, "3/2", 1.5)
209TEST_EVAL(BinaryDiv, "3/x", 2, 1.5)
210
211TEST_CONST(Arith1, "1 + -2 * 3", -5.0)
212TEST_CONST(Arith2, "(1 + -2) * 3", -3.0)
213TEST_CONST(Arith3, "-1 + 2 * 3", 5.0)
214TEST_CONST(Arith4, "3 * (-2 + 1)", -3.0)
215
216TEST_EVAL(Arith1, "1 + -x * 3", 2, -5.0)
217
218TEST_CONST(Eq1, "1 == 1.0", TRUE_VAL)
219TEST_CONST(Eq2, "1 == 2.0", FALSE_VAL)
220TEST_CONST(Eq3, "True == 1", TRUE_VAL)
221TEST_CONST(Eq4, "False == 0", TRUE_VAL)
222
223TEST_EVAL(Eq1, "1 == x", 1.0, TRUE_VAL)
224TEST_EVAL(Eq2, "1 == x", 2.0, FALSE_VAL)
225
226TEST_CONST(NEq1, "1 != 1.0", FALSE_VAL)
227TEST_CONST(NEq2, "1 != 2.0", TRUE_VAL)
228
229TEST_EVAL(NEq1, "1 != x", 1.0, FALSE_VAL)
230TEST_EVAL(NEq2, "1 != x", 2.0, TRUE_VAL)
231
232TEST_CONST(Lt1, "1 < 1", FALSE_VAL)
233TEST_CONST(Lt2, "1 < 2", TRUE_VAL)
234TEST_CONST(Lt3, "2 < 1", FALSE_VAL)
235
236TEST_CONST(Le1, "1 <= 1", TRUE_VAL)
237TEST_CONST(Le2, "1 <= 2", TRUE_VAL)
238TEST_CONST(Le3, "2 <= 1", FALSE_VAL)
239
240TEST_CONST(Gt1, "1 > 1", FALSE_VAL)
241TEST_CONST(Gt2, "1 > 2", FALSE_VAL)
242TEST_CONST(Gt3, "2 > 1", TRUE_VAL)
243
244TEST_CONST(Ge1, "1 >= 1", TRUE_VAL)
245TEST_CONST(Ge2, "1 >= 2", FALSE_VAL)
246TEST_CONST(Ge3, "2 >= 1", TRUE_VAL)
247
248TEST_CONST(Cmp1, "3 == 1 + 2", TRUE_VAL)
249
250TEST_EVAL(Cmp1, "3 == x + 2", 1, TRUE_VAL)
251TEST_EVAL(Cmp1b, "3 == x + 2", 1.5, FALSE_VAL)
252
253TEST_RESULT(CmpChain1, "1 < 2 < 3", TRUE_VAL)
254TEST_RESULT(CmpChain2, "1 < 2 == 2", TRUE_VAL)
255TEST_RESULT(CmpChain3, "1 < 2 > -1", TRUE_VAL)
256TEST_RESULT(CmpChain4, "1 < 2 < 2 < 3", FALSE_VAL)
257TEST_RESULT(CmpChain5, "1 < 2 <= 2 < 3", TRUE_VAL)
258
259TEST_EVAL(CmpChain1a, "1 < x < 3", 2, TRUE_VAL)
260TEST_EVAL(CmpChain1b, "1 < x < 3", 1, FALSE_VAL)
261TEST_EVAL(CmpChain1c, "1 < x < 3", 3, FALSE_VAL)
262
263TEST_CONST(Not1, "not 2", FALSE_VAL)
264TEST_CONST(Not2, "not 0", TRUE_VAL)
265TEST_CONST(Not3, "not not 2", TRUE_VAL)
266
267TEST_EVAL(Not1, "not x", 2, FALSE_VAL)
268TEST_EVAL(Not2, "not x", 0, TRUE_VAL)
269
270TEST_RESULT(And1, "2 and 3", 3.0)
271TEST_RESULT(And2, "0 and 3", 0.0)
272
273TEST_RESULT(Or1, "2 or 3", 2.0)
274TEST_RESULT(Or2, "0 or 3", 3.0)
275
276TEST_RESULT(Bool1, "2 or 3 and 4", 2.0)
277TEST_RESULT(Bool2, "not 2 or 3 and 4", 4.0)
278
279TEST(expr_pylike, Eval_Ternary1)
280{
281 ExprPyLike_Parsed *expr = parse_for_eval("x / 2 if x < 4 else x - 2 if x < 8 else x*2 - 12",
282 true);
283
284 for (int i = 0; i <= 10; i++) {
285 double x = i;
286 double v = (x < 4) ? (x / 2) : (x < 8) ? (x - 2) : (x * 2 - 12);
287
288 verify_eval_result(expr, x, v);
289 }
290
292}
293
294TEST(expr_pylike, MultipleArgs)
295{
296 const char *names[3] = {"x", "y", "x"};
297 const double values[3] = {1.0, 2.0, 3.0};
298
299 ExprPyLike_Parsed *expr = BLI_expr_pylike_parse("x*10 + y", names, ARRAY_SIZE(names));
300
301 EXPECT_TRUE(BLI_expr_pylike_is_valid(expr));
302
303 double result;
304 eExprPyLike_EvalStatus status = BLI_expr_pylike_eval(expr, values, 3, &result);
305
307 EXPECT_EQ(result, 32.0);
308
310}
311
312TEST(expr_pylike, UsingParam)
313{
314 const char *names[3] = {"x", "y", "z"};
315
316 ExprPyLike_Parsed *expr = BLI_expr_pylike_parse("x + z", names, ARRAY_SIZE(names));
317
318 EXPECT_TRUE(BLI_expr_pylike_is_using_param(expr, 0));
319 EXPECT_FALSE(BLI_expr_pylike_is_using_param(expr, 1));
320 EXPECT_TRUE(BLI_expr_pylike_is_using_param(expr, 2));
321
323}
324
325#define TEST_ERROR(name, str, x, code) \
326 TEST(expr_pylike, Error_##name) \
327 { \
328 expr_pylike_error_test(str, x, code); \
329 }
330
331TEST_ERROR(DivZero1, "0 / 0", 0.0, EXPR_PYLIKE_MATH_ERROR)
332TEST_ERROR(DivZero2, "1 / 0", 0.0, EXPR_PYLIKE_DIV_BY_ZERO)
333TEST_ERROR(DivZero3, "1 / x", 0.0, EXPR_PYLIKE_DIV_BY_ZERO)
334TEST_ERROR(DivZero4, "1 / x", 1.0, EXPR_PYLIKE_SUCCESS)
335
336TEST_ERROR(SqrtDomain1, "sqrt(-1)", 0.0, EXPR_PYLIKE_MATH_ERROR)
337TEST_ERROR(SqrtDomain2, "sqrt(x)", -1.0, EXPR_PYLIKE_MATH_ERROR)
338TEST_ERROR(SqrtDomain3, "sqrt(x)", 0.0, EXPR_PYLIKE_SUCCESS)
339
340TEST_ERROR(PowDomain1, "pow(-1, 0.5)", 0.0, EXPR_PYLIKE_MATH_ERROR)
341TEST_ERROR(PowDomain2, "pow(-1, x)", 0.5, EXPR_PYLIKE_MATH_ERROR)
342TEST_ERROR(PowDomain3, "pow(-1, x)", 2.0, EXPR_PYLIKE_SUCCESS)
343
344TEST_ERROR(Mixed1, "sqrt(x) + 1 / max(0, x)", -1.0, EXPR_PYLIKE_MATH_ERROR)
345TEST_ERROR(Mixed2, "sqrt(x) + 1 / max(0, x)", 0.0, EXPR_PYLIKE_DIV_BY_ZERO)
346TEST_ERROR(Mixed3, "sqrt(x) + 1 / max(0, x)", 1.0, EXPR_PYLIKE_SUCCESS)
347
348TEST(expr_pylike, Error_Invalid)
349{
350 ExprPyLike_Parsed *expr = BLI_expr_pylike_parse("", nullptr, 0);
351 double result;
352
354
356}
357
358TEST(expr_pylike, Error_ArgumentCount)
359{
360 ExprPyLike_Parsed *expr = parse_for_eval("x", false);
361 double result;
362
363 EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_FATAL_ERROR);
364
366}
eExprPyLike_EvalStatus
@ EXPR_PYLIKE_FATAL_ERROR
@ EXPR_PYLIKE_SUCCESS
@ EXPR_PYLIKE_DIV_BY_ZERO
@ EXPR_PYLIKE_MATH_ERROR
@ EXPR_PYLIKE_INVALID
bool BLI_expr_pylike_is_constant(const struct ExprPyLike_Parsed *expr)
eExprPyLike_EvalStatus BLI_expr_pylike_eval(struct ExprPyLike_Parsed *expr, const double *param_values, int param_values_len, double *r_result)
bool BLI_expr_pylike_is_valid(const struct ExprPyLike_Parsed *expr)
ExprPyLike_Parsed * BLI_expr_pylike_parse(const char *expression, const char **param_names, int param_names_len)
bool BLI_expr_pylike_is_using_param(const struct ExprPyLike_Parsed *expr, int index)
#define TEST_RESULT(name, str, value)
EXPR_PYLIKE_DIV_BY_ZERO Error_Invalid
#define TEST_EVAL(name, str, x, value)
#define TEST_CONST(name, str, value)
#define TEST_PARSE_FAIL(name, str)
static void verify_eval_result(ExprPyLike_Parsed *expr, double x, double value)
static void expr_pylike_const_test(const char *str, double value, bool force_const)
#define TRUE_VAL
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
static void expr_pylike_eval_test(const char *str, double x, double value)
static ExprPyLike_Parsed * parse_for_eval(const char *str, bool nonconst)
static void expr_pylike_error_test(const char *str, double x, eExprPyLike_EvalStatus error)
#define FALSE_VAL
static void expr_pylike_parse_fail_test(const char *str)
BLI_expr_pylike_free(expr)
TEST(expr_pylike, Eval_Ternary1)
#define TEST_ERROR(name, str, x, code)
#define M_PI
#define ARRAY_SIZE(arr)
#define lerp(a, b, t)
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
ATTR_WARN_UNUSED_RESULT const BMVert * v
pow(value.r - subtrahend, 2.0)") .do_static_compilation(true)
#define str(s)
MINLINE float smoothstep(float edge0, float edge1, float x)
ccl_device_inline float2 fmod(const float2 a, const float b)
ccl_device_inline float3 log(float3 v)
static void error(const char *str)
#define min(a, b)
Definition sort.c:32