修复了大量存在的问题,增加了假鸭子类型等等机制
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import t
|
||||
from stdio import printf
|
||||
import testcheck
|
||||
|
||||
# ============================================================
|
||||
# 自定义装饰器处理函数
|
||||
@@ -226,65 +227,72 @@ def fib(n: t.CInt) -> t.CInt | t.CExport:
|
||||
# ============================================================
|
||||
|
||||
def main() -> t.CInt | t.CExport:
|
||||
printf("=== Decorator Test v4 ===\n\n")
|
||||
testcheck.begin("Decorator Test v4")
|
||||
|
||||
# 测试 1:单个 @log
|
||||
printf("--- Test 1: single @log ---\n")
|
||||
testcheck.section("Test 1: single @log")
|
||||
r1: t.CInt = add(3, 4)
|
||||
printf("add(3,4) = %d\n\n", r1)
|
||||
printf("add(3,4) = %d\n", r1)
|
||||
testcheck.check(r1 == 7, "add(3,4) = 7", "add(3,4) expect 7")
|
||||
|
||||
# 测试 2:链式 @log @trace
|
||||
printf("--- Test 2: chained @log @trace ---\n")
|
||||
testcheck.section("Test 2: chained @log @trace")
|
||||
r2: t.CInt = multiply(5, 6)
|
||||
printf("multiply(5,6) = %d\n\n", r2)
|
||||
printf("multiply(5,6) = %d\n", r2)
|
||||
testcheck.check(r2 == 30, "multiply(5,6) = 30", "multiply(5,6) expect 30")
|
||||
|
||||
# 测试 3:void 返回
|
||||
printf("--- Test 3: @log on void function ---\n")
|
||||
testcheck.section("Test 3: @log on void function")
|
||||
greet("Viper")
|
||||
printf("\n")
|
||||
testcheck.ok("@log on void function greet")
|
||||
|
||||
# 测试 4:无参数
|
||||
printf("--- Test 4: @log on no-arg function ---\n")
|
||||
testcheck.section("Test 4: @log on no-arg function")
|
||||
r4: t.CInt = get_value()
|
||||
printf("get_value() = %d\n\n", r4)
|
||||
printf("get_value() = %d\n", r4)
|
||||
testcheck.check(r4 == 42, "get_value() = 42", "get_value() expect 42")
|
||||
|
||||
# 测试 5:@timing
|
||||
printf("--- Test 5: @timing ---\n")
|
||||
testcheck.section("Test 5: @timing")
|
||||
r5: t.CInt = compute(7)
|
||||
printf("compute(7) = %d\n\n", r5)
|
||||
printf("compute(7) = %d\n", r5)
|
||||
testcheck.check(r5 == 50, "compute(7) = 50", "compute(7) expect 50")
|
||||
|
||||
# 测试 6:@repeat(3) — 流程劫持,循环 3 次
|
||||
printf("--- Test 6: @repeat(3) flow hijack ---\n")
|
||||
testcheck.section("Test 6: @repeat(3) flow hijack")
|
||||
echo("hello")
|
||||
printf("\n")
|
||||
testcheck.ok("@repeat(3) flow hijack echo")
|
||||
|
||||
# 测试 7:链式 @timing @log
|
||||
printf("--- Test 7: chained @timing @log ---\n")
|
||||
testcheck.section("Test 7: chained @timing @log")
|
||||
r7: t.CInt = power(2, 10)
|
||||
printf("power(2,10) = %d\n\n", r7)
|
||||
printf("power(2,10) = %d\n", r7)
|
||||
testcheck.check(r7 == 1024, "power(2,10) = 1024", "power(2,10) expect 1024")
|
||||
|
||||
# 测试 8:@skip — 跳过原函数调用
|
||||
printf("--- Test 8: @skip flow hijack ---\n")
|
||||
testcheck.section("Test 8: @skip flow hijack")
|
||||
r8: t.CInt = dangerous()
|
||||
printf("dangerous() = %d (should be 0, skipped)\n\n", r8)
|
||||
printf("dangerous() = %d (should be 0, skipped)\n", r8)
|
||||
testcheck.check(r8 == 0, "dangerous() skipped = 0", "dangerous() expect 0")
|
||||
|
||||
# 测试 9:@repeat(3) 有返回值
|
||||
printf("--- Test 9: @repeat(3) with return value ---\n")
|
||||
testcheck.section("Test 9: @repeat(3) with return value")
|
||||
r9: t.CInt = accumulate(5)
|
||||
printf("accumulate(5) = %d (last iteration result)\n\n", r9)
|
||||
printf("accumulate(5) = %d (last iteration result)\n", r9)
|
||||
testcheck.check(r9 == 50, "accumulate(5) = 50", "accumulate(5) expect 50")
|
||||
|
||||
# 测试 10:递归装饰保护 — @count_calls
|
||||
printf("--- Test 10: recursive decoration protection ---\n")
|
||||
testcheck.section("Test 10: recursive decoration protection")
|
||||
_call_count = 0
|
||||
r10: t.CInt = factorial(5)
|
||||
printf("factorial(5) = %d (should be 120)\n", r10)
|
||||
printf(" decorator call count = %d (should be 1, not 5)\n\n", _call_count)
|
||||
printf(" decorator call count = %d (should be 1, not 5)\n", _call_count)
|
||||
testcheck.check(r10 == 120 and _call_count == 1, "factorial(5)=120, calls=1", "factorial(5) expect 120, calls=1")
|
||||
|
||||
# 测试 11:@log 递归保护 — fib
|
||||
printf("--- Test 11: @log on recursive fib ---\n")
|
||||
testcheck.section("Test 11: @log on recursive fib")
|
||||
r11: t.CInt = fib(6)
|
||||
printf("fib(6) = %d (should be 8)\n", r11)
|
||||
printf(" (should see only 1 LOG enter/exit pair)\n\n")
|
||||
testcheck.check(r11 == 8, "fib(6) = 8", "fib(6) expect 8")
|
||||
|
||||
printf("=== All decorator tests done ===\n")
|
||||
return 0
|
||||
return testcheck.end()
|
||||
|
||||
Reference in New Issue
Block a user