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