v2 / vlib / v / tests / fns / fn_mut_args_test.v
89 lines · 73 sloc · 1.22 KB · 6488041a749df9762348d019c4223908c476f2e2
Raw
1fn func(mut a []int) {
2 a = [1, 2, 3, 4]
3 println('inside fn: ${a}')
4 assert '${a}' == '[1, 2, 3, 4]'
5}
6
7fn test_fn_mut_args_of_array() {
8 mut a := [1, 2, 3]
9 func(mut a)
10 println('inside main: ${a}')
11 assert '${a}' == '[1, 2, 3, 4]'
12}
13
14fn init_map(mut n map[string]int) {
15 n = {
16 'one': 1
17 }
18}
19
20fn test_fn_mut_args_of_map() {
21 mut m := map[string]int{}
22 init_map(mut m)
23 println(m)
24 assert m == {
25 'one': 1
26 }
27}
28
29struct MyData {
30pub mut:
31 ar []int
32}
33
34fn pass_array_mut(mut ar []int) int {
35 if ar.len > 0 && ar.last() == 99 {
36 return 99
37 }
38 return 0
39}
40
41fn test_fn_mut_args_of_array_last() {
42 mut m := MyData{}
43 m.ar << 99
44 assert pass_array_mut(mut m.ar) == 99
45}
46
47interface ChildInterface {
48 data int
49}
50
51struct Child {
52 data int
53}
54
55struct Parent {
56mut:
57 children []ChildInterface
58}
59
60fn (mut p Parent) add(mut x ChildInterface) {
61 p.children << x
62}
63
64fn test_fn_mut_args_of_interface() {
65 mut x := Parent{}
66 x.add(mut Child{ data: 123 })
67 println(x.children[0].data)
68 assert x.children[0].data == 123
69}
70
71struct LinuxFile {
72}
73
74interface File {
75}
76
77fn b(parent File) {
78 println(parent)
79 assert '${parent}' == 'File(LinuxFile{})'
80}
81
82fn a(mut parent File) {
83 b(parent)
84}
85
86fn test_fn_mut_args_of_interface2() {
87 mut file := LinuxFile{}
88 a(mut file)
89}
90