OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [testsuite/] [go.test/] [test/] [switch.go] - Blame information for rev 700

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 700 jeremybenn
// $G $F.go && $L $F.$A && ./$A.out
2
 
3
// Copyright 2009 The Go Authors. All rights reserved.
4
// Use of this source code is governed by a BSD-style
5
// license that can be found in the LICENSE file.
6
 
7
package main
8
 
9
func assert(cond bool, msg string) {
10
        if !cond {
11
                print("assertion fail: ", msg, "\n")
12
                panic(1)
13
        }
14
}
15
 
16
func main() {
17
        i5 := 5
18
        i7 := 7
19
        hello := "hello"
20
 
21
        switch true {
22
        case i5 < 5:
23
                assert(false, "<")
24
        case i5 == 5:
25
                assert(true, "!")
26
        case i5 > 5:
27
                assert(false, ">")
28
        }
29
 
30
        switch {
31
        case i5 < 5:
32
                assert(false, "<")
33
        case i5 == 5:
34
                assert(true, "!")
35
        case i5 > 5:
36
                assert(false, ">")
37
        }
38
 
39
        switch x := 5; true {
40
        case i5 < x:
41
                assert(false, "<")
42
        case i5 == x:
43
                assert(true, "!")
44
        case i5 > x:
45
                assert(false, ">")
46
        }
47
 
48
        switch x := 5; true {
49
        case i5 < x:
50
                assert(false, "<")
51
        case i5 == x:
52
                assert(true, "!")
53
        case i5 > x:
54
                assert(false, ">")
55
        }
56
 
57
        switch i5 {
58
        case 0:
59
                assert(false, "0")
60
        case 1:
61
                assert(false, "1")
62
        case 2:
63
                assert(false, "2")
64
        case 3:
65
                assert(false, "3")
66
        case 4:
67
                assert(false, "4")
68
        case 5:
69
                assert(true, "5")
70
        case 6:
71
                assert(false, "6")
72
        case 7:
73
                assert(false, "7")
74
        case 8:
75
                assert(false, "8")
76
        case 9:
77
                assert(false, "9")
78
        default:
79
                assert(false, "default")
80
        }
81
 
82
        switch i5 {
83
        case 0, 1, 2, 3, 4:
84
                assert(false, "4")
85
        case 5:
86
                assert(true, "5")
87
        case 6, 7, 8, 9:
88
                assert(false, "9")
89
        default:
90
                assert(false, "default")
91
        }
92
 
93
        switch i5 {
94
        case 0:
95
        case 1:
96
        case 2:
97
        case 3:
98
        case 4:
99
                assert(false, "4")
100
        case 5:
101
                assert(true, "5")
102
        case 6:
103
        case 7:
104
        case 8:
105
        case 9:
106
        default:
107
                assert(i5 == 5, "good")
108
        }
109
 
110
        switch i5 {
111
        case 0:
112
                dummy := 0
113
                _ = dummy
114
                fallthrough
115
        case 1:
116
                dummy := 0
117
                _ = dummy
118
                fallthrough
119
        case 2:
120
                dummy := 0
121
                _ = dummy
122
                fallthrough
123
        case 3:
124
                dummy := 0
125
                _ = dummy
126
                fallthrough
127
        case 4:
128
                dummy := 0
129
                _ = dummy
130
                assert(false, "4")
131
        case 5:
132
                dummy := 0
133
                _ = dummy
134
                fallthrough
135
        case 6:
136
                dummy := 0
137
                _ = dummy
138
                fallthrough
139
        case 7:
140
                dummy := 0
141
                _ = dummy
142
                fallthrough
143
        case 8:
144
                dummy := 0
145
                _ = dummy
146
                fallthrough
147
        case 9:
148
                dummy := 0
149
                _ = dummy
150
                fallthrough
151
        default:
152
                dummy := 0
153
                _ = dummy
154
                assert(i5 == 5, "good")
155
        }
156
 
157
        fired := false
158
        switch i5 {
159
        case 0:
160
                dummy := 0
161
                _ = dummy
162
                fallthrough // tests scoping of cases
163
        case 1:
164
                dummy := 0
165
                _ = dummy
166
                fallthrough
167
        case 2:
168
                dummy := 0
169
                _ = dummy
170
                fallthrough
171
        case 3:
172
                dummy := 0
173
                _ = dummy
174
                fallthrough
175
        case 4:
176
                dummy := 0
177
                _ = dummy
178
                assert(false, "4")
179
        case 5:
180
                dummy := 0
181
                _ = dummy
182
                fallthrough
183
        case 6:
184
                dummy := 0
185
                _ = dummy
186
                fallthrough
187
        case 7:
188
                dummy := 0
189
                _ = dummy
190
                fallthrough
191
        case 8:
192
                dummy := 0
193
                _ = dummy
194
                fallthrough
195
        case 9:
196
                dummy := 0
197
                _ = dummy
198
                fallthrough
199
        default:
200
                dummy := 0
201
                _ = dummy
202
                fired = !fired
203
                assert(i5 == 5, "good")
204
        }
205
        assert(fired, "fired")
206
 
207
        count := 0
208
        switch i5 {
209
        case 0:
210
                count = count + 1
211
                fallthrough
212
        case 1:
213
                count = count + 1
214
                fallthrough
215
        case 2:
216
                count = count + 1
217
                fallthrough
218
        case 3:
219
                count = count + 1
220
                fallthrough
221
        case 4:
222
                count = count + 1
223
                assert(false, "4")
224
        case 5:
225
                count = count + 1
226
                fallthrough
227
        case 6:
228
                count = count + 1
229
                fallthrough
230
        case 7:
231
                count = count + 1
232
                fallthrough
233
        case 8:
234
                count = count + 1
235
                fallthrough
236
        case 9:
237
                count = count + 1
238
                fallthrough
239
        default:
240
                assert(i5 == count, "good")
241
        }
242
        assert(fired, "fired")
243
 
244
        switch hello {
245
        case "wowie":
246
                assert(false, "wowie")
247
        case "hello":
248
                assert(true, "hello")
249
        case "jumpn":
250
                assert(false, "jumpn")
251
        default:
252
                assert(false, "default")
253
        }
254
 
255
        fired = false
256
        switch i := i5 + 2; i {
257
        case i7:
258
                fired = true
259
        default:
260
                assert(false, "fail")
261
        }
262
        assert(fired, "var")
263
 
264
        // switch on nil-only comparison types
265
        switch f := func() {}; f {
266
        case nil:
267
                assert(false, "f should not be nil")
268
        default:
269
        }
270
 
271
        switch m := make(map[int]int); m {
272
        case nil:
273
                assert(false, "m should not be nil")
274
        default:
275
        }
276
 
277
        switch a := make([]int, 1); a {
278
        case nil:
279
                assert(false, "m should not be nil")
280
        default:
281
        }
282
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.