Source file test/fixedbugs/issue24817.go

     1  // run
     2  
     3  // Copyright 2018 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  // Check all ways to compare a non-constant string to the empty string.
     8  
     9  package main
    10  
    11  import (
    12  	"fmt"
    13  	"os"
    14  )
    15  
    16  var (
    17  	s      = "abc"
    18  	e      = ""
    19  	failed bool
    20  )
    21  
    22  func main() {
    23  	want(true, "" < s, `"" < s`)
    24  	want(false, s < "", `s < ""`)
    25  	want(false, "" < e, `"" < e`)
    26  	want(false, e < "", `e < ""`)
    27  
    28  	want(true, "" <= s, `"" <= s`)
    29  	want(false, s <= "", `s <= ""`)
    30  	want(true, "" <= e, `"" <= e`)
    31  	want(true, e <= "", `e <= ""`)
    32  
    33  	want(false, "" > s, `"" > s`)
    34  	want(true, s > "", `s > ""`)
    35  	want(false, "" > e, `"" > e`)
    36  	want(false, e > "", `e > ""`)
    37  
    38  	want(false, "" >= s, `"" >= s`)
    39  	want(true, s >= "", `s >= ""`)
    40  	want(true, "" >= e, `"" >= e`)
    41  	want(true, e >= "", `e >= ""`)
    42  
    43  	want(false, "" == s, `"" == s`)
    44  	want(false, s == "", `s == ""`)
    45  	want(true, "" == e, `"" == e`)
    46  	want(true, e == "", `e == ""`)
    47  
    48  	want(true, "" != s, `"" != s`)
    49  	want(true, s != "", `s != ""`)
    50  	want(false, "" != e, `"" != e`)
    51  	want(false, e != "", `e != ""`)
    52  
    53  	if failed {
    54  		os.Exit(1)
    55  	}
    56  }
    57  
    58  //go:noinline
    59  func want(b bool, have bool, msg string) {
    60  	if b != have {
    61  		fmt.Println(msg)
    62  		failed = true
    63  	}
    64  }
    65  

View as plain text