1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
fn f(n int) ?f64 {
if n < 0 {
return error('negative')
}
return 1.5 * f64(n)
}
fn test_fn_return() {
mut res := []f64{cap: 2}
for m in [-3, 5] {
if x := f(m) {
res << x
} else {
res << 31.0
}
}
assert res == [31.0, 7.5]
}
fn test_fn_return_empty() {
if _ := f(-3) {
assert false
} else {
assert true
}
}
fn test_map_get() {
mut m := {
'xy': 5
'zu': 7
}
mut res := []int{cap: 2}
for k in ['jk', 'zu'] {
if x := m[k] {
res << x
} else {
res << -17
}
}
assert res == [-17, 7]
}
fn test_map_get_empty() {
mut m := {
'xy': 5
'zu': 7
}
if _ := m['jk'] {
assert false
} else {
assert true
}
}
fn test_array_get() {
mut a := [12.5, 6.5, -17.25]
mut res := []f64{cap: 2}
for i in [1, 4] {
if x := a[i] {
res << x
} else {
res << -23.0
}
}
assert res == [6.5, -23.0]
}
fn test_array_get_empty() {
mut a := [12.5, 6.5, -17.25]
if _ := a[7] {
assert false
} else {
assert true
}
}
fn test_chan_pop() {
mut res := []f64{cap: 3}
ch := chan f64{cap: 10}
ch <- 6.75
ch <- -3.25
ch.close()
for _ in 0 .. 3 {
if x := <-ch {
res << x
} else {
res << -37.5
}
}
assert res == [6.75, -3.25, -37.5]
}
fn test_chan_pop_empty() {
ch := chan f64{cap: 10}
ch <- 6.75
ch <- -3.25
ch.close()
for i in 0 .. 3 {
if _ := <-ch {
assert i < 2
} else {
assert i == 2
}
}
}
struct Thing {
name string
}
fn test_return_if_guard() {
ret := option_check('zoo')
println(ret)
assert ret == 'zs'
}
fn option_check(name string) string {
return if thing := find_thing_by_name(name) { thing.name } else { 'safename' }
}
fn find_thing_by_name(name string) ?&Thing {
return &Thing{
name: 'zs'
}
}
|