Implement hack to support both remove() and removeAt() in MutableList<Int>

Also add couple of tests about CharSequence.get
This commit is contained in:
Denis Zharkov
2015-10-10 20:49:18 +03:00
parent 742a538aed
commit 89ded4ab1d
10 changed files with 320 additions and 1 deletions
@@ -0,0 +1,24 @@
import java.util.*;
public class J {
public static class B extends A {
public char get(int index) {
if (index == 1) return 'a';
return super.get(index);
}
}
public static String foo() {
B b = new B();
CharSequence cs = (CharSequence) b;
if (cs.charAt(0) != 'z') return "fail 1";
if (b.get(0) != 'z') return "fail 2";
if (cs.charAt(1) != 'a') return "fail 3";
if (b.get(1) != 'a') return "fail 4";
return "OK";
}
}
@@ -0,0 +1,28 @@
open class A : CharSequence {
override fun length(): Int {
throw UnsupportedOperationException()
}
override fun get(index: Int) = 'z';
override fun subSequence(start: Int, end: Int): CharSequence {
throw UnsupportedOperationException()
}
}
fun box(): String {
val b = J.B()
val a = A()
if (b[0] != 'z') return "fail 6"
if (a[0] != 'z') return "fail 7"
if (b[1] != 'a') return "fail 8"
if (a[0] != 'z') return "fail 9"
if (b.get(0) != 'z') return "fail 10"
if (a.get(0) != 'z') return "fail 11"
if (b.get(1) != 'a') return "fail 12"
if (a.get(1) != 'z') return "fail 13"
return J.foo();
}
@@ -0,0 +1,20 @@
import java.util.*;
public class J {
private static class MyList extends A {}
public static String foo() {
MyList myList = new MyList();
List<Integer> list = (List<Integer>) myList;
if (!list.remove((Integer) 1)) return "fail 1";
if (list.remove((int) 1) != 123) return "fail 2";
if (!myList.remove((Integer) 1)) return "fail 3";
if (myList.remove((int) 1) != 123) return "fail 4";
if (myList.removeAt(1) != 123) return "fail 5";
return "OK";
}
}
@@ -0,0 +1,80 @@
open class A : MutableList<Int> {
override val size: Int
get() = throw UnsupportedOperationException()
override val isEmpty: Boolean
get() = throw UnsupportedOperationException()
override fun contains(o: Int): Boolean {
throw UnsupportedOperationException()
}
override fun containsAll(c: Collection<Int>): Boolean {
throw UnsupportedOperationException()
}
override fun get(index: Int): Int {
throw UnsupportedOperationException()
}
override fun indexOf(o: Any?): Int {
throw UnsupportedOperationException()
}
override fun lastIndexOf(o: Any?): Int {
throw UnsupportedOperationException()
}
override fun add(e: Int): Boolean {
throw UnsupportedOperationException()
}
override fun remove(o: Int) = true
override fun removeAt(index: Int): Int = 123
override fun addAll(c: Collection<Int>): Boolean {
throw UnsupportedOperationException()
}
override fun addAll(index: Int, c: Collection<Int>): Boolean {
throw UnsupportedOperationException()
}
override fun removeAll(c: Collection<Any?>): Boolean {
throw UnsupportedOperationException()
}
override fun retainAll(c: Collection<Any?>): Boolean {
throw UnsupportedOperationException()
}
override fun clear() {
throw UnsupportedOperationException()
}
override fun set(index: Int, element: Int): Int {
throw UnsupportedOperationException()
}
override fun add(index: Int, element: Int) {
throw UnsupportedOperationException()
}
override fun listIterator(): MutableListIterator<Int> {
throw UnsupportedOperationException()
}
override fun listIterator(index: Int): MutableListIterator<Int> {
throw UnsupportedOperationException()
}
override fun subList(fromIndex: Int, toIndex: Int): MutableList<Int> {
throw UnsupportedOperationException()
}
override fun iterator(): MutableIterator<Int> {
throw UnsupportedOperationException()
}
}
fun box() = J.foo()