# # Test of function calls # # Copyright © 2022-2024 Samuel Lidén Borell # # 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() }