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
|
// run
// Copyright 2023 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.
// Test the 'for range' construct ranging over integers.
package main
func testint1() {
bad := false
j := 0
for i := range int(4) {
if i != j {
println("range var", i, "want", j)
bad = true
}
j++
}
if j != 4 {
println("wrong count ranging over 4:", j)
bad = true
}
if bad {
panic("testint1")
}
}
func testint2() {
bad := false
j := 0
for i := range 4 {
if i != j {
println("range var", i, "want", j)
bad = true
}
j++
}
if j != 4 {
println("wrong count ranging over 4:", j)
bad = true
}
if bad {
panic("testint2")
}
}
func testint3() {
bad := false
type MyInt int
j := MyInt(0)
for i := range MyInt(4) {
if i != j {
println("range var", i, "want", j)
bad = true
}
j++
}
if j != 4 {
println("wrong count ranging over 4:", j)
bad = true
}
if bad {
panic("testint3")
}
}
// Issue #63378.
func testint4() {
for i := range -1 {
_ = i
panic("must not be executed")
}
}
// Issue #64471.
func testint5() {
for i := range 'a' {
var _ *rune = &i // ensure i has type rune
}
}
func main() {
testint1()
testint2()
testint3()
testint4()
testint5()
}
|