tests: supplement tests for different types of generic parameters (#12432)

pull/12441/head
yuyi 2021-11-11 20:45:55 +08:00 committed by GitHub
parent a4c57ba56e
commit 6f55439930
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 150 additions and 27 deletions

View File

@ -14,10 +14,14 @@ fn (p1 Point) + (p2 Point) Point {
}
fn test_generic_arrays_sum() {
ret := arrays.sum<Point>([Point{ x: 1, y: 1 }, Point{
ret1 := arrays.sum<Point>([Point{ x: 1, y: 1 }, Point{
x: 2
y: 2
}]) or { Point{} }
println(ret)
assert ret == Point{3, 3}
println(ret1)
assert ret1 == Point{3, 3}
ret2 := arrays.sum<int>([1, 2, 3, 4]) or { 0 }
println(ret2)
assert ret2 == 10
}

View File

@ -7,10 +7,35 @@ fn g_array<T>(mut a []T) {
}
fn test_array() {
mut a := [7, 8]
r := f_array(a)
assert r == 7
// []int
mut a1 := [7, 8]
r1 := f_array(a1)
assert r1 == 7
g_array(mut a)
assert a[0] == 8
g_array(mut a1)
assert a1[0] == 8
// []f64
mut a2 := [1.1, 2.2]
r2 := f_array(a2)
assert r2 == 1.1
g_array(mut a2)
assert a2[0] == 2.2
// []string
mut a3 := ['aa', 'bb']
r3 := f_array(a3)
assert r3 == 'aa'
g_array(mut a3)
assert a3[0] == 'bb'
// []bool
mut a4 := [true, false]
r4 := f_array(a4)
assert r4 == true
g_array(mut a4)
assert a4[0] == false
}

View File

@ -6,4 +6,6 @@ fn g<T>(arr []T) {
fn test_generic_array_append() {
g([1, 2, 3])
g([1.1, 2.2, 3.3])
g(['aa', 'bb', 'cc'])
}

View File

@ -27,22 +27,63 @@ fn (mut s Set<T>) remove<T>(value T) bool {
}
fn test_generics_array_delete() {
mut set := Set<int>{}
// int
mut set1 := Set<int>{}
mut added := set.add(4)
mut added := set1.add(4)
println(added)
assert added
added = set.add(3)
added = set1.add(3)
println(added)
assert added
added = set.add(3)
added = set1.add(3)
println(added)
assert !added
println(set)
mut removed := set.remove(4)
println(set1)
mut removed := set1.remove(4)
println(removed)
assert removed
// f64
mut set2 := Set<f64>{}
added = set2.add(4.4)
println(added)
assert added
added = set2.add(3.3)
println(added)
assert added
added = set2.add(3.3)
println(added)
assert !added
println(set2)
removed = set2.remove(4.4)
println(removed)
assert removed
// string
mut set3 := Set<string>{}
added = set3.add('aaa')
println(added)
assert added
added = set3.add('bbb')
println(added)
assert added
added = set3.add('bbb')
println(added)
assert !added
println(set3)
removed = set3.remove('aaa')
println(removed)
assert removed
}

View File

@ -7,7 +7,19 @@ fn (s Abc<T>) get_value() T {
}
fn test_generics_method_on_receiver_types() {
s := Abc<string>{'hello'}
println(s.get_value())
assert s.get_value() == 'hello'
s1 := Abc<string>{'hello'}
println(s1.get_value())
assert s1.get_value() == 'hello'
s2 := Abc<int>{22}
println(s2.get_value())
assert s2.get_value() == 22
s3 := Abc<f64>{1.1}
println(s3.get_value())
assert s3.get_value() == 1.1
s4 := Abc<bool>{true}
println(s4.get_value())
assert s4.get_value() == true
}

View File

@ -9,7 +9,19 @@ fn (g Gen<G>) str() string {
}
fn test_generics_method_str_overload() {
mut g := Gen<string>{'Gen', 0, 10}
println(g)
assert '$g' == 'Gen<G> 0, Gen, 10.'
mut g1 := Gen<string>{'aaa', 0, 10}
println(g1)
assert '$g1' == 'Gen<G> 0, aaa, 10.'
mut g2 := Gen<int>{22, 0, 10}
println(g2)
assert '$g2' == 'Gen<G> 0, 22, 10.'
mut g3 := Gen<f64>{2.22, 0, 10}
println(g3)
assert '$g3' == 'Gen<G> 0, 2.22, 10.'
mut g4 := Gen<bool>{true, 0, 10}
println(g4)
assert '$g4' == 'Gen<G> 0, true, 10.'
}

View File

@ -14,12 +14,39 @@ fn (mut b Bar<T>) add(v T) {
}
fn test_nested_generics_struct_init() {
mut bar := Bar<string>{}
bar.add('bar')
println(bar)
mut bar1 := Bar<string>{}
bar1.add('bar')
println(bar1)
result := '$bar'
assert result.contains('Bar<string>{')
assert result.contains('foos: [Foo<string>{')
assert result.contains("foo: 'bar'")
result1 := '$bar1'
assert result1.contains('Bar<string>{')
assert result1.contains('foos: [Foo<string>{')
assert result1.contains("foo: 'bar'")
mut bar2 := Bar<int>{}
bar2.add(22)
println(bar2)
result2 := '$bar2'
assert result2.contains('Bar<int>{')
assert result2.contains('foos: [Foo<int>{')
assert result2.contains('foo: 22')
mut bar3 := Bar<f64>{}
bar3.add(2.2)
println(bar3)
result3 := '$bar3'
assert result3.contains('Bar<f64>{')
assert result3.contains('foos: [Foo<f64>{')
assert result3.contains('foo: 2.2')
mut bar4 := Bar<bool>{}
bar4.add(true)
println(bar4)
result4 := '$bar4'
assert result4.contains('Bar<bool>{')
assert result4.contains('foos: [Foo<bool>{')
assert result4.contains('foo: true')
}