xgo/runtime/vm_array_test.go

56 lines
2.2 KiB
Go
Raw Normal View History

2019-01-11 13:27:28 +03:00
package runtime_test
2019-01-09 10:17:42 +03:00
import (
2019-01-14 15:26:20 +03:00
"fmt"
2019-01-09 10:17:42 +03:00
"testing"
"github.com/d5/tengo/objects"
2019-01-09 10:17:42 +03:00
)
func TestArray(t *testing.T) {
expect(t, `out = [1, 2 * 2, 3 + 3]`, nil, ARR{1, 4, 6})
2019-01-09 10:17:42 +03:00
// array copy-by-reference
expect(t, `a1 := [1, 2, 3]; a2 := a1; a1[0] = 5; out = a2`, nil, ARR{5, 2, 3})
expect(t, `func () { a1 := [1, 2, 3]; a2 := a1; a1[0] = 5; out = a2 }()`, nil, ARR{5, 2, 3})
2019-01-09 10:17:42 +03:00
// array index set
expectError(t, `a1 := [1, 2, 3]; a1[3] = 5`, nil, "index out of bounds")
2019-01-14 15:26:20 +03:00
// index operator
arr := ARR{1, 2, 3, 4, 5, 6}
arrStr := `[1, 2, 3, 4, 5, 6]`
arrLen := 6
for idx := 0; idx < arrLen; idx++ {
expect(t, fmt.Sprintf("out = %s[%d]", arrStr, idx), nil, arr[idx])
expect(t, fmt.Sprintf("out = %s[0 + %d]", arrStr, idx), nil, arr[idx])
expect(t, fmt.Sprintf("out = %s[1 + %d - 1]", arrStr, idx), nil, arr[idx])
expect(t, fmt.Sprintf("idx := %d; out = %s[idx]", idx, arrStr), nil, arr[idx])
2019-01-14 15:26:20 +03:00
}
expect(t, fmt.Sprintf("%s[%d]", arrStr, -1), nil, objects.UndefinedValue)
expect(t, fmt.Sprintf("%s[%d]", arrStr, arrLen), nil, objects.UndefinedValue)
2019-01-14 15:26:20 +03:00
// slice operator
for low := 0; low < arrLen; low++ {
expect(t, fmt.Sprintf("out = %s[%d:%d]", arrStr, low, low), nil, ARR{})
2019-01-14 15:26:20 +03:00
for high := low; high <= arrLen; high++ {
expect(t, fmt.Sprintf("out = %s[%d:%d]", arrStr, low, high), nil, arr[low:high])
expect(t, fmt.Sprintf("out = %s[0 + %d : 0 + %d]", arrStr, low, high), nil, arr[low:high])
expect(t, fmt.Sprintf("out = %s[1 + %d - 1 : 1 + %d - 1]", arrStr, low, high), nil, arr[low:high])
expect(t, fmt.Sprintf("out = %s[:%d]", arrStr, high), nil, arr[:high])
expect(t, fmt.Sprintf("out = %s[%d:]", arrStr, low), nil, arr[low:])
2019-01-14 15:26:20 +03:00
}
}
expect(t, fmt.Sprintf("out = %s[:]", arrStr), nil, arr)
expect(t, fmt.Sprintf("out = %s[%d:]", arrStr, -1), nil, arr)
expect(t, fmt.Sprintf("out = %s[:%d]", arrStr, arrLen+1), nil, arr)
expect(t, fmt.Sprintf("out = %s[%d:%d]", arrStr, 2, 2), nil, ARR{})
expectError(t, fmt.Sprintf("%s[:%d]", arrStr, -1), nil, "invalid slice index")
expectError(t, fmt.Sprintf("%s[%d:]", arrStr, arrLen+1), nil, "invalid slice index")
expectError(t, fmt.Sprintf("%s[%d:%d]", arrStr, 0, -1), nil, "invalid slice index")
expectError(t, fmt.Sprintf("%s[%d:%d]", arrStr, 2, 1), nil, "invalid slice index")
2019-01-09 10:17:42 +03:00
}