Files
2026-10-17 21:57:03 +08:00

436 lines
10 KiB
Plaintext

import config
def ForRangeBasicTest(stop: int):
i: int = 0
for i in range(stop):
print("For range: ", i)
print("Done")
def ForRangeWithStartStop(start: int, stop: int, step: int):
i: int = 0
for i in range(start, stop, step):
print("Range: ", i)
print("Done")
def NestedForRangeTest(rows: int, cols: int):
r: int = 0
c_val: int = 0
for r in range(rows):
for c_val in range(cols):
print("Cell: ", r, c_val)
print("Grid done")
def ForWithIfBreakTest(data_size: int):
i: int = 0
found: int = 0
for i in range(data_size):
if i == 5:
print("Found at: ", i)
found = 1
break
print("Checking: ", i)
if found == 0:
print("Not found")
def ForWithIfContinueTest(n: int):
i: int = 0
count: int = 0
for i in range(n):
if i % 2 == 0:
continue
print("Odd: ", i)
count += 1
print("Total odd: ", count)
def ForWithElifBranchTest(value: int):
i: int = 0
result: int = 0
for i in range(value):
if i < 3:
result += 1
elif i < 6:
result += 2
elif i < 9:
result += 3
else:
result += 4
print("Elif branch result: ", result)
def WhileWithIfElifElseTest(iterations: int):
for counter in range(iterations):
if counter < 2:
print("Phase 1: ", counter)
elif counter < 4:
print("Phase 2: ", counter)
elif counter < 6:
print("Phase 3: ", counter)
else:
print("Phase 4: ", counter)
print("While done")
def WhileWithNestedIfTest(limit: int):
outer: int = 0
while outer < limit:
inner: int = 0
while inner < limit:
if inner == 2 and outer == 1:
print("Target: ", outer, inner)
inner += 1
continue
if inner > 3:
break
print("Inner: ", inner)
inner += 1
outer += 1
def WhileWithMatchCaseTest(value: int):
result: int = 0
match value:
case 1:
result = 100
print("Case 1")
case 2:
result = 200
print("Case 2")
case 3:
result = 300
print("Case 3")
case _:
result = 999
print("Default case")
print("Match result: ", result)
def WhileWithMatchCaseNoBreakTest(value: int):
result: int = 0
match value:
case 1:
result = 10
print("Case 1: ", result)
case 2:
result = 20
print("Case 2: ", result)
result += 5
case 3:
result = 30
print("Case 3: ", result)
case _:
result = 0
print("No break match result: ", result)
def ComplexForWhileMatchTest(outer_limit: int, inner_limit: int):
i: int = 0
j: int = 0
result: int = 0
for i in range(outer_limit):
for j in range(inner_limit):
match (i, j):
case (0, 0):
result += 1
case (1, 1):
result += 10
case (2, _):
result += 100
case (_, 0):
result += 1000
case _:
result += 10000
print("Complex match result: ", result)
def ForWithCaseAndNoBreakTest(n: int):
i: int = 0
result: int = 0
for i in range(n):
switch i:
case 0:
result += 1
fallthrough
case 1:
result += 2
fallthrough
case 2:
result += 3
result += 10
fallthrough
case _:
result += i
print("NoBreak result: ", result)
def WhileWithBreakConditionTest(limit: int):
i: int = 0
early_exit: int = 0
while i < limit:
if i == 3:
early_exit = 1
break
if i == 2:
i += 1
continue
print("While i: ", i)
i += 1
def NestedWhileWithMultipleBreakPoints(depth: int, width: int):
w: int = 0
count: int = 0
for d in range(depth):
for w in range(width):
if w == 2:
break
if d == 1 and w == 1:
break
count += 1
print("Nested break count: ", count)
def ForWithMatchCaseInLoopTest(iterations: int):
i: int = 0
sum_even: int = 0
sum_odd: int = 0
for i in range(iterations):
match i % 3:
case 0:
sum_even += i
case 1:
sum_odd += i
case 2:
sum_even += i * 2
case _:
pass
print("Sum even: ", sum_even)
print("Sum odd: ", sum_odd)
def ComplexLogicWithElifChains(a: int, b: int, c_val: int):
result: int = 0
for i in range(10):
if i < a:
if i < b:
result += 1
elif i < c_val:
result += 2
else:
result += 3
elif i < b:
if i < c_val:
result += 4
else:
result += 5
elif i < c_val:
result += 6
else:
result += 7
print("Complex elif chain result: ", result)
def ForRangeWithStepAndCondition(n: int, step: int):
i: int = 0
count: int = 0
for i in range(0, n, step):
if i % 2 == 0:
print("Even step: ", i)
count += 1
else:
if i > 5:
print("Large odd: ", i)
else:
print("Small odd: ", i)
print("Total steps: ", count)
def MatchCaseWithGuardConditions(value: int):
result: int = 0
match value:
case 1 if config.TRUE == 1:
result = 100
case 2 if config.FALSE == 0:
result = 200
case 3:
result = 300
case _:
result = 0
print("Guard match result: ", result)
def WhileWithMultipleMatchCases(iterations: int):
i: int = 0
state: int = 0
while i < iterations:
match state:
case 0:
if i > 3:
state = 1
case 1:
if i > 6:
state = 2
case 2:
if i > 9:
state = 0
print("State at i=", i, ": ", state)
i += 1
def ForWhileMixWithBreakAndContinue(n: int):
i: int = 0
j: int = 0
count: int = 0
for i in range(n):
j = 0
while j < n:
if j == 2:
j += 1
continue
if i == 3 and j == 3:
break
count += 1
j += 1
print("Mix break continue count: ", count)
def ComplexCaseMatchingWithRanges(start: int, end: int):
i: int = 0
bucket0: int = 0
bucket1: int = 0
bucket2: int = 0
bucket3: int = 0
for i in range(start, end):
match i:
case _ if i < 10:
bucket0 += 1
case _ if i < 20:
bucket1 += 1
case _ if i < 30:
bucket2 += 1
case _:
bucket3 += 1
print("Bucket0 (<10): ", bucket0)
print("Bucket1 (10-19): ", bucket1)
print("Bucket2 (20-29): ", bucket2)
print("Bucket3 (>=30): ", bucket3)
def WhileWithElifInElifChain(limit: int):
result: int = 0
for i in range(limit):
if i < 2:
if i == 0:
result = 1
else:
result = 2
elif i < 4:
if i == 2:
result = 3
else:
result = 4
elif i < 6:
if i == 4:
result = 5
else:
result = 6
else:
result = 7
print("Chain result[", i, "]: ", result)
def ForWithCaseNoBreakNested(inner_limit: int):
i: int = 0
j: int = 0
result: int = 0
for i in range(3):
for j in range(inner_limit):
switch j:
case 0:
result += 1
fallthrough
case 1:
result += 2
fallthrough
case _:
result += 3
print("Nested NoBreak result: ", result)
# ============================================================
# break inside match exits the match (C switch semantics), not the outer loop.
# For limit=5 the expected result is 117.
# ============================================================
def MatchCaseBreakTest(limit: int) -> int:
i: int = 0
result: int = 0
for i in range(limit):
match i:
case 1:
result += 100
break
case 2:
result += 10
case _:
result += i
return result
# ============================================================
# switch (C semantics): no fallthrough by default; `fallthrough` continues.
# Expected: value=1 -> 6, value=2 -> 5, value=3 -> 3, else -> 77.
# ============================================================
def SwitchCaseFallthroughTest(value: int) -> int:
result: int = 0
switch value:
case 1:
result += 1
fallthrough
case 2:
result += 2
fallthrough
case 3:
result += 3
default:
result = 77
return result
# ============================================================
# break inside switch exits the switch, not the outer loop.
# For limit=5 the expected result is 117.
# ============================================================
def SwitchCaseBreakTest(limit: int) -> int:
i: int = 0
result: int = 0
for i in range(limit):
switch i:
case 1:
result += 100
break
case 2:
result += 10
default:
result += i
return result
# ============================================================
# switch multi-value case: `case 1 | 2:` hits the same arm.
# Expected: value=1 -> 10, value=2 -> 10, value=3 -> 30, else -> 99.
# ============================================================
def SwitchMultiValueTest(value: int) -> int:
result: int = 0
switch value:
case 1 | 2:
result = 10
case 3:
result = 30
default:
result = 99
return result