修复了大量存在的问题,增加了假鸭子类型等等机制

This commit is contained in:
2026-06-25 14:49:46 +08:00
parent 19f2787db0
commit d88d11b646
827 changed files with 32617 additions and 18316 deletions

View File

@@ -1,5 +1,6 @@
import t
import stdio
import testcheck
# ============================================================
# Test 1: global 在嵌套函数中
@@ -14,20 +15,17 @@ def outer_modify_global():
inner_modify()
def test_global_in_nested():
stdio.printf("--- Test 1: global in nested function ---\n")
testcheck.section("Test 1: global in nested function")
global g_val
g_val = 100
outer_modify_global()
if g_val == 150:
stdio.printf("PASS: global nested (g_val=%d)\n", g_val)
else:
stdio.printf("FAIL: global nested (g_val=%d, expect 150)\n", g_val)
testcheck.check(g_val == 150, "global nested (g_val == 150)", "global nested (expect 150)")
# ============================================================
# Test 2: nonlocal 在多层嵌套中
# ============================================================
def test_nonlocal_multi_level():
stdio.printf("--- Test 2: nonlocal multi-level ---\n")
testcheck.section("Test 2: nonlocal multi-level")
x: t.CInt = 1
def level1():
nonlocal x
@@ -41,10 +39,7 @@ def test_nonlocal_multi_level():
level3()
level2()
level1()
if x == 1111:
stdio.printf("PASS: nonlocal multi (x=%d)\n", x)
else:
stdio.printf("FAIL: nonlocal multi (x=%d, expect 1111)\n", x)
testcheck.check(x == 1111, "nonlocal multi (x == 1111)", "nonlocal multi (expect 1111)")
# ============================================================
# Test 3: global 和 nonlocal 混合
@@ -52,7 +47,7 @@ def test_nonlocal_multi_level():
g_mixed: t.CInt = 0
def test_global_nonlocal_mixed():
stdio.printf("--- Test 3: global + nonlocal mixed ---\n")
testcheck.section("Test 3: global + nonlocal mixed")
global g_mixed
g_mixed = 0
local_var: t.CInt = 0
@@ -63,59 +58,47 @@ def test_global_nonlocal_mixed():
local_var += 20
modify_both()
modify_both()
if g_mixed == 20 and local_var == 40:
stdio.printf("PASS: mixed (global=%d local=%d)\n", g_mixed, local_var)
else:
stdio.printf("FAIL: mixed (global=%d local=%d, expect 20 40)\n", g_mixed, local_var)
testcheck.check(g_mixed == 20 and local_var == 40, "mixed (global==20 local==40)", "mixed (expect global=20 local=40)")
# ============================================================
# Test 4: lambda 捕获多个变量
# ============================================================
def test_lambda_multi_capture():
stdio.printf("--- Test 4: lambda multi-capture ---\n")
testcheck.section("Test 4: lambda multi-capture")
a: t.CInt = 10
b: t.CInt = 20
f = lambda: a + b
r: t.CInt = f()
if r == 30:
stdio.printf("PASS: lambda multi-capture (a+b=%d)\n", r)
else:
stdio.printf("FAIL: lambda multi-capture (a+b=%d, expect 30)\n", r)
testcheck.check(r == 30, "lambda multi-capture (a+b == 30)", "lambda multi-capture (expect 30)")
# ============================================================
# Test 5: lambda 带参数 + 捕获
# ============================================================
def test_lambda_arg_capture():
stdio.printf("--- Test 5: lambda arg + capture ---\n")
testcheck.section("Test 5: lambda arg + capture")
base: t.CInt = 100
add = lambda x: base + x
r: t.CInt = add(42)
if r == 142:
stdio.printf("PASS: lambda arg+capture (100+42=%d)\n", r)
else:
stdio.printf("FAIL: lambda arg+capture (result=%d, expect 142)\n", r)
testcheck.check(r == 142, "lambda arg+capture (100+42 == 142)", "lambda arg+capture (expect 142)")
# ============================================================
# Test 6: 嵌套函数返回值
# ============================================================
def test_nested_return():
stdio.printf("--- Test 6: nested function return ---\n")
testcheck.section("Test 6: nested function return")
x: t.CInt = 5
def double_x() -> t.CInt:
nonlocal x
x = x * 2
return x
r: t.CInt = double_x()
if r == 10 and x == 10:
stdio.printf("PASS: nested return (r=%d x=%d)\n", r, x)
else:
stdio.printf("FAIL: nested return (r=%d x=%d, expect 10 10)\n", r, x)
testcheck.check(r == 10 and x == 10, "nested return (r==10 x==10)", "nested return (expect r=10 x=10)")
# ============================================================
# Test 7: nonlocal 与循环变量
# ============================================================
def test_nonlocal_loop():
stdio.printf("--- Test 7: nonlocal with loop ---\n")
testcheck.section("Test 7: nonlocal with loop")
total: t.CInt = 0
def accumulate(n: t.CInt):
nonlocal total
@@ -124,34 +107,28 @@ def test_nonlocal_loop():
accumulate(5)
accumulate(5)
# 0+1+2+3+4 = 10, twice = 20
if total == 20:
stdio.printf("PASS: nonlocal loop (total=%d)\n", total)
else:
stdio.printf("FAIL: nonlocal loop (total=%d, expect 20)\n", total)
testcheck.check(total == 20, "nonlocal loop (total == 20)", "nonlocal loop (expect 20)")
# ============================================================
# Test 8: global 数组操作
# ============================================================
g_arr: list[t.CInt, 4] = [0]
g_arr: t.CArray[t.CInt, 4] = [0]
def test_global_array():
stdio.printf("--- Test 8: global array ---\n")
testcheck.section("Test 8: global array")
global g_arr
g_arr[0] = 10
g_arr[1] = 20
g_arr[2] = 30
g_arr[3] = 40
total: t.CInt = g_arr[0] + g_arr[1] + g_arr[2] + g_arr[3]
if total == 100:
stdio.printf("PASS: global array (total=%d)\n", total)
else:
stdio.printf("FAIL: global array (total=%d, expect 100)\n", total)
testcheck.check(total == 100, "global array (total == 100)", "global array (expect 100)")
# ============================================================
# Test 9: 嵌套函数条件调用
# ============================================================
def test_nested_conditional():
stdio.printf("--- Test 9: nested conditional call ---\n")
testcheck.section("Test 9: nested conditional call")
result: t.CInt = 0
def set_result(val: t.CInt):
nonlocal result
@@ -160,26 +137,20 @@ def test_nested_conditional():
set_result(42)
else:
set_result(99)
if result == 42:
stdio.printf("PASS: nested conditional (result=%d)\n", result)
else:
stdio.printf("FAIL: nested conditional (result=%d, expect 42)\n", result)
testcheck.check(result == 42, "nested conditional (result == 42)", "nested conditional (expect 42)")
# ============================================================
# Test 10: lambda 链式调用
# ============================================================
def test_lambda_chain():
stdio.printf("--- Test 10: lambda chain ---\n")
testcheck.section("Test 10: lambda chain")
f1 = lambda: 10
f2 = lambda: 20
r: t.CInt = f1() + f2()
if r == 30:
stdio.printf("PASS: lambda chain (10+20=%d)\n", r)
else:
stdio.printf("FAIL: lambda chain (result=%d, expect 30)\n", r)
testcheck.check(r == 30, "lambda chain (10+20 == 30)", "lambda chain (expect 30)")
def main() -> t.CInt:
stdio.printf("=== ClosureAdvancedTest: 高级闭包测试 ===\n\n")
testcheck.begin("ClosureAdvancedTest: 高级闭包测试")
test_global_in_nested()
test_nonlocal_multi_level()
test_global_nonlocal_mixed()
@@ -190,5 +161,4 @@ def main() -> t.CInt:
test_global_array()
test_nested_conditional()
test_lambda_chain()
stdio.printf("\n=== ClosureAdvancedTest Complete ===\n")
return 0
return testcheck.end()

