Files
Viper_RegressionTesting/ClosureTest2/App/main.vp
T
TermiNexus ee6461be46 chore(comments): Test 仓注释英文化(R11)
把 81 个 .vp 文件的中文 # 注释与三引号文档串改写为简洁英文,每个连续注释块 <=2 行;运行时中文字符串字面量(testcheck.begin/print 等用户可见消息)保持原样。骨架比对 HEAD 与工作区 112 文件漂移 0,全量 77/77 顺序回归通过。
2026-10-05 22:43:38 +08:00

221 lines
7.4 KiB
Plaintext

import t
import stdio
import testcheck
# ============================================================
# Test 1: nonlocal modified in conditional branches
# ============================================================
def test_nonlocal_conditional():
testcheck.section("Test 1: nonlocal in conditional branches")
x: int = 0
def modify(flag: int):
nonlocal x
if flag:
x += 10
else:
x += 100
modify(1)
modify(0)
modify(1)
testcheck.check(x == 120, "nonlocal conditional (x == 120)", "nonlocal conditional (expect 120)")
# ============================================================
# Test 2: multiple nonlocal variables modified together
# ============================================================
def test_nonlocal_multi_var():
testcheck.section("Test 2: multiple nonlocal variables")
a: int = 1
b: int = 2
c_val: int = 3
def swap_add():
nonlocal a, b, c_val
tmp: int = a
a = b
b = c_val
c_val = tmp
swap_add()
testcheck.check(a == 2 and b == 3 and c_val == 1, "multi nonlocal (a==2 b==3 c==1)", "multi nonlocal (expect 2 3 1)")
# ============================================================
# Test 3: global modified across multiple functions
# ============================================================
g_counter: int = 0
def inc_global():
global g_counter
g_counter += 1
def add_global(n: int):
global g_counter
g_counter += n
def test_global_cross_functions():
testcheck.section("Test 3: global cross-function modification")
global g_counter
g_counter = 0
inc_global()
inc_global()
add_global(10)
inc_global()
testcheck.check(g_counter == 13, "global cross (g_counter == 13)", "global cross (expect 13)")
# ============================================================
# Test 4: nested function modifies outer local (reference capture)
# ============================================================
def test_nested_ref_capture():
testcheck.section("Test 4: nested function ref capture")
arr: t.CArray[int, 4] = [0]
def fill():
nonlocal arr
for i in range(4):
arr[i] = (i + 1) * 10
fill()
total: int = arr[0] + arr[1] + arr[2] + arr[3]
testcheck.check(total == 100, "ref capture (total == 100)", "ref capture (expect 100)")
# ============================================================
# Test 5: modify original variable after lambda capture
# ============================================================
def test_lambda_capture_then_modify():
testcheck.section("Test 5: lambda capture then modify")
x: int = 10
f = lambda: x
r1: int = f()
x = 20
r2: int = f()
testcheck.check(r1 == 10 and r2 == 10, "lambda value capture (before==10 after==10)", "lambda value capture (expect 10 10)")
# ============================================================
# Test 6: nonlocal combined with loop
# ============================================================
def test_nonlocal_loop_accumulate():
testcheck.section("Test 6: nonlocal with loop accumulate")
sum_val: int = 0
count: int = 0
def add_numbers(n: int):
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
testcheck.check(sum_val == 21 and count == 8, "loop accumulate (sum==21 count==8)", "loop accumulate (expect 21 8)")
# ============================================================
# Test 7: three-level nesting, each level modifies a different var
# ============================================================
def test_deep_nested_vars():
testcheck.section("Test 7: deep nested different vars")
a: int = 0
b: int = 0
c_val: int = 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()
testcheck.check(a == 2 and b == 20 and c_val == 200, "deep nested (a==2 b==20 c==200)", "deep nested (expect 2 20 200)")
# ============================================================
# Test 8: global and nonlocal in the same nested function
# ============================================================
g_state: int = 0
def test_global_nonlocal_same_func():
testcheck.section("Test 8: global+nonlocal in same nested func")
global g_state
g_state = 0
local_state: int = 0
def worker(n: int):
nonlocal local_state
global g_state
for i in range(n):
g_state += 1
local_state += 2
worker(5)
testcheck.check(g_state == 5 and local_state == 10, "global+nonlocal same (g==5 l==10)", "global+nonlocal same (expect 5 10)")
# ============================================================
# Test 9: lambda with argument and capture
# ============================================================
def test_lambda_default_and_capture():
testcheck.section("Test 9: lambda with arg and capture")
base: int = 100
multiply = lambda x: base * x
r1: int = multiply(3)
r2: int = multiply(5)
testcheck.check(r1 == 300 and r2 == 500, "lambda arg+capture (100*3==300 100*5==500)", "lambda arg+capture (expect 300 500)")
# ============================================================
# Test 10: nested function accumulates over multiple calls
# ============================================================
def test_nested_accumulate():
testcheck.section("Test 10: nested function accumulate")
total: int = 0
def add_val(n: int):
nonlocal total
total += n
add_val(10)
add_val(20)
add_val(30)
testcheck.check(total == 60, "nested accumulate (total == 60)", "nested accumulate (expect 60)")
# ============================================================
# Test 11: nonlocal in while loop
# ============================================================
def test_nonlocal_while():
testcheck.section("Test 11: nonlocal in while loop")
result: int = 0
def countdown(n: int):
nonlocal result
i: int = n
while i > 0:
result += i
i -= 1
countdown(5) # 5+4+3+2+1=15
testcheck.check(result == 15, "nonlocal while (result == 15)", "nonlocal while (expect 15)")
# ============================================================
# Test 12: nested function modifies struct fields
# ============================================================
class Point:
x: int
y: int
def test_nested_modify_struct():
testcheck.section("Test 12: nested modify struct")
p: Point = Point()
p.x = 0
p.y = 0
def translate(dx: int, dy: int):
nonlocal p
p.x += dx
p.y += dy
translate(10, 20)
translate(5, 15)
testcheck.check(p.x == 15 and p.y == 35, "nested struct (x==15 y==35)", "nested struct (expect 15 35)")
def main() -> int:
testcheck.begin("ClosureTest2: Advanced Closure/Lambda/Global/Nonlocal")
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()
return testcheck.end()