Files
Viper_RegressionTesting/FuncTest/App/main.vp
T
TermiNexus 44ce29200e refactor(test): 收尾 6 个测试用例的新规范语法迁移
CoroTest/DefaultArgTest/FormatStringTest/FuncTest/MLIRLiteTest/ThreadingTest 的 App/main.vp 从旧写法(t.CInt/t.CPtr 注解、多行单语句体)迁到新规范(int 短别名、T* 后缀、单语句体并一行),为既有迁移的遗留未提交部分。验证:TPV 自举产物顺序回归 77/77 全通过。
2026-10-05 09:24:42 +08:00

84 lines
2.6 KiB
Plaintext

import testcheck
# import w32.win32memory
import stdlib
def add(a: int, b: int) -> int: return a + b
def sub(a: int, b: int) -> int: return a - b
def multi_args(a: int, b: int, c: int, d: int) -> int: return a + b + c + d
def factorial(n: int) -> int:
if n <= 1: return 1
return n * factorial(n - 1)
def fib(n: int) -> int:
if n <= 0: return 0
if n == 1: return 1
return fib(n - 1) + fib(n - 2)
def add_via_ptr(p: int*, val: int):
p[0] = p[0] + val
def make_u64() -> uint64: return 0xDEADBEEF
def is_even(n: int) -> bool: return n % 2 == 0
def test_basic_call():
testcheck.section("Test 1: basic function call")
r: int = add(10, 20)
testcheck.check(r == 30, "add(10,20)=30", "add(10,20) expect 30")
def test_multi_args():
testcheck.section("Test 2: multi args")
r: int = multi_args(1, 2, 3, 4)
testcheck.check(r == 10, "multi_args(1,2,3,4)=10", "multi_args(1,2,3,4) expect 10")
def test_recursion():
testcheck.section("Test 3: recursion (factorial)")
r: int = factorial(10)
testcheck.check(r == 3628800, "factorial(10)=3628800", "factorial(10) expect 3628800")
def test_fibonacci():
testcheck.section("Test 4: fibonacci")
r: int = fib(10)
testcheck.check(r == 55, "fib(10)=55", "fib(10) expect 55")
def test_ptr_param():
testcheck.section("Test 5: pointer parameter")
# buf: int* = w32.win32memory.VirtualAlloc(None, 64, 12288, 4)
buf: int* = stdlib.malloc(int.__sizeof__())
if buf is None:
testcheck.info("SKIP: VirtualAlloc returned NULL")
return
buf[0] = 10
add_via_ptr(buf, 25)
testcheck.check(buf[0] == 35, "ptr param (result=35)", "ptr param expect 35")
def test_u64_return():
testcheck.section("Test 6: u64 return")
r: uint64 = make_u64()
testcheck.check(r == 0xDEADBEEF, "u64 return (0xdeadbeef)", "u64 return expect 0xdeadbeef")
def test_bool_return():
testcheck.section("Test 7: bool return")
r1: bool = is_even(4)
r2: bool = is_even(7)
testcheck.check(r1 and not r2, "bool return (is_even(4)=1, is_even(7)=0)", "bool return expect is_even(4)=1, is_even(7)=0")
def test_nested_call():
testcheck.section("Test 8: nested function call")
r: int = add(factorial(3), sub(10, 3))
testcheck.check(r == 13, "nested call (add(factorial(3), sub(10,3))=13)", "nested call expect 13")
export def main() -> int:
testcheck.begin("FuncTest: 函数调用基准测试")
test_basic_call()
test_multi_args()
test_recursion()
test_fibonacci()
test_ptr_param()
test_u64_return()
test_bool_return()
test_nested_call()
return testcheck.end()