Files
2026-10-17 21:57:03 +08:00

164 lines
5.3 KiB
Plaintext

import stdio
import testcheck
# ============================================================
# Test 1: global in nested function
# ============================================================
g_val: int = 100
def outer_modify_global():
global g_val
def inner_modify():
global g_val
g_val += 50
inner_modify()
def test_global_in_nested():
testcheck.section("Test 1: global in nested function")
global g_val
g_val = 100
outer_modify_global()
testcheck.check(g_val == 150, "global nested (g_val == 150)", "global nested (expect 150)")
# ============================================================
# Test 2: nonlocal across multiple nesting levels
# ============================================================
def test_nonlocal_multi_level():
testcheck.section("Test 2: nonlocal multi-level")
x: int = 1
def level1():
nonlocal x
x += 10
def level2():
nonlocal x
x += 100
def level3():
nonlocal x
x += 1000
level3()
level2()
level1()
testcheck.check(x == 1111, "nonlocal multi (x == 1111)", "nonlocal multi (expect 1111)")
# ============================================================
# Test 3: global and nonlocal mixed
# ============================================================
g_mixed: int = 0
def test_global_nonlocal_mixed():
testcheck.section("Test 3: global + nonlocal mixed")
global g_mixed
g_mixed = 0
local_var: int = 0
def modify_both():
nonlocal local_var
global g_mixed
g_mixed += 10
local_var += 20
modify_both()
modify_both()
testcheck.check(g_mixed == 20 and local_var == 40, "mixed (global==20 local==40)", "mixed (expect global=20 local=40)")
# ============================================================
# Test 4: lambda captures multiple variables
# ============================================================
def test_lambda_multi_capture():
testcheck.section("Test 4: lambda multi-capture")
a: int = 10
b: int = 20
f = lambda: a + b
r: int = f()
testcheck.check(r == 30, "lambda multi-capture (a+b == 30)", "lambda multi-capture (expect 30)")
# ============================================================
# Test 5: lambda with argument + capture
# ============================================================
def test_lambda_arg_capture():
testcheck.section("Test 5: lambda arg + capture")
base: int = 100
add = lambda x: base + x
r: int = add(42)
testcheck.check(r == 142, "lambda arg+capture (100+42 == 142)", "lambda arg+capture (expect 142)")
# ============================================================
# Test 6: nested function return value
# ============================================================
def test_nested_return():
testcheck.section("Test 6: nested function return")
x: int = 5
def double_x() -> int:
nonlocal x
x = x * 2
return x
r: int = double_x()
testcheck.check(r == 10 and x == 10, "nested return (r==10 x==10)", "nested return (expect r=10 x=10)")
# ============================================================
# Test 7: nonlocal with loop variable
# ============================================================
def test_nonlocal_loop():
testcheck.section("Test 7: nonlocal with loop")
total: int = 0
def accumulate(n: int):
nonlocal total
for i in range(n):
total += i
accumulate(5)
accumulate(5)
# 0+1+2+3+4 = 10, twice = 20
testcheck.check(total == 20, "nonlocal loop (total == 20)", "nonlocal loop (expect 20)")
# ============================================================
# Test 8: global array operation
# ============================================================
g_arr: [4]int = None
def test_global_array():
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: int = g_arr[0] + g_arr[1] + g_arr[2] + g_arr[3]
testcheck.check(total == 100, "global array (total == 100)", "global array (expect 100)")
# ============================================================
# Test 9: nested function conditional call
# ============================================================
def test_nested_conditional():
testcheck.section("Test 9: nested conditional call")
result: int = 0
def set_result(val: int):
nonlocal result
result = val
if 1:
set_result(42)
else:
set_result(99)
testcheck.check(result == 42, "nested conditional (result == 42)", "nested conditional (expect 42)")
# ============================================================
# Test 10: lambda chained calls
# ============================================================
def test_lambda_chain():
testcheck.section("Test 10: lambda chain")
f1 = lambda: 10
f2 = lambda: 20
r: int = f1() + f2()
testcheck.check(r == 30, "lambda chain (10+20 == 30)", "lambda chain (expect 30)")
def main() -> int:
testcheck.begin("ClosureAdvancedTest: 高级闭包测试")
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()
return testcheck.end()