duke@1: /* ohair@554: * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as duke@1: * published by the Free Software Foundation. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: /* duke@1: * @test duke@1: * @bug 6267067 6351336 6389198 duke@1: * @summary unit test for javac List duke@1: */ duke@1: duke@1: import java.util.*; duke@1: import com.sun.tools.javac.util.List; duke@1: duke@1: public class TList { duke@1: public static void main(String[] args) { duke@1: new TList().run(); duke@1: } duke@1: duke@1: String[][] data = { duke@1: { }, duke@1: { "1" }, duke@1: { "1", "2" }, duke@1: { "1", "2" }, // different but equal duke@1: { "1", "2", "3", "4", "X", "X", "X", "8", "9", "10" } // contains duplicates duke@1: }; duke@1: duke@1: Map,List> examples; duke@1: duke@1: void run() { duke@1: examples = new LinkedHashMap,List>(); duke@1: for (String[] values: data) duke@1: examples.put(Arrays.asList(values), createList(values)); duke@1: duke@1: // 6351336: com.sun.tools.javac.util.List shouldn't extend java.util.AbstractList duke@1: test_AbstractList(); duke@1: duke@1: // general unit tests for java.util.List methods, including... duke@1: // 6389198: com.sun.tools.javac.util.List.equals() violates java.util.List.equals() contract duke@1: test_add_E(); duke@1: test_add_int_E(); duke@1: test_addAll_Collection(); duke@1: test_addAll_int_Collection(); duke@1: test_clear(); duke@1: test_contains_Object(); duke@1: test_contains_All(); duke@1: test_equals_Object(); duke@1: test_get_int(); duke@1: test_hashCode(); duke@1: test_indexOf_Object(); duke@1: test_isEmpty(); duke@1: test_iterator(); duke@1: test_lastIndexOf_Object(); duke@1: test_listIterator(); duke@1: test_listIterator_int(); duke@1: test_remove_int(); duke@1: test_remove_Object(); duke@1: test_removeAll_Collection(); duke@1: test_retainAll_Collection(); duke@1: test_set_int_E(); duke@1: test_size(); duke@1: test_subList_int_int(); duke@1: test_toArray(); duke@1: test_toArray_TArray(); duke@1: duke@1: // tests for additional methods duke@1: test_prependList_List(); duke@1: test_reverse(); duke@1: } duke@1: duke@1: // 6351336 duke@1: void test_AbstractList() { duke@1: System.err.println("test AbstractList"); duke@1: if (AbstractList.class.isAssignableFrom(List.class)) duke@1: throw new AssertionError(); duke@1: } duke@1: duke@1: void test_add_E() { duke@1: System.err.println("test add(E)"); duke@1: for (List l: examples.values()) { duke@1: try { duke@1: l.add("test"); duke@1: throw new AssertionError(); duke@1: } catch (UnsupportedOperationException ex) { duke@1: } duke@1: } duke@1: } duke@1: duke@1: void test_add_int_E() { duke@1: System.err.println("test add(int,E)"); duke@1: for (List l: examples.values()) { duke@1: try { duke@1: l.add(0, "test"); duke@1: throw new AssertionError(); duke@1: } catch (UnsupportedOperationException ex) { duke@1: } duke@1: } duke@1: } duke@1: duke@1: void test_addAll_Collection() { duke@1: System.err.println("test addAll(Collection)"); duke@1: for (List l: examples.values()) { duke@1: int l_size = l.size(); duke@1: for (java.util.List arg: examples.keySet()) { duke@1: try { duke@1: boolean modified = l.addAll(arg); duke@1: if (modified) duke@1: throw new AssertionError(); duke@1: } catch (UnsupportedOperationException e) { duke@1: } duke@1: if (l.size() != l_size) duke@1: throw new AssertionError(); duke@1: } duke@1: } duke@1: } duke@1: duke@1: void test_addAll_int_Collection() { duke@1: System.err.println("test addAll(int,Collection)"); duke@1: for (List l: examples.values()) { duke@1: int l_size = l.size(); duke@1: for (java.util.List arg: examples.keySet()) { duke@1: try { duke@1: boolean modified = l.addAll(0, arg); duke@1: if (modified) duke@1: throw new AssertionError(); duke@1: } catch (UnsupportedOperationException e) { duke@1: } duke@1: if (l.size() != l_size) duke@1: throw new AssertionError(); duke@1: } duke@1: } duke@1: } duke@1: duke@1: void test_clear() { duke@1: System.err.println("test clear()"); duke@1: for (List l: examples.values()) { duke@1: int l_size = l.size(); duke@1: try { duke@1: l.clear(); duke@1: if (l_size > 0) duke@1: throw new AssertionError(); duke@1: } catch (UnsupportedOperationException e) { duke@1: } duke@1: if (l.size() != l_size) duke@1: throw new AssertionError(); duke@1: } duke@1: } duke@1: duke@1: void test_contains_Object() { duke@1: System.err.println("test contains(Object)"); duke@1: for (Map.Entry,List> e: examples.entrySet()) { duke@1: java.util.List ref = e.getKey(); duke@1: List l = e.getValue(); duke@1: boolean expect = ref.contains("1"); duke@1: boolean found = l.contains("1"); duke@1: if (expect != found) duke@1: throw new AssertionError(); duke@1: } duke@1: } duke@1: duke@1: void test_contains_All() { duke@1: System.err.println("test containsAll()"); duke@1: for (Map.Entry,List> e: examples.entrySet()) { duke@1: java.util.List ref = e.getKey(); duke@1: List l = e.getValue(); duke@1: for (java.util.List arg: examples.keySet()) { duke@1: boolean expect = ref.containsAll(arg); duke@1: boolean found = l.containsAll(arg); duke@1: if (expect != found) duke@1: throw new AssertionError(); duke@1: } duke@1: } duke@1: } duke@1: duke@1: // 6389198 duke@1: void test_equals_Object() { duke@1: System.err.println("test equals(Object)"); duke@1: for (Map.Entry,List> e: examples.entrySet()) { duke@1: java.util.List ref = e.getKey(); duke@1: List l = e.getValue(); duke@1: for (java.util.List arg: examples.keySet()) { duke@1: boolean expect = ref.equals(arg); duke@1: boolean found = l.equals(arg); duke@1: if (expect != found) { duke@1: System.err.println("ref: " + ref); duke@1: System.err.println("l: " + l); duke@1: System.err.println("arg: " + arg); duke@1: System.err.println("expect: " + expect + ", found: " + found); duke@1: throw new AssertionError(); duke@1: } duke@1: } duke@1: } duke@1: } duke@1: duke@1: void test_get_int() { duke@1: System.err.println("test get(int)"); duke@1: for (Map.Entry,List> e: examples.entrySet()) { duke@1: java.util.List ref = e.getKey(); duke@1: List l = e.getValue(); duke@1: for (int i = -1; i <= ref.size(); i++) { duke@1: boolean expectException = i < 0 || i >= ref.size(); duke@1: String expectValue = (expectException ? null : ref.get(i)); duke@1: try { duke@1: String foundValue = l.get(i); duke@1: if (expectException || !equal(expectValue, foundValue)) duke@1: throw new AssertionError(); duke@1: } catch (IndexOutOfBoundsException ex) { duke@1: if (!expectException) duke@1: throw new AssertionError(); duke@1: } duke@1: } duke@1: } duke@1: } duke@1: duke@1: void test_hashCode() { duke@1: System.err.println("test hashCode()"); duke@1: for (Map.Entry,List> e: examples.entrySet()) { duke@1: java.util.List ref = e.getKey(); duke@1: List l = e.getValue(); duke@1: long expect = ref.hashCode(); duke@1: long found = l.hashCode(); duke@1: if (expect != found) { duke@1: System.err.println("ref: " + ref); duke@1: System.err.println("l: " + l); duke@1: System.err.println("expect: " + expect); duke@1: System.err.println("found: " + found); duke@1: throw new AssertionError(); duke@1: } duke@1: } duke@1: } duke@1: duke@1: void test_indexOf_Object() { duke@1: System.err.println("test indexOf(Object)"); duke@1: for (Map.Entry,List> e: examples.entrySet()) { duke@1: java.util.List ref = e.getKey(); duke@1: List l = e.getValue(); duke@1: for (int i = -1; i < ref.size(); i++) { duke@1: String arg = (i == -1 ? "NOT IN LIST" : ref.get(i)); duke@1: int expect = ref.indexOf(arg); duke@1: int found = l.indexOf(arg); duke@1: if (expect != found) duke@1: throw new AssertionError(); duke@1: } duke@1: } duke@1: } duke@1: duke@1: void test_isEmpty() { duke@1: System.err.println("test isEmpty()"); duke@1: for (Map.Entry,List> e: examples.entrySet()) { duke@1: java.util.List ref = e.getKey(); duke@1: List l = e.getValue(); duke@1: boolean expect = ref.isEmpty(); duke@1: boolean found = l.isEmpty(); duke@1: if (expect != found) duke@1: throw new AssertionError(); duke@1: } duke@1: } duke@1: duke@1: void test_iterator() { duke@1: System.err.println("test iterator()"); duke@1: for (Map.Entry,List> e: examples.entrySet()) { duke@1: java.util.List ref = e.getKey(); duke@1: List l = e.getValue(); duke@1: if (!equal(l.iterator(), ref.iterator())) duke@1: throw new AssertionError(); duke@1: } duke@1: } duke@1: duke@1: void test_lastIndexOf_Object() { duke@1: System.err.println("test lastIndexOf(Object)"); duke@1: for (Map.Entry,List> e: examples.entrySet()) { duke@1: java.util.List ref = e.getKey(); duke@1: List l = e.getValue(); duke@1: for (int i = -1; i < ref.size(); i++) { duke@1: String arg = (i == -1 ? "NOT IN LIST" : ref.get(i)); duke@1: int expect = ref.lastIndexOf(arg); duke@1: int found = l.lastIndexOf(arg); duke@1: if (expect != found) duke@1: throw new AssertionError(); duke@1: } duke@1: } duke@1: } duke@1: duke@1: void test_listIterator() { duke@1: System.err.println("test listIterator()"); duke@1: for (Map.Entry,List> e: examples.entrySet()) { duke@1: java.util.List ref = e.getKey(); duke@1: List l = e.getValue(); duke@1: if (!equal(l.listIterator(), ref.listIterator())) duke@1: throw new AssertionError(); duke@1: } duke@1: } duke@1: duke@1: void test_listIterator_int() { duke@1: System.err.println("test listIterator(int)"); duke@1: for (Map.Entry,List> e: examples.entrySet()) { duke@1: java.util.List ref = e.getKey(); duke@1: List l = e.getValue(); duke@1: for (int i = 0; i < ref.size(); i++) { duke@1: if (!equal(l.listIterator(i), ref.listIterator(i))) duke@1: throw new AssertionError(); duke@1: } duke@1: } duke@1: } duke@1: duke@1: void test_remove_int() { duke@1: System.err.println("test remove(int)"); duke@1: for (List l: examples.values()) { duke@1: try { duke@1: l.remove(0); duke@1: throw new AssertionError(); duke@1: } catch (UnsupportedOperationException ex) { duke@1: } duke@1: } duke@1: } duke@1: duke@1: void test_remove_Object() { duke@1: System.err.println("test remove(Object)"); duke@1: for (List l: examples.values()) { duke@1: boolean hasX = l.contains("X"); duke@1: try { duke@1: l.remove("X"); duke@1: if (hasX) duke@1: throw new AssertionError(); duke@1: } catch (UnsupportedOperationException ex) { duke@1: } duke@1: } duke@1: } duke@1: duke@1: void test_removeAll_Collection() { duke@1: System.err.println("test removeAll(Collection)"); duke@1: for (List l: examples.values()) { duke@1: int l_size = l.size(); duke@1: for (java.util.List arg: examples.keySet()) { duke@1: try { duke@1: boolean modified = l.removeAll(arg); duke@1: if (modified) duke@1: throw new AssertionError(); duke@1: } catch (UnsupportedOperationException e) { duke@1: } duke@1: if (l.size() != l_size) duke@1: throw new AssertionError(); duke@1: } duke@1: } duke@1: } duke@1: duke@1: void test_retainAll_Collection() { duke@1: System.err.println("test retainAll(Collection)"); duke@1: for (List l: examples.values()) { duke@1: int l_size = l.size(); duke@1: for (java.util.List arg: examples.keySet()) { duke@1: try { duke@1: boolean modified = l.retainAll(arg); duke@1: if (modified) duke@1: throw new AssertionError(); duke@1: } catch (UnsupportedOperationException e) { duke@1: } duke@1: if (l.size() != l_size) duke@1: throw new AssertionError(); duke@1: } duke@1: } duke@1: } duke@1: duke@1: void test_set_int_E() { duke@1: System.err.println("test set(int,E)"); duke@1: for (List l: examples.values()) { duke@1: try { duke@1: l.set(0, "X"); duke@1: throw new AssertionError(); duke@1: } catch (UnsupportedOperationException ex) { duke@1: } duke@1: } duke@1: } duke@1: duke@1: void test_size() { duke@1: System.err.println("test size()"); duke@1: for (Map.Entry,List> e: examples.entrySet()) { duke@1: java.util.List ref = e.getKey(); duke@1: List l = e.getValue(); duke@1: int expect = ref.size(); duke@1: int found = l.size(); duke@1: if (expect != found) duke@1: throw new AssertionError(); duke@1: } duke@1: } duke@1: duke@1: void test_subList_int_int() { duke@1: System.err.println("test subList(int,int)"); duke@1: for (Map.Entry,List> e: examples.entrySet()) { duke@1: java.util.List ref = e.getKey(); duke@1: List l = e.getValue(); duke@1: for (int lwb = 0; lwb < ref.size(); lwb++) { duke@1: for (int upb = lwb; upb <= ref.size(); upb++) { duke@1: if (!equal(l.subList(lwb, upb), ref.subList(lwb,upb))) duke@1: throw new AssertionError(); duke@1: } duke@1: } duke@1: } duke@1: } duke@1: duke@1: void test_toArray() { duke@1: System.err.println("test toArray()"); duke@1: for (Map.Entry,List> e: examples.entrySet()) { duke@1: java.util.List ref = e.getKey(); duke@1: List l = e.getValue(); duke@1: if (!equal(l.toArray(), ref.toArray())) duke@1: throw new AssertionError(); duke@1: } duke@1: } duke@1: duke@1: void test_toArray_TArray() { duke@1: System.err.println("test toArray(E[])"); duke@1: for (Map.Entry,List> e: examples.entrySet()) { duke@1: java.util.List ref = e.getKey(); duke@1: List l = e.getValue(); duke@1: if (!equal(l.toArray(new String[0]), ref.toArray(new String[0]))) duke@1: throw new AssertionError(); duke@1: } duke@1: } duke@1: duke@1: void test_prependList_List() { duke@1: System.err.println("test prependList(List)"); duke@1: for (Map.Entry,List> e: examples.entrySet()) { duke@1: java.util.List ref = e.getKey(); duke@1: List l = e.getValue(); duke@1: for (Map.Entry, List> arg_e: examples.entrySet()) { duke@1: java.util.List arg_ref = arg_e.getKey(); duke@1: List arg = arg_e.getValue(); duke@1: java.util.List expect = join(arg, ref); duke@1: List found = l.prependList(arg); duke@1: // verify results, and that original and arg lists are unchanged duke@1: if (!equal(expect, found)) { duke@1: System.err.println("ref: " + ref); duke@1: System.err.println("l: " + l); duke@1: System.err.println("arg: " + arg); duke@1: System.err.println("expect: " + expect); duke@1: System.err.println("found: " + found); duke@1: throw new AssertionError(); duke@1: } duke@1: if (!equal(l, ref)) duke@1: throw new AssertionError(); duke@1: if (!equal(arg, arg_ref)) duke@1: throw new AssertionError(); duke@1: } duke@1: } duke@1: } duke@1: duke@1: void test_reverse() { duke@1: System.err.println("test reverse()"); duke@1: for (Map.Entry,List> e: examples.entrySet()) { duke@1: java.util.List ref = e.getKey(); duke@1: List l = e.getValue(); duke@1: java.util.List expect = reverse(ref); duke@1: List found = l.reverse(); duke@1: if (l.size() < 2 && found != l) // reverse of empty or singleton list is itself duke@1: throw new AssertionError(); duke@1: if (!equal(l, ref)) // orginal should be unchanged duke@1: throw new AssertionError(); duke@1: if (!equal(expect, found)) duke@1: throw new AssertionError(); duke@1: } duke@1: } duke@1: duke@1: static com.sun.tools.javac.util.List createList(List d) { duke@1: com.sun.tools.javac.util.List l = com.sun.tools.javac.util.List.nil(); duke@1: for (ListIterator iter = d.listIterator(d.size()); iter.hasPrevious(); ) duke@1: l = l.prepend(iter.previous()); duke@1: return l; duke@1: } duke@1: duke@1: static com.sun.tools.javac.util.List createList(T... d) { duke@1: com.sun.tools.javac.util.List l = com.sun.tools.javac.util.List.nil(); duke@1: for (int i = d.length - 1; i >= 0; i--) duke@1: l = l.prepend(d[i]); duke@1: return l; duke@1: } duke@1: duke@1: static boolean equal(T t1, T t2) { duke@1: return (t1 == null ? t2 == null : t1.equals(t2)); duke@1: } duke@1: duke@1: static boolean equal(Iterator iter1, Iterator iter2) { duke@1: if (iter1 == null || iter2 == null) duke@1: return (iter1 == iter2); duke@1: duke@1: while (iter1.hasNext() && iter2.hasNext()) { duke@1: if (!equal(iter1.next(), iter2.next())) duke@1: return false; duke@1: } duke@1: duke@1: return (!iter1.hasNext() && !iter2.hasNext()); duke@1: } duke@1: duke@1: static boolean equal(ListIterator iter1, ListIterator iter2) { duke@1: if (iter1 == null || iter2 == null) duke@1: return (iter1 == iter2); duke@1: duke@1: if (iter1.previousIndex() != iter2.previousIndex()) duke@1: return false; duke@1: duke@1: while (iter1.hasPrevious() && iter2.hasPrevious()) { duke@1: iter1.previous(); duke@1: iter2.previous(); duke@1: } duke@1: duke@1: return equal((Iterator) iter1, (Iterator) iter2); duke@1: } duke@1: duke@1: static boolean equal(T[] a1, T[] a2) { duke@1: if (a1 == null || a2 == null) duke@1: return (a1 == a2); duke@1: duke@1: if (a1.length != a2.length) duke@1: return false; duke@1: duke@1: for (int i = 0; i < a1.length; i++) { duke@1: if (!equal(a1[i], a2[i])) duke@1: return false; duke@1: } duke@1: duke@1: return true; duke@1: } duke@1: duke@1: static java.util.List join(java.util.List... lists) { duke@1: java.util.List r = new ArrayList(); duke@1: for (java.util.List l: lists) duke@1: r.addAll(l); duke@1: return r; duke@1: } duke@1: duke@1: static java.util.List reverse(java.util.List l) { duke@1: java.util.List r = new ArrayList(l.size()); duke@1: for (T t: l) duke@1: r.add(0, t); duke@1: return r; duke@1: } duke@1: }