v2 / vlib / v / tests / loops / for_loops_2_test.v
55 lines · 53 sloc · 679 bytes · 6488041a749df9762348d019c4223908c476f2e2
Raw
1fn test_for_match() {
2 mut a := 2
3 mut b := 0
4 for {
5 match a {
6 2 {
7 println('a == 2')
8 a = 0
9 continue
10 }
11 0 {
12 println('a == 0')
13 a = 5
14 b++
15 break
16 }
17 else {
18 println('unexpected branch')
19 break
20 }
21 }
22 }
23 assert a == 5
24 assert b == 1
25}
26
27fn test_for_select() {
28 ch1 := chan int{}
29 ch2 := chan f64{}
30 spawn do_send(ch1, ch2)
31 mut a := 0
32 mut b := 0
33 for select {
34 x := <-ch1 {
35 a += x
36 }
37 y := <-ch2 {
38 a += int(y)
39 }
40 } {
41 // count number of receive events
42 b++
43 println('${b}. event')
44 }
45 assert a == 10
46 assert b == 3
47}
48
49fn do_send(ch1 chan int, ch2 chan f64) {
50 ch1 <- 3
51 ch2 <- 5.0
52 ch2.close()
53 ch1 <- 2
54 ch1.close()
55}
56