aboutsummaryrefslogtreecommitdiffhomepage
path: root/testdata/slice.go
diff options
context:
space:
mode:
authorAyke van Laethem <[email protected]>2018-11-14 14:41:40 +0100
committerAyke van Laethem <[email protected]>2018-11-14 14:41:40 +0100
commit9bddaae04a1593789a8f3f0bf877298e8d504be8 (patch)
treef866cc4a8aa2602474f9358679b62cc830e529a0 /testdata/slice.go
parentee7c276493547f1e8ea0ded3f91310ed38d0e1ac (diff)
downloadtinygo-9bddaae04a1593789a8f3f0bf877298e8d504be8.tar.gz
tinygo-9bddaae04a1593789a8f3f0bf877298e8d504be8.zip
compiler: support any int type in slice indexes
Make sure the compiler will correctly compile indexes of type uint64, for example.
Diffstat (limited to 'testdata/slice.go')
-rw-r--r--testdata/slice.go46
1 files changed, 46 insertions, 0 deletions
diff --git a/testdata/slice.go b/testdata/slice.go
index 8fcbffff1..6237a0157 100644
--- a/testdata/slice.go
+++ b/testdata/slice.go
@@ -10,6 +10,46 @@ func main() {
printslice("foo[1:2]", foo[1:2])
println("sum foo:", sum(foo))
+ // indexing into a slice with uncommon index types
+ assert(foo[int(2)] == 4)
+ assert(foo[int8(2)] == 4)
+ assert(foo[int16(2)] == 4)
+ assert(foo[int32(2)] == 4)
+ assert(foo[int64(2)] == 4)
+ assert(foo[uint(2)] == 4)
+ assert(foo[uint8(2)] == 4)
+ assert(foo[uint16(2)] == 4)
+ assert(foo[uint32(2)] == 4)
+ assert(foo[uint64(2)] == 4)
+ assert(foo[uintptr(2)] == 4)
+
+ // slicing with uncommon low, high types
+ assert(len(foo[int(1):int(3)]) == 2)
+ assert(len(foo[int8(1):int8(3)]) == 2)
+ assert(len(foo[int16(1):int16(3)]) == 2)
+ assert(len(foo[int32(1):int32(3)]) == 2)
+ assert(len(foo[int64(1):int64(3)]) == 2)
+ assert(len(foo[uint(1):uint(3)]) == 2)
+ assert(len(foo[uint8(1):uint8(3)]) == 2)
+ assert(len(foo[uint16(1):uint16(3)]) == 2)
+ assert(len(foo[uint32(1):uint32(3)]) == 2)
+ assert(len(foo[uint64(1):uint64(3)]) == 2)
+ assert(len(foo[uintptr(1):uintptr(3)]) == 2)
+
+ // slicing an array with uncommon low, high types
+ arr := [4]int{1, 2, 4, 5}
+ assert(len(arr[int(1):int(3)]) == 2)
+ assert(len(arr[int8(1):int8(3)]) == 2)
+ assert(len(arr[int16(1):int16(3)]) == 2)
+ assert(len(arr[int32(1):int32(3)]) == 2)
+ assert(len(arr[int64(1):int64(3)]) == 2)
+ assert(len(arr[uint(1):uint(3)]) == 2)
+ assert(len(arr[uint8(1):uint8(3)]) == 2)
+ assert(len(arr[uint16(1):uint16(3)]) == 2)
+ assert(len(arr[uint32(1):uint32(3)]) == 2)
+ assert(len(arr[uint64(1):uint64(3)]) == 2)
+ assert(len(arr[uintptr(1):uintptr(3)]) == 2)
+
// copy
println("copy foo -> bar:", copy(bar, foo))
printslice("bar", bar)
@@ -53,3 +93,9 @@ func sum(l []int) int {
}
return sum
}
+
+func assert(ok bool) {
+ if !ok {
+ panic("assert failed")
+ }
+}