modernc.org/gc@v1.0.1-0.20240304020402-f0dba7c97c2b/testdata/errchk/test/fixedbugs/issue5162.go (about)

     1  // runoutput
     2  
     3  // Copyright 2013 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  // issue 5162: bad array equality when multiple comparisons
     8  // happen in the same expression.
     9  
    10  package main
    11  
    12  import (
    13  	"fmt"
    14  	"strings"
    15  )
    16  
    17  const template = `
    18  func CheckEqNNN_TTT() {
    19  	onesA := [NNN]ttt{ONES}
    20  	onesB := [NNN]ttt{ONES}
    21  	twos := [NNN]ttt{TWOS}
    22  	if onesA != onesB {
    23  		println("onesA != onesB in CheckEqNNN_TTT")
    24  	}
    25  	if onesA == twos {
    26  		println("onesA == twos in CheckEqNNN_TTT")
    27  	}
    28  	if onesB == twos {
    29  		println("onesB == twos in CheckEqNNN_TTT")
    30  	}
    31  	if s := fmt.Sprint(onesA == onesB, onesA != twos, onesB != twos); s != "true true true" {
    32  		println("fail in CheckEqNNN_TTT:", s)
    33  	}
    34  }
    35  
    36  func CheckEqNNN_TTTExtraVar() {
    37  	onesA := [NNN]ttt{ONES}
    38  	onesB := [NNN]ttt{ONES}
    39  	twos := [NNN]ttt{TWOS}
    40  	onesX := onesA
    41  	if onesA != onesB {
    42  		println("onesA != onesB in CheckEqNNN_TTTExtraVar")
    43  	}
    44  	if onesA == twos {
    45  		println("onesA == twos in CheckEqNNN_TTTExtraVar")
    46  	}
    47  	if onesB == twos {
    48  		println("onesB == twos in CheckEqNNN_TTTExtraVar")
    49  	}
    50  	if s := fmt.Sprint(onesA == onesB, onesA != twos, onesB != twos); s != "true true true" {
    51  		println("fail in CheckEqNNN_TTTExtraVar:", s)
    52  	}
    53  	if s := fmt.Sprint(onesB == onesX); s != "true" {
    54  		println("extra var fail in CheckEqNNN_TTTExtraVar")
    55  	}
    56  }
    57  `
    58  
    59  func main() {
    60  	fmt.Print("// run\n\n")
    61  	fmt.Print("// THIS FILE IS AUTO-GENERATED\n\n")
    62  	fmt.Print("package main\n\n")
    63  	fmt.Println(`import "fmt"`)
    64  
    65  	types := []string{
    66  		"int", "int8", "int16", "int32", "int64",
    67  		"uint", "uint8", "uint16", "uint32", "uint64",
    68  		"float32", "float64"}
    69  	tocall := make([]string, 0, 32*len(types))
    70  	for i := 1; i <= 32; i++ {
    71  		for _, typ := range types {
    72  			src := template
    73  			src = strings.Replace(src, "NNN", fmt.Sprint(i), -1)
    74  			src = strings.Replace(src, "TTT", strings.Title(typ), -1)
    75  			src = strings.Replace(src, "ttt", typ, -1)
    76  			src = strings.Replace(src, "ONES", "1"+strings.Repeat(", 1", i-1), -1)
    77  			src = strings.Replace(src, "TWOS", "2"+strings.Repeat(", 2", i-1), -1)
    78  			fmt.Print(src)
    79  			tocall = append(tocall, fmt.Sprintf("CheckEq%d_%s", i, strings.Title(typ)))
    80  		}
    81  	}
    82  	fmt.Println("func main() {")
    83  	for _, fun := range tocall {
    84  		fmt.Printf("\t%s()\n", fun)
    85  		fmt.Printf("\t%sExtraVar()\n", fun)
    86  	}
    87  	fmt.Println("}")
    88  }