-
Notifications
You must be signed in to change notification settings - Fork 552
/
Copy pathrun_test.go
206 lines (183 loc) · 4.22 KB
/
run_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package cl_test
import (
"bytes"
"io"
"log"
"os"
"os/exec"
"strconv"
"strings"
"sync/atomic"
"testing"
"github.com/goplus/gop/cl"
"github.com/goplus/gop/cl/cltest"
"github.com/goplus/gop/parser"
"github.com/goplus/gop/parser/fsx/memfs"
"github.com/goplus/gop/scanner"
)
var (
tmpDir string
tmpFileIdx int64
)
func init() {
home, err := os.Getwd()
check(err)
tmpDir = home + "/.gop/tmp/"
err = os.MkdirAll(tmpDir, 0755)
check(err)
}
func check(err error) {
if err != nil {
log.Panicln(err)
}
}
func checkWith(err error, stdout, stderr io.Writer) {
if err != nil {
fatalWith(err, stdout, stderr)
}
}
func fatalWith(err error, stdout, stderr io.Writer) {
if o, ok := getBytes(stdout, stderr); ok {
os.Stderr.Write(o.Bytes())
}
log.Panicln(err)
}
type iBytes interface {
Bytes() []byte
}
func getBytes(stdout, stderr io.Writer) (o iBytes, ok bool) {
if o, ok = stderr.(iBytes); ok {
return
}
o, ok = stdout.(iBytes)
return
}
func goRun(_ *testing.T, code []byte) string {
idx := atomic.AddInt64(&tmpFileIdx, 1)
infile := tmpDir + strconv.FormatInt(idx, 10) + ".go"
err := os.WriteFile(infile, []byte(code), 0666)
check(err)
var stdout, stderr bytes.Buffer
cmd := exec.Command("go", "run", infile)
cmd.Dir = tmpDir
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err = cmd.Run()
os.Remove(infile)
checkWith(err, &stdout, &stderr)
return stdout.String()
}
func genGo(t *testing.T, conf *cl.Config, gopcode string) []byte {
cl.SetDisableRecover(true)
defer cl.SetDisableRecover(false)
fs := memfs.SingleFile("/foo", "bar.gop", gopcode)
pkgs, err := parser.ParseFSDir(cltest.Conf.Fset, fs, "/foo", parser.Config{Mode: parser.ParseComments})
if err != nil {
scanner.PrintError(os.Stderr, err)
t.Fatal("ParseFSDir:", err)
}
pkg, err := cl.NewPackage("", pkgs["main"], conf)
if err != nil {
t.Fatal("NewPackage:", err)
}
var b bytes.Buffer
err = pkg.WriteTo(&b)
if err != nil {
t.Fatal("gogen.WriteTo failed:", err)
}
return b.Bytes()
}
func testRun(t *testing.T, gopcode, expected string) {
code := genGo(t, cltest.Conf, gopcode)
result := goRun(t, code)
if result != expected {
t.Fatalf("=> Result:\n%s\n=> Expected:\n%s\n", result, expected)
}
}
func testRunType(t *testing.T, typ, gopcodeT, expected string) {
gopcode := strings.ReplaceAll(gopcodeT, "$(type)", typ)
testRun(t, gopcode, expected)
}
// -----------------------------------------------------------------------------
const (
testType_inc_code, testType_inc_ret = `
{
var x $(type) = 1
var y = +x
x++
x+=10
println x, y
}
`, `12 1
`
testType_dec_code, testType_dec_ret = `
{
var x $(type) = 0
x--
println x
}
`, `-1
`
testType_init_code, testType_init_ret = `
{
var x $(type) = 1 << 65
var y = x >> 63
var z = x >> 65
println x
println y, z
}
`, `36893488147419103232
4 1
`
testType_twoval_code, testType_twoval_ret = `
{
var x $(type) = 1 << 65
var y = (x >> 2) - 1
v1, ok1 := int64(x)
v2, ok2 := int64(y)
println v1, ok1
println v2, ok2
}
`, `0 false
9223372036854775807 true
`
testType_cast_code, testType_cast_ret = `
{
println $(type)(1 << 65), $(type)()
}
`, `36893488147419103232 0
`
testType_printf_code, testType_printf_ret = `
{
var x $(type) = 1
printf "%4d\n", x
}
`, ` 1
`
testTypescanf_code, testType_scanf_ret = `
import "fmt"
{
var name string
var age $(type)
fmt.Sscanf("Kim is 22 years old", "%s is %d years old", &name, &age)
println name, age
}
`, `Kim 22
`
)
const (
testType_com_code, testType_com_ret = testType_inc_code + testType_init_code + testType_cast_code + testType_printf_code + testType_twoval_code,
testType_inc_ret + testType_init_ret + testType_cast_ret + testType_printf_ret + testType_twoval_ret
testType_fixint_code, testType_fixint_ret = testTypescanf_code + testType_com_code,
testType_scanf_ret + testType_com_ret
)
func TestUint128_run(t *testing.T) {
testRunType(t, "uint128", testType_fixint_code, testType_fixint_ret)
}
func TestInt128_run(t *testing.T) {
testRunType(t, "int128", testType_fixint_code+testType_dec_code, testType_fixint_ret+testType_dec_ret)
}
func TestBigint_run(t *testing.T) {
testRunType(t, "bigint", testType_com_code+testType_dec_code, testType_com_ret+testType_dec_ret)
}
// -----------------------------------------------------------------------------