diff --git a/LogicFlowTest/App/logic_test.vp b/LogicFlowTest/App/logic_test.vp index 4d5c266..57c39cf 100644 --- a/LogicFlowTest/App/logic_test.vp +++ b/LogicFlowTest/App/logic_test.vp @@ -369,4 +369,44 @@ def ForWithCaseNoBreakNested(inner_limit: t.CInt): case _: result += 3 c.NoBreak - print("Nested NoBreak result: ", result) \ No newline at end of file + print("Nested NoBreak result: ", result) + + +# ============================================================ +# match case 内 break = 跳出 match(C switch 语义),**不**跳出外层循环 +# 期望 limit=5:i=0 走 _ (+0)、i=1 走 case 1 (+100) 后 break 出 match(循环继续)、 +# i=2 走 case 2 (+10)、i=3 (+3)、i=4 (+4) ⇒ 117 +# ============================================================ +def MatchCaseBreakTest(limit: t.CInt) -> t.CInt: + i: t.CInt = 0 + result: t.CInt = 0 + for i in range(limit): + match i: + case 1: + result += 100 + break + case 2: + result += 10 + case _: + result += i + return result + + +# ============================================================ +# fallthrough:case 体末尾写 `fallthrough` 则不自动 break,落到下一个 case +# 期望 value=1 → 1+2+3=6;value=2 → 2+3=5;value=3 → 3;其它 → 77 +# ============================================================ +def MatchCaseFallthroughTest(value: t.CInt) -> t.CInt: + result: t.CInt = 0 + match value: + case 1: + result += 1 + fallthrough + case 2: + result += 2 + fallthrough + case 3: + result += 3 + case _: + result = 77 + return result \ No newline at end of file diff --git a/LogicFlowTest/App/main.vp b/LogicFlowTest/App/main.vp index 78154c4..8dfe1f5 100644 --- a/LogicFlowTest/App/main.vp +++ b/LogicFlowTest/App/main.vp @@ -103,4 +103,24 @@ def main() -> t.CInt: logic_test.ForWithCaseNoBreakNested(4) testcheck.ok("For With Case NoBreak Nested (4)") + testcheck.section("Match Case Break = Exit Match (limit=5)") + testcheck.check(logic_test.MatchCaseBreakTest(5) == 117, + "match break exits match only (117)", "match break expect 117") + + testcheck.section("Match Case Fallthrough (value=1)") + testcheck.check(logic_test.MatchCaseFallthroughTest(1) == 6, + "fallthrough 1 -> 6", "fallthrough 1 expect 6") + + testcheck.section("Match Case Fallthrough (value=2)") + testcheck.check(logic_test.MatchCaseFallthroughTest(2) == 5, + "fallthrough 2 -> 5", "fallthrough 2 expect 5") + + testcheck.section("Match Case Fallthrough (value=3)") + testcheck.check(logic_test.MatchCaseFallthroughTest(3) == 3, + "fallthrough 3 -> 3", "fallthrough 3 expect 3") + + testcheck.section("Match Case Fallthrough (value=9, default)") + testcheck.check(logic_test.MatchCaseFallthroughTest(9) == 77, + "fallthrough default -> 77", "fallthrough default expect 77") + return testcheck.end()