tests added

This commit is contained in:
Denis Zharkov
2014-07-09 20:09:18 +04:00
committed by Alexander Udalov
parent e53e4fe257
commit 0f5e29df9b
22 changed files with 541 additions and 2 deletions
@@ -0,0 +1,17 @@
import kotlin.test.assertEquals
inline fun <R, T> foo(x : R?, block : (R?) -> T) : T {
return block(x)
}
fun box() : String {
assertEquals(1L, foo(1) { x -> x!!.toLong() })
assertEquals(1.toShort(), foo(1) { x -> x!!.toShort() })
assertEquals(1.toByte(), foo(1L) { x -> x!!.toByte() })
assertEquals(1.toShort(), foo(1L) { x -> x!!.toShort() })
assertEquals('a'.toDouble(), foo('a') { x -> x!!.toDouble() })
assertEquals(1.0.toByte(), foo(1.0) { x -> x!!.toByte() })
return "OK"
}
@@ -0,0 +1,22 @@
import kotlin.test.assertEquals
inline fun <R, T> foo(x : R, y : R, block : (R) -> T) : T {
val a = x is Number
val b = x is Object
val b1 = x as Object
if (a && b) {
return block(x)
} else {
return block(y)
}
}
fun box() : String {
assertEquals(1, foo(1, 2) { x -> x as Int })
assertEquals("def", foo("abc", "def") { x -> x as String })
return "OK"
}
@@ -0,0 +1,13 @@
import kotlin.test.assertEquals
fun box() : String {
val x = LongArray(5)
for (i in 0..4) {
x[i] = (i + 1).toLong()
}
assertEquals(15L, x.fold(0L) { x, y -> x + y })
return "OK"
}
@@ -0,0 +1,16 @@
import kotlin.test.assertEquals
inline fun <R, T> foo(x : R?, y : R?, block : (R?) -> T) : T {
if (x == null) {
return block(x)
} else {
return block(y)
}
}
fun box() : String {
assertEquals(3, foo(1, 2) { x -> if (x != null) 3 else 4 })
return "OK"
}
@@ -0,0 +1,28 @@
import kotlin.test.assertEquals
fun box() : String {
val result1 = (1..100).count { x -> x % 2 == 0 }
val result2 = (1..100).filter { x -> x % 2 == 0 }.size()
assertEquals(result1, 50)
assertEquals(result2, 50)
val result3 = (1..100).map { x -> 2 * x }.count { x -> x % 2 == 0 }
val result4 = (1..100).map { x -> 2 * x }.filter { x -> x % 2 == 0 }.size()
assertEquals(result3, 100)
assertEquals(result4, 100)
val result5 = (1L..100L).count { x -> x % 2 == 0L }
val result6 = (1L..100L).filter { x -> x % 2 == 0L }.size()
assertEquals(result5, 50)
assertEquals(result6, 50)
val result7 = (1L..100L).map { x -> 2 * x }.count { x -> x % 2 == 0L }
val result8 = (1L..100L).map { x -> 2 * x }.filter { x -> x % 2 == 0L }.size()
assertEquals(result7, 100)
assertEquals(result8, 100)
return "OK"
}
@@ -0,0 +1,28 @@
import kotlin.test.assertEquals
class A(val x : Int, val y : A?)
fun check(a : A?) : Int {
return a?.y?.x ?: (a?.x ?: 3)
}
fun checkLeftAssoc(a : A?) : Int {
return (a?.y?.x ?: a?.x) ?: 3
}
fun box() : String {
val a1 = A(2, A(1, null))
val a2 = A(2, null)
val a3 = null
assertEquals(1, check(a1))
assertEquals(2, check(a2))
assertEquals(3, check(a3))
assertEquals(1, checkLeftAssoc(a1))
assertEquals(2, checkLeftAssoc(a2))
assertEquals(3, checkLeftAssoc(a3))
return "OK"
}
@@ -0,0 +1,12 @@
import kotlin.test.assertEquals
inline fun <R> foo(x : R, block : (R) -> R) : R {
return block(x)
}
fun box() : String {
val result = foo(1) { x -> x + 1 }
assertEquals(2, result)
return "OK"
}
@@ -0,0 +1,34 @@
import kotlin.test.assertEquals
fun returningBoxed() : Int? = 1
fun acceptingBoxed(x : Int?) : Int ? = x
class A(var x : Int? = null)
fun box() : String {
assertEquals(1, returningBoxed())
assertEquals(1, acceptingBoxed(1))
val a = A()
a.x = 1
assertEquals(1, a.x)
val b = Array<Int?>(1, { null })
b[0] = 1
assertEquals(1, b[0])
val x = 1 : Int?
assertEquals(1, x!!.hashCode())
val y = 1000 : Int?
val z = 1000 : Int?
val res = y.identityEquals(z)
val c1: Any = if (1 == 1) 0 else "abc"
val c2: Any = if (1 != 1) 0 else "abc"
assertEquals(0, c1)
assertEquals("abc", c2)
return "OK"
}
@@ -0,0 +1,22 @@
import kotlin.test.assertEquals
inline fun <R, T> foo(x : R, block : (R) -> T) : T {
var y = x
var z = y
z = x
return block(z)
}
fun box() : String {
assertEquals(1, foo(1) { x -> x })
assertEquals(1f, foo(1f) { x -> x })
assertEquals(1L, foo(1L) { x -> x })
assertEquals(1.toDouble(), foo(1.toDouble()) { x -> x })
assertEquals(1.toShort(), foo(1.toShort()) { x -> x })
assertEquals(1.toByte(), foo(1.toByte()) { x -> x })
assertEquals('a', foo('a') { x -> x })
assertEquals(true, foo(true) { x -> x })
return "OK"
}
@@ -0,0 +1,22 @@
inline fun <R, T> foo(x : R?, block : (R?) -> T) : T {
return block(x)
}
fun bar() {
foo(1) { x -> x!!.toLong() }
foo(1) { x -> x!!.toShort() }
foo(1L) { x -> x!!.toByte() }
foo(1L) { x -> x!!.toShort() }
foo('a') { x -> x!!.toDouble() }
foo(1.0) { x -> x!!.toByte() }
}
// 0 valueOf
// 0 Value\s\(\)
// 1 I2L
// 2 L2I
// 2 I2S
// 2 I2B
// 1 I2D
// 1 D2I
@@ -0,0 +1,23 @@
inline fun <R, T> foo(x : R, y : R, block : (R) -> T) : T {
val a = x is Number
val b = x is Object
val a1 = x as Number
val b1 = x as Object
if (a && b) {
return block(x)
} else {
return block(y)
}
}
fun bar() {
foo(1, 2) { x -> x is Int }
}
// 0 valueOf
// 0 Value\s\(\)
// 2 INSTANCEOF
// 2 CHECKCAST
@@ -0,0 +1,13 @@
fun foo() : Long {
val x = LongArray(5)
for (i in 0..4) {
x[i] = (i + 1).toLong()
}
return x.fold(0L) { x, y -> x + y }
}
// 0 valueOf
// 0 Value\s\(\)
@@ -0,0 +1,17 @@
inline fun <R, T> foo(x : R?, y : R?, block : (R?) -> T) : T {
if (x == null) {
return block(x)
} else {
return block(y)
}
}
fun bar() {
foo(1, 2) { x -> if (x != null) 1 else 2 }
}
// 0 valueOf
// 0 Value\s\(\)
// 1 IFNULL
// 0 IFNONNULL
@@ -0,0 +1,37 @@
inline fun <R, T : Iterable<R>> foo(x : T, block : (R) -> R) : R {
val y = x.iterator()
if (y.hasNext()) {
return block(y.next())
} else {
throw RuntimeException()
}
}
fun bar() {
foo((1..100)) { x -> x + 1 }
foo((1L..100L)) { x -> x + 1 }
foo((1.0.toDouble()..2.0.toDouble())) { x -> x + 1 }
foo((1.0f..2.0f)) { x -> x + 1 }
foo((1.toByte()..100.toByte())) { x -> x }
foo((1.toShort()..100.toShort())) { x -> x }
foo(('a'..'z')) { x -> x }
foo(IntRange(1, 100)) { x -> x + 1 }
foo(LongRange(1L, 100L)) { x -> x + 1 }
foo(DoubleRange(1.0, 2.0)) { x -> x + 1 }
foo(FloatRange(1.0f, 2.0f)) { x -> x + 1 }
foo(ByteRange(1.toByte(), 100.toByte())) { x -> x }
foo(ShortRange(1.toShort(), 100.toShort())) { x -> x }
foo(CharRange('a', 'z')) { x -> x }
}
// 1 next\s\(
// 2 nextInt
// 2 nextLong
// 2 nextDouble
// 2 nextFloat
// 2 nextByte
// 2 nextShort
// 2 nextChar
// 0 Value\s\(\)
@@ -0,0 +1,9 @@
class A(val x : Int, val y : A?)
fun check(a : A?) : Int {
return a?.y?.x ?: (a?.x ?: 3)
}
// 0 valueOf
// 0 Value\s\(\)
// 0 ACONST_NULL
@@ -0,0 +1,34 @@
inline fun <R, T> foo(x : R, y : R, block : (R, R) -> T) : T {
return block(x, y)
}
fun bar() {
foo(1, 2) { x, y -> x + y }
foo(1L, 2L) { x, y -> x + y }
foo(1f, 2f) { x, y -> x + y }
foo(1.toDouble(), 2.toDouble()) { x, y -> x + y }
foo(1.toByte(), 2.toByte()) { x, y -> x + y }
foo(1.toShort(), 2.toShort()) { x, y -> x + y }
foo('a', 'b') { x, y -> x == y }
foo(true, false) { x, y -> x || y }
}
// 0 valueOf
// 0 Value\s\(\)
// 1 LOCALVARIABLE x I L6 L11 5
// 1 LOCALVARIABLE y I L6 L11 4
// 1 LOCALVARIABLE x J L19 L24 6
// 1 LOCALVARIABLE y J L19 L24 4
// 1 LOCALVARIABLE x F L32 L37 5
// 1 LOCALVARIABLE y F L32 L37 4
// 1 LOCALVARIABLE x D L45 L50 6
// 1 LOCALVARIABLE y D L45 L50 4
// 1 LOCALVARIABLE x B L58 L63 5
// 1 LOCALVARIABLE y B L58 L63 4
// 1 LOCALVARIABLE x S L71 L76 5
// 1 LOCALVARIABLE y S L71 L76 4
// 1 LOCALVARIABLE x C L84 L91 5
// 1 LOCALVARIABLE y C L84 L91 4
// 1 LOCALVARIABLE x Z L99 L106 5
// 1 LOCALVARIABLE y Z L99 L106 4
@@ -0,0 +1,11 @@
inline fun foo(x : Int, block : (Int) -> Int) : Int {
return block(x)
}
fun bar() {
foo(1) { x -> x + 1 }
}
// 1 java/lang/Integer.valueOf
// 1 intValue
@@ -0,0 +1,28 @@
fun returningBoxed() : Int? = 1
fun acceptingBoxed(x : Int?) : Int ? = x
class A(var x : Int? = null)
fun foo() {
val rb = returningBoxed()
acceptingBoxed(2)
val a = A()
a.x = 3
val b = Array<Int?>(4, { null })
b[100] = 5
val x = 6 : Int?
val hc = x!!.hashCode()
val y = 7 : Int?
val z = 8 : Int?
val res = y.identityEquals(z)
val c1: Any = if (1 == 1) 0 else "abc"
val c2: Any = if (1 != 1) 0 else "abc"
}
// 10 java/lang/Integer.valueOf
// 1 intValue
@@ -0,0 +1,14 @@
fun bar() {
var x : Object? = java.lang.Integer.valueOf(1) as Object?
val y1 : Int = (x as Int?)!!
x = java.lang.Long.valueOf(1) as Object?
val y2 : Long = (x as Long?)!!
}
// 2 valueOf
// 1 intValue
// 1 longValue
@@ -0,0 +1,21 @@
inline fun <R, T> foo(x : R, block : (R) -> T) : T {
var y = x
var z = y
z = x
return block(z)
}
fun bar() {
foo(1) { x -> x }
foo(1f) { x -> x }
foo(1L) { x -> x }
foo(1.toDouble()) { x -> x }
foo(1.toShort()) { x -> x }
foo(1.toByte()) { x -> x }
foo('a') { x -> x }
foo(true) { x -> x }
}
// 0 valueOf
// 0 Value\s\(\)