Robert Griesemer | 6a7ef36 | 2023-11-01 16:09:45 -0700 | [diff] [blame] | 1 | // run |
Russ Cox | a94347a | 2023-06-14 10:55:06 -0400 | [diff] [blame] | 2 | |
Robert Griesemer | 6a7ef36 | 2023-11-01 16:09:45 -0700 | [diff] [blame] | 3 | // Copyright 2023 The Go Authors. All rights reserved. |
Russ Cox | a94347a | 2023-06-14 10:55:06 -0400 | [diff] [blame] | 4 | // Use of this source code is governed by a BSD-style |
| 5 | // license that can be found in the LICENSE file. |
| 6 | |
Robert Griesemer | 6a7ef36 | 2023-11-01 16:09:45 -0700 | [diff] [blame] | 7 | // Test the 'for range' construct ranging over integers. |
Russ Cox | a94347a | 2023-06-14 10:55:06 -0400 | [diff] [blame] | 8 | |
| 9 | package main |
| 10 | |
Russ Cox | a94347a | 2023-06-14 10:55:06 -0400 | [diff] [blame] | 11 | func testint1() { |
Russ Cox | 2fba42c | 2023-06-14 10:56:49 -0400 | [diff] [blame] | 12 | bad := false |
Russ Cox | a94347a | 2023-06-14 10:55:06 -0400 | [diff] [blame] | 13 | j := 0 |
| 14 | for i := range int(4) { |
| 15 | if i != j { |
| 16 | println("range var", i, "want", j) |
Russ Cox | 2fba42c | 2023-06-14 10:56:49 -0400 | [diff] [blame] | 17 | bad = true |
Russ Cox | a94347a | 2023-06-14 10:55:06 -0400 | [diff] [blame] | 18 | } |
| 19 | j++ |
| 20 | } |
| 21 | if j != 4 { |
| 22 | println("wrong count ranging over 4:", j) |
Russ Cox | 2fba42c | 2023-06-14 10:56:49 -0400 | [diff] [blame] | 23 | bad = true |
| 24 | } |
| 25 | if bad { |
| 26 | panic("testint1") |
Russ Cox | a94347a | 2023-06-14 10:55:06 -0400 | [diff] [blame] | 27 | } |
| 28 | } |
| 29 | |
| 30 | func testint2() { |
Russ Cox | 2fba42c | 2023-06-14 10:56:49 -0400 | [diff] [blame] | 31 | bad := false |
Russ Cox | a94347a | 2023-06-14 10:55:06 -0400 | [diff] [blame] | 32 | j := 0 |
| 33 | for i := range 4 { |
| 34 | if i != j { |
| 35 | println("range var", i, "want", j) |
Russ Cox | 2fba42c | 2023-06-14 10:56:49 -0400 | [diff] [blame] | 36 | bad = true |
Russ Cox | a94347a | 2023-06-14 10:55:06 -0400 | [diff] [blame] | 37 | } |
| 38 | j++ |
| 39 | } |
| 40 | if j != 4 { |
| 41 | println("wrong count ranging over 4:", j) |
Russ Cox | 2fba42c | 2023-06-14 10:56:49 -0400 | [diff] [blame] | 42 | bad = true |
| 43 | } |
| 44 | if bad { |
| 45 | panic("testint2") |
Russ Cox | a94347a | 2023-06-14 10:55:06 -0400 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
| 49 | func testint3() { |
Russ Cox | 2fba42c | 2023-06-14 10:56:49 -0400 | [diff] [blame] | 50 | bad := false |
Russ Cox | a94347a | 2023-06-14 10:55:06 -0400 | [diff] [blame] | 51 | type MyInt int |
Russ Cox | a94347a | 2023-06-14 10:55:06 -0400 | [diff] [blame] | 52 | j := MyInt(0) |
| 53 | for i := range MyInt(4) { |
| 54 | if i != j { |
| 55 | println("range var", i, "want", j) |
Russ Cox | 2fba42c | 2023-06-14 10:56:49 -0400 | [diff] [blame] | 56 | bad = true |
Russ Cox | a94347a | 2023-06-14 10:55:06 -0400 | [diff] [blame] | 57 | } |
| 58 | j++ |
| 59 | } |
| 60 | if j != 4 { |
| 61 | println("wrong count ranging over 4:", j) |
Russ Cox | 2fba42c | 2023-06-14 10:56:49 -0400 | [diff] [blame] | 62 | bad = true |
| 63 | } |
| 64 | if bad { |
| 65 | panic("testint3") |
| 66 | } |
| 67 | } |
| 68 | |
Cuong Manh Le | 778880b | 2023-10-05 11:11:51 +0700 | [diff] [blame] | 69 | // Issue #63378. |
| 70 | func testint4() { |
| 71 | for i := range -1 { |
| 72 | _ = i |
| 73 | panic("must not be executed") |
| 74 | } |
| 75 | } |
| 76 | |
Cuong Manh Le | fbfe62b | 2023-12-01 00:16:24 +0700 | [diff] [blame] | 77 | // Issue #64471. |
| 78 | func testint5() { |
| 79 | for i := range 'a' { |
| 80 | var _ *rune = &i // ensure i has type rune |
| 81 | } |
| 82 | } |
| 83 | |
Russ Cox | a94347a | 2023-06-14 10:55:06 -0400 | [diff] [blame] | 84 | func main() { |
| 85 | testint1() |
| 86 | testint2() |
| 87 | testint3() |
Cuong Manh Le | 778880b | 2023-10-05 11:11:51 +0700 | [diff] [blame] | 88 | testint4() |
Cuong Manh Le | fbfe62b | 2023-12-01 00:16:24 +0700 | [diff] [blame] | 89 | testint5() |
Russ Cox | a94347a | 2023-06-14 10:55:06 -0400 | [diff] [blame] | 90 | } |