100 lines
2.9 KiB
Python
100 lines
2.9 KiB
Python
import t
|
|
import stdio
|
|
|
|
def test_for_loop():
|
|
stdio.printf("--- Test 1: for loop ---\n")
|
|
sum_val: t.CInt = 0
|
|
for i in range(10):
|
|
sum_val += i
|
|
# 0+1+2+...+9 = 45
|
|
if sum_val == 45:
|
|
stdio.printf("PASS: for loop sum (0..9 = %d)\n", sum_val)
|
|
else:
|
|
stdio.printf("FAIL: for loop sum (%d, expect 45)\n", sum_val)
|
|
|
|
def test_while_loop():
|
|
stdio.printf("--- Test 2: while loop ---\n")
|
|
n: t.CInt = 100
|
|
count: t.CInt = 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
|
|
stdio.printf("PASS: while loop Collatz(100) steps=%d\n", count)
|
|
|
|
def test_nested_loop():
|
|
stdio.printf("--- Test 3: nested loop ---\n")
|
|
count: t.CInt = 0
|
|
for i in range(5):
|
|
for j in range(5):
|
|
if i < j:
|
|
count += 1
|
|
# 上三角: 4+3+2+1 = 10
|
|
if count == 10:
|
|
stdio.printf("PASS: nested loop (count=%d)\n", count)
|
|
else:
|
|
stdio.printf("FAIL: nested loop (count=%d, expect 10)\n", count)
|
|
|
|
def test_break():
|
|
stdio.printf("--- Test 4: break ---\n")
|
|
found: t.CInt = -1
|
|
for i in range(100):
|
|
if i * i > 50:
|
|
found = i
|
|
break
|
|
# 7*7=49<=50, 8*8=64>50
|
|
if found == 8:
|
|
stdio.printf("PASS: break (found=%d)\n", found)
|
|
else:
|
|
stdio.printf("FAIL: break (found=%d, expect 8)\n", found)
|
|
|
|
def test_continue():
|
|
stdio.printf("--- Test 5: continue ---\n")
|
|
sum_val: t.CInt = 0
|
|
for i in range(20):
|
|
if i % 3 == 0:
|
|
continue
|
|
sum_val += i
|
|
# 跳过 0,3,6,9,12,15,18, 剩余和 = sum(0..19) - sum(0,3,6,9,12,15,18) = 190 - 63 = 127
|
|
if sum_val == 127:
|
|
stdio.printf("PASS: continue (sum=%d)\n", sum_val)
|
|
else:
|
|
stdio.printf("FAIL: continue (sum=%d, expect 127)\n", sum_val)
|
|
|
|
def test_range_step():
|
|
stdio.printf("--- Test 6: range with step ---\n")
|
|
sum_val: t.CInt = 0
|
|
for i in range(0, 20, 3):
|
|
sum_val += i
|
|
# 0+3+6+9+12+15+18 = 63
|
|
if sum_val == 63:
|
|
stdio.printf("PASS: range step (sum=%d)\n", sum_val)
|
|
else:
|
|
stdio.printf("FAIL: range step (sum=%d, expect 63)\n", sum_val)
|
|
|
|
def test_loop_u64():
|
|
stdio.printf("--- Test 7: loop with u64 counter ---\n")
|
|
total: t.CUInt64T = 0
|
|
for i in range(100):
|
|
total += t.CUInt64T(i)
|
|
# 0+1+2+...+99 = 4950
|
|
if total == 4950:
|
|
stdio.printf("PASS: u64 loop (total=%lu)\n", total)
|
|
else:
|
|
stdio.printf("FAIL: u64 loop (total=%lu, expect 4950)\n", total)
|
|
|
|
def main() -> t.CInt:
|
|
stdio.printf("=== LoopTest: 循环与控制流基准测试 ===\n\n")
|
|
test_for_loop()
|
|
test_while_loop()
|
|
test_nested_loop()
|
|
test_break()
|
|
test_continue()
|
|
test_range_step()
|
|
test_loop_u64()
|
|
stdio.printf("\n=== LoopTest Complete ===\n")
|
|
return 0
|