forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gno_test.go
368 lines (331 loc) · 6.43 KB
/
gno_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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package gno
import (
"bytes"
"fmt"
"reflect"
"testing"
"unsafe"
//"github.com/davecgh/go-spew/spew"
"github.com/jaekwon/testify/assert"
)
// run empty main().
func TestRunEmptyMain(t *testing.T) {
m := NewMachine("test", nil)
main := FuncD("main", nil, nil, nil)
m.RunDeclaration(main)
m.RunMain()
}
// run main() with a for loop.
func TestRunLoopyMain(t *testing.T) {
m := NewMachine("test", nil)
c := `package test
func main() {
for i:=0; i<1000; i++ {
if i == -1 {
return
}
}
}`
n := MustParseFile("main.go", c)
m.RunFiles(n)
m.RunMain()
}
func TestEval(t *testing.T) {
m := NewMachine("test", nil)
c := `package test
func next(i int) int {
return i+1
}`
n := MustParseFile("main.go", c)
m.RunFiles(n)
res := m.Eval(Call("next", "1"))
fmt.Println(res)
}
func assertOutput(t *testing.T, input string, output string) {
buf := new(bytes.Buffer)
m := NewMachineWithOptions(MachineOptions{
PkgPath: "test",
Output: buf,
})
n := MustParseFile("main.go", input)
m.RunFiles(n)
m.RunMain()
assert.Equal(t, string(buf.Bytes()), output)
err := m.CheckEmpty()
assert.Nil(t, err)
}
func TestRunMakeStruct(t *testing.T) {
assertOutput(t, `package test
type Outfit struct {
Scarf string
Shirt string
Belts string
Strap string
Pants string
Socks string
Shoes string
}
func main() {
s := Outfit {
// some fields are out of order.
// some fields are left unset.
Scarf:"scarf",
Shirt:"shirt",
Shoes:"shoes",
Socks:"socks",
}
// some fields out of order are used.
// some fields left unset are used.
print(s.Shoes+","+s.Shirt+","+s.Pants+","+s.Scarf)
}`, `shoes,shirt,,scarf`)
}
func TestRunReturnStruct(t *testing.T) {
assertOutput(t, `package test
type MyStruct struct {
FieldA string
FieldB string
}
func myStruct(a, b string) MyStruct {
return MyStruct{
FieldA: a,
FieldB: b,
}
}
func main() {
s := myStruct("aaa", "bbb")
print(s.FieldA+","+s.FieldB)
}`, `aaa,bbb`)
}
//----------------------------------------
// Benchmarks
func BenchmarkPreprocess(b *testing.B) {
for i := 0; i < b.N; i++ {
// stop timer
b.StopTimer()
pkg := &PackageNode{
PkgName: "main",
PkgPath: ".main",
FileSet: nil,
}
pkg.InitStaticBlock(pkg, nil)
main := FuncD("main", nil, nil, Ss(
A("mx", ":=", "1000000"),
For(
A("i", ":=", "0"),
X("i < mx"),
Inc("i"),
),
))
b.StartTimer()
// timer started
main = Preprocess(nil, pkg, main).(*FuncDecl)
}
}
func BenchmarkLoopyMain(b *testing.B) {
m := NewMachine("test", nil)
main := FuncD("main", nil, nil, Ss(
A("mx", ":=", "10000000"),
For(
A("i", ":=", "0"),
// X("i < 10000000"),
X("i < mx"),
Inc("i"),
),
))
m.RunDeclaration(main)
for i := 0; i < b.N; i++ {
m.RunMain()
}
}
//----------------------------------------
// Unsorted
type Struct1 struct {
A int
B int
}
func TestModifyTypeAsserted(t *testing.T) {
x := Struct1{1, 1}
var v interface{} = x
x2 := v.(Struct1)
x2.A = 2
// only x2 is changed.
assert.Equal(t, x.A, 1)
assert.Equal(t, v.(Struct1).A, 1)
assert.Equal(t, x2.A, 2)
}
type Interface1 interface {
Foo()
}
func TestTypeConversion(t *testing.T) {
x := 1
var v interface{} = x
if _, ok := v.(Interface1); ok {
panic("should not happen")
}
v = nil
if _, ok := v.(Interface1); ok {
panic("should not happen")
}
assert.Panics(t, func() {
// this would panic.
z := v.(Interface1)
fmt.Println(z)
})
}
func TestSomething(t *testing.T) {
type Foo struct {
X interface{}
}
type Bar struct {
X interface{}
Y bool
}
fmt.Println(unsafe.Sizeof(Foo{})) // 16
fmt.Println(unsafe.Sizeof(Foo{X: reflect.ValueOf(0)}.X)) // still 16? weird.
fmt.Println(unsafe.Sizeof(reflect.ValueOf(0))) // 24
fmt.Println(unsafe.Sizeof(Bar{}))
}
// XXX is there a way to test in Go as well as Gno?
func TestDeferOrder(t *testing.T) {
a := func() func(int, int) int {
fmt.Println("a constructed")
return func(x int, y int) int {
fmt.Println("a called")
return x + y
}
}
b := func() int {
fmt.Println("b constructed")
return 1
}
c := func() int {
fmt.Println("c constructed")
return 2
}
defer a()(b(), c())
fmt.Println("post defer")
// should print
// a constructed
// b constructed
// c constructed
// post defer
// a called
}
// XXX is there a way to test in Go as well as Gno?
func TestCallOrder(t *testing.T) {
a := func() func(int, int) int {
fmt.Println("a constructed")
return func(x int, y int) int {
fmt.Println("a called")
return x + y
}
}
b := func() int {
fmt.Println("b constructed")
return 1
}
c := func() int {
fmt.Println("c constructed")
return 2
}
a()(b(), c())
// should print
// a constructed
// b constructed
// c constructed
// a called
}
// XXX is there a way to test in Go as well as Gno?
func TestBinaryShortCircuit(t *testing.T) {
tr := func() bool {
fmt.Println("t called")
return true
}
fa := func() bool {
fmt.Println("f called")
return false
}
if fa() && tr() {
fmt.Println("error")
} else {
fmt.Println("done")
}
}
// XXX is there a way to test in Go as well as Gno?
func TestSwitchDefine(t *testing.T) {
var x interface{} = 1
switch y := x.(type) {
case int:
fmt.Println("int", y) // XXX
default:
fmt.Println("not int")
}
}
// XXX is there a way to test in Go as well as Gno?
func TestBinaryCircuit(t *testing.T) {
tr := func() bool {
fmt.Println("tr() called")
return true
}
fa := func() bool {
fmt.Println("fa() called")
return false
}
fmt.Println("case 1")
fmt.Println(fa() && tr())
fmt.Println("case 1")
fmt.Println(tr() || fa())
// should print
// case 1
// fa() called
// false
// case 1
// tr() called
// true
}
func TestMultiAssignment(t *testing.T) {
buf := make([]int, 4)
ref := func(i int) *int {
fmt.Printf("ref(%v) called\n", i)
return &buf[i]
}
val := func(i int) int {
fmt.Printf("val(%v) called\n", i)
return i
}
*ref(0), *ref(1), *ref(2), *ref(3) =
val(11), val(22), val(33), val(44)
/*
ref(0) called
ref(1) called
ref(2) called
ref(3) called
val(11) called
val(22) called
val(33) called
val(44) called
*/
}
// XXX is there a way to test in Go as well as Gno?
func TestCallLHS(t *testing.T) {
x := 1
xptr := func() *int {
return &x
}
*xptr() = 2
assert.Equal(t, x, 2)
}
// XXX is there a way to test in Go as well as Gno?
func TestCallFieldLHS(t *testing.T) {
type str struct {
X int
}
x := str{}
xptr := func() *str {
return &x
}
y := 0
xptr().X, y = 2, 3
assert.Equal(t, x.X, 2)
assert.Equal(t, y, 3)
}