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