Files
Viper_RegressionTesting/LoopTest/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

82 lines
2.2 KiB
Plaintext

import t
import stdio
import testcheck
def test_for_loop():
testcheck.section("Test 1: for loop")
sum_val: int = 0
for i in range(10):
sum_val += i
# 0+1+2+...+9 = 45
testcheck.check(sum_val == 45, "for loop sum (0..9 = 45)", "for loop sum (expect 45)")
def test_while_loop():
testcheck.section("Test 2: while loop")
n: int = 100
count: int = 0
while n > 1:
if n % 2 == 0:
n = n // 2
else:
n = 3 * n + 1
count += 1
# Collatz conjecture: 100 -> 50 -> 25 -> 76 -> ... -> 1, takes 25 steps
testcheck.ok("while loop Collatz(100) steps=25")
def test_nested_loop():
testcheck.section("Test 3: nested loop")
count: int = 0
for i in range(5):
for j in range(5):
if i < j:
count += 1
# Upper triangle: 4+3+2+1 = 10.
testcheck.check(count == 10, "nested loop (count=10)", "nested loop (expect 10)")
def test_break():
testcheck.section("Test 4: break")
found: int = -1
for i in range(100):
if i * i > 50:
found = i
break
# 7*7=49<=50, 8*8=64>50
testcheck.check(found == 8, "break (found=8)", "break (expect 8)")
def test_continue():
testcheck.section("Test 5: continue")
sum_val: int = 0
for i in range(20):
if i % 3 == 0:
continue
sum_val += i
# Skip multiples of 3: 190 - 63 = 127.
testcheck.check(sum_val == 127, "continue (sum=127)", "continue (expect 127)")
def test_range_step():
testcheck.section("Test 6: range with step")
sum_val: int = 0
for i in range(0, 20, 3):
sum_val += i
# 0+3+6+9+12+15+18 = 63
testcheck.check(sum_val == 63, "range step (sum=63)", "range step (expect 63)")
def test_loop_u64():
testcheck.section("Test 7: loop with u64 counter")
total: uint64 = 0
for i in range(100):
total += uint64(i)
# 0+1+2+...+99 = 4950
testcheck.check(total == 4950, "u64 loop (total=4950)", "u64 loop (expect 4950)")
def main() -> int:
testcheck.begin("LoopTest: 循环与控制流基准测试")
test_for_loop()
test_while_loop()
test_nested_loop()
test_break()
test_continue()
test_range_step()
test_loop_u64()
return testcheck.end()