v2 / vlib / v / tests / generics / generic_fn_assign_generics_struct_test.v
65 lines · 53 sloc · 1.04 KB · 2b52153c1477bb782645f6c50ce6773db6bb5092
Raw
1struct Test[T] {
2 v T
3}
4
5fn get_test[T](v T) Test[T] {
6 return Test[T]{
7 v: v
8 }
9}
10
11fn test_generics_assign_generics_struct() {
12 x1 := get_test(1)
13 println('${x1.v}')
14 assert x1.v == 1
15
16 x2 := get_test(2.2)
17 println('${x2.v}')
18 assert x2.v == 2.2
19
20 x3 := get_test('aaa')
21 println('${x3.v}')
22 assert x3.v == 'aaa'
23
24 x4 := get_test(true)
25 println('${x4.v}')
26 assert x4.v == true
27}
28
29// test generics assign generics struct_init
30struct Node[T] {
31pub mut:
32 val T
33 next &Node[T] = unsafe { nil }
34}
35
36fn new[T]() &Node[T] {
37 return &Node[T]{}
38}
39
40fn (mut n Node[T]) add(val T) {
41 node := &Node[T]{val, unsafe { nil }}
42 n.next = node
43}
44
45fn test_generic_fn_assign_generic_struct_init() {
46 mut list1 := new[int]()
47 list1.add(100)
48 println(list1.next)
49 assert list1.next.val == 100
50
51 mut list2 := new[f64]()
52 list2.add(2.22)
53 println(list2.next)
54 assert list2.next.val == 2.22
55
56 mut list3 := new[bool]()
57 list3.add(false)
58 println(list3.next)
59 assert list3.next.val == false
60
61 mut list4 := new[string]()
62 list4.add('aaa')
63 println(list4.next)
64 assert list4.next.val == 'aaa'
65}
66