import t import stdio # 测试基本函数调用 def add(a: t.CInt, b: t.CInt) -> t.CInt: return a + b def sub(a: t.CInt, b: t.CInt) -> t.CInt: return a - b # 测试多参数 def multi_args(a: t.CInt, b: t.CInt, c: t.CInt, d: t.CInt) -> t.CInt: return a + b + c + d # 测试递归 def factorial(n: t.CInt) -> t.CInt: if n <= 1: return 1 return n * factorial(n - 1) # 测试尾递归式斐波那契 def fib(n: t.CInt) -> t.CInt: if n <= 0: return 0 if n == 1: return 1 return fib(n - 1) + fib(n - 2) # 测试指针参数 def add_via_ptr(p: t.CInt | t.CPtr, val: t.CInt): p[0] = p[0] + val # 测试 u64 返回值 def make_u64() -> t.CUInt64T: return t.CUInt64T(0xDEADBEEF) # 测试 bool 返回值 def is_even(n: t.CInt) -> t.CBool: return n % 2 == 0 def test_basic_call(): stdio.printf("--- Test 1: basic function call ---\n") r: t.CInt = add(10, 20) if r == 30: stdio.printf("PASS: add(10,20)=%d\n", r) else: stdio.printf("FAIL: add(10,20)=%d, expect 30\n", r) def test_multi_args(): stdio.printf("--- Test 2: multi args ---\n") r: t.CInt = multi_args(1, 2, 3, 4) if r == 10: stdio.printf("PASS: multi_args(1,2,3,4)=%d\n", r) else: stdio.printf("FAIL: multi_args(1,2,3,4)=%d, expect 10\n", r) def test_recursion(): stdio.printf("--- Test 3: recursion (factorial) ---\n") r: t.CInt = factorial(10) # 10! = 3628800 if r == 3628800: stdio.printf("PASS: factorial(10)=%d\n", r) else: stdio.printf("FAIL: factorial(10)=%d, expect 3628800\n", r) def test_fibonacci(): stdio.printf("--- Test 4: fibonacci ---\n") r: t.CInt = fib(10) # fib(10) = 55 if r == 55: stdio.printf("PASS: fib(10)=%d\n", r) else: stdio.printf("FAIL: fib(10)=%d, expect 55\n", r) def test_ptr_param(): stdio.printf("--- Test 5: pointer parameter ---\n") x: t.CInt = 10 # 需要取 x 的地址 import w32.win32memory buf: t.CInt | t.CPtr = w32.win32memory.VirtualAlloc(t.CVoid(0, t.CPtr), 64, 12288, 4) if t.CUInt64T(buf) == 0: stdio.printf("SKIP: VirtualAlloc returned NULL\n") return buf[0] = 10 add_via_ptr(buf, 25) if buf[0] == 35: stdio.printf("PASS: ptr param (result=%d)\n", buf[0]) else: stdio.printf("FAIL: ptr param (result=%d, expect 35)\n", buf[0]) def test_u64_return(): stdio.printf("--- Test 6: u64 return ---\n") r: t.CUInt64T = make_u64() if r == t.CUInt64T(0xDEADBEEF): stdio.printf("PASS: u64 return (0x%lx)\n", r) else: stdio.printf("FAIL: u64 return (0x%lx, expect 0xdeadbeef)\n", r) def test_bool_return(): stdio.printf("--- Test 7: bool return ---\n") r1: t.CBool = is_even(4) r2: t.CBool = is_even(7) if r1 and not r2: stdio.printf("PASS: bool return (is_even(4)=%d, is_even(7)=%d)\n", r1, r2) else: stdio.printf("FAIL: bool return (is_even(4)=%d, is_even(7)=%d)\n", r1, r2) def test_nested_call(): stdio.printf("--- Test 8: nested function call ---\n") r: t.CInt = add(factorial(3), sub(10, 3)) # 3! = 6, 10-3 = 7, 6+7 = 13 if r == 13: stdio.printf("PASS: nested call (add(factorial(3), sub(10,3))=%d)\n", r) else: stdio.printf("FAIL: nested call (result=%d, expect 13)\n", r) import w32.win32memory def main() -> t.CInt: stdio.printf("=== FuncTest: 函数调用基准测试 ===\n\n") test_basic_call() test_multi_args() test_recursion() test_fibonacci() test_ptr_param() test_u64_return() test_bool_return() test_nested_call() stdio.printf("\n=== FuncTest Complete ===\n") return 0