58 lines
1.4 KiB
Go
Raw Normal View History

2024-07-03 11:39:24 +02:00
package tests_test
import (
"path/filepath"
"testing"
"git.akyoto.dev/cli/q/src/build"
2024-08-07 19:39:10 +02:00
"git.akyoto.dev/cli/q/src/config"
"git.akyoto.dev/go/assert"
2024-07-03 11:39:24 +02:00
)
2024-07-03 11:59:36 +02:00
var examples = []struct {
2024-07-18 21:10:27 +02:00
Name string
Input string
Output string
ExitCode int
2024-07-03 11:59:36 +02:00
}{
2024-07-31 17:50:31 +02:00
{"hello", "", "Hello\n", 0},
2024-07-30 16:36:33 +02:00
{"factorial", "", "120", 0},
{"fibonacci", "", "55", 0},
{"gcd", "", "21", 0},
2024-07-22 15:32:16 +02:00
{"array", "", "Hello", 0},
2024-07-29 18:03:15 +02:00
{"echo", "Echo", "Echo", 0},
2024-07-26 16:19:13 +02:00
{"itoa", "", "9223372036854775807", 0},
2024-07-31 00:03:16 +02:00
{"collatz", "", "6 3 10 5 16 8 4 2 1", 0},
2024-08-03 01:11:22 +02:00
{"fizzbuzz", "", "1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz", 0},
2024-08-04 16:17:33 +02:00
{"prime", "", "2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97", 0},
2024-07-03 11:59:36 +02:00
}
2024-07-03 11:39:24 +02:00
2024-07-03 11:59:36 +02:00
func TestExamples(t *testing.T) {
for _, test := range examples {
2024-07-29 18:03:15 +02:00
directory := filepath.Join("..", "examples", test.Name)
t.Run(test.Name+"/debug", func(t *testing.T) {
config.ConstantFold = false
2024-08-14 13:41:22 +02:00
run(t, directory, "debug", test.Input, test.Output, test.ExitCode)
2024-07-29 18:03:15 +02:00
})
t.Run(test.Name+"/release", func(t *testing.T) {
config.ConstantFold = true
2024-08-14 13:41:22 +02:00
run(t, directory, "release", test.Input, test.Output, test.ExitCode)
2024-07-03 11:39:24 +02:00
})
}
}
func BenchmarkExamples(b *testing.B) {
for _, test := range examples {
b.Run(test.Name, func(b *testing.B) {
compiler := build.New(filepath.Join("..", "examples", test.Name))
for i := 0; i < b.N; i++ {
_, err := compiler.Run()
assert.Nil(b, err)
}
})
}
}