Files
TransPyC/Test/ClosureTest/App/main.py
2026-06-16 16:09:42 +08:00

195 lines
6.2 KiB
Python

import t
import stdio
# ============================================================
# Test 1: global 在嵌套函数中
# ============================================================
g_val: t.CInt = 100
def outer_modify_global():
global g_val
def inner_modify():
global g_val
g_val += 50
inner_modify()
def test_global_in_nested():
stdio.printf("--- Test 1: global in nested function ---\n")
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)
# ============================================================
# Test 2: nonlocal 在多层嵌套中
# ============================================================
def test_nonlocal_multi_level():
stdio.printf("--- Test 2: nonlocal multi-level ---\n")
x: t.CInt = 1
def level1():
nonlocal x
x += 10
def level2():
nonlocal x
x += 100
def level3():
nonlocal x
x += 1000
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)
# ============================================================
# Test 3: global 和 nonlocal 混合
# ============================================================
g_mixed: t.CInt = 0
def test_global_nonlocal_mixed():
stdio.printf("--- Test 3: global + nonlocal mixed ---\n")
global g_mixed
g_mixed = 0
local_var: t.CInt = 0
def modify_both():
nonlocal local_var
global g_mixed
g_mixed += 10
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)
# ============================================================
# Test 4: lambda 捕获多个变量
# ============================================================
def test_lambda_multi_capture():
stdio.printf("--- Test 4: lambda multi-capture ---\n")
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)
# ============================================================
# Test 5: lambda 带参数 + 捕获
# ============================================================
def test_lambda_arg_capture():
stdio.printf("--- Test 5: lambda arg + capture ---\n")
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)
# ============================================================
# Test 6: 嵌套函数返回值
# ============================================================
def test_nested_return():
stdio.printf("--- Test 6: nested function return ---\n")
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)
# ============================================================
# Test 7: nonlocal 与循环变量
# ============================================================
def test_nonlocal_loop():
stdio.printf("--- Test 7: nonlocal with loop ---\n")
total: t.CInt = 0
def accumulate(n: t.CInt):
nonlocal total
for i in range(n):
total += i
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)
# ============================================================
# Test 8: global 数组操作
# ============================================================
g_arr: list[t.CInt, 4] = [0]
def test_global_array():
stdio.printf("--- Test 8: global array ---\n")
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)
# ============================================================
# Test 9: 嵌套函数条件调用
# ============================================================
def test_nested_conditional():
stdio.printf("--- Test 9: nested conditional call ---\n")
result: t.CInt = 0
def set_result(val: t.CInt):
nonlocal result
result = val
if 1:
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)
# ============================================================
# Test 10: lambda 链式调用
# ============================================================
def test_lambda_chain():
stdio.printf("--- Test 10: lambda chain ---\n")
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)
def main() -> t.CInt:
stdio.printf("=== ClosureAdvancedTest: 高级闭包测试 ===\n\n")
test_global_in_nested()
test_nonlocal_multi_level()
test_global_nonlocal_mixed()
test_lambda_multi_capture()
test_lambda_arg_capture()
test_nested_return()
test_nonlocal_loop()
test_global_array()
test_nested_conditional()
test_lambda_chain()
stdio.printf("\n=== ClosureAdvancedTest Complete ===\n")
return 0