Rob Pike | 126150d | 2008-06-06 13:28:03 -0700 | [diff] [blame] | 1 | // $G $F.go && $L $F.$A && ./$A.out |
| 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 | package main |
| 8 | |
| 9 | func assertequal(is, shouldbe int, msg string) { |
| 10 | if is != shouldbe { |
Rob Pike | bc2f5f1 | 2008-08-11 22:07:49 -0700 | [diff] [blame] | 11 | print("assertion fail", msg, "\n"); |
| 12 | panic(1); |
Rob Pike | 126150d | 2008-06-06 13:28:03 -0700 | [diff] [blame] | 13 | } |
| 14 | } |
| 15 | |
| 16 | func main() { |
| 17 | i5 := 5; |
| 18 | i7 := 7; |
| 19 | |
| 20 | var count int; |
| 21 | |
| 22 | count = 0; |
| 23 | if true { |
| 24 | count = count + 1; |
| 25 | } |
| 26 | assertequal(count, 1, "if true"); |
| 27 | |
| 28 | count = 0; |
| 29 | if false { |
| 30 | count = count + 1; |
| 31 | } |
| 32 | assertequal(count, 0, "if false"); |
| 33 | |
| 34 | count = 0; |
| 35 | if one := 1; true { |
| 36 | count = count + one; |
| 37 | } |
| 38 | assertequal(count, 1, "if true one"); |
| 39 | |
| 40 | count = 0; |
| 41 | if one := 1; false { |
| 42 | count = count + 1; |
| 43 | } |
| 44 | assertequal(count, 0, "if false one"); |
| 45 | |
| 46 | count = 0; |
| 47 | if { |
| 48 | count = count + 1; |
| 49 | } |
| 50 | assertequal(count, 1, "if empty"); |
| 51 | |
| 52 | count = 0; |
Robert Griesemer | e92b753 | 2008-06-06 15:53:14 -0700 | [diff] [blame] | 53 | if one := 1; true { |
Rob Pike | 126150d | 2008-06-06 13:28:03 -0700 | [diff] [blame] | 54 | count = count + one; |
| 55 | } |
| 56 | assertequal(count, 1, "if empty one"); |
| 57 | |
| 58 | count = 0; |
| 59 | if i5 < i7 { |
| 60 | count = count + 1; |
| 61 | } |
| 62 | assertequal(count, 1, "if cond"); |
| 63 | |
| 64 | count = 0; |
| 65 | if true { |
| 66 | count = count + 1; |
| 67 | } else |
| 68 | count = count - 1; |
| 69 | assertequal(count, 1, "if else true"); |
| 70 | |
| 71 | count = 0; |
| 72 | if false { |
| 73 | count = count + 1; |
| 74 | } else |
| 75 | count = count - 1; |
| 76 | assertequal(count, -1, "if else false"); |
| 77 | |
| 78 | count = 0; |
| 79 | if t:=1; false { |
| 80 | count = count + 1; |
| 81 | t := 7; |
| 82 | } else |
| 83 | count = count - t; |
| 84 | assertequal(count, -1, "if else false var"); |
| 85 | |
| 86 | count = 0; |
| 87 | t := 1; |
| 88 | if false { |
| 89 | count = count + 1; |
| 90 | t := 7; |
| 91 | } else |
| 92 | count = count - t; |
| 93 | assertequal(count, -1, "if else false var outside"); |
| 94 | } |