github.com/aykevl/tinygo@v0.5.0/testdata/slice.go (about) 1 package main 2 3 type MySlice [32]byte 4 5 func main() { 6 l := 5 7 foo := []int{1, 2, 4, 5} 8 bar := make([]int, l-2, l) 9 println("foo is nil?", foo == nil, nil == foo) 10 printslice("foo", foo) 11 printslice("bar", bar) 12 printslice("foo[1:2]", foo[1:2]) 13 println("sum foo:", sum(foo)) 14 15 // creating a slice with uncommon len, cap types 16 assert(len(make([]int, makeInt(2), makeInt(3))) == 2) 17 assert(len(make([]int, makeInt8(2), makeInt8(3))) == 2) 18 assert(len(make([]int, makeInt16(2), makeInt16(3))) == 2) 19 assert(len(make([]int, makeInt32(2), makeInt32(3))) == 2) 20 assert(len(make([]int, makeInt64(2), makeInt64(3))) == 2) 21 assert(len(make([]int, makeUint(2), makeUint(3))) == 2) 22 assert(len(make([]int, makeUint8(2), makeUint8(3))) == 2) 23 assert(len(make([]int, makeUint16(2), makeUint16(3))) == 2) 24 assert(len(make([]int, makeUint32(2), makeUint32(3))) == 2) 25 assert(len(make([]int, makeUint64(2), makeUint64(3))) == 2) 26 assert(len(make([]int, makeUintptr(2), makeUintptr(3))) == 2) 27 28 // indexing into a slice with uncommon index types 29 assert(foo[int(2)] == 4) 30 assert(foo[int8(2)] == 4) 31 assert(foo[int16(2)] == 4) 32 assert(foo[int32(2)] == 4) 33 assert(foo[int64(2)] == 4) 34 assert(foo[uint(2)] == 4) 35 assert(foo[uint8(2)] == 4) 36 assert(foo[uint16(2)] == 4) 37 assert(foo[uint32(2)] == 4) 38 assert(foo[uint64(2)] == 4) 39 assert(foo[uintptr(2)] == 4) 40 41 // slicing with uncommon low, high types 42 assert(len(foo[int(1):int(3)]) == 2) 43 assert(len(foo[int8(1):int8(3)]) == 2) 44 assert(len(foo[int16(1):int16(3)]) == 2) 45 assert(len(foo[int32(1):int32(3)]) == 2) 46 assert(len(foo[int64(1):int64(3)]) == 2) 47 assert(len(foo[uint(1):uint(3)]) == 2) 48 assert(len(foo[uint8(1):uint8(3)]) == 2) 49 assert(len(foo[uint16(1):uint16(3)]) == 2) 50 assert(len(foo[uint32(1):uint32(3)]) == 2) 51 assert(len(foo[uint64(1):uint64(3)]) == 2) 52 assert(len(foo[uintptr(1):uintptr(3)]) == 2) 53 54 // slicing an array with uncommon low, high types 55 arr := [4]int{1, 2, 4, 5} 56 assert(len(arr[int(1):int(3)]) == 2) 57 assert(len(arr[int8(1):int8(3)]) == 2) 58 assert(len(arr[int16(1):int16(3)]) == 2) 59 assert(len(arr[int32(1):int32(3)]) == 2) 60 assert(len(arr[int64(1):int64(3)]) == 2) 61 assert(len(arr[uint(1):uint(3)]) == 2) 62 assert(len(arr[uint8(1):uint8(3)]) == 2) 63 assert(len(arr[uint16(1):uint16(3)]) == 2) 64 assert(len(arr[uint32(1):uint32(3)]) == 2) 65 assert(len(arr[uint64(1):uint64(3)]) == 2) 66 assert(len(arr[uintptr(1):uintptr(3)]) == 2) 67 68 // copy 69 println("copy foo -> bar:", copy(bar, foo)) 70 printslice("bar", bar) 71 72 // append 73 var grow []int 74 println("slice is nil?", grow == nil, nil == grow) 75 printslice("grow", grow) 76 grow = append(grow, 42) 77 printslice("grow", grow) 78 grow = append(grow, -1, -2) 79 printslice("grow", grow) 80 grow = append(grow, foo...) 81 printslice("grow", grow) 82 grow = append(grow) 83 printslice("grow", grow) 84 grow = append(grow, grow...) 85 printslice("grow", grow) 86 87 // append string to []bytes 88 bytes := append([]byte{1, 2, 3}, "foo"...) 89 print("bytes: len=", len(bytes), " cap=", cap(bytes), " data:") 90 for _, n := range bytes { 91 print(" ", n) 92 } 93 println() 94 95 // Verify the fix in https://github.com/tinygo-org/tinygo/pull/119 96 var unnamed [32]byte 97 var named MySlice 98 assert(len(unnamed[:]) == 32) 99 assert(len(named[:]) == 32) 100 } 101 102 func printslice(name string, s []int) { 103 print(name, ": len=", len(s), " cap=", cap(s), " data:") 104 for _, n := range s { 105 print(" ", n) 106 } 107 println() 108 } 109 110 func sum(l []int) int { 111 sum := 0 112 for _, n := range l { 113 sum += n 114 } 115 return sum 116 } 117 118 func assert(ok bool) { 119 if !ok { 120 panic("assert failed") 121 } 122 } 123 124 // Helper functions used to hide const values from the compiler during IR 125 // construction. 126 127 func makeInt(x int) int { return x } 128 func makeInt8(x int8) int8 { return x } 129 func makeInt16(x int16) int16 { return x } 130 func makeInt32(x int32) int32 { return x } 131 func makeInt64(x int64) int64 { return x } 132 func makeUint(x uint) uint { return x } 133 func makeUint8(x uint8) uint8 { return x } 134 func makeUint16(x uint16) uint16 { return x } 135 func makeUint32(x uint32) uint32 { return x } 136 func makeUint64(x uint64) uint64 { return x } 137 func makeUintptr(x uintptr) uintptr { return x }