Refactored tests for switch optimization of When expression
This commit is contained in:
committed by
Evgeny Gerashchenko
parent
95345a9bd4
commit
3bc1c45fde
@@ -1,77 +0,0 @@
|
||||
|
||||
fun foo1(x : Int) : Int {
|
||||
return when ((x % 10)*100) {
|
||||
700,800,900 -> 3
|
||||
100,200,300 -> 1
|
||||
400,500,600 -> 2
|
||||
else -> 4
|
||||
}
|
||||
}
|
||||
|
||||
fun foo2(x : Short) : Int {
|
||||
when ((x % 10).toShort()) {
|
||||
1.toShort(),2.toShort(),3.toShort() -> return 1
|
||||
7.toShort(),8.toShort(),9.toShort() -> return 3
|
||||
4.toShort(),5.toShort(),6.toShort() -> return 2
|
||||
}
|
||||
|
||||
return 4
|
||||
}
|
||||
|
||||
fun foo3(x : Char) : Int {
|
||||
|
||||
when (x) {
|
||||
'5' -> return 2
|
||||
'6' -> return 2
|
||||
'7' -> return 3
|
||||
'8' -> return 3
|
||||
'9' -> return 3
|
||||
'1' -> return 1
|
||||
'2' -> return 1
|
||||
'3' -> return 1
|
||||
'4' -> return 2
|
||||
}
|
||||
|
||||
return 4
|
||||
}
|
||||
|
||||
fun foo4(x : Byte) : Int {
|
||||
return when ((x % 10).toByte()) {
|
||||
1.toByte() -> 1
|
||||
2.toByte() -> 1
|
||||
3.toByte() -> 1
|
||||
4.toByte() -> 2
|
||||
5.toByte() -> 2
|
||||
6.toByte() -> 2
|
||||
7.toByte() -> 3
|
||||
8.toByte() -> 3
|
||||
9.toByte() -> 3
|
||||
else -> 4
|
||||
}
|
||||
}
|
||||
|
||||
fun test(foo : (Int) -> Int, name : String) : String {
|
||||
val foo0 = foo(10)
|
||||
|
||||
var result = if (foo0 == 4) "" else "$name[10/$foo0/4]"
|
||||
|
||||
for (i in 11..19) {
|
||||
val shouldBe = (i-11) / 3 + 1
|
||||
val fooI = foo(i)
|
||||
if (fooI != shouldBe) {
|
||||
result += "$name[$i/$fooI/$shouldBe]"
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val testResult = test({x -> foo1(x)}, "foo1") +
|
||||
test({x -> foo2(x.toShort())}, "foo2") +
|
||||
test({x -> foo3((x % 10 + '0'.toInt()).toChar())}, "foo3") +
|
||||
test({x -> foo4(x.toByte())}, "foo4")
|
||||
if (testResult != "") return testResult
|
||||
return "OK"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
fun foo(x: Int): Int {
|
||||
return when (x) {
|
||||
1, 2, 3 -> 1
|
||||
4, 5, 6 -> 2
|
||||
7, 8, 9 -> 3
|
||||
else -> 4
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
var result = (0..10).map(::foo).makeString()
|
||||
|
||||
if (result != "4, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4") return result
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import java.util.ArrayList
|
||||
|
||||
fun sparse(x: Int): Int {
|
||||
return when ((x % 4) * 100) {
|
||||
100 -> 1
|
||||
200 -> 2
|
||||
300 -> 3
|
||||
else -> 4
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
var result = (0..3).map(::sparse).makeString()
|
||||
|
||||
if (result != "4, 1, 2, 3") return "sparse:" + result
|
||||
return "OK"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
fun exhaustive(x: Int): Int {
|
||||
var r: Int
|
||||
when (x) {
|
||||
1 -> r = 1
|
||||
2 -> r = 2
|
||||
3 -> r = 3
|
||||
else -> r = 4
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
fun nonExhaustive(x: Int): Int {
|
||||
var r: Int = 4
|
||||
when (x) {
|
||||
1 -> r = 1
|
||||
2 -> r = 2
|
||||
3 -> r = 3
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
var result = (0..3).map(::exhaustive).makeString()
|
||||
|
||||
if (result != "4, 1, 2, 3") return "exhaustive:" + result
|
||||
|
||||
result = (0..3).map(::nonExhaustive).makeString()
|
||||
|
||||
if (result != "4, 1, 2, 3") return "non-exhaustive:" + result
|
||||
return "OK"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
fun intFoo(x: Int): Int {
|
||||
return when (x) {
|
||||
1 -> 5
|
||||
2 -> 6
|
||||
3 -> 7
|
||||
else -> 8
|
||||
}
|
||||
}
|
||||
|
||||
fun shortFoo(x: Short): Int {
|
||||
return when (x) {
|
||||
1.toShort() -> 5
|
||||
2.toShort() -> 6
|
||||
3.toShort() -> 7
|
||||
else -> 8
|
||||
}
|
||||
}
|
||||
|
||||
fun byteFoo(x: Byte): Int {
|
||||
return when (x) {
|
||||
1.toByte() -> 5
|
||||
2.toByte() -> 6
|
||||
3.toByte() -> 7
|
||||
else -> 8
|
||||
}
|
||||
}
|
||||
|
||||
fun charFoo(x: Char): Int {
|
||||
return when (x) {
|
||||
'a' -> 5
|
||||
'b' -> 6
|
||||
'c' -> 7
|
||||
else -> 8
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
var result = (1..4).map(::intFoo).makeString()
|
||||
|
||||
if (result != "5, 6, 7, 8") return "int:" + result
|
||||
|
||||
result = (1.toShort()..4.toShort()).map(::shortFoo).makeString()
|
||||
|
||||
if (result != "5, 6, 7, 8") return "short:" + result
|
||||
|
||||
result = (1.toByte()..4.toByte()).map(::byteFoo).makeString()
|
||||
|
||||
if (result != "5, 6, 7, 8") return "byte:" + result
|
||||
|
||||
result = ('a'..'d').map(::charFoo).makeString()
|
||||
|
||||
if (result != "5, 6, 7, 8") return "int:" + result
|
||||
return "OK"
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
fun foo(x: Int): Int {
|
||||
return when (x) {
|
||||
2 -> 6
|
||||
1 -> 5
|
||||
3 -> 7
|
||||
else -> 8
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
var result = (0..3).map(::foo).makeString()
|
||||
|
||||
if (result != "8, 5, 6, 7") return "unordered:" + result
|
||||
return "OK"
|
||||
}
|
||||
@@ -5583,11 +5583,6 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
||||
doTest("compiler/testData/codegen/box/when/range.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("switchOptimization.kt")
|
||||
public void testSwitchOptimization() throws Exception {
|
||||
doTest("compiler/testData/codegen/box/when/switchOptimization.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("whenArgumentIsEvaluatedOnlyOnce.kt")
|
||||
public void testWhenArgumentIsEvaluatedOnlyOnce() throws Exception {
|
||||
doTest("compiler/testData/codegen/box/when/whenArgumentIsEvaluatedOnlyOnce.kt");
|
||||
|
||||
+35
-1
@@ -31,7 +31,7 @@ import org.jetbrains.jet.codegen.generated.AbstractBlackBoxCodegenTest;
|
||||
/** This class is generated by {@link org.jetbrains.jet.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
|
||||
@SuppressWarnings("all")
|
||||
@TestMetadata("compiler/testData/codegen/boxWithStdlib")
|
||||
@InnerTestClasses({BlackBoxWithStdlibCodegenTestGenerated.Annotations.class, BlackBoxWithStdlibCodegenTestGenerated.Arrays.class, BlackBoxWithStdlibCodegenTestGenerated.Casts.class, BlackBoxWithStdlibCodegenTestGenerated.DataClasses.class, BlackBoxWithStdlibCodegenTestGenerated.Evaluate.class, BlackBoxWithStdlibCodegenTestGenerated.FullJdk.class, BlackBoxWithStdlibCodegenTestGenerated.JdkAnnotations.class, BlackBoxWithStdlibCodegenTestGenerated.Ranges.class, BlackBoxWithStdlibCodegenTestGenerated.Regressions.class, BlackBoxWithStdlibCodegenTestGenerated.Strings.class, BlackBoxWithStdlibCodegenTestGenerated.ToArray.class, BlackBoxWithStdlibCodegenTestGenerated.Vararg.class})
|
||||
@InnerTestClasses({BlackBoxWithStdlibCodegenTestGenerated.Annotations.class, BlackBoxWithStdlibCodegenTestGenerated.Arrays.class, BlackBoxWithStdlibCodegenTestGenerated.Casts.class, BlackBoxWithStdlibCodegenTestGenerated.DataClasses.class, BlackBoxWithStdlibCodegenTestGenerated.Evaluate.class, BlackBoxWithStdlibCodegenTestGenerated.FullJdk.class, BlackBoxWithStdlibCodegenTestGenerated.JdkAnnotations.class, BlackBoxWithStdlibCodegenTestGenerated.Ranges.class, BlackBoxWithStdlibCodegenTestGenerated.Regressions.class, BlackBoxWithStdlibCodegenTestGenerated.Strings.class, BlackBoxWithStdlibCodegenTestGenerated.ToArray.class, BlackBoxWithStdlibCodegenTestGenerated.Vararg.class, BlackBoxWithStdlibCodegenTestGenerated.When.class})
|
||||
public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
||||
public void testAllFilesPresentInBoxWithStdlib() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("compiler/testData/codegen/boxWithStdlib"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
@@ -1084,6 +1084,39 @@ public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCode
|
||||
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/codegen/boxWithStdlib/when")
|
||||
public static class When extends AbstractBlackBoxCodegenTest {
|
||||
public void testAllFilesPresentInWhen() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("compiler/testData/codegen/boxWithStdlib/when"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("switchOptimizationMultipleConditions.kt")
|
||||
public void testSwitchOptimizationMultipleConditions() throws Exception {
|
||||
doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/when/switchOptimizationMultipleConditions.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("switchOptimizationSparse.kt")
|
||||
public void testSwitchOptimizationSparse() throws Exception {
|
||||
doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/when/switchOptimizationSparse.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("switchOptimizationStatement.kt")
|
||||
public void testSwitchOptimizationStatement() throws Exception {
|
||||
doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/when/switchOptimizationStatement.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("switchOptimizationTypes.kt")
|
||||
public void testSwitchOptimizationTypes() throws Exception {
|
||||
doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/when/switchOptimizationTypes.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("switchOptimizationUnordered.kt")
|
||||
public void testSwitchOptimizationUnordered() throws Exception {
|
||||
doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/when/switchOptimizationUnordered.kt");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static Test suite() {
|
||||
TestSuite suite = new TestSuite("BlackBoxWithStdlibCodegenTestGenerated");
|
||||
suite.addTestSuite(BlackBoxWithStdlibCodegenTestGenerated.class);
|
||||
@@ -1099,6 +1132,7 @@ public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCode
|
||||
suite.addTestSuite(Strings.class);
|
||||
suite.addTestSuite(ToArray.class);
|
||||
suite.addTestSuite(Vararg.class);
|
||||
suite.addTestSuite(When.class);
|
||||
return suite;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user