2024-07-03 11:39:24 +02:00
|
|
|
package tests_test
|
2024-06-29 21:40:14 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2024-07-03 11:39:24 +02:00
|
|
|
"path/filepath"
|
2024-07-18 21:10:27 +02:00
|
|
|
"strings"
|
2024-06-29 21:40:14 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"git.akyoto.dev/cli/q/src/build"
|
2024-08-07 19:39:10 +02:00
|
|
|
"git.akyoto.dev/cli/q/src/config"
|
2024-06-29 21:40:14 +02:00
|
|
|
"git.akyoto.dev/go/assert"
|
|
|
|
)
|
|
|
|
|
2024-07-03 11:59:36 +02:00
|
|
|
var programs = []struct {
|
2024-07-18 21:10:27 +02:00
|
|
|
Name string
|
|
|
|
ExitCode int
|
2024-07-03 11:59:36 +02:00
|
|
|
}{
|
2025-02-22 12:54:23 +01:00
|
|
|
{"empty", 0},
|
|
|
|
{"assert", 1},
|
|
|
|
{"binary", 0},
|
|
|
|
{"octal", 0},
|
|
|
|
{"hexadecimal", 0},
|
|
|
|
{"variables", 0},
|
|
|
|
{"reassign", 0},
|
|
|
|
{"reuse", 0},
|
|
|
|
{"return", 0},
|
|
|
|
{"return-multi", 0},
|
|
|
|
{"math", 0},
|
|
|
|
{"precedence", 0},
|
2025-02-22 13:58:07 +01:00
|
|
|
{"operator-number", 0},
|
|
|
|
{"operator-variable", 0},
|
|
|
|
{"operator-assign-number", 0},
|
|
|
|
{"operator-assign-variable", 0},
|
2025-02-22 12:54:23 +01:00
|
|
|
{"bitwise-and", 0},
|
|
|
|
{"bitwise-or", 0},
|
|
|
|
{"bitwise-xor", 0},
|
|
|
|
{"shift", 0},
|
|
|
|
{"modulo", 0},
|
|
|
|
{"modulo-assign", 0},
|
|
|
|
{"div-split", 0},
|
2025-02-25 17:06:46 +01:00
|
|
|
{"const", 0},
|
|
|
|
{"escape-rune", 0},
|
|
|
|
{"escape-string", 0},
|
2025-02-22 12:54:23 +01:00
|
|
|
{"int64", 0},
|
|
|
|
{"negative", 0},
|
|
|
|
{"negation", 0},
|
|
|
|
{"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},
|
|
|
|
{"branch-save", 0},
|
|
|
|
{"jump-near", 0},
|
|
|
|
{"switch", 0},
|
|
|
|
{"loop-infinite", 0},
|
|
|
|
{"loop-lifetime", 0},
|
|
|
|
{"loop-for", 0},
|
|
|
|
{"memory-free", 0},
|
|
|
|
{"out-of-memory", 0},
|
|
|
|
{"index-static", 0},
|
|
|
|
{"index-dynamic", 0},
|
|
|
|
{"struct", 0},
|
|
|
|
{"len", 0},
|
2024-07-04 12:32:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrograms(t *testing.T) {
|
|
|
|
for _, test := range programs {
|
2024-07-29 18:03:15 +02:00
|
|
|
file := filepath.Join("programs", test.Name+".q")
|
2024-07-29 14:44:16 +02:00
|
|
|
|
2024-07-29 18:03:15 +02:00
|
|
|
t.Run(test.Name+"/debug", func(t *testing.T) {
|
2025-02-17 23:36:11 +01:00
|
|
|
config.Optimize(false)
|
2025-02-22 12:54:23 +01:00
|
|
|
run(t, file, "debug", "", "", test.ExitCode)
|
2024-07-29 18:03:15 +02:00
|
|
|
})
|
2024-07-29 14:44:16 +02:00
|
|
|
|
2024-07-29 18:03:15 +02:00
|
|
|
t.Run(test.Name+"/release", func(t *testing.T) {
|
2025-02-17 23:36:11 +01:00
|
|
|
config.Optimize(true)
|
2025-02-22 12:54:23 +01:00
|
|
|
run(t, file, "release", "", "", test.ExitCode)
|
2024-07-29 14:44:16 +02:00
|
|
|
})
|
|
|
|
}
|
2024-07-03 11:59:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkPrograms(b *testing.B) {
|
|
|
|
for _, test := range programs {
|
|
|
|
b.Run(test.Name, func(b *testing.B) {
|
2024-07-09 17:00:04 +02:00
|
|
|
compiler := build.New(filepath.Join("programs", test.Name+".q"))
|
2024-07-03 11:59:36 +02:00
|
|
|
|
2025-02-14 16:46:36 +01:00
|
|
|
for b.Loop() {
|
2024-07-03 11:59:36 +02:00
|
|
|
_, err := compiler.Run()
|
|
|
|
assert.Nil(b, err)
|
|
|
|
}
|
|
|
|
})
|
2024-06-29 21:40:14 +02:00
|
|
|
}
|
2024-07-03 11:59:36 +02:00
|
|
|
}
|
2024-06-29 21:40:14 +02:00
|
|
|
|
2024-07-03 11:39:24 +02:00
|
|
|
// run builds and runs the file to check if the output matches the expected output.
|
2024-08-14 13:41:22 +02:00
|
|
|
func run(t *testing.T, path string, version string, input string, expectedOutput string, expectedExitCode int) {
|
2024-07-29 18:03:15 +02:00
|
|
|
b := build.New(path)
|
2024-07-09 12:13:59 +02:00
|
|
|
result, err := b.Run()
|
|
|
|
assert.Nil(t, err)
|
2024-06-29 21:40:14 +02:00
|
|
|
|
2024-08-14 13:41:22 +02:00
|
|
|
tmpDir := filepath.Join(os.TempDir(), "q", version)
|
|
|
|
err = os.MkdirAll(tmpDir, 0755)
|
2024-07-29 18:03:15 +02:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2024-08-14 13:41:22 +02:00
|
|
|
executable := b.Executable()
|
|
|
|
executable = filepath.Join(tmpDir, filepath.Base(executable))
|
2024-07-29 18:03:15 +02:00
|
|
|
err = result.WriteFile(executable)
|
2024-07-09 12:13:59 +02:00
|
|
|
assert.Nil(t, err)
|
2024-06-29 21:40:14 +02:00
|
|
|
|
2024-07-29 18:03:15 +02:00
|
|
|
stat, err := os.Stat(executable)
|
2024-07-09 12:13:59 +02:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.True(t, stat.Size() > 0)
|
2024-06-29 21:40:14 +02:00
|
|
|
|
2024-07-29 18:03:15 +02:00
|
|
|
cmd := exec.Command(executable)
|
2024-07-18 21:10:27 +02:00
|
|
|
cmd.Stdin = strings.NewReader(input)
|
2024-07-09 12:13:59 +02:00
|
|
|
output, err := cmd.Output()
|
|
|
|
exitCode := 0
|
2024-06-29 21:40:14 +02:00
|
|
|
|
2024-07-09 12:13:59 +02:00
|
|
|
if err != nil {
|
|
|
|
exitError, ok := err.(*exec.ExitError)
|
2024-06-29 21:40:14 +02:00
|
|
|
|
2024-07-09 12:13:59 +02:00
|
|
|
if !ok {
|
|
|
|
t.Fatal(exitError)
|
2024-06-29 21:40:14 +02:00
|
|
|
}
|
|
|
|
|
2024-07-09 12:13:59 +02:00
|
|
|
exitCode = exitError.ExitCode()
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, exitCode, expectedExitCode)
|
|
|
|
assert.DeepEqual(t, string(output), expectedOutput)
|
2024-06-29 21:40:14 +02:00
|
|
|
}
|