125 lines
2.9 KiB
Go
Raw Normal View History

2024-07-03 09:39:24 +00:00
package tests_test
2024-06-29 19:40:14 +00:00
import (
"os"
"os/exec"
2024-07-03 09:39:24 +00:00
"path/filepath"
2024-07-18 19:10:27 +00:00
"strings"
2024-06-29 19:40:14 +00:00
"testing"
"git.akyoto.dev/cli/q/src/build"
2024-07-29 12:44:16 +00:00
"git.akyoto.dev/cli/q/src/build/config"
2024-06-29 19:40:14 +00:00
"git.akyoto.dev/go/assert"
)
2024-07-03 09:59:36 +00:00
var programs = []struct {
2024-07-18 19:10:27 +00:00
Name string
Input string
Output string
ExitCode int
2024-07-03 09:59:36 +00:00
}{
2024-07-18 19:10:27 +00:00
{"empty", "", "", 0},
2024-07-28 16:47:13 +00:00
{"assert", "", "", 1},
2024-07-30 13:46:47 +00:00
{"variables", "", "", 0},
2024-07-28 16:47:13 +00:00
{"reassign", "", "", 0},
2024-07-30 13:46:47 +00:00
{"reuse", "", "", 0},
2024-07-28 16:47:13 +00:00
{"return", "", "", 0},
2024-07-31 15:50:31 +00:00
{"math", "", "", 0},
{"precedence", "", "", 0},
{"binary", "", "", 0},
{"octal", "", "", 0},
{"hexadecimal", "", "", 0},
2024-07-31 15:50:31 +00:00
{"escape-rune", "", "", 0},
{"escape-string", "", "", 0},
2024-07-25 12:17:51 +00:00
{"bitwise-and", "", "", 0},
{"bitwise-or", "", "", 0},
{"bitwise-xor", "", "", 0},
2024-07-25 13:47:19 +00:00
{"shift", "", "", 0},
2024-07-28 16:12:42 +00:00
{"modulo", "", "", 0},
{"modulo-assign", "", "", 0},
{"div-split", "", "", 0},
2024-07-31 13:27:20 +00:00
{"64-bit", "", "", 0},
2024-07-28 19:09:42 +00:00
{"negative", "", "", 0},
{"negation", "", "", 0},
2024-07-28 16:47:13 +00:00
{"square-sum", "", "", 0},
{"chained-calls", "", "", 0},
{"nested-calls", "", "", 0},
{"param", "", "", 0},
{"param-multi", "", "", 0},
{"param-order", "", "", 0},
{"branch", "", "", 0},
{"branch-and", "", "", 0},
{"branch-or", "", "", 0},
{"branch-both", "", "", 0},
{"jump-near", "", "", 0},
{"loop", "", "", 0},
{"loop-lifetime", "", "", 0},
2024-07-31 13:24:21 +00:00
{"out-of-memory", "", "", 0},
2024-07-04 10:32:56 +00:00
}
func TestPrograms(t *testing.T) {
for _, test := range programs {
2024-07-29 16:03:15 +00:00
file := filepath.Join("programs", test.Name+".q")
2024-07-29 12:44:16 +00:00
2024-07-29 16:03:15 +00:00
t.Run(test.Name+"/debug", func(t *testing.T) {
config.ConstantFold = false
run(t, file, "debug", test.Name, test.Input, test.Output, test.ExitCode)
})
2024-07-29 12:44:16 +00:00
2024-07-29 16:03:15 +00:00
t.Run(test.Name+"/release", func(t *testing.T) {
config.ConstantFold = true
run(t, file, "release", test.Name, test.Input, test.Output, test.ExitCode)
2024-07-29 12:44:16 +00:00
})
}
2024-07-03 09:59:36 +00:00
}
func BenchmarkPrograms(b *testing.B) {
for _, test := range programs {
b.Run(test.Name, func(b *testing.B) {
2024-07-09 15:00:04 +00:00
compiler := build.New(filepath.Join("programs", test.Name+".q"))
2024-07-03 09:59:36 +00:00
for i := 0; i < b.N; i++ {
_, err := compiler.Run()
assert.Nil(b, err)
}
})
2024-06-29 19:40:14 +00:00
}
2024-07-03 09:59:36 +00:00
}
2024-06-29 19:40:14 +00:00
2024-07-03 09:39:24 +00:00
// run builds and runs the file to check if the output matches the expected output.
2024-07-29 16:03:15 +00:00
func run(t *testing.T, path string, version string, name string, input string, expectedOutput string, expectedExitCode int) {
b := build.New(path)
2024-07-09 10:13:59 +00:00
result, err := b.Run()
assert.Nil(t, err)
2024-06-29 19:40:14 +00:00
2024-07-29 16:03:15 +00:00
directory := filepath.Join(os.TempDir(), "q", version)
err = os.MkdirAll(directory, 0755)
assert.Nil(t, err)
executable := filepath.Join(directory, name)
err = result.WriteFile(executable)
2024-07-09 10:13:59 +00:00
assert.Nil(t, err)
2024-06-29 19:40:14 +00:00
2024-07-29 16:03:15 +00:00
stat, err := os.Stat(executable)
2024-07-09 10:13:59 +00:00
assert.Nil(t, err)
assert.True(t, stat.Size() > 0)
2024-06-29 19:40:14 +00:00
2024-07-29 16:03:15 +00:00
cmd := exec.Command(executable)
2024-07-18 19:10:27 +00:00
cmd.Stdin = strings.NewReader(input)
2024-07-09 10:13:59 +00:00
output, err := cmd.Output()
exitCode := 0
2024-06-29 19:40:14 +00:00
2024-07-09 10:13:59 +00:00
if err != nil {
exitError, ok := err.(*exec.ExitError)
2024-06-29 19:40:14 +00:00
2024-07-09 10:13:59 +00:00
if !ok {
t.Fatal(exitError)
2024-06-29 19:40:14 +00:00
}
2024-07-09 10:13:59 +00:00
exitCode = exitError.ExitCode()
}
assert.Equal(t, exitCode, expectedExitCode)
assert.DeepEqual(t, string(output), expectedOutput)
2024-06-29 19:40:14 +00:00
}