-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration_test.go
90 lines (77 loc) · 2.14 KB
/
integration_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
package tests
import (
"bytes"
"github.com/Moleus/comp-arch-lab3/pkg/isa"
"github.com/Moleus/comp-arch-lab3/pkg/machine"
translator2 "github.com/Moleus/comp-arch-lab3/pkg/translator"
"gopkg.in/yaml.v3"
"gotest.tools/v3/golden"
"os"
"strings"
"testing"
)
type GoldenContents struct {
TranslatorInput string `yaml:"translator_input"`
TranslatorOutput string `yaml:"translator_output"`
MachineInput string `yaml:"stdin"`
MachineStdout string `yaml:"stdout"`
MachineLog string `yaml:"log"`
}
func TestTranslationAndSimulation(t *testing.T) {
dir, err := os.ReadDir("testdata")
if err != nil {
t.Fatal(err)
}
for _, file := range dir {
t.Run(file.Name(), func(t *testing.T) {
goldenFile := file.Name()
runTest(t, goldenFile)
})
}
}
func parseGoldenFile(t *testing.T, filename string) GoldenContents {
inputContent, err := os.ReadFile("testdata/" + filename)
if err != nil {
t.Fatal(err)
}
input := GoldenContents{}
err = yaml.Unmarshal(inputContent, &input)
if err != nil {
t.Fatal(err)
}
return input
}
func runTest(t *testing.T, goldenFile string) {
goldenContents := parseGoldenFile(t, goldenFile)
translator := translator2.NewTranslator()
program, err := translator.Translate(goldenContents.TranslatorInput)
if err != nil {
t.Fatal(err)
}
serializedMachineCode, err := isa.SerializeCode(program)
if err != nil {
t.Fatal(err)
}
ioData, err := isa.ReadIoData(strings.NewReader(goldenContents.MachineInput))
if err != nil {
t.Fatal(err)
}
dataPathOutputBuffer := bytes.NewBuffer([]byte{})
controlUnitStateOutputBuffer := bytes.NewBuffer([]byte{})
err = machine.RunSimulation(ioData, program, dataPathOutputBuffer, controlUnitStateOutputBuffer)
if err != nil {
t.Fatal(err)
}
testOutput := GoldenContents{
TranslatorInput: goldenContents.TranslatorInput,
TranslatorOutput: string(serializedMachineCode),
MachineInput: goldenContents.MachineInput,
MachineStdout: dataPathOutputBuffer.String(),
MachineLog: controlUnitStateOutputBuffer.String(),
}
yamlOutput, err := yaml.Marshal(testOutput)
if err != nil {
t.Fatal(err)
}
golden.Assert(t, string(yamlOutput), goldenFile)
}