001: /*
002: * Copyright 2007 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: package com.google.gwt.emultest.java.util;
017:
018: import java.util.ArrayList;
019: import java.util.Arrays;
020: import java.util.Collections;
021: import java.util.Comparator;
022: import java.util.List;
023:
024: /**
025: * TODO: document me.
026: */
027: public class CollectionsTest extends EmulTestBase {
028:
029: public static List createSortedList() {
030: ArrayList l = new ArrayList();
031: l.add("a");
032: l.add("b");
033: l.add("c");
034: return l;
035: }
036:
037: public static List createRandomList() {
038: ArrayList l = new ArrayList();
039: l.add(new Integer(5));
040: l.add(new Integer(2));
041: l.add(new Integer(3));
042: l.add(new Integer(1));
043: l.add(new Integer(4));
044: return l;
045: }
046:
047: /**
048: * Test Collections.binarySearch(List, Object).
049: *
050: * Verify the following cases:
051: * empty List
052: * odd numbers of elements
053: * even numbers of elements
054: * not found value larger than all elements
055: * not found value smaller than all elements
056: */
057: public void testBinarySearchObject() {
058: List a1 = new ArrayList();
059: int ret = Collections.binarySearch(a1, "");
060: assertEquals(-1, ret);
061: List a2 = new ArrayList(Arrays.asList(new String[] { "a", "g",
062: "y" }));
063: ret = Collections.binarySearch(a2, "c");
064: assertEquals(-2, ret);
065: ret = Collections.binarySearch(a2, "y");
066: assertEquals(2, ret);
067: List a3 = new ArrayList(Arrays.asList(new String[] { "b", "c",
068: "x", "y" }));
069: ret = Collections.binarySearch(a3, "z");
070: assertEquals(-5, ret);
071: ret = Collections.binarySearch(a3, "a");
072: assertEquals(-1, ret);
073: ret = Collections.binarySearch(a3, "b");
074: assertEquals(0, ret);
075: }
076:
077: /**
078: * Test Collections.binarySearch(List, Object, Comparator).
079: *
080: * Verify the following cases:
081: * empty List
082: * odd numbers of elements
083: * even numbers of elements
084: * not found value larger than all elements
085: * not found value smaller than all elements
086: * null Comparator uses natural ordering
087: */
088: public void testBinarySearchObjectComparator() {
089: Comparator inverseSort = new Comparator() {
090: public int compare(Object o1, Object o2) {
091: return ((Comparable) o2).compareTo(o1);
092: }
093: };
094: List a1 = new ArrayList();
095: int ret = Collections.binarySearch(a1, "", inverseSort);
096: assertEquals(-1, ret);
097: List a2 = new ArrayList(Arrays.asList(new String[] { "y", "g",
098: "a" }));
099: ret = Collections.binarySearch(a2, "c", inverseSort);
100: assertEquals(-3, ret);
101: ret = Collections.binarySearch(a2, "a", inverseSort);
102: assertEquals(2, ret);
103: List a3 = new ArrayList(Arrays.asList(new String[] { "y", "x",
104: "c", "b" }));
105: ret = Collections.binarySearch(a3, "a", inverseSort);
106: assertEquals(-5, ret);
107: ret = Collections.binarySearch(a3, "z", inverseSort);
108: assertEquals(-1, ret);
109: ret = Collections.binarySearch(a3, "y", inverseSort);
110: assertEquals(0, ret);
111:
112: List a4 = new ArrayList(Arrays.asList(new String[] { "a", "b",
113: "c", "d", "e" }));
114: ret = Collections.binarySearch(a4, "d", null); // should not NPE
115: assertEquals(3, ret);
116: }
117:
118: public void testReverse() {
119: List a = createSortedList();
120: Collections.reverse(a);
121: Object[] x = { "c", "b", "a" };
122: assertEquals(x, a);
123:
124: List b = createRandomList();
125: Collections.reverse(b);
126: Collections.reverse(b);
127: assertEquals(b, createRandomList());
128: }
129:
130: public void testSort() {
131: List a = createSortedList();
132: Collections.reverse(a);
133: Collections.sort(a);
134: assertEquals(createSortedList(), a);
135: }
136:
137: public void testSortWithComparator() {
138: Comparator x = new Comparator() {
139: public int compare(Object o1, Object o2) {
140: String s1 = (String) o1;
141: String s2 = (String) o2;
142: // sort into reverse order
143: return s2.compareTo(s1);
144: }
145: };
146: List a = createSortedList();
147: Collections.sort(a, x);
148: Object[] expected = { "c", "b", "a" };
149: assertEquals(expected, a);
150: }
151:
152: public void testToArray() {
153: List<Integer> testList = createRandomList();
154: Integer[] testArray = new Integer[testList.size()];
155: testList.toArray(testArray);
156: for (int i = 0; i < testList.size(); ++i) {
157: Integer val = testList.get(i);
158: assertEquals(val, testArray[i]);
159: }
160: }
161:
162: }
|