from stdint import * import t, c import stdio import testcheck # ============================================================ # ExpandKeywordsTest - verify def alias keywords fn/fun/func/function; # identical semantics to def (define/call/nest/generic/empty body). # ============================================================ # fn: single argument, returns value fn add_one(x: int) -> int: return x + 1 # fun: two arguments, returns value fun add_two(a: int, b: int) -> int: return a + b # func: float arguments func mul_float(a: double, b: double) -> double: return a * b # function: defines and calls an inner def (alias mixed with def) function get_ten() -> int: def inner() -> int: return 10 return inner() # fn generic (PEP 695 style type parameters) fn ident[T](x: T) -> T: return x # fn empty body (pass) fn noop(): pass # fun recursion fun fact(n: int) -> int: if n <= 1: return 1 return n * fact(n - 1) # func multiple statements + local variable func avg3(a: double, b: double, c: double) -> double: total: double = a + b + c return total / 3.0 # func12344444 avg3(a: t.CDouble, b: t.CDouble, c: t.CDouble) -> t.CDouble: # total: t.CDouble = a + b + c # return total / 3.0 def main() -> int: testcheck.begin("ExpandKeywordsTest: fn/fun/func/function") testcheck.section("function def keywords") testcheck.check(add_one(41) == 42, "fn add_one(41)==42", "FAIL fn add_one") testcheck.check(add_two(20, 22) == 42, "fun add_two==42", "FAIL fun add_two") testcheck.check(mul_float(2.5, 4.0) == 10.0, "func mul_float==10", "FAIL func mul_float") testcheck.check(get_ten() == 10, "function get_ten==10", "FAIL function get_ten") testcheck.check(ident(7) == 7, "fn ident[T](7)==7", "FAIL fn ident") testcheck.check(fact(5) == 120, "fun fact(5)==120", "FAIL fun fact") testcheck.check(avg3(1.0, 2.0, 3.0) == 2.0, "func avg3==2", "FAIL func avg3") noop() testcheck.check(True, "fn noop() ran", "FAIL fn noop") return testcheck.end()