Skip to content

Commit 2202ee5

Browse files
committed
tests: add tests for continue/break in last statement of an or{}
1 parent 4e760c7 commit 2202ee5

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

vlib/v/tests/option_in_loop_test.v

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
fn opt_0_10_20(x int) ?int {
2+
if x < 0 || (x >= 10 && x <= 20) {
3+
return error('invalid')
4+
}
5+
return x
6+
}
7+
8+
fn test_options_in_for_loop_break() {
9+
mut sum := 0
10+
mut nbreaks := 0
11+
for i := 5; i < 15; i++ {
12+
x := opt_0_10_20(i) or {
13+
nbreaks++
14+
break
15+
}
16+
sum += x
17+
// println('i: ${i:3} | sum: ${sum:3}')
18+
}
19+
assert nbreaks == 1
20+
assert sum == 35
21+
}
22+
23+
fn test_options_in_for_loop_continue() {
24+
mut sum := 0
25+
mut ncontinue := 0
26+
for i := -5; i < 30; i++ {
27+
x := opt_0_10_20(i) or {
28+
ncontinue++
29+
continue
30+
}
31+
sum += x
32+
// println('i: ${i:3} | sum: ${sum:3}')
33+
}
34+
assert ncontinue == 16
35+
assert sum == 270
36+
}

0 commit comments

Comments
 (0)