Debian Codegraph @

modules/lsp-runtime-lib/src/test/utest/expr/expression.cpp

raw
1 /*
2 * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2020 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 19 сент. 2019 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <lsp-plug.in/expr/Expression.h>
23 #include <lsp-plug.in/expr/Variables.h>
24 #include <lsp-plug.in/stdlib/math.h>
25 #include <lsp-plug.in/test-fw/helpers.h>
26 #include <lsp-plug.in/test-fw/utest.h>
27
28 #define GAIN_AMP_P_12_DB 3.98107 /* +12 dB */
29 #define GAIN_AMP_M_12_DB 0.25119 /* -12 dB */
30
31 namespace lsp
32 {
33 using namespace lsp::expr;
34 }
35
36 UTEST_BEGIN("runtime.expr", expression)
37
38 typedef struct context_t
39 {
40 test_type_t *pThis;
41 Variables *pVars;
42 } context_t;
43
44
45 void test_float(const char *expr, Resolver *r, double value, float tol = 0.001)
46 {
47 Expression e(r);
48 value_t res;
49 init_value(&res);
50
51 printf("Evaluating expression: %s -> %f\n", expr, value);
52 UTEST_ASSERT_MSG(e.parse(expr, NULL, Expression::FLAG_NONE) == STATUS_OK, "Error parsing expression: %s", expr);
53 UTEST_ASSERT(e.evaluate(&res) == STATUS_OK);
54 UTEST_ASSERT(res.type == VT_FLOAT);
55 UTEST_ASSERT_MSG(float_equals_relative(res.v_float, value, tol),
56 "%s: result (%f) != expected (%f)", expr, double(res.v_float), value);
57 destroy_value(&res);
58 }
59
60 void test_int(const char *expr, Resolver *r, ssize_t value)
61 {
62 Expression e(r);
63 value_t res;
64 init_value(&res);
65
66 printf("Evaluating expression: %s -> %ld\n", expr, long(value));
67 UTEST_ASSERT_MSG(e.parse(expr, NULL, Expression::FLAG_NONE) == STATUS_OK, "Error parsing expression: %s", expr);
68 UTEST_ASSERT(e.evaluate(&res) == STATUS_OK);
69 UTEST_ASSERT(res.type == VT_INT);
70 UTEST_ASSERT_MSG(res.v_int == value,
71 "%s: result (%ld) != expected (%ld)", expr, long(res.v_int), long(value));
72 destroy_value(&res);
73 }
74
75 void test_bool(const char *expr, Resolver *r, bool value)
76 {
77 Expression e(r);
78 value_t res;
79 init_value(&res);
80
81 printf("Evaluating expression: %s -> %s\n", expr, (value) ? "true" : "false");
82 UTEST_ASSERT_MSG(e.parse(expr, NULL, Expression::FLAG_NONE) == STATUS_OK, "Error parsing expression: %s", expr);
83 UTEST_ASSERT(e.evaluate(&res) == STATUS_OK);
84 UTEST_ASSERT(res.type == VT_BOOL);
85 UTEST_ASSERT_MSG(res.v_bool == value,
86 "%s: result (%s) != expected (%s)", expr, (res.v_bool) ? "true" : "false", (value) ? "true" : "false");
87 destroy_value(&res);
88 }
89
90 void test_string(const char *expr, Resolver *r, const char *value)
91 {
92 LSPString tmp;
93 Expression e(r);
94 value_t res;
95 init_value(&res);
96
97 printf("Evaluating expression: %s -> '%s'\n", expr, value);
98 UTEST_ASSERT(tmp.set_utf8(expr) == true);
99 UTEST_ASSERT_MSG(e.parse(&tmp, Expression::FLAG_NONE) == STATUS_OK, "Error parsing expression: %s", expr);
100 UTEST_ASSERT(e.evaluate(&res) == STATUS_OK);
101 UTEST_ASSERT(res.type == VT_STRING);
102 UTEST_ASSERT(tmp.set_utf8(value) == true);
103 UTEST_ASSERT_MSG(tmp.equals(res.v_str),
104 "%s: result ('%s') != expected ('%s')", expr, res.v_str->get_utf8(), tmp.get_utf8());
105 destroy_value(&res);
106 }
107
108 void test_substitution(const char *expr, Resolver *r, const char *value)
109 {
110 LSPString tmp;
111 Expression e(r);
112 value_t res;
113 init_value(&res);
114
115 printf("Evaluating expression: %s -> '%s'\n", expr, value);
116 UTEST_ASSERT(tmp.set_utf8(expr) == true);
117 UTEST_ASSERT_MSG(e.parse(&tmp, Expression::FLAG_STRING) == STATUS_OK, "Error parsing expression: %s", expr);
118 UTEST_ASSERT(e.evaluate(&res) == STATUS_OK);
119 UTEST_ASSERT(cast_string(&res) == STATUS_OK);
120 UTEST_ASSERT(res.type == VT_STRING);
121 UTEST_ASSERT(tmp.set_utf8(value) == true);
122 UTEST_ASSERT_MSG(tmp.equals(res.v_str),
123 "%s: result ('%s') != expected ('%s')", expr, res.v_str->get_utf8(), tmp.get_utf8());
124 destroy_value(&res);
125 }
126
127 void test_dependencies(Resolver *r)
128 {
129 LSPString tmp;
130 Expression e(r);
131
132 static const char *expr = "(:v[:fa][:ia-:fd]) && (:v[1][:bc] = 'test') || (:za + :zb == undef)";
133
134 printf("Testing dependencies for expression\n");
135 UTEST_ASSERT(tmp.set_utf8(expr) == true);
136 UTEST_ASSERT_MSG(e.parse(&tmp, Expression::FLAG_NONE) == STATUS_OK, "Error parsing expression: %s", expr);
137 UTEST_ASSERT(e.has_dependency("v"));
138 UTEST_ASSERT(e.has_dependency("fa"));
139 UTEST_ASSERT(e.has_dependency("ia"));
140 UTEST_ASSERT(e.has_dependency("fd"));
141 UTEST_ASSERT(e.has_dependency("bc"));
142 UTEST_ASSERT(e.has_dependency("za"));
143 UTEST_ASSERT(e.has_dependency("zb"));
144 UTEST_ASSERT(!e.has_dependency("zc"));
145 }
146
147 void init_vars(Variables &v)
148 {
149 UTEST_ASSERT(v.set_int("ia", 1) == STATUS_OK);
150 UTEST_ASSERT(v.set_int("ib", 3) == STATUS_OK);
151 UTEST_ASSERT(v.set_int("ic", 5) == STATUS_OK);
152 UTEST_ASSERT(v.set_int("id", 7) == STATUS_OK);
153 UTEST_ASSERT(v.set_int("ie", 10) == STATUS_OK);
154
155 UTEST_ASSERT(v.set_bool("ba", true) == STATUS_OK);
156 UTEST_ASSERT(v.set_bool("bb", false) == STATUS_OK);
157 UTEST_ASSERT(v.set_bool("bc", true) == STATUS_OK);
158 UTEST_ASSERT(v.set_bool("bd", false) == STATUS_OK);
159
160 UTEST_ASSERT(v.set_float("fa", 1) == STATUS_OK);
161 UTEST_ASSERT(v.set_float("fb", 0.3) == STATUS_OK);
162 UTEST_ASSERT(v.set_float("fc", 0.5) == STATUS_OK);
163 UTEST_ASSERT(v.set_float("fd", 0.7) == STATUS_OK);
164 UTEST_ASSERT(v.set_float("fe", 0.01) == STATUS_OK);
165 UTEST_ASSERT(v.set_float("fg", 14.1) == STATUS_OK);
166 UTEST_ASSERT(v.set_float("zoom1", GAIN_AMP_M_12_DB) == STATUS_OK);
167 UTEST_ASSERT(v.set_float("zoom2", GAIN_AMP_P_12_DB) == STATUS_OK);
168
169 UTEST_ASSERT(v.set_null("za") == STATUS_OK);
170 UTEST_ASSERT(v.set_null("zb") == STATUS_OK);
171
172 UTEST_ASSERT(v.set_int("v_0_0", 1234) == STATUS_OK);
173 UTEST_ASSERT(v.set_float("v_0_1", 1.234) == STATUS_OK);
174 UTEST_ASSERT(v.set_bool("v_1_0", true) == STATUS_OK);
175 UTEST_ASSERT(v.set_string("v_1_1", "test") == STATUS_OK);
176
177 LSPString lower, upper;
178 UTEST_ASSERT(lower.set_ascii("lower"));
179 UTEST_ASSERT(upper.set_ascii("UPPER"));
180
181 UTEST_ASSERT(v.set_string("sa", &lower) == STATUS_OK);
182 UTEST_ASSERT(v.set_string("sb", &upper) == STATUS_OK);
183
184 }
185
186 void test_invalid(const char *expr)
187 {
188 printf("Testing invalid expression: %s\n", expr);
189
190 Expression e;
191 UTEST_ASSERT(e.parse(expr, Expression::FLAG_NONE) != STATUS_OK);
192 }
193
194 static status_t func_hello(void *context, value_t *result, size_t num_args, const value_t *args)
195 {
196 context_t *ctx = static_cast<context_t *>(context);
197 test_type_t * const self = ctx->pThis;
198
199 UTEST_ASSERT_PTR(self, context != NULL);
200 UTEST_ASSERT_PTR(self, result != NULL);
201 UTEST_ASSERT_PTR(self, num_args == 4);
202 UTEST_ASSERT_PTR(self, args != NULL);
203
204 // Check arguments
205 UTEST_ASSERT_PTR(self, args[0].type == VT_FLOAT);
206 UTEST_ASSERT_MSG_PTR(self, float_equals_relative(args[0].v_float, 3.0f),
207 "Argument 0: result (%f) != expected (%f)", double(args[0].v_float), 3.0f);
208 UTEST_ASSERT_PTR(self, args[1].type == VT_FLOAT);
209 UTEST_ASSERT_MSG_PTR(self, float_equals_relative(args[1].v_float, 2.0f),
210 "Argument 1: result (%f) != expected (%f)", double(args[1].v_float), 2.0f);
211 UTEST_ASSERT_PTR(self, args[2].type == VT_UNDEF);
212 UTEST_ASSERT_PTR(self, args[3].type == VT_INT);
213 UTEST_ASSERT_MSG_PTR(self, args[3].v_int == -1,
214 "Argument 3: result (%d) != expected (%d)", int(args[3].v_int), int(-1));
215
216 UTEST_ASSERT_PTR(self, set_value_string(result, "Hello, ") == STATUS_OK);
217
218 return STATUS_OK;
219 }
220
221 static status_t func_world(void *context, value_t *result, size_t num_args, const value_t *args)
222 {
223 context_t *ctx = static_cast<context_t *>(context);
224 test_type_t * const self = ctx->pThis;
225
226 UTEST_ASSERT_PTR(self, context != NULL);
227 UTEST_ASSERT_PTR(self, result != NULL);
228 UTEST_ASSERT_PTR(self, num_args == 5);
229 UTEST_ASSERT_PTR(self, args != NULL);
230
231 // Check arguments
232 UTEST_ASSERT_PTR(self, args[0].type == VT_NULL);
233 UTEST_ASSERT_PTR(self, args[1].type == VT_FLOAT);
234 UTEST_ASSERT_MSG_PTR(self, float_equals_relative(args[1].v_float, 0.7f),
235 "Argument 1: result (%f) != expected (%f)", double(args[1].v_float), 0.7f);
236 UTEST_ASSERT_PTR(self, args[2].type == VT_BOOL);
237 UTEST_ASSERT_PTR(self, args[2].v_bool == true);
238 UTEST_ASSERT_PTR(self, args[3].type == VT_UNDEF);
239 UTEST_ASSERT_PTR(self, args[4].type == VT_STRING);
240 UTEST_ASSERT_PTR(self, args[4].v_str->equals_ascii("test"));
241
242 UTEST_ASSERT_PTR(self, set_value_string(result, "World!") == STATUS_OK);
243
244 return STATUS_OK;
245 }
246
247
248 void test_function_call(Resolver *r)
249 {
250 context_t ctx;
251 Variables v(r);
252 Expression e(&v);
253 LSPString tmp;
254
255 ctx.pThis = this;
256 ctx.pVars = &v;
257
258 // Inject additional functions
259 UTEST_ASSERT(v.bind_func("hello", func_hello, &ctx) == STATUS_OK);
260 UTEST_ASSERT(v.bind_func("world", func_world, &ctx) == STATUS_OK);
261
262 static const char *expr = "hello(log2(:ia + :id), :fa + :ia, :za - :zb, -1) scat world(null, :fd, :bc, undef, 'test')";
263 static const char *expected = "Hello, World!";
264
265 printf("Testing dependencies for expression\n");
266 UTEST_ASSERT(tmp.set_utf8(expr) == true);
267 UTEST_ASSERT_MSG(e.parse(&tmp, Expression::FLAG_NONE) == STATUS_OK, "Error parsing expression: %s", expr);
268 UTEST_ASSERT(e.has_dependency("ia"));
269 UTEST_ASSERT(e.has_dependency("id"));
270 UTEST_ASSERT(e.has_dependency("fa"));
271 UTEST_ASSERT(e.has_dependency("za"));
272 UTEST_ASSERT(e.has_dependency("zb"));
273 UTEST_ASSERT(e.has_dependency("fd"));
274 UTEST_ASSERT(e.has_dependency("bc"));
275 UTEST_ASSERT(!e.has_dependency("zc"));
276
277 // Evaluate expression
278 value_t res;
279 init_value(&res);
280
281 printf("Evaluating expression: %s -> '%s'\n", expr, expected);
282 UTEST_ASSERT(tmp.set_utf8(expr) == true);
283 UTEST_ASSERT_MSG(e.parse(&tmp, Expression::FLAG_NONE) == STATUS_OK, "Error parsing expression: %s", expr);
284 UTEST_ASSERT(e.evaluate(&res) == STATUS_OK);
285 UTEST_ASSERT(res.type == VT_STRING);
286 UTEST_ASSERT_MSG(res.v_str->equals_utf8(expected),
287 "%s: result ('%s') != expected ('%s')", expr, res.v_str->get_utf8(), expected);
288 destroy_value(&res);
289 }
290
291 void test_standard_functions(Resolver *r)
292 {
293 Expression e(r);
294 value_t res;
295 init_value(&res);
296 lsp_finally { destroy_value(&res); };
297
298 // Evaluate min() expression
299 static const char *min_expr = "min(5.0, 4, 10.0, 3, 0.1, 2, 1)";
300 printf("Evaluating min() expression: %s\n", min_expr);
301 UTEST_ASSERT_MSG(e.parse(min_expr, Expression::FLAG_NONE) == STATUS_OK, "Error parsing expression: %s", min_expr);
302 UTEST_ASSERT(e.evaluate(&res) == STATUS_OK);
303 UTEST_ASSERT(res.type == VT_FLOAT);
304 UTEST_ASSERT(float_equals_adaptive(res.v_float, 0.1));
305
306 // Evaluate min() expression
307 static const char *max_expr = "max(5.0, 4, 10.0, 3, 0.1, 2, 1)";
308 printf("Evaluating max() expression: %s\n", max_expr);
309 UTEST_ASSERT_MSG(e.parse(max_expr, Expression::FLAG_NONE) == STATUS_OK, "Error parsing expression: %s", max_expr);
310 UTEST_ASSERT(e.evaluate(&res) == STATUS_OK);
311 UTEST_ASSERT(res.type == VT_FLOAT);
312 UTEST_ASSERT(float_equals_adaptive(res.v_float, 10.0));
313
314 // Evaluate avg() expression
315 static const char *avg_expr = "avg(1, 2, 3, 4, 5, 6, 7)";
316 printf("Evaluating avg() expression: %s\n", avg_expr);
317 UTEST_ASSERT_MSG(e.parse(avg_expr, Expression::FLAG_NONE) == STATUS_OK, "Error parsing expression: %s", avg_expr);
318 UTEST_ASSERT(e.evaluate(&res) == STATUS_OK);
319 UTEST_ASSERT(res.type == VT_FLOAT);
320 UTEST_ASSERT(float_equals_adaptive(res.v_float, 4.0));
321
322 // Evaluate rms() expression
323 static const char *rms_expr = "rms(1, 2, 3, 4, 5, 6, 7)";
324 printf("Evaluating avg() expression: %s\n", rms_expr);
325 UTEST_ASSERT_MSG(e.parse(rms_expr, Expression::FLAG_NONE) == STATUS_OK, "Error parsing expression: %s", rms_expr);
326 UTEST_ASSERT(e.evaluate(&res) == STATUS_OK);
327 UTEST_ASSERT(res.type == VT_FLOAT);
328 UTEST_ASSERT(float_equals_adaptive(res.v_float, sqrtf(20.0)));
329 }
330
331 UTEST_MAIN
332 {
333 Variables v;
334 init_vars(v);
335
336 test_float("12 db", &v, GAIN_AMP_P_12_DB);
337 test_float("-12 db", &v, GAIN_AMP_M_12_DB);
338 test_float("db 12", &v, GAIN_AMP_P_12_DB);
339 test_float("db -12", &v, GAIN_AMP_M_12_DB);
340 test_float(":fa + :fb/:fc - :fe", &v, 1.59);
341 test_float(":ic ** :ib", &v, 125.0f);
342 test_float("fp (:ie + :id)", &v, 17.0);
343
344 test_float("sqrt 4 * 25", &v, 50.0);
345 test_float("sqrt (4 * 25)", &v, 10.0);
346 test_float("lg 100", &v, 2.0);
347 test_float("log2 256", &v, 8.0);
348 test_float("ln exp 11", &v, 11.0);
349 test_float("deg pi", &v, 180.0f);
350 test_float("rad deg pi", &v, M_PI);
351 test_float("sin(pi / 6)", &v, 0.5);
352 test_float("cos(pi / 3)", &v, 0.5);
353 test_float("tg(pi / 4)", &v, 1.0);
354 test_float("arctg 1", &v, M_PI / 4);
355 test_float("arcsin 0.5", &v, M_PI / 6);
356 test_float("arccos 0.5", &v, M_PI / 3);
357 test_float("abs -10.1 - abs 4", &v, 6.1);
358 test_float("2.0 ** 2.0 ** 4.0", &v, 256.0);
359
360 test_int("0b1011_0010", &v, 0xb2);
361 test_int("0o1_1", &v, 9);
362 test_int("0d12_34", &v, 1234);
363 test_int("0x12_34", &v, 0x1234);
364 test_int("+6 + -3 - --2", &v, 1);
365 test_int("(:ia+:ic) idiv :ib", &v, 2);
366 test_int(":ia * :ic + :id idiv :ib", &v, 7);
367 test_int(":ie bxor 0x3", &v, 9);
368 test_int(":ie bor :ic", &v, 15);
369 test_int(":ie band 0xC", &v, 8);
370 test_int("~:ia band 0xf", &v, 0x0e);
371 test_int(":ia icmp null", &v, 1);
372 test_int("null cmp :ia", &v, -1);
373 test_int("null <=> undef", &v, 1);
374 test_int("undef <=> :za", &v, -1);
375 test_int("undef <=> :za", &v, -1);
376 test_int("int :ba + int :fg", &v, 15);
377 test_int("abs -10 - abs 4", &v, 6);
378 test_int("10 - 3 - 2", &v, 5);
379 test_int("40/10/2", &v, 2);
380 test_int("0x3f band 0xdf band 0xfe", &v, 0x1e);
381
382 test_bool(":zoom1 le -9 db", &v, true);
383 test_bool(":zoom2 le -9 db", &v, false);
384 test_bool(":ia*:ib < :fc / :fe", &v, true);
385 test_bool(":ia*:ic > :fa / :fb", &v, true);
386 test_bool(":bb || :ba && :bd ^^ :bc", &v, true);
387 test_bool("(:bb || :bd) || !(:ba eq :bc)", &v, false);
388 test_bool("-:ia + :ib - :ic ieq -(:ie - :id)", &v, true);
389 test_bool("'true' eq true", &v, true);
390 test_bool("'TRUE' ne true", &v, true);
391 test_bool("'TRUE' ieq true", &v, true);
392 test_bool("'0x100' ieq 0x100", &v, true);
393 test_bool("'0x100' != 0x100", &v, true);
394 test_bool("ex :ia", &v, true);
395 test_bool("ex :fz", &v, false);
396 test_bool(":za ieq :zb", &v, true);
397 test_bool(":ia >= :za", &v, true);
398 test_bool(":ia + :za == undef", &v, true);
399 test_bool("(:v[0][0] ieq 1234) and (:v[bb][ia] = 1.234)", &v, true);
400 test_bool("(:v[:fa][:ia-:fd]) && (:v[1][:bc] = 'test')", &v, true);
401 test_bool("bool :fb", &v, false);
402
403 for (size_t i=0, j=0; i<40; i += 10, ++j)
404 {
405 UTEST_ASSERT(v.set_int("x", i) == STATUS_OK);
406 test_int(":x < 20 ? :x < 10 ? 0 : 1 : :x < 30 ? 2 : 3", &v, j);
407 }
408
409 test_int("slen 'abcdef'", &v, 6);
410 test_string("'ABC'", &v, "ABC");
411 test_string("'1' sc 20+:ib sc :ic*9", &v, "12345");
412 test_string("'xy' sr :id", &v, "xyxyxyxyxyxyxy");
413 test_string("lc :sa sc uc :sb", &v, "lowerUPPER");
414 test_string("uc :sa sc lc :sb", &v, "LOWERupper");
415 test_string("srev :sa sc srev :sb", &v, "rewolREPPU");
416 test_string("'null: ' sc :za sc ', undef: ' sc :zx", &v, "null: null, undef: undef");
417 test_string("str :bc", &v, "true");
418
419 test_substitution("some bare string", &v, "some bare string");
420 test_substitution("${ia}", &v, "1");
421 test_substitution("Value is: ${ia}", &v, "Value is: 1");
422 test_substitution("Value is: ${:ba}", &v, "Value is: true");
423 test_substitution("$${ia}", &v, "${ia}");
424 test_substitution("${ia}+${:ie}-${:ic}=${:ia+:ie-:ic}", &v, "1+10-5=6");
425
426 test_dependencies(&v);
427 test_function_call(&v);
428 test_standard_functions(&v);
429
430 test_invalid("(:a ge 0 db) : -1 : 1");
431 }
432
433 UTEST_END;
434
435