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