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