-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tester.cs
87 lines (72 loc) · 2.39 KB
/
Tester.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
namespace NetLMC;
public static class Tester
{
public enum Result
{
PASS,
FAIL,
CRASH,
};
public struct ExpectedAction
{
public bool isInput;
public int value;
}
[Serializable]
private class TesterException : System.Exception
{
public TesterException() { }
public TesterException(string message) : base(message) { }
public TesterException(string message, System.Exception inner) : base(message, inner) { }
protected TesterException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
}
private class ValidatorInterface : IInterface
{
private readonly IEnumerator<ExpectedAction> actions;
public ValidatorInterface(IEnumerable<ExpectedAction> action)
{
this.actions = action.GetEnumerator();
}
public ValidatorInterface(IEnumerator<ExpectedAction> action)
{
this.actions = action;
}
public int Input()
{
if (!actions.MoveNext() || !actions.Current.isInput) { throw new TesterException("Unexpected input request."); }
return actions.Current.value;
}
public void Output(int v)
{
if (!actions.MoveNext() || actions.Current.isInput) { throw new TesterException("Unexpected output"); }
if (actions.Current.value != v) { throw new TesterException($"Output {v} != expected {actions.Current.value}"); }
}
public void DebugLog(string _) { }
public void CheckDone()
{
if (actions.MoveNext()) { throw new TesterException("Unexpected end of program."); }
}
}
public static Result RunTest(IEnumerator<ExpectedAction> actions, ref Interpreter.InterpreterState state)
{
var iface = new ValidatorInterface(actions);
try
{
Interpreter.Run(ref state, iface);
iface.CheckDone();
}
catch (TesterException)
{
return Result.FAIL;
}
catch
{
return Result.CRASH;
}
return Result.PASS;
}
public static ExpectedAction In(int v) => new() { isInput = true, value = v };
public static ExpectedAction Out(int v) => new() { isInput = false, value = v };
}