v2 / vlib / v / tests / assign / cross_assign_test.v
191 lines · 163 sloc · 3.01 KB · 6488041a749df9762348d019c4223908c476f2e2
Raw
1import math.big
2
3// Test cross assign of array elements
4fn test_cross_assign_of_array() {
5 mut a := [0, 1]
6 a[0], a[1] = a[1], a[0]
7 assert a[0] == 1
8 assert a[1] == 0
9
10 mut b1 := [1, 2, 3]
11 mut b2 := 4
12 b1[2], b2, b1[0] = b1[0], b1[2], 5
13 assert b1 == [5, 2, 1]
14 assert b2 == 3
15}
16
17// Test cross assign of array elements in function
18fn foo1(mut arr []int) {
19 arr[0], arr[1] = arr[1], arr[0]
20}
21
22fn test_cross_assign_of_array_in_fn() {
23 mut arr := [1, 2]
24 foo1(mut arr)
25 assert arr[0] == 2
26 assert arr[1] == 1
27}
28
29// Test cross assign of map values
30fn test_cross_assign_of_map() {
31 mut a := {
32 'one': 1
33 'two': 2
34 }
35 a['one'], a['two'] = a['two'], a['one']
36 println(a)
37 assert a['one'] == 2
38 assert a['two'] == 1
39}
40
41// Test cross assign of map values in function
42fn foo2(mut a map[string]int) {
43 a['one'], a['two'] = a['two'], a['one']
44}
45
46fn test_cross_assign_of_map_in_fn() {
47 mut a := {
48 'one': 1
49 'two': 2
50 }
51 foo2(mut a)
52 assert a['one'] == 2
53 assert a['two'] == 1
54}
55
56// Test cross assign of struct fields
57struct Zoo {
58mut:
59 a int
60 b int
61}
62
63fn test_cross_assign_of_struct() {
64 mut x := Zoo{
65 a: 1
66 b: 2
67 }
68 x.a, x.b = x.b, x.a
69 // println(x)
70 assert x.a == 2
71 assert x.b == 1
72}
73
74// Test cross assign of struct fields in function
75struct Foo {
76mut:
77 a int
78 b int
79}
80
81fn foo3(mut f Foo) {
82 f.a, f.b = f.b, f.a
83}
84
85fn test_cross_assign_of_struct_in_fn() {
86 mut a := Foo{
87 a: 1
88 b: 2
89 }
90 foo3(mut a)
91 println(a)
92 assert a.a == 2
93 assert a.b == 1
94}
95
96// Test cross assign of mixed types
97fn test_cross_assign_of_mixed_types() {
98 mut a := [0, 1]
99 mut m := {
100 'one': 1
101 'two': 2
102 }
103 mut x := Zoo{
104 a: 1
105 b: 2
106 }
107
108 a[0], m['one'], x.a, a[1], m['two'], x.b = a[1], m['two'], x.b, a[0], m['one'], x.a
109
110 assert a == [1, 0]
111 assert m['one'] == 2
112 assert m['two'] == 1
113 assert x.a == 2
114 assert x.b == 1
115}
116
117// Test cross assign of mixed types in function
118fn foo(mut a []int, mut m map[string]int, mut x Zoo) {
119 a[0], m['one'], x.a, a[1], m['two'], x.b = a[1], m['two'], x.b, a[0], m['one'], x.a
120}
121
122fn test_cross_assign_of_mixed_types_in_fn() {
123 mut a := [0, 1]
124 mut m := {
125 'one': 1
126 'two': 2
127 }
128 mut x := Zoo{
129 a: 1
130 b: 2
131 }
132
133 foo(mut a, mut m, mut x)
134
135 assert a == [1, 0]
136 assert m['one'] == 2
137 assert m['two'] == 1
138 assert x.a == 2
139 assert x.b == 1
140}
141
142// Test cross assign of complex types
143fn test_cross_assign_of_complex_types() {
144 mut a := [0, 1]
145 mut m := {
146 'one': 1
147 'two': 2
148 }
149 mut x := Zoo{
150 a: 1
151 b: 2
152 }
153
154 a[0], m['one'], x.a, a[1], m['two'], x.b = a[1] + 1, -m['two'], x.b, a[0] * 2, m['one'] * 3, x.a - x.b
155
156 assert a == [2, 0]
157 assert m['one'] == -2
158 assert m['two'] == 3
159 assert x.a == 2
160 assert x.b == -1
161}
162
163fn test_cross_assign_of_big_int() {
164 mut a := big.zero_int
165 mut b := big.one_int
166
167 a, b = a + b, a
168 println(a)
169 assert a == big.one_int
170}
171
172fn test_cross_assign_of_reserved_name_variable() {
173 mut small := 1
174 mut big_ := 2
175 mut sum := 2
176
177 for big_ < 4_000_000 {
178 small, big_ = big_, small + big_
179 if big_ % 2 == 0 {
180 sum += big_
181 }
182 }
183 println(small)
184 assert small == 3524578
185
186 println(big_)
187 assert big_ == 5702887
188
189 println(sum)
190 assert sum == 4613732
191}
192