-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval_test.go
More file actions
183 lines (176 loc) · 5.9 KB
/
Copy patheval_test.go
File metadata and controls
183 lines (176 loc) · 5.9 KB
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
package main
import (
"strings"
"testing"
)
// eval runs a whole program in a fresh interpreter and returns the result.
func evalSrc(t *testing.T, src string) (Value, error) {
t.Helper()
ast, err := Parse(src)
if err != nil {
return Value{}, err
}
return Eval(ast, NewInterp())
}
func TestEval(t *testing.T) {
tests := []struct{ src, want string }{
// arithmetic + precedence
{"2 + 3 * 4", "14"},
{"(2 + 3) * 4", "20"},
{"-5 + 2", "-3"},
{"10 / 4", "2.5"},
// comparisons + booleans
{"3 < 5", "true"},
{"2 + 3 == 5", "true"},
{"2 < 3 == true", "true"},
{"true == 1", "false"},
{"1 != 2", "true"},
// strings
{`"a" + "b"`, `"ab"`},
{`"n=" + 5`, `"n=5"`},
{`"x" == "x"`, "true"},
// variables + statements
{"x = 5; x + 1", "6"},
{"a = 1; b = 2; a + b", "3"},
// reversible updates
{"x = 5; x += 3", "8"},
{"x = 5; x -= 1", "4"},
{"x = 12; x ^= 10", "6"},
{"a = 1; b = 2; a <=> b; a", "2"},
// control flow
{"if 2 > 1 { 10 } else { 20 }", "10"},
{"if 1 > 2 { 10 } else { 20 }", "20"},
{"if false { 1 } else if true { 2 } else { 3 }", "2"},
{"i = 0; while i < 3 { i = i + 1 }; i", "3"},
{"i = 0; n = 3; from i == 0 { i += 1 } loop { i += 1 } until i == n; i", "3"},
// reversible if + assert (exit assertion holds)
{"x = 0; if true { x += 5 } else { x -= 1 } assert x == 5; x", "5"},
// reverse block round-trips
{"x = 0; x += 5; reverse { x += 5 }; x", "0"},
// procedures
{"x = 1; proc bump { x += 4 }; call bump; x", "5"},
{"x = 1; proc bump { x += 4 }; call bump; uncall bump; x", "1"},
// parameterized procedures (by-reference)
{"x = 3; y = 10; proc add(d, s) { d += s }; call add(x, y); x", "13"},
{"x = 3; y = 10; proc add(d, s) { d += s }; call add(x, y); uncall add(x, y); x", "3"},
{"x = 1; y = 2; proc sw(a, b) { a <=> b }; call sw(x, y); x", "2"},
// arrays
{"a = [10, 20, 30]; a", "[10, 20, 30]"},
{"a = [10, 20, 30]; a[1]", "20"},
{"a = [1, 2, 3]; a[0] += 5; a[0]", "6"},
{"a = [1, 2, 3]; a[2] ^= 1; a[2]", "2"},
{"a = [1, 2, 3]; a[0] <=> a[2]; a", "[3, 2, 1]"},
{"a = [1, 2, 3]; a[0] += 5; reverse { a[0] += 5 }; a[0]", "1"},
{"a = [[1, 2], [3, 4]]; a[1][0]", "3"},
{"a = [5, 6]; proc bump(arr) { arr[0] += 10 }; call bump(a); a[0]", "15"},
// local scope (Janus local / delocal)
{"x = 10; local t = 5; t += x; t", "15"},
{"x = 2; local t = 10; t -= x; t", "8"},
{"local t = 3; delocal t = 3; 7", "7"},
{"a = 7; reverse { local t = 0; t += a; delocal t = a }; a", "7"},
// logical operators
{"x = 5; x > 1 && x < 10", "true"},
{"x = 5; x > 10 || x == 5", "true"},
{"x = 5; !(x == 5)", "false"},
{"!false && true", "true"}, // ! binds tighter than &&
{"true || false && false", "true"}, // && binds tighter than ||
{"a = [1, 2, 3]; i = 5; i < 3 && a[i] > 0", "false"}, // short-circuit guards the index
}
for _, tc := range tests {
got, err := evalSrc(t, tc.src)
if err != nil {
t.Errorf("%q: unexpected error: %v", tc.src, err)
continue
}
if got.String() != tc.want {
t.Errorf("%q = %s, want %s", tc.src, got.String(), tc.want)
}
}
}
func TestEvalErrors(t *testing.T) {
tests := []struct{ src, msgContains string }{
{"1 / 0", "division by zero"},
{"true + 1", "needs numbers or a string"},
{"x", "undefined variable"},
{"if 1 { 2 }", "must be bool"},
{"x = 1.5; x ^= 1", "whole number"},
{"_ = 5", "cannot be assigned"},
{"x = 5; reverse { x = 9 }", "cannot reverse destructive assignment"},
{"if true { 1 } assert false", "exit assertion violated"},
{"from x == 0 { x += 1 } loop { x += 1 } until x == 1", "undefined variable"},
{"proc p { x = 1 }; uncall p", "cannot uncall"},
{"x = 1; proc add(d, s) { d += s }; call add(x, x)", "aliased argument"},
{"x = 1; y = 2; proc add(d, s) { d += s }; call add(x)", "takes 2 argument"},
{"a = [1, 2]; a[5]", "out of range"},
{"x = 5; x[0]", "cannot index number"},
{"y = 1; local y = 2", "already exists"},
{"local a = 5; delocal a = 6", "expected 6"},
{"delocal z = 0", "does not exist"},
{"true && 5", "must be bool"},
{"!5", "needs a bool"},
}
for _, tc := range tests {
_, err := evalSrc(t, tc.src)
if err == nil {
t.Errorf("%q: expected error containing %q, got nil", tc.src, tc.msgContains)
continue
}
if !strings.Contains(err.Error(), tc.msgContains) {
t.Errorf("%q: error %q does not contain %q", tc.src, err.Error(), tc.msgContains)
}
}
}
func TestInvertFormat(t *testing.T) {
tests := []struct{ src, want string }{
{"a += 5", "a -= 5"},
{"a -= 5", "a += 5"},
{"a ^= 3", "a ^= 3"}, // self-inverse
{"a <=> b", "a <=> b"}, // self-inverse
{"a += 5; b <=> c", "{ b <=> c; a -= 5 }"}, // reversed order
{"call p", "uncall p"},
{"if c { x += 1 } else { x -= 1 } assert d", "if d { x -= 1 } else { x += 1 } assert c"},
}
for _, tc := range tests {
ast, err := Parse(tc.src)
if err != nil {
t.Errorf("%q: parse error: %v", tc.src, err)
continue
}
inv, err := invert(ast)
if err != nil {
t.Errorf("%q: invert error: %v", tc.src, err)
continue
}
if got := format(inv); got != tc.want {
t.Errorf("invert(%q) = %q, want %q", tc.src, got, tc.want)
}
}
}
func TestAtomicRollback(t *testing.T) {
// A line that errors part-way must leave no mutation behind.
ip := NewInterp()
mustRun(t, ip, "a = 1")
cp := ip.checkpoint()
ast, _ := Parse("a += 5; a <=> b") // b undefined -> swap errors after a += 5
if _, err := Eval(ast, ip); err == nil {
t.Fatal("expected error")
}
ip.rollback(cp)
if v, _ := ip.get("a"); v.Num != 1 {
t.Errorf("after rollback a = %v, want 1", v.Num)
}
if len(ip.past) != 1 { // only the original `a = 1`
t.Errorf("history len = %d, want 1", len(ip.past))
}
}
// mustRun evaluates src into ip, failing the test on any error.
func mustRun(t *testing.T, ip *Interp, src string) {
t.Helper()
ast, err := Parse(src)
if err != nil {
t.Fatalf("parse %q: %v", src, err)
}
if _, err := Eval(ast, ip); err != nil {
t.Fatalf("eval %q: %v", src, err)
}
}