Revision: 7994
Author: [email protected]
Date: Tue Apr 27 10:51:33 2010
Log: MutableArray and ImmutableArray JSNI implementation.
Organized tests into client-side and sever-side tests and test suites.
Reapplying http://gwt-code-reviews.appspot.com/363801 but with the supersource in separate forders: bikeshed/super and bikeshed/test-super

Review by: [email protected]
http://code.google.com/p/google-web-toolkit/source/detail?r=7994

Added:
 /branches/2.1/bikeshed/super
 /branches/2.1/bikeshed/super/com
 /branches/2.1/bikeshed/super/com/google
 /branches/2.1/bikeshed/super/com/google/gwt
 /branches/2.1/bikeshed/super/com/google/gwt/collections
 /branches/2.1/bikeshed/super/com/google/gwt/collections/super
 /branches/2.1/bikeshed/super/com/google/gwt/collections/super/com
 /branches/2.1/bikeshed/super/com/google/gwt/collections/super/com/google
/branches/2.1/bikeshed/super/com/google/gwt/collections/super/com/google/gwt /branches/2.1/bikeshed/super/com/google/gwt/collections/super/com/google/gwt/collections /branches/2.1/bikeshed/super/com/google/gwt/collections/super/com/google/gwt/collections/ImmutableArrayImpl.java /branches/2.1/bikeshed/super/com/google/gwt/collections/super/com/google/gwt/collections/MutableArray.java /branches/2.1/bikeshed/test/com/google/gwt/collections/CollectionsServerSideTestSuite.java /branches/2.1/bikeshed/test/com/google/gwt/collections/CollectionsTestSuite.java /branches/2.1/bikeshed/test/com/google/gwt/collections/MutableArrayInternalTest.java /branches/2.1/bikeshed/test/com/google/gwt/collections/MutableArrayTest.java
 /branches/2.1/bikeshed/test-super
 /branches/2.1/bikeshed/test-super/com
 /branches/2.1/bikeshed/test-super/com/google
 /branches/2.1/bikeshed/test-super/com/google/gwt
 /branches/2.1/bikeshed/test-super/com/google/gwt/collections
 /branches/2.1/bikeshed/test-super/com/google/gwt/collections/super
 /branches/2.1/bikeshed/test-super/com/google/gwt/collections/super/com
/branches/2.1/bikeshed/test-super/com/google/gwt/collections/super/com/google /branches/2.1/bikeshed/test-super/com/google/gwt/collections/super/com/google/gwt /branches/2.1/bikeshed/test-super/com/google/gwt/collections/super/com/google/gwt/collections /branches/2.1/bikeshed/test-super/com/google/gwt/collections/super/com/google/gwt/collections/ImmutableArrayInternalTest.java /branches/2.1/bikeshed/test-super/com/google/gwt/collections/super/com/google/gwt/collections/MutableArrayInternalTest.java
Deleted:
 /branches/2.1/bikeshed/test/com/google/gwt/collections/ObjectArrayTest.java
Modified:
 /branches/2.1/bikeshed/src/com/google/gwt/collections/Collections.gwt.xml
 /branches/2.1/bikeshed/src/com/google/gwt/collections/MutableArray.java
/branches/2.1/bikeshed/test/com/google/gwt/collections/ClientMutableArrayTest.java /branches/2.1/bikeshed/test/com/google/gwt/collections/ImmutableArrayTest.java /branches/2.1/bikeshed/test/com/google/gwt/collections/MutableArrayBenchmarkTest.java

