Source file test/range3.go

     1  // run
     2  
     3  // Copyright 2023 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  // Test the 'for range' construct ranging over integers.
     8  
     9  package main
    10  
    11  func testint1() {
    12  	bad := false
    13  	j := 0
    14  	for i := range int(4) {
    15  		if i != j {
    16  			println("range var", i, "want", j)
    17  			bad = true
    18  		}
    19  		j++
    20  	}
    21  	if j != 4 {
    22  		println("wrong count ranging over 4:", j)
    23  		bad = true
    24  	}
    25  	if bad {
    26  		panic("testint1")
    27  	}
    28  }
    29  
    30  func testint2() {
    31  	bad := false
    32  	j := 0
    33  	for i := range 4 {
    34  		if i != j {
    35  			println("range var", i, "want", j)
    36  			bad = true
    37  		}
    38  		j++
    39  	}
    40  	if j != 4 {
    41  		println("wrong count ranging over 4:", j)
    42  		bad = true
    43  	}
    44  	if bad {
    45  		panic("testint2")
    46  	}
    47  }
    48  
    49  func testint3() {
    50  	bad := false
    51  	type MyInt int
    52  	j := MyInt(0)
    53  	for i := range MyInt(4) {
    54  		if i != j {
    55  			println("range var", i, "want", j)
    56  			bad = true
    57  		}
    58  		j++
    59  	}
    60  	if j != 4 {
    61  		println("wrong count ranging over 4:", j)
    62  		bad = true
    63  	}
    64  	if bad {
    65  		panic("testint3")
    66  	}
    67  }
    68  
    69  // Issue #63378.
    70  func testint4() {
    71  	for i := range -1 {
    72  		_ = i
    73  		panic("must not be executed")
    74  	}
    75  }
    76  
    77  // Issue #64471.
    78  func testint5() {
    79  	for i := range 'a' {
    80  		var _ *rune = &i // ensure i has type rune
    81  	}
    82  }
    83  
    84  func main() {
    85  	testint1()
    86  	testint2()
    87  	testint3()
    88  	testint4()
    89  	testint5()
    90  }
    91  

View as plain text