-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
255 lines (230 loc) · 8.72 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Licensed under the LGPL3 license.
// See the LICENSE file in the project root for more information.
using System;
using System.Linq;
using Eval;
using static Eval.Globals;
// generate csharp tests using exprgen
// $ ExprGen | EvalTest gen >> file.cs
if (args.Length > 0 && args[0] == "gen")
{
string input;
while ((input = Console.ReadLine()) != null)
{
Console.WriteLine($"""Test({input}, "{input}");""");
}
Environment.Exit(0);
}
double[] xs;
double[] xss;
double result;
static int Factorial(int n)
{
return n == 0 ? 1 : Enumerable.Range(1, n).Aggregate(1, (acc, x) => acc * x);
}
double Percentage(double x, double y)
{
return x * (y / 100);
}
void Separator(string header = "")
{
string separator = new('-', (80 - header.Length) / 2);
Console.WriteLine(separator + header + separator);
}
/// <summary>
/// Joins the tokens separated by the lexer
/// </summary>
string GetTokens(string expr)
{
Lexer lexer = new(expr);
string tokens = "" + lexer.NextToken();
Token next = lexer.NextToken();
while (next.Kind != TokenKind.End)
{
tokens += " " + next;
next = lexer.NextToken();
}
return tokens;
}
static string Error(string message, string src, int offset, int length)
{
if (length < 1)
{
return $"{message}";
}
string marker = new(' ', offset);
if (length == 1)
{
marker += "^";
}
else if (length == 2)
{
marker += "^^";
}
else if (length > 2)
{
marker += $"^{new('~', length - 2)}^";
}
return $"{message}\n{src}\n{marker}";
}
void TestExceptions(string expectedException, string expression)
{
Console.WriteLine($"ExpectedException: {expectedException}");
Console.WriteLine($"Expression: {expression}");
try
{
Console.WriteLine($"Lexicons: {GetTokens(expression)}");
result = Evaluator.Evaluate(expression);
Console.WriteLine("❌ TestExceptions() Should not be evaluated Correctly!");
Environment.Exit(1);
}
catch (Exception e)
{
Console.WriteLine(
e switch
{
InvalidExpressionException ie => Error(ie.Message, ie.Src, ie.Offset, ie.Length),
ArgumentAmountException ae => Error(ae.Message, ae.Src, ae.Offset, ae.Length),
_ => e.Message
}
);
Separator();
}
}
void Test(double expectedResult, string expression, string reason = "")
{
Console.WriteLine($"Expression: {expression}");
Console.WriteLine($"Lexicons: {GetTokens(expression)}");
result = Evaluator.Evaluate(expression);
Console.WriteLine($"Result: {expectedResult} = {result}");
Console.WriteLine("Passed: " + (expectedResult == result ? "✅" : $"{reason}❌"));
Separator();
}
// disable parens warnings
#pragma warning disable IDE0048
// disable formatting warnings
#pragma warning disable IDE0055
Separator();
Test(+42, "+42");
xs = new double[] { 2, 3, 5 };
Test(Math.Pow(-xs.Average(), -5), "Math.Pow(-average(2, 3, 5), -5)");
xs = new double[] { 2, 3, 5 };
Test(Math.Pow(-xs.Average(), 4), "Math.Pow(-average(2, 3, 5), 4)");
Test(42, "42");
Test(-42, "-42");
Test(-Math.PI, "-pi");
Test(1 + 1 - Math.PI, "1 + 1 - Math.PI");
Test(-Math.E + 1 - Math.PI, "-e + 1 - Math.PI");
Test(-1 + 1 - Math.PI, "-1 + 1 - Math.PI");
Test(-2 + Math.PI - Math.Ceiling(3.2), "-2 + Math.PI - Math.Ceiling(3.2)");
xs = new double[] { 2, 3, 5 };
Test(xs.Average(), "IEnumerable.Average(2, 3, 5)");
xs = new double[] { 2, 35, 5 };
Test(xs.Max(), "Max(2, 35, 5)");
Test(1 - 2 - 3, "1 - 2 - 3");
Test(1 - 2 * 3, "1 - 2 * 3");
Test((1 - 2) * 3, "(1 - 2) * 3");
Test(1 - Math.Log(10) * 3, "1 - Math.Log(10) * 3");
Test((1 - Math.Pow(2, (1 + 2) * 3)) * 3, "(1 - Math.Pow(2, (1 + 2) * 3)) * 3");
xs = new double[] { 2, 9, 5 };
Test((1 - xs.Average()) * 3, "(1 - IEnumerable.Average(2, (1 + 2) * 3, 5)) * 3");
Test(Math.BitIncrement(2.5) + 7.1 * Math.Floor(-7.4), "bitincrement(2.5) + 7.1 * floor(-7.4)");
Test(20e+3, "20e+3");
Test(23 + 2e-13 * 2.3, "23 + 2e-13 * 2.3");
Test(23 - 10e-3, "23 - 10e-3");
Test(19e-11 / -12, "19e-11 /- 12");
xss = new double[] { 4, 5, 1 };
xs = new double[] { 2, (1 + 2) * 3, 23, xss.Last(), 3 };
Test((1 - xs.Average()) * 3, "(1 - average(2, (1 + 2) * 3, 23, last(4, 5, 1), 3)) * 3");
xss = new double[] { 4, 5, 1 };
xs = new double[] { 2, 9, 23, xss.Last(), 3 };
Test((1 - xs.Average()) * 3, "(1 - average(2, 9, 23, last(4, 5, 1), 3)) * 3");
xs = new double[] { 1, 2 };
xss = new double[] { 4, xs.Last(), 5 };
Test(xss.Last(), "last(4, last(1, 2), 5)");
Test(Percentage(921.315 * -20.93, 34.567), "921.315 * -20.93 % 34.567");
Test(921.315 * (-20.93 % 34.567), "921.315 * mod(-20.93, 34.567)");
Test(Percentage(25, 200), "25%200");
Test(Percentage(200, 25), "200%25");
Test(1.6 * -6.7 / -9.6, "1.6*-6.7/- 9.6");
Test(9 >> (int)(3 / +1.2), " 9>>3 /+ 1.2");
Test(4 >> 8 >> 1, " 4 >> 8 >> 1");
Test(+(int)(5.7 * 4) << 6, " +5.7 *4<<6");
Test((int)(6.7 / +3) >> 1, "6.7 /+ 3>> 1");
Test(-5 << 3, "- 5 << 3");
Test(4/.2, "4/.2");
Test((int)-.5 << 3, "-.5 << 3");
xs = new double[] { 1, 2 };
xss = new double[] { .4, xs.Last(), 5 };
Test(xss.Last(), "last(.4, last(1, 2), 5)");
xs = new double[] { 1, 2 };
xss = new double[] { 4, xs.Last(), .5 };
Test(xss.Last(), "last(4, last(1, 2), .5)");
Test(xss.Last(), "last(4., last(1, 2), .5)");
Test(xss.Last(), "last(4, last(1, 2.), .5)");
Test(7.9 / -0, "7.9/-0", "Lambda function makes this negative ");
Test(Math.Log(-42), "Math.Log(-42)", "Nan is always false ");
Test(Math.Pow(4, 7), "4 ^ 7");
Test((1 - Math.Pow(2, (1 + 2) * 3)) * 3, "(1 - (2 ^ ((1 + 2) * 3))) * 3");
Test(Math.Pow(-new double[] { 2, 3, 5 }.Average(), -5), "(-average(2, 3, 5)^ -5)");
Test(Math.Pow(-new double[] { 2, 3, 5 }.Average(), 4), "(-average(2, 3, 5)^ 4)");
Test(Factorial(5), "5!");
Test(Factorial(0), "0!");
Test(Factorial(1), "1!");
Test(-Factorial(0), "-0!");
Test(-Factorial(5), "-5!");
Test(Factorial(15), "15!");
Test(1 + 1 - Factorial((int)Math.PI), "1 + 1 - Math.PI!");
Test(-2 + Math.PI - Factorial((int)Math.Ceiling(3.2)), "-2 + Math.PI - Math.Ceiling(3.2)!");
Test(23 + Factorial((int)2e-13) * 2.3, "23 + 2e-13! * 2.3");
// NOTE(LucasTA): This is valid c# scientific notation WTF!
Test(.2e3, ".2e3");
// TODO(LucasTA): try fuzzing to catch more edge cases
Separator("[Should error properly]");
Separator();
TestExceptions("Lack of operands!", "-");
TestExceptions("Lack of operands!", "+");
TestExceptions("Lack of operands!", "(9)+");
TestExceptions("Invalid function!", "avg(2, 3, 5)");
TestExceptions("Invalid variable!", "average(2, pie, 5)");
TestExceptions("Closing unexisting paren!", "6 +3) /5-+8%6 / 8 ^5 ^4 * 2*+1");
TestExceptions("Opened paren is not closed!", "(1 - Math.Pow(2, (1 + 2) * 3) * 3");
TestExceptions("Not closing early opened function!", "1 - Math.Pow(2, (1 + 2) * 3 * 3");
TestExceptions("Invalid number", "2e +10");
TestExceptions("'$' is not a valid character!", "(1 - Math.Pow($, (1 + 2)))");
TestExceptions("More arguments than supported!", "Math.Pow(8, 4, -2, 5, 4)");
TestExceptions("Empty parens!", "last()");
TestExceptions("Empty parens!", "4 /+ 2 * last() + 3");
TestExceptions("Less arguments than supported!", "Math.Pow(8)");
TestExceptions("Evaluating empty string ", "");
TestExceptions("Evaluating null string ", null);
TestExceptions("Invalid number!", "4.2.0");
TestExceptions("Invalid number!", "4..");
TestExceptions("Lack of operator after function!", "Math.Pow(8, 7)6");
TestExceptions("Invalid variable!", "pi7");
TestExceptions("Invalid variable!", "last(8, pi7)");
TestExceptions("Lack of operator", "last(9 pi, 7)");
TestExceptions("Invalid variable!", "last(9, e9)");
TestExceptions("Lack of operator", "last(9, e 9)");
TestExceptions("Lack of operator", "pi last(9, 7)");
TestExceptions("Lack of operator", "pi (last(9, 7))");
TestExceptions("Lack of operator", "8 last(e, 7)");
TestExceptions("Lack of operator after function!", "Math.Pow(8, 7)(6)");
TestExceptions("Lack of operator after function!", "Math.Pow(8, 7) (6)");
TestExceptions("Lack of operator after closed pared", "(pi)Math.Pow(8, 7) + 6");
TestExceptions("Lack of operator after closed pared", "(pi) Math.Pow(8, 7) + 6");
TestExceptions("Empty parens", "()Math.Pow(8, 7)6");
TestExceptions("Closing unexisting paren", ")Math.Pow(8, 7)6");
TestExceptions("Lack of operator after function!", "(Math.Pow(8, 7)6");
TestExceptions("Invalid number", ".t3");
TestExceptions("Invalid number", ".e3");
TestExceptions("Invalid number", ".23s3");
TestExceptions("Invalid number", "8e");
TestExceptions("Invalid number", "8e + pi7");
TestExceptions("Invalid number", "last(8e, 7)");
TestExceptions("Invalid number", "last(9pi, 7)");
TestExceptions("Invalid comma", "(pi, 7)");
TestExceptions("Invalid comma", "(pi , 7)");
TestExceptions("Invalid comma", "(pi ,7)");
Separator("[GENERATED]");
Separator();