github.com/zach-klippenstein/go@v0.0.0-20150108044943-fcfbeb3adf58/test/if.go (about)

     1  // run
     2  
     3  // Copyright 2009 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 if statements in various forms.
     8  
     9  package main
    10  
    11  func assertequal(is, shouldbe int, msg string) {
    12  	if is != shouldbe {
    13  		print("assertion fail", msg, "\n")
    14  		panic(1)
    15  	}
    16  }
    17  
    18  func main() {
    19  	i5 := 5
    20  	i7 := 7
    21  
    22  	var count int
    23  
    24  	count = 0
    25  	if true {
    26  		count = count + 1
    27  	}
    28  	assertequal(count, 1, "if true")
    29  
    30  	count = 0
    31  	if false {
    32  		count = count + 1
    33  	}
    34  	assertequal(count, 0, "if false")
    35  
    36  	count = 0
    37  	if one := 1; true {
    38  		count = count + one
    39  	}
    40  	assertequal(count, 1, "if true one")
    41  
    42  	count = 0
    43  	if one := 1; false {
    44  		count = count + 1
    45  		_ = one
    46  	}
    47  	assertequal(count, 0, "if false one")
    48  
    49  	count = 0
    50  	if i5 < i7 {
    51  		count = count + 1
    52  	}
    53  	assertequal(count, 1, "if cond")
    54  
    55  	count = 0
    56  	if true {
    57  		count = count + 1
    58  	} else {
    59  		count = count - 1
    60  	}
    61  	assertequal(count, 1, "if else true")
    62  
    63  	count = 0
    64  	if false {
    65  		count = count + 1
    66  	} else {
    67  		count = count - 1
    68  	}
    69  	assertequal(count, -1, "if else false")
    70  
    71  	count = 0
    72  	if t := 1; false {
    73  		count = count + 1
    74  		_ = t
    75  		t := 7
    76  		_ = t
    77  	} else {
    78  		count = count - t
    79  	}
    80  	assertequal(count, -1, "if else false var")
    81  
    82  	count = 0
    83  	t := 1
    84  	if false {
    85  		count = count + 1
    86  		t := 7
    87  		_ = t
    88  	} else {
    89  		count = count - t
    90  	}
    91  	_ = t
    92  	assertequal(count, -1, "if else false var outside")
    93  }