From 282c69eb6b3a99480a43d19973253719ba998c5b Mon Sep 17 00:00:00 2001 From: Zalim Bashorov Date: Tue, 26 Mar 2013 20:31:29 +0400 Subject: [PATCH] Fixed return value for ArrayList#remove in js library. Removed unnecessary Kotlin.IndexOutOfBounds. --- .../k2js/test/semantics/ArrayListTest.java | 4 +++ .../testFiles/java/arrayList/cases/remove.kt | 18 +++++------ .../cases/removeWithIndexOutOfBounds.kt | 30 +++++++++++++++++++ js/js.translator/testFiles/kotlin_lib.js | 17 ++++++----- 4 files changed, 50 insertions(+), 19 deletions(-) create mode 100644 js/js.translator/testFiles/java/arrayList/cases/removeWithIndexOutOfBounds.kt diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/ArrayListTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/ArrayListTest.java index b4681a397fa..f6238a98bd5 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/ArrayListTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/ArrayListTest.java @@ -48,6 +48,10 @@ public final class ArrayListTest extends JavaClassesTest { fooBoxTest(); } + public void testRemoveWithIndexOutOfBounds() throws Exception { + fooBoxTest(); + } + public void testToArray() throws Exception { fooBoxTest(); } diff --git a/js/js.translator/testFiles/java/arrayList/cases/remove.kt b/js/js.translator/testFiles/java/arrayList/cases/remove.kt index 20095823895..f4f3a8551ba 100644 --- a/js/js.translator/testFiles/java/arrayList/cases/remove.kt +++ b/js/js.translator/testFiles/java/arrayList/cases/remove.kt @@ -1,17 +1,13 @@ package foo -import java.util.ArrayList; +import java.util.ArrayList fun box() : Boolean { - var i = 0 - val arr = ArrayList(); - while (i++ < 10) { - arr.add(i); + val arr = ArrayList() + for (i in 0..5) { + arr.add(i) } - arr.remove(2) - var sum = 0 - for (a in arr) { - sum += a; - } - return ((sum == 52) && (arr[1] == 2) && (arr[2] == 4) && (arr[3] == 5) && (arr[4] == 6) && (arr[8] == 10)) + + 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 } \ No newline at end of file diff --git a/js/js.translator/testFiles/java/arrayList/cases/removeWithIndexOutOfBounds.kt b/js/js.translator/testFiles/java/arrayList/cases/removeWithIndexOutOfBounds.kt new file mode 100644 index 00000000000..8a869490e81 --- /dev/null +++ b/js/js.translator/testFiles/java/arrayList/cases/removeWithIndexOutOfBounds.kt @@ -0,0 +1,30 @@ +package foo + +import java.util.ArrayList + +fun box() : Boolean { + var threwForEmptyList = false + + val arr = ArrayList() + try { + arr.remove(2) + } + catch(e: IndexOutOfBoundsException) { + threwForEmptyList = true + } + + for (i in 0..10) { + arr.add(i) + } + + var threwForFilled = false + + try { + arr.remove(20) + } + catch(e: IndexOutOfBoundsException) { + threwForFilled = true + } + + return threwForEmptyList && threwForFilled +} \ No newline at end of file diff --git a/js/js.translator/testFiles/kotlin_lib.js b/js/js.translator/testFiles/kotlin_lib.js index 817653ac16c..cf4b0008f47 100644 --- a/js/js.translator/testFiles/kotlin_lib.js +++ b/js/js.translator/testFiles/kotlin_lib.js @@ -75,7 +75,6 @@ var kotlin = {set:function (receiver, key, value) { Kotlin.Exception = Kotlin.$createClass(); Kotlin.RuntimeException = Kotlin.$createClass(Kotlin.Exception); - Kotlin.IndexOutOfBounds = Kotlin.$createClass(Kotlin.Exception); Kotlin.NullPointerException = Kotlin.$createClass(Kotlin.Exception); Kotlin.NoSuchElementException = Kotlin.$createClass(Kotlin.Exception); Kotlin.IllegalArgumentException = Kotlin.$createClass(Kotlin.Exception); @@ -205,15 +204,11 @@ var kotlin = {set:function (receiver, key, value) { this.$size = 0; }, get: function (index) { - if (index < 0 || index >= this.$size) { - throw Kotlin.IndexOutOfBounds; - } + this.checkRange(index); return this.array[index]; }, set: function (index, value) { - if (index < 0 || index >= this.$size) { - throw Kotlin.IndexOutOfBounds; - } + this.checkRange(index); this.array[index] = value; }, toArray: function () { @@ -241,8 +236,9 @@ var kotlin = {set:function (receiver, key, value) { this.$size += collection.size(); }, removeAt: function (index) { - this.array.splice(index, 1); + this.checkRange(index); this.$size--; + return this.array.splice(index, 1)[0]; }, clear: function () { this.array.length = 0; @@ -261,6 +257,11 @@ var kotlin = {set:function (receiver, key, value) { }, toJSON: function () { return this.array; + }, + checkRange: function(index) { + if (index < 0 || index >= this.$size) { + throw new Kotlin.IndexOutOfBoundsException(); + } } });