1.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/StringArray.java Mon Dec 03 00:03:40 2012 +0100
1.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/StringArray.java Mon Dec 03 19:51:46 2012 +0100
1.3 @@ -73,16 +73,16 @@
1.4 }
1.5
1.6 void delete(int indx) {
1.7 - if (arr == null) {
1.8 + if (arr == null || indx < 0 || indx >= arr.length) {
1.9 return;
1.10 }
1.11 String[] tmp = new String[arr.length - 1];
1.12 for (int i = 0, j = 0; i < arr.length; i++) {
1.13 - tmp[j] = arr[i];
1.14 - if (j == indx) {
1.15 - continue;
1.16 + if (i != indx) {
1.17 + tmp[j++] = arr[i];
1.18 }
1.19 }
1.20 + arr = tmp;
1.21 }
1.22
1.23 int indexOf(String ic) {
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/StringArrayTest.java Mon Dec 03 19:51:46 2012 +0100
2.3 @@ -0,0 +1,50 @@
2.4 +/**
2.5 + * Back 2 Browser Bytecode Translator
2.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
2.7 + *
2.8 + * This program is free software: you can redistribute it and/or modify
2.9 + * it under the terms of the GNU General Public License as published by
2.10 + * the Free Software Foundation, version 2 of the License.
2.11 + *
2.12 + * This program is distributed in the hope that it will be useful,
2.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2.15 + * GNU General Public License for more details.
2.16 + *
2.17 + * You should have received a copy of the GNU General Public License
2.18 + * along with this program. Look for COPYING file in the top folder.
2.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
2.20 + */
2.21 +package org.apidesign.vm4brwsr;
2.22 +
2.23 +import org.testng.annotations.Test;
2.24 +import static org.testng.Assert.*;
2.25 +
2.26 +public class StringArrayTest {
2.27 + @Test public void deleteMinusIndex() throws Exception {
2.28 + String[] arr = { "Ahoj", "Kluci" };
2.29 + StringArray list = StringArray.asList(arr);
2.30 + list.delete(-1);
2.31 + assertEquals(list.toArray().length, 2, "No element removed");
2.32 + }
2.33 + @Test public void deleteTooHighIndex() throws Exception {
2.34 + String[] arr = { "Ahoj", "Kluci" };
2.35 + StringArray list = StringArray.asList(arr);
2.36 + list.delete(5);
2.37 + assertEquals(list.toArray().length, 2, "No element removed");
2.38 + }
2.39 + @Test public void deleteFirst() throws Exception {
2.40 + String[] arr = { "Ahoj", "Kluci" };
2.41 + StringArray list = StringArray.asList(arr);
2.42 + list.delete(0);
2.43 + assertEquals(list.toArray().length, 1, "First element removed");
2.44 + assertEquals(list.toArray()[0], "Kluci");
2.45 + }
2.46 + @Test public void deleteSecond() throws Exception {
2.47 + String[] arr = { "Ahoj", "Kluci" };
2.48 + StringArray list = StringArray.asList(arr);
2.49 + list.delete(1);
2.50 + assertEquals(list.toArray().length, 1, "Second element removed");
2.51 + assertEquals(list.toArray()[0], "Ahoj");
2.52 + }
2.53 +}