[Tests] Test samples from KEEP

This commit is contained in:
Anastasiya Shadrina
2021-09-06 18:43:33 +07:00
committed by TeamCityServer
parent b0a7be72e8
commit d8faa9686d
50 changed files with 1077 additions and 51 deletions
@@ -0,0 +1,22 @@
// TARGET_BACKEND: JVM_IR
// IGNORE_BACKEND_FIR: JVM_IR
interface Canvas {
val suffix: String
}
interface Shape {
context(Canvas)
fun draw(): String
}
class Circle : Shape {
context(Canvas)
override fun draw() = "OK" + suffix
}
object MyCanvas : Canvas {
override val suffix = ""
}
fun box() = with(MyCanvas) { Circle().draw() }
@@ -0,0 +1,19 @@
// TARGET_BACKEND: JVM_IR
// IGNORE_BACKEND_FIR: JVM_IR
data class Pair<A, B>(val first: A, val second: B)
context(Comparator<T>)
infix operator fun <T> T.compareTo(other: T) = compare(this, other)
context(Comparator<T>)
val <T> Pair<T, T>.min get() = if (first < second) first else second
fun box(): String {
val comparator = Comparator<String> { a, b ->
if (a == null || b == null) 0 else a.length.compareTo(b.length)
}
return with(comparator) {
Pair("OK", "fail").min
}
}
@@ -0,0 +1,17 @@
// TARGET_BACKEND: JVM_IR
// IGNORE_BACKEND_FIR: JVM_IR
// WITH_RUNTIME
fun List<Int>.decimateEveryEvenThird() = sequence {
var counter = 1
for (e in this@List) {
if (e % 2 == 0 && counter % 3 == 0) {
yield(e)
}
counter += 1
}
}
fun box() = with(listOf(0, 1, 2).decimateEveryEvenThird()) {
if (toList() == listOf(2)) "OK" else "fail"
}
@@ -0,0 +1,31 @@
// TARGET_BACKEND: JVM_IR
// IGNORE_BACKEND_FIR: JVM_IR
// WITH_RUNTIME
interface Semigroup<T> {
infix fun T.combine(other: T): T
}
interface Monoid<T> : Semigroup<T> {
val unit: T
}
object IntMonoid : Monoid<Int> {
override fun Int.combine(other: Int): Int = this + other
override val unit: Int = 0
}
object StringMonoid : Monoid<String> {
override fun String.combine(other: String): String = this + other
override val unit: String = ""
}
context(Monoid<T>)
fun <T> List<T>.sum(): T = fold(unit) { acc, e -> acc.combine(e) }
fun box(): String {
with(IntMonoid) {
listOf(1, 2, 3).sum()
}
return with(StringMonoid) {
listOf("O", "K").sum()
}
}