[Tests] Make sure desugaring works with context receivers

This commit is contained in:
Anastasiya Shadrina
2021-11-17 17:39:49 +07:00
committed by TeamCityServer
parent 0bfea4fc52
commit 4d0eb74d79
29 changed files with 2740 additions and 0 deletions
@@ -0,0 +1,33 @@
// !LANGUAGE: +ContextReceivers
// TARGET_BACKEND: JVM_IR
// IGNORE_BACKEND_FIR: JVM_IR
data class MyContainer(var i: Int)
var operationScore = 0
context(Int)
operator fun MyContainer.get(index: Int): Int {
operationScore += this@Int
return if (index == 0) i else -1
}
context(Int)
operator fun MyContainer.plusAssign(other: MyContainer) {
operationScore += this@Int
i += other.i
}
context(Int)
operator fun MyContainer.inc(): MyContainer {
operationScore += this@Int
return MyContainer(i + 1)
}
fun box(): String {
var myContainer = MyContainer(0)
with(1) {
myContainer += MyContainer(myContainer++[0])
}
return if (myContainer.i == 1 && operationScore == 3) "OK" else "fail"
}
@@ -0,0 +1,24 @@
// !LANGUAGE: +ContextReceivers
// TARGET_BACKEND: JVM_IR
// IGNORE_BACKEND_FIR: JVM_IR
data class MyContainer(var s: String)
context(Int)
operator fun MyContainer.get(index: Int): String? {
return if (index == 0 && this@Int == 42) s else null
}
context(Int)
operator fun MyContainer.set(index: Int, value: String) {
if (index != 0 || this@Int != 42) return
s = value
}
fun box(): String {
return with(42) {
val myContainer = MyContainer("fail")
myContainer[0] = "OK"
myContainer[0] ?: "fail"
}
}
@@ -0,0 +1,67 @@
// !LANGUAGE: +ContextReceivers
// TARGET_BACKEND: JVM_IR
// IGNORE_BACKEND_FIR: JVM_IR
data class Result(var i: Int)
var operationScore = 0
context(Int)
operator fun Result.plus(other: Result): Result {
operationScore += this@Int
return Result(i + other.i)
}
context(Int)
operator fun Result.plusAssign(other: Result) {
operationScore += this@Int
i += other.i
}
context(Int)
operator fun Result.minus(other: Result): Result {
operationScore += this@Int
return Result(i - other.i)
}
context(Int)
operator fun Result.minusAssign(other: Result) {
operationScore += this@Int
i -= other.i
}
context(Int)
operator fun Result.times(other: Result): Result {
operationScore += this@Int
return Result(i * other.i)
}
context(Int)
operator fun Result.timesAssign(other: Result) {
operationScore += this@Int
i *= other.i
}
context(Int)
operator fun Result.div(other: Result): Result {
operationScore += this@Int
return Result(i / other.i)
}
context(Int)
operator fun Result.divAssign(other: Result) {
operationScore += this@Int
i /= other.i
}
fun box(): String {
val result = Result(0)
with(1) {
result += (Result(1) + Result(1))
result -= (Result(1) - Result(0))
result *= (Result(1) * Result(2))
result /= (Result(4) / Result(2))
}
return if (result.i == 1 && operationScore == 8) "OK" else "fail"
}
@@ -0,0 +1,36 @@
// !LANGUAGE: +ContextReceivers
// TARGET_BACKEND: JVM_IR
// IGNORE_BACKEND_FIR: JVM_IR
// WITH_STDLIB
import kotlin.reflect.KProperty
var operationScore = 0
class Delegate {
var delegateValue = "fail"
context(Int)
operator fun getValue(thisRef: Any?, property: KProperty<*>): String {
operationScore += this@Int
return delegateValue
}
context(Int)
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) {
operationScore += this@Int
delegateValue = value
}
}
context(Int)
class Result {
var s: String by Delegate()
}
fun box(): String {
val result = with(1) { Result() }
result.s = "OK"
val returnValue = result.s
return if (operationScore == 2) returnValue else "fail"
}
@@ -0,0 +1,28 @@
// !LANGUAGE: +ContextReceivers
// TARGET_BACKEND: JVM_IR
// IGNORE_BACKEND_FIR: JVM_IR
// WITH_STDLIB
data class Counter(var i: Int = 0)
data class CounterConfig(val max: Int = 10)
context(CounterConfig)
class CounterIterator(private val counter: Counter) : Iterator<Int> {
override fun hasNext() = counter.i < max
override fun next() = counter.i++
}
context(CounterConfig)
operator fun Counter.iterator() = with(this@CounterConfig) { CounterIterator(this@Counter) }
fun box(): String {
var result = 0
with(CounterConfig()) {
for (i in Counter()) {
result += i
}
}
return if (result == 45) "OK" else "fail"
}
@@ -0,0 +1,45 @@
// !LANGUAGE: +ContextReceivers
// TARGET_BACKEND: JVM_IR
// IGNORE_BACKEND_FIR: JVM_IR
data class Result(val i: Int)
var operationScore = 0
context(Int)
operator fun Result.unaryMinus(): Result {
operationScore += this@Int
return Result(-i)
}
context(Int)
operator fun Result.unaryPlus(): Result {
operationScore += this@Int
return Result(if (i < 0) (-i) else i)
}
context(Int)
operator fun Result.inc(): Result {
operationScore += this@Int
return Result(i + 1)
}
context(Int)
operator fun Result.dec(): Result {
operationScore += this@Int
return Result(i - 1)
}
fun box(): String {
var result = Result(0)
with(1) {
result++
result++
(-result)
+result
result--
}
return if (result.i == 1 && operationScore == 5) "OK" else "fail"
}