test(switch): switch 穿透/跳出/多值 case 回归用例
- LogicFlowTest:MatchCaseFallthroughTest 改为 SwitchCaseFallthroughTest(match -> switch,case _ -> default) - 新增 SwitchCaseBreakTest(switch 内 break 只跳出 switch)与 SwitchMultiValueTest(case 1 | 2:) - main.vp:同步注册上述断言
This commit is contained in:
1 parent
05eec02a9a
commit
f15bb0bc8e
2 files changed
+58
-12
No files matched your search
@@ -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
|
||||
@@ -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()
|
||||
Reference in new issue
Block a user