=======================================
--- /dev/null
+++ /branches/2.1/bikeshed/super/com/google/gwt/collections/super/com/google/gwt/collections/ImmutableArrayImpl.java Tue Apr 27 10:51:33 2010
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2009 Google Inc.
+ *
+ * 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 com.google.gwt.collections;
+
+import com.google.gwt.core.client.JsArray;
+
+/**
+ * The standard JSNI implementation of an immutable array.
+ *
+ * @param <E> The type stored in the array elements
+ */
+public class ImmutableArrayImpl<E> extends ImmutableArray<E> {
+
+  final JsArray elems;
+
+  ImmutableArrayImpl(JsArray elems) {
+    Assertions.assertNotNull(elems);
+    this.elems = elems;
+  }
+
+  @Override
+  public E get(int index) {
+    Assertions.assertIndexInRange(index, 0, size());
+    return jsniGet(index);
+  }
+
+  @Override
+  public int size() {
+    return elems.length();
+  }
+
+  private native E jsniGet(int index) /*-{
+ return [email protected]::elems[index];
+  }-*/;
+
+}
=======================================
--- /dev/null
+++ /branches/2.1/bikeshed/super/com/google/gwt/collections/super/com/google/gwt/collections/MutableArray.java Tue Apr 27 10:51:33 2010
@@ -0,0 +1,218 @@
+/*
+ * Copyright 2009 Google Inc.
+ *
+ * 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 com.google.gwt.collections;
+
+import com.google.gwt.core.client.JavaScriptObject;
+import com.google.gwt.core.client.JsArray;
+
+/**
+ * An array whose content and length can change over time. This implementation
+ * is used in web mode.
+ *
+ * @param <E> The type stored in the array elements
+ */
+public class MutableArray<E> extends Array<E> {
+
+  JsArray elems;
+
+  private boolean frozen;
+
+  /**
+   * Can only be constructed via {...@link CollectionFactory}.
+   */
+  MutableArray() {
+    elems = (JsArray) JavaScriptObject.createArray();
+  }
+
+  @ConstantTime
+  public void add(E elem) {
+    Assertions.assertNotFrozen(this);
+    jsniAdd(elem);
+  }
+
+  @ConstantTime
+  public void clear() {
+    Assertions.assertNotFrozen(this);
+    elems.setLength(0);
+  }
+
+  /**
+ * Creates an immutable array based on this one. Also marks this object as
+   * read-only. After calling {...@code freeze()}, only use methods from
+ * {...@link Array} or the returned {...@link ImmutableArray} should be to access
+   * the elements of the array is preferred.
+   */
+  public ImmutableArray<E> freeze() {
+    Assertions.markFrozen(this);
+    if (size() != 0) {
+      return new ImmutableArrayImpl(elems);
+    } else {
+      return ImmutableArray.getEmptyInstance();
+    }
+  }
+
+  @Override
+  @ConstantTime
+  public E get(int index) {
+    Assertions.assertIndexInRange(index, 0, size());
+    return jsniGet(index);
+  }
+
+  /**
+   * Inserts {...@code element} before the element residing at {...@code 
index}.
+   *
+ * @param index in the range [0, this.size()], inclusive; if index is equal to
+   *          the array's current size, the result is equivalent to calling
+   *          {...@link #add(Object)}
+   * @param elem the element to insert or {...@code null}
+   */
+  @LinearTime
+  public void insert(int index, E elem) {
+    Assertions.assertNotFrozen(this);
+    Assertions.assertIndexInRange(index, 0, size() + 1);
+    jsniInsert(index, elem);
+  }
+
+  /**
+   * Removes the element at the specified index.
+   */
+  @LinearTime
+  public void remove(int index) {
+    Assertions.assertNotFrozen(this);
+    Assertions.assertIndexInRange(index, 0, size());
+    jsniRemove(index);
+  }
+
+  /**
+   * Replaces the element at the specified index.
+   *
+   * @param index in the range [0, this.size()), exclusive
+   * @param elem the element to insert or {...@code null}
+   */
+  @ConstantTime
+  public void set(int index, E elem) {
+    Assertions.assertNotFrozen(this);
+    Assertions.assertIndexInRange(index, 0, size());
+
+    jsniSet(index, elem);
+  }
+
+  /**
+ * Changes the array size. If {...@code newSize} is less than the current size, + * the array is truncated. If {...@code newSize} is greater than the current size + * the array is grown and the new elements of the array filled up with {...@code
+   * fillValue}.
+   */
+  @LinearTime
+  public void setSize(int newSize, E fillValue) {
+    Assertions.assertNotFrozen(this);
+    jsniSetSize(newSize, fillValue);
+  }
+
+  @Override
+  @ConstantTime
+  public int size() {
+    return elems.length();
+  }
+
+  // Only meant to be called from within Assertions
+  boolean isFrozen() {
+    return frozen;
+  }
+
+  // Only meant to be called from within Assertions
+  void markFrozen() {
+    frozen = true;
+  }
+
+  private Object[] copyNativeArray(JsArray jsElems) {
+    if (jsElems == null) {
+      return null;
+    }
+
+    Object[] jreElems = new Object[jsElems.length()];
+
+    for (int i = 0; i < jsElems.length(); ++i) {
+      jreElems[i] = jsElems.get(i);
+    }
+    return jreElems;
+  }
+
+  @ConstantTime
+  private native void jsniAdd(E elem) /*-{
+    [email protected]::elems.push(elem);
+  }-*/;
+
+  @ConstantTime
+  private native E jsniGet(int index) /*-{
+    return [email protected]::elems[index];
+  }-*/;
+
+  /**
+   * Inserts {...@code element} before the element residing at {...@code 
index}.
+   *
+ * @param index in the range [0, this.size()], inclusive; if index is equal to
+   *          the array's current size, the result is equivalent to calling
+   *          {...@link #add(Object)}
+   * @param elem the element to insert or {...@code null}
+   */
+  @LinearTime
+  private native void jsniInsert(int index, E elem) /*-{
+ [email protected]::elems.splice(index, 0, elem);
+  }-*/;
+
+  /**
+   * Removes the element at the specified index.
+   */
+  @LinearTime
+  private native void jsniRemove(int index) /*-{
+    [email protected]::elems.splice(index, 1);
+  }-*/;
+
+  /**
+   * Replaces the element at the specified index.
+   *
+   * @param index in the range [0, this.size()), exclusive
+   * @param elem the element to insert or {...@code null}
+   */
+  @ConstantTime
+  private native void jsniSet(int index, E elem) /*-{
+    [email protected]::elems[index] = elem;
+  }-*/;
+
+  /**
+ * Changes the array size. If {...@code newSize} is less than the current size, + * the array is truncated. If {...@code newSize} is greater than the current size + * the array is grown and the new elements of the array filled up with {...@code
+   * fillValue}.
+   */
+  @LinearTime
+  private native void jsniSetSize(int newSize, E fillValue) /*-{
+    var fillStart;
+    var i;
+
+ fillStart = [email protected]::elems.length;
+
+    [email protected]::elems.length = newSize;
+
+    if (fillValue != null) {
+      for (i = fillStart; i < newSize; ++i) {
+ [email protected]::elems[i] = fillValue;
+      }
+    }
+  }-*/;
+
+}
=======================================
--- /dev/null
+++ /branches/2.1/bikeshed/test/com/google/gwt/collections/CollectionsServerSideTestSuite.java Tue Apr 27 10:51:33 2010
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2009 Google Inc.
+ *
+ * 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 com.google.gwt.collections;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+/**
+ * This suite collects all server-side Lightweight collections tests.
+ */
+public class CollectionsServerSideTestSuite extends TestSuite {
+  public static Test suite() {
+    TestSuite suite =
+      new TestSuite("All Gwt Lightweight collections unit tests");
+
+    suite.addTestSuite(MutableArrayTest.class);
+    suite.addTestSuite(MutableArrayInternalTest.class);
+    suite.addTestSuite(ImmutableArrayTest.class);
+
+    return suite;
+  }
+}
=======================================
--- /dev/null
+++ /branches/2.1/bikeshed/test/com/google/gwt/collections/CollectionsTestSuite.java Tue Apr 27 10:51:33 2010
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2009 Google Inc.
+ *
+ * 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 com.google.gwt.collections;
+
+import com.google.gwt.junit.tools.GWTTestSuite;
+
+import junit.framework.Test;
+
+/**
+ * This suite collects all client-side Lightweight collections tests.
+ */
+public class CollectionsTestSuite extends GWTTestSuite {
+  public static Test suite() {
+ GWTTestSuite suite = new GWTTestSuite("All Gwt Lightwight collections unit tests");
+
+    suite.addTestSuite(ClientMutableArrayTest.class);
+    suite.addTestSuite(GwtImmutableArrayTest.class);
+
+    return suite;
+  }
+}
=======================================
--- /dev/null
+++ /branches/2.1/bikeshed/test/com/google/gwt/collections/MutableArrayInternalTest.java Tue Apr 27 10:51:33 2010
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2009 Google Inc.
+ *
+ * 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 com.google.gwt.collections;
+
+import com.google.gwt.junit.client.GWTTestCase;
+
+/**
+ * Tests mutable array implementation internal details.
+ */
+public class MutableArrayInternalTest extends GWTTestCase {
+
+  @Override
+  public String getModuleName() {
+    return null;
+  }
+
+  public void testSetSizeNullElems() {
+    MutableArray<String> b = CollectionFactory.createMutableArray();
+
+    b.setSize(1, "fillValue");
+    assertNotNull(b.elems);
+
+    b.setSize(0, null);
+    assertEquals(null, b.elems);
+  }
+
+}
=======================================
--- /dev/null
+++ /branches/2.1/bikeshed/test/com/google/gwt/collections/MutableArrayTest.java Tue Apr 27 10:51:33 2010
@@ -0,0 +1,297 @@
+/*
+ * Copyright 2009 Google Inc.
+ *
+ * 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 com.google.gwt.collections;
+
+import static com.google.gwt.collections.CollectionFactory.createMutableArray;
+
+import com.google.gwt.junit.client.GWTTestCase;
+
+/**
+ * Tests mutable arrays.
+ */
+public class MutableArrayTest extends GWTTestCase {
+
+  boolean assertionsEnabled;
+
+  @Override
+  public String getModuleName() {
+    return null;
+  }
+
+  public void gwtSetUp() {
+    assertionsEnabled = this.getClass().desiredAssertionStatus();
+  }
+
+  public void testClear() {
+    {
+      // Clear an empty array
+      MutableArray<Object> b = createMutableArray();
+      b.clear();
+      assertEquals(0, b.size());
+    }
+
+    {
+      // Clear a non-empty array
+      MutableArray<Object> b = createMutableArray();
+      b.add("string");
+      b.add(false);
+      b.add(3);
+      b.clear();
+      assertEquals(0, b.size());
+    }
+  }
+
+  public void testCreateNonEmptyArray() {
+    MutableArray<String> b = createMutableArray(2, "apples");
+
+    assertEquals(2, b.size());
+    assertEquals("apples", b.get(0));
+    assertEquals("apples", b.get(1));
+  }
+
+  public void testInsertAtBeginning() {
+    final int n = 10;
+    MutableArray<Integer> b = createMutableArray();
+
+    for (int i = 0; i < n; ++i) {
+      b.insert(0, i);
+    }
+
+    for (int i = 0; i < n; ++i) {
+      assertEquals(n - i - 1, b.get(i).intValue());
+    }
+  }
+
+  public void testInsertAtEnd() {
+    final int n = 10;
+    MutableArray<Integer> b = createMutableArray();
+
+    for (int i = 0; i < n; ++i) {
+      b.insert(b.size(), i);
+    }
+
+    for (int i = 0; i < n; ++i) {
+      assertEquals(i, b.get(i).intValue());
+    }
+  }
+
+  public void testInsertInMiddle() {
+    final int n = 10;
+    MutableArray<Integer> b = createMutableArray();
+
+    // Fill the array with 0..(N-1)
+    for (int i = 0; i < n; ++i) {
+      b.insert(b.size(), i);
+    }
+
+    // Double each number by inserting.
+    for (int i = 0; i < n; ++i) {
+      b.insert(i * 2, i);
+    }
+
+    for (int i = 0, j = 0; i < 2 * n; i += 2, ++j) {
+      assertEquals(j, b.get(i).intValue());
+      assertEquals(j, b.get(i + 1).intValue());
+    }
+  }
+
+  public void testMultiElementArrayManipulations() {
+    MutableArray<String> b = createMutableArray();
+    b.add("apple");
+    b.add("banana");
+    b.add("coconut");
+    b.add(null);
+    b.add("donut");
+
+    // On mutable array, get() in order
+    assertEquals("apple", b.get(0));
+    assertEquals("banana", b.get(1));
+    assertEquals("coconut", b.get(2));
+    assertEquals(null, b.get(3));
+    assertEquals("donut", b.get(4));
+
+    // On mutable array, get() in non-sequential order
+    assertEquals("coconut", b.get(2));
+    assertEquals("apple", b.get(0));
+    assertEquals("donut", b.get(4));
+    assertEquals("banana", b.get(1));
+    assertEquals(null, b.get(3));
+
+    // Try a set() call, too
+    b.set(3, "eclair");
+    assertEquals("eclair", b.get(3));
+  }
+
+  public void testRemove() {
+    MutableArray<Integer> b = createMutableArray();
+
+    b.add(1);
+    b.add(2);
+    b.add(3);
+    b.add(4);
+    b.add(5);
+
+    // Remove from the middle to make 1,2,4,5
+    b.remove(2);
+    assertEquals(4, b.size());
+    assertEquals(1, b.get(0).intValue());
+    assertEquals(5, b.get(3).intValue());
+
+    // Remove from the beginning to make 2,4,5
+    b.remove(0);
+    assertEquals(3, b.size());
+    assertEquals(2, b.get(0).intValue());
+    assertEquals(5, b.get(2).intValue());
+
+    // Remove from the end to make 2,4
+    b.remove(b.size() - 1);
+    assertEquals(2, b.size());
+    assertEquals(2, b.get(0).intValue());
+    assertEquals(4, b.get(1).intValue());
+  }
+
+  public void testSetSize() {
+    MutableArray<String> b = createMutableArray();
+
+    b.setSize(1, "fillValue");
+    assertEquals(1, b.size());
+    assertEquals("fillValue", b.get(0));
+
+    b.setSize(2, "anotherValue");
+    assertEquals(2, b.size());
+    assertEquals("fillValue", b.get(0));
+    assertEquals("anotherValue", b.get(1));
+
+    b.setSize(1, null);
+    assertEquals(1, b.size());
+    assertEquals("fillValue", b.get(0));
+
+    b.setSize(0, null);
+    assertEquals(0, b.size());
+  }
+
+  public void testSingleElementAddAndRemove() {
+      MutableArray<String> a = createMutableArray();
+
+      a.add("foo");
+
+      assertEquals(1, a.size());
+      assertEquals("foo", a.get(0));
+
+      a.remove(0);
+
+      assertEquals(0, a.size());
+
+      a.add("bar");
+
+      assertEquals(1, a.size());
+      assertEquals("bar", a.get(0));
+  }
+
+  public void testSingleElementNull() {
+    MutableArray<String> b = createMutableArray();
+    b.add(null);
+
+    assertEquals(null, b.get(0));
+  }
+
+  public void testSingletonArrayCreationAndRetrieval() throws Exception {
+    final int c = 2112;
+    MutableArray<Integer> b = createMutableArray();
+    b.add(c);
+    assertEquals(c, b.get(0).intValue());
+
+    Array<Integer> a = b;
+    assertEquals(1, a.size());
+
+    assertEquals((Integer) 2112, a.get(0));
+  }
+
+  public void testSingletonArrayInvalidIndex() throws Exception {
+    MutableArray<Boolean> b = createMutableArray();
+    b.add(false);
+    Array<Boolean> a = b;
+
+    assertEquals((Boolean) false, a.get(0));
+
+    // Do not test undefined behavior without assertions
+    if (!assertionsEnabled) {
+      return;
+    }
+
+    try {
+      a.get(1);
+      fail("That should have failed");
+    } catch (AssertionError e) {
+ assertEquals(("Index " + 1 + " was not in the acceptable range [" + 0 + ", " + 1 + ")"),
+          e.getMessage());
+    }
+  }
+
+  public void testSingletonArrayManipulations() {
+    MutableArray<String> b = createMutableArray();
+    b.add("diva");
+    b.set(0, "avid");
+    assertEquals(1, b.size());
+    assertEquals("avid", b.get(0));
+  }
+
+ public void testThatEmptyArraysHaveHaveNoValidIndices() throws Exception {
+    {
+      // Do not test undefined behavior without assertions
+      if (!assertionsEnabled) {
+        return;
+      }
+      // Tests get().
+      MutableArray<String> b = createMutableArray();
+      Array<String> a = b;
+      // Iterate i through the various likely errors states.
+      for (int i = -1; i < 4; ++i) {
+        try {
+          a.get(i);
+          fail("Should have triggered an assertion");
+        } catch (AssertionError e) {
+          // Good
+ assertEquals(("Index " + i + " was not in the acceptable range [" + 0 + ", "
+          + 0 + ")"), e.getMessage());
+        }
+      }
+    }
+
+    {
+      // Tests set().
+      MutableArray<String> b = createMutableArray();
+      // Iterate i through the various likely errors states.
+      for (int i = -1; i < 4; ++i) {
+        try {
+          b.set(i, "random string");
+          fail("Should have triggered an assertion");
+        } catch (AssertionError e) {
+          // Good
+ assertEquals(("Index " + i + " was not in the acceptable range [" + 0 + ", "
+          + 0 + ")"), e.getMessage());
+        }
+      }
+    }
+  }
+
+  public void testThatEmptyArraysHaveSizeZero() {
+    MutableArray<String> b = createMutableArray();
+    Array<String> a = b;
+    assertEquals(0, a.size());
+  }
+
+}
=======================================
--- /dev/null
+++ /branches/2.1/bikeshed/test-super/com/google/gwt/collections/super/com/google/gwt/collections/ImmutableArrayInternalTest.java Tue Apr 27 10:51:33 2010
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2009 Google Inc.
+ *
+ * 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 com.google.gwt.collections;
+
+import com.google.gwt.junit.client.GWTTestCase;
+
+/**
+ * Tests mutable array implementation internal details.
+ */
+public class ImmutableArrayInternalTest extends GWTTestCase {
+
+  @Override
+  public String getModuleName() {
+    return null;
+  }
+
+  public void testImmutableNoCopy() {
+    // TODO: Test no copying is made
+  }
+
+}
=======================================
--- /dev/null
+++ /branches/2.1/bikeshed/test-super/com/google/gwt/collections/super/com/google/gwt/collections/MutableArrayInternalTest.java Tue Apr 27 10:51:33 2010
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2009 Google Inc.
+ *
+ * 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 com.google.gwt.collections;
+
+import static com.google.gwt.collections.CollectionFactory.createMutableArray;
+
+import com.google.gwt.junit.client.GWTTestCase;
+
+/**
+ * Tests mutable array implementation internal details.
+ */
+public class MutableArrayInternalTest extends GWTTestCase {
+
+  @Override
+  public String getModuleName() {
+    return null;
+  }
+
+  public void testSetSizeNullElems() {
+    MutableArray<String> b = createMutableArray();
+
+    b.setSize(1, "fillValue");
+    assertTrue(hasElems(b));
+
+    b.setSize(0, null);
+    assertFalse(hasElems(b));
+  }
+
+  public native boolean hasElems(MutableArray ma) /*-{
+    return !(ma.elems === undefined)
+  }-*/;
+
+}
=======================================
--- /branches/2.1/bikeshed/test/com/google/gwt/collections/ObjectArrayTest.java Tue Apr 27 07:18:58 2010
+++ /dev/null
@@ -1,298 +0,0 @@
-/*
- * Copyright 2009 Google Inc.
- *
- * 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 com.google.gwt.collections;
-
-import static com.google.gwt.collections.CollectionFactory.createMutableArray;
-
-import com.google.gwt.junit.client.GWTTestCase;
-
-/**
- * Tests mutable arrays.
- */
-public class ObjectArrayTest extends GWTTestCase {
-
-  boolean assertionsEnabled;
-
-  @Override
-  public String getModuleName() {
-    return null;
-  }
-
-  public void gwtSetUp() {
-    assertionsEnabled = this.getClass().desiredAssertionStatus();
-  }
-
-  public void testClear() {
-    {
-      // Clear an empty array
-      MutableArray<Object> b = createMutableArray();
-      b.clear();
-      assertEquals(0, b.size());
-    }
-
-    {
-      // Clear a non-empty array
-      MutableArray<Object> b = createMutableArray();
-      b.add("string");
-      b.add(false);
-      b.add(3);
-      b.clear();
-      assertEquals(0, b.size());
-    }
-  }
-
-  public void testCreateNonEmptyArray() {
-    MutableArray<String> b = createMutableArray(2, "apples");
-
-    assertEquals(2, b.size());
-    assertEquals("apples", b.get(0));
-    assertEquals("apples", b.get(1));
-  }
-
-  public void testInsertAtBeginning() {
-    final int n = 10;
-    MutableArray<Integer> b = createMutableArray();
-
-    for (int i = 0; i < n; ++i) {
-      b.insert(0, i);
-    }
-
-    for (int i = 0; i < n; ++i) {
-      assertEquals(n - i - 1, b.get(i).intValue());
-    }
-  }
-
-  public void testInsertAtEnd() {
-    final int n = 10;
-    MutableArray<Integer> b = createMutableArray();
-
-    for (int i = 0; i < n; ++i) {
-      b.insert(b.size(), i);
-    }
-
-    for (int i = 0; i < n; ++i) {
-      assertEquals(i, b.get(i).intValue());
-    }
-  }
-
-  public void testInsertInMiddle() {
-    final int n = 10;
-    MutableArray<Integer> b = createMutableArray();
-
-    // Fill the array with 0..(N-1)
-    for (int i = 0; i < n; ++i) {
-      b.insert(b.size(), i);
-    }
-
-    // Double each number by inserting.
-    for (int i = 0; i < n; ++i) {
-      b.insert(i * 2, i);
-    }
-
-    for (int i = 0, j = 0; i < 2 * n; i += 2, ++j) {
-      assertEquals(j, b.get(i).intValue());
-      assertEquals(j, b.get(i + 1).intValue());
-    }
-  }
-
-  public void testMultiElementArrayManipulations() {
-    MutableArray<String> b = createMutableArray();
-    b.add("apple");
-    b.add("banana");
-    b.add("coconut");
-    b.add(null);
-    b.add("donut");
-
-    // On mutable array, get() in order
-    assertEquals("apple", b.get(0));
-    assertEquals("banana", b.get(1));
-    assertEquals("coconut", b.get(2));
-    assertEquals(null, b.get(3));
-    assertEquals("donut", b.get(4));
-
-    // On mutable array, get() in non-sequential order
-    assertEquals("coconut", b.get(2));
-    assertEquals("apple", b.get(0));
-    assertEquals("donut", b.get(4));
-    assertEquals("banana", b.get(1));
-    assertEquals(null, b.get(3));
-
-    // Try a set() call, too
-    b.set(3, "eclair");
-    assertEquals("eclair", b.get(3));
-  }
-
-  public void testRemove() {
-    MutableArray<Integer> b = createMutableArray();
-
-    b.add(1);
-    b.add(2);
-    b.add(3);
-    b.add(4);
-    b.add(5);
-
-    // Remove from the middle to make 1,2,4,5
-    b.remove(2);
-    assertEquals(4, b.size());
-    assertEquals(1, b.get(0).intValue());
-    assertEquals(5, b.get(3).intValue());
-
-    // Remove from the beginning to make 2,4,5
-    b.remove(0);
-    assertEquals(3, b.size());
-    assertEquals(2, b.get(0).intValue());
-    assertEquals(5, b.get(2).intValue());
-
-    // Remove from the end to make 2,4
-    b.remove(b.size() - 1);
-    assertEquals(2, b.size());
-    assertEquals(2, b.get(0).intValue());
-    assertEquals(4, b.get(1).intValue());
-  }
-
-  public void testSetSize() {
-    MutableArray<String> b = createMutableArray();
-
-    b.setSize(1, "fillValue");
-    assertEquals(1, b.size());
-    assertEquals("fillValue", b.get(0));
-
-    b.setSize(2, "anotherValue");
-    assertEquals(2, b.size());
-    assertEquals("fillValue", b.get(0));
-    assertEquals("anotherValue", b.get(1));
-
-    b.setSize(1, null);
-    assertEquals(1, b.size());
-    assertEquals("fillValue", b.get(0));
-
-    b.setSize(0, null);
-    assertEquals(0, b.size());
-    assertEquals(null, b.elems);
-  }
-
-  public void testSingleElementAddAndRemove() {
-      MutableArray<String> a = createMutableArray();
-
-      a.add("foo");
-
-      assertEquals(1, a.size());
-      assertEquals("foo", a.get(0));
-
-      a.remove(0);
-
-      assertEquals(0, a.size());
-
-      a.add("bar");
-
-      assertEquals(1, a.size());
-      assertEquals("bar", a.get(0));
-  }
-
-  public void testSingleElementNull() {
-    MutableArray<String> b = createMutableArray();
-    b.add(null);
-
-    assertEquals(null, b.get(0));
-  }
-
-  public void testSingletonArrayCreationAndRetrieval() throws Exception {
-    final int c = 2112;
-    MutableArray<Integer> b = createMutableArray();
-    b.add(c);
-    assertEquals(c, b.get(0).intValue());
-
-    Array<Integer> a = b;
-    assertEquals(1, a.size());
-
-    assertEquals((Integer) 2112, a.get(0));
-  }
-
-  public void testSingletonArrayInvalidIndex() throws Exception {
-    MutableArray<Boolean> b = createMutableArray();
-    b.add(false);
-    Array<Boolean> a = b;
-
-    assertEquals((Boolean) false, a.get(0));
-
-    // Do not test undefined behavior without assertions
-    if (!assertionsEnabled) {
-      return;
-    }
-
-    try {
-      a.get(1);
-      fail("That should have failed");
-    } catch (AssertionError e) {
- assertEquals(("Index " + 1 + " was not in the acceptable range [" + 0 + ", " + 1 + ")"),
-          e.getMessage());
-    }
-  }
-
-  public void testSingletonArrayManipulations() {
-    MutableArray<String> b = createMutableArray();
-    b.add("diva");
-    b.set(0, "avid");
-    assertEquals(1, b.size());
-    assertEquals("avid", b.get(0));
-  }
-
- public void testThatEmptyArraysHaveHaveNoValidIndices() throws Exception {
-    {
-      // Do not test undefined behavior without assertions
-      if (!assertionsEnabled) {
-        return;
-      }
-      // Tests get().
-      MutableArray<String> b = createMutableArray();
-      Array<String> a = b;
-      // Iterate i through the various likely errors states.
-      for (int i = -1; i < 4; ++i) {
-        try {
-          a.get(i);
-          fail("Should have triggered an assertion");
-        } catch (AssertionError e) {
-          // Good
- assertEquals(("Index " + i + " was not in the acceptable range [" + 0 + ", "
-          + 0 + ")"), e.getMessage());
-        }
-      }
-    }
-
-    {
-      // Tests set().
-      MutableArray<String> b = createMutableArray();
-      // Iterate i through the various likely errors states.
-      for (int i = -1; i < 4; ++i) {
-        try {
-          b.set(i, "random string");
-          fail("Should have triggered an assertion");
-        } catch (AssertionError e) {
-          // Good
- assertEquals(("Index " + i + " was not in the acceptable range [" + 0 + ", "
-          + 0 + ")"), e.getMessage());
-        }
-      }
-    }
-  }
-
-  public void testThatEmptyArraysHaveSizeZero() {
-    MutableArray<String> b = createMutableArray();
-    Array<String> a = b;
-    assertEquals(0, a.size());
-  }
-
-}
=======================================
--- /branches/2.1/bikeshed/src/com/google/gwt/collections/Collections.gwt.xml Mon Apr 26 12:38:54 2010 +++ /branches/2.1/bikeshed/src/com/google/gwt/collections/Collections.gwt.xml Tue Apr 27 10:51:33 2010
@@ -16,4 +16,5 @@
 <module>
   <inherits name='com.google.gwt.user.User'/>
   <source path="" />
