diff --git a/runtime/vm_array_test.go b/runtime/vm_array_test.go index d8ba8d0..2fe32e4 100644 --- a/runtime/vm_array_test.go +++ b/runtime/vm_array_test.go @@ -1,44 +1,42 @@ package runtime_test import ( + "fmt" "testing" ) func TestArray(t *testing.T) { expect(t, `out = [1, 2 * 2, 3 + 3]`, ARR{1, 4, 6}) - expect(t, `out = [1, 2, 3][0]`, 1) - expect(t, `out = [1, 2, 3][1]`, 2) - expect(t, `out = [1, 2, 3][2]`, 3) - expect(t, `i := 0; out = [1, 2, 3][i]`, 1) - expect(t, `out = [1, 2, 3][1 + 1]`, 3) - expect(t, `arr := [1, 2, 3]; out = arr[2]`, 3) - expect(t, `arr := [1, 2, 3]; out = arr[0] + arr[1] + arr[2]`, 6) - expect(t, `arr := [1, 2, 3]; i := arr[0]; out = arr[i]`, 2) - - expect(t, `out = [1, 2, 3][1+1]`, 3) - expect(t, `a := 1; out = [1, 2, 3][a+1]`, 3) - - expect(t, `out = [1, 2, 3][:]`, ARR{1, 2, 3}) - expect(t, `out = [1, 2, 3][0:3]`, ARR{1, 2, 3}) - expect(t, `out = [1, 2, 3][1:]`, ARR{2, 3}) - expect(t, `out = [1, 2, 3][1:2]`, ARR{2}) - expect(t, `out = [1, 2, 3][:2]`, ARR{1, 2}) - expect(t, `out = [1, 2, 3][1:1]`, ARR{}) - - expect(t, `out = [1, 2, 3][3-2:1+1]`, ARR{2}) - expect(t, `a := 1; out = [1, 2, 3][a-1:a+1]`, ARR{1, 2}) - // array copy-by-reference expect(t, `a1 := [1, 2, 3]; a2 := a1; a1[0] = 5; out = a2`, ARR{5, 2, 3}) expect(t, `func () { a1 := [1, 2, 3]; a2 := a1; a1[0] = 5; out = a2 }()`, ARR{5, 2, 3}) - expectError(t, `[1, 2, 3][3]`) - expectError(t, `[1, 2, 3][-1]`) + // 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), arr[idx]) + expect(t, fmt.Sprintf("out = %s[0 + %d]", arrStr, idx), arr[idx]) + expect(t, fmt.Sprintf("out = %s[1 + %d - 1]", arrStr, idx), arr[idx]) + expect(t, fmt.Sprintf("idx := %d; out = %s[idx]", idx, arrStr), arr[idx]) + } + expectError(t, fmt.Sprintf("%s[%d]", arrStr, -1)) + expectError(t, fmt.Sprintf("%s[%d]", arrStr, arrLen)) - expectError(t, `[1, 2, 3][-1:]`) - expectError(t, `[1, 2, 3][:4]`) - expectError(t, `[1, 2, 3][-1:3]`) - expectError(t, `[1, 2, 3][0:4]`) - expectError(t, `[1, 2, 3][2:1]`) + // slice operator + for low := 0; low < arrLen; low++ { + for high := low; high <= arrLen; high++ { + expect(t, fmt.Sprintf("out = %s[%d:%d]", arrStr, low, high), arr[low:high]) + expect(t, fmt.Sprintf("out = %s[0 + %d : 0 + %d]", arrStr, low, high), arr[low:high]) + expect(t, fmt.Sprintf("out = %s[1 + %d - 1 : 1 + %d - 1]", arrStr, low, high), arr[low:high]) + expect(t, fmt.Sprintf("out = %s[:%d]", arrStr, high), arr[:high]) + expect(t, fmt.Sprintf("out = %s[%d:]", arrStr, low), arr[low:]) + expect(t, fmt.Sprintf("out = %s[:]", arrStr), arr[:]) + } + } + expectError(t, fmt.Sprintf("%s[%d:]", arrStr, -1)) + expectError(t, fmt.Sprintf("%s[:%d]", arrStr, arrLen+1)) + expectError(t, fmt.Sprintf("%s[%d:%d]", arrStr, 2, 1)) } diff --git a/runtime/vm_string_test.go b/runtime/vm_string_test.go index 11d6f44..d84cd11 100644 --- a/runtime/vm_string_test.go +++ b/runtime/vm_string_test.go @@ -1,6 +1,7 @@ package runtime_test import ( + "fmt" "testing" ) @@ -13,20 +14,31 @@ func TestString(t *testing.T) { expect(t, `out = "Hello" != "Hello"`, false) expect(t, `out = "Hello" != "World"`, true) - expect(t, `out = "abcde"[0]`, 'a') - expect(t, `out = "abcde"[4]`, 'e') - expectError(t, `out = "abcde"[-1]`) - expectError(t, `out = "abcde"[5]`) + // index operator + str := "abcdef" + strStr := `"abcdef"` + strLen := 6 + for idx := 0; idx < strLen; idx++ { + expect(t, fmt.Sprintf("out = %s[%d]", strStr, idx), str[idx]) + expect(t, fmt.Sprintf("out = %s[0 + %d]", strStr, idx), str[idx]) + expect(t, fmt.Sprintf("out = %s[1 + %d - 1]", strStr, idx), str[idx]) + expect(t, fmt.Sprintf("idx := %d; out = %s[idx]", idx, strStr), str[idx]) + } + expectError(t, fmt.Sprintf("%s[%d]", strStr, -1)) + expectError(t, fmt.Sprintf("%s[%d]", strStr, strLen)) - expect(t, `out = "abcde"[:]`, "abcde") - expect(t, `out = "abcde"[0:5]`, "abcde") - expect(t, `out = "abcde"[1:]`, "bcde") - expect(t, `out = "abcde"[:4]`, "abcd") - expect(t, `out = "abcde"[1:4]`, "bcd") - expect(t, `out = "abcde"[2:3]`, "c") - expect(t, `out = "abcde"[2:2]`, "") - expectError(t, `out = "abcde"[-1:]`) - expectError(t, `out = "abcde"[:6]`) - expectError(t, `out = "abcde"[-1:6]`) - expectError(t, `out = "abcde"[3:2]`) + // slice operator + for low := 0; low < strLen; low++ { + for high := low; high <= strLen; high++ { + expect(t, fmt.Sprintf("out = %s[%d:%d]", strStr, low, high), str[low:high]) + expect(t, fmt.Sprintf("out = %s[0 + %d : 0 + %d]", strStr, low, high), str[low:high]) + expect(t, fmt.Sprintf("out = %s[1 + %d - 1 : 1 + %d - 1]", strStr, low, high), str[low:high]) + expect(t, fmt.Sprintf("out = %s[:%d]", strStr, high), str[:high]) + expect(t, fmt.Sprintf("out = %s[%d:]", strStr, low), str[low:]) + expect(t, fmt.Sprintf("out = %s[:]", strStr), str[:]) + } + } + expectError(t, fmt.Sprintf("%s[%d:]", strStr, -1)) + expectError(t, fmt.Sprintf("%s[:%d]", strStr, strLen+1)) + expectError(t, fmt.Sprintf("%s[%d:%d]", strStr, 2, 1)) }