27
27
28
28
/*
29
29
* @test
30
- * @bug 8262891
30
+ * @bug 8262891 8268333
31
31
* @summary Check behavior of pattern switches.
32
32
* @compile --enable-preview -source ${jdk.version} Switches.java
33
33
* @run main/othervm --enable-preview Switches
@@ -46,6 +46,8 @@ void run() {
46
46
assertTrue (testNullSwitch ("" ));
47
47
runArrayTypeTest (this ::testArrayTypeStatement );
48
48
runArrayTypeTest (this ::testArrayTypeExpression );
49
+ runDefaultTest (this ::testDefaultDoesNotDominateStatement );
50
+ runDefaultTest (this ::testDefaultDoesNotDominateExpression );
49
51
runEnumTest (this ::testEnumExpression1 );
50
52
runEnumTest (this ::testEnumExpression2 );
51
53
runEnumTest (this ::testEnumWithGuards1 );
@@ -81,6 +83,13 @@ void runArrayTypeTest(Function<Object, String> mapper) {
81
83
assertEquals ("" , mapper .apply (1.0 ));
82
84
}
83
85
86
+ void runDefaultTest (Function <Object , String > mapper ) {
87
+ assertEquals ("default" , mapper .apply (new int [0 ]));
88
+ assertEquals ("str6" , mapper .apply ("string" ));
89
+ assertEquals ("default" , mapper .apply (1 ));
90
+ assertEquals ("default" , mapper .apply (1.0 ));
91
+ }
92
+
84
93
void runEnumTest (Function <E , String > mapper ) {
85
94
assertEquals ("a" , mapper .apply (E .A ));
86
95
assertEquals ("b" , mapper .apply (E .B ));
@@ -172,6 +181,22 @@ String testArrayTypeExpression(Object o) {
172
181
};
173
182
}
174
183
184
+ String testDefaultDoesNotDominateStatement (Object o ) {
185
+ String res ;
186
+ switch (o ) {
187
+ default -> res = "default" ;
188
+ case String str -> res = "str" + str .length ();
189
+ }
190
+ return res ;
191
+ }
192
+
193
+ String testDefaultDoesNotDominateExpression (Object o ) {
194
+ return switch (o ) {
195
+ case default -> "default" ;
196
+ case String str -> "str" + str .length ();
197
+ };
198
+ }
199
+
175
200
int testStringWithConstant (String str ) {
176
201
switch (str ) {
177
202
case "A" : return 1 ;
0 commit comments