tests: add mixed and complex types tests for cross assign (#5622)

pull/5627/head
yuyi 2020-07-03 00:20:03 +08:00 committed by GitHub
parent 5b900056f3
commit 68ac8fde26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 50 additions and 1 deletions

View File

@ -67,7 +67,7 @@ mut:
b int
}
fn foo3(mut f &Foo) {
fn foo3(mut f Foo) {
f.a, f.b = f.b, f.a
}
@ -78,3 +78,52 @@ fn test_cross_assign_of_struct_in_fn() {
assert a.a == 2
assert a.b == 1
}
// Test cross assign of mixed types
fn test_cross_assign_of_mixed_types() {
mut a := [0, 1]
mut m := {'one':1, 'two':2}
mut x := Zoo{a:1, b:2}
a[0], m['one'], x.a, a[1], m['two'], x.b = a[1], m['two'], x.b, a[0], m['one'], x.a
assert a == [1, 0]
assert m['one'] == 2
assert m['two'] == 1
assert x.a == 2
assert x.b == 1
}
// Test cross assign of mixed types in function
fn foo(mut a []int, mut m map[string]int, mut x Zoo) {
a[0], m['one'], x.a, a[1], m['two'], x.b = a[1], m['two'], x.b, a[0], m['one'], x.a
}
fn test_cross_assign_of_mixed_types_in_fn() {
mut a := [0, 1]
mut m := {'one':1, 'two':2}
mut x := Zoo{a:1, b:2}
foo(mut a, mut m, mut x)
assert a == [1, 0]
assert m['one'] == 2
assert m['two'] == 1
assert x.a == 2
assert x.b == 1
}
// Test cross assign of complex types
fn test_cross_assign_of_complex_types() {
mut a := [0, 1]
mut m := {'one':1, 'two':2}
mut x := Zoo{a:1, b:2}
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
assert a == [2, 0]
assert m['one'] == -2
assert m['two'] == 3
assert x.a == 2
assert x.b == -1
}