+  <super-source path="super"/>
 </module>
=======================================
--- /branches/2.1/bikeshed/src/com/google/gwt/collections/MutableArray.java Mon Apr 26 12:38:54 2010 +++ /branches/2.1/bikeshed/src/com/google/gwt/collections/MutableArray.java Tue Apr 27 10:51:33 2010
@@ -115,7 +115,6 @@
     Assertions.assertNotFrozen(this);
     Assertions.assertIndexInRange(index, 0, size());
     if (elems != null && elems.length >= 1) {
-      // TODO: replace with splice using JSNI
       int oldLen = elems.length;
       E[] newElems = (E[]) new Object[oldLen - 1];
       System.arraycopy(elems, 0, newElems, 0, index);
=======================================
--- /branches/2.1/bikeshed/test/com/google/gwt/collections/ClientMutableArrayTest.java Mon Apr 26 12:38:54 2010 +++ /branches/2.1/bikeshed/test/com/google/gwt/collections/ClientMutableArrayTest.java Tue Apr 27 10:51:33 2010
@@ -16,12 +16,12 @@
 package com.google.gwt.collections;

 /**
- * Re-run {...@link ObjectArrayTest} tests under GWT.
+ * Re-run {...@link MutableArrayTest} tests under GWT.
  */
-public class ClientMutableArrayTest extends ObjectArrayTest {
+public class ClientMutableArrayTest extends MutableArrayTest {
   @Override
   public String getModuleName() {
     return "com.google.gwt.collections.Collections";
   }
-
-}
+
+}
=======================================
--- /branches/2.1/bikeshed/test/com/google/gwt/collections/ImmutableArrayTest.java Mon Apr 26 12:38:54 2010 +++ /branches/2.1/bikeshed/test/com/google/gwt/collections/ImmutableArrayTest.java Tue Apr 27 10:51:33 2010
@@ -111,6 +111,7 @@
     assertTrue(ia1.elems == ia2.elems);
   }

