jdk-24/test/langtools/tools/javac/switchexpr/ExpressionSwitchEmbedding.java

152 lines
5.0 KiB
Java
Raw Normal View History

/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8214031
* @summary Verify switch expressions embedded in various statements work properly.
* @compile --enable-preview -source 12 ExpressionSwitchEmbedding.java
* @run main/othervm --enable-preview ExpressionSwitchEmbedding
*/
public class ExpressionSwitchEmbedding {
public static void main(String... args) {
new ExpressionSwitchEmbedding().run();
}
private void run() {
{
int i = 6;
int o = 0;
while (switch (i) {
case 1: i = 0; break true;
case 2: i = 1; break true;
case 3, 4: i--;
if (i == 2 || i == 4) {
break switch (i) {
case 2 -> true;
case 4 -> false;
default -> throw new IllegalStateException();
};
} else {
break true;
}
default: i--; break switch (i) {
case -1 -> false;
case 3 -> true;
default -> true;
};
}) {
o++;
}
if (o != 6 && i >= 0) {
throw new IllegalStateException();
}
}
{
int i = 6;
int o = 0;
if (switch (i) {
case 1: i = 0; break true;
case 2: i = 1; break true;
case 3, 4: i--;
if (i == 2 || i == 4) {
break (switch (i) {
case 2 -> 3;
case 4 -> 5;
default -> throw new IllegalStateException();
}) == i + 1;
} else {
break true;
}
default: i--; break switch (i) {
case -1 -> false;
case 3 -> true;
default -> true;
};
}) {
o++;
}
if (o != 1 && i != 5) {
throw new IllegalStateException();
}
}
{
int o = 0;
for (int i = 6; (switch (i) {
case 1: i = 0; break true;
case 2: i = 1; break true;
case 3, 4: i--;
if (i == 2 || i == 4) {
break switch (i) {
case 2 -> true;
case 4 -> false;
default -> throw new IllegalStateException();
};
} else {
break true;
}
default: i--; break switch (i) {
case -1 -> false;
case 3 -> true;
default -> true;
};
}); ) {
o++;
}
if (o != 6) {
throw new IllegalStateException();
}
}
{
int i = 6;
int o = 0;
do {
o++;
} while (switch (i) {
case 1: i = 0; break true;
case 2: i = 1; break true;
case 3, 4: i--;
if (i == 2 || i == 4) {
break switch (i) {
case 2 -> true;
case 4 -> false;
default -> throw new IllegalStateException();
};
} else {
break true;
}
default: i--; break switch (i) {
case -1 -> false;
case 3 -> true;
default -> true;
};
});
if (o != 6 && i >= 0) {
throw new IllegalStateException();
}
}
}
}