From f15bb0bc8eb91d5fb38abbac05d1bce496244588 Mon Sep 17 00:00:00 2001 From: TermiNexus Date: Fri, 2 Oct 2026 23:15:14 +0800 Subject: [PATCH] =?UTF-8?q?test(switch):=20switch=20=E7=A9=BF=E9=80=8F/?= =?UTF-8?q?=E8=B7=B3=E5=87=BA/=E5=A4=9A=E5=80=BC=20case=20=E5=9B=9E?= =?UTF-8?q?=E5=BD=92=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - LogicFlowTest:MatchCaseFallthroughTest 改为 SwitchCaseFallthroughTest(match -> switch,case _ -> default) - 新增 SwitchCaseBreakTest(switch 内 break 只跳出 switch)与 SwitchMultiValueTest(case 1 | 2:) - main.vp:同步注册上述断言 --- LogicFlowTest/App/logic_test.vp | 44 ++++++++++++++++++++++++++++++--- LogicFlowTest/App/main.vp | 26 +++++++++++++------ 2 files changed, 58 insertions(+), 12 deletions(-) diff --git a/LogicFlowTest/App/logic_test.vp b/LogicFlowTest/App/logic_test.vp index 57c39cf..a204cfc 100644 --- a/LogicFlowTest/App/logic_test.vp +++ b/LogicFlowTest/App/logic_test.vp @@ -393,12 +393,12 @@ def MatchCaseBreakTest(limit: t.CInt) -> t.CInt: # ============================================================ -# fallthrough:case 体末尾写 `fallthrough` 则不自动 break,落到下一个 case +# switch(C switch 语义):默认不穿透;case 体内写 `fallthrough` 才落到下一个 case。 # 期望 value=1 → 1+2+3=6;value=2 → 2+3=5;value=3 → 3;其它 → 77 # ============================================================ -def MatchCaseFallthroughTest(value: t.CInt) -> t.CInt: +def SwitchCaseFallthroughTest(value: t.CInt) -> t.CInt: result: t.CInt = 0 - match value: + switch value: case 1: result += 1 fallthrough @@ -407,6 +407,42 @@ def MatchCaseFallthroughTest(value: t.CInt) -> t.CInt: fallthrough case 3: result += 3 - case _: + default: result = 77 + return result + + +# ============================================================ +# switch 内 break = 跳出 switch(不跳出外层循环) +# 期望 limit=5:i=0 走 default (+0)、i=1 走 case 1 (+100) 后 break 出 switch、 +# i=2 走 case 2 (+10)、i=3 (+3)、i=4 (+4) ⇒ 117 +# ============================================================ +def SwitchCaseBreakTest(limit: t.CInt) -> t.CInt: + i: t.CInt = 0 + result: t.CInt = 0 + for i in range(limit): + switch i: + case 1: + result += 100 + break + case 2: + result += 10 + default: + result += i + return result + + +# ============================================================ +# switch 多值 case:`case 1 | 2:` 命中同一个臂 +# 期望 value=1 → 10;value=2 → 10;value=3 → 30;其它 → 99 +# ============================================================ +def SwitchMultiValueTest(value: t.CInt) -> t.CInt: + result: t.CInt = 0 + switch value: + case 1 | 2: + result = 10 + case 3: + result = 30 + default: + result = 99 return result \ No newline at end of file diff --git a/LogicFlowTest/App/main.vp b/LogicFlowTest/App/main.vp index 8dfe1f5..80845ca 100644 --- a/LogicFlowTest/App/main.vp +++ b/LogicFlowTest/App/main.vp @@ -107,20 +107,30 @@ def main() -> t.CInt: 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, + testcheck.section("Switch Case Fallthrough (value=1)") + testcheck.check(logic_test.SwitchCaseFallthroughTest(1) == 6, "fallthrough 1 -> 6", "fallthrough 1 expect 6") - testcheck.section("Match Case Fallthrough (value=2)") - testcheck.check(logic_test.MatchCaseFallthroughTest(2) == 5, + testcheck.section("Switch Case Fallthrough (value=2)") + testcheck.check(logic_test.SwitchCaseFallthroughTest(2) == 5, "fallthrough 2 -> 5", "fallthrough 2 expect 5") - testcheck.section("Match Case Fallthrough (value=3)") - testcheck.check(logic_test.MatchCaseFallthroughTest(3) == 3, + testcheck.section("Switch Case Fallthrough (value=3)") + testcheck.check(logic_test.SwitchCaseFallthroughTest(3) == 3, "fallthrough 3 -> 3", "fallthrough 3 expect 3") - testcheck.section("Match Case Fallthrough (value=9, default)") - testcheck.check(logic_test.MatchCaseFallthroughTest(9) == 77, + testcheck.section("Switch Case Fallthrough (value=9, default)") + testcheck.check(logic_test.SwitchCaseFallthroughTest(9) == 77, "fallthrough default -> 77", "fallthrough default expect 77") + testcheck.section("Switch Case Break = Exit Switch (limit=5)") + testcheck.check(logic_test.SwitchCaseBreakTest(5) == 117, + "switch break exits switch only (117)", "switch break expect 117") + + testcheck.section("Switch Multi-Value Case (1 | 2)") + testcheck.check(logic_test.SwitchMultiValueTest(1) == 10, "switch 1 -> 10", "switch 1 expect 10") + testcheck.check(logic_test.SwitchMultiValueTest(2) == 10, "switch 2 -> 10", "switch 2 expect 10") + testcheck.check(logic_test.SwitchMultiValueTest(3) == 30, "switch 3 -> 30", "switch 3 expect 30") + testcheck.check(logic_test.SwitchMultiValueTest(7) == 99, "switch 7 -> 99", "switch 7 expect 99") + return testcheck.end()