import t import stdio # ============================================================ # Test 1: nonlocal 在条件分支中修改 # ============================================================ def test_nonlocal_conditional(): stdio.printf("--- Test 1: nonlocal in conditional branches ---\n") x: t.CInt = 0 def modify(flag: t.CInt): nonlocal x if flag: x += 10 else: x += 100 modify(1) modify(0) modify(1) if x == 120: stdio.printf("PASS: nonlocal conditional (x=%d)\n", x) else: stdio.printf("FAIL: nonlocal conditional (x=%d, expect 120)\n", x) # ============================================================ # Test 2: 多个nonlocal变量同时修改 # ============================================================ def test_nonlocal_multi_var(): stdio.printf("--- Test 2: multiple nonlocal variables ---\n") a: t.CInt = 1 b: t.CInt = 2 c_val: t.CInt = 3 def swap_add(): nonlocal a, b, c_val tmp: t.CInt = a a = b b = c_val c_val = tmp swap_add() if a == 2 and b == 3 and c_val == 1: stdio.printf("PASS: multi nonlocal (a=%d b=%d c=%d)\n", a, b, c_val) else: stdio.printf("FAIL: multi nonlocal (a=%d b=%d c=%d, expect 2 3 1)\n", a, b, c_val) # ============================================================ # Test 3: global 在多个函数中交叉修改 # ============================================================ g_counter: t.CInt = 0 def inc_global(): global g_counter g_counter += 1 def add_global(n: t.CInt): global g_counter g_counter += n def test_global_cross_functions(): stdio.printf("--- Test 3: global cross-function modification ---\n") global g_counter g_counter = 0 inc_global() inc_global() add_global(10) inc_global() if g_counter == 13: stdio.printf("PASS: global cross (g_counter=%d)\n", g_counter) else: stdio.printf("FAIL: global cross (g_counter=%d, expect 13)\n", g_counter) # ============================================================ # Test 4: 嵌套函数修改外层局部变量(引用捕获) # ============================================================ def test_nested_ref_capture(): stdio.printf("--- Test 4: nested function ref capture ---\n") arr: list[t.CInt, 4] = [0] def fill(): nonlocal arr for i in range(4): arr[i] = (i + 1) * 10 fill() total: t.CInt = arr[0] + arr[1] + arr[2] + arr[3] if total == 100: stdio.printf("PASS: ref capture (total=%d)\n", total) else: stdio.printf("FAIL: ref capture (total=%d, expect 100)\n", total) # ============================================================ # Test 5: lambda捕获后修改原变量 # ============================================================ def test_lambda_capture_then_modify(): stdio.printf("--- Test 5: lambda capture then modify ---\n") x: t.CInt = 10 f = lambda: x r1: t.CInt = f() x = 20 r2: t.CInt = f() if r1 == 10 and r2 == 10: stdio.printf("PASS: lambda value capture (before=%d after=%d)\n", r1, r2) else: stdio.printf("FAIL: lambda value capture (before=%d after=%d, expect 10 10)\n", r1, r2) # ============================================================ # Test 6: 嵌套函数中nonlocal与循环结合 # ============================================================ def test_nonlocal_loop_accumulate(): stdio.printf("--- Test 6: nonlocal with loop accumulate ---\n") sum_val: t.CInt = 0 count: t.CInt = 0 def add_numbers(n: t.CInt): nonlocal sum_val, count for i in range(1, n + 1): sum_val += i count += 1 add_numbers(5) # 1+2+3+4+5=15 add_numbers(3) # 1+2+3=6 if sum_val == 21 and count == 8: stdio.printf("PASS: loop accumulate (sum=%d count=%d)\n", sum_val, count) else: stdio.printf("FAIL: loop accumulate (sum=%d count=%d, expect 21 8)\n", sum_val, count) # ============================================================ # Test 7: 三层嵌套中每层修改不同变量 # ============================================================ def test_deep_nested_vars(): stdio.printf("--- Test 7: deep nested different vars ---\n") a: t.CInt = 0 b: t.CInt = 0 c_val: t.CInt = 0 def level1(): nonlocal a a += 1 def level2(): nonlocal b b += 10 def level3(): nonlocal c_val c_val += 100 level3() level2() level1() level1() if a == 2 and b == 20 and c_val == 200: stdio.printf("PASS: deep nested (a=%d b=%d c=%d)\n", a, b, c_val) else: stdio.printf("FAIL: deep nested (a=%d b=%d c=%d, expect 2 20 200)\n", a, b, c_val) # ============================================================ # Test 8: global + nonlocal 在同一嵌套函数中 # ============================================================ g_state: t.CInt = 0 def test_global_nonlocal_same_func(): stdio.printf("--- Test 8: global+nonlocal in same nested func ---\n") global g_state g_state = 0 local_state: t.CInt = 0 def worker(n: t.CInt): nonlocal local_state global g_state for i in range(n): g_state += 1 local_state += 2 worker(5) if g_state == 5 and local_state == 10: stdio.printf("PASS: global+nonlocal same (g=%d l=%d)\n", g_state, local_state) else: stdio.printf("FAIL: global+nonlocal same (g=%d l=%d, expect 5 10)\n", g_state, local_state) # ============================================================ # Test 9: lambda带默认参数与捕获 # ============================================================ def test_lambda_default_and_capture(): stdio.printf("--- Test 9: lambda with arg and capture ---\n") base: t.CInt = 100 multiply = lambda x: base * x r1: t.CInt = multiply(3) r2: t.CInt = multiply(5) if r1 == 300 and r2 == 500: stdio.printf("PASS: lambda arg+capture (100*3=%d 100*5=%d)\n", r1, r2) else: stdio.printf("FAIL: lambda arg+capture (100*3=%d 100*5=%d, expect 300 500)\n", r1, r2) # ============================================================ # Test 10: 嵌套函数多次调用累积 # ============================================================ def test_nested_accumulate(): stdio.printf("--- Test 10: nested function accumulate ---\n") total: t.CInt = 0 def add_val(n: t.CInt): nonlocal total total += n add_val(10) add_val(20) add_val(30) if total == 60: stdio.printf("PASS: nested accumulate (total=%d)\n", total) else: stdio.printf("FAIL: nested accumulate (total=%d, expect 60)\n", total) # ============================================================ # Test 11: nonlocal在while循环中 # ============================================================ def test_nonlocal_while(): stdio.printf("--- Test 11: nonlocal in while loop ---\n") result: t.CInt = 0 def countdown(n: t.CInt): nonlocal result i: t.CInt = n while i > 0: result += i i -= 1 countdown(5) # 5+4+3+2+1=15 if result == 15: stdio.printf("PASS: nonlocal while (result=%d)\n", result) else: stdio.printf("FAIL: nonlocal while (result=%d, expect 15)\n", result) # ============================================================ # Test 12: 嵌套函数修改结构体字段 # ============================================================ class Point: x: t.CInt y: t.CInt def test_nested_modify_struct(): stdio.printf("--- Test 12: nested modify struct ---\n") p: Point = Point() p.x = 0 p.y = 0 def translate(dx: t.CInt, dy: t.CInt): nonlocal p p.x += dx p.y += dy translate(10, 20) translate(5, 15) if p.x == 15 and p.y == 35: stdio.printf("PASS: nested struct (x=%d y=%d)\n", p.x, p.y) else: stdio.printf("FAIL: nested struct (x=%d y=%d, expect 15 35)\n", p.x, p.y) def main() -> t.CInt: stdio.printf("=== ClosureTest2: Advanced Closure/Lambda/Global/Nonlocal ===\n\n") test_nonlocal_conditional() test_nonlocal_multi_var() test_global_cross_functions() test_nested_ref_capture() test_lambda_capture_then_modify() test_nonlocal_loop_accumulate() test_deep_nested_vars() test_global_nonlocal_same_func() test_lambda_default_and_capture() test_nested_accumulate() test_nonlocal_while() test_nested_modify_struct() stdio.printf("\n=== ClosureTest2 Complete ===\n") return 0