-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tests.cs
217 lines (185 loc) · 6.9 KB
/
Tests.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
namespace NetLMC;
using System.Diagnostics;
public static class Test
{
public static readonly Dictionary<string, Action<Interpreter.InterpreterState>> Tests = new()
{
{ "Polynomial", TestAllPolynomials },
};
public static int EvalPolynomial(int a, int b, int c, int x) => Math.Clamp(a + b * x + c * x * x, 0, 999);
public static IEnumerator<Tester.ExpectedAction> Polynomial(int a, int b, int c, int x)
{
yield return Tester.In(a);
yield return Tester.In(b);
yield return Tester.In(c);
yield return Tester.In(x);
yield return Tester.Out(EvalPolynomial(a, b, c, x));
}
public static void TestAllPolynomials(Interpreter.InterpreterState inState)
{
long totalPass = 0, totalFail = 0, totalCrash = 0, total = 0;
object ioLock = new();
var fails = new (int a, int b, int c, int x)[1024];
int failIndex = 0;
var w = Stopwatch.StartNew();
Parallel.For(0, 1_000_000, (int i, ParallelLoopState parallelState) =>
{
long numPass = 0, numFail = 0, numCrash = 0;
for (int j = 0; j < 1000; j++)
{
long total = totalPass + totalFail + totalCrash;
var rand = new Random(i * 1000 + j);
var state = inState;
int a = rand.Next(0, 999),
b = rand.Next(0, 999),
c = rand.Next(0, 999),
x = rand.Next(0, 999);
{
state.pc = 0;
state.calc = 0;
var res = Tester.RunTest(Polynomial(a, b, c, x), ref state);
switch (res)
{
case Tester.Result.PASS:
numPass++;
break;
case Tester.Result.FAIL:
numFail++;
int failIdx = Interlocked.Increment(ref failIndex);
if (failIdx < fails.Length) fails[failIdx] = (a, b, c, x);
lock (ioLock)
{
Console.WriteLine($"{a},{b},{c},{x}: {EvalPolynomial(a, b, c, x)}");
}
break;
case Tester.Result.CRASH:
numCrash++;
break;
}
}
}
Interlocked.Add(ref totalPass, numPass);
Interlocked.Add(ref totalFail, numFail);
Interlocked.Add(ref totalCrash, numCrash);
if (Interlocked.Add(ref total, 1000) % 1_000_000 == 0)
{
lock (ioLock)
{
Console.WriteLine($"Done: {total}, Fail: {totalFail}, Pass: {totalPass}, Remain: {1_000_000_000 - total}");
}
}
});
w.Stop();
Console.WriteLine($"Done in {w.Elapsed}");
Console.WriteLine($"Pass: {totalPass}, Fail: {totalFail}, Crash: {totalCrash}");
}
public static void RunTxtTests(Interpreter.InterpreterState inState, FileInfo testFile)
{
using var f = File.OpenRead(testFile.FullName);
var sr = new StreamReader(f);
string l;
bool allPassed = false;
while ((l = sr.ReadLine()) != null)
{
l = l.Trim();
if (!string.IsNullOrEmpty(l))
{
bool passed = RunTxtTest(l, ref inState, out string testName, out string msg);
Console.WriteLine($"{testName,-12} {(passed ? "PASS" : "FAIL")}: {msg}");
allPassed |= passed;
}
}
if (allPassed) Console.WriteLine("All tests passed");
else Console.WriteLine("Some tests failed");
}
private static bool RunTxtTest(string test, ref Interpreter.InterpreterState state, out string testName, out string msg)
{
string[] flds = test.Trim().Split(';');
if (flds.Length != 4)
{
msg = $"Unable to read test";
testName = "N/A";
return false;
}
testName = flds[0];
int[] inputs, outputs;
int maxInstr;
try
{
inputs = flds[1].Trim().Split(',').Select(x => int.Parse(x)).ToArray();
outputs = flds[2].Trim().Split(',').Select(x => int.Parse(x)).ToArray();
maxInstr = int.Parse(flds[3]);
}
catch (Exception e)
{
msg = $"Unable to read test: {e}";
return false;
}
var iface = new TxtTestInterface(inputs, outputs);
try
{
state.Reset();
int steps = Interpreter.Run(ref state, iface, maxInstr);
iface.Done();
msg = $"{steps} steps used.";
return true;
}
catch (TxtTestInterface.TestException exc)
{
msg = exc.Message;
}
catch (Exception e)
{
msg = $"Execution failed on instruction {state.pc}. Inner exception: {e}";
}
return false;
}
private class TxtTestInterface : IInterface
{
private readonly int[] inputs, outputs;
private int inIdx, outIdx;
public TxtTestInterface(int[] ins, int[] outs)
{
inputs = ins;
outputs = outs;
}
void IInterface.DebugLog(string v) => Console.WriteLine($"DEBUG: {v}");
int IInterface.Input()
{
int idx = inIdx++;
if (idx < inputs.Length) return inputs[idx];
throw new InputException(true);
}
void IInterface.Output(int s)
{
int idx = outIdx++;
if (idx < inputs.Length) AssertEqual(s, outputs[idx]);
else throw new OutputException(true);
}
public void Done()
{
if (inIdx != inputs.Length) throw new InputException(false);
if (outIdx != outputs.Length) throw new OutputException(false);
}
private static void AssertEqual(int got, int expected)
{
if (got != expected) throw new WrongOutputException(got, expected);
}
public abstract class TestException : Exception
{
public TestException(string message) : base(message) { }
}
public class WrongOutputException : TestException
{
public WrongOutputException(int got, int expected) : base($"Got output {got} when expecting {expected}") { }
}
public class InputException : Exception
{
public InputException(bool overflow) : base($"Program requested too {(overflow ? "many" : "few")} inputs") { }
}
public class OutputException : Exception
{
public OutputException(bool overflow) : base($"Program gave too {(overflow ? "many" : "few")} outputs") { }
}
}
}