Improved tests
This commit is contained in:
parent
ce74685231
commit
ae8e46de4d
@ -2,6 +2,7 @@ package compiler
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"git.akyoto.dev/cli/q/src/build/arch/x64"
|
||||
@ -83,24 +84,27 @@ func (r *Result) PrintInstructions() {
|
||||
})
|
||||
}
|
||||
|
||||
// Write writes an executable file to disk.
|
||||
func (r *Result) Write(path string) error {
|
||||
// Write writes the executable to the given writer.
|
||||
func (r *Result) Write(writer io.Writer) error {
|
||||
code, data := r.finalize()
|
||||
return write(path, code, data)
|
||||
return write(writer, code, data)
|
||||
}
|
||||
|
||||
// write writes an executable file to disk.
|
||||
func write(path string, code []byte, data []byte) error {
|
||||
// Write writes an executable file to disk.
|
||||
func (r *Result) WriteFile(path string) error {
|
||||
file, err := os.Create(path)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
buffer := bufio.NewWriter(file)
|
||||
executable := elf.New(code, data)
|
||||
executable.Write(buffer)
|
||||
buffer.Flush()
|
||||
err = r.Write(file)
|
||||
|
||||
if err != nil {
|
||||
file.Close()
|
||||
return err
|
||||
}
|
||||
|
||||
err = file.Close()
|
||||
|
||||
if err != nil {
|
||||
@ -109,3 +113,11 @@ func write(path string, code []byte, data []byte) error {
|
||||
|
||||
return os.Chmod(path, 0755)
|
||||
}
|
||||
|
||||
// write writes an executable file to the given writer.
|
||||
func write(writer io.Writer, code []byte, data []byte) error {
|
||||
buffer := bufio.NewWriter(writer)
|
||||
executable := elf.New(code, data)
|
||||
executable.Write(buffer)
|
||||
return buffer.Flush()
|
||||
}
|
||||
|
@ -74,5 +74,5 @@ func makeExecutable(b *build.Build) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
return result.Write(b.Executable())
|
||||
return result.WriteFile(b.Executable())
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"git.akyoto.dev/cli/q/src/build"
|
||||
"git.akyoto.dev/cli/q/src/build/config"
|
||||
"git.akyoto.dev/go/assert"
|
||||
)
|
||||
|
||||
@ -18,13 +19,22 @@ var examples = []struct {
|
||||
{"factorial", "", "", 120},
|
||||
{"fibonacci", "", "", 55},
|
||||
{"array", "", "Hello", 0},
|
||||
{"echo", "Echo", "Echo", 0},
|
||||
{"itoa", "", "9223372036854775807", 0},
|
||||
}
|
||||
|
||||
func TestExamples(t *testing.T) {
|
||||
for _, test := range examples {
|
||||
t.Run(test.Name, func(t *testing.T) {
|
||||
run(t, filepath.Join("..", "examples", test.Name), test.Input, test.Output, test.ExitCode)
|
||||
directory := filepath.Join("..", "examples", test.Name)
|
||||
|
||||
t.Run(test.Name+"/debug", func(t *testing.T) {
|
||||
config.ConstantFold = false
|
||||
run(t, directory, "debug", test.Name, test.Input, test.Output, test.ExitCode)
|
||||
})
|
||||
|
||||
t.Run(test.Name+"/release", func(t *testing.T) {
|
||||
config.ConstantFold = true
|
||||
run(t, directory, "release", test.Name, test.Input, test.Output, test.ExitCode)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -53,19 +53,17 @@ var programs = []struct {
|
||||
}
|
||||
|
||||
func TestPrograms(t *testing.T) {
|
||||
for _, test := range programs {
|
||||
file := filepath.Join("programs", test.Name+".q")
|
||||
|
||||
t.Run(test.Name+"/debug", func(t *testing.T) {
|
||||
config.ConstantFold = false
|
||||
|
||||
for _, test := range programs {
|
||||
t.Run(test.Name, func(t *testing.T) {
|
||||
run(t, filepath.Join("programs", test.Name+".q"), test.Input, test.Output, test.ExitCode)
|
||||
run(t, file, "debug", test.Name, test.Input, test.Output, test.ExitCode)
|
||||
})
|
||||
}
|
||||
|
||||
t.Run(test.Name+"/release", func(t *testing.T) {
|
||||
config.ConstantFold = true
|
||||
|
||||
for _, test := range programs {
|
||||
t.Run(test.Name+" (optimized)", func(t *testing.T) {
|
||||
run(t, filepath.Join("programs", test.Name+".q"), test.Input, test.Output, test.ExitCode)
|
||||
run(t, file, "release", test.Name, test.Input, test.Output, test.ExitCode)
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -84,21 +82,24 @@ func BenchmarkPrograms(b *testing.B) {
|
||||
}
|
||||
|
||||
// run builds and runs the file to check if the output matches the expected output.
|
||||
func run(t *testing.T, name string, input string, expectedOutput string, expectedExitCode int) {
|
||||
b := build.New(name)
|
||||
assert.True(t, len(b.Executable()) > 0)
|
||||
|
||||
func run(t *testing.T, path string, version string, name string, input string, expectedOutput string, expectedExitCode int) {
|
||||
b := build.New(path)
|
||||
result, err := b.Run()
|
||||
assert.Nil(t, err)
|
||||
|
||||
err = result.Write(b.Executable())
|
||||
directory := filepath.Join(os.TempDir(), "q", version)
|
||||
err = os.MkdirAll(directory, 0755)
|
||||
assert.Nil(t, err)
|
||||
|
||||
stat, err := os.Stat(b.Executable())
|
||||
executable := filepath.Join(directory, name)
|
||||
err = result.WriteFile(executable)
|
||||
assert.Nil(t, err)
|
||||
|
||||
stat, err := os.Stat(executable)
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, stat.Size() > 0)
|
||||
|
||||
cmd := exec.Command(b.Executable())
|
||||
cmd := exec.Command(executable)
|
||||
cmd.Stdin = strings.NewReader(input)
|
||||
output, err := cmd.Output()
|
||||
exitCode := 0
|
||||
|
Loading…
Reference in New Issue
Block a user