compiler: group small tests in a single file

Signed-off-by: Evgeniy Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgeniy Stratonikov 2022-01-16 15:58:51 +03:00
parent 870fd024c9
commit 0e86073a77
9 changed files with 804 additions and 783 deletions

View file

@ -1,11 +1,14 @@
package compiler_test
import (
"bytes"
"fmt"
"math/big"
"strings"
"testing"
"github.com/nspcc-dev/neo-go/pkg/compiler"
"github.com/nspcc-dev/neo-go/pkg/vm"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/stretchr/testify/require"
)
@ -13,9 +16,7 @@ import (
var sliceTestCases = []testCase{
{
"constant index",
`
package foo
func Main() int {
`func F%d() int {
a := []int{0,0}
a[1] = 42
return a[1]+0
@ -25,9 +26,7 @@ var sliceTestCases = []testCase{
},
{
"variable index",
`
package foo
func Main() int {
`func F%d() int {
a := []int{0,0}
i := 1
a[i] = 42
@ -38,19 +37,17 @@ var sliceTestCases = []testCase{
},
{
"increase slice element with +=",
`package foo
func Main() int {
`func F%d() int {
a := []int{1, 2, 3}
a[1] += 40
return a[1]
}`,
}
`,
big.NewInt(42),
},
{
"complex test",
`
package foo
func Main() int {
`func F%d() int {
a := []int{1,2,3}
x := a[0]
a[x] = a[x] + 4
@ -62,9 +59,7 @@ var sliceTestCases = []testCase{
},
{
"slice literals with variables",
`
package foo
func Main() int {
`func F%d() int {
elem := 7
a := []int{6, elem, 8}
return a[1]
@ -74,9 +69,7 @@ var sliceTestCases = []testCase{
},
{
"slice literals with expressions",
`
package foo
func Main() int {
`func F%d() int {
elem := []int{3, 7}
a := []int{6, elem[1]*2+1, 24}
return a[1]
@ -86,93 +79,88 @@ var sliceTestCases = []testCase{
},
{
"sub-slice with literal bounds",
`
package foo
func Main() []byte {
`func F%d() []byte {
a := []byte{0, 1, 2, 3}
b := a[1:3]
return b
}`,
}
`,
[]byte{1, 2},
},
{
"sub-slice with constant bounds",
`
package foo
const x = 1
`const x = 1
const y = 3
func Main() []byte {
func F%d() []byte {
a := []byte{0, 1, 2, 3}
b := a[x:y]
return b
}`,
}
`,
[]byte{1, 2},
},
{
"sub-slice with variable bounds",
`
package foo
func Main() []byte {
`func F%d() []byte {
a := []byte{0, 1, 2, 3}
x := 1
y := 3
b := a[x:y]
return b
}`,
}
`,
[]byte{1, 2},
},
{
"sub-slice with no lower bound",
`
package foo
func Main() []byte {
`func F%d() []byte {
a := []byte{0, 1, 2, 3}
b := a[:3]
return b
}`,
}
`,
[]byte{0, 1, 2},
},
{
"sub-slice with no upper bound",
`
package foo
func Main() []byte {
`func F%d() []byte {
a := []byte{0, 1, 2, 3}
b := a[2:]
return b
}`,
}
`,
[]byte{2, 3},
},
{
"declare byte slice",
`package foo
func Main() []byte {
`func F%d() []byte {
var a []byte
a = append(a, 1)
a = append(a, 2)
return a
}`,
}
`,
[]byte{1, 2},
},
{
"append multiple bytes to a slice",
`package foo
func Main() []byte {
`func F%d() []byte {
var a []byte
a = append(a, 1, 2)
return a
}`,
}
`,
[]byte{1, 2},
},
{
"append multiple ints to a slice",
`package foo
func Main() []int {
`func F%d() []int {
var a []int
a = append(a, 1, 2, 3)
a = append(a, 4, 5)
return a
}`,
}
`,
[]stackitem.Item{
stackitem.NewBigInteger(big.NewInt(1)),
stackitem.NewBigInteger(big.NewInt(2)),
@ -183,15 +171,15 @@ var sliceTestCases = []testCase{
},
{
"int slice, append slice",
`package foo
func getByte() byte { return 0x80 }
func Main() []int {
` func getByte() byte { return 0x80 }
func F%d() []int {
x := []int{1}
y := []int{2, 3}
x = append(x, y...)
x = append(x, y...)
return x
}`,
}
`,
[]stackitem.Item{
stackitem.Make(1),
stackitem.Make(2), stackitem.Make(3),
@ -200,13 +188,13 @@ var sliceTestCases = []testCase{
},
{
"declare compound slice",
`package foo
func Main() []string {
`func F%d() []string {
var a []string
a = append(a, "a")
a = append(a, "b")
return a
}`,
}
`,
[]stackitem.Item{
stackitem.NewByteArray([]byte("a")),
stackitem.NewByteArray([]byte("b")),
@ -214,14 +202,14 @@ var sliceTestCases = []testCase{
},
{
"declare compound slice alias",
`package foo
type strs []string
func Main() []string {
`type strs []string
func F%d() []string {
var a strs
a = append(a, "a")
a = append(a, "b")
return a
}`,
}
`,
[]stackitem.Item{
stackitem.NewByteArray([]byte("a")),
stackitem.NewByteArray([]byte("b")),
@ -229,72 +217,70 @@ var sliceTestCases = []testCase{
},
{
"byte-slice assignment",
`package foo
func Main() []byte {
`func F%d() []byte {
a := []byte{0, 1, 2}
a[1] = 42
return a
}`,
}
`,
[]byte{0, 42, 2},
},
{
"byte-slice assignment after string conversion",
`package foo
func Main() []byte {
`func F%d() []byte {
a := "abc"
b := []byte(a)
b[1] = 42
return []byte(a)
}`,
}
`,
[]byte{0x61, 0x62, 0x63},
},
{
"declare and append byte-slice",
`package foo
func Main() []byte {
`func F%d() []byte {
var a []byte
a = append(a, 1)
a = append(a, 2)
return a
}`,
}
`,
[]byte{1, 2},
},
{
"nested slice assignment",
`package foo
func Main() int {
`func F%d() int {
a := [][]int{[]int{1, 2}, []int{3, 4}}
a[1][0] = 42
return a[1][0]
}`,
}
`,
big.NewInt(42),
},
{
"nested slice omitted type (slice)",
`package foo
func Main() int {
`func F%d() int {
a := [][]int{{1, 2}, {3, 4}}
a[1][0] = 42
return a[1][0]
}`,
}
`,
big.NewInt(42),
},
{
"nested slice omitted type (struct)",
`package foo
type pair struct { a, b int }
func Main() int {
a := []pair{{a: 1, b: 2}, {a: 3, b: 4}}
`type pairA struct { a, b int }
func F%d() int {
a := []pairA{{a: 1, b: 2}, {a: 3, b: 4}}
a[1].a = 42
return a[1].a
}`,
}
`,
big.NewInt(42),
},
{
"defaults to nil for byte slice",
`
package foo
func Main() int {
`func F%d() int {
var a []byte
if a != nil { return 1}
return 2
@ -304,9 +290,7 @@ var sliceTestCases = []testCase{
},
{
"defaults to nil for int slice",
`
package foo
func Main() int {
`func F%d() int {
var a []int
if a != nil { return 1}
return 2
@ -316,11 +300,9 @@ var sliceTestCases = []testCase{
},
{
"defaults to nil for struct slice",
`
package foo
type pair struct { a, b int }
func Main() int {
var a []pair
`type pairB struct { a, b int }
func F%d() int {
var a []pairB
if a != nil { return 1}
return 2
}
@ -329,28 +311,42 @@ var sliceTestCases = []testCase{
},
{
"literal byte-slice with variable values",
`package foo
const sym1 = 's'
func Main() []byte {
`const sym1 = 's'
func F%d() []byte {
sym2 := byte('t')
sym4 := byte('i')
return []byte{sym1, sym2, 'r', sym4, 'n', 'g'}
}`,
}
`,
[]byte("string"),
},
{
"literal slice with function call",
`package foo
func fn() byte { return 't' }
func Main() []byte {
`func fn() byte { return 't' }
func F%d() []byte {
return []byte{'s', fn(), 'r'}
}`,
}
`,
[]byte("str"),
},
}
func TestSliceOperations(t *testing.T) {
runTestCases(t, sliceTestCases)
srcBuilder := bytes.NewBuffer([]byte("package testcase\n"))
for i, tc := range sliceTestCases {
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 sliceTestCases {
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)
})
}
}
func TestByteSlices(t *testing.T) {