diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TAbstractList.java b/classlib/src/main/java/org/teavm/classlib/java/util/TAbstractList.java index b31a3df7c..179f60306 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/TAbstractList.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TAbstractList.java @@ -144,7 +144,7 @@ public abstract class TAbstractList extends TAbstractCollection implements if (fromIndex > toIndex) { throw new TIllegalArgumentException(); } - if (fromIndex < 0 || toIndex >= size()) { + if (fromIndex < 0 || toIndex > size()) { throw new TIndexOutOfBoundsException(); } if (this instanceof TRandomAccess) { diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TArrays.java b/classlib/src/main/java/org/teavm/classlib/java/util/TArrays.java index c28b6c3d9..ded65c3aa 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/TArrays.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TArrays.java @@ -17,6 +17,7 @@ package org.teavm.classlib.java.util; import java.lang.reflect.Array; import java.util.Objects; +import java.util.RandomAccess; import org.teavm.classlib.java.lang.TClass; import org.teavm.classlib.java.lang.TComparable; import org.teavm.classlib.java.lang.TDouble; @@ -1493,19 +1494,27 @@ public class TArrays extends TObject { @SafeVarargs public static TList asList(final T... a) { - return new TAbstractList() { - @Override public T get(int index) { - return a[index]; - } - @Override public T set(int index, T element) { - T old = a[index]; - a[index] = element; - return old; - } - @Override public int size() { - return a.length; - } - }; + return new ArrayAsList<>(a); + } + + static class ArrayAsList extends TAbstractList implements RandomAccess { + private T[] array; + + public ArrayAsList(T[] array) { + this.array = array; + } + + @Override public T get(int index) { + return array[index]; + } + @Override public T set(int index, T element) { + T old = array[index]; + array[index] = element; + return old; + } + @Override public int size() { + return array.length; + } } public static TString deepToString(Object[] a) { diff --git a/tests/src/test/java/org/teavm/classlib/java/util/ArrayListTest.java b/tests/src/test/java/org/teavm/classlib/java/util/ArrayListTest.java index 899b46ada..5ea618556 100644 --- a/tests/src/test/java/org/teavm/classlib/java/util/ArrayListTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/util/ArrayListTest.java @@ -113,4 +113,49 @@ public class ArrayListTest { } return list; } + + @Test + public void subListRange() { + List list = fillFromZeroToNine(); + + List sublist = list.subList(0, 10); + assertEquals(10, sublist.size()); + + sublist = list.subList(0, 0); + assertEquals(0, sublist.size()); + + sublist = list.subList(10, 10); + assertEquals(0, sublist.size()); + + sublist = list.subList(5, 5); + assertEquals(0, sublist.size()); + + try { + list.subList(-1, -1); + fail("Expected IOOBE for negative indexes"); + } catch (IndexOutOfBoundsException e) { + // OK + } + + try { + list.subList(11, 11); + fail("Expected IOOBE for indexes beyond size"); + } catch (IndexOutOfBoundsException e) { + // OK + } + + try { + list.subList(-1, 11); + fail("Expected IOOBE for indexes beyond limits"); + } catch (IndexOutOfBoundsException e) { + // OK + } + + try { + list.subList(5, 4); + fail("Expected IAE for lowerIndex > upperIndex"); + } catch (IllegalArgumentException e) { + // OK + } + } } diff --git a/tests/src/test/java/org/teavm/classlib/java/util/CollectionsTest.java b/tests/src/test/java/org/teavm/classlib/java/util/CollectionsTest.java index 46778d906..8dba06374 100644 --- a/tests/src/test/java/org/teavm/classlib/java/util/CollectionsTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/util/CollectionsTest.java @@ -116,4 +116,13 @@ public class CollectionsTest { assertEquals(-1, Collections.lastIndexOfSubList(list, Arrays.asList(5, 1))); assertEquals(0, Collections.lastIndexOfSubList(list, list)); } + + @Test + public void shuffleWorksOnArrayAsList() { + List list = Arrays.asList(1, 2, 3, 4); + Collections.shuffle(list); + for (int i = 1; i <= 4; ++i) { + assertTrue("List expected to contain " + i, list.contains(i)); + } + } }