[go: up one dir, main page]

blob: f58a398f94c16c95623217ad3e06e8b12d4184a6 [file] [log] [blame]
Robert Griesemer6a7ef362023-11-01 16:09:45 -07001// run
Russ Coxa94347a2023-06-14 10:55:06 -04002
Robert Griesemer6a7ef362023-11-01 16:09:45 -07003// Copyright 2023 The Go Authors. All rights reserved.
Russ Coxa94347a2023-06-14 10:55:06 -04004// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file.
6
Robert Griesemer6a7ef362023-11-01 16:09:45 -07007// Test the 'for range' construct ranging over integers.
Russ Coxa94347a2023-06-14 10:55:06 -04008
9package main
10
Russ Coxa94347a2023-06-14 10:55:06 -040011func testint1() {
Russ Cox2fba42c2023-06-14 10:56:49 -040012 bad := false
Russ Coxa94347a2023-06-14 10:55:06 -040013 j := 0
14 for i := range int(4) {
15 if i != j {
16 println("range var", i, "want", j)
Russ Cox2fba42c2023-06-14 10:56:49 -040017 bad = true
Russ Coxa94347a2023-06-14 10:55:06 -040018 }
19 j++
20 }
21 if j != 4 {
22 println("wrong count ranging over 4:", j)
Russ Cox2fba42c2023-06-14 10:56:49 -040023 bad = true
24 }
25 if bad {
26 panic("testint1")
Russ Coxa94347a2023-06-14 10:55:06 -040027 }
28}
29
30func testint2() {
Russ Cox2fba42c2023-06-14 10:56:49 -040031 bad := false
Russ Coxa94347a2023-06-14 10:55:06 -040032 j := 0
33 for i := range 4 {
34 if i != j {
35 println("range var", i, "want", j)
Russ Cox2fba42c2023-06-14 10:56:49 -040036 bad = true
Russ Coxa94347a2023-06-14 10:55:06 -040037 }
38 j++
39 }
40 if j != 4 {
41 println("wrong count ranging over 4:", j)
Russ Cox2fba42c2023-06-14 10:56:49 -040042 bad = true
43 }
44 if bad {
45 panic("testint2")
Russ Coxa94347a2023-06-14 10:55:06 -040046 }
47}
48
49func testint3() {
Russ Cox2fba42c2023-06-14 10:56:49 -040050 bad := false
Russ Coxa94347a2023-06-14 10:55:06 -040051 type MyInt int
Russ Coxa94347a2023-06-14 10:55:06 -040052 j := MyInt(0)
53 for i := range MyInt(4) {
54 if i != j {
55 println("range var", i, "want", j)
Russ Cox2fba42c2023-06-14 10:56:49 -040056 bad = true
Russ Coxa94347a2023-06-14 10:55:06 -040057 }
58 j++
59 }
60 if j != 4 {
61 println("wrong count ranging over 4:", j)
Russ Cox2fba42c2023-06-14 10:56:49 -040062 bad = true
63 }
64 if bad {
65 panic("testint3")
66 }
67}
68
Cuong Manh Le778880b2023-10-05 11:11:51 +070069// Issue #63378.
70func testint4() {
71 for i := range -1 {
72 _ = i
73 panic("must not be executed")
74 }
75}
76
Cuong Manh Lefbfe62b2023-12-01 00:16:24 +070077// Issue #64471.
78func testint5() {
79 for i := range 'a' {
80 var _ *rune = &i // ensure i has type rune
81 }
82}
83
Russ Coxa94347a2023-06-14 10:55:06 -040084func main() {
85 testint1()
86 testint2()
87 testint3()
Cuong Manh Le778880b2023-10-05 11:11:51 +070088 testint4()
Cuong Manh Lefbfe62b2023-12-01 00:16:24 +070089 testint5()
Russ Coxa94347a2023-06-14 10:55:06 -040090}