001: /*
002: * Copyright 2006 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016:
017: package com.google.gwt.emultest.java.util;
018:
019: import org.apache.commons.collections.TestArrayList;
020:
021: import java.util.ArrayList;
022: import java.util.List;
023: import java.util.ListIterator;
024:
025: /**
026: * Tests ArrayList, and, by extension AbstractList. Uses inheritance to inherit
027: * all of Apache's TestList and TestCollection.
028: */
029: public class ArrayListTest extends TestArrayList {
030:
031: private static final class ArrayListWithRemoveRange extends
032: ArrayList {
033: public void removeRange(int fromIndex, int toIndex) {
034: super .removeRange(fromIndex, toIndex);
035: }
036: }
037:
038: public ArrayListTest() {
039: }
040:
041: public void testAddWatch() {
042: ArrayList s = new ArrayList();
043: s.add("watch");
044: assertEquals(s.get(0), "watch");
045: }
046:
047: public void testListIteratorAddInSeveralPositions() {
048: ArrayList l = new ArrayList();
049: ListIterator i = l.listIterator();
050: l.add(new Integer(0));
051: for (int n = 2; n < 5; n += 2) {
052: l.add(new Integer(n));
053: }
054: i = l.listIterator();
055: i.next();
056: i.add(new Integer(1));
057: i.next();
058: i.next();
059: i.previous();
060: i.add(new Integer(3));
061: i.next();
062: i.add(new Integer(5));
063: i.add(new Integer(6));
064: for (int n = 0; n < 6; n++) {
065: assertEquals(new Integer(n), l.get(n));
066: }
067: }
068:
069: public void testListIteratorCreateInvalid() {
070: ArrayList l = new ArrayList();
071: l.add(new Integer(1));
072: l.listIterator(0);
073: try {
074: l.listIterator(1);
075: } catch (IndexOutOfBoundsException e) {
076: // expected
077: }
078: try {
079: l.listIterator(-1);
080: } catch (IndexOutOfBoundsException e) {
081: // expected
082: }
083: }
084:
085: public void testListIteratorHasNextHasPreviousAndIndexes() {
086: List l = new ArrayList();
087: ListIterator i = l.listIterator();
088: assertFalse(i.hasNext());
089: assertFalse(i.hasPrevious());
090: i.add(new Integer(1));
091: assertEquals(1, i.nextIndex());
092: assertEquals(0, i.previousIndex());
093: i = l.listIterator();
094: assertEquals(0, i.nextIndex());
095: assertEquals(-1, i.previousIndex());
096: assertTrue(i.hasNext());
097: assertFalse(i.hasPrevious());
098: i.next();
099: assertEquals(1, i.nextIndex());
100: assertEquals(0, i.previousIndex());
101: assertFalse(i.hasNext());
102: assertTrue(i.hasPrevious());
103: }
104:
105: public void testListIteratorSetInSeveralPositions() {
106: ArrayList l = new ArrayList();
107: for (int n = 0; n < 5; n += 2) {
108: l.add(new Integer(n));
109: }
110: l.listIterator();
111: for (int n = 0; n < 3; n++) {
112: l.set(n, new Integer(n));
113: }
114: for (int n = 0; n < 3; n++) {
115: assertEquals(new Integer(n), l.get(n));
116: }
117: }
118:
119: public void testRemoveRange() {
120: ArrayListWithRemoveRange l = new ArrayListWithRemoveRange();
121: for (int i = 0; i < 10; i++) {
122: l.add(new Integer(i));
123: }
124: try {
125: l.removeRange(-1, 1);
126: fail();
127: } catch (IndexOutOfBoundsException expected) {
128: }
129:
130: try {
131: l.removeRange(2, 1);
132: fail();
133: } catch (IndexOutOfBoundsException expected) {
134: }
135:
136: try {
137: l.removeRange(2, 11);
138: fail();
139: } catch (IndexOutOfBoundsException expected) {
140: }
141:
142: l.removeRange(3, 5);
143: assertEquals(8, l.size());
144: for (int i = 0; i < 3; i++) {
145: Integer elem = (Integer) l.get(i);
146: assertEquals(i, elem.intValue());
147: }
148: for (int i = 3; i < 8; i++) {
149: Integer elem = (Integer) l.get(i);
150: assertEquals(i + 2, elem.intValue());
151: }
152: }
153:
154: public void testToArray() {
155: ArrayList l = new ArrayList();
156: for (int i = 0; i < 10; i++) {
157: l.add(new Integer(i));
158: }
159:
160: {
161: Object[] objArray = l.toArray();
162: assertEquals(10, objArray.length);
163: for (int i = 0; i < 10; i++) {
164: Integer elem = (Integer) objArray[i];
165: assertEquals(i, elem.intValue());
166: }
167: // should not cause ArrayStore
168: objArray[0] = new Object();
169: }
170:
171: {
172: Integer[] firstArray = new Integer[13];
173: firstArray[10] = new Integer(10);
174: firstArray[11] = new Integer(11);
175: firstArray[12] = new Integer(12);
176: Integer[] intArray = (Integer[]) l.toArray(firstArray);
177: assertTrue(firstArray == intArray);
178: assertEquals(13, intArray.length);
179: for (int i = 0; i < 13; i++) {
180: if (i == 10) {
181: assertNull(intArray[i]);
182: } else {
183: Integer elem = intArray[i];
184: assertEquals(i, elem.intValue());
185: }
186: }
187: try {
188: Object[] objArray = noOptimizeFalse() ? new Object[1]
189: : intArray;
190: assertTrue(objArray instanceof Integer[]);
191: objArray[0] = new Object();
192: fail("expected ArrayStoreException");
193: } catch (ArrayStoreException e) {
194: }
195: }
196:
197: {
198: Integer[] firstArray = new Integer[0];
199: Integer[] intArray = (Integer[]) l.toArray(firstArray);
200: assertFalse(firstArray == intArray);
201: assertEquals(10, intArray.length);
202: for (int i = 0; i < 10; i++) {
203: Integer elem = intArray[i];
204: assertEquals(i, elem.intValue());
205: }
206: try {
207: Object[] objArray = noOptimizeFalse() ? new Object[1]
208: : intArray;
209: assertTrue(objArray instanceof Integer[]);
210: objArray[0] = new Object();
211: fail("expected ArrayStoreException");
212: } catch (ArrayStoreException e) {
213: }
214: }
215: }
216:
217: protected List makeEmptyList() {
218: return new ArrayList();
219: }
220:
221: private static native boolean noOptimizeFalse() /*-{
222: return false;
223: }-*/;
224: }
|