2023-11-01 17:09:45 -06:00
|
|
|
// run
|
2023-06-14 08:55:06 -06:00
|
|
|
|
2023-11-01 17:09:45 -06:00
|
|
|
// Copyright 2023 The Go Authors. All rights reserved.
|
2023-06-14 08:55:06 -06:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2023-11-01 17:09:45 -06:00
|
|
|
// Test the 'for range' construct ranging over integers.
|
2023-06-14 08:55:06 -06:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
func testint1() {
|
2023-06-14 08:56:49 -06:00
|
|
|
bad := false
|
2023-06-14 08:55:06 -06:00
|
|
|
j := 0
|
|
|
|
for i := range int(4) {
|
|
|
|
if i != j {
|
|
|
|
println("range var", i, "want", j)
|
2023-06-14 08:56:49 -06:00
|
|
|
bad = true
|
2023-06-14 08:55:06 -06:00
|
|
|
}
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
if j != 4 {
|
|
|
|
println("wrong count ranging over 4:", j)
|
2023-06-14 08:56:49 -06:00
|
|
|
bad = true
|
|
|
|
}
|
|
|
|
if bad {
|
|
|
|
panic("testint1")
|
2023-06-14 08:55:06 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testint2() {
|
2023-06-14 08:56:49 -06:00
|
|
|
bad := false
|
2023-06-14 08:55:06 -06:00
|
|
|
j := 0
|
|
|
|
for i := range 4 {
|
|
|
|
if i != j {
|
|
|
|
println("range var", i, "want", j)
|
2023-06-14 08:56:49 -06:00
|
|
|
bad = true
|
2023-06-14 08:55:06 -06:00
|
|
|
}
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
if j != 4 {
|
|
|
|
println("wrong count ranging over 4:", j)
|
2023-06-14 08:56:49 -06:00
|
|
|
bad = true
|
|
|
|
}
|
|
|
|
if bad {
|
|
|
|
panic("testint2")
|
2023-06-14 08:55:06 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testint3() {
|
2023-06-14 08:56:49 -06:00
|
|
|
bad := false
|
2023-06-14 08:55:06 -06:00
|
|
|
type MyInt int
|
|
|
|
j := MyInt(0)
|
|
|
|
for i := range MyInt(4) {
|
|
|
|
if i != j {
|
|
|
|
println("range var", i, "want", j)
|
2023-06-14 08:56:49 -06:00
|
|
|
bad = true
|
2023-06-14 08:55:06 -06:00
|
|
|
}
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
if j != 4 {
|
|
|
|
println("wrong count ranging over 4:", j)
|
2023-06-14 08:56:49 -06:00
|
|
|
bad = true
|
|
|
|
}
|
|
|
|
if bad {
|
|
|
|
panic("testint3")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-04 22:11:51 -06:00
|
|
|
// Issue #63378.
|
|
|
|
func testint4() {
|
|
|
|
for i := range -1 {
|
|
|
|
_ = i
|
|
|
|
panic("must not be executed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-14 08:55:06 -06:00
|
|
|
func main() {
|
|
|
|
testint1()
|
|
|
|
testint2()
|
|
|
|
testint3()
|
2023-10-04 22:11:51 -06:00
|
|
|
testint4()
|
2023-06-14 08:55:06 -06:00
|
|
|
}
|