Minor. Add more tests

This commit is contained in:
Ilmir Usmanov
2021-12-28 11:52:26 +01:00
parent c8817893d4
commit cece7120df
274 changed files with 20551 additions and 25 deletions
@@ -0,0 +1,95 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
var setterInvoked = 0
var backing = 42
OPTIONAL_JVM_INLINE_ANNOTATION
value class DelegateStr<T: String>(val ignored: T) {
operator fun getValue(thisRef: Any?, prop: Any?) =
backing
operator fun setValue(thisRef: Any?, prop: Any?, newValue: Int) {
setterInvoked++
backing = newValue
}
}
OPTIONAL_JVM_INLINE_ANNOTATION
value class DelegateInt<T: Int>(val ignored: T) {
operator fun getValue(thisRef: Any?, prop: Any?) =
backing
operator fun setValue(thisRef: Any?, prop: Any?, newValue: Int) {
setterInvoked++
backing = newValue
}
}
OPTIONAL_JVM_INLINE_ANNOTATION
value class DelegateLong<T: Long>(val ignored: T) {
operator fun getValue(thisRef: Any?, prop: Any?) =
backing
operator fun setValue(thisRef: Any?, prop: Any?, newValue: Int) {
setterInvoked++
backing = newValue
}
}
fun box(): String {
setterInvoked = 0
testDelegateStr()
if (setterInvoked != 1) throw AssertionError()
setterInvoked = 0
testDelegateInt()
if (setterInvoked != 1) throw AssertionError()
setterInvoked = 0
testDelegateLong()
if (setterInvoked != 1) throw AssertionError()
return "OK"
}
private fun testDelegateStr() {
var localD by DelegateStr("don't care")
return {
if (localD != 42) AssertionError()
localD = 1234
if (localD != 1234) throw AssertionError()
if (backing != 1234) throw AssertionError()
}()
}
private fun testDelegateInt() {
var localD by DelegateInt(999)
return {
if (localD != 42) AssertionError()
localD = 1234
if (localD != 1234) throw AssertionError()
if (backing != 1234) throw AssertionError()
}()
}
private fun testDelegateLong() {
var localD by DelegateLong(999L)
return {
if (localD != 42) AssertionError()
localD = 1234
if (localD != 1234) throw AssertionError()
if (backing != 1234) throw AssertionError()
}()
}
@@ -0,0 +1,38 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
class Foo {
var a: Int = 42
var d by Delegate(0)
}
var setterInvoked = 0
OPTIONAL_JVM_INLINE_ANNOTATION
value class Delegate<T: Int>(val default: T) {
operator fun getValue(thisRef: Any?, prop: Any?) =
(thisRef as? Foo)?.a ?: default
operator fun setValue(thisRef: Any?, prop: Any?, newValue: Int) {
setterInvoked++
if (thisRef is Foo) {
thisRef.a = newValue
}
}
}
fun box(): String {
val x = Foo()
if (x.d != 42) throw AssertionError()
x.d = 1234
if (x.d != 1234) throw AssertionError()
if (x.a != 1234) throw AssertionError()
if (setterInvoked != 1) throw AssertionError()
return "OK"
}
@@ -0,0 +1,48 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
class Foo {
var a: Int = 42
var d by DelegateFactory(0)
}
var provideDelegateInvoked = 0
var setterInvoked = 0
OPTIONAL_JVM_INLINE_ANNOTATION
value class DelegateFactory<T: Int>(val default: T) {
operator fun provideDelegate(thisRef: Any?, prop: Any?): Delegate<T> {
provideDelegateInvoked++
return Delegate(default)
}
}
OPTIONAL_JVM_INLINE_ANNOTATION
value class Delegate<T: Int>(val default: T) {
operator fun getValue(thisRef: Any?, prop: Any?) =
(thisRef as? Foo)?.a ?: default
operator fun setValue(thisRef: Any?, prop: Any?, newValue: Int) {
setterInvoked++
if (thisRef is Foo) {
thisRef.a = newValue
}
}
}
fun box(): String {
val x = Foo()
if (x.d != 42) throw AssertionError()
x.d = 1234
if (x.d != 1234) throw AssertionError()
if (x.a != 1234) throw AssertionError()
if (setterInvoked != 1) throw AssertionError()
if (provideDelegateInvoked != 1) throw AssertionError()
return "OK"
}
@@ -0,0 +1,36 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
class Foo {
companion object {
var a: Int = 42
var d by Delegate(0)
}
}
var setterInvoked = 0
OPTIONAL_JVM_INLINE_ANNOTATION
value class Delegate<T: Int>(val ignored: T) {
operator fun getValue(thisRef: Any?, prop: Any?) = Foo.a
operator fun setValue(thisRef: Any?, prop: Any?, newValue: Int) {
setterInvoked++
Foo.a = newValue
}
}
fun box(): String {
if (Foo.d != 42) throw AssertionError()
Foo.d = 1234
if (Foo.d != 1234) throw AssertionError()
if (Foo.a != 1234) throw AssertionError()
if (setterInvoked != 1) throw AssertionError()
return "OK"
}
@@ -0,0 +1,37 @@
// TARGET_BACKEND: JVM
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
class Foo {
companion object {
var a: Int = 42
@JvmStatic var d by Delegate(0)
}
}
var setterInvoked = 0
OPTIONAL_JVM_INLINE_ANNOTATION
value class Delegate<T: Int>(val ignored: T) {
operator fun getValue(thisRef: Any?, prop: Any?) = Foo.a
operator fun setValue(thisRef: Any?, prop: Any?, newValue: Int) {
setterInvoked++
Foo.a = newValue
}
}
fun box(): String {
if (Foo.d != 42) throw AssertionError()
Foo.d = 1234
if (Foo.d != 1234) throw AssertionError()
if (Foo.a != 1234) throw AssertionError()
if (setterInvoked != 1) throw AssertionError()
return "OK"
}
@@ -0,0 +1,89 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
var setterInvoked = 0
var backing = 42
OPTIONAL_JVM_INLINE_ANNOTATION
value class DelegateStr<T: String>(val ignored: T) {
operator fun getValue(thisRef: Any?, prop: Any?) =
backing
operator fun setValue(thisRef: Any?, prop: Any?, newValue: Int) {
setterInvoked++
backing = newValue
}
}
OPTIONAL_JVM_INLINE_ANNOTATION
value class DelegateInt<T: Int>(val ignored: T) {
operator fun getValue(thisRef: Any?, prop: Any?) =
backing
operator fun setValue(thisRef: Any?, prop: Any?, newValue: Int) {
setterInvoked++
backing = newValue
}
}
OPTIONAL_JVM_INLINE_ANNOTATION
value class DelegateLong<T: Long>(val ignored: T) {
operator fun getValue(thisRef: Any?, prop: Any?) =
backing
operator fun setValue(thisRef: Any?, prop: Any?, newValue: Int) {
setterInvoked++
backing = newValue
}
}
fun box(): String {
setterInvoked = 0
testDelegateStr()
if (setterInvoked != 1) throw AssertionError()
setterInvoked = 0
testDelegateInt()
if (setterInvoked != 1) throw AssertionError()
setterInvoked = 0
testDelegateLong()
if (setterInvoked != 1) throw AssertionError()
return "OK"
}
private fun testDelegateStr() {
var localD by DelegateStr("don't care")
if (localD != 42) AssertionError()
localD = 1234
if (localD != 1234) throw AssertionError()
if (backing != 1234) throw AssertionError()
}
private fun testDelegateInt() {
var localD by DelegateInt(999)
if (localD != 42) AssertionError()
localD = 1234
if (localD != 1234) throw AssertionError()
if (backing != 1234) throw AssertionError()
}
private fun testDelegateLong() {
var localD by DelegateLong(999L)
if (localD != 42) AssertionError()
localD = 1234
if (localD != 1234) throw AssertionError()
if (backing != 1234) throw AssertionError()
}
@@ -0,0 +1,34 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
object Foo {
var a: Int = 42
var d by Delegate(0)
}
var setterInvoked = 0
OPTIONAL_JVM_INLINE_ANNOTATION
value class Delegate<T: Int>(val ignored: T) {
operator fun getValue(thisRef: Any?, prop: Any?) = Foo.a
operator fun setValue(thisRef: Any?, prop: Any?, newValue: Int) {
setterInvoked++
Foo.a = newValue
}
}
fun box(): String {
if (Foo.d != 42) throw AssertionError()
Foo.d = 1234
if (Foo.d != 1234) throw AssertionError()
if (Foo.a != 1234) throw AssertionError()
if (setterInvoked != 1) throw AssertionError()
return "OK"
}
@@ -0,0 +1,39 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
class Foo {
companion object {
var a: Int = 42
private var d by Delegate(0)
fun d() = d
fun d(newValue: Int) { d = newValue }
}
}
var setterInvoked = 0
OPTIONAL_JVM_INLINE_ANNOTATION
value class Delegate<T: Int>(val ignored: T) {
operator fun getValue(thisRef: Any?, prop: Any?) = Foo.a
operator fun setValue(thisRef: Any?, prop: Any?, newValue: Int) {
setterInvoked++
Foo.a = newValue
}
}
fun box(): String {
if (Foo.d() != 42) throw AssertionError()
Foo.d(1234)
if (Foo.d() != 1234) throw AssertionError()
if (Foo.a != 1234) throw AssertionError()
if (setterInvoked != 1) throw AssertionError()
return "OK"
}
@@ -0,0 +1,32 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
var setterInvoked = 0
var backing = 42
OPTIONAL_JVM_INLINE_ANNOTATION
value class Delegate<T: Int>(val ignored: T) {
operator fun getValue(thisRef: Any?, prop: Any?) =
backing
operator fun setValue(thisRef: Any?, prop: Any?, newValue: Int) {
setterInvoked++
backing = newValue
}
}
var topLevelD by Delegate(0)
fun box(): String {
if (topLevelD != 42) AssertionError()
topLevelD = 1234
if (topLevelD != 1234) throw AssertionError()
if (backing != 1234) throw AssertionError()
if (setterInvoked != 1) throw AssertionError()
return "OK"
}
@@ -0,0 +1,63 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
import kotlin.reflect.KProperty
OPTIONAL_JVM_INLINE_ANNOTATION
value class ICInt<T: Int>(val i: T)
OPTIONAL_JVM_INLINE_ANNOTATION
value class ICLong<T: Long>(val l: T)
OPTIONAL_JVM_INLINE_ANNOTATION
value class ICOverIC<T: ICLong<Long>>(val o: T)
class Delegate<T>(var f: () -> T) {
operator fun getValue(thisRef: Any?, property: KProperty<*>): T = f()
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
f = { value }
}
}
object Demo {
val i0 by Delegate { ICInt(1) }
val l0 by Delegate { ICLong(2L) }
val o0 by Delegate { ICOverIC(ICLong(3L)) }
val i1: ICInt<Int> by Delegate { ICInt(11) }
val l1: ICLong<Long> by Delegate { ICLong(22) }
val o1: ICOverIC<ICLong<Long>> by Delegate { ICOverIC(ICLong(33)) }
var i2 by Delegate { ICInt(0) }
var l2 by Delegate { ICLong(0) }
var o2 by Delegate { ICOverIC(ICLong(0)) }
}
fun box(): String {
if (Demo.i0.i != 1) return "Fail 1"
if (Demo.l0.l != 2L) return "Fail 2"
if (Demo.o0.o.l != 3L) return "Fail 3"
if (Demo.i1.i != 11) return "Fail 2 1"
if (Demo.l1.l != 22L) return "Fail 2 2"
if (Demo.o1.o.l != 33L) return "Fail 2 3"
Demo.i2 = ICInt(33)
Demo.l2 = ICLong(33)
Demo.o2 = ICOverIC(ICLong(33))
if (Demo.i2.i != 33) return "Fail 3 1"
if (Demo.l2.l != 33L) return "Fail 3 2"
if (Demo.o2.o.l != 33L) return "Fail 3 3"
val localI by Delegate { ICInt(44) }
val localL by Delegate { ICLong(44) }
val localO by Delegate { ICOverIC(ICLong(44)) }
if (localI.i != 44) return "Fail 4 1"
if (localL.l != 44L) return "Fail 4 2"
if (localO.o.l != 44L) return "Fail 4 3"
return "OK"
}
@@ -0,0 +1,25 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
class Foo {
val a: Int = 42
val b by Delegate(0)
}
OPTIONAL_JVM_INLINE_ANNOTATION
value class Delegate<T: Int>(val ignored: T): ReadOnlyProperty<Foo, Int> {
override fun getValue(thisRef: Foo, property: KProperty<*>): Int {
return thisRef.a
}
}
fun box(): String {
val x = Foo()
if (x.b != 42) throw AssertionError()
return "OK"
}
@@ -0,0 +1,17 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
class Delegate {
operator fun getValue(t: Any?, p: Any): String = "OK"
}
OPTIONAL_JVM_INLINE_ANNOTATION
value class Kla1<T: Int>(val default: T) {
fun getValue(): String {
val prop by Delegate()
return prop
}
}
fun box() = Kla1(1).getValue()