/[svn]/linuxsampler/trunk/src/scriptvm/tests/NKSPCoreLangTest.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/scriptvm/tests/NKSPCoreLangTest.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3816 - (show annotations) (download)
Fri Aug 28 17:28:48 2020 UTC (3 months ago) by schoenebeck
File size: 232408 byte(s)
* NKSP parser: Fixed crash if unary '-' operator was used on a non-number
  data type.

* NKSP parser: Raise parser error if either unary '-' or '+' operator was
  used with a non-number data type.

* Tests: Added test cases for the fixed issues described above.

* Bumped version (2.1.1.svn63).

1 /*
2 * Copyright (c) 2019 - 2020 Christian Schoenebeck
3 *
4 * http://www.linuxsampler.org
5 *
6 * This file is part of LinuxSampler and released under the same terms.
7 * See README file for details.
8 */
9
10 // This file contains automated test cases against the NKSP real-time
11 // instrument script engine.
12
13 #include "../../common/global.h"
14 #include "../../common/optional.h"
15 #include "../../common/RTMath.h"
16 #include "../ScriptVM.h"
17 #include "../common.h"
18 #include <sstream>
19 #include <stdlib.h>
20 #include <time.h>
21 #include <assert.h>
22 #include <functional>
23
24 #ifndef TEST_ASSERT
25 # define TEST_ASSERT assert
26 #endif
27
28 #define TEST_VERIFY(...) \
29 if (!(__VA_ARGS__)) { \
30 fprintf(stderr, "\n[ERROR]: The following NKSP test has failed:\n%s\n", \
31 opt.code.c_str()); \
32 fprintf(stderr, "Violated expectation for this failure was: " #__VA_ARGS__ "\n\n"); \
33 fprintf(stderr, "The overall expectations for this failed NKSP test were:\n\n"); \
34 printExpectations(opt); \
35 fprintf(stderr, "\n"); \
36 } \
37 TEST_ASSERT(__VA_ARGS__); \
38
39 #define TEST_PRINT_BOOL_EXPECTATION(e) \
40 printf(#e ": %s\n", (opt.e) ? "Yes" : "No"); \
41
42 #define TEST_PRINT_BOOL_EXPECTATION_OR(e,alt) \
43 printf(#e ": %s", (opt.e || opt.alt) ? "Yes" : "No"); \
44 if (!opt.e && opt.alt) { \
45 printf(" (implied by " #alt ")"); \
46 } \
47 printf("\n"); \
48
49 #define TEST_PRINT_BOOL_EXPECTATION_OR2(e,alt1,alt2) \
50 printf(#e ": %s", (opt.e || opt.alt1 || opt.alt2) ? "Yes" : "No"); \
51 if (!opt.e) { \
52 if (opt.alt1 && opt.alt2) { \
53 printf(" (implied by " #alt1 " and " #alt2 ")"); \
54 } else if (opt.alt1) { \
55 printf(" (implied by " #alt1 ")"); \
56 } else if (opt.alt2) { \
57 printf(" (implied by " #alt2 ")"); \
58 } \
59 } \
60 printf("\n"); \
61
62 #define TEST_PRINT_OPTIONAL_EXPECTATION(e) \
63 printf(#e ": %s\n", \
64 (opt.e) ? std::to_string(*opt.e).c_str() : "Not expected"); \
65
66 #define TEST_PRINT_OPTIONAL_STRING_EXPECTATION(e) \
67 printf(#e ": %s\n", \
68 (opt.e) ? ("'" + *opt.e + "'").c_str() : "Not expected"); \
69
70 #define TEST_PRINT_VECTOR_EXPECTATION(e) \
71 if (opt.e.empty()) { \
72 printf(#e ": Not expected\n"); \
73 } else { \
74 for (size_t i = 0; i < opt.e.size(); ++i) { \
75 printf(#e ": %s\n", std::to_string(opt.e[i]).c_str()); \
76 if (i < opt.e.size() - 1) printf(", "); \
77 } \
78 } \
79
80 using namespace LinuxSampler;
81 using namespace std;
82
83 struct RunScriptOpt {
84 String code;
85 bool expectParseError;
86 bool expectParseWarning;
87 bool expectRuntimeError;
88 bool expectNoExitResult;
89 bool expectExitResultIsInt;
90 bool expectExitResultIsReal;
91 bool prohibitExitFunctionArguments;
92 optional<vmint> expectIntExitResult;
93 optional<bool> expectBoolExitResult;
94 optional<vmfloat> expectRealExitResult;
95 optional<String> expectStringExitResult;
96 vector<MetricPrefix_t> expectExitResultUnitPrefix;
97 optional<StdUnit_t> expectExitResultUnit;
98 optional<bool> expectExitResultFinal;
99 };
100
101 static void printExpectations(RunScriptOpt opt) {
102 TEST_PRINT_BOOL_EXPECTATION(expectParseError);
103 TEST_PRINT_BOOL_EXPECTATION(expectParseWarning);
104 TEST_PRINT_BOOL_EXPECTATION(expectRuntimeError);
105 TEST_PRINT_BOOL_EXPECTATION(expectNoExitResult);
106 TEST_PRINT_BOOL_EXPECTATION_OR2(expectExitResultIsInt, /* Or: */ expectIntExitResult, expectBoolExitResult);
107 TEST_PRINT_BOOL_EXPECTATION_OR(expectExitResultIsReal, /* Or: */ expectRealExitResult);
108 TEST_PRINT_BOOL_EXPECTATION(prohibitExitFunctionArguments);
109 TEST_PRINT_OPTIONAL_EXPECTATION(expectIntExitResult);
110 TEST_PRINT_OPTIONAL_EXPECTATION(expectBoolExitResult);
111 TEST_PRINT_OPTIONAL_EXPECTATION(expectRealExitResult);
112 TEST_PRINT_OPTIONAL_STRING_EXPECTATION(expectStringExitResult);
113 TEST_PRINT_VECTOR_EXPECTATION(expectExitResultUnitPrefix);
114 TEST_PRINT_OPTIONAL_EXPECTATION(expectExitResultUnit);
115 TEST_PRINT_OPTIONAL_EXPECTATION(expectExitResultFinal);
116 }
117
118 static void runScript(const RunScriptOpt& opt) {
119 ScriptVM vm;
120 vm.setAutoSuspendEnabled(false);
121 if (!opt.prohibitExitFunctionArguments)
122 vm.setExitResultEnabled(true);
123 unique_ptr<VMParserContext> parserCtx(
124 vm.loadScript(opt.code)
125 );
126 vector<ParserIssue> errors = parserCtx->errors();
127 vector<ParserIssue> warnings = parserCtx->warnings();
128 if (opt.expectParseError) {
129 TEST_VERIFY(!errors.empty());
130 return;
131 } else {
132 for (ParserIssue& err : errors) {
133 err.dump();
134 }
135 TEST_VERIFY(errors.empty());
136 }
137 if (opt.expectParseWarning) {
138 TEST_VERIFY(!warnings.empty());
139 } else {
140 for (ParserIssue& wrn : warnings) {
141 wrn.dump();
142 }
143 }
144 TEST_VERIFY(parserCtx->eventHandler(0));
145 unique_ptr<VMExecContext> execCtx(
146 vm.createExecContext(&*parserCtx)
147 );
148 for (int i = 0; parserCtx->eventHandler(i); ++i) {
149 VMEventHandler* handler = parserCtx->eventHandler(i);
150 VMExecStatus_t result = vm.exec(&*parserCtx, &*execCtx, handler);
151 if (opt.expectRuntimeError) {
152 TEST_VERIFY(result & VM_EXEC_ERROR);
153 } else {
154 TEST_VERIFY(!(result & VM_EXEC_ERROR));
155 }
156 if (opt.expectNoExitResult) {
157 VMExpr* resExpr = execCtx->exitResult();
158 TEST_VERIFY(!resExpr);
159 }
160 if (opt.expectExitResultIsInt) {
161 VMExpr* resExpr = execCtx->exitResult();
162 TEST_VERIFY(resExpr);
163 TEST_VERIFY(resExpr->exprType() == INT_EXPR);
164 }
165 if (opt.expectExitResultIsReal) {
166 VMExpr* resExpr = execCtx->exitResult();
167 TEST_VERIFY(resExpr);
168 TEST_VERIFY(resExpr->exprType() == REAL_EXPR);
169 }
170 if (opt.expectIntExitResult) {
171 VMExpr* resExpr = execCtx->exitResult();
172 TEST_VERIFY(resExpr);
173 TEST_VERIFY(resExpr->exprType() == INT_EXPR);
174 TEST_VERIFY(resExpr->asInt()->evalInt() == *opt.expectIntExitResult);
175 }
176 if (opt.expectRealExitResult) {
177 VMExpr* resExpr = execCtx->exitResult();
178 TEST_VERIFY(resExpr);
179 TEST_VERIFY(resExpr->exprType() == REAL_EXPR);
180 if (sizeof(vmfloat) == sizeof(float)) {
181 TEST_VERIFY(RTMath::fEqual32(resExpr->asReal()->evalReal(), *opt.expectRealExitResult));
182 } else {
183 TEST_VERIFY(RTMath::fEqual64(resExpr->asReal()->evalReal(), *opt.expectRealExitResult));
184 }
185 }
186 if (opt.expectBoolExitResult) {
187 VMExpr* resExpr = execCtx->exitResult();
188 TEST_VERIFY(resExpr);
189 TEST_VERIFY(resExpr->exprType() == INT_EXPR);
190 TEST_VERIFY(bool(resExpr->asInt()->evalInt()) == *opt.expectBoolExitResult);
191 }
192 if (opt.expectStringExitResult) {
193 VMExpr* resExpr = execCtx->exitResult();
194 TEST_VERIFY(resExpr);
195 TEST_VERIFY(resExpr->exprType() == STRING_EXPR);
196 TEST_VERIFY(resExpr->asString()->evalStr() == *opt.expectStringExitResult);
197 }
198 if (opt.expectExitResultUnit) {
199 VMExpr* resExpr = execCtx->exitResult();
200 TEST_VERIFY(resExpr);
201 VMNumberExpr* numberExpr = resExpr->asNumber();
202 TEST_VERIFY(numberExpr);
203 TEST_VERIFY(numberExpr->unitType() == *opt.expectExitResultUnit);
204 }
205 if (!opt.expectExitResultUnitPrefix.empty()) {
206 VMExpr* resExpr = execCtx->exitResult();
207 TEST_VERIFY(resExpr);
208 VMNumberExpr* numberExpr = resExpr->asNumber();
209 TEST_VERIFY(numberExpr);
210 auto prefixes = opt.expectExitResultUnitPrefix;
211 if (*prefixes.rbegin() != VM_NO_PREFIX)
212 prefixes.push_back(VM_NO_PREFIX); // VM_NO_PREFIX termination required by unitFactr() call
213 vmfloat expectedFactor = VMUnit::unitFactor(&prefixes[0]);
214 vmfloat actualFactor = numberExpr->unitFactor();
215 if (sizeof(vmfloat) == sizeof(float)) {
216 TEST_VERIFY(RTMath::fEqual32(expectedFactor, actualFactor));
217 } else {
218 TEST_VERIFY(RTMath::fEqual64(expectedFactor, actualFactor));
219 }
220 }
221 if (opt.expectExitResultFinal) {
222 VMExpr* resExpr = execCtx->exitResult();
223 TEST_VERIFY(resExpr);
224 VMNumberExpr* numberExpr = resExpr->asNumber();
225 TEST_VERIFY(numberExpr);
226 TEST_VERIFY(numberExpr->isFinal() == *opt.expectExitResultFinal);
227 }
228 }
229 }
230
231 static void testBuiltInExitFunction() {
232 #if !SILENT_TEST
233 std::cout << "UNIT TEST: built-in exit() function\n";
234 #endif
235
236 runScript({
237 .code = R"NKSP_CODE(
238 on init
239 end on
240 )NKSP_CODE",
241 .expectNoExitResult = true
242 });
243
244 runScript({
245 .code = R"NKSP_CODE(
246 on init
247 exit
248 end on
249 )NKSP_CODE",
250 .expectNoExitResult = true
251 });
252
253 runScript({
254 .code = R"NKSP_CODE(
255 on init
256 exit()
257 end on
258 )NKSP_CODE",
259 .expectNoExitResult = true
260 });
261
262 // integer tests ...
263
264 runScript({
265 .code = R"NKSP_CODE(
266 on init
267 exit(42)
268 end on
269 )NKSP_CODE",
270 .expectIntExitResult = 42
271 });
272
273 runScript({
274 .code = R"NKSP_CODE(
275 on init
276 declare $foo := 1
277 if ($foo)
278 exit(21)
279 end if
280 end on
281 )NKSP_CODE",
282 .expectIntExitResult = 21
283 });
284
285 runScript({
286 .code = R"NKSP_CODE(
287 on init
288 declare $foo := 0
289 if ($foo)
290 exit(21)
291 end if
292 exit(99)
293 end on
294 )NKSP_CODE",
295 .expectIntExitResult = 99
296 });
297
298 // string tests ...
299
300 runScript({
301 .code = R"NKSP_CODE(
302 on init
303 exit("fourtytwo!")
304 end on
305 )NKSP_CODE",
306 .expectStringExitResult = "fourtytwo!"
307 });
308
309 // in production environment we prohibit the built-in exit() function to
310 // accept any arguments
311 runScript({
312 .code = R"NKSP_CODE(
313 on init
314 exit(42)
315 end on
316 )NKSP_CODE",
317 .expectParseError = true, // see comment above why
318 .prohibitExitFunctionArguments = true // simulate production environment
319 });
320
321 // real number tests ...
322
323 runScript({
324 .code = R"NKSP_CODE(
325 on init
326 exit(3.14)
327 end on
328 )NKSP_CODE",
329 .expectRealExitResult = 3.14
330 });
331
332 runScript({
333 .code = R"NKSP_CODE(
334 on init
335 declare $foo := 1
336 if ($foo)
337 exit(3.14)
338 end if
339 end on
340 )NKSP_CODE",
341 .expectRealExitResult = 3.14
342 });
343
344 runScript({
345 .code = R"NKSP_CODE(
346 on init
347 declare $foo := 0
348 if ($foo)
349 exit(3.14)
350 end if
351 exit(6.9)
352 end on
353 )NKSP_CODE",
354 .expectRealExitResult = 6.9
355 });
356
357 // int array tests ...
358
359 runScript({
360 .code = R"NKSP_CODE(
361 on init
362 declare %foo[3]
363 %foo[0] := 21
364 exit(%foo[0])
365 end on
366 )NKSP_CODE",
367 .expectIntExitResult = 21
368 });
369
370 runScript({
371 .code = R"NKSP_CODE(
372 on init
373 declare %foo[3] := ( 12, 23, 34 )
374 exit(%foo[0])
375 end on
376 )NKSP_CODE",
377 .expectIntExitResult = 12
378 });
379
380 runScript({
381 .code = R"NKSP_CODE(
382 on init
383 declare %foo[3] := ( 12, 23, 34 )
384 exit(%foo[1])
385 end on
386 )NKSP_CODE",
387 .expectIntExitResult = 23
388 });
389
390 runScript({
391 .code = R"NKSP_CODE(
392 on init
393 declare %foo[3] := ( 12, 23, 34 )
394 exit(%foo[2])
395 end on
396 )NKSP_CODE",
397 .expectIntExitResult = 34
398 });
399
400 runScript({ // make sure array is entirely initialized with zeroes
401 .code = R"NKSP_CODE(
402 on init
403 declare $i
404 declare $result
405 declare %foo[100]
406 while ($i < 100)
407 $result := $result .or. %foo[$i]
408 inc($i)
409 end while
410 exit($result)
411 end on
412 )NKSP_CODE",
413 .expectIntExitResult = 0
414 });
415
416 // real array tests ...
417
418 runScript({
419 .code = R"NKSP_CODE(
420 on init
421 declare ?foo[3]
422 ?foo[0] := 34.9
423 exit(?foo[0])
424 end on
425 )NKSP_CODE",
426 .expectRealExitResult = 34.9
427 });
428
429 runScript({
430 .code = R"NKSP_CODE(
431 on init
432 declare ?foo[3] := ( 0.3, 23.5, 900.1 )
433 exit(?foo[0])
434 end on
435 )NKSP_CODE",
436 .expectRealExitResult = 0.3
437 });
438
439 runScript({
440 .code = R"NKSP_CODE(
441 on init
442 declare ?foo[3] := ( 0.3, 23.5, 900.1 )
443 exit(?foo[1])
444 end on
445 )NKSP_CODE",
446 .expectRealExitResult = 23.5
447 });
448
449 runScript({
450 .code = R"NKSP_CODE(
451 on init
452 declare ?foo[3] := ( 0.3, 23.5, 900.1 )
453 exit(?foo[2])
454 end on
455 )NKSP_CODE",
456 .expectRealExitResult = 900.1
457 });
458
459 runScript({ // make sure array is entirely initialized with zeroes
460 .code = R"NKSP_CODE(
461 on init
462 declare $i
463 declare ?foo[100]
464 while ($i < 100)
465 if (?foo[$i] # 0.0)
466 exit(-1) { test failed }
467 end if
468 inc($i)
469 end while
470 exit(0) { test succeeded }
471 end on
472 )NKSP_CODE",
473 .expectIntExitResult = 0
474 });
475
476 // std unit tests ...
477
478 runScript({
479 .code = R"NKSP_CODE(
480 on init
481 exit(42s)
482 end on
483 )NKSP_CODE",
484 .expectIntExitResult = 42,
485 .expectExitResultUnit = VM_SECOND
486 });
487
488 runScript({
489 .code = R"NKSP_CODE(
490 on init
491 exit(42Hz)
492 end on
493 )NKSP_CODE",
494 .expectIntExitResult = 42,
495 .expectExitResultUnit = VM_HERTZ
496 });
497
498 runScript({
499 .code = R"NKSP_CODE(
500 on init
501 exit(42B)
502 end on
503 )NKSP_CODE",
504 .expectIntExitResult = 42,
505 .expectExitResultUnit = VM_BEL
506 });
507
508 runScript({
509 .code = R"NKSP_CODE(
510 on init
511 exit(42us)
512 end on
513 )NKSP_CODE",
514 .expectIntExitResult = 42,
515 .expectExitResultUnitPrefix = { VM_MICRO },
516 .expectExitResultUnit = VM_SECOND
517 });
518
519 runScript({
520 .code = R"NKSP_CODE(
521 on init
522 exit(42ms)
523 end on
524 )NKSP_CODE",
525 .expectIntExitResult = 42,
526 .expectExitResultUnitPrefix = { VM_MILLI },
527 .expectExitResultUnit = VM_SECOND
528 });
529
530 runScript({
531 .code = R"NKSP_CODE(
532 on init
533 exit(42cs)
534 end on
535 )NKSP_CODE",
536 .expectIntExitResult = 42,
537 .expectExitResultUnitPrefix = { VM_CENTI },
538 .expectExitResultUnit = VM_SECOND
539 });
540
541 runScript({
542 .code = R"NKSP_CODE(
543 on init
544 exit(42ds)
545 end on
546 )NKSP_CODE",
547 .expectIntExitResult = 42,
548 .expectExitResultUnitPrefix = { VM_DECI },
549 .expectExitResultUnit = VM_SECOND
550 });
551
552 runScript({
553 .code = R"NKSP_CODE(
554 on init
555 exit(42das)
556 end on
557 )NKSP_CODE",
558 .expectIntExitResult = 42,
559 .expectExitResultUnitPrefix = { VM_DECA },
560 .expectExitResultUnit = VM_SECOND
561 });
562
563 runScript({
564 .code = R"NKSP_CODE(
565 on init
566 exit(42hs)
567 end on
568 )NKSP_CODE",
569 .expectIntExitResult = 42,
570 .expectExitResultUnitPrefix = { VM_HECTO },
571 .expectExitResultUnit = VM_SECOND
572 });
573
574 runScript({
575 .code = R"NKSP_CODE(
576 on init
577 exit(42ks)
578 end on
579 )NKSP_CODE",
580 .expectIntExitResult = 42,
581 .expectExitResultUnitPrefix = { VM_KILO },
582 .expectExitResultUnit = VM_SECOND
583 });
584
585 runScript({
586 .code = R"NKSP_CODE(
587 on init
588 exit(42s)
589 end on
590 )NKSP_CODE",
591 .expectIntExitResult = 42,
592 .expectExitResultUnitPrefix = { VM_NO_PREFIX },
593 .expectExitResultUnit = VM_SECOND
594 });
595
596 runScript({
597 .code = R"NKSP_CODE(
598 on init
599 exit(42uHz)
600 end on
601 )NKSP_CODE",
602 .expectIntExitResult = 42,
603 .expectExitResultUnitPrefix = { VM_MICRO },
604 .expectExitResultUnit = VM_HERTZ
605 });
606
607 runScript({
608 .code = R"NKSP_CODE(
609 on init
610 exit(42mHz)
611 end on
612 )NKSP_CODE",
613 .expectIntExitResult = 42,
614 .expectExitResultUnitPrefix = { VM_MILLI },
615 .expectExitResultUnit = VM_HERTZ
616 });
617
618 runScript({
619 .code = R"NKSP_CODE(
620 on init
621 exit(42cHz)
622 end on
623 )NKSP_CODE",
624 .expectIntExitResult = 42,
625 .expectExitResultUnitPrefix = { VM_CENTI },
626 .expectExitResultUnit = VM_HERTZ
627 });
628
629 runScript({
630 .code = R"NKSP_CODE(
631 on init
632 exit(42dHz)
633 end on
634 )NKSP_CODE",
635 .expectIntExitResult = 42,
636 .expectExitResultUnitPrefix = { VM_DECI },
637 .expectExitResultUnit = VM_HERTZ
638 });
639
640 runScript({
641 .code = R"NKSP_CODE(
642 on init
643 exit(42daHz)
644 end on
645 )NKSP_CODE",
646 .expectIntExitResult = 42,
647 .expectExitResultUnitPrefix = { VM_DECA },
648 .expectExitResultUnit = VM_HERTZ
649 });
650
651 runScript({
652 .code = R"NKSP_CODE(
653 on init
654 exit(42hHz)
655 end on
656 )NKSP_CODE",
657 .expectIntExitResult = 42,
658 .expectExitResultUnitPrefix = { VM_HECTO },
659 .expectExitResultUnit = VM_HERTZ
660 });
661
662 runScript({
663 .code = R"NKSP_CODE(
664 on init
665 exit(42kHz)
666 end on
667 )NKSP_CODE",
668 .expectIntExitResult = 42,
669 .expectExitResultUnitPrefix = { VM_KILO },
670 .expectExitResultUnit = VM_HERTZ
671 });
672
673 runScript({
674 .code = R"NKSP_CODE(
675 on init
676 exit(42Hz)
677 end on
678 )NKSP_CODE",
679 .expectIntExitResult = 42,
680 .expectExitResultUnitPrefix = { VM_NO_PREFIX },
681 .expectExitResultUnit = VM_HERTZ
682 });
683
684 runScript({
685 .code = R"NKSP_CODE(
686 on init
687 exit(42uB)
688 end on
689 )NKSP_CODE",
690 .expectIntExitResult = 42,
691 .expectExitResultUnitPrefix = { VM_MICRO },
692 .expectExitResultUnit = VM_BEL
693 });
694
695 runScript({
696 .code = R"NKSP_CODE(
697 on init
698 exit(42mB)
699 end on
700 )NKSP_CODE",
701 .expectIntExitResult = 42,
702 .expectExitResultUnitPrefix = { VM_MILLI },
703 .expectExitResultUnit = VM_BEL
704 });
705
706 runScript({
707 .code = R"NKSP_CODE(
708 on init
709 exit(42cB)
710 end on
711 )NKSP_CODE",
712 .expectIntExitResult = 42,
713 .expectExitResultUnitPrefix = { VM_CENTI },
714 .expectExitResultUnit = VM_BEL
715 });
716
717 runScript({
718 .code = R"NKSP_CODE(
719 on init
720 exit(42dB)
721 end on
722 )NKSP_CODE",
723 .expectIntExitResult = 42,
724 .expectExitResultUnitPrefix = { VM_DECI },
725 .expectExitResultUnit = VM_BEL
726 });
727
728 runScript({
729 .code = R"NKSP_CODE(
730 on init
731 exit(42daB)
732 end on
733 )NKSP_CODE",
734 .expectIntExitResult = 42,
735 .expectExitResultUnitPrefix = { VM_DECA },
736 .expectExitResultUnit = VM_BEL
737 });
738
739 runScript({
740 .code = R"NKSP_CODE(
741 on init
742 exit(42hB)
743 end on
744 )NKSP_CODE",
745 .expectIntExitResult = 42,
746 .expectExitResultUnitPrefix = { VM_HECTO },
747 .expectExitResultUnit = VM_BEL
748 });
749
750 runScript({
751 .code = R"NKSP_CODE(
752 on init
753 exit(42kB)
754 end on
755 )NKSP_CODE",
756 .expectIntExitResult = 42,
757 .expectExitResultUnitPrefix = { VM_KILO },
758 .expectExitResultUnit = VM_BEL
759 });
760
761 runScript({
762 .code = R"NKSP_CODE(
763 on init
764 exit(42B)
765 end on
766 )NKSP_CODE",
767 .expectIntExitResult = 42,
768 .expectExitResultUnitPrefix = { VM_NO_PREFIX },
769 .expectExitResultUnit = VM_BEL
770 });
771
772 runScript({
773 .code = R"NKSP_CODE(
774 on init
775 exit(42udB)
776 end on
777 )NKSP_CODE",
778 .expectIntExitResult = 42,
779 .expectExitResultUnitPrefix = { VM_MICRO, VM_DECI },
780 .expectExitResultUnit = VM_BEL
781 });
782
783 runScript({
784 .code = R"NKSP_CODE(
785 on init
786 exit(42mdB)
787 end on
788 )NKSP_CODE",
789 .expectIntExitResult = 42,
790 .expectExitResultUnitPrefix = { VM_MILLI, VM_DECI },
791 .expectExitResultUnit = VM_BEL
792 });
793
794 runScript({
795 .code = R"NKSP_CODE(
796 on init
797 exit(42cdB)
798 end on
799 )NKSP_CODE",
800 .expectIntExitResult = 42,
801 .expectExitResultUnitPrefix = { VM_CENTI, VM_DECI },
802 .expectExitResultUnit = VM_BEL
803 });
804
805 runScript({
806 .code = R"NKSP_CODE(
807 on init
808 exit(42ddB)
809 end on
810 )NKSP_CODE",
811 .expectIntExitResult = 42,
812 .expectExitResultUnitPrefix = { VM_DECI, VM_DECI },
813 .expectExitResultUnit = VM_BEL
814 });
815
816 runScript({
817 .code = R"NKSP_CODE(
818 on init
819 exit(42dadB)
820 end on
821 )NKSP_CODE",
822 .expectIntExitResult = 42,
823 .expectExitResultUnitPrefix = { VM_DECA, VM_DECI },
824 .expectExitResultUnit = VM_BEL
825 });
826
827 runScript({
828 .code = R"NKSP_CODE(
829 on init
830 exit(42hdB)
831 end on
832 )NKSP_CODE",
833 .expectIntExitResult = 42,
834 .expectExitResultUnitPrefix = { VM_HECTO, VM_DECI },
835 .expectExitResultUnit = VM_BEL
836 });
837
838 runScript({
839 .code = R"NKSP_CODE(
840 on init
841 exit(42kdB)
842 end on
843 )NKSP_CODE",
844 .expectIntExitResult = 42,
845 .expectExitResultUnitPrefix = { VM_KILO, VM_DECI },
846 .expectExitResultUnit = VM_BEL
847 });
848
849 runScript({
850 .code = R"NKSP_CODE(
851 on init
852 declare $foo := 42mdB
853 exit($foo)
854 end on
855 )NKSP_CODE",
856 .expectIntExitResult = 42,
857 .expectExitResultUnitPrefix = { VM_MILLI, VM_DECI },
858 .expectExitResultUnit = VM_BEL
859 });
860
861 runScript({
862 .code = R"NKSP_CODE(
863 on init
864 exit(3.14s)
865 end on
866 )NKSP_CODE",
867 .expectRealExitResult = 3.14,
868 .expectExitResultUnitPrefix = { VM_NO_PREFIX },
869 .expectExitResultUnit = VM_SECOND
870 });
871
872 runScript({
873 .code = R"NKSP_CODE(
874 on init
875 exit(3.14us)
876 end on
877 )NKSP_CODE",
878 .expectRealExitResult = 3.14,
879 .expectExitResultUnitPrefix = { VM_MICRO },
880 .expectExitResultUnit = VM_SECOND
881 });
882
883 runScript({
884 .code = R"NKSP_CODE(
885 on init
886 exit(3.14ms)
887 end on
888 )NKSP_CODE",
889 .expectRealExitResult = 3.14,
890 .expectExitResultUnitPrefix = { VM_MILLI },
891 .expectExitResultUnit = VM_SECOND
892 });
893
894 runScript({
895 .code = R"NKSP_CODE(
896 on init
897 exit(-0.1B)
898 end on
899 )NKSP_CODE",
900 .expectRealExitResult = -0.1,
901 .expectExitResultUnitPrefix = { VM_NO_PREFIX },
902 .expectExitResultUnit = VM_BEL
903 });
904
905 runScript({
906 .code = R"NKSP_CODE(
907 on init
908 exit(-0.1dB)
909 end on
910 )NKSP_CODE",
911 .expectRealExitResult = -0.1,
912 .expectExitResultUnitPrefix = { VM_DECI },
913 .expectExitResultUnit = VM_BEL
914 });
915
916 runScript({
917 .code = R"NKSP_CODE(
918 on init
919 exit(-0.1mdB)
920 end on
921 )NKSP_CODE",
922 .expectRealExitResult = -0.1,
923 .expectExitResultUnitPrefix = { VM_MILLI, VM_DECI },
924 .expectExitResultUnit = VM_BEL
925 });
926
927 runScript({
928 .code = R"NKSP_CODE(
929 on init
930 declare ~foo := -0.1mdB
931 exit(~foo)
932 end on
933 )NKSP_CODE",
934 .expectRealExitResult = -0.1,
935 .expectExitResultUnitPrefix = { VM_MILLI, VM_DECI },
936 .expectExitResultUnit = VM_BEL
937 });
938
939 runScript({
940 .code = R"NKSP_CODE(
941 on init
942 declare ~foo := 0.0dB
943 ~foo := -0.1mdB
944 exit(~foo)
945 end on
946 )NKSP_CODE",
947 .expectRealExitResult = -0.1,
948 .expectExitResultUnitPrefix = { VM_MILLI, VM_DECI },
949 .expectExitResultUnit = VM_BEL
950 });
951
952 runScript({
953 .code = R"NKSP_CODE(
954 on init
955 declare ~foo := 0.0dB
956 ~foo := -0.1Hz
957 exit(~foo)
958 end on
959 )NKSP_CODE",
960 .expectParseError = true // assigning different unit type to a variable is not allowed
961 });
962
963 // 'final' ('!') operator tests ...
964
965 runScript({
966 .code = R"NKSP_CODE(
967 on init
968 exit(!42)
969 end on
970 )NKSP_CODE",
971 .expectIntExitResult = 42,
972 .expectExitResultFinal = true
973 });
974
975 runScript({
976 .code = R"NKSP_CODE(
977 on init
978 exit(42)
979 end on
980 )NKSP_CODE",
981 .expectIntExitResult = 42,
982 .expectExitResultFinal = false
983 });
984
985 runScript({
986 .code = R"NKSP_CODE(
987 on init
988 declare $foo := !42
989 exit($foo)
990 end on
991 )NKSP_CODE",
992 .expectIntExitResult = 42,
993 .expectExitResultFinal = true
994 });
995
996 runScript({
997 .code = R"NKSP_CODE(
998 on init
999 declare $foo := 42
1000 exit($foo)
1001 end on
1002 )NKSP_CODE",
1003 .expectIntExitResult = 42,
1004 .expectExitResultFinal = false
1005 });
1006
1007 runScript({
1008 .code = R"NKSP_CODE(
1009 on init
1010 declare ~foo := !3.14
1011 exit(~foo)
1012 end on
1013 )NKSP_CODE",
1014 .expectRealExitResult = 3.14,
1015 .expectExitResultFinal = true
1016 });
1017
1018 runScript({
1019 .code = R"NKSP_CODE(
1020 on init
1021 declare ~foo := 3.14
1022 exit(~foo)
1023 end on
1024 )NKSP_CODE",
1025 .expectRealExitResult = 3.14,
1026 .expectExitResultFinal = false
1027 });
1028
1029 runScript({
1030 .code = R"NKSP_CODE(
1031 on init
1032 declare ~foo := !3.14mdB
1033 exit(~foo)
1034 end on
1035 )NKSP_CODE",
1036 .expectRealExitResult = 3.14,
1037 .expectExitResultUnitPrefix = { VM_MILLI, VM_DECI },
1038 .expectExitResultUnit = VM_BEL,
1039 .expectExitResultFinal = true
1040 });
1041
1042 runScript({
1043 .code = R"NKSP_CODE(
1044 on init
1045 declare ~foo := !0.0mdB
1046 ~foo := !3.14mdB
1047 exit(~foo)
1048 end on
1049 )NKSP_CODE",
1050 .expectRealExitResult = 3.14,
1051 .expectExitResultUnitPrefix = { VM_MILLI, VM_DECI },
1052 .expectExitResultUnit = VM_BEL,
1053 .expectExitResultFinal = true
1054 });
1055
1056 runScript({
1057 .code = R"NKSP_CODE(
1058 on init
1059 declare ~foo := !0.0mdB
1060 ~foo := 3.14mdB
1061 exit(~foo)
1062 end on
1063 )NKSP_CODE",
1064 .expectParseError = true // assigning non-final to a final variable not allowed
1065 });
1066
1067 runScript({
1068 .code = R"NKSP_CODE(
1069 on init
1070 declare ~foo := 0.0mdB
1071 ~foo := !3.14mdB
1072 exit(~foo)
1073 end on
1074 )NKSP_CODE",
1075 .expectParseError = true // assigning final to a non-final variable not allowed
1076 });
1077
1078 // exit() acting as return statement ...
1079
1080 runScript({
1081 .code = R"NKSP_CODE(
1082 function doFoo
1083 exit(2) { just return from this user function, i.e. don't stop init handler }
1084 end function
1085
1086 on init
1087 call doFoo
1088 exit(3)
1089 end on
1090 )NKSP_CODE",
1091 .expectIntExitResult = 3
1092 });
1093
1094 runScript({
1095 .code = R"NKSP_CODE(
1096 function doFoo1
1097 exit(2) { just return from this user function, i.e. don't stop init handler }
1098 end function
1099
1100 function doFoo2
1101 call doFoo1
1102 exit(3) { just return from this user function, i.e. don't stop init handler }
1103 end function
1104
1105 on init
1106 call doFoo2
1107 exit(4)
1108 end on
1109 )NKSP_CODE",
1110 .expectIntExitResult = 4
1111 });
1112
1113 runScript({
1114 .code = R"NKSP_CODE(
1115 function doFoo
1116 exit(2) { just return from this user function, i.e. don't stop init handler }
1117 end function
1118
1119 on init
1120 call doFoo
1121 exit(3)
1122 { dead code ... }
1123 call doFoo
1124 exit(4)
1125 end on
1126 )NKSP_CODE",
1127 .expectIntExitResult = 3
1128 });
1129
1130 runScript({
1131 .code = R"NKSP_CODE(
1132 function doFoo
1133 exit(2) { just return from this user function, i.e. don't stop init handler }
1134 end function
1135
1136 on init
1137 call doFoo
1138 exit(3)
1139 { dead code ... }
1140 call doFoo
1141 end on
1142 )NKSP_CODE",
1143 .expectIntExitResult = 3
1144 });
1145
1146 #if !SILENT_TEST
1147 std::cout << std::endl;
1148 #endif
1149 }
1150
1151 static void testStringConcatOperator() {
1152 #if !SILENT_TEST
1153 std::cout << "UNIT TEST: string concatenation (&) operator\n";
1154 #endif
1155
1156 // strings only tests ...
1157
1158 runScript({
1159 .code = R"NKSP_CODE(
1160 on init
1161 declare @s := "foo" & " bar"
1162 exit(@s)
1163 end on
1164 )NKSP_CODE",
1165 .expectStringExitResult = "foo bar"
1166 });
1167
1168 // integer tests ...
1169
1170 runScript({
1171 .code = R"NKSP_CODE(
1172 on init
1173 declare @s := "foo" & " bar" & " " & 123
1174 exit(@s)
1175 end on
1176 )NKSP_CODE",
1177 .expectStringExitResult = "foo bar 123"
1178 });
1179
1180 runScript({
1181 .code = R"NKSP_CODE(
1182 on init
1183 declare $i := 123
1184 declare @s := "foo" & " bar" & " " & $i
1185 exit(@s)
1186 end on
1187 )NKSP_CODE",
1188 .expectStringExitResult = "foo bar 123"
1189 });
1190
1191 // real number tests ...
1192
1193 runScript({
1194 .code = R"NKSP_CODE(
1195 on init
1196 declare @s := "foo" & " bar" & " " & 1.23
1197 exit(@s)
1198 end on
1199 )NKSP_CODE",
1200 .expectStringExitResult = "foo bar 1.23"
1201 });
1202
1203 runScript({
1204 .code = R"NKSP_CODE(
1205 on init
1206 declare ~r := 3.14
1207 declare @s := "foo" & " bar" & " " & ~r
1208 exit(@s)
1209 end on
1210 )NKSP_CODE",
1211 .expectStringExitResult = "foo bar 3.14"
1212 });
1213
1214 // std unit tests ...
1215
1216 runScript({
1217 .code = R"NKSP_CODE(
1218 on init
1219 declare $i := 500Hz
1220 declare @s := "foo" & " bar" & " " & $i
1221 exit(@s)
1222 end on
1223 )NKSP_CODE",
1224 .expectStringExitResult = "foo bar 500Hz"
1225 });
1226
1227 runScript({
1228 .code = R"NKSP_CODE(
1229 on init
1230 declare ~r := 3.14s
1231 declare @s := "foo" & " bar" & " " & ~r
1232 exit(@s)
1233 end on
1234 )NKSP_CODE",
1235 .expectStringExitResult = "foo bar 3.14s"
1236 });
1237
1238 runScript({
1239 .code = R"NKSP_CODE(
1240 on init
1241 declare ~r := -22.3mdB
1242 declare @s := "foo" & " bar" & " " & ~r
1243 exit(@s)
1244 end on
1245 )NKSP_CODE",
1246 .expectStringExitResult = "foo bar -22.3mdB"
1247 });
1248
1249 runScript({
1250 .code = R"NKSP_CODE(
1251 on init
1252 declare $i := 20us
1253 declare @s := "foo" & " bar" & " " & $i
1254 exit(@s)
1255 end on
1256 )NKSP_CODE",
1257 .expectStringExitResult = "foo bar 20us"
1258 });
1259
1260 runScript({
1261 .code = R"NKSP_CODE(
1262 on init
1263 declare $i := 20kHz
1264 declare @s := "foo" & " bar" & " " & $i
1265 exit(@s)
1266 end on
1267 )NKSP_CODE",
1268 .expectStringExitResult = "foo bar 20kHz"
1269 });
1270
1271 runScript({
1272 .code = R"NKSP_CODE(
1273 on init
1274 declare $i := -6dB
1275 declare @s := "foo" & " bar" & " " & $i
1276 exit(@s)
1277 end on
1278 )NKSP_CODE",
1279 .expectStringExitResult = "foo bar -6dB"
1280 });
1281
1282 runScript({
1283 .code = R"NKSP_CODE(
1284 on init
1285 declare $i := 1us * 1d
1286 declare @s := "foo" & " bar" & " " & $i
1287 exit(@s)
1288 end on
1289 )NKSP_CODE",
1290 .expectStringExitResult = "foo bar 1*10^-7s"
1291 });
1292
1293 runScript({
1294 .code = R"NKSP_CODE(
1295 on init
1296 declare ~r := 12.4mc
1297 declare @s := "foo" & " bar" & " " & ~r
1298 exit(@s)
1299 end on
1300 )NKSP_CODE",
1301 .expectStringExitResult = "foo bar 12.4mc"
1302 });
1303
1304 #if !SILENT_TEST
1305 std::cout << std::endl;
1306 #endif
1307 }
1308
1309 static void testNegOperator() {
1310 #if !SILENT_TEST
1311 std::cout << "UNIT TEST: negate (-) operator\n";
1312 #endif
1313
1314 // integer tests ...
1315
1316 runScript({
1317 .code = R"NKSP_CODE(
1318 on init
1319 exit(-87)
1320 end on
1321 )NKSP_CODE",
1322 .expectIntExitResult = -87
1323 });
1324
1325 runScript({
1326 .code = R"NKSP_CODE(
1327 on init
1328 declare $foo := -87
1329 exit(-$foo)
1330 end on
1331 )NKSP_CODE",
1332 .expectIntExitResult = 87
1333 });
1334
1335 // real number tests ...
1336
1337 runScript({
1338 .code = R"NKSP_CODE(
1339 on init
1340 exit(-99.3)
1341 end on
1342 )NKSP_CODE",
1343 .expectRealExitResult = -99.3
1344 });
1345
1346 runScript({
1347 .code = R"NKSP_CODE(
1348 on init
1349 declare ~foo := -99.3
1350 exit(-~foo)
1351 end on
1352 )NKSP_CODE",
1353 .expectRealExitResult = 99.3
1354 });
1355
1356 // std unit tests
1357
1358 runScript({
1359 .code = R"NKSP_CODE(
1360 on init
1361 declare $foo := -87mdB
1362 exit(-$foo)
1363 end on
1364 )NKSP_CODE",
1365 .expectIntExitResult = 87,
1366 .expectExitResultUnitPrefix = { VM_MILLI, VM_DECI },
1367 .expectExitResultUnit = VM_BEL
1368 });
1369
1370 // 'final' ('!') operator tests ...
1371
1372 runScript({
1373 .code = R"NKSP_CODE(
1374 on init
1375 declare $foo := !-87
1376 exit(-$foo)
1377 end on
1378 )NKSP_CODE",
1379 .expectIntExitResult = 87,
1380 .expectExitResultFinal = true
1381 });
1382
1383 runScript({
1384 .code = R"NKSP_CODE(
1385 on init
1386 declare $foo := -87
1387 exit(-$foo)
1388 end on
1389 )NKSP_CODE",
1390 .expectIntExitResult = 87,
1391 .expectExitResultFinal = false
1392 });
1393
1394 // string tests ...
1395
1396 runScript({
1397 .code = R"NKSP_CODE(
1398 on init
1399 exit(-"text")
1400 end on
1401 )NKSP_CODE",
1402 .expectParseError = true // unary '-' operator requires number
1403 });
1404
1405 runScript({
1406 .code = R"NKSP_CODE(
1407 on init
1408 declare @s := "text"
1409 exit(-@s)
1410 end on
1411 )NKSP_CODE",
1412 .expectParseError = true // unary '-' operator requires number
1413 });
1414
1415 //TODO: the following are unary '+' operator tests which should be moved to their own function (lazy me).
1416
1417 runScript({
1418 .code = R"NKSP_CODE(
1419 on init
1420 exit(+54)
1421 end on
1422 )NKSP_CODE",
1423 .expectIntExitResult = 54
1424 });
1425
1426 runScript({
1427 .code = R"NKSP_CODE(
1428 on init
1429 declare $foo := +54
1430 exit( $foo )
1431 end on
1432 )NKSP_CODE",
1433 .expectIntExitResult = 54
1434 });
1435
1436 runScript({
1437 .code = R"NKSP_CODE(
1438 on init
1439 exit(+0.45)
1440 end on
1441 )NKSP_CODE",
1442 .expectRealExitResult = 0.45
1443 });
1444
1445 runScript({
1446 .code = R"NKSP_CODE(
1447 on init
1448 declare ~foo := +0.29
1449 exit( ~foo )
1450 end on
1451 )NKSP_CODE",
1452 .expectRealExitResult = 0.29
1453 });
1454
1455 // string tests ...
1456
1457 runScript({
1458 .code = R"NKSP_CODE(
1459 on init
1460 exit(+"text")
1461 end on
1462 )NKSP_CODE",
1463 .expectParseError = true // unary '+' operator requires number
1464 });
1465
1466 runScript({
1467 .code = R"NKSP_CODE(
1468 on init
1469 declare @s := "text"
1470 exit(+@s)
1471 end on
1472 )NKSP_CODE",
1473 .expectParseError = true // unary '+' operator requires number
1474 });
1475
1476 #if !SILENT_TEST
1477 std::cout << std::endl;
1478 #endif
1479 }
1480
1481 static void testPlusOperator() {
1482 #if !SILENT_TEST
1483 std::cout << "UNIT TEST: plus (+) operator\n";
1484 #endif
1485
1486 // integer tests ...
1487
1488 runScript({
1489 .code = R"NKSP_CODE(
1490 on init
1491 exit(4 + 3)
1492 end on
1493 )NKSP_CODE",
1494 .expectIntExitResult = 7
1495 });
1496
1497 runScript({
1498 .code = R"NKSP_CODE(
1499 on init
1500 exit(42 + 145)
1501 end on
1502 )NKSP_CODE",
1503 .expectIntExitResult = 187
1504 });
1505
1506 runScript({
1507 .code = R"NKSP_CODE(
1508 on init
1509 exit(-4 + 2)
1510 end on
1511 )NKSP_CODE",
1512 .expectIntExitResult = -2
1513 });
1514
1515 // real number tests ...
1516
1517 runScript({
1518 .code = R"NKSP_CODE(
1519 on init
1520 exit(4.0 + 3.0)
1521 end on
1522 )NKSP_CODE",
1523 .expectRealExitResult = 7.0
1524 });
1525
1526 runScript({
1527 .code = R"NKSP_CODE(
1528 on init
1529 exit(42.3 + 145.2)
1530 end on
1531 )NKSP_CODE",
1532 .expectRealExitResult = 187.5
1533 });
1534
1535 runScript({
1536 .code = R"NKSP_CODE(
1537 on init
1538 exit(-4.0 + 2.2)
1539 end on
1540 )NKSP_CODE",
1541 .expectRealExitResult = -1.8
1542 });
1543
1544 // std unit tests ...
1545
1546 runScript({
1547 .code = R"NKSP_CODE(
1548 on init
1549 exit(42ms + 145ms)
1550 end on
1551 )NKSP_CODE",
1552 .expectIntExitResult = 187,
1553 .expectExitResultUnitPrefix = { VM_MILLI },
1554 .expectExitResultUnit = VM_SECOND
1555 });
1556
1557 runScript({
1558 .code = R"NKSP_CODE(
1559 on init
1560 exit(1s + 145ms)
1561 end on
1562 )NKSP_CODE",
1563 .expectIntExitResult = 1145,
1564 .expectExitResultUnitPrefix = { VM_MILLI },
1565 .expectExitResultUnit = VM_SECOND
1566 });
1567
1568 runScript({
1569 .code = R"NKSP_CODE(
1570 on init
1571 exit(42ms + 145)
1572 end on
1573 )NKSP_CODE",
1574 .expectParseError = true // units must match for + operator
1575 });
1576
1577 runScript({
1578 .code = R"NKSP_CODE(
1579 on init
1580 exit(42 + 145ms)
1581 end on
1582 )NKSP_CODE",
1583 .expectParseError = true // units must match for + operator
1584 });
1585
1586 runScript({
1587 .code = R"NKSP_CODE(
1588 on init
1589 exit(42Hz + 145s)
1590 end on
1591 )NKSP_CODE",
1592 .expectParseError = true // units must match for + operator
1593 });
1594
1595 runScript({
1596 .code = R"NKSP_CODE(
1597 on init
1598 exit(42.1ms + 145.3ms)
1599 end on
1600 )NKSP_CODE",
1601 .expectRealExitResult = 187.4,
1602 .expectExitResultUnitPrefix = { VM_MILLI },
1603 .expectExitResultUnit = VM_SECOND
1604 });
1605
1606 runScript({
1607 .code = R"NKSP_CODE(
1608 on init
1609 exit(1.1s + 145.0ms)
1610 end on
1611 )NKSP_CODE",
1612 .expectRealExitResult = 1245.0,
1613 .expectExitResultUnitPrefix = { VM_MILLI },
1614 .expectExitResultUnit = VM_SECOND
1615 });
1616
1617 runScript({
1618 .code = R"NKSP_CODE(
1619 on init
1620 exit(42.1ms + 145.3)
1621 end on
1622 )NKSP_CODE",
1623 .expectParseError = true // units must match for + operator
1624 });
1625
1626 runScript({
1627 .code = R"NKSP_CODE(
1628 on init
1629 exit(42.0 + 145.0ms)
1630 end on
1631 )NKSP_CODE",
1632 .expectParseError = true // units must match for + operator
1633 });
1634
1635 runScript({
1636 .code = R"NKSP_CODE(
1637 on init
1638 exit(42.0Hz + 145.0s)
1639 end on
1640 )NKSP_CODE",
1641 .expectParseError = true // units must match for + operator
1642 });
1643
1644 // 'final' ('!') operator tests ...
1645
1646 runScript({
1647 .code = R"NKSP_CODE(
1648 on init
1649 exit(!4 + !3)
1650 end on
1651 )NKSP_CODE",
1652 .expectIntExitResult = 7,
1653 .expectExitResultFinal = true
1654 });
1655
1656 runScript({
1657 .code = R"NKSP_CODE(
1658 on init
1659 exit(4 + 3)
1660 end on
1661 )NKSP_CODE",
1662 .expectIntExitResult = 7,
1663 .expectExitResultFinal = false
1664 });
1665
1666 runScript({
1667 .code = R"NKSP_CODE(
1668 on init
1669 exit(!4.1 + !3.3)
1670 end on
1671 )NKSP_CODE",
1672 .expectRealExitResult = 7.4,
1673 .expectExitResultFinal = true
1674 });
1675
1676 runScript({
1677 .code = R"NKSP_CODE(
1678 on init
1679 exit(4.1 + 3.3)
1680 end on
1681 )NKSP_CODE",
1682 .expectRealExitResult = 7.4,
1683 .expectExitResultFinal = false
1684 });
1685
1686 #if !SILENT_TEST
1687 std::cout << std::endl;
1688 #endif
1689 }
1690
1691 static void testMinusOperator() {
1692 #if !SILENT_TEST
1693 std::cout << "UNIT TEST: minus (-) operator\n";
1694 #endif
1695
1696 // integer tests ...
1697
1698 runScript({
1699 .code = R"NKSP_CODE(
1700 on init
1701 exit(4 - 3)
1702 end on
1703 )NKSP_CODE",
1704 .expectIntExitResult = 1
1705 });
1706
1707 runScript({
1708 .code = R"NKSP_CODE(
1709 on init
1710 exit(139 - 74)
1711 end on
1712 )NKSP_CODE",
1713 .expectIntExitResult = 65
1714 });
1715
1716 runScript({
1717 .code = R"NKSP_CODE(
1718 on init
1719 exit(3 - 9)
1720 end on
1721 )NKSP_CODE",
1722 .expectIntExitResult = -6
1723 });
1724
1725 runScript({
1726 .code = R"NKSP_CODE(
1727 on init
1728 exit(-3 - 18)
1729 end on
1730 )NKSP_CODE",
1731 .expectIntExitResult = -21
1732 });
1733
1734 // real number tests ...
1735
1736 runScript({
1737 .code = R"NKSP_CODE(
1738 on init
1739 exit(4.0 - 0.2)
1740 end on
1741 )NKSP_CODE",
1742 .expectRealExitResult = 3.8
1743 });
1744
1745 runScript({
1746 .code = R"NKSP_CODE(
1747 on init
1748 exit(3.1 - 9.65)
1749 end on
1750 )NKSP_CODE",
1751 .expectRealExitResult = -6.55
1752 });
1753
1754 runScript({
1755 .code = R"NKSP_CODE(
1756 on init
1757 exit(-3.0 - 18.1)
1758 end on
1759 )NKSP_CODE",
1760 .expectRealExitResult = -21.1
1761 });
1762
1763 // std unit tests ...
1764
1765 runScript({
1766 .code = R"NKSP_CODE(
1767 on init
1768 exit(1000ms - 145ms)
1769 end on
1770 )NKSP_CODE",
1771 .expectIntExitResult = 855,
1772 .expectExitResultUnitPrefix = { VM_MILLI },
1773 .expectExitResultUnit = VM_SECOND
1774 });
1775
1776 runScript({
1777 .code = R"NKSP_CODE(
1778 on init
1779 exit(1s - 145ms)
1780 end on
1781 )NKSP_CODE",
1782 .expectIntExitResult = 855,
1783 .expectExitResultUnitPrefix = { VM_MILLI },
1784 .expectExitResultUnit = VM_SECOND
1785 });
1786
1787 runScript({
1788 .code = R"NKSP_CODE(
1789 on init
1790 exit(1s - 145)
1791 end on
1792 )NKSP_CODE",
1793 .expectParseError = true // units must match for - operator
1794 });
1795
1796 runScript({
1797 .code = R"NKSP_CODE(
1798 on init
1799 exit(1 - 145s)
1800 end on
1801 )NKSP_CODE",
1802 .expectParseError = true // units must match for - operator
1803 });
1804
1805 runScript({
1806 .code = R"NKSP_CODE(
1807 on init
1808 exit(1ms - 145mB)
1809 end on
1810 )NKSP_CODE",
1811 .expectParseError = true // units must match for - operator
1812 });
1813
1814 runScript({
1815 .code = R"NKSP_CODE(
1816 on init
1817 exit(1.0ms - 0.1ms)
1818 end on
1819 )NKSP_CODE",
1820 .expectRealExitResult = 0.9,
1821 .expectExitResultUnitPrefix = { VM_MILLI },
1822 .expectExitResultUnit = VM_SECOND
1823 });
1824
1825 runScript({
1826 .code = R"NKSP_CODE(
1827 on init
1828 exit(1.1s - 106.0ms)
1829 end on
1830 )NKSP_CODE",
1831 .expectRealExitResult = 994.0,
1832 .expectExitResultUnitPrefix = { VM_MILLI },
1833 .expectExitResultUnit = VM_SECOND
1834 });
1835
1836 runScript({
1837 .code = R"NKSP_CODE(
1838 on init
1839 exit(1100.0ms - 0.106s)
1840 end on
1841 )NKSP_CODE",
1842 .expectRealExitResult = 994.0,
1843 .expectExitResultUnitPrefix = { VM_MILLI },
1844 .expectExitResultUnit = VM_SECOND
1845 });
1846
1847 runScript({
1848 .code = R"NKSP_CODE(
1849 on init
1850 exit(1.0s - 145.0)
1851 end on
1852 )NKSP_CODE",
1853 .expectParseError = true // units must match for - operator
1854 });
1855
1856 runScript({
1857 .code = R"NKSP_CODE(
1858 on init
1859 exit(1.0 - 145.0s)
1860 end on
1861 )NKSP_CODE",
1862 .expectParseError = true // units must match for - operator
1863 });
1864
1865 runScript({
1866 .code = R"NKSP_CODE(
1867 on init
1868 exit(1.0ms - 145.0mB)
1869 end on
1870 )NKSP_CODE",
1871 .expectParseError = true // units must match for - operator
1872 });
1873
1874 // 'final' ('!') operator tests ...
1875
1876 runScript({
1877 .code = R"NKSP_CODE(
1878 on init
1879 exit(!5 - !3)
1880 end on
1881 )NKSP_CODE",
1882 .expectIntExitResult = 2,
1883 .expectExitResultFinal = true
1884 });
1885
1886 runScript({
1887 .code = R"NKSP_CODE(
1888 on init
1889 exit(5 - 3)
1890 end on
1891 )NKSP_CODE",
1892 .expectIntExitResult = 2,
1893 .expectExitResultFinal = false
1894 });
1895
1896 runScript({
1897 .code = R"NKSP_CODE(
1898 on init
1899 exit(!5.9 - !3.3)
1900 end on
1901 )NKSP_CODE",
1902 .expectRealExitResult = 2.6,
1903 .expectExitResultFinal = true
1904 });
1905
1906 runScript({
1907 .code = R"NKSP_CODE(
1908 on init
1909 exit(5.9 - 3.3)
1910 end on
1911 )NKSP_CODE",
1912 .expectRealExitResult = 2.6,
1913 .expectExitResultFinal = false
1914 });
1915
1916 #if !SILENT_TEST
1917 std::cout << std::endl;
1918 #endif
1919 }
1920
1921 static void testModuloOperator() {
1922 #if !SILENT_TEST
1923 std::cout << "UNIT TEST: modulo (mod) operator\n";
1924 #endif
1925
1926 // integer tests ...
1927
1928 runScript({
1929 .code = R"NKSP_CODE(
1930 on init
1931 exit(10 mod 8)
1932 end on
1933 )NKSP_CODE",
1934 .expectIntExitResult = 2
1935 });
1936
1937 runScript({
1938 .code = R"NKSP_CODE(
1939 on init
1940 declare $a := 10
1941 declare $b := 8
1942 exit($a mod $b)
1943 end on
1944 )NKSP_CODE",
1945 .expectIntExitResult = 2
1946 });
1947
1948 // real number tests ...
1949 // (mod operator prohibits real numbers ATM)
1950
1951 runScript({
1952 .code = R"NKSP_CODE(
1953 on init
1954 exit(10.0 mod 8.0)
1955 end on
1956 )NKSP_CODE",
1957 .expectParseError = true // mod operator prohibits real numbers ATM
1958 });
1959
1960 runScript({
1961 .code = R"NKSP_CODE(
1962 on init
1963 exit(10 mod 8.0)
1964 end on
1965 )NKSP_CODE",
1966 .expectParseError = true // mod operator prohibits real numbers ATM
1967 });
1968
1969 runScript({
1970 .code = R"NKSP_CODE(
1971 on init
1972 exit(10.0 mod 8)
1973 end on
1974 )NKSP_CODE",
1975 .expectParseError = true // mod operator prohibits real numbers ATM
1976 });
1977
1978 runScript({
1979 .code = R"NKSP_CODE(
1980 on init
1981 declare ~a := 10.0
1982 declare ~b := 8.0
1983 exit(~a mod ~b)
1984 end on
1985 )NKSP_CODE",
1986 .expectParseError = true // mod operator prohibits real numbers ATM
1987 });
1988
1989 // std unit tests ...
1990
1991 runScript({
1992 .code = R"NKSP_CODE(
1993 on init
1994 exit(10s mod 8)
1995 end on
1996 )NKSP_CODE",
1997 .expectParseError = true // mod operator prohibits std units ATM
1998 });
1999
2000 runScript({
2001 .code = R"NKSP_CODE(
2002 on init
2003 exit(10 mod 8s)
2004 end on
2005 )NKSP_CODE",
2006 .expectParseError = true // mod operator prohibits std units ATM
2007 });
2008
2009 runScript({
2010 .code = R"NKSP_CODE(
2011 on init
2012 exit(10s mod 8s)
2013 end on
2014 )NKSP_CODE",
2015 .expectParseError = true // mod operator prohibits std units ATM
2016 });
2017
2018 // 'final' ('!') operator tests ...
2019
2020 runScript({
2021 .code = R"NKSP_CODE(
2022 on init
2023 exit(!10 mod !8)
2024 end on
2025 )NKSP_CODE",
2026 .expectIntExitResult = 2,
2027 .expectExitResultFinal = true
2028 });
2029
2030 runScript({
2031 .code = R"NKSP_CODE(
2032 on init
2033 exit(10 mod 8)
2034 end on
2035 )NKSP_CODE",
2036 .expectIntExitResult = 2,
2037 .expectExitResultFinal = false
2038 });
2039
2040 #if !SILENT_TEST
2041 std::cout << std::endl;
2042 #endif
2043 }
2044
2045 static void testMultiplyOperator() {
2046 #if !SILENT_TEST
2047 std::cout << "UNIT TEST: multiply (*) operator\n";
2048 #endif
2049
2050 // integer tests ...
2051
2052 runScript({
2053 .code = R"NKSP_CODE(
2054 on init
2055 exit(10 * 8)
2056 end on
2057 )NKSP_CODE",
2058 .expectIntExitResult = 80
2059 });
2060
2061 runScript({
2062 .code = R"NKSP_CODE(
2063 on init
2064 exit(-3 * -4)
2065 end on
2066 )NKSP_CODE",
2067 .expectIntExitResult = 12
2068 });
2069
2070 runScript({
2071 .code = R"NKSP_CODE(
2072 on init
2073 exit(-52 * 63)
2074 end on
2075 )NKSP_CODE",
2076 .expectIntExitResult = -3276
2077 });
2078
2079 runScript({
2080 .code = R"NKSP_CODE(
2081 on init
2082 exit(123 * -59)
2083 end on
2084 )NKSP_CODE",
2085 .expectIntExitResult = -7257
2086 });
2087
2088 // real number tests ...
2089
2090 runScript({
2091 .code = R"NKSP_CODE(
2092 on init
2093 exit(10.2 * 8.4)
2094 end on
2095 )NKSP_CODE",
2096 .expectRealExitResult = 85.68
2097 });
2098
2099 runScript({
2100 .code = R"NKSP_CODE(
2101 on init
2102 exit(10.0 * -3.33)
2103 end on
2104 )NKSP_CODE",
2105 .expectRealExitResult = -33.3
2106 });
2107
2108 runScript({
2109 .code = R"NKSP_CODE(
2110 on init
2111 exit(-3.33 * 10.0)
2112 end on
2113 )NKSP_CODE",
2114 .expectRealExitResult = -33.3
2115 });
2116
2117 runScript({
2118 .code = R"NKSP_CODE(
2119 on init
2120 exit(-3.33 * -10.0)
2121 end on
2122 )NKSP_CODE",
2123 .expectRealExitResult = 33.3
2124 });
2125
2126 // mixed type tests ...
2127 // (mixed int * real forbidden ATM)
2128
2129 runScript({
2130 .code = R"NKSP_CODE(
2131 on init
2132 exit(2 * 3.0)
2133 end on
2134 )NKSP_CODE",
2135 .expectParseError = true // mixed int * real forbidden ATM
2136 });
2137
2138 runScript({
2139 .code = R"NKSP_CODE(
2140 on init
2141 exit(2.0 * 3)
2142 end on
2143 )NKSP_CODE",
2144 .expectParseError = true // mixed int * real forbidden ATM
2145 });
2146
2147 // std unit tests ...
2148
2149 runScript({
2150 .code = R"NKSP_CODE(
2151 on init
2152 exit(10ms * 8)
2153 end on
2154 )NKSP_CODE",
2155 .expectIntExitResult = 80,
2156 .expectExitResultUnitPrefix = { VM_MILLI },
2157 .expectExitResultUnit = VM_SECOND
2158 });
2159
2160 runScript({
2161 .code = R"NKSP_CODE(
2162 on init
2163 exit(10 * 8ms)
2164 end on
2165 )NKSP_CODE",
2166 .expectIntExitResult = 80,
2167 .expectExitResultUnitPrefix = { VM_MILLI },
2168 .expectExitResultUnit = VM_SECOND
2169 });
2170
2171 runScript({
2172 .code = R"NKSP_CODE(
2173 on init
2174 exit(10s * 8s)
2175 end on
2176 )NKSP_CODE",
2177 .expectParseError = true // units on both sides not allowed for * ATM
2178 });
2179
2180 runScript({
2181 .code = R"NKSP_CODE(
2182 on init
2183 exit(10cs * 8d)
2184 end on
2185 )NKSP_CODE",
2186 .expectIntExitResult = 80,
2187 .expectExitResultUnitPrefix = { VM_MILLI },
2188 .expectExitResultUnit = VM_SECOND
2189 });
2190
2191 runScript({
2192 .code = R"NKSP_CODE(
2193 on init
2194 exit(10m * 8ms)
2195 end on
2196 )NKSP_CODE",
2197 .expectIntExitResult = 80,
2198 .expectExitResultUnitPrefix = { VM_MICRO },
2199 .expectExitResultUnit = VM_SECOND
2200 });
2201
2202 runScript({
2203 .code = R"NKSP_CODE(
2204 on init
2205 exit(10ms * 8k)
2206 end on
2207 )NKSP_CODE",
2208 .expectIntExitResult = 80,
2209 .expectExitResultUnitPrefix = { VM_NO_PREFIX },
2210 .expectExitResultUnit = VM_SECOND
2211 });
2212
2213 runScript({
2214 .code = R"NKSP_CODE(
2215 on init
2216 exit(10.1ms * 8.0)
2217 end on
2218 )NKSP_CODE",
2219 .expectRealExitResult = 80.8,
2220 .expectExitResultUnitPrefix = { VM_MILLI },
2221 .expectExitResultUnit = VM_SECOND
2222 });
2223
2224 runScript({
2225 .code = R"NKSP_CODE(
2226 on init
2227 exit(10.1 * 8.0ms)
2228 end on
2229 )NKSP_CODE",
2230 .expectRealExitResult = 80.8,
2231 .expectExitResultUnitPrefix = { VM_MILLI },
2232 .expectExitResultUnit = VM_SECOND
2233 });
2234
2235 runScript({
2236 .code = R"NKSP_CODE(
2237 on init
2238 exit(10.0s * 8.0s)
2239 end on
2240 )NKSP_CODE",
2241 .expectParseError = true // units on both sides not allowed for * ATM
2242 });
2243
2244 runScript({
2245 .code = R"NKSP_CODE(
2246 on init
2247 exit(10.1ds * 8.0c)
2248 end on
2249 )NKSP_CODE",
2250 .expectRealExitResult = 80.8,
2251 .expectExitResultUnitPrefix = { VM_MILLI },
2252 .expectExitResultUnit = VM_SECOND
2253 });
2254
2255 runScript({
2256 .code = R"NKSP_CODE(
2257 on init
2258 exit(10.1m * 8.0ms)
2259 end on
2260 )NKSP_CODE",
2261 .expectRealExitResult = 80.8,
2262 .expectExitResultUnitPrefix = { VM_MICRO },
2263 .expectExitResultUnit = VM_SECOND
2264 });
2265
2266 runScript({
2267 .code = R"NKSP_CODE(
2268 on init
2269 exit(10.1m * 8.0ks)
2270 end on
2271 )NKSP_CODE",
2272 .expectRealExitResult = 80.8,
2273 .expectExitResultUnitPrefix = { VM_NO_PREFIX },
2274 .expectExitResultUnit = VM_SECOND
2275 });
2276
2277 runScript({
2278 .code = R"NKSP_CODE(
2279 on init
2280 declare ~foo := 1.0 { neutral }
2281 declare $bar := 7000ms
2282 exit(~foo * real($bar))
2283 end on
2284 )NKSP_CODE",
2285 .expectRealExitResult = 7000.0,
2286 .expectExitResultUnitPrefix = { VM_MILLI },
2287 .expectExitResultUnit = VM_SECOND
2288 });
2289
2290 runScript({
2291 .code = R"NKSP_CODE(
2292 on init
2293 declare $foo := 1 { neutral }
2294 declare $bar := 7000ms
2295 exit(real($foo) * real($bar))
2296 end on
2297 )NKSP_CODE",
2298 .expectRealExitResult = 7000.0,
2299 .expectExitResultUnitPrefix = { VM_MILLI },
2300 .expectExitResultUnit = VM_SECOND
2301 });
2302
2303 // 'final' ('!') operator tests ...
2304
2305 runScript({
2306 .code = R"NKSP_CODE(
2307 on init
2308 exit(!10 * !8)
2309 end on
2310 )NKSP_CODE",
2311 .expectIntExitResult = 80,
2312 .expectExitResultFinal = true
2313 });
2314
2315 runScript({
2316 .code = R"NKSP_CODE(
2317 on init
2318 exit(10 * 8)
2319 end on
2320 )NKSP_CODE",
2321 .expectIntExitResult = 80,
2322 .expectExitResultFinal = false
2323 });
2324
2325 runScript({
2326 .code = R"NKSP_CODE(
2327 on init
2328 exit(!10 * 8)
2329 end on
2330 )NKSP_CODE",
2331 .expectIntExitResult = 80,
2332 .expectExitResultFinal = true,
2333 .expectParseWarning = true // since final only on one side, result will be final though
2334 });
2335
2336 runScript({
2337 .code = R"NKSP_CODE(
2338 on init
2339 exit(10 * !8)
2340 end on
2341 )NKSP_CODE",
2342 .expectIntExitResult = 80,
2343 .expectExitResultFinal = true,
2344 .expectParseWarning = true // since final only on one side, result will be final though
2345 });
2346
2347 runScript({
2348 .code = R"NKSP_CODE(
2349 on init
2350 exit(!10.1 * !8.0)
2351 end on
2352 )NKSP_CODE",
2353 .expectRealExitResult = 80.8,
2354 .expectExitResultFinal = true
2355 });
2356
2357 runScript({
2358 .code = R"NKSP_CODE(
2359 on init
2360 exit(10.1 * 8.0)
2361 end on
2362 )NKSP_CODE",
2363 .expectRealExitResult = 80.8,
2364 .expectExitResultFinal = false
2365 });
2366
2367 runScript({
2368 .code = R"NKSP_CODE(
2369 on init
2370 exit(!10.1 * 8.0)
2371 end on
2372 )NKSP_CODE",
2373 .expectRealExitResult = 80.8,
2374 .expectExitResultFinal = true,
2375 .expectParseWarning = true // since final only on one side, result will be final though
2376 });
2377
2378 runScript({
2379 .code = R"NKSP_CODE(
2380 on init
2381 exit(10.1 * !8.0)
2382 end on
2383 )NKSP_CODE",
2384 .expectRealExitResult = 80.8,
2385 .expectExitResultFinal = true,
2386 .expectParseWarning = true // since final only on one side, result will be final though
2387 });
2388
2389 #if !SILENT_TEST
2390 std::cout << std::endl;
2391 #endif
2392 }
2393
2394 static void testDivideOperator() {
2395 #if !SILENT_TEST
2396 std::cout << "UNIT TEST: divide (/) operator\n";
2397 #endif
2398
2399 // integer tests ...
2400
2401 runScript({
2402 .code = R"NKSP_CODE(
2403 on init
2404 exit(9 / 3)
2405 end on
2406 )NKSP_CODE",
2407 .expectIntExitResult = 3
2408 });
2409
2410 runScript({
2411 .code = R"NKSP_CODE(
2412 on init
2413 exit(-27 / 3)
2414 end on
2415 )NKSP_CODE",
2416 .expectIntExitResult = -9
2417 });
2418
2419 runScript({
2420 .code = R"NKSP_CODE(
2421 on init
2422 exit(35 / -5)
2423 end on
2424 )NKSP_CODE",
2425 .expectIntExitResult = -7
2426 });
2427
2428 runScript({
2429 .code = R"NKSP_CODE(
2430 on init
2431 exit(39 / -5)
2432 end on
2433 )NKSP_CODE",
2434 .expectIntExitResult = -7
2435 });
2436
2437 // real number tests ...
2438
2439 runScript({
2440 .code = R"NKSP_CODE(
2441 on init
2442 exit(9.0 / 10.0)
2443 end on
2444 )NKSP_CODE",
2445 .expectRealExitResult = 0.9
2446 });
2447
2448 runScript({
2449 .code = R"NKSP_CODE(
2450 on init
2451 exit(-9.0 / 10.0)
2452 end on
2453 )NKSP_CODE",
2454 .expectRealExitResult = -0.9
2455 });
2456
2457 runScript({
2458 .code = R"NKSP_CODE(
2459 on init
2460 exit(9.0 / -10.0)
2461 end on
2462 )NKSP_CODE",
2463 .expectRealExitResult = -0.9
2464 });
2465
2466 runScript({
2467 .code = R"NKSP_CODE(
2468 on init
2469 exit(-9.0 / -10.0)
2470 end on
2471 )NKSP_CODE",
2472 .expectRealExitResult = 0.9
2473 });
2474
2475 // mixed type tests ...
2476 // (mixed int / real forbidden ATM)
2477
2478 runScript({
2479 .code = R"NKSP_CODE(
2480 on init
2481 exit(9 / 10.0)
2482 end on
2483 )NKSP_CODE",
2484 .expectParseError = true // mixed int / real forbidden ATM
2485 });
2486
2487 runScript({
2488 .code = R"NKSP_CODE(
2489 on init
2490 exit(9.0 / 10)
2491 end on
2492 )NKSP_CODE",
2493 .expectParseError = true // mixed int / real forbidden ATM
2494 });
2495
2496 // std unit tests ...
2497
2498 runScript({
2499 .code = R"NKSP_CODE(
2500 on init
2501 exit(-27us / 3)
2502 end on
2503 )NKSP_CODE",
2504 .expectIntExitResult = -9,
2505 .expectExitResultUnitPrefix = { VM_MICRO },
2506 .expectExitResultUnit = VM_SECOND
2507 });
2508
2509 runScript({
2510 .code = R"NKSP_CODE(
2511 on init
2512 exit(-27mdB / 3mdB)
2513 end on
2514 )NKSP_CODE",
2515 .expectIntExitResult = -9,
2516 .expectExitResultUnitPrefix = { VM_NO_PREFIX },
2517 .expectExitResultUnit = VM_NO_UNIT
2518 });
2519
2520 runScript({
2521 .code = R"NKSP_CODE(
2522 on init
2523 exit(-27s / 3m)
2524 end on
2525 )NKSP_CODE",
2526 .expectIntExitResult = -9,
2527 .expectExitResultUnitPrefix = { VM_KILO },
2528 .expectExitResultUnit = VM_SECOND
2529 });
2530
2531 runScript({
2532 .code = R"NKSP_CODE(
2533 on init
2534 exit(-27us / 3m)
2535 end on
2536 )NKSP_CODE",
2537 .expectIntExitResult = -9,
2538 .expectExitResultUnitPrefix = { VM_MILLI },
2539 .expectExitResultUnit = VM_SECOND
2540 });
2541
2542 runScript({
2543 .code = R"NKSP_CODE(
2544 on init
2545 exit(-27 / 3s)
2546 end on
2547 )NKSP_CODE",
2548 .expectParseError = true // illegal unit type arrangement for divisions
2549 });
2550
2551 runScript({
2552 .code = R"NKSP_CODE(
2553 on init
2554 exit(-27s / 3Hz)
2555 end on
2556 )NKSP_CODE",
2557 .expectParseError = true // unit types are not matching
2558 });
2559
2560 runScript({
2561 .code = R"NKSP_CODE(
2562 on init
2563 declare $foo := 1000000
2564 declare $bar := 7000ms
2565 exit(real($foo) / 1000000.0 * real($bar))
2566 end on
2567 )NKSP_CODE",
2568 .expectRealExitResult = 7000.0,
2569 .expectExitResultUnitPrefix = { VM_MILLI },
2570 .expectExitResultUnit = VM_SECOND
2571 });
2572
2573 // 'final' ('!') operator tests ...
2574
2575 runScript({
2576 .code = R"NKSP_CODE(
2577 on init
2578 exit(!-27 / !3)
2579 end on
2580 )NKSP_CODE",
2581 .expectIntExitResult = -9,
2582 .expectExitResultFinal = true
2583 });
2584
2585 runScript({
2586 .code = R"NKSP_CODE(
2587 on init
2588 exit(-27 / 3)
2589 end on
2590 )NKSP_CODE",
2591 .expectIntExitResult = -9,
2592 .expectExitResultFinal = false
2593 });
2594
2595 runScript({
2596 .code = R"NKSP_CODE(
2597 on init
2598 exit(!-27 / 3)
2599 end on
2600 )NKSP_CODE",
2601 .expectIntExitResult = -9,
2602 .expectExitResultFinal = true,
2603 .expectParseWarning = true // final only on one side, result will be final though
2604 });
2605
2606 runScript({
2607 .code = R"NKSP_CODE(
2608 on init
2609 exit(-27 / !3)
2610 end on
2611 )NKSP_CODE",
2612 .expectIntExitResult = -9,
2613 .expectExitResultFinal = true,
2614 .expectParseWarning = true // final only on one side, result will be final though
2615 });
2616
2617 #if !SILENT_TEST
2618 std::cout << std::endl;
2619 #endif
2620 }
2621
2622 static void testSmallerThanOperator() {
2623 #if !SILENT_TEST
2624 std::cout << "UNIT TEST: smaller than (<) operator\n";
2625 #endif
2626
2627 // integer tests ...
2628
2629 runScript({
2630 .code = R"NKSP_CODE(
2631 on init
2632 exit(3 < 4)
2633 end on
2634 )NKSP_CODE",
2635 .expectBoolExitResult = true
2636 });
2637
2638 runScript({
2639 .code = R"NKSP_CODE(
2640 on init
2641 exit(4 < 3)
2642 end on
2643 )NKSP_CODE",
2644 .expectBoolExitResult = false
2645 });
2646
2647 runScript({
2648 .code = R"NKSP_CODE(
2649 on init
2650 exit(-4 < 3)
2651 end on
2652 )NKSP_CODE",
2653 .expectBoolExitResult = true
2654 });
2655
2656 runScript({
2657 .code = R"NKSP_CODE(
2658 on init
2659 exit(3 < -4)
2660 end on
2661 )NKSP_CODE",
2662 .expectBoolExitResult = false
2663 });
2664
2665 runScript({
2666 .code = R"NKSP_CODE(
2667 on init
2668 exit(123 < -45)
2669 end on
2670 )NKSP_CODE",
2671 .expectBoolExitResult = false
2672 });
2673
2674 runScript({
2675 .code = R"NKSP_CODE(
2676 on init
2677 exit(-45 < 123)
2678 end on
2679 )NKSP_CODE",
2680 .expectBoolExitResult = true
2681 });
2682
2683 // real number tests ...
2684
2685 runScript({
2686 .code = R"NKSP_CODE(
2687 on init
2688 exit(3.0 < 4.0)
2689 end on
2690 )NKSP_CODE",
2691 .expectBoolExitResult = true
2692 });
2693
2694 runScript({
2695 .code = R"NKSP_CODE(
2696 on init
2697 exit(4.0 < 3.0)
2698 end on
2699 )NKSP_CODE",
2700 .expectBoolExitResult = false
2701 });
2702
2703 runScript({
2704 .code = R"NKSP_CODE(
2705 on init
2706 exit(1.2 < 1.23)
2707 end on
2708 )NKSP_CODE",
2709 .expectBoolExitResult = true
2710 });
2711
2712 runScript({
2713 .code = R"NKSP_CODE(
2714 on init
2715 exit(1.23 < 1.2)
2716 end on
2717 )NKSP_CODE",
2718 .expectBoolExitResult = false
2719 });
2720
2721 runScript({
2722 .code = R"NKSP_CODE(
2723 on init
2724 exit(-4.0 < 3.0)
2725 end on
2726 )NKSP_CODE",
2727 .expectBoolExitResult = true
2728 });
2729
2730 runScript({
2731 .code = R"NKSP_CODE(
2732 on init
2733 exit(3.0 < -4.0)
2734 end on
2735 )NKSP_CODE",
2736 .expectBoolExitResult = false
2737 });
2738
2739 runScript({
2740 .code = R"NKSP_CODE(
2741 on init
2742 exit(123.0 < -45.0)
2743 end on
2744 )NKSP_CODE",
2745 .expectBoolExitResult = false
2746 });
2747
2748 runScript({
2749 .code = R"NKSP_CODE(
2750 on init
2751 exit(-45.0 < 123.0)
2752 end on
2753 )NKSP_CODE",
2754 .expectBoolExitResult = true
2755 });
2756
2757 // mixed type tests ...
2758
2759 runScript({
2760 .code = R"NKSP_CODE(
2761 on init
2762 exit(9 < 9.1)
2763 end on
2764 )NKSP_CODE",
2765 .expectBoolExitResult = true
2766 });
2767
2768 runScript({
2769 .code = R"NKSP_CODE(
2770 on init
2771 exit(9.1 < 9)
2772 end on
2773 )NKSP_CODE",
2774 .expectBoolExitResult = false
2775 });
2776
2777 // std unit tests ...
2778
2779 runScript({
2780 .code = R"NKSP_CODE(
2781 on init
2782 exit(13ms < 14ms)
2783 end on
2784 )NKSP_CODE",
2785 .expectBoolExitResult = true
2786 });
2787
2788 runScript({
2789 .code = R"NKSP_CODE(
2790 on init
2791 exit(14ms < 13ms)
2792 end on
2793 )NKSP_CODE",
2794 .expectBoolExitResult = false
2795 });
2796
2797 runScript({
2798 .code = R"NKSP_CODE(
2799 on init
2800 exit(1s < 990ms)
2801 end on
2802 )NKSP_CODE",
2803 .expectBoolExitResult = false
2804 });
2805
2806 runScript({
2807 .code = R"NKSP_CODE(
2808 on init
2809 exit(990ms < 1s)
2810 end on
2811 )NKSP_CODE",
2812 .expectBoolExitResult = true
2813 });
2814
2815 runScript({
2816 .code = R"NKSP_CODE(
2817 on init
2818 exit(1000ms < 1s)
2819 end on
2820 )NKSP_CODE",
2821 .expectBoolExitResult = false
2822 });
2823
2824 runScript({
2825 .code = R"NKSP_CODE(
2826 on init
2827 exit(1s < 1000ms)
2828 end on
2829 )NKSP_CODE",
2830 .expectBoolExitResult = false
2831 });
2832
2833 runScript({
2834 .code = R"NKSP_CODE(
2835 on init
2836 exit(1s < 1)
2837 end on
2838 )NKSP_CODE",
2839 .expectParseError = true // units on both sides must match
2840 });
2841
2842 runScript({
2843 .code = R"NKSP_CODE(
2844 on init
2845 exit(1 < 1s)
2846 end on
2847 )NKSP_CODE",
2848 .expectParseError = true // units on both sides must match
2849 });
2850
2851 runScript({
2852 .code = R"NKSP_CODE(
2853 on init
2854 exit(1Hz < 1B)
2855 end on
2856 )NKSP_CODE",
2857 .expectParseError = true // units on both sides must match
2858 });
2859
2860 runScript({
2861 .code = R"NKSP_CODE(
2862 on init
2863 exit(13.0ms < 13.1ms)
2864 end on
2865 )NKSP_CODE",
2866 .expectBoolExitResult = true
2867 });
2868
2869 runScript({
2870 .code = R"NKSP_CODE(
2871 on init
2872 exit(13.1ms < 13.0ms)
2873 end on
2874 )NKSP_CODE",
2875 .expectBoolExitResult = false
2876 });
2877
2878 runScript({
2879 .code = R"NKSP_CODE(
2880 on init
2881 exit(0.9s < 600.0ms)
2882 end on
2883 )NKSP_CODE",
2884 .expectBoolExitResult = false
2885 });
2886
2887 runScript({
2888 .code = R"NKSP_CODE(
2889 on init
2890 exit(600.0ms < 0.9s)
2891 end on
2892 )NKSP_CODE",
2893 .expectBoolExitResult = true
2894 });
2895
2896 runScript({
2897 .code = R"NKSP_CODE(
2898 on init
2899 exit(5.1kHz < 5100.0Hz)
2900 end on
2901 )NKSP_CODE",
2902 .expectBoolExitResult = false
2903 });
2904
2905 runScript({
2906 .code = R"NKSP_CODE(
2907 on init
2908 exit(5100.0Hz < 5.1kHz)
2909 end on
2910 )NKSP_CODE",
2911 .expectBoolExitResult = false
2912 });
2913
2914 runScript({
2915 .code = R"NKSP_CODE(
2916 on init
2917 exit(1.0Hz < 1.1)
2918 end on
2919 )NKSP_CODE",
2920 .expectParseError = true // units on both sides must match
2921 });
2922
2923 runScript({
2924 .code = R"NKSP_CODE(
2925 on init
2926 exit(1.2 < 1.34mdB)
2927 end on
2928 )NKSP_CODE",
2929 .expectParseError = true // units on both sides must match
2930 });
2931
2932 runScript({
2933 .code = R"NKSP_CODE(
2934 on init
2935 exit(9.23us < 3.14kHz)
2936 end on
2937 )NKSP_CODE",
2938 .expectParseError = true // units on both sides must match
2939 });
2940
2941 // 'final' ('!') operator tests ...
2942 // (should always yield in false for relation operators)
2943
2944 runScript({
2945 .code = R"NKSP_CODE(
2946 on init
2947 exit(!-4 < !3)
2948 end on
2949 )NKSP_CODE",
2950 .expectBoolExitResult = true,
2951 .expectExitResultFinal = false
2952 });
2953
2954 runScript({
2955 .code = R"NKSP_CODE(
2956 on init
2957 exit(-4 < 3)
2958 end on
2959 )NKSP_CODE",
2960 .expectBoolExitResult = true,
2961 .expectExitResultFinal = false
2962 });
2963
2964 #if !SILENT_TEST
2965 std::cout << std::endl;
2966 #endif
2967 }
2968
2969 static void testGreaterThanOperator() {
2970 #if !SILENT_TEST
2971 std::cout << "UNIT TEST: greater than (>) operator\n";
2972 #endif
2973
2974 // integer tests ...
2975
2976 runScript({
2977 .code = R"NKSP_CODE(
2978 on init
2979 exit(3 > 4)
2980 end on
2981 )NKSP_CODE",
2982 .expectBoolExitResult = false
2983 });
2984
2985 runScript({
2986 .code = R"NKSP_CODE(
2987 on init
2988 exit(4 > 3)
2989 end on
2990 )NKSP_CODE",
2991 .expectBoolExitResult = true
2992 });
2993
2994 runScript({
2995 .code = R"NKSP_CODE(
2996 on init
2997 exit(-4 > 3)
2998 end on
2999 )NKSP_CODE",
3000 .expectBoolExitResult = false
3001 });
3002
3003 runScript({
3004 .code = R"NKSP_CODE(
3005 on init
3006 exit(3 > -4)
3007 end on
3008 )NKSP_CODE",
3009 .expectBoolExitResult = true
3010 });
3011
3012 runScript({
3013 .code = R"NKSP_CODE(
3014 on init
3015 exit(123 > -45)
3016 end on
3017 )NKSP_CODE",
3018 .expectBoolExitResult = true
3019 });
3020
3021 runScript({
3022 .code = R"NKSP_CODE(
3023 on init
3024 exit(-45 > 123)
3025 end on
3026 )NKSP_CODE",
3027 .expectBoolExitResult = false
3028 });
3029
3030 // real number tests ...
3031
3032 runScript({
3033 .code = R"NKSP_CODE(
3034 on init
3035 exit(3.0 > 4.0)
3036 end on
3037 )NKSP_CODE",
3038 .expectBoolExitResult = false
3039 });
3040
3041 runScript({
3042 .code = R"NKSP_CODE(
3043 on init
3044 exit(4.0 > 3.0)
3045 end on
3046 )NKSP_CODE",
3047 .expectBoolExitResult = true
3048 });
3049
3050 runScript({
3051 .code = R"NKSP_CODE(
3052 on init
3053 exit(1.2 > 1.23)
3054 end on
3055 )NKSP_CODE",
3056 .expectBoolExitResult = false
3057 });
3058
3059 runScript({
3060 .code = R"NKSP_CODE(
3061 on init
3062 exit(1.23 > 1.2)
3063 end on
3064 )NKSP_CODE",
3065 .expectBoolExitResult = true
3066 });
3067
3068 runScript({
3069 .code = R"NKSP_CODE(
3070 on init
3071 exit(-4.0 > 3.0)
3072 end on
3073 )NKSP_CODE",
3074 .expectBoolExitResult = false
3075 });
3076
3077 runScript({
3078 .code = R"NKSP_CODE(
3079 on init
3080 exit(3.0 > -4.0)
3081 end on
3082 )NKSP_CODE",
3083 .expectBoolExitResult = true
3084 });
3085
3086 runScript({
3087 .code = R"NKSP_CODE(
3088 on init
3089 exit(123.0 > -45.0)
3090 end on
3091 )NKSP_CODE",
3092 .expectBoolExitResult = true
3093 });
3094
3095 runScript({
3096 .code = R"NKSP_CODE(
3097 on init
3098 exit(-45.0 > 123.0)
3099 end on
3100 )NKSP_CODE",
3101 .expectBoolExitResult = false
3102 });
3103
3104 // mixed type tests ...
3105
3106 runScript({
3107 .code = R"NKSP_CODE(
3108 on init
3109 exit(9 > 9.1)
3110 end on
3111 )NKSP_CODE",
3112 .expectBoolExitResult = false
3113 });
3114
3115 runScript({
3116 .code = R"NKSP_CODE(
3117 on init
3118 exit(9.1 > 9)
3119 end on
3120 )NKSP_CODE",
3121 .expectBoolExitResult = true
3122 });
3123
3124 // std unit tests ...
3125
3126 runScript({
3127 .code = R"NKSP_CODE(
3128 on init
3129 exit(13ms > 14ms)
3130 end on
3131 )NKSP_CODE",
3132 .expectBoolExitResult = false
3133 });
3134
3135 runScript({
3136 .code = R"NKSP_CODE(
3137 on init
3138 exit(14ms > 13ms)
3139 end on
3140 )NKSP_CODE",
3141 .expectBoolExitResult = true
3142 });
3143
3144 runScript({
3145 .code = R"NKSP_CODE(
3146 on init
3147 exit(1s > 990ms)
3148 end on
3149 )NKSP_CODE",
3150 .expectBoolExitResult = true
3151 });
3152
3153 runScript({
3154 .code = R"NKSP_CODE(
3155 on init
3156 exit(990ms > 1s)
3157 end on
3158 )NKSP_CODE",
3159 .expectBoolExitResult = false
3160 });
3161
3162 runScript({
3163 .code = R"NKSP_CODE(
3164 on init
3165 exit(1000ms > 1s)
3166 end on
3167 )NKSP_CODE",
3168 .expectBoolExitResult = false
3169 });
3170
3171 runScript({
3172 .code = R"NKSP_CODE(
3173 on init
3174 exit(1s > 1000ms)
3175 end on
3176 )NKSP_CODE",
3177 .expectBoolExitResult = false
3178 });
3179
3180 runScript({
3181 .code = R"NKSP_CODE(
3182 on init
3183 exit(1s > 1)
3184 end on
3185 )NKSP_CODE",
3186 .expectParseError = true // units on both sides must match
3187 });
3188
3189 runScript({
3190 .code = R"NKSP_CODE(
3191 on init
3192 exit(1 > 1s)
3193 end on
3194 )NKSP_CODE",
3195 .expectParseError = true // units on both sides must match
3196 });
3197
3198 runScript({
3199 .code = R"NKSP_CODE(
3200 on init
3201 exit(1Hz > 1B)
3202 end on
3203 )NKSP_CODE",
3204 .expectParseError = true // units on both sides must match
3205 });
3206
3207 runScript({
3208 .code = R"NKSP_CODE(
3209 on init
3210 exit(13.0ms > 13.1ms)
3211 end on
3212 )NKSP_CODE",
3213 .expectBoolExitResult = false
3214 });
3215
3216 runScript({
3217 .code = R"NKSP_CODE(
3218 on init
3219 exit(13.1ms > 13.0ms)
3220 end on
3221 )NKSP_CODE",
3222 .expectBoolExitResult = true
3223 });
3224
3225 runScript({
3226 .code = R"NKSP_CODE(
3227 on init
3228 exit(0.9s > 600.0ms)
3229 end on
3230 )NKSP_CODE",
3231 .expectBoolExitResult = true
3232 });
3233
3234 runScript({
3235 .code = R"NKSP_CODE(
3236 on init
3237 exit(600.0ms > 0.9s)
3238 end on
3239 )NKSP_CODE",
3240 .expectBoolExitResult = false
3241 });
3242
3243 runScript({
3244 .code = R"NKSP_CODE(
3245 on init
3246 exit(5.1kHz > 5100.0Hz)
3247 end on
3248 )NKSP_CODE",
3249 .expectBoolExitResult = false
3250 });
3251
3252 runScript({
3253 .code = R"NKSP_CODE(
3254 on init
3255 exit(5100.0Hz > 5.1kHz)
3256 end on
3257 )NKSP_CODE",
3258 .expectBoolExitResult = false
3259 });
3260
3261 runScript({
3262 .code = R"NKSP_CODE(
3263 on init
3264 exit(1.0Hz > 1.1)
3265 end on
3266 )NKSP_CODE",
3267 .expectParseError = true // units on both sides must match
3268 });
3269
3270 runScript({
3271 .code = R"NKSP_CODE(
3272 on init
3273 exit(1.2 > 1.34mdB)
3274 end on
3275 )NKSP_CODE",
3276 .expectParseError = true // units on both sides must match
3277 });
3278
3279 runScript({
3280 .code = R"NKSP_CODE(
3281 on init
3282 exit(9.23us > 3.14kHz)
3283 end on
3284 )NKSP_CODE",
3285 .expectParseError = true // units on both sides must match
3286 });
3287
3288 // 'final' ('!') operator tests ...
3289 // (should always yield in false for relation operators)
3290
3291 runScript({
3292 .code = R"NKSP_CODE(
3293 on init
3294 exit(!-4 > !3)
3295 end on
3296 )NKSP_CODE",
3297 .expectBoolExitResult = false,
3298 .expectExitResultFinal = false
3299 });
3300
3301 runScript({
3302 .code = R"NKSP_CODE(
3303 on init
3304 exit(-4 > 3)
3305 end on
3306 )NKSP_CODE",
3307 .expectBoolExitResult = false,
3308 .expectExitResultFinal = false
3309 });
3310
3311 #if !SILENT_TEST
3312 std::cout << std::endl;
3313 #endif
3314 }
3315
3316 static void testSmallerOrEqualOperator() {
3317 #if !SILENT_TEST
3318 std::cout << "UNIT TEST: smaller-or-equal (<=) operator\n";
3319 #endif
3320
3321 // integer tests ...
3322
3323 runScript({
3324 .code = R"NKSP_CODE(
3325 on init
3326 exit(3 <= 3)
3327 end on
3328 )NKSP_CODE",
3329 .expectBoolExitResult = true
3330 });
3331
3332 runScript({
3333 .code = R"NKSP_CODE(
3334 on init
3335 exit(4 <= 4)
3336 end on
3337 )NKSP_CODE",
3338 .expectBoolExitResult = true
3339 });
3340
3341 runScript({
3342 .code = R"NKSP_CODE(
3343 on init
3344 exit(-23 <= -23)
3345 end on
3346 )NKSP_CODE",
3347 .expectBoolExitResult = true
3348 });
3349
3350 runScript({
3351 .code = R"NKSP_CODE(
3352 on init
3353 exit(23 <= -23)
3354 end on
3355 )NKSP_CODE",
3356 .expectBoolExitResult = false
3357 });
3358
3359 runScript({
3360 .code = R"NKSP_CODE(
3361 on init
3362 exit(3 <= 4)
3363 end on
3364 )NKSP_CODE",
3365 .expectBoolExitResult = true
3366 });
3367
3368 runScript({
3369 .code = R"NKSP_CODE(
3370 on init
3371 exit(4 <= 3)
3372 end on
3373 )NKSP_CODE",
3374 .expectBoolExitResult = false
3375 });
3376
3377 runScript({
3378 .code = R"NKSP_CODE(
3379 on init
3380 exit(-4 <= 3)
3381 end on
3382 )NKSP_CODE",
3383 .expectBoolExitResult = true
3384 });
3385
3386 runScript({
3387 .code = R"NKSP_CODE(
3388 on init
3389 exit(3 <= -4)
3390 end on
3391 )NKSP_CODE",
3392 .expectBoolExitResult = false
3393 });
3394
3395 runScript({
3396 .code = R"NKSP_CODE(
3397 on init
3398 exit(123 <= -45)
3399 end on
3400 )NKSP_CODE",
3401 .expectBoolExitResult = false
3402 });
3403
3404 runScript({
3405 .code = R"NKSP_CODE(
3406 on init
3407 exit(-45 <= 123)
3408 end on
3409 )NKSP_CODE",
3410 .expectBoolExitResult = true
3411 });
3412
3413 // real number tests ...
3414
3415 runScript({
3416 .code = R"NKSP_CODE(
3417 on init
3418 exit(3.0 <= 3.0)
3419 end on
3420 )NKSP_CODE",
3421 .expectBoolExitResult = true
3422 });
3423
3424 runScript({
3425 .code = R"NKSP_CODE(
3426 on init
3427 exit(4.33 <= 4.33)
3428 end on
3429 )NKSP_CODE",
3430 .expectBoolExitResult = true
3431 });
3432
3433 runScript({
3434 .code = R"NKSP_CODE(
3435 on init
3436 exit(-23.1 <= -23.1)
3437 end on
3438 )NKSP_CODE",
3439 .expectBoolExitResult = true
3440 });
3441
3442 runScript({
3443 .code = R"NKSP_CODE(
3444 on init
3445 exit(23.3 <= -23.3)
3446 end on
3447 )NKSP_CODE",
3448 .expectBoolExitResult = false
3449 });
3450
3451 runScript({
3452 .code = R"NKSP_CODE(
3453 on init
3454 exit(3.0 <= 4.0)
3455 end on
3456 )NKSP_CODE",
3457 .expectBoolExitResult = true
3458 });
3459
3460 runScript({
3461 .code = R"NKSP_CODE(
3462 on init
3463 exit(4.0 <= 3.0)
3464 end on
3465 )NKSP_CODE",
3466 .expectBoolExitResult = false
3467 });
3468
3469 runScript({
3470 .code = R"NKSP_CODE(
3471 on init
3472 exit(-4.0 <= 3.0)
3473 end on
3474 )NKSP_CODE",
3475 .expectBoolExitResult = true
3476 });
3477
3478 runScript({
3479 .code = R"NKSP_CODE(
3480 on init
3481 exit(3.0 <= -4.0)
3482 end on
3483 )NKSP_CODE",
3484 .expectBoolExitResult = false
3485 });
3486
3487 runScript({
3488 .code = R"NKSP_CODE(
3489 on init
3490 exit(123.0 <= -45.0)
3491 end on
3492 )NKSP_CODE",
3493 .expectBoolExitResult = false
3494 });
3495
3496 runScript({
3497 .code = R"NKSP_CODE(
3498 on init
3499 exit(-45.0 <= 123.0)
3500 end on
3501 )NKSP_CODE",
3502 .expectBoolExitResult = true
3503 });
3504
3505 // mixed type tests ...
3506
3507 runScript({
3508 .code = R"NKSP_CODE(
3509 on init
3510 exit(9 <= 9.1)
3511 end on
3512 )NKSP_CODE",
3513 .expectBoolExitResult = true
3514 });
3515
3516 runScript({
3517 .code = R"NKSP_CODE(
3518 on init
3519 exit(9.1 <= 9)
3520 end on
3521 )NKSP_CODE",
3522 .expectBoolExitResult = false
3523 });
3524
3525 runScript({
3526 .code = R"NKSP_CODE(
3527 on init
3528 exit(9 <= 9.0)
3529 end on
3530 )NKSP_CODE",
3531 .expectBoolExitResult = true
3532 });
3533
3534 runScript({
3535 .code = R"NKSP_CODE(
3536 on init
3537 exit(9.0 <= 9)
3538 end on
3539 )NKSP_CODE",
3540 .expectBoolExitResult = true
3541 });
3542
3543 // std unit tests ...
3544
3545 runScript({
3546 .code = R"NKSP_CODE(
3547 on init
3548 exit(13ms <= 14ms)
3549 end on
3550 )NKSP_CODE",
3551 .expectBoolExitResult = true
3552 });
3553
3554 runScript({
3555 .code = R"NKSP_CODE(
3556 on init
3557 exit(14ms <= 13ms)
3558 end on
3559 )NKSP_CODE",
3560 .expectBoolExitResult = false
3561 });
3562
3563 runScript({
3564 .code = R"NKSP_CODE(
3565 on init
3566 exit(1s <= 990ms)
3567 end on
3568 )NKSP_CODE",
3569 .expectBoolExitResult = false
3570 });
3571
3572 runScript({
3573 .code = R"NKSP_CODE(
3574 on init
3575 exit(990ms <= 1s)
3576 end on
3577 )NKSP_CODE",
3578 .expectBoolExitResult = true
3579 });
3580
3581 runScript({
3582 .code = R"NKSP_CODE(
3583 on init
3584 exit(1000ms <= 1s)
3585 end on
3586 )NKSP_CODE",
3587 .expectBoolExitResult = true
3588 });
3589
3590 runScript({
3591 .code = R"NKSP_CODE(
3592 on init
3593 exit(1s <= 1000ms)
3594 end on
3595 )NKSP_CODE",
3596 .expectBoolExitResult = true
3597 });
3598
3599 runScript({
3600 .code = R"NKSP_CODE(
3601 on init
3602 exit(1s <= 1)
3603 end on
3604 )NKSP_CODE",
3605 .expectParseError = true // units on both sides must match
3606 });
3607
3608 runScript({
3609 .code = R"NKSP_CODE(
3610 on init
3611 exit(1 <= 1s)
3612 end on
3613 )NKSP_CODE",
3614 .expectParseError = true // units on both sides must match
3615 });
3616
3617 runScript({
3618 .code = R"NKSP_CODE(
3619 on init
3620 exit(1Hz <= 1B)
3621 end on
3622 )NKSP_CODE",
3623 .expectParseError = true // units on both sides must match
3624 });
3625
3626 runScript({
3627 .code = R"NKSP_CODE(
3628 on init
3629 exit(13.0ms <= 13.1ms)
3630 end on
3631 )NKSP_CODE",
3632 .expectBoolExitResult = true
3633 });
3634
3635 runScript({
3636 .code = R"NKSP_CODE(
3637 on init
3638 exit(13.1ms <= 13.0ms)
3639 end on
3640 )NKSP_CODE",
3641 .expectBoolExitResult = false
3642 });
3643
3644 runScript({
3645 .code = R"NKSP_CODE(
3646 on init
3647 exit(0.9s <= 600.0ms)
3648 end on
3649 )NKSP_CODE",
3650 .expectBoolExitResult = false
3651 });
3652
3653 runScript({
3654 .code = R"NKSP_CODE(
3655 on init
3656 exit(600.0ms <= 0.9s)
3657 end on
3658 )NKSP_CODE",
3659 .expectBoolExitResult = true
3660 });
3661
3662 runScript({
3663 .code = R"NKSP_CODE(
3664 on init
3665 exit(5.1kHz <= 5100.0Hz)
3666 end on
3667 )NKSP_CODE",
3668 .expectBoolExitResult = true
3669 });
3670
3671 runScript({
3672 .code = R"NKSP_CODE(
3673 on init
3674 exit(5100.0Hz <= 5.1kHz)
3675 end on
3676 )NKSP_CODE",
3677 .expectBoolExitResult = true
3678 });
3679
3680 runScript({
3681 .code = R"NKSP_CODE(
3682 on init
3683 exit(1.0Hz <= 1.1)
3684 end on
3685 )NKSP_CODE",
3686 .expectParseError = true // units on both sides must match
3687 });
3688
3689 runScript({
3690 .code = R"NKSP_CODE(
3691 on init
3692 exit(1.2 <= 1.34mdB)
3693 end on
3694 )NKSP_CODE",
3695 .expectParseError = true // units on both sides must match
3696 });
3697
3698 runScript({
3699 .code = R"NKSP_CODE(
3700 on init
3701 exit(9.23us <= 3.14kHz)
3702 end on
3703 )NKSP_CODE",
3704 .expectParseError = true // units on both sides must match
3705 });
3706
3707 // 'final' ('!') operator tests ...
3708 // (should always yield in false for relation operators)
3709
3710 runScript({
3711 .code = R"NKSP_CODE(
3712 on init
3713 exit(!-4 <= !3)
3714 end on
3715 )NKSP_CODE",
3716 .expectBoolExitResult = true,
3717 .expectExitResultFinal = false
3718 });
3719
3720 runScript({
3721 .code = R"NKSP_CODE(
3722 on init
3723 exit(-4 <= 3)
3724 end on
3725 )NKSP_CODE",
3726 .expectBoolExitResult = true,
3727 .expectExitResultFinal = false
3728 });
3729
3730 #if !SILENT_TEST
3731 std::cout << std::endl;
3732 #endif
3733 }
3734
3735 static void testGreaterOrEqualOperator() {
3736 #if !SILENT_TEST
3737 std::cout << "UNIT TEST: greater-or-equal (>=) operator\n";
3738 #endif
3739
3740 // integer tests ...
3741
3742 runScript({
3743 .code = R"NKSP_CODE(
3744 on init
3745 exit(3 >= 3)
3746 end on
3747 )NKSP_CODE",
3748 .expectBoolExitResult = true
3749 });
3750
3751 runScript({
3752 .code = R"NKSP_CODE(
3753 on init
3754 exit(4 >= 4)
3755 end on
3756 )NKSP_CODE",
3757 .expectBoolExitResult = true
3758 });
3759
3760 runScript({
3761 .code = R"NKSP_CODE(
3762 on init
3763 exit(-23 >= -23)
3764 end on
3765 )NKSP_CODE",
3766 .expectBoolExitResult = true
3767 });
3768
3769 runScript({
3770 .code = R"NKSP_CODE(
3771 on init
3772 exit(23 >= -23)
3773 end on
3774 )NKSP_CODE",
3775 .expectBoolExitResult = true
3776 });
3777
3778 runScript({
3779 .code = R"NKSP_CODE(
3780 on init
3781 exit(3 >= 4)
3782 end on
3783 )NKSP_CODE",
3784 .expectBoolExitResult = false
3785 });
3786
3787 runScript({
3788 .code = R"NKSP_CODE(
3789 on init
3790 exit(4 >= 3)
3791 end on
3792 )NKSP_CODE",
3793 .expectBoolExitResult = true
3794 });
3795
3796 runScript({
3797 .code = R"NKSP_CODE(
3798 on init
3799 exit(-4 >= 3)
3800 end on
3801 )NKSP_CODE",
3802 .expectBoolExitResult = false
3803 });
3804
3805 runScript({
3806 .code = R"NKSP_CODE(
3807 on init
3808 exit(3 >= -4)
3809 end on
3810 )NKSP_CODE",
3811 .expectBoolExitResult = true
3812 });
3813
3814 runScript({
3815 .code = R"NKSP_CODE(
3816 on init
3817 exit(123 >= -45)
3818 end on
3819 )NKSP_CODE",
3820 .expectBoolExitResult = true
3821 });
3822
3823 runScript({
3824 .code = R"NKSP_CODE(
3825 on init
3826 exit(-45 >= 123)
3827 end on
3828 )NKSP_CODE",
3829 .expectBoolExitResult = false
3830 });
3831
3832 // real number tests ...
3833
3834 runScript({
3835 .code = R"NKSP_CODE(
3836 on init
3837 exit(3.0 >= 3.0)
3838 end on
3839 )NKSP_CODE",
3840 .expectBoolExitResult = true
3841 });
3842
3843 runScript({
3844 .code = R"NKSP_CODE(
3845 on init
3846 exit(3.1 >= 3.1)
3847 end on
3848 )NKSP_CODE",
3849 .expectBoolExitResult = true
3850 });
3851
3852 runScript({
3853 .code = R"NKSP_CODE(
3854 on init
3855 exit(3.1 >= 3.0)
3856 end on
3857 )NKSP_CODE",
3858 .expectBoolExitResult = true
3859 });
3860
3861 runScript({
3862 .code = R"NKSP_CODE(
3863 on init
3864 exit(3.0 >= 3.1)
3865 end on
3866 )NKSP_CODE",
3867 .expectBoolExitResult = false
3868 });
3869
3870 runScript({
3871 .code = R"NKSP_CODE(
3872 on init
3873 exit(-23.33 >= -23.33)
3874 end on
3875 )NKSP_CODE",
3876 .expectBoolExitResult = true
3877 });
3878
3879 runScript({
3880 .code = R"NKSP_CODE(
3881 on init
3882 exit(23.0 >= -23.0)
3883 end on
3884 )NKSP_CODE",
3885 .expectBoolExitResult = true
3886 });
3887
3888 runScript({
3889 .code = R"NKSP_CODE(
3890 on init
3891 exit(3.0 >= 4.0)
3892 end on
3893 )NKSP_CODE",
3894 .expectBoolExitResult = false
3895 });
3896
3897 runScript({
3898 .code = R"NKSP_CODE(
3899 on init
3900 exit(4.0 >= 3.0)
3901 end on
3902 )NKSP_CODE",
3903 .expectBoolExitResult = true
3904 });
3905
3906 runScript({
3907 .code = R"NKSP_CODE(
3908 on init
3909 exit(-4.0 >= 3.0)
3910 end on
3911 )NKSP_CODE",
3912 .expectBoolExitResult = false
3913 });
3914
3915 runScript({
3916 .code = R"NKSP_CODE(
3917 on init
3918 exit(3.0 >= -4.0)
3919 end on
3920 )NKSP_CODE",
3921 .expectBoolExitResult = true
3922 });
3923
3924 runScript({
3925 .code = R"NKSP_CODE(
3926 on init
3927 exit(123.0 >= -45.0)
3928 end on
3929 )NKSP_CODE",
3930 .expectBoolExitResult = true
3931 });
3932
3933 runScript({
3934 .code = R"NKSP_CODE(
3935 on init
3936 exit(-45.0 >= 123.0)
3937 end on
3938 )NKSP_CODE",
3939 .expectBoolExitResult = false
3940 });
3941
3942 // mixed type tests ...
3943
3944 runScript({
3945 .code = R"NKSP_CODE(
3946 on init
3947 exit(9 >= 9.1)
3948 end on
3949 )NKSP_CODE",
3950 .expectBoolExitResult = false
3951 });
3952
3953 runScript({
3954 .code = R"NKSP_CODE(
3955 on init
3956 exit(9.1 >= 9)
3957 end on
3958 )NKSP_CODE",
3959 .expectBoolExitResult = true
3960 });
3961
3962 runScript({
3963 .code = R"NKSP_CODE(
3964 on init
3965 exit(9 >= 9.0)
3966 end on
3967 )NKSP_CODE",
3968 .expectBoolExitResult = true
3969 });
3970
3971 runScript({
3972 .code = R"NKSP_CODE(
3973 on init
3974 exit(9.0 >= 9)
3975 end on
3976 )NKSP_CODE",
3977 .expectBoolExitResult = true
3978 });
3979
3980 // std unit tests ...
3981
3982 runScript({
3983 .code = R"NKSP_CODE(
3984 on init
3985 exit(13ms >= 14ms)
3986 end on
3987 )NKSP_CODE",
3988 .expectBoolExitResult = false
3989 });
3990
3991 runScript({
3992 .code = R"NKSP_CODE(
3993 on init
3994 exit(14ms >= 13ms)
3995 end on
3996 )NKSP_CODE",
3997 .expectBoolExitResult = true
3998 });
3999
4000 runScript({
4001 .code = R"NKSP_CODE(
4002 on init
4003 exit(1s >= 990ms)
4004 end on
4005 )NKSP_CODE",
4006 .expectBoolExitResult = true
4007 });
4008
4009 runScript({
4010 .code = R"NKSP_CODE(
4011 on init
4012 exit(990ms >= 1s)
4013 end on
4014 )NKSP_CODE",
4015 .expectBoolExitResult = false
4016 });
4017
4018 runScript({
4019 .code = R"NKSP_CODE(
4020 on init
4021 exit(1000ms >= 1s)
4022 end on
4023 )NKSP_CODE",
4024 .expectBoolExitResult = true
4025 });
4026
4027 runScript({
4028 .code = R"NKSP_CODE(
4029 on init
4030 exit(1s >= 1000ms)
4031 end on
4032 )NKSP_CODE",
4033 .expectBoolExitResult = true
4034 });
4035
4036 runScript({
4037 .code = R"NKSP_CODE(
4038 on init
4039 exit(1s >= 1)
4040 end on
4041 )NKSP_CODE",
4042 .expectParseError = true // units on both sides must match
4043 });
4044
4045 runScript({
4046 .code = R"NKSP_CODE(
4047 on init
4048 exit(1 >= 1s)
4049 end on
4050 )NKSP_CODE",
4051 .expectParseError = true // units on both sides must match
4052 });
4053
4054 runScript({
4055 .code = R"NKSP_CODE(
4056 on init
4057 exit(1Hz >= 1B)
4058 end on
4059 )NKSP_CODE",
4060 .expectParseError = true // units on both sides must match
4061 });
4062
4063 runScript({
4064 .code = R"NKSP_CODE(
4065 on init
4066 exit(13.0ms >= 13.1ms)
4067 end on
4068 )NKSP_CODE",
4069 .expectBoolExitResult = false
4070 });
4071
4072 runScript({
4073 .code = R"NKSP_CODE(
4074 on init
4075 exit(13.1ms >= 13.0ms)
4076 end on
4077 )NKSP_CODE",
4078 .expectBoolExitResult = true
4079 });
4080
4081 runScript({
4082 .code = R"NKSP_CODE(
4083 on init
4084 exit(0.9s >= 600.0ms)
4085 end on
4086 )NKSP_CODE",
4087 .expectBoolExitResult = true
4088 });
4089
4090 runScript({
4091 .code = R"NKSP_CODE(
4092 on init
4093 exit(600.0ms >= 0.9s)
4094 end on
4095 )NKSP_CODE",
4096 .expectBoolExitResult = false
4097 });
4098
4099 runScript({
4100 .code = R"NKSP_CODE(
4101 on init
4102 exit(5.1kHz >= 5100.0Hz)
4103 end on
4104 )NKSP_CODE",
4105 .expectBoolExitResult = true
4106 });
4107
4108 runScript({
4109 .code = R"NKSP_CODE(
4110 on init
4111 exit(5100.0Hz >= 5.1kHz)
4112 end on
4113 )NKSP_CODE",
4114 .expectBoolExitResult = true
4115 });
4116
4117 runScript({
4118 .code = R"NKSP_CODE(
4119 on init
4120 exit(1.0Hz >= 1.1)
4121 end on
4122 )NKSP_CODE",
4123 .expectParseError = true // units on both sides must match
4124 });
4125
4126 runScript({
4127 .code = R"NKSP_CODE(
4128 on init
4129 exit(1.2 >= 1.34mdB)
4130 end on
4131 )NKSP_CODE",
4132 .expectParseError = true // units on both sides must match
4133 });
4134
4135 runScript({
4136 .code = R"NKSP_CODE(
4137 on init
4138 exit(9.23us >= 3.14kHz)
4139 end on
4140 )NKSP_CODE",
4141 .expectParseError = true // units on both sides must match
4142 });
4143
4144 // 'final' ('!') operator tests ...
4145 // (should always yield in false for relation operators)
4146
4147 runScript({
4148 .code = R"NKSP_CODE(
4149 on init
4150 exit(!-4 >= !3)
4151 end on
4152 )NKSP_CODE",
4153 .expectBoolExitResult = false,
4154 .expectExitResultFinal = false
4155 });
4156
4157 runScript({
4158 .code = R"NKSP_CODE(
4159 on init
4160 exit(-4 >= 3)
4161 end on
4162 )NKSP_CODE",
4163 .expectBoolExitResult = false,
4164 .expectExitResultFinal = false
4165 });
4166
4167 #if !SILENT_TEST
4168 std::cout << std::endl;
4169 #endif
4170 }
4171
4172 static void testEqualOperator() {
4173 #if !SILENT_TEST
4174 std::cout << "UNIT TEST: equal (=) operator\n";
4175 #endif
4176
4177 // integer tests ...
4178
4179 runScript({
4180 .code = R"NKSP_CODE(
4181 on init
4182 exit(3 = 3)
4183 end on
4184 )NKSP_CODE",
4185 .expectBoolExitResult = true
4186 });
4187
4188 runScript({
4189 .code = R"NKSP_CODE(
4190 on init
4191 exit(4 = 4)
4192 end on
4193 )NKSP_CODE",
4194 .expectBoolExitResult = true
4195 });
4196
4197 runScript({
4198 .code = R"NKSP_CODE(
4199 on init
4200 exit(3 = 4)
4201 end on
4202 )NKSP_CODE",
4203 .expectBoolExitResult = false
4204 });
4205
4206 runScript({
4207 .code = R"NKSP_CODE(
4208 on init
4209 exit(23 = -23)
4210 end on
4211 )NKSP_CODE",
4212 .expectBoolExitResult = false
4213 });
4214
4215 // real number tests ...
4216
4217 runScript({
4218 .code = R"NKSP_CODE(
4219 on init
4220 exit(3.0 = 3.0)
4221 end on
4222 )NKSP_CODE",
4223 .expectBoolExitResult = true
4224 });
4225
4226 runScript({
4227 .code = R"NKSP_CODE(
4228 on init
4229 exit(4.33 = 4.33)
4230 end on
4231 )NKSP_CODE",
4232 .expectBoolExitResult = true
4233 });
4234
4235 runScript({
4236 .code = R"NKSP_CODE(
4237 on init
4238 exit(4.31 = 4.35)
4239 end on
4240 )NKSP_CODE",
4241 .expectBoolExitResult = false
4242 });
4243
4244 runScript({
4245 .code = R"NKSP_CODE(
4246 on init
4247 exit(3.0 = 4.0)
4248 end on
4249 )NKSP_CODE",
4250 .expectBoolExitResult = false
4251 });
4252
4253 runScript({
4254 .code = R"NKSP_CODE(
4255 on init
4256 exit(23.0 = -23.0)
4257 end on
4258 )NKSP_CODE",
4259 .expectBoolExitResult = false
4260 });
4261
4262 // deal with inaccuracy of float point
4263 runScript({
4264 .code = R"NKSP_CODE(
4265 on init
4266 declare ~a := 0.165
4267 declare ~b := 0.185
4268 declare ~x := 0.1
4269 declare ~y := 0.25
4270 exit(~a + ~b = ~x + ~y) { both sides should actually be 0.35, they slightly deviate both though }
4271 end on
4272 )NKSP_CODE",
4273 .expectBoolExitResult = true // our implementation of real number equal comparison should take care about floating point tolerance
4274 });
4275
4276 // deal with inaccuracy of float point
4277 runScript({
4278 .code = R"NKSP_CODE(
4279 on init
4280 declare ~a := 0.166
4281 declare ~b := 0.185
4282 declare ~x := 0.1
4283 declare ~y := 0.25
4284 exit(~a + ~b = ~x + ~y) { left side approx. 0.351, right side approx. 0.35 }
4285 end on
4286 )NKSP_CODE",
4287 .expectBoolExitResult = false
4288 });
4289
4290 // mixed type tests ...
4291
4292 runScript({
4293 .code = R"NKSP_CODE(
4294 on init
4295 exit(23 = 23.0)
4296 end on
4297 )NKSP_CODE",
4298 .expectBoolExitResult = true
4299 });
4300
4301 runScript({
4302 .code = R"NKSP_CODE(
4303 on init
4304 exit(23.0 = 23)
4305 end on
4306 )NKSP_CODE",
4307 .expectBoolExitResult = true
4308 });
4309
4310 runScript({
4311 .code = R"NKSP_CODE(
4312 on init
4313 exit(23 = 23.1)
4314 end on
4315 )NKSP_CODE",
4316 .expectBoolExitResult = false
4317 });
4318
4319 runScript({
4320 .code = R"NKSP_CODE(
4321 on init
4322 exit(23.1 = 23)
4323 end on
4324 )NKSP_CODE",
4325 .expectBoolExitResult = false
4326 });
4327
4328 // std unit tests ...
4329
4330 runScript({
4331 .code = R"NKSP_CODE(
4332 on init
4333 exit(13ms = 14ms)
4334 end on
4335 )NKSP_CODE",
4336 .expectBoolExitResult = false