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