aboutsummaryrefslogtreecommitdiffhomepage
path: root/testdata/slice.go
blob: b5e5436714527da9fa44f8a9ae7af28cdb4968bf (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main

type MySlice [32]byte

type myUint8 uint8

// Indexing into slice with named type (regression test).
var array = [4]int{
	myUint8(2): 3,
}

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)
	assert(len(make([]int, makeMyUint8(2), makeMyUint8(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)

	// slicing with max parameter (added in Go 1.2)
	longfoo := []int{1, 2, 4, 5, 10, 11}
	assert(cap(longfoo[int(1):int(3):int(5)]) == 4)
	assert(cap(longfoo[int8(1):int8(3):int8(5)]) == 4)
	assert(cap(longfoo[int16(1):int16(3):int16(5)]) == 4)
	assert(cap(longfoo[int32(1):int32(3):int32(5)]) == 4)
	assert(cap(longfoo[int64(1):int64(3):int64(5)]) == 4)
	assert(cap(longfoo[uint(1):uint(3):uint(5)]) == 4)
	assert(cap(longfoo[uint8(1):uint8(3):uint8(5)]) == 4)
	assert(cap(longfoo[uint16(1):uint16(3):uint16(5)]) == 4)
	assert(cap(longfoo[uint32(1):uint32(3):uint32(5)]) == 4)
	assert(cap(longfoo[uint64(1):uint64(3):uint64(5)]) == 4)
	assert(cap(longfoo[uintptr(1):uintptr(3):uintptr(5)]) == 4)

	// slicing an array with max parameter (added in Go 1.2)
	assert(cap(arr[int(1):int(2):int(4)]) == 3)
	assert(cap(arr[int8(1):int8(2):int8(4)]) == 3)
	assert(cap(arr[int16(1):int16(2):int16(4)]) == 3)
	assert(cap(arr[int32(1):int32(2):int32(4)]) == 3)
	assert(cap(arr[int64(1):int64(2):int64(4)]) == 3)
	assert(cap(arr[uint(1):uint(2):uint(4)]) == 3)
	assert(cap(arr[uint8(1):uint8(2):uint8(4)]) == 3)
	assert(cap(arr[uint16(1):uint16(2):uint16(4)]) == 3)
	assert(cap(arr[uint32(1):uint32(2):uint32(4)]) == 3)
	assert(cap(arr[uint64(1):uint64(2):uint64(4)]) == 3)
	assert(cap(arr[uintptr(1):uintptr(2):uintptr(4)]) == 3)

	// 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)
	for _, c := range named {
		assert(c == 0)
	}
}

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 }
func makeMyUint8(x myUint8) myUint8 { return x }