Refactored tests for switch optimization of When expression

This commit is contained in:
Denis Zharkov
2014-04-09 12:29:53 +04:00
committed by Evgeny Gerashchenko
parent 95345a9bd4
commit 3bc1c45fde
8 changed files with 173 additions and 83 deletions
@@ -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"
}