| 1 |
// Copyright 2015 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
// Tests continue and break. |
| 6 |
|
| 7 |
package main |
| 8 |
|
| 9 |
import "testing" |
| 10 |
|
| 11 |
func continuePlain_ssa() int { |
| 12 |
var n int |
| 13 |
for i := 0; i < 10; i++ { |
| 14 |
if i == 6 { |
| 15 |
continue |
| 16 |
} |
| 17 |
n = i |
| 18 |
} |
| 19 |
return n |
| 20 |
} |
| 21 |
|
| 22 |
func continueLabeled_ssa() int { |
| 23 |
var n int |
| 24 |
Next: |
| 25 |
for i := 0; i < 10; i++ { |
| 26 |
if i == 6 { |
| 27 |
continue Next |
| 28 |
} |
| 29 |
n = i |
| 30 |
} |
| 31 |
return n |
| 32 |
} |
| 33 |
|
| 34 |
func continuePlainInner_ssa() int { |
| 35 |
var n int |
| 36 |
for j := 0; j < 30; j += 10 { |
| 37 |
for i := 0; i < 10; i++ { |
| 38 |
if i == 6 { |
| 39 |
continue |
| 40 |
} |
| 41 |
n = i |
| 42 |
} |
| 43 |
n += j |
| 44 |
} |
| 45 |
return n |
| 46 |
} |
| 47 |
|
| 48 |
func continueLabeledInner_ssa() int { |
| 49 |
var n int |
| 50 |
for j := 0; j < 30; j += 10 { |
| 51 |
Next: |
| 52 |
for i := 0; i < 10; i++ { |
| 53 |
if i == 6 { |
| 54 |
continue Next |
| 55 |
} |
| 56 |
n = i |
| 57 |
} |
| 58 |
n += j |
| 59 |
} |
| 60 |
return n |
| 61 |
} |
| 62 |
|
| 63 |
func continueLabeledOuter_ssa() int { |
| 64 |
var n int |
| 65 |
Next: |
| 66 |
for j := 0; j < 30; j += 10 { |
| 67 |
for i := 0; i < 10; i++ { |
| 68 |
if i == 6 { |
| 69 |
continue Next |
| 70 |
} |
| 71 |
n = i |
| 72 |
} |
| 73 |
n += j |
| 74 |
} |
| 75 |
return n |
| 76 |
} |
| 77 |
|
| 78 |
func breakPlain_ssa() int { |
| 79 |
var n int |
| 80 |
for i := 0; i < 10; i++ { |
| 81 |
if i == 6 { |
| 82 |
break |
| 83 |
} |
| 84 |
n = i |
| 85 |
} |
| 86 |
return n |
| 87 |
} |
| 88 |
|
| 89 |
func breakLabeled_ssa() int { |
| 90 |
var n int |
| 91 |
Next: |
| 92 |
for i := 0; i < 10; i++ { |
| 93 |
if i == 6 { |
| 94 |
break Next |
| 95 |
} |
| 96 |
n = i |
| 97 |
} |
| 98 |
return n |
| 99 |
} |
| 100 |
|
| 101 |
func breakPlainInner_ssa() int { |
| 102 |
var n int |
| 103 |
for j := 0; j < 30; j += 10 { |
| 104 |
for i := 0; i < 10; i++ { |
| 105 |
if i == 6 { |
| 106 |
break |
| 107 |
} |
| 108 |
n = i |
| 109 |
} |
| 110 |
n += j |
| 111 |
} |
| 112 |
return n |
| 113 |
} |
| 114 |
|
| 115 |
func breakLabeledInner_ssa() int { |
| 116 |
var n int |
| 117 |
for j := 0; j < 30; j += 10 { |
| 118 |
Next: |
| 119 |
for i := 0; i < 10; i++ { |
| 120 |
if i == 6 { |
| 121 |
break Next |
| 122 |
} |
| 123 |
n = i |
| 124 |
} |
| 125 |
n += j |
| 126 |
} |
| 127 |
return n |
| 128 |
} |
| 129 |
|
| 130 |
func breakLabeledOuter_ssa() int { |
| 131 |
var n int |
| 132 |
Next: |
| 133 |
for j := 0; j < 30; j += 10 { |
| 134 |
for i := 0; i < 10; i++ { |
| 135 |
if i == 6 { |
| 136 |
break Next |
| 137 |
} |
| 138 |
n = i |
| 139 |
} |
| 140 |
n += j |
| 141 |
} |
| 142 |
return n |
| 143 |
} |
| 144 |
|
| 145 |
var g, h int // globals to ensure optimizations don't collapse our switch statements |
| 146 |
|
| 147 |
func switchPlain_ssa() int { |
| 148 |
var n int |
| 149 |
switch g { |
| 150 |
case 0: |
| 151 |
n = 1 |
| 152 |
break |
| 153 |
n = 2 |
| 154 |
} |
| 155 |
return n |
| 156 |
} |
| 157 |
|
| 158 |
func switchLabeled_ssa() int { |
| 159 |
var n int |
| 160 |
Done: |
| 161 |
switch g { |
| 162 |
case 0: |
| 163 |
n = 1 |
| 164 |
break Done |
| 165 |
n = 2 |
| 166 |
} |
| 167 |
return n |
| 168 |
} |
| 169 |
|
| 170 |
func switchPlainInner_ssa() int { |
| 171 |
var n int |
| 172 |
switch g { |
| 173 |
case 0: |
| 174 |
n = 1 |
| 175 |
switch h { |
| 176 |
case 0: |
| 177 |
n += 10 |
| 178 |
break |
| 179 |
} |
| 180 |
n = 2 |
| 181 |
} |
| 182 |
return n |
| 183 |
} |
| 184 |
|
| 185 |
func switchLabeledInner_ssa() int { |
| 186 |
var n int |
| 187 |
switch g { |
| 188 |
case 0: |
| 189 |
n = 1 |
| 190 |
Done: |
| 191 |
switch h { |
| 192 |
case 0: |
| 193 |
n += 10 |
| 194 |
break Done |
| 195 |
} |
| 196 |
n = 2 |
| 197 |
} |
| 198 |
return n |
| 199 |
} |
| 200 |
|
| 201 |
func switchLabeledOuter_ssa() int { |
| 202 |
var n int |
| 203 |
Done: |
| 204 |
switch g { |
| 205 |
case 0: |
| 206 |
n = 1 |
| 207 |
switch h { |
| 208 |
case 0: |
| 209 |
n += 10 |
| 210 |
break Done |
| 211 |
} |
| 212 |
n = 2 |
| 213 |
} |
| 214 |
return n |
| 215 |
} |
| 216 |
|
| 217 |
// TestBreakContinue tests that continue and break statements do what they say. |
| 218 |
func TestBreakContinue(t *testing.T) { |
| 219 |
tests := [...]struct { |
| 220 |
name string |
| 221 |
fn func() int |
| 222 |
want int |
| 223 |
}{ |
| 224 |
{"continuePlain_ssa", continuePlain_ssa, 9}, |
| 225 |
{"continueLabeled_ssa", continueLabeled_ssa, 9}, |
| 226 |
{"continuePlainInner_ssa", continuePlainInner_ssa, 29}, |
| 227 |
{"continueLabeledInner_ssa", continueLabeledInner_ssa, 29}, |
| 228 |
{"continueLabeledOuter_ssa", continueLabeledOuter_ssa, 5}, |
| 229 |
|
| 230 |
{"breakPlain_ssa", breakPlain_ssa, 5}, |
| 231 |
{"breakLabeled_ssa", breakLabeled_ssa, 5}, |
| 232 |
{"breakPlainInner_ssa", breakPlainInner_ssa, 25}, |
| 233 |
{"breakLabeledInner_ssa", breakLabeledInner_ssa, 25}, |
| 234 |
{"breakLabeledOuter_ssa", breakLabeledOuter_ssa, 5}, |
| 235 |
|
| 236 |
{"switchPlain_ssa", switchPlain_ssa, 1}, |
| 237 |
{"switchLabeled_ssa", switchLabeled_ssa, 1}, |
| 238 |
{"switchPlainInner_ssa", switchPlainInner_ssa, 2}, |
| 239 |
{"switchLabeledInner_ssa", switchLabeledInner_ssa, 2}, |
| 240 |
{"switchLabeledOuter_ssa", switchLabeledOuter_ssa, 11}, |
| 241 |
|
| 242 |
// no select tests; they're identical to switch |
| 243 |
} |
| 244 |
|
| 245 |
for _, test := range tests { |
| 246 |
if got := test.fn(); got != test.want { |
| 247 |
t.Errorf("%s()=%d, want %d", test.name, got, test.want) |
| 248 |
} |
| 249 |
} |
| 250 |
} |