aboutsummaryrefslogtreecommitdiffhomepage
path: root/testdata/interface.go
blob: 72cc76bce5c4bfa1ed87a85de97f44b7c37591b2 (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package main

import "time"

func main() {
	thing := &Thing{"foo"}
	println("thing:", thing.String())
	thing.Print()
	printItf(5)
	printItf(byte('x'))
	printItf("foo")
	printItf(Foo(18))
	printItf(*thing)
	printItf(thing)
	printItf(Stringer(thing))
	printItf(struct{ n int }{})
	printItf(struct {
		n int `foo:"bar"`
	}{})
	printItf(Number(3))
	array := Array([4]uint32{1, 7, 11, 13})
	printItf(array)
	printItf(ArrayStruct{3, array})
	printItf(SmallPair{3, 5})
	s := Stringer(thing)
	println("Stringer.String():", s.String())
	var itf interface{} = s
	println("Stringer.(*Thing).String():", itf.(Stringer).String())
	if s, ok := s.(interface{ String() string }); ok {
		println("s has String() method:", s.String())
	}

	println("nested switch:", nestedSwitch('v', 3))

	// Try putting a linked list in an interface:
	// https://github.com/tinygo-org/tinygo/issues/309
	itf = linkedList{}

	// Test bugfix for assertion on named empty interface:
	// https://github.com/tinygo-org/tinygo/issues/453
	_, _ = itf.(Empty)

	var v Byter = FooByte(3)
	println("Byte(): ", v.Byte())

	var n int
	var f float32
	var interfaceEqualTests = []struct {
		equal bool
		lhs   interface{}
		rhs   interface{}
	}{
		{true, true, true},
		{true, int(1), int(1)},
		{true, int8(1), int8(1)},
		{true, int16(1), int16(1)},
		{true, int32(1), int32(1)},
		{true, int64(1), int64(1)},
		{true, uint(1), uint(1)},
		{false, uint(1), uint(2)},
		{true, uint8(1), uint8(1)},
		{true, uint16(1), uint16(1)},
		{true, uint32(1), uint32(1)},
		{true, uint64(1), uint64(1)},
		{true, uintptr(1), uintptr(1)},
		{true, float32(1.1), float32(1.1)},
		{true, float64(1.1), float64(1.1)},
		{true, complex(100, 8), complex(100, 8)},
		{false, complex(100, 8), complex(101, 8)},
		{false, complex(100, 8), complex(100, 9)},
		{true, complex64(8), complex64(8)},
		{true, complex128(8), complex128(8)},
		{true, "string", "string"},
		{false, "string", "stringx"},
		{true, [2]int16{-5, 201}, [2]int16{-5, 201}},
		{false, [2]int16{-5, 201}, [2]int16{-5, 202}},
		{false, [2]int16{-5, 201}, [2]int16{5, 201}},
		{true, &n, &n},
		{false, &n, new(int)},
		{false, new(int), new(int)},
		{false, &n, &f},
		{true, struct {
			a int
			b int
		}{3, 5}, struct {
			a int
			b int
		}{3, 5}},
		{false, struct {
			a int
			b int
		}{3, 5}, struct {
			a int
			b int
		}{3, 6}},
		{true, named1(), named1()},
		{true, named2(), named2()},
		{false, named1(), named2()},
		{false, named2(), named3()},
		{true, namedptr1(), namedptr1()},
		{false, namedptr1(), namedptr2()},
	}
	for i, tc := range interfaceEqualTests {
		if (tc.lhs == tc.rhs) != tc.equal {
			println("test", i, "of interfaceEqualTests failed")
		}
	}

	// test interface blocking
	blockDynamic(NonBlocker{})
	println("non-blocking call on sometimes-blocking interface")
	blockDynamic(SleepBlocker(time.Millisecond))
	println("slept 1ms")
	blockStatic(SleepBlocker(time.Millisecond))
	println("slept 1ms")

	// check that pointer-to-pointer type switches work
	ptrptrswitch()
}

func printItf(val interface{}) {
	switch val := val.(type) {
	case Unmatched:
		panic("matched the unmatchable")
	case Doubler:
		println("is Doubler:", val.Double())
	case Tuple:
		println("is Tuple:", val.Nth(0), val.Nth(1), val.Nth(2), val.Nth(3))
		val.Print()
	case int:
		println("is int:", val)
	case byte:
		println("is byte:", val)
	case string:
		println("is string:", val)
	case Thing:
		println("is Thing:", val.String())
	case *Thing:
		println("is *Thing:", val.String())
	case struct{ i int }:
		println("is struct{i int}")
	case struct{ n int }:
		println("is struct{n int}")
	case struct {
		n int `foo:"bar"`
	}:
		println("is struct{n int `foo:\"bar\"`}")
	case Foo:
		println("is Foo:", val)
	default:
		println("is ?")
	}
}

var (
	// Test for type assert support in the interp package.
	globalThing interface{} = Foo(3)
	_                       = globalThing.(Foo)
)

func nestedSwitch(verb rune, arg interface{}) bool {
	switch verb {
	case 'v', 's':
		switch arg.(type) {
		case int:
			return true
		}
	}
	return false
}

func blockDynamic(blocker DynamicBlocker) {
	blocker.Block()
}

func blockStatic(blocker StaticBlocker) {
	blocker.Sleep()
}

type Thing struct {
	name string
}

func (t Thing) String() string {
	return t.name
}

func (t Thing) Print() {
	println("Thing.Print:", t.name)
}

type Stringer interface {
	String() string
}

type Foo int

type Number int

func (n Number) Double() int {
	return int(n) * 2
}

type Doubler interface {
	Double() int
}

type Tuple interface {
	Nth(int) uint32
	Print()
}

type Array [4]uint32

func (a Array) Nth(n int) uint32 {
	return a[n]
}

func (a Array) Print() {
	println("Array len:", len(a))
}

type ArrayStruct struct {
	n int
	a Array
}

func (a ArrayStruct) Nth(n int) uint32 {
	return a.a[n]
}

func (a ArrayStruct) Print() {
	println("ArrayStruct.Print:", len(a.a), a.n)
}

type SmallPair struct {
	a byte
	b byte
}

func (p SmallPair) Nth(n int) uint32 {
	return uint32(int(p.a)*n + int(p.b)*n)
}

func (p SmallPair) Print() {
	println("SmallPair.Print:", p.a, p.b)
}

// There is no type that matches this method.
type Unmatched interface {
	NeverImplementedMethod()
}

type linkedList struct {
	addr *linkedList
}

type DynamicBlocker interface {
	Block()
}

type NonBlocker struct{}

func (b NonBlocker) Block() {}

type SleepBlocker time.Duration

func (s SleepBlocker) Block() {
	time.Sleep(time.Duration(s))
}

func (s SleepBlocker) Sleep() {
	s.Block()
}

type StaticBlocker interface {
	Sleep()
}

type Empty interface{}

type FooByte int

func (f FooByte) Byte() byte { return byte(f) }

type Byter interface {
	Byte() uint8
}

// Make sure that named types inside functions do not alias with any other named
// functions.

type named int

func named1() any {
	return named(0)
}

func named2() any {
	type named int
	return named(0)
}

func named3() any {
	type named int
	return named(0)
}

func namedptr1() interface{} {
	type Test int
	return (*Test)(nil)
}

func namedptr2() interface{} {
	type Test byte
	return (*Test)(nil)
}

func ptrptrswitch() {
	identify(0)
	identify(new(int))
	identify(new(*int))
}

func identify(itf any) {
	switch itf.(type) {
	case int:
		println("type is int")
	case *int:
		println("type is *int")
	case **int:
		println("type is **int")
	default:
		println("other type??")
	}
}