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"
|
|
|
|
"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},
|
|
|
|
{"reuse", "", "", 0},
|
|
|
|
{"reassign", "", "", 0},
|
|
|
|
{"return", "", "", 0},
|
|
|
|
{"math", "", "", 0},
|
|
|
|
{"precedence", "", "", 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},
|
|
|
|
{"division-split", "", "", 0},
|
2024-07-27 15:48:03 +00:00
|
|
|
{"negative", "", "", 32},
|
2024-07-28 13:42:51 +00:00
|
|
|
{"negation", "", "", 32},
|
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-04 10:32:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrograms(t *testing.T) {
|
|
|
|
for _, test := range programs {
|
|
|
|
t.Run(test.Name, func(t *testing.T) {
|
2024-07-18 19:10:27 +00:00
|
|
|
run(t, filepath.Join("programs", test.Name+".q"), test.Input, test.Output, test.ExitCode)
|
2024-07-04 10:32:56 +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-18 19:10:27 +00:00
|
|
|
func run(t *testing.T, name string, input string, expectedOutput string, expectedExitCode int) {
|
2024-07-03 09:39:24 +00:00
|
|
|
b := build.New(name)
|
2024-06-29 19:40:14 +00:00
|
|
|
assert.True(t, len(b.Executable()) > 0)
|
|
|
|
|
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-09 10:13:59 +00:00
|
|
|
err = result.Write(b.Executable())
|
|
|
|
assert.Nil(t, err)
|
2024-06-29 19:40:14 +00:00
|
|
|
|
2024-07-09 10:13:59 +00:00
|
|
|
stat, err := os.Stat(b.Executable())
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.True(t, stat.Size() > 0)
|
2024-06-29 19:40:14 +00:00
|
|
|
|
2024-07-09 10:13:59 +00:00
|
|
|
cmd := exec.Command(b.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
|
|
|
}
|