test(match): 补 break 跳出 match 与 fallthrough 回归用例
- MatchCaseBreakTest:循环内 match 的 case 写 break 只跳出 match(limit=5 ⇒ 117) - MatchCaseFallthroughTest:`fallthrough` 落到下一个 case(value=1/2/3/9 ⇒ 6/5/3/77) - main.vp 注册上述 5 个断言
This commit is contained in:
1 parent
e82440bdd8
commit
05eec02a9a
2 files changed
+61
-1
No files matched your search
@@ -369,4 +369,44 @@ def ForWithCaseNoBreakNested(inner_limit: t.CInt):
|
||||
case _:
|
||||
result += 3
|
||||
c.NoBreak
|
||||
print("Nested NoBreak result: ", result)
|
||||
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
|
||||
@@ -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()
|
||||
Reference in new issue
Block a user