2019-12-23 14:05:34 +00:00
|
|
|
package compiler_test
|
2018-04-02 15:04:42 +00:00
|
|
|
|
|
|
|
import (
|
2022-01-16 12:58:51 +00:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2018-04-02 15:04:42 +00:00
|
|
|
"math/big"
|
2022-01-16 12:58:51 +00:00
|
|
|
"strings"
|
2019-08-20 17:37:06 +00:00
|
|
|
"testing"
|
2018-04-02 15:04:42 +00:00
|
|
|
|
2022-01-16 12:58:51 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/compiler"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
2020-06-03 12:55:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2022-01-16 12:58:51 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-04-02 15:04:42 +00:00
|
|
|
)
|
2018-02-19 09:24:28 +00:00
|
|
|
|
|
|
|
var structTestCases = []testCase{
|
|
|
|
{
|
|
|
|
"struct field assign",
|
2022-01-16 12:58:51 +00:00
|
|
|
`func F%d() int {
|
|
|
|
t := token1 {
|
2018-02-19 09:24:28 +00:00
|
|
|
x: 2,
|
|
|
|
y: 4,
|
|
|
|
}
|
|
|
|
|
|
|
|
age := t.x
|
|
|
|
return age
|
|
|
|
}
|
2018-02-24 09:06:48 +00:00
|
|
|
|
2022-01-16 12:58:51 +00:00
|
|
|
type token1 struct {
|
2018-02-24 09:06:48 +00:00
|
|
|
x int
|
|
|
|
y int
|
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
`,
|
2018-04-02 15:04:42 +00:00
|
|
|
big.NewInt(2),
|
2018-02-19 09:24:28 +00:00
|
|
|
},
|
2020-09-24 17:29:52 +00:00
|
|
|
{
|
|
|
|
"struct field from func result",
|
2022-01-16 12:58:51 +00:00
|
|
|
`type S struct { x int }
|
2020-09-24 17:29:52 +00:00
|
|
|
func fn() int { return 2 }
|
2022-01-16 12:58:51 +00:00
|
|
|
func F%d() int {
|
2020-09-24 17:29:52 +00:00
|
|
|
t := S{x: fn()}
|
|
|
|
return t.x
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
big.NewInt(2),
|
|
|
|
},
|
2018-02-19 09:24:28 +00:00
|
|
|
{
|
|
|
|
"struct field return",
|
2022-01-16 12:58:51 +00:00
|
|
|
`type token2 struct {
|
2018-02-19 09:24:28 +00:00
|
|
|
x int
|
|
|
|
y int
|
|
|
|
}
|
|
|
|
|
2022-01-16 12:58:51 +00:00
|
|
|
func F%d() int {
|
|
|
|
t := token2 {
|
2018-02-19 09:24:28 +00:00
|
|
|
x: 2,
|
|
|
|
y: 4,
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.x
|
|
|
|
}
|
|
|
|
`,
|
2018-04-02 15:04:42 +00:00
|
|
|
big.NewInt(2),
|
2018-02-19 09:24:28 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"struct field assign",
|
2022-01-16 12:58:51 +00:00
|
|
|
`type token3 struct {
|
2018-02-19 09:24:28 +00:00
|
|
|
x int
|
|
|
|
y int
|
|
|
|
}
|
|
|
|
|
2022-01-16 12:58:51 +00:00
|
|
|
func F%d() int {
|
|
|
|
t := token3 {
|
2018-02-19 09:24:28 +00:00
|
|
|
x: 2,
|
|
|
|
y: 4,
|
|
|
|
}
|
|
|
|
t.x = 10
|
|
|
|
return t.x
|
|
|
|
}
|
|
|
|
`,
|
2018-04-02 15:04:42 +00:00
|
|
|
big.NewInt(10),
|
2018-02-19 09:24:28 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"complex struct",
|
2022-01-16 12:58:51 +00:00
|
|
|
`type token4 struct {
|
2018-02-19 09:24:28 +00:00
|
|
|
x int
|
|
|
|
y int
|
|
|
|
}
|
|
|
|
|
2022-01-16 12:58:51 +00:00
|
|
|
func F%d() int {
|
2018-02-19 09:24:28 +00:00
|
|
|
x := 10
|
|
|
|
|
2022-01-16 12:58:51 +00:00
|
|
|
t := token4 {
|
2018-02-19 09:24:28 +00:00
|
|
|
x: 2,
|
|
|
|
y: 4,
|
|
|
|
}
|
|
|
|
|
|
|
|
y := x + t.x
|
|
|
|
|
|
|
|
return y
|
|
|
|
}
|
|
|
|
`,
|
2018-04-02 15:04:42 +00:00
|
|
|
big.NewInt(12),
|
2018-02-19 09:24:28 +00:00
|
|
|
},
|
2020-02-11 08:10:51 +00:00
|
|
|
{
|
|
|
|
"initialize struct field from variable",
|
2022-01-16 12:58:51 +00:00
|
|
|
`type token5 struct {
|
2020-02-11 08:10:51 +00:00
|
|
|
x int
|
|
|
|
y int
|
|
|
|
}
|
|
|
|
|
2022-01-16 12:58:51 +00:00
|
|
|
func F%d() int {
|
2020-02-11 08:10:51 +00:00
|
|
|
x := 10
|
2022-01-16 12:58:51 +00:00
|
|
|
t := token5 {
|
2020-02-11 08:10:51 +00:00
|
|
|
x: x,
|
|
|
|
y: 4,
|
|
|
|
}
|
|
|
|
y := t.x + t.y
|
|
|
|
return y
|
2022-01-16 12:58:51 +00:00
|
|
|
}
|
|
|
|
`,
|
2020-02-11 08:10:51 +00:00
|
|
|
big.NewInt(14),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"assign a variable to a struct field",
|
2022-01-16 12:58:51 +00:00
|
|
|
`type token6 struct {
|
2020-02-11 08:10:51 +00:00
|
|
|
x int
|
|
|
|
y int
|
|
|
|
}
|
|
|
|
|
2022-01-16 12:58:51 +00:00
|
|
|
func F%d() int {
|
2020-02-11 08:10:51 +00:00
|
|
|
ten := 10
|
2022-01-16 12:58:51 +00:00
|
|
|
t := token6 {
|
2020-02-11 08:10:51 +00:00
|
|
|
x: 2,
|
|
|
|
y: 4,
|
|
|
|
}
|
|
|
|
t.x = ten
|
|
|
|
y := t.y + t.x
|
|
|
|
return y
|
2022-01-16 12:58:51 +00:00
|
|
|
}
|
|
|
|
`,
|
2020-02-11 08:10:51 +00:00
|
|
|
big.NewInt(14),
|
|
|
|
},
|
2020-05-18 08:45:20 +00:00
|
|
|
{
|
|
|
|
"increase struct field with +=",
|
2022-01-16 12:58:51 +00:00
|
|
|
`type token7 struct { x int }
|
|
|
|
func F%d() int {
|
|
|
|
t := token7{x: 2}
|
2020-05-18 08:45:20 +00:00
|
|
|
t.x += 3
|
|
|
|
return t.x
|
2022-01-16 12:58:51 +00:00
|
|
|
}
|
|
|
|
`,
|
2020-05-18 08:45:20 +00:00
|
|
|
big.NewInt(5),
|
|
|
|
},
|
2020-02-11 08:10:51 +00:00
|
|
|
{
|
|
|
|
"assign a struct field to a struct field",
|
2022-01-16 12:58:51 +00:00
|
|
|
`type token8 struct {
|
2020-02-11 08:10:51 +00:00
|
|
|
x int
|
|
|
|
y int
|
|
|
|
}
|
|
|
|
|
2022-01-16 12:58:51 +00:00
|
|
|
func F%d() int {
|
|
|
|
t1 := token8 {
|
2020-02-11 08:10:51 +00:00
|
|
|
x: 2,
|
|
|
|
y: 4,
|
|
|
|
}
|
2022-01-16 12:58:51 +00:00
|
|
|
t2 := token8 {
|
2020-02-11 08:10:51 +00:00
|
|
|
x: 3,
|
|
|
|
y: 5,
|
|
|
|
}
|
|
|
|
t1.x = t2.y
|
|
|
|
y := t1.x + t2.x
|
|
|
|
return y
|
2022-01-16 12:58:51 +00:00
|
|
|
}
|
|
|
|
`,
|
2020-02-11 08:10:51 +00:00
|
|
|
big.NewInt(8),
|
|
|
|
},
|
2018-02-19 09:24:28 +00:00
|
|
|
{
|
|
|
|
"initialize same struct twice",
|
2022-01-16 12:58:51 +00:00
|
|
|
`type token9 struct {
|
2018-02-19 09:24:28 +00:00
|
|
|
x int
|
|
|
|
y int
|
|
|
|
}
|
|
|
|
|
2022-01-16 12:58:51 +00:00
|
|
|
func F%d() int {
|
|
|
|
t1 := token9 {
|
2018-02-19 09:24:28 +00:00
|
|
|
x: 2,
|
|
|
|
y: 4,
|
|
|
|
}
|
2022-01-16 12:58:51 +00:00
|
|
|
t2 := token9 {
|
2018-02-19 09:24:28 +00:00
|
|
|
x: 2,
|
|
|
|
y: 4,
|
|
|
|
}
|
|
|
|
return t1.x + t2.y
|
|
|
|
}
|
|
|
|
`,
|
2018-04-02 15:04:42 +00:00
|
|
|
big.NewInt(6),
|
2018-02-19 09:24:28 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"struct methods",
|
2022-01-16 12:58:51 +00:00
|
|
|
`type token10 struct {
|
2018-02-19 09:24:28 +00:00
|
|
|
x int
|
|
|
|
}
|
|
|
|
|
2022-01-16 12:58:51 +00:00
|
|
|
func(t token10) getInteger() int {
|
2018-02-19 09:24:28 +00:00
|
|
|
return t.x
|
|
|
|
}
|
|
|
|
|
2022-01-16 12:58:51 +00:00
|
|
|
func F%d() int {
|
|
|
|
t := token10 {
|
2018-02-19 09:24:28 +00:00
|
|
|
x: 4,
|
|
|
|
}
|
|
|
|
someInt := t.getInteger()
|
|
|
|
return someInt
|
|
|
|
}
|
|
|
|
`,
|
2018-04-02 15:04:42 +00:00
|
|
|
big.NewInt(4),
|
2018-02-19 09:24:28 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"struct methods with arguments",
|
2022-01-16 12:58:51 +00:00
|
|
|
`type token11 struct {
|
2018-02-19 09:24:28 +00:00
|
|
|
x int
|
|
|
|
}
|
|
|
|
|
|
|
|
// Also tests if x conflicts with t.x
|
2022-01-16 12:58:51 +00:00
|
|
|
func(t token11) addIntegers(x int, y int) int {
|
2018-02-19 09:24:28 +00:00
|
|
|
return t.x + x + y
|
|
|
|
}
|
|
|
|
|
2022-01-16 12:58:51 +00:00
|
|
|
func F%d() int {
|
|
|
|
t := token11 {
|
2018-02-19 09:24:28 +00:00
|
|
|
x: 4,
|
|
|
|
}
|
|
|
|
someInt := t.addIntegers(2, 4)
|
|
|
|
return someInt
|
|
|
|
}
|
|
|
|
`,
|
2018-04-02 15:04:42 +00:00
|
|
|
big.NewInt(10),
|
2018-02-19 09:24:28 +00:00
|
|
|
},
|
2018-02-24 09:06:48 +00:00
|
|
|
{
|
|
|
|
"initialize struct partially",
|
2022-01-16 12:58:51 +00:00
|
|
|
`type token12 struct {
|
2018-02-24 09:06:48 +00:00
|
|
|
x int
|
|
|
|
y int
|
|
|
|
z string
|
|
|
|
b bool
|
|
|
|
}
|
|
|
|
|
2022-01-16 12:58:51 +00:00
|
|
|
func F%d() int {
|
|
|
|
t := token12 {
|
2018-02-24 09:06:48 +00:00
|
|
|
x: 4,
|
|
|
|
}
|
|
|
|
return t.y
|
|
|
|
}
|
|
|
|
`,
|
2020-05-20 13:31:10 +00:00
|
|
|
big.NewInt(0),
|
2018-02-24 09:06:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"test return struct from func",
|
2022-01-16 12:58:51 +00:00
|
|
|
`type token13 struct {
|
2018-02-24 09:06:48 +00:00
|
|
|
x int
|
|
|
|
y int
|
|
|
|
z string
|
|
|
|
b bool
|
|
|
|
}
|
|
|
|
|
2022-01-16 12:58:51 +00:00
|
|
|
func newToken() token13 {
|
|
|
|
return token13{
|
2018-02-24 09:06:48 +00:00
|
|
|
x: 1,
|
|
|
|
y: 2,
|
|
|
|
z: "hello",
|
|
|
|
b: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-16 12:58:51 +00:00
|
|
|
func F%d() token13 {
|
2018-02-24 09:06:48 +00:00
|
|
|
return newToken()
|
|
|
|
}
|
|
|
|
`,
|
2020-06-03 12:55:06 +00:00
|
|
|
[]stackitem.Item{
|
|
|
|
stackitem.NewBigInteger(big.NewInt(1)),
|
|
|
|
stackitem.NewBigInteger(big.NewInt(2)),
|
|
|
|
stackitem.NewByteArray([]byte("hello")),
|
|
|
|
stackitem.NewBool(false),
|
2018-04-02 15:04:42 +00:00
|
|
|
},
|
2018-02-24 09:06:48 +00:00
|
|
|
},
|
2018-02-27 09:04:24 +00:00
|
|
|
{
|
|
|
|
"pass struct as argument",
|
2022-01-16 12:58:51 +00:00
|
|
|
`type Bar struct {
|
2018-02-27 09:04:24 +00:00
|
|
|
amount int
|
|
|
|
}
|
|
|
|
|
|
|
|
func addToAmount(x int, bar Bar) int {
|
|
|
|
bar.amount = bar.amount + x
|
|
|
|
return bar.amount
|
|
|
|
}
|
|
|
|
|
2022-01-16 12:58:51 +00:00
|
|
|
func F%d() int {
|
2018-02-27 09:04:24 +00:00
|
|
|
b := Bar{
|
|
|
|
amount: 10,
|
|
|
|
}
|
|
|
|
|
|
|
|
x := addToAmount(4, b)
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
`,
|
2018-04-02 15:04:42 +00:00
|
|
|
big.NewInt(14),
|
2018-02-27 09:04:24 +00:00
|
|
|
},
|
2020-03-26 12:35:15 +00:00
|
|
|
{
|
|
|
|
"declare struct literal",
|
2022-01-16 12:58:51 +00:00
|
|
|
`func F%d() int {
|
2020-03-26 12:35:15 +00:00
|
|
|
var x struct {
|
|
|
|
a int
|
|
|
|
}
|
|
|
|
x.a = 2
|
|
|
|
return x.a
|
2022-01-16 12:58:51 +00:00
|
|
|
}
|
|
|
|
`,
|
2020-03-26 12:35:15 +00:00
|
|
|
big.NewInt(2),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"declare struct type",
|
2022-01-16 12:58:51 +00:00
|
|
|
`type withA struct {
|
2020-03-26 12:35:15 +00:00
|
|
|
a int
|
|
|
|
}
|
2022-01-16 12:58:51 +00:00
|
|
|
func F%d() int {
|
2020-03-26 12:35:15 +00:00
|
|
|
var x withA
|
|
|
|
x.a = 2
|
|
|
|
return x.a
|
2022-01-16 12:58:51 +00:00
|
|
|
}
|
|
|
|
`,
|
2020-03-26 12:35:15 +00:00
|
|
|
big.NewInt(2),
|
|
|
|
},
|
2020-05-20 08:05:01 +00:00
|
|
|
{
|
|
|
|
"nested selectors (simple read)",
|
2022-01-16 12:58:51 +00:00
|
|
|
`type S1 struct { x, y S2 }
|
2020-05-20 08:05:01 +00:00
|
|
|
type S2 struct { a, b int }
|
2022-01-16 12:58:51 +00:00
|
|
|
func F%d() int {
|
2020-05-20 08:05:01 +00:00
|
|
|
var s1 S1
|
|
|
|
var s2 S2
|
|
|
|
s2.a = 3
|
|
|
|
s1.y = s2
|
|
|
|
return s1.y.a
|
2022-01-16 12:58:51 +00:00
|
|
|
}
|
|
|
|
`,
|
2020-05-20 08:05:01 +00:00
|
|
|
big.NewInt(3),
|
|
|
|
},
|
2020-05-20 08:08:59 +00:00
|
|
|
{
|
|
|
|
"nested selectors (simple write)",
|
2022-01-16 12:58:51 +00:00
|
|
|
`type S3 struct { x S4 }
|
|
|
|
type S4 struct { a int }
|
|
|
|
func F%d() int {
|
|
|
|
s1 := S3{
|
|
|
|
x: S4 {
|
2020-05-20 08:08:59 +00:00
|
|
|
a: 3,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
s1.x.a = 11
|
|
|
|
return s1.x.a
|
2022-01-16 12:58:51 +00:00
|
|
|
}
|
|
|
|
`,
|
2020-05-20 08:08:59 +00:00
|
|
|
big.NewInt(11),
|
|
|
|
},
|
2020-05-20 08:32:52 +00:00
|
|
|
{
|
|
|
|
"complex struct default value",
|
2022-01-16 12:58:51 +00:00
|
|
|
`type S5 struct { x S6 }
|
|
|
|
type S6 struct { y S7 }
|
|
|
|
type S7 struct { a int }
|
|
|
|
func F%d() int {
|
|
|
|
var s1 S5
|
2020-05-20 08:32:52 +00:00
|
|
|
s1.x.y.a = 11
|
|
|
|
return s1.x.y.a
|
2022-01-16 12:58:51 +00:00
|
|
|
}
|
|
|
|
`,
|
2020-05-20 08:32:52 +00:00
|
|
|
big.NewInt(11),
|
|
|
|
},
|
2021-11-12 13:35:30 +00:00
|
|
|
{
|
|
|
|
"lengthy struct default value",
|
2022-01-16 12:58:51 +00:00
|
|
|
`type SS struct { x int; y []byte; z bool }
|
|
|
|
func F%d() int {
|
|
|
|
var s SS
|
2021-11-12 13:35:30 +00:00
|
|
|
return s.x
|
2022-01-16 12:58:51 +00:00
|
|
|
}
|
|
|
|
`,
|
2021-11-12 13:35:30 +00:00
|
|
|
big.NewInt(0),
|
|
|
|
},
|
2020-05-20 08:32:52 +00:00
|
|
|
{
|
|
|
|
"nested selectors (complex write)",
|
2022-01-16 12:58:51 +00:00
|
|
|
`type S8 struct { x S9 }
|
|
|
|
type S9 struct { y, z S10 }
|
|
|
|
type S10 struct { a int }
|
|
|
|
func F%d() int {
|
|
|
|
var s1 S8
|
2020-05-20 08:32:52 +00:00
|
|
|
s1.x.y.a, s1.x.z.a = 11, 31
|
|
|
|
return s1.x.y.a + s1.x.z.a
|
2022-01-16 12:58:51 +00:00
|
|
|
}
|
|
|
|
`,
|
2020-05-20 08:32:52 +00:00
|
|
|
big.NewInt(42),
|
|
|
|
},
|
2020-06-24 16:33:58 +00:00
|
|
|
{
|
|
|
|
"omit field names",
|
2022-01-16 12:58:51 +00:00
|
|
|
`type pair struct { a, b int }
|
|
|
|
func F%d() int {
|
2020-06-24 16:33:58 +00:00
|
|
|
p := pair{1, 2}
|
|
|
|
x := p.a * 10
|
|
|
|
return x + p.b
|
2022-01-16 12:58:51 +00:00
|
|
|
}
|
|
|
|
`,
|
2020-06-24 16:33:58 +00:00
|
|
|
big.NewInt(12),
|
|
|
|
},
|
2020-07-09 09:26:16 +00:00
|
|
|
{
|
|
|
|
"uninitialized struct fields",
|
2022-01-16 12:58:51 +00:00
|
|
|
`type Foo struct {
|
2020-07-09 09:26:16 +00:00
|
|
|
i int
|
|
|
|
m map[string]int
|
|
|
|
b []byte
|
|
|
|
a []int
|
|
|
|
s struct { ii int }
|
|
|
|
}
|
|
|
|
func NewFoo() Foo { return Foo{} }
|
2022-01-16 12:58:51 +00:00
|
|
|
func F%d() int {
|
2020-07-09 09:26:16 +00:00
|
|
|
foo := NewFoo()
|
|
|
|
if foo.i != 0 { return 1 }
|
|
|
|
if len(foo.m) != 0 { return 1 }
|
|
|
|
if len(foo.b) != 0 { return 1 }
|
|
|
|
if len(foo.a) != 0 { return 1 }
|
|
|
|
s := foo.s
|
|
|
|
if s.ii != 0 { return 1 }
|
|
|
|
return 2
|
2022-01-16 12:58:51 +00:00
|
|
|
}
|
|
|
|
`,
|
2020-07-09 09:26:16 +00:00
|
|
|
big.NewInt(2),
|
|
|
|
},
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
2019-08-20 17:37:06 +00:00
|
|
|
|
|
|
|
func TestStructs(t *testing.T) {
|
2022-01-16 12:58:51 +00:00
|
|
|
srcBuilder := bytes.NewBuffer([]byte("package testcase\n"))
|
|
|
|
for i, tc := range structTestCases {
|
|
|
|
srcBuilder.WriteString(fmt.Sprintf(tc.src, i))
|
|
|
|
}
|
|
|
|
|
|
|
|
ne, di, err := compiler.CompileWithOptions("file.go", strings.NewReader(srcBuilder.String()), nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
for i, tc := range structTestCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
v := vm.New()
|
|
|
|
invokeMethod(t, fmt.Sprintf("F%d", i), ne.Script, v, di)
|
|
|
|
runAndCheck(t, v, tc.result)
|
|
|
|
})
|
|
|
|
}
|
2019-08-20 17:37:06 +00:00
|
|
|
}
|