import t import stdio import testcheck # ============================================================ # 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(): 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 在多层嵌套中 # ============================================================ def test_nonlocal_multi_level(): testcheck.section("Test 2: nonlocal multi-level") 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() testcheck.check(x == 1111, "nonlocal multi (x == 1111)", "nonlocal multi (expect 1111)") # ============================================================ # Test 3: global 和 nonlocal 混合 # ============================================================ g_mixed: t.CInt = 0 def test_global_nonlocal_mixed(): testcheck.section("Test 3: global + nonlocal mixed") 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() 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(): testcheck.section("Test 4: lambda multi-capture") a: t.CInt = 10 b: t.CInt = 20 f = lambda: a + b r: t.CInt = f() testcheck.check(r == 30, "lambda multi-capture (a+b == 30)", "lambda multi-capture (expect 30)") # ============================================================ # Test 5: lambda 带参数 + 捕获 # ============================================================ def test_lambda_arg_capture(): testcheck.section("Test 5: lambda arg + capture") base: t.CInt = 100 add = lambda x: base + x r: t.CInt = add(42) testcheck.check(r == 142, "lambda arg+capture (100+42 == 142)", "lambda arg+capture (expect 142)") # ============================================================ # Test 6: 嵌套函数返回值 # ============================================================ def test_nested_return(): 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() 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(): testcheck.section("Test 7: nonlocal with loop") 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 testcheck.check(total == 20, "nonlocal loop (total == 20)", "nonlocal loop (expect 20)") # ============================================================ # Test 8: global 数组操作 # ============================================================ g_arr: t.CArray[t.CInt, 4] = [0] 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: t.CInt = 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: 嵌套函数条件调用 # ============================================================ def test_nested_conditional(): testcheck.section("Test 9: nested conditional call") result: t.CInt = 0 def set_result(val: t.CInt): 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 链式调用 # ============================================================ def test_lambda_chain(): testcheck.section("Test 10: lambda chain") f1 = lambda: 10 f2 = lambda: 20 r: t.CInt = f1() + f2() testcheck.check(r == 30, "lambda chain (10+20 == 30)", "lambda chain (expect 30)") def main() -> t.CInt: 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()