v2 / vlib / v / tests / conditions / matches / match_const_range_test.v
78 lines · 71 sloc · 1.46 KB · 8e35f4d9848f7ad35d857a187dddbfd2eca5e19d
Raw
1const start = 1
2const start_2 = 4
3const end = 3
4const end_2 = 8
5//
6const start_rune = `a`
7const start_2_rune = `d`
8const end_rune = `c`
9const end_2_rune = `i`
10//
11const start_cast_expr = u16(1)
12const end_cast_expr = u16(5)
13
14fn test_match_int_const_ranges() {
15 mut results := []int{}
16 for x in 0 .. 10 {
17 match x {
18 start...end { results << 1 }
19 start_2...5 { results << 2 }
20 6...end_2 { results << 3 }
21 else { results << 4 }
22 }
23 }
24 assert results == [4, 1, 1, 1, 2, 2, 3, 3, 3, 4]
25}
26
27fn test_match_rune_const_ranges() {
28 mut results := []int{}
29 for x in `a` .. `l` {
30 match x {
31 start_rune...end_rune { results << 1 }
32 start_2_rune...`e` { results << 2 }
33 `f`...end_2_rune { results << 3 }
34 else { results << 4 }
35 }
36 }
37 assert results == [1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4]
38}
39
40fn test_match_expr_int_const_ranges() {
41 mut results := []int{}
42 for x in 0 .. 10 {
43 result := match x {
44 start...end { 1 }
45 start_2...5 { 2 }
46 6...end_2 { 3 }
47 else { 4 }
48 }
49
50 results << result
51 }
52 assert results == [4, 1, 1, 1, 2, 2, 3, 3, 3, 4]
53}
54
55fn test_match_expr_rune_const_ranges() {
56 mut results := []int{}
57 for x in `a` .. `l` {
58 result := match x {
59 start_rune...end_rune { 1 }
60 start_2_rune...`e` { 2 }
61 `f`...end_2_rune { 3 }
62 else { 4 }
63 }
64
65 results << result
66 }
67 assert results == [1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4]
68}
69
70fn test_match_expr_integer_cast_const_ranges() {
71 c := u16(3)
72 match c {
73 start_cast_expr...end_cast_expr {
74 assert c == u16(3)
75 }
76 else {}
77 }
78}
79