Js stdlib: fixed the ArrayList implementation and added tests for indexOf, addAll, remove(by ref), removeAll, retainAll, containsAll.
This commit is contained in:
@@ -76,4 +76,20 @@ public final class ArrayListTest extends JavaClassesTest {
|
||||
public void testConstructWithSideEffectParam() throws Exception {
|
||||
fooBoxTest();
|
||||
}
|
||||
|
||||
public void testIndexOf() throws Exception {
|
||||
checkFooBoxIsOk();
|
||||
}
|
||||
|
||||
public void testContainsAll() throws Exception {
|
||||
checkFooBoxIsOk();
|
||||
}
|
||||
|
||||
public void testRemoveAll() throws Exception {
|
||||
checkFooBoxIsOk();
|
||||
}
|
||||
|
||||
public void testRetainAll() throws Exception {
|
||||
checkFooBoxIsOk();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package foo
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
||||
// TODO: drop when arrayListOf will be available here.
|
||||
fun arrayListOf<T>(vararg a: T): ArrayList<T> {
|
||||
val list = ArrayList<T>();
|
||||
|
||||
for (e in a) {
|
||||
list.add(e)
|
||||
}
|
||||
|
||||
return list
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val list = arrayListOf(3, "2", -1, null, 0, 8, 5, "3", 77, -15)
|
||||
val subset = arrayListOf(3, "2", -1, null)
|
||||
val empty = arrayListOf<Any?>()
|
||||
val withOtherElements = arrayListOf(3, 54, null)
|
||||
|
||||
if (!list.containsAll(subset)) return "FAIL: $list.containsAll($subset)"
|
||||
if (!list.containsAll(empty)) return "FAIL: $list.containsAll($empty)"
|
||||
if (list.containsAll(withOtherElements)) return "FAIL: $list.containsAll($withOtherElements)"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package foo
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
// TODO: drop when listOf will be available here.
|
||||
fun listOf<T>(vararg a: T): List<T> {
|
||||
val list = ArrayList<T>();
|
||||
|
||||
for (e in a) {
|
||||
list.add(e)
|
||||
}
|
||||
|
||||
return list
|
||||
}
|
||||
|
||||
fun test<T>(list: List<T>, elements: List<T>, expected: List<Int>, method: List<T>.(T)->Int, methodName: String): String? {
|
||||
for (i in 0..elements.size() - 1) {
|
||||
val actual = list.method(elements[i])
|
||||
if (actual != expected[i]) return "$methodName failed when find: ${elements[i]}, expected: ${expected[i]}, actual: $actual"
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
// 0 1 2 3 4 5 6 7 8 9
|
||||
val list = listOf(3, "2", -1, null, 0, "2", -1, null, 0, 8)
|
||||
val findingElements = listOf(77, null, 0, 8, -15, "2", 5, 3, -1, "3")
|
||||
val expectedIndexOf = listOf(-1, 3, 4, 9, -1, 1,-1, 0, 2, -1)
|
||||
val expectedLastIndexOf = listOf(-1, 7, 8, 9, -1, 5,-1, 0, 6, -1)
|
||||
|
||||
return test(list, findingElements, expectedIndexOf, { indexOf(it) }, "indexOf") ?:
|
||||
test(list, findingElements, expectedLastIndexOf, { lastIndexOf(it) }, "lastIndexOf") ?:
|
||||
"OK"
|
||||
}
|
||||
@@ -8,6 +8,7 @@ fun box() : Boolean {
|
||||
arr.add(i)
|
||||
}
|
||||
|
||||
val removed = arr.remove(2)
|
||||
return arr.size() == 5 && removed == 2 && arr[0] == 0 && arr[1] == 1 && arr[2] == 3 && arr[3] == 4 && arr[4] == 5
|
||||
val removedElement = arr.remove(2)
|
||||
val removed = arr.remove(4: Any)
|
||||
return arr.size() == 4 && removedElement == 2 && removed && arr[0] == 0 && arr[1] == 1 && arr[2] == 3 && arr[3] == 5
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package foo
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
||||
// TODO: drop when listOf will be available here.
|
||||
fun listOf<T>(vararg a: T): List<T> {
|
||||
val list = ArrayList<T>();
|
||||
|
||||
for (e in a) {
|
||||
list.add(e)
|
||||
}
|
||||
|
||||
return list
|
||||
}
|
||||
|
||||
fun test<T>(a: List<T>, b: List<T>, removed: Boolean, expected: List<T>): String? {
|
||||
val t = ArrayList<T>(a.size())
|
||||
t.addAll(a)
|
||||
|
||||
if (t.removeAll(b) != removed) return "$a.removeAll($b) != $removed, result list: $t"
|
||||
if (t != expected) return "Wrong result of $a.removeAll($b), expected: $expected, actual: $t"
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val list = listOf(3, "2", -1, null, 0, 8, 5, "3", 77, -15)
|
||||
val subset = listOf(3, "2", -1, null)
|
||||
val empty = listOf<Any?>()
|
||||
val withOtherElements = listOf(3, 54, null)
|
||||
|
||||
return test(list, subset, removed = true, expected = listOf(0, 8, 5, "3", 77, -15)) ?:
|
||||
test(list, empty, removed = false, expected = list) ?:
|
||||
test(list, withOtherElements, removed = true, expected = listOf("2", -1, 0, 8, 5, "3", 77, -15)) ?:
|
||||
"OK"
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 2010-2014 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package foo
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
||||
// TODO: drop when listOf will be available here.
|
||||
fun listOf<T>(vararg a: T): List<T> {
|
||||
val list = ArrayList<T>();
|
||||
|
||||
for (e in a) {
|
||||
list.add(e)
|
||||
}
|
||||
|
||||
return list
|
||||
}
|
||||
|
||||
fun test<T>(a: List<T>, b: List<T>, removed: Boolean, expected: List<T>): String? {
|
||||
val t = ArrayList<T>(a.size())
|
||||
t.addAll(a)
|
||||
|
||||
if (t.retainAll(b) != removed) return "$a.retainAll($b) != $removed, result list: $t"
|
||||
if (t != expected) return "Wrong result of $a.retainAll($b), expected: $expected, actual: $t"
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val list = listOf(3, "2", -1, null, 0, 8, 5, "3", 77, -15)
|
||||
val subset = listOf(3, "2", -1, null)
|
||||
val empty = listOf<Any?>()
|
||||
val withOtherElements = listOf(3, 54, null)
|
||||
|
||||
return test(list, subset, removed = true, expected = subset) ?:
|
||||
test(list, empty, removed = true, expected = empty) ?:
|
||||
test(list, withOtherElements, removed = true, expected = listOf(3, null)) ?:
|
||||
test(list, list, removed = false, expected = list) ?:
|
||||
"OK"
|
||||
}
|
||||
@@ -104,17 +104,16 @@ String.prototype.contains = function (s) {
|
||||
var ArrayIterator = Kotlin.createClassNow(Kotlin.Iterator,
|
||||
function (array) {
|
||||
this.array = array;
|
||||
this.size = array.length;
|
||||
this.index = 0;
|
||||
}, {
|
||||
next: function () {
|
||||
return this.array[this.index++];
|
||||
},
|
||||
hasNext: function () {
|
||||
return this.index < this.size;
|
||||
return this.index < this.array.length;
|
||||
},
|
||||
remove: function () {
|
||||
if (this.index < 0 || this.index > this.size) throw new RangeError();
|
||||
if (this.index < 0 || this.index > this.array.length) throw new RangeError();
|
||||
this.index--;
|
||||
this.array.splice(this.index, 1);
|
||||
}
|
||||
@@ -182,9 +181,6 @@ String.prototype.contains = function (s) {
|
||||
);
|
||||
|
||||
Kotlin.AbstractCollection = Kotlin.createClassNow(Kotlin.Collection, null, {
|
||||
size: function () {
|
||||
return this.$size;
|
||||
},
|
||||
addAll_5ib00d$: function (collection) {
|
||||
var modified = false;
|
||||
var it = collection.iterator();
|
||||
@@ -247,7 +243,7 @@ String.prototype.contains = function (s) {
|
||||
var builder = "[";
|
||||
var iterator = this.iterator();
|
||||
var first = true;
|
||||
var i = this.$size;
|
||||
var i = this.size();
|
||||
while (i-- > 0) {
|
||||
if (first) {
|
||||
first = false;
|
||||
@@ -270,13 +266,15 @@ String.prototype.contains = function (s) {
|
||||
return new ListIterator(this);
|
||||
},
|
||||
remove_s9cetl$: function (o) {
|
||||
var index = this.indexOf(o);
|
||||
var index = this.indexOf_s9cetl$(o);
|
||||
if (index !== -1) {
|
||||
this.remove_s9c8w6$(index);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
contains_s9cetl$: function (o) {
|
||||
return this.indexOf(o) !== -1;
|
||||
return this.indexOf_s9cetl$(o) !== -1;
|
||||
}
|
||||
});
|
||||
|
||||
@@ -284,7 +282,6 @@ String.prototype.contains = function (s) {
|
||||
Kotlin.ArrayList = Kotlin.createClassNow(Kotlin.AbstractList,
|
||||
function () {
|
||||
this.array = [];
|
||||
this.$size = 0;
|
||||
}, {
|
||||
get_s9c8w6$: function (index) {
|
||||
this.checkRange(index);
|
||||
@@ -295,38 +292,41 @@ String.prototype.contains = function (s) {
|
||||
this.array[index] = value;
|
||||
},
|
||||
size: function () {
|
||||
return this.$size;
|
||||
return this.array.length;
|
||||
},
|
||||
iterator: function () {
|
||||
return Kotlin.arrayIterator(this.array);
|
||||
},
|
||||
add_s9cetl$: function (element) {
|
||||
this.array[this.$size++] = element;
|
||||
this.array.push(element);
|
||||
return true;
|
||||
},
|
||||
add_bar457$: function (index, element) {
|
||||
this.array.splice(index, 0, element);
|
||||
this.$size++;
|
||||
},
|
||||
addAll_5ib00d$: function (collection) {
|
||||
var it = collection.iterator();
|
||||
for (var i = this.$size, n = collection.size(); n-- > 0;) {
|
||||
for (var i = this.array.length, n = collection.size(); n-- > 0;) {
|
||||
this.array[i++] = it.next();
|
||||
}
|
||||
|
||||
this.$size += collection.size();
|
||||
},
|
||||
remove_s9c8w6$: function (index) {
|
||||
this.checkRange(index);
|
||||
this.$size--;
|
||||
return this.array.splice(index, 1)[0];
|
||||
},
|
||||
clear: function () {
|
||||
this.array.length = 0;
|
||||
this.$size = 0;
|
||||
},
|
||||
indexOf: function (o) {
|
||||
for (var i = 0, n = this.$size; i < n; ++i) {
|
||||
indexOf_s9cetl$: function (o) {
|
||||
for (var i = 0; i < this.array.length; i++) {
|
||||
if (Kotlin.equals(this.array[i], o)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
},
|
||||
lastIndexOf_s9cetl$: function (o) {
|
||||
for (var i = this.array.length - 1; i >= 0; i--) {
|
||||
if (Kotlin.equals(this.array[i], o)) {
|
||||
return i;
|
||||
}
|
||||
@@ -334,7 +334,7 @@ String.prototype.contains = function (s) {
|
||||
return -1;
|
||||
},
|
||||
toArray: function () {
|
||||
return this.array.slice(0, this.$size);
|
||||
return this.array.slice(0);
|
||||
},
|
||||
toString: function () {
|
||||
return "[" + this.array.join(", ") + "]";
|
||||
@@ -343,11 +343,11 @@ String.prototype.contains = function (s) {
|
||||
return this.array;
|
||||
},
|
||||
checkRange: function(index) {
|
||||
if (index < 0 || index >= this.$size) {
|
||||
if (index < 0 || index >= this.array.length) {
|
||||
throw new RangeError();
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Kotlin.Runnable = Kotlin.createClassNow(null, null, {
|
||||
run: throwAbstractFunctionInvocationError("Runnable#run")
|
||||
|
||||
Reference in New Issue
Block a user