tests: supplement multi-instances generic testing (#11170)

pull/11173/head
yuyi 2021-08-13 13:38:20 +08:00 committed by GitHub
parent 579aa7b1b5
commit 083a90148d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 155 additions and 35 deletions

View File

@ -9,9 +9,21 @@ fn get_test<T>(v T) Test<T> {
}
fn test_generics_assign_generics_struct() {
x := get_test(1)
println('$x.v')
assert x.v == 1
x1 := get_test(1)
println('$x1.v')
assert x1.v == 1
x2 := get_test(2.2)
println('$x2.v')
assert x2.v == 2.2
x3 := get_test('aaa')
println('$x3.v')
assert x3.v == 'aaa'
x4 := get_test(true)
println('$x4.v')
assert x4.v == true
}
// test generics assign generics struct_init
@ -31,8 +43,23 @@ fn (mut n Node<T>) add(val T) {
}
fn test_generic_fn_assign_generic_struct_init() {
mut list := new<int>()
list.add(100)
println(list.next)
assert list.next.val == 100
mut list1 := new<int>()
list1.add(100)
println(list1.next)
assert list1.next.val == 100
mut list2 := new<f64>()
list2.add(2.22)
println(list2.next)
assert list2.next.val == 2.22
mut list3 := new<bool>()
list3.add(false)
println(list3.next)
assert list3.next.val == false
mut list4 := new<string>()
list4.add('aaa')
println(list4.next)
assert list4.next.val == 'aaa'
}

View File

@ -25,14 +25,36 @@ fn set<A, B>(mut opt Foo<A, B>, a A, b B) {
}
fn test_generics_return_multiple_generics_struct() {
mut o := new_foo<int, string>(23, 'aaa')
println(get_a<int, string>(o))
assert get_a<int, string>(o) == 23
println(get_b<int, string>(o))
assert get_b<int, string>(o) == 'aaa'
set<int, string>(mut o, 42, 'bbb')
println(get_a<int, string>(o))
assert get_a<int, string>(o) == 42
println(get_b<int, string>(o))
assert get_b<int, string>(o) == 'bbb'
mut o1 := new_foo<int, string>(23, 'aaa')
println(get_a<int, string>(o1))
assert get_a<int, string>(o1) == 23
println(get_b<int, string>(o1))
assert get_b<int, string>(o1) == 'aaa'
set<int, string>(mut o1, 42, 'bbb')
println(get_a<int, string>(o1))
assert get_a<int, string>(o1) == 42
println(get_b<int, string>(o1))
assert get_b<int, string>(o1) == 'bbb'
mut o2 := new_foo<string, int>('bbb', 22)
println(get_a<string, int>(o2))
assert get_a<string, int>(o2) == 'bbb'
println(get_b<string, int>(o2))
assert get_b<string, int>(o2) == 22
set<string, int>(mut o2, 'bbb', 42)
println(get_a<string, int>(o2))
assert get_a<string, int>(o2) == 'bbb'
println(get_b<string, int>(o2))
assert get_b<string, int>(o2) == 42
mut o3 := new_foo<int, bool>(23, true)
println(get_a<int, bool>(o3))
assert get_a<int, bool>(o3) == 23
println(get_b<int, bool>(o3))
assert get_b<int, bool>(o3) == true
set<int, bool>(mut o3, 42, false)
println(get_a<int, bool>(o3))
assert get_a<int, bool>(o3) == 42
println(get_b<int, bool>(o3))
assert get_b<int, bool>(o3) == false
}

View File

@ -12,7 +12,19 @@ fn make_node<T>(val []T) Node<T> {
}
fn test_generics_return_recursive_generics_struct() {
n := make_node([1, 2, 3])
println(n.val)
assert n.val == 1
n1 := make_node([1, 2, 3])
println(n1.val)
assert n1.val == 1
n2 := make_node([1.1, 2.2, 3.3])
println(n2.val)
assert n2.val == 1.1
n3 := make_node([true, false, true])
println(n3.val)
assert n3.val == true
n4 := make_node(['aa', 'bb', 'cc'])
println(n4.val)
assert n4.val == 'aa'
}

View File

@ -9,7 +9,15 @@ fn new_foo<T>(len int) &Foo<T> {
}
fn test_generics_return_reference_generics_struct() {
f := new_foo<int>(4)
println(f)
assert f.data == [0, 0, 0, 0]
f1 := new_foo<int>(4)
println(f1)
assert f1.data == [0, 0, 0, 0]
f2 := new_foo<bool>(4)
println(f2)
assert f2.data == [false, false, false, false]
f3 := new_foo<f64>(4)
println(f3)
assert f3.data == [0.0, 0.0, 0.0, 0.0]
}

View File

@ -5,10 +5,31 @@ struct Scope<T> {
}
fn test_generics_struct_anon_fn_fields() {
s := Scope<u32>{}
println(s)
ts := '$s'
assert ts.contains('before: fn () u32')
assert ts.contains('specs: []')
assert ts.contains('after: fn (u32)')
s1 := Scope<u32>{}
println(s1)
ts1 := '$s1'
assert ts1.contains('before: fn () u32')
assert ts1.contains('specs: []')
assert ts1.contains('after: fn (u32)')
s2 := Scope<f64>{}
println(s2)
ts2 := '$s2'
assert ts2.contains('before: fn () f64')
assert ts2.contains('specs: []')
assert ts2.contains('after: fn (f64)')
s3 := Scope<string>{}
println(s3)
ts3 := '$s3'
assert ts3.contains('before: fn () string')
assert ts3.contains('specs: []')
assert ts3.contains('after: fn (string)')
s4 := Scope<bool>{}
println(s4)
ts4 := '$s4'
assert ts4.contains('before: fn () bool')
assert ts4.contains('specs: []')
assert ts4.contains('after: fn (bool)')
}

View File

@ -28,9 +28,27 @@ fn (mut it Group<T>) next() ?T {
}
fn test_generics_with_embed_generics() {
gx := group_new<int>(1, 2, 3)
for x in gx.val {
gx1 := group_new<int>(1, 2, 3)
for x in gx1.val {
println(x)
}
assert gx.val == [1, 2, 3]
assert gx1.val == [1, 2, 3]
gx2 := group_new<f64>(1.1, 2.2, 3.3)
for x in gx2.val {
println(x)
}
assert gx2.val == [1.1, 2.2, 3.3]
gx3 := group_new<bool>(true, true, false)
for x in gx3.val {
println(x)
}
assert gx3.val == [true, true, false]
gx4 := group_new<string>('aa', 'bb', 'cc')
for x in gx4.val {
println(x)
}
assert gx4.val == ['aa', 'bb', 'cc']
}

View File

@ -7,7 +7,19 @@ fn (num Num<T>) is_autom() bool {
}
fn test_generics_with_generic_struct_receiver() {
num := Num<int>{3}
println(num.is_autom())
assert num.is_autom()
num1 := Num<int>{3}
println(num1.is_autom())
assert num1.is_autom()
num2 := Num<f64>{3.3}
println(num2.is_autom())
assert num2.is_autom()
num3 := Num<string>{'aaa'}
println(num3.is_autom())
assert num3.is_autom()
num4 := Num<bool>{true}
println(num4.is_autom())
assert num4.is_autom()
}