-
Notifications
You must be signed in to change notification settings - Fork 1
/
spil_test.go
94 lines (84 loc) · 1.97 KB
/
spil_test.go
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
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
)
func TestExamples(t *testing.T) {
inputs, err := filepath.Glob("examples/ex.*")
if err != nil {
panic(err)
}
for _, test := range inputs {
for _, bigint := range []bool{false, true} {
name := test
if bigint {
name += "-big"
}
t.Run(name, func(t *testing.T) {
dir, file := filepath.Split(test)
output := filepath.Join(dir, "out"+file[2:])
checkInterpreter(t, test, output, bigint)
})
}
}
}
func TestBuiltin(t *testing.T) {
inputs, err := filepath.Glob("examples/builtin.*")
if err != nil {
panic(err)
}
for _, test := range inputs {
for _, bigint := range []bool{false, true} {
name := test
if bigint {
name += "-big"
}
t.Run(name, func(t *testing.T) {
dir, file := filepath.Split(test)
output := filepath.Join(dir, "output."+file)
checkInterpreter(t, test, output, bigint)
})
}
}
}
func checkInterpreter(t *testing.T, input, output string, bigint bool) {
fin, err := os.Open(input)
if err != nil {
t.Fatalf("Cannot open input file: %v", err)
}
defer fin.Close()
expData, err := ioutil.ReadFile(output)
if os.IsNotExist(err) {
t.Skipf("No output file for %v found: %v", input, output)
}
if err != nil {
t.Fatalf("Reading output file failed: %v", err)
}
buffer := &strings.Builder{}
in := NewInterpreter(buffer)
in.UseBigInt(bigint)
inputPath, err := filepath.Abs(input)
if err != nil {
t.Fatalf("Abs(%v) failed: %v", input, err)
}
if err := run(in, inputPath, fin); err != nil {
t.Fatalf("Interpreter Run() failed: %v", err)
}
if act, exp := buffer.String(), string(expData); act != exp {
t.Errorf("Incorrect output for %v:\nexpected %q,\n actual %q", input, exp, act)
}
}
func run(i *Interpret, file string, input io.Reader) error {
if err := i.Parse(file, input); err != nil {
return err
}
if err := i.Check(); err != nil {
return fmt.Errorf("Check failed: %v", err)
}
return i.Run()
}