|
22 | 22 | */ |
23 | 23 |
|
24 | 24 | import java.util.Objects; |
| 25 | +import java.util.function.BiFunction; |
25 | 26 | import java.util.function.Consumer; |
26 | 27 | import java.util.function.Function; |
27 | 28 |
|
28 | 29 | /* |
29 | 30 | * @test |
30 | | - * @bug 8262891 8268333 8268896 |
| 31 | + * @bug 8262891 8268333 8268896 8269802 8269808 |
31 | 32 | * @summary Check behavior of pattern switches. |
32 | 33 | * @compile --enable-preview -source ${jdk.version} Switches.java |
33 | 34 | * @run main/othervm --enable-preview Switches |
@@ -66,6 +67,11 @@ void run() { |
66 | 67 | npeTest(this::npeTestExpression); |
67 | 68 | exhaustiveStatementSane(""); |
68 | 69 | exhaustiveStatementSane(null); |
| 70 | + switchNestingTest(this::switchNestingStatementStatement); |
| 71 | + switchNestingTest(this::switchNestingStatementExpression); |
| 72 | + switchNestingTest(this::switchNestingExpressionStatement); |
| 73 | + switchNestingTest(this::switchNestingExpressionExpression); |
| 74 | + switchNestingTest(this::switchNestingIfSwitch); |
69 | 75 | } |
70 | 76 |
|
71 | 77 | void run(Function<Object, Integer> mapper) { |
@@ -119,6 +125,13 @@ void npeTest(Consumer<I> testCase) { |
119 | 125 | } |
120 | 126 | } |
121 | 127 |
|
| 128 | + void switchNestingTest(BiFunction<Object, Object, String> testCase) { |
| 129 | + assertEquals("string, string", testCase.apply("", "")); |
| 130 | + assertEquals("string, other", testCase.apply("", 1)); |
| 131 | + assertEquals("other, string", testCase.apply(1, "")); |
| 132 | + assertEquals("other, other", testCase.apply(1, 1)); |
| 133 | + } |
| 134 | + |
122 | 135 | int typeTestPatternSwitchTest(Object o) { |
123 | 136 | switch (o) { |
124 | 137 | case String s: return Integer.parseInt(s.toString()); |
@@ -366,6 +379,113 @@ void exhaustiveStatementSane(Object o) { |
366 | 379 | } |
367 | 380 | } |
368 | 381 |
|
| 382 | + String switchNestingStatementStatement(Object o1, Object o2) { |
| 383 | + switch (o1) { |
| 384 | + case String s1 -> { |
| 385 | + switch (o2) { |
| 386 | + case String s2 -> { |
| 387 | + return "string, string"; |
| 388 | + } |
| 389 | + default -> { |
| 390 | + return "string, other"; |
| 391 | + } |
| 392 | + } |
| 393 | + } |
| 394 | + default -> { |
| 395 | + switch (o2) { |
| 396 | + case String s2 -> { |
| 397 | + return "other, string"; |
| 398 | + } |
| 399 | + default -> { |
| 400 | + return "other, other"; |
| 401 | + } |
| 402 | + } |
| 403 | + } |
| 404 | + } |
| 405 | + } |
| 406 | + |
| 407 | + String switchNestingStatementExpression(Object o1, Object o2) { |
| 408 | + switch (o1) { |
| 409 | + case String s1 -> { |
| 410 | + return switch (o2) { |
| 411 | + case String s2 -> "string, string"; |
| 412 | + default -> "string, other"; |
| 413 | + }; |
| 414 | + } |
| 415 | + default -> { |
| 416 | + return switch (o2) { |
| 417 | + case String s2 -> "other, string"; |
| 418 | + default -> "other, other"; |
| 419 | + }; |
| 420 | + } |
| 421 | + } |
| 422 | + } |
| 423 | + |
| 424 | + String switchNestingExpressionStatement(Object o1, Object o2) { |
| 425 | + return switch (o1) { |
| 426 | + case String s1 -> { |
| 427 | + switch (o2) { |
| 428 | + case String s2 -> { |
| 429 | + yield "string, string"; |
| 430 | + } |
| 431 | + default -> { |
| 432 | + yield "string, other"; |
| 433 | + } |
| 434 | + } |
| 435 | + } |
| 436 | + default -> { |
| 437 | + switch (o2) { |
| 438 | + case String s2 -> { |
| 439 | + yield "other, string"; |
| 440 | + } |
| 441 | + default -> { |
| 442 | + yield "other, other"; |
| 443 | + } |
| 444 | + } |
| 445 | + } |
| 446 | + }; |
| 447 | + } |
| 448 | + |
| 449 | + String switchNestingExpressionExpression(Object o1, Object o2) { |
| 450 | + return switch (o1) { |
| 451 | + case String s1 -> |
| 452 | + switch (o2) { |
| 453 | + case String s2 -> "string, string"; |
| 454 | + default -> "string, other"; |
| 455 | + }; |
| 456 | + default -> |
| 457 | + switch (o2) { |
| 458 | + case String s2 -> "other, string"; |
| 459 | + default -> "other, other"; |
| 460 | + }; |
| 461 | + }; |
| 462 | + } |
| 463 | + |
| 464 | + String switchNestingIfSwitch(Object o1, Object o2) { |
| 465 | + BiFunction<Object, Object, String> f = (n1, n2) -> { |
| 466 | + if (o1 instanceof CharSequence cs) { |
| 467 | + return switch (cs) { |
| 468 | + case String s1 -> |
| 469 | + switch (o2) { |
| 470 | + case String s2 -> "string, string"; |
| 471 | + default -> "string, other"; |
| 472 | + }; |
| 473 | + default -> |
| 474 | + switch (o2) { |
| 475 | + case String s2 -> "other, string"; |
| 476 | + default -> "other, other"; |
| 477 | + }; |
| 478 | + }; |
| 479 | + } else { |
| 480 | + return switch (o2) { |
| 481 | + case String s2 -> "other, string"; |
| 482 | + default -> "other, other"; |
| 483 | + }; |
| 484 | + } |
| 485 | + }; |
| 486 | + return f.apply(o1, o2); |
| 487 | + } |
| 488 | + |
369 | 489 | //verify that for cases like: |
370 | 490 | //case ConstantClassClash -> |
371 | 491 | //ConstantClassClash is interpreted as a field, not as a class |
|
0 commit comments