Move JVM8 box test to common

This commit is contained in:
Mikhael Bogdanov
2018-10-16 11:06:27 +02:00
parent b61608aba7
commit ac8e1a0124
110 changed files with 0 additions and 0 deletions
@@ -0,0 +1,52 @@
// TARGET_BACKEND: JVM
// FULL_JDK
// WITH_RUNTIME
interface ImmutableCollection<out E> : Collection<E> {
fun add(element: @UnsafeVariance E): ImmutableCollection<E>
fun addAll(elements: Collection<@UnsafeVariance E>): ImmutableCollection<E>
fun remove(element: @UnsafeVariance E): ImmutableCollection<E>
}
class ImmutableCollectionmpl<E> : ImmutableCollection<E> {
override val size: Int
get() = throw UnsupportedOperationException()
override fun contains(element: E): Boolean {
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun containsAll(elements: Collection<E>): Boolean {
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun isEmpty(): Boolean {
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun iterator(): Iterator<E> {
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun add(element: E): ImmutableCollection<E> = this
override fun addAll(elements: Collection<E>): ImmutableCollection<E> = this
override fun remove(element: E): ImmutableCollection<E> = this
}
fun box(): String {
val c = ImmutableCollectionmpl<String>()
if (c.remove("") !== c) return "fail 1"
if (c.add("") !== c) return "fail 2"
if (c.addAll(java.util.ArrayList()) !== c) return "fail 3"
val method = c.javaClass.methods.single { it.name == "remove" && it.returnType == Boolean::class.javaPrimitiveType }
try {
method.invoke(c, "")
return "fail 4"
} catch (e: java.lang.reflect.InvocationTargetException) {
if (e.cause!!.message != "Operation is not supported for read-only collection") return "fail 5: ${e.cause!!.message}"
}
return "OK"
}
@@ -0,0 +1,67 @@
// TARGET_BACKEND: JVM
// FULL_JDK
// WITH_RUNTIME
class A : MutableMap<Any, Any?> {
override val entries: MutableSet<MutableMap.MutableEntry<Any, Any?>>
get() = throw UnsupportedOperationException()
override val keys: MutableSet<Any>
get() = throw UnsupportedOperationException()
override val values: MutableCollection<Any?>
get() = throw UnsupportedOperationException()
override fun clear() {
throw UnsupportedOperationException()
}
override fun put(key: Any, value: Any?): Any? {
throw UnsupportedOperationException()
}
override fun putAll(from: Map<out Any, Any?>) {
throw UnsupportedOperationException()
}
override fun remove(key: Any): Any? {
throw UnsupportedOperationException()
}
override val size: Int
get() = throw UnsupportedOperationException()
override fun containsKey(key: Any): Boolean {
throw UnsupportedOperationException()
}
override fun containsValue(value: Any?): Boolean {
throw UnsupportedOperationException()
}
override fun get(key: Any): Any? {
throw UnsupportedOperationException()
}
override fun isEmpty(): Boolean {
throw UnsupportedOperationException()
}
override fun getOrDefault(key: Any, defaultValue: Any?): Any? {
if (key == "abc") return "cde"
return defaultValue
}
}
fun box(): String {
val a = A()
if (a.getOrDefault("abc", "xyz") != "cde") return "fail 1"
if (a.getOrDefault("56", "123") != "123") return "fail 2"
val mm = a as MutableMap<Any?, Any?>
if (mm.getOrDefault("abc", "xyz") != "cde") return "fail 3"
if (mm.getOrDefault("56", 123) != 123) return "fail 4"
if (mm.getOrDefault(1, "456") != "456") return "fail 5"
if (mm.getOrDefault(null, "qwe") != "qwe") return "fail 6"
if (mm.getOrDefault("abc", null) != "cde") return "fail 7"
return "OK"
}
@@ -0,0 +1,75 @@
// TARGET_BACKEND: JVM
// FULL_JDK
// WITH_RUNTIME
class A : MutableMap<String, String> {
override val entries: MutableSet<MutableMap.MutableEntry<String, String>>
get() = throw UnsupportedOperationException()
override val keys: MutableSet<String>
get() = throw UnsupportedOperationException()
override val values: MutableCollection<String>
get() = throw UnsupportedOperationException()
override fun clear() {
throw UnsupportedOperationException()
}
override fun put(key: String, value: String): String? {
throw UnsupportedOperationException()
}
override fun putAll(from: Map<out String, String>) {
throw UnsupportedOperationException()
}
override fun remove(key: String): String? {
throw UnsupportedOperationException()
}
override val size: Int
get() = throw UnsupportedOperationException()
override fun containsKey(key: String): Boolean {
throw UnsupportedOperationException()
}
override fun containsValue(value: String): Boolean {
throw UnsupportedOperationException()
}
override fun get(key: String): String? {
throw UnsupportedOperationException()
}
override fun isEmpty(): Boolean {
throw UnsupportedOperationException()
}
override fun getOrDefault(key: String, defaultValue: String): String {
if (key == "abc") return "cde"
return defaultValue
}
}
fun box(): String {
val a = A()
if (a.getOrDefault("abc", "xyz") != "cde") return "fail 1"
if (a.getOrDefault("56", "123") != "123") return "fail 2"
val mm = a as MutableMap<Any?, Any?>
if (mm.getOrDefault("abc", "xyz") != "cde") return "fail 3"
if (mm.getOrDefault("56", "123") != "123") return "fail 4"
if (mm.getOrDefault(1, "456") != "456") return "fail 5"
if (mm.getOrDefault(null, "qwe") != "qwe") return "fail 6"
try {
// This is a known problem, there's no way to implement type-safe bridge/barrier properly:
// 'override fun getOrDefault(key: String, defaultValue: String): String' expects two strings,
// and returning defaultValue if Int was received seems incorrect here
mm.getOrDefault("abc", 123)
return "fail 7"
} catch (e: java.lang.ClassCastException) {
}
return "OK"
}
@@ -0,0 +1,81 @@
// TARGET_BACKEND: JVM
// FULL_JDK
// WITH_RUNTIME
class A : MutableMap<Any, Any> {
override val entries: MutableSet<MutableMap.MutableEntry<Any, Any>>
get() = throw UnsupportedOperationException()
override val keys: MutableSet<Any>
get() = throw UnsupportedOperationException()
override val values: MutableCollection<Any>
get() = throw UnsupportedOperationException()
override fun clear() {
throw UnsupportedOperationException()
}
override fun put(key: Any, value: Any): Any? {
throw UnsupportedOperationException()
}
override fun putAll(from: Map<out Any, Any>) {
throw UnsupportedOperationException()
}
override fun remove(key: Any): Any? {
throw UnsupportedOperationException()
}
override val size: Int
get() = throw UnsupportedOperationException()
override fun containsKey(key: Any): Boolean {
throw UnsupportedOperationException()
}
override fun containsValue(value: Any): Boolean {
throw UnsupportedOperationException()
}
override fun get(key: Any): Any? {
throw UnsupportedOperationException()
}
override fun isEmpty(): Boolean {
throw UnsupportedOperationException()
}
override fun getOrDefault(key: Any, defaultValue: Any): Any {
// this condition can not be true because of checkParameterIsNotNull checks in the begin of every method, but it's left here
// to emphasize that we expect these parameters are not null
if (key == null || defaultValue == null) {
throw IllegalArgumentException("fail")
}
if (key == "abc") return "cde"
return defaultValue
}
}
fun box(): String {
val a = A()
if (a.getOrDefault("abc", "xyz") != "cde") return "fail 1"
if (a.getOrDefault("56", "123") != "123") return "fail 2"
val mm = a as MutableMap<Any?, Any?>
if (mm.getOrDefault("abc", "xyz") != "cde") return "fail 3"
if (mm.getOrDefault("56", 123) != 123) return "fail 4"
if (mm.getOrDefault(1, "456") != "456") return "fail 5"
if (mm.getOrDefault(null, "qwe") != "qwe") return "fail 6"
try {
// This is a known problem, there's no way to implement type-safe bridge/barrier properly:
// 'override fun getOrDefault(key: Any, defaultValue: Any): Any' expects two not-nullable values,
// and returning defaultValue if null was received seems incorrect here
mm.getOrDefault("abc", null)
return "fail 7"
} catch (e: java.lang.IllegalArgumentException) {
// Parameter specified as non-null is null
}
return "OK"
}
@@ -0,0 +1,34 @@
// !API_VERSION: 1.3
// !JVM_DEFAULT_MODE: enable
// TARGET_BACKEND: JVM
// JVM_TARGET: 1.8
// WITH_RUNTIME
// FULL_JDK
// There should be no DefaultImpls method for MutableMap.remove(K;V)
interface A<K, V> : MutableMap<K, V>
class B : A<String, String>, java.util.AbstractMap<String, String>() {
override val entries: MutableSet<MutableMap.MutableEntry<String, String>>
get() = java.util.HashSet()
}
interface C<K, V> : MutableMap<K, V> {
@JvmDefault
override fun remove(key: K, value: V) = true
}
class D : A<String, String>, java.util.AbstractMap<String, String>() {
override val entries: MutableSet<MutableMap.MutableEntry<String, String>>
get() = java.util.HashSet()
}
fun box(): String {
val x1 = B().remove("1", "2")
if (x1) return "fail 1"
val x2 = D().remove("3", "4")
if (x1) return "fail 2"
return "OK"
}
@@ -0,0 +1,46 @@
// TARGET_BACKEND: JVM
// FULL_JDK
// WITH_RUNTIME
// FILE: A.java
public class A {
public static void foo(java.util.Map<String, String> x) {
x.remove("abc", "cde");
}
}
// FILE: main.kt
class ReadOnlyMap<K, V>(val x: K, val y: V) : Map<K, V> {
override val entries: Set<Map.Entry<K, V>>
get() = throw UnsupportedOperationException()
override val keys: Set<K>
get() = throw UnsupportedOperationException()
override val size: Int
get() = throw UnsupportedOperationException()
override val values: Collection<V>
get() = throw UnsupportedOperationException()
override fun containsKey(key: K) = key == x
override fun containsValue(value: V) = value == y
override fun get(key: K): V? = if (key == x) y else null
override fun isEmpty() = false
}
fun box(): String {
try {
A.foo(ReadOnlyMap("abc", "cde"))
return "fail 1"
} catch (e: UnsupportedOperationException) { }
try {
// Default Map 'remove' implenetation actually does remove iff entry exists
A.foo(ReadOnlyMap("abc", "123"))
return "fail 2"
} catch (e: UnsupportedOperationException) { }
return "OK"
}
@@ -0,0 +1,69 @@
// TARGET_BACKEND: JVM
// FULL_JDK
// WITH_RUNTIME
class A : MutableMap<String, String> {
override val entries: MutableSet<MutableMap.MutableEntry<String, String>>
get() = throw UnsupportedOperationException()
override val keys: MutableSet<String>
get() = throw UnsupportedOperationException()
override val values: MutableCollection<String>
get() = throw UnsupportedOperationException()
override fun clear() {
throw UnsupportedOperationException()
}
override fun put(key: String, value: String): String? {
throw UnsupportedOperationException()
}
override fun putAll(from: Map<out String, String>) {
throw UnsupportedOperationException()
}
override fun remove(key: String): String? {
throw UnsupportedOperationException()
}
override val size: Int
get() = throw UnsupportedOperationException()
override fun containsKey(key: String): Boolean {
throw UnsupportedOperationException()
}
override fun containsValue(value: String): Boolean {
throw UnsupportedOperationException()
}
override fun get(key: String): String? {
throw UnsupportedOperationException()
}
override fun isEmpty(): Boolean {
throw UnsupportedOperationException()
}
override fun remove(key: String, value: String): Boolean {
val h = key.hashCode() + value.hashCode()
if (h != ("abc".hashCode() + "cde".hashCode())) return false
return key == "abc" && value == "cde"
}
}
fun box(): String {
val a = A()
if (!a.remove("abc", "cde")) return "fail 1"
if (a.remove("abc", "123")) return "fail 2"
val mm = a as MutableMap<Any?, Any?>
if (!mm.remove("abc", "cde")) return "fail 3"
if (mm.remove("abc", "123")) return "fail 4"
if (mm.remove(1, "cde")) return "fail 5"
if (mm.remove(null, "cde")) return "fail 6"
if (mm.remove("abc", null)) return "fail 7"
if (mm.remove(null, null)) return "fail 8"
return "OK"
}
@@ -0,0 +1,69 @@
// TARGET_BACKEND: JVM
// FULL_JDK
// WITH_RUNTIME
class A : MutableMap<Any, Any> {
override val entries: MutableSet<MutableMap.MutableEntry<Any, Any>>
get() = throw UnsupportedOperationException()
override val keys: MutableSet<Any>
get() = throw UnsupportedOperationException()
override val values: MutableCollection<Any>
get() = throw UnsupportedOperationException()
override fun clear() {
throw UnsupportedOperationException()
}
override fun put(key: Any, value: Any): Any? {
throw UnsupportedOperationException()
}
override fun putAll(from: Map<out Any, Any>) {
throw UnsupportedOperationException()
}
override fun remove(key: Any): Any? {
throw UnsupportedOperationException()
}
override val size: Int
get() = throw UnsupportedOperationException()
override fun containsKey(key: Any): Boolean {
throw UnsupportedOperationException()
}
override fun containsValue(value: Any): Boolean {
throw UnsupportedOperationException()
}
override fun get(key: Any): Any? {
throw UnsupportedOperationException()
}
override fun isEmpty(): Boolean {
throw UnsupportedOperationException()
}
override fun remove(key: Any, value: Any): Boolean {
val h = key.hashCode() + value.hashCode()
if (h != ("abc".hashCode() + "cde".hashCode())) return false
return key == "abc" && value == "cde"
}
}
fun box(): String {
val a = A()
if (!a.remove("abc", "cde")) return "fail 1"
if (a.remove("abc", "123")) return "fail 2"
val mm = a as MutableMap<Any?, Any?>
if (!mm.remove("abc", "cde")) return "fail 3"
if (mm.remove("abc", "123")) return "fail 4"
if (mm.remove(1, "cde")) return "fail 5"
if (mm.remove(null, "cde")) return "fail 6"
if (mm.remove("abc", null)) return "fail 7"
if (mm.remove(null, null)) return "fail 8"
return "OK"
}