1// Test cross assign of array elements
2fn test_cross_assign_of_array() {
3	mut a := [0, 1]
4	a[0], a[1] = a[1], a[0]
5	assert a[0] == 1
6	assert a[1] == 0
7
8	mut b1 := [1, 2, 3]
9	mut b2 := 4
10	b1[2], b2, b1[0] = b1[0], b1[2], 5
11	assert b1 == [5, 2, 1]
12	assert b2 == 3
13}
14
15// Test cross assign of array elements in function
16fn foo1(mut arr []int) {
17	arr[0], arr[1] = arr[1], arr[0]
18}
19
20fn test_cross_assign_of_array_in_fn() {
21	mut arr := [1,2]
22	foo1(mut arr)
23	assert arr[0] == 2
24	assert arr[1] == 1
25}
26
27// Test cross assign of map values
28fn test_cross_assign_of_map() {
29	mut a := {'one':1, 'two':2}
30	a['one'], a['two'] = a['two'], a['one']
31	println(a)
32	assert a['one'] == 2
33	assert a['two'] == 1
34}
35
36// Test cross assign of map values in function
37fn foo2(mut a map[string]int) {
38	a['one'], a['two'] = a['two'], a['one']
39}
40
41fn test_cross_assign_of_map_in_fn() {
42	mut a := {'one':1, 'two':2}
43	foo2(mut a)
44	assert a['one'] == 2
45	assert a['two'] == 1
46}
47
48// Test cross assign of struct fields
49struct Zoo {
50mut:
51	a int
52	b int
53}
54
55fn test_cross_assign_of_struct() {
56	mut x := Zoo{a:1, b:2}
57	x.a, x.b = x.b, x.a
58	//println(x)
59	assert x.a == 2
60	assert x.b == 1
61}
62
63// Test cross assign of struct fields in function
64struct Foo {
65mut:
66	a int
67	b int
68}
69
70fn foo3(mut f Foo) {
71	f.a, f.b = f.b, f.a
72}
73
74fn test_cross_assign_of_struct_in_fn() {
75	mut a := Foo{a:1, b:2}
76	foo3(mut a)
77	println(a)
78	assert a.a == 2
79	assert a.b == 1
80}
81
82// Test cross assign of mixed types
83fn test_cross_assign_of_mixed_types() {
84	mut a := [0, 1]
85	mut m := {'one':1, 'two':2}
86	mut x := Zoo{a:1, b:2}
87
88	a[0], m['one'], x.a, a[1], m['two'], x.b = a[1], m['two'], x.b, a[0], m['one'], x.a
89
90	assert a == [1, 0]
91	assert m['one'] == 2
92	assert m['two'] == 1
93	assert x.a == 2
94	assert x.b == 1
95}
96
97// Test cross assign of mixed types in function
98fn foo(mut a []int,  mut m map[string]int, mut x Zoo) {
99	a[0], m['one'], x.a, a[1], m['two'], x.b = a[1], m['two'], x.b, a[0], m['one'], x.a
100}
101
102fn test_cross_assign_of_mixed_types_in_fn() {
103	mut a := [0, 1]
104	mut m := {'one':1, 'two':2}
105	mut x := Zoo{a:1, b:2}
106
107	foo(mut a, mut m, mut x)
108
109	assert a == [1, 0]
110	assert m['one'] == 2
111	assert m['two'] == 1
112	assert x.a == 2
113	assert x.b == 1
114}
115
116// Test cross assign of complex types
117fn test_cross_assign_of_complex_types() {
118	mut a := [0, 1]
119	mut m := {'one':1, 'two':2}
120	mut x := Zoo{a:1, b:2}
121
122	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
123
124	assert a == [2, 0]
125	assert m['one'] == -2
126	assert m['two'] == 3
127	assert x.a == 2
128	assert x.b == -1
129}
130