Generating stub methods for read-only list.

This commit is contained in:
Evgeny Gerashchenko
2013-09-21 11:12:59 +04:00
parent e98b75b462
commit b4368ad578
6 changed files with 277 additions and 3 deletions
@@ -0,0 +1,41 @@
class MyList<T>: List<T> {
override fun size(): Int = 0
override fun isEmpty(): Boolean = true
override fun contains(o: Any?): Boolean = false
override fun iterator(): Iterator<T> = throw Error()
override fun toArray(): Array<Any?> = throw Error()
override fun <E> toArray(a: Array<out E>): Array<E> = throw Error()
override fun containsAll(c: Collection<Any?>): Boolean = false
override fun get(index: Int): T = throw IndexOutOfBoundsException()
override fun indexOf(o: Any?): Int = -1
override fun lastIndexOf(o: Any?): Int = -1
override fun listIterator(): ListIterator<T> = throw Error()
override fun listIterator(index: Int): ListIterator<T> = throw Error()
override fun subList(fromIndex: Int, toIndex: Int): List<T> = this
override fun hashCode(): Int = 0
override fun equals(other: Any?): Boolean = false
}
fun expectUoe(block: () -> Any) {
try {
block()
throw AssertionError()
} catch (e: UnsupportedOperationException) {
}
}
fun box(): String {
val list = MyList<String>() as MutableList<String>
expectUoe { list.add("") }
expectUoe { list.remove("") }
expectUoe { list.addAll(list) }
expectUoe { list.removeAll(list) }
expectUoe { list.retainAll(list) }
expectUoe { list.clear() }
expectUoe { list.set(0, "") }
expectUoe { list.add(0, "") }
expectUoe { list.remove(0) }
return "OK"
}
@@ -0,0 +1,44 @@
class MyList<T>(val v: T): List<T> {
override fun size(): Int = 0
override fun isEmpty(): Boolean = true
override fun contains(o: Any?): Boolean = false
override fun iterator(): Iterator<T> = throw Error()
override fun toArray(): Array<Any?> = throw Error()
override fun <E> toArray(a: Array<out E>): Array<E> = throw Error()
override fun containsAll(c: Collection<Any?>): Boolean = false
override fun get(index: Int): T = v
override fun indexOf(o: Any?): Int = -1
override fun lastIndexOf(o: Any?): Int = -1
override fun listIterator(): ListIterator<T> = throw Error()
override fun listIterator(index: Int): ListIterator<T> = throw Error()
override fun subList(fromIndex: Int, toIndex: Int): List<T> = throw Error()
override fun hashCode(): Int = 0
override fun equals(other: Any?): Boolean = false
public fun add(e: T): Boolean = true
public fun remove(o: Any?): Boolean = true
public fun addAll(c: Collection<T>): Boolean = true
public fun addAll(index: Int, c: Collection<T>): Boolean = true
public fun removeAll(c: Collection<Any?>): Boolean = true
public fun retainAll(c: Collection<Any?>): Boolean = true
public fun clear() {}
public fun set(index: Int, element: T): T = element
public fun add(index: Int, element: T) {}
public fun remove(index: Int): T = v
}
fun box(): String {
val list = MyList<String>("") as MutableList<String>
list.add("")
list.remove("")
list.addAll(list)
list.removeAll(list)
list.retainAll(list)
list.clear()
list.set(0, "")
list.add(0, "")
list.remove(0)
return "OK"
}
@@ -0,0 +1,46 @@
open class Super<T>(val v: T) {
public fun add(e: T): Boolean = true
public fun remove(o: Any?): Boolean = true
public fun addAll(c: Collection<T>): Boolean = true
public fun addAll(index: Int, c: Collection<T>): Boolean = true
public fun removeAll(c: Collection<Any?>): Boolean = true
public fun retainAll(c: Collection<Any?>): Boolean = true
public fun clear() {}
public fun set(index: Int, element: T): T = element
public fun add(index: Int, element: T) {}
public fun remove(index: Int): T = v
}
class MyList<T>(v: T): Super<T>(v), List<T> {
override fun size(): Int = 0
override fun isEmpty(): Boolean = true
override fun contains(o: Any?): Boolean = false
override fun iterator(): Iterator<T> = throw Error()
override fun toArray(): Array<Any?> = throw Error()
override fun <E> toArray(a: Array<out E>): Array<E> = throw Error()
override fun containsAll(c: Collection<Any?>): Boolean = false
override fun get(index: Int): T = v
override fun indexOf(o: Any?): Int = -1
override fun lastIndexOf(o: Any?): Int = -1
override fun listIterator(): ListIterator<T> = throw Error()
override fun listIterator(index: Int): ListIterator<T> = throw Error()
override fun subList(fromIndex: Int, toIndex: Int): List<T> = throw Error()
override fun hashCode(): Int = 0
override fun equals(other: Any?): Boolean = false
}
fun box(): String {
val list = MyList<String>("") as MutableList<String>
list.add("")
list.remove("")
list.addAll(list)
list.removeAll(list)
list.retainAll(list)
list.clear()
list.set(0, "")
list.add(0, "")
list.remove(0)
return "OK"
}
@@ -0,0 +1,41 @@
class MyList: List<String> {
override fun size(): Int = 0
override fun isEmpty(): Boolean = true
override fun contains(o: Any?): Boolean = false
override fun iterator(): Iterator<String> = throw Error()
override fun toArray(): Array<Any?> = throw Error()
override fun <E> toArray(a: Array<out E>): Array<E> = throw Error()
override fun containsAll(c: Collection<Any?>): Boolean = false
override fun get(index: Int): String = throw IndexOutOfBoundsException()
override fun indexOf(o: Any?): Int = -1
override fun lastIndexOf(o: Any?): Int = -1
override fun listIterator(): ListIterator<String> = throw Error()
override fun listIterator(index: Int): ListIterator<String> = throw Error()
override fun subList(fromIndex: Int, toIndex: Int): List<String> = this
override fun hashCode(): Int = 0
override fun equals(other: Any?): Boolean = false
}
fun expectUoe(block: () -> Any) {
try {
block()
throw AssertionError()
} catch (e: UnsupportedOperationException) {
}
}
fun box(): String {
val list = MyList() as MutableList<String>
expectUoe { list.add("") }
expectUoe { list.remove("") }
expectUoe { list.addAll(list) }
expectUoe { list.removeAll(list) }
expectUoe { list.retainAll(list) }
expectUoe { list.clear() }
expectUoe { list.set(0, "") }
expectUoe { list.add(0, "") }
expectUoe { list.remove(0) }
return "OK"
}