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
|
#
# Test of function calls
#
# Copyright © 2022-2024 Samuel Lidén Borell <samuel@kodafritt.se>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
func test_function_calls()
{
f_void()
f_int_void(123)
f_bool_void(false)
f_intbool_void(-456, true)
# Using return values
bool b1 = f_bool()
while not f_int_bool(123) { }
do {
if f_bool_bool(false) { }
} while not f_intbool_bool(-456, true)
assert b1
# Discarding return values
f_bool()
f_int_bool(123)
f_bool_bool(false)
f_intbool_bool(-456, true)
}
func test_method_calls_byvalue()
{
var FuncObj fo = .init()
# TODO check the fields once struct returns are implemented
fo.x = 678
fo.b = true
fo.f_void()
fo.f_int_void(123)
fo.f_bool_void(false)
fo.f_intbool_void(-456, true)
# Using return values
bool b1 = fo.f_bool()
while not fo.f_int_bool(123) { }
do {
if fo.f_bool_bool(false) { }
} while not fo.f_intbool_bool(-456, true)
assert b1
# Discarding return values
fo.f_bool()
fo.f_int_bool(123)
fo.f_bool_bool(false)
fo.f_intbool_bool(-456, true)
}
func test_method_calls_byref()
{
var FuncObj the_fo = .init()
# TODO check the fields once struct returns are implemented
the_fo.x = 678
the_fo.b = true
ref var FuncObj fr = refto the_fo
fr.f_void()
fr.f_int_void(123)
fr.f_bool_void(false)
fr.f_intbool_void(-456, true)
# Using return values
bool b1 = fr.f_bool()
while not fr.f_int_bool(123) { }
do {
if fr.f_bool_bool(false) { }
} while not fr.f_intbool_bool(-456, true)
# Discarding return values
fr.f_bool()
fr.f_int_bool(123)
fr.f_bool_bool(false)
fr.f_intbool_bool(-456, true)
}
func f_void() { }
func f_int_void(int x) { assert x == 123 }
func f_bool_void(bool b) { assert not b }
func f_intbool_void(int x, bool b)
{
assert x == -456
assert b
}
func f_bool() -> bool { return true }
func f_int_bool(int x) -> bool
{
assert x == 123
return true
}
func f_bool_bool(bool b) -> bool
{
assert not b
return true
}
func f_intbool_bool(int x, bool b) -> bool
{
assert x == -456
assert b
return true
}
type FuncObj = struct {
int x
bool b
}
func .init() -> FuncObj { return (.x=123, .b=true) }
func FuncObj.f_void() { }
func FuncObj.f_int_void(int x) { assert x == 123 }
func FuncObj.f_bool_void(bool b) { assert not b }
func FuncObj.f_intbool_void(int x, bool b)
{
assert x == -456
assert b
}
func FuncObj.f_bool() -> bool { return true }
func FuncObj.f_int_bool(int x) -> bool
{
assert x == 123
return true
}
func FuncObj.f_bool_bool(bool b) -> bool
{
assert not b
return true
}
func FuncObj.f_intbool_bool(int x, bool b) -> bool
{
assert x == -456
assert b
return true
}
func FuncObj.access_fields(int value)
{
if this.b {
this.x += value * this.x
} else {
this.b = (this.x == value)
assert this.x == value
this.b = true
var FuncObj obj = (.x=111, .b=true)
assert obj.x == 111
obj.b = true
obj.b = (value == 111)
obj.b = (obj.x == 222)
obj.b = (obj.x == value)
obj.b = (this.x == value)
this.b = (obj.x == value)
}
}
func funccalls_test()
{
test_function_calls()
test_method_calls_byvalue()
test_method_calls_byref()
}
|