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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3802 by schoenebeck, Mon Jun 15 15:26:10 2020 UTC revision 3803 by schoenebeck, Thu Aug 6 11:45:24 2020 UTC
# Line 25  Line 25 
25  # define TEST_ASSERT assert  # define TEST_ASSERT assert
26  #endif  #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;  using namespace LinuxSampler;
81  using namespace std;  using namespace std;
82    
# Line 46  struct RunScriptOpt { Line 98  struct RunScriptOpt {
98      optional<bool> expectExitResultFinal;      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) {  static void runScript(const RunScriptOpt& opt) {
119      ScriptVM vm;      ScriptVM vm;
120      vm.setAutoSuspendEnabled(false);      vm.setAutoSuspendEnabled(false);
# Line 57  static void runScript(const RunScriptOpt Line 126  static void runScript(const RunScriptOpt
126      vector<ParserIssue> errors = parserCtx->errors();      vector<ParserIssue> errors = parserCtx->errors();
127      vector<ParserIssue> warnings = parserCtx->warnings();      vector<ParserIssue> warnings = parserCtx->warnings();
128      if (opt.expectParseError) {      if (opt.expectParseError) {
129          TEST_ASSERT(!errors.empty());          TEST_VERIFY(!errors.empty());
130          return;          return;
131      } else {      } else {
132          for (ParserIssue& err : errors) {          for (ParserIssue& err : errors) {
133              err.dump();              err.dump();
134          }          }
135          TEST_ASSERT(errors.empty());          TEST_VERIFY(errors.empty());
136      }      }
137      if (opt.expectParseWarning) {      if (opt.expectParseWarning) {
138          TEST_ASSERT(!warnings.empty());          TEST_VERIFY(!warnings.empty());
139      } else {      } else {
140          for (ParserIssue& wrn : warnings) {          for (ParserIssue& wrn : warnings) {
141              wrn.dump();              wrn.dump();
142          }          }
143      }      }
144      TEST_ASSERT(parserCtx->eventHandler(0));      TEST_VERIFY(parserCtx->eventHandler(0));
145      unique_ptr<VMExecContext> execCtx(      unique_ptr<VMExecContext> execCtx(
146          vm.createExecContext(&*parserCtx)          vm.createExecContext(&*parserCtx)
147      );      );
# Line 80  static void runScript(const RunScriptOpt Line 149  static void runScript(const RunScriptOpt
149          VMEventHandler* handler = parserCtx->eventHandler(i);          VMEventHandler* handler = parserCtx->eventHandler(i);
150          VMExecStatus_t result = vm.exec(&*parserCtx, &*execCtx, handler);          VMExecStatus_t result = vm.exec(&*parserCtx, &*execCtx, handler);
151          if (opt.expectRuntimeError) {          if (opt.expectRuntimeError) {
152              TEST_ASSERT(result & VM_EXEC_ERROR);              TEST_VERIFY(result & VM_EXEC_ERROR);
153          } else {          } else {
154              TEST_ASSERT(!(result & VM_EXEC_ERROR));              TEST_VERIFY(!(result & VM_EXEC_ERROR));
155          }          }
156          if (opt.expectNoExitResult) {          if (opt.expectNoExitResult) {
157              VMExpr* resExpr = execCtx->exitResult();              VMExpr* resExpr = execCtx->exitResult();
158              TEST_ASSERT(!resExpr);              TEST_VERIFY(!resExpr);
159          }          }
160          if (opt.expectExitResultIsInt) {          if (opt.expectExitResultIsInt) {
161              VMExpr* resExpr = execCtx->exitResult();              VMExpr* resExpr = execCtx->exitResult();
162              TEST_ASSERT(resExpr);              TEST_VERIFY(resExpr);
163              TEST_ASSERT(resExpr->exprType() == INT_EXPR);              TEST_VERIFY(resExpr->exprType() == INT_EXPR);
164          }          }
165          if (opt.expectExitResultIsReal) {          if (opt.expectExitResultIsReal) {
166              VMExpr* resExpr = execCtx->exitResult();              VMExpr* resExpr = execCtx->exitResult();
167              TEST_ASSERT(resExpr);              TEST_VERIFY(resExpr);
168              TEST_ASSERT(resExpr->exprType() == REAL_EXPR);              TEST_VERIFY(resExpr->exprType() == REAL_EXPR);
169          }          }
170          if (opt.expectIntExitResult) {          if (opt.expectIntExitResult) {
171              VMExpr* resExpr = execCtx->exitResult();              VMExpr* resExpr = execCtx->exitResult();
172              TEST_ASSERT(resExpr);              TEST_VERIFY(resExpr);
173              TEST_ASSERT(resExpr->exprType() == INT_EXPR);              TEST_VERIFY(resExpr->exprType() == INT_EXPR);
174              TEST_ASSERT(resExpr->asInt()->evalInt() == *opt.expectIntExitResult);              TEST_VERIFY(resExpr->asInt()->evalInt() == *opt.expectIntExitResult);
175          }          }
176          if (opt.expectRealExitResult) {          if (opt.expectRealExitResult) {
177              VMExpr* resExpr = execCtx->exitResult();              VMExpr* resExpr = execCtx->exitResult();
178              TEST_ASSERT(resExpr);              TEST_VERIFY(resExpr);
179              TEST_ASSERT(resExpr->exprType() == REAL_EXPR);              TEST_VERIFY(resExpr->exprType() == REAL_EXPR);
180              if (sizeof(vmfloat) == sizeof(float)) {              if (sizeof(vmfloat) == sizeof(float)) {
181                  TEST_ASSERT(RTMath::fEqual32(resExpr->asReal()->evalReal(), *opt.expectRealExitResult));                  TEST_VERIFY(RTMath::fEqual32(resExpr->asReal()->evalReal(), *opt.expectRealExitResult));
182              } else {              } else {
183                  TEST_ASSERT(RTMath::fEqual64(resExpr->asReal()->evalReal(), *opt.expectRealExitResult));                  TEST_VERIFY(RTMath::fEqual64(resExpr->asReal()->evalReal(), *opt.expectRealExitResult));
184              }              }
185          }          }
186          if (opt.expectBoolExitResult) {          if (opt.expectBoolExitResult) {
187              VMExpr* resExpr = execCtx->exitResult();              VMExpr* resExpr = execCtx->exitResult();
188              TEST_ASSERT(resExpr);              TEST_VERIFY(resExpr);
189              TEST_ASSERT(resExpr->exprType() == INT_EXPR);              TEST_VERIFY(resExpr->exprType() == INT_EXPR);
190              TEST_ASSERT(bool(resExpr->asInt()->evalInt()) == *opt.expectBoolExitResult);              TEST_VERIFY(bool(resExpr->asInt()->evalInt()) == *opt.expectBoolExitResult);
191          }          }
192          if (opt.expectStringExitResult) {          if (opt.expectStringExitResult) {
193              VMExpr* resExpr = execCtx->exitResult();              VMExpr* resExpr = execCtx->exitResult();
194              TEST_ASSERT(resExpr);              TEST_VERIFY(resExpr);
195              TEST_ASSERT(resExpr->exprType() == STRING_EXPR);              TEST_VERIFY(resExpr->exprType() == STRING_EXPR);
196              TEST_ASSERT(resExpr->asString()->evalStr() == *opt.expectStringExitResult);              TEST_VERIFY(resExpr->asString()->evalStr() == *opt.expectStringExitResult);
197          }          }
198          if (opt.expectExitResultUnit) {          if (opt.expectExitResultUnit) {
199              VMExpr* resExpr = execCtx->exitResult();              VMExpr* resExpr = execCtx->exitResult();
200              TEST_ASSERT(resExpr);              TEST_VERIFY(resExpr);
201              VMNumberExpr* numberExpr = resExpr->asNumber();              VMNumberExpr* numberExpr = resExpr->asNumber();
202              TEST_ASSERT(numberExpr);              TEST_VERIFY(numberExpr);
203              TEST_ASSERT(numberExpr->unitType() == *opt.expectExitResultUnit);              TEST_VERIFY(numberExpr->unitType() == *opt.expectExitResultUnit);
204          }          }
205          if (!opt.expectExitResultUnitPrefix.empty()) {          if (!opt.expectExitResultUnitPrefix.empty()) {
206              VMExpr* resExpr = execCtx->exitResult();              VMExpr* resExpr = execCtx->exitResult();
207              TEST_ASSERT(resExpr);              TEST_VERIFY(resExpr);
208              VMNumberExpr* numberExpr = resExpr->asNumber();              VMNumberExpr* numberExpr = resExpr->asNumber();
209              TEST_ASSERT(numberExpr);              TEST_VERIFY(numberExpr);
210              auto prefixes = opt.expectExitResultUnitPrefix;              auto prefixes = opt.expectExitResultUnitPrefix;
211              if (*prefixes.rbegin() != VM_NO_PREFIX)              if (*prefixes.rbegin() != VM_NO_PREFIX)
212                  prefixes.push_back(VM_NO_PREFIX); // VM_NO_PREFIX termination required by unitFactr() call                  prefixes.push_back(VM_NO_PREFIX); // VM_NO_PREFIX termination required by unitFactr() call
213              vmfloat expectedFactor = VMUnit::unitFactor(&prefixes[0]);              vmfloat expectedFactor = VMUnit::unitFactor(&prefixes[0]);
214              vmfloat actualFactor = numberExpr->unitFactor();              vmfloat actualFactor = numberExpr->unitFactor();
215              if (sizeof(vmfloat) == sizeof(float)) {              if (sizeof(vmfloat) == sizeof(float)) {
216                  TEST_ASSERT(RTMath::fEqual32(expectedFactor, actualFactor));                  TEST_VERIFY(RTMath::fEqual32(expectedFactor, actualFactor));
217              } else {              } else {
218                  TEST_ASSERT(RTMath::fEqual64(expectedFactor, actualFactor));                  TEST_VERIFY(RTMath::fEqual64(expectedFactor, actualFactor));
219              }              }
220          }          }
221          if (opt.expectExitResultFinal) {          if (opt.expectExitResultFinal) {
222              VMExpr* resExpr = execCtx->exitResult();              VMExpr* resExpr = execCtx->exitResult();
223              TEST_ASSERT(resExpr);              TEST_VERIFY(resExpr);
224              VMNumberExpr* numberExpr = resExpr->asNumber();              VMNumberExpr* numberExpr = resExpr->asNumber();
225              TEST_ASSERT(numberExpr);              TEST_VERIFY(numberExpr);
226              TEST_ASSERT(numberExpr->isFinal() == *opt.expectExitResultFinal);              TEST_VERIFY(numberExpr->isFinal() == *opt.expectExitResultFinal);
227          }          }
228      }      }
229  }  }

Legend:
Removed from v.3802  
changed lines
  Added in v.3803

  ViewVC Help
Powered by ViewVC