summaryrefslogtreecommitdiffstats
path: root/test/fixedbugs/issue16870.go
blob: 2309997cacf549056b08f36094c0cf000f07aee2 (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
// run

// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"log"
	"reflect"
)

func test(got, want interface{}) {
	if !reflect.DeepEqual(got, want) {
		log.Fatalf("got %v, want %v", got, want)
	}
}

func main() {
	var i int
	var ip *int
	var ok interface{}

	// Channel receives.
	c := make(chan int, 1)
	c2 := make(chan int)

	c <- 42
	i, ok = <-c
	test(i, 42)
	test(ok, true)

	c <- 42
	_, ok = <-c
	test(ok, true)

	c <- 42
	select {
	case i, ok = <-c:
		test(i, 42)
		test(ok, true)
	}

	c <- 42
	select {
	case _, ok = <-c:
		test(ok, true)
	}

	c <- 42
	select {
	case i, ok = <-c:
		test(i, 42)
		test(ok, true)
	default:
		log.Fatal("bad select")
	}

	c <- 42
	select {
	case _, ok = <-c:
		test(ok, true)
	default:
		log.Fatal("bad select")
	}

	c <- 42
	select {
	case i, ok = <-c:
		test(i, 42)
		test(ok, true)
	case <-c2:
		log.Fatal("bad select")
	}

	c <- 42
	select {
	case _, ok = <-c:
		test(ok, true)
	case <-c2:
		log.Fatal("bad select")
	}

	close(c)
	i, ok = <-c
	test(i, 0)
	test(ok, false)

	_, ok = <-c
	test(ok, false)

	// Map indexing.
	m := make(map[int]int)

	i, ok = m[0]
	test(i, 0)
	test(ok, false)

	_, ok = m[0]
	test(ok, false)

	m[0] = 42
	i, ok = m[0]
	test(i, 42)
	test(ok, true)

	_, ok = m[0]
	test(ok, true)

	// Type assertions.
	var u interface{}

	i, ok = u.(int)
	test(i, 0)
	test(ok, false)

	ip, ok = u.(*int)
	test(ip, (*int)(nil))
	test(ok, false)

	_, ok = u.(int)
	test(ok, false)

	u = 42
	i, ok = u.(int)
	test(i, 42)
	test(ok, true)

	_, ok = u.(int)
	test(ok, true)

	u = &i
	ip, ok = u.(*int)
	test(ip, &i)
	test(ok, true)

	_, ok = u.(*int)
	test(ok, true)
}