View File

@@ -0,0 +1 @@
{"stdio": "73edbcf76e32d00b", "testcheck": "9dbecd0942a39782"}

View File

@@ -1 +0,0 @@
{"stdio": "73edbcf76e32d00b"}

View File

@@ -0,0 +1 @@
{"main": "52e6b51da061882f", "stdio": "73edbcf76e32d00b", "testcheck": "9dbecd0942a39782"}

View File

@@ -8,6 +8,7 @@ import c
import t
import stdio
import testcheck
g_val: t.CExtern | t.CInt
@@ -31,7 +32,7 @@ def test_nested_return() -> t.CInt: pass
def test_nonlocal_loop() -> t.CInt: pass
g_arr: t.CExtern | list[t.CInt, 4]
g_arr: t.CExtern | t.CArray[t.CInt, 4]
def test_global_array() -> t.CInt: pass

View File

@@ -0,0 +1,25 @@
"""
Auto-generated Python stub file from testcheck.py
Module: testcheck
"""
import t, c
import stdio
_pass_count: t.CExtern | t.CInt
_fail_count: t.CExtern | t.CInt
def begin(name: str) -> t.CInt: pass
def section(name: str) -> t.CInt: pass
def ok(msg: str) -> t.CInt: pass
def fail(msg: str) -> t.CInt: pass
def check(cond: t.CInt, ok_msg: str, fail_msg: str) -> t.CInt: pass
def info(msg: str) -> t.CInt: pass
def end() -> t.CInt: pass

View File

@@ -1,2 +1,3 @@
52e6b51da061882f:main.py
73edbcf76e32d00b:includes/stdio.py
7dbf085f7e3e8944:main.py
9dbecd0942a39782:includes/testcheck.py