+
   public void testModifyFrozenMutable() {
     // Do not test undefined behavior with assertions disabled
     if (!assertionsEnabled) {
=======================================
--- /branches/2.1/bikeshed/test/com/google/gwt/collections/MutableArrayBenchmarkTest.java Mon Apr 26 12:38:54 2010 +++ /branches/2.1/bikeshed/test/com/google/gwt/collections/MutableArrayBenchmarkTest.java Tue Apr 27 10:51:33 2010
@@ -20,22 +20,24 @@
 import com.google.gwt.benchmarks.client.Operator;
 import com.google.gwt.benchmarks.client.RangeField;

+import java.util.ArrayList;
+
 /**
  * Benchmarks the performance of various MutableArray methods.
  */
 public class MutableArrayBenchmarkTest extends Benchmark {

-  final IntRange elemRange = new IntRange(0, 5000, Operator.ADD, 100);
+  final IntRange elemRange = new IntRange(5, 30000, Operator.ADD, 500);

   @Override
   public String getModuleName() {
     return "com.google.gwt.collections.Collections";
   }

-  public void testAddGrowth() {
+  public void testGwtCollectionsArrayAddGrowth() {
   }

-  public void testAddGrowth(@RangeField("elemRange") Integer numElements) {
+ public void testGwtCollectionsArrayAddGrowth(@RangeField("elemRange") Integer numElements) {
     MutableArray<Integer> ma = CollectionFactory.createMutableArray();

     for (int i = 0; i < numElements; i++) {
@@ -43,10 +45,10 @@
     }
   }

-  public void testSetSizeGrowth() {
+  public void testGwtCollectionsArraySetSizeGrowth() {
   }

- public void testSetSizeGrowth(@RangeField("elemRange") Integer numElements) { + public void testGwtCollectionsArraySetSizeGrowth(@RangeField("elemRange") Integer numElements) {
     MutableArray<Integer> ma = CollectionFactory.createMutableArray();

     ma.setSize(numElements, null);
@@ -54,5 +56,40 @@
       ma.set(i, i);
     }
   }
+
+  public void testGwtCollectionsArraySetSizeInitGrowth() {
+  }
+
+  public void testGwtCollectionsArraySetSizeInitGrowth(
+      @RangeField("elemRange") Integer numElements) {
+    MutableArray<Integer> ma = CollectionFactory.createMutableArray();
+
+    ma.setSize(numElements, new Integer(0));
+    for (int i = 0; i < numElements; i++) {
+      ma.set(i, i);
+    }
+  }
+
+  public void testJavaArraySetGrowth() {
+  }
+
+ public void testJavaArraySetGrowth(@RangeField("elemRange") Integer numElements) {
+    Integer[] ia = new Integer[numElements];
+
+    for (int i = 0; i < numElements; i++) {
+      ia[i] = i;
+    }
+  }
+
+  public void testJreArrayListAddGrowth() {
+  }
+
+ public void testJreArrayListAddGrowth(@RangeField("elemRange") Integer numElements) {
+    ArrayList<Integer> al = new ArrayList<Integer>();
+
+    for (int i = 0; i < numElements; i++) {
+      al.add(i);
+    }
+  }

 }

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to