swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 1 | #include <QCoreApplication> |
| 2 | #include <QDebug> |
| 3 | |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 4 | #include "Matcher.h" |
| 5 | #include "Token.h" |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 6 | #include "AstNode.h" |
| 7 | #include "Parser.h" |
swissChili | 07d325f | 2021-12-08 20:02:05 -0800 | [diff] [blame^] | 8 | #include "Evaluator.h" |
| 9 | #include "VarContext.h" |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 10 | |
| 11 | int g_numFailed = 0; |
| 12 | |
swissChili | 07d325f | 2021-12-08 20:02:05 -0800 | [diff] [blame^] | 13 | void testEval(QString function, QString expression, QString expected) |
| 14 | { |
| 15 | Evaluator eval; |
| 16 | Parser funcParser(function), |
| 17 | exprParser(expression), |
| 18 | resParser(expected); |
| 19 | |
| 20 | Function func; |
| 21 | |
| 22 | QList<AstNode> expr = exprParser.parseMany<AstNode>(); |
| 23 | QList<Token> res = resParser.parseMany<Token>(); |
| 24 | |
| 25 | QList<Token> result; |
| 26 | |
| 27 | exprParser.skip(); |
| 28 | resParser.skip(); |
| 29 | while (funcParser.parseFunctionDefinition(&func)) |
| 30 | { |
| 31 | eval.addFunction(func); |
| 32 | } |
| 33 | |
| 34 | funcParser.skip(); |
| 35 | |
| 36 | if (!exprParser.atEnd() || !resParser.atEnd() || !funcParser.atEnd()) |
| 37 | { |
| 38 | g_numFailed++; |
| 39 | qDebug() << "\n\033[31mTEST FAILS:\033[0m"; |
| 40 | qDebug() << "Failed to fully parse expression, function or result"; |
| 41 | qDebug() << function << expression << expected; |
| 42 | |
| 43 | goto end; |
| 44 | } |
| 45 | |
| 46 | for (const AstNode &node : expr) |
| 47 | { |
| 48 | RuntimeResult rr = eval.evaluate(node, VarContext()); |
| 49 | |
| 50 | if (!rr.success()) |
| 51 | { |
| 52 | g_numFailed++; |
| 53 | qDebug() << "\n\033[31mTEST FAILS:\033[0m"; |
| 54 | qDebug() << "Runtime error while evaluating" << node; |
| 55 | qDebug() << rr; |
| 56 | |
| 57 | goto end; |
| 58 | } |
| 59 | |
| 60 | result.append(rr.result()); |
| 61 | } |
| 62 | |
| 63 | if (result != res) |
| 64 | { |
| 65 | g_numFailed++; |
| 66 | qDebug() << "\n\033[31mTEST FAILS:\033[0m"; |
| 67 | qDebug() << "Expected result" << res; |
| 68 | qDebug() << "Got" << result; |
| 69 | } |
| 70 | |
| 71 | end: |
| 72 | qDebug() << "\033[36mEvaluate\033[0m" << function << expression << "->" << result; |
| 73 | } |
| 74 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 75 | void testMatch(const QString &test, bool shouldBe, const MatchResult &result) |
| 76 | { |
| 77 | if (result.success != shouldBe) |
| 78 | { |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 79 | g_numFailed++; |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 80 | qDebug() << "\n\033[31mTEST FAILS:\033[0m"; |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 81 | qDebug() << "with context" << result.context; |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 82 | } |
| 83 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 84 | qDebug() << "\033[36mMatchResult\033[0m" << test << result.success; |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 85 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 86 | if (result.success != shouldBe) |
| 87 | { |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 88 | qDebug() << ""; |
| 89 | } |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 90 | } |
| 91 | |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 92 | void testMatch(QString data, QString pattern, bool shouldBe = true) |
| 93 | { |
| 94 | Parser dataParser(data), |
| 95 | patternParser(pattern); |
| 96 | |
swissChili | 07d325f | 2021-12-08 20:02:05 -0800 | [diff] [blame^] | 97 | testMatch(pattern + " = " + data, shouldBe, |
| 98 | match(dataParser.parseMany<Token>(), patternParser.parseMany<Token>(), VarContext())); |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | void testParseAst(QString string) |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 102 | { |
| 103 | Parser parser{string}; |
| 104 | |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 105 | QList<AstNode> result = parser.parseMany<AstNode>(); |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 106 | |
| 107 | qDebug() << "\033[36mParse\033[0m" << string << result; |
| 108 | } |
| 109 | |
swissChili | 8a581c6 | 2021-12-07 13:29:21 -0800 | [diff] [blame] | 110 | void testParseFunc(QString string) |
| 111 | { |
| 112 | Parser parser{string}; |
| 113 | |
| 114 | Function func; |
| 115 | |
| 116 | if (!parser.parseFunctionDefinition(&func)) |
| 117 | { |
| 118 | g_numFailed++; |
| 119 | qDebug() << "\n\033[31mTEST FAILS:\033[0m"; |
| 120 | qDebug() << string; |
| 121 | } |
| 122 | else |
| 123 | { |
| 124 | qDebug() << "\033[36mFunction\033[0m"; |
| 125 | qDebug().noquote() << func; |
| 126 | } |
| 127 | } |
| 128 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 129 | int testResults() |
| 130 | { |
| 131 | if (g_numFailed == 0) |
| 132 | { |
| 133 | qDebug() << "\033[32mALL TESTS SUCCEEDED\033[0m"; |
| 134 | } |
| 135 | else |
| 136 | { |
| 137 | qDebug().nospace() << "\033[31m" << g_numFailed << " TESTS FAILED\033[0m"; |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | return g_numFailed; |
| 141 | } |
| 142 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 143 | void testAllMatches() |
| 144 | { |
| 145 | testMatch("a = a", true, match({Token('a')}, {Token('a')}, VarContext())); |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 146 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 147 | testMatch("s.a = y", true, match({Token('y')}, {Token('s', "a")}, VarContext())); |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 148 | |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 149 | LTok sameTwo = {Token('s', "a"), Token('s', "a")}; |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 150 | testMatch("s.a s.a = aa", true, match({Token('a'), Token('a')}, sameTwo, VarContext())); |
| 151 | testMatch("s.a s.a = ab", false, match({Token('a'), Token('b')}, sameTwo, VarContext())); |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 152 | |
| 153 | LTok sameStartEnd = { |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 154 | Token('s', "a"), |
| 155 | Token('e', "middle"), |
| 156 | Token('s', "a")}; |
| 157 | testMatch("s.a e.middle s.a = aea", true, |
| 158 | match({Token('a'), Token('e'), Token('a')}, sameStartEnd, VarContext())); |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 159 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 160 | testMatch("s.a e.middle s.a = aef Hi a", true, |
| 161 | match({Token('a'), Token('e'), Token('f'), Token("Hi"), Token('a')}, sameStartEnd, VarContext())); |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 162 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 163 | testMatch("s.a e.middle s.a = aef Hi c", false, |
| 164 | match({Token('a'), Token('e'), Token('f'), Token("Hi"), Token('c')}, sameStartEnd, VarContext())); |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 165 | |
| 166 | LTok parenthesized = { |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 167 | Token(LTok({Token('s', "a")})), |
| 168 | Token('e', "Middle"), |
| 169 | Token('s', "a"), |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 170 | }; |
| 171 | LTok parenTest1 = { |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 172 | Token(LTok({Token('y')})), |
| 173 | Token('f'), |
| 174 | Token("MiddleStuff"), |
| 175 | Token('y')}; |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 176 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 177 | testMatch("(s.a) e.Middle s.a = (y)f MiddleStuff y", true, |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 178 | match(parenTest1, parenthesized, VarContext())); |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 179 | // testMatch("(y)f Middle-stuff y", "(s.a) e.Middle s.a"); |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 180 | |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 181 | testMatch("(a)", "(a)"); |
swissChili | 07d325f | 2021-12-08 20:02:05 -0800 | [diff] [blame^] | 182 | testMatch("hello", "s.A e.Rest"); |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 183 | } |
| 184 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 185 | void testAllParses() |
| 186 | { |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 187 | testParseAst("all symbols"); |
| 188 | testParseAst("Identifier symbols Identifier"); |
| 189 | testParseAst("s.A"); |
| 190 | testParseAst("(s.A) Variable-quoted"); |
| 191 | testParseAst("<Func-name a b (c)>"); |
| 192 | testParseAst("<Prout hi>"); |
| 193 | testParseAst("(Prout hi)"); |
| 194 | testParseAst("(<Prout hi>)"); |
| 195 | testParseAst("<If T Then (<Prout hi>) Else (<Prout sorry>)>"); |
| 196 | testParseAst("(s.a) e.Middle s.a"); |
swissChili | 8a581c6 | 2021-12-07 13:29:21 -0800 | [diff] [blame] | 197 | testParseAst("Hello; Goodbye"); |
| 198 | testParseAst("Key = Value"); |
| 199 | } |
| 200 | |
| 201 | void testAllFunctionDefs() |
| 202 | { |
| 203 | testParseFunc("Test { = HI; }"); |
swissChili | 07d325f | 2021-12-08 20:02:05 -0800 | [diff] [blame^] | 204 | testParseFunc("Palindrome { = T; s.A = T; s.A s.A = T; s.A e.Middle s.A = <Palindrome e.Middle>; e.Ignored = F; } "); |
| 205 | } |
| 206 | |
| 207 | void testAllEvals() |
| 208 | { |
| 209 | testEval("First {s.A e.Rest = s.A;}", "<First hello>", "h"); |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | int main(int argc, char *argv[]) |
| 213 | { |
swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 214 | QCoreApplication a(argc, argv); |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 215 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 216 | testAllMatches(); |
| 217 | qDebug() << ""; |
| 218 | testAllParses(); |
swissChili | 8a581c6 | 2021-12-07 13:29:21 -0800 | [diff] [blame] | 219 | qDebug() << ""; |
| 220 | testAllFunctionDefs(); |
swissChili | 07d325f | 2021-12-08 20:02:05 -0800 | [diff] [blame^] | 221 | qDebug() << ""; |
| 222 | testAllEvals(); |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 223 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 224 | qDebug() << ""; |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 225 | return testResults(); |
swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 226 | } |