v2 / vlib / v / tests / fns / fn_test.c.v
202 lines · 156 sloc · 2.64 KB · 8d745ef69eed77b5fdf5cb84a49881385e585eae
Raw
1import time
2// 1 line comment // 1 line comment
3
4/*
5multi line comment (1)
6multi line comment (2)
7multi line comment (3)
8*/
9/*
10multi line comment (1)
11 /*
12 nested comment
13 */
14 /*nested comment*/
15 /*nested comment
16*/
17 /* nested comment */
18 /* /* nested comment */ */
19 multi line comment (2)
20*/
21type MyFn1 = fn (int) string
22
23type MyFn2 = fn (a int, b int) int
24
25type MyFn3 = fn (int, int)
26
27fn myfn4(string)
28
29fn foobar()
30
31fn slopediv(num u32, den u32) int
32
33type F1 = fn ()
34
35type F2 = fn (voidptr)
36
37type F3 = fn (voidptr, voidptr)
38
39type F4 = fn (voidptr) int
40
41type F5 = fn (int, int) int
42
43type F6 = fn (int, int)
44
45type F7 = fn (time.Time, int)
46
47type MyTime = time.Time
48type F8 = fn (MyTime)
49
50interface MyInterface {}
51
52type F9 = fn (MyInterface)
53
54fn C.atoi(const_s &char) i32
55fn C.freec(ptr voidptr)
56
57@[trusted]
58fn C.exitc(code i32)
59
60// above checks attribute doesn't conflict with `freec` return type
61
62fn foo() {
63}
64
65type ActionfV = fn ()
66
67type ActionfP1 = fn (voidptr)
68
69type ActionfP2 = fn (voidptr, voidptr)
70
71// TODO
72fn modify_array(mut a []int) {
73 a[0] = 10
74 for i in 0 .. a.len {
75 a[i] = a[i] * 2
76 }
77 // a << 888
78}
79
80fn modify_array2(mut a []int) {
81 a[0] = 10
82 for i in 0 .. a.len {
83 a[i] = a[i] * 2
84 }
85 // a << 888
86}
87
88fn test_mut_array() {
89 mut nums := [1, 2, 3]
90 modify_array(mut nums)
91 // assert nums.len == 4
92 // println(nums)
93 assert nums[0] == 20
94 assert nums[1] == 4
95 assert nums[2] == 6
96 // assert nums[3] == 888
97 // workaround for // [91, 32, -33686272] windows bug
98 println(nums.clone())
99}
100
101fn mod_struct(mut user User) {
102 user.age++
103}
104
105struct User {
106mut:
107 age int
108}
109
110fn test_mut_struct() {
111 mut user := User{18}
112 mod_struct(mut user)
113 assert user.age == 19
114}
115
116/*
117fn mod_ptr(mut buf &byte) {
118 buf[0] = 77
119}
120
121fn test_mut_ptr() {
122 buf := malloc(10)
123 mod_ptr(mut buf)
124 assert buf[0] == 77
125}
126*/
127
128fn assert_in_bool_fn(v int) bool {
129 assert v < 3
130 return true
131}
132
133fn test_assert_in_bool_fn() {
134 assert_in_bool_fn(2)
135}
136
137type MyFn = fn (int) int
138
139fn test(n int) int {
140 return n + 1000
141}
142
143struct MySt {
144 f MyFn
145}
146
147fn test_fn_type_call() {
148 mut arr := []MyFn{}
149 arr << MyFn(test)
150 // TODO: `arr[0](10)`
151 // assert arr[0](10) == 1010
152 x1 := arr[0]
153 x2 := x1(10)
154 assert x2 == 1010
155 st := MySt{
156 f: test
157 }
158 assert st.f(10) == 1010
159 st1 := &MySt{
160 f: test
161 }
162 assert st1.f(10) == 1010
163}
164
165fn ff() fn () int {
166 return fn () int {
167 return 22
168 }
169}
170
171fn test_fn_return_fn() {
172 f := ff()
173 assert f() == 22
174}
175
176// Test new static methods
177
178struct Foo {
179 x int
180}
181
182struct Foo2 {
183 x int
184}
185
186fn (f Foo) normal_method() {
187}
188
189fn Foo.static_method() int {
190 return 7
191}
192
193fn Foo2.static_method() int {
194 return 8
195}
196
197fn test_static_method() {
198 x := Foo.static_method()
199 assert x == 7
200 x2 := Foo2.static_method()
201 assert x2 == 8
202}
203