Path: blob/master/test/jdk/java/util/Collections/CheckedListBash.java
41149 views
/*1* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 490406726* @summary Unit test for Collections.checkedList27* @author Josh Bloch28* @key randomness29*/3031import java.util.ArrayList;32import java.util.Arrays;33import java.util.Collections;34import java.util.Iterator;35import java.util.List;36import java.util.ListIterator;37import java.util.Random;3839public class CheckedListBash {40static Random rnd = new Random();4142public static void main(String[] args) {43int numItr = 100;44int listSize = 100;4546for (int i=0; i<numItr; i++) {47List s1 = newList();48AddRandoms(s1, listSize);4950List s2 = newList();51AddRandoms(s2, listSize);5253List intersection = clone(s1); intersection.retainAll(s2);54List diff1 = clone(s1); diff1.removeAll(s2);55List diff2 = clone(s2); diff2.removeAll(s1);56List union = clone(s1); union.addAll(s2);5758if (diff1.removeAll(diff2))59fail("List algebra identity 2 failed");60if (diff1.removeAll(intersection))61fail("List algebra identity 3 failed");62if (diff2.removeAll(diff1))63fail("List algebra identity 4 failed");64if (diff2.removeAll(intersection))65fail("List algebra identity 5 failed");66if (intersection.removeAll(diff1))67fail("List algebra identity 6 failed");68if (intersection.removeAll(diff1))69fail("List algebra identity 7 failed");7071intersection.addAll(diff1); intersection.addAll(diff2);72if (!(intersection.containsAll(union) &&73union.containsAll(intersection)))74fail("List algebra identity 1 failed");7576Iterator e = union.iterator();77while (e.hasNext())78intersection.remove(e.next());79if (!intersection.isEmpty())80fail("Copy nonempty after deleting all elements.");8182e = union.iterator();83while (e.hasNext()) {84Object o = e.next();85if (!union.contains(o))86fail("List doesn't contain one of its elements.");87e.remove();88}89if (!union.isEmpty())90fail("List nonempty after deleting all elements.");9192s1.clear();93if (s1.size() != 0)94fail("Clear didn't reduce size to zero.");9596s1.addAll(0, s2);97if (!(s1.equals(s2) && s2.equals(s1)))98fail("addAll(int, Collection) doesn't work.");99// Reverse List100for (int j=0, n=s1.size(); j<n; j++)101s1.set(j, s1.set(n-j-1, s1.get(j)));102// Reverse it again103for (int j=0, n=s1.size(); j<n; j++)104s1.set(j, s1.set(n-j-1, s1.get(j)));105if (!(s1.equals(s2) && s2.equals(s1)))106fail("set(int, Object) doesn't work");107}108109List s = newList();110for (int i=0; i<listSize; i++)111s.add(new Integer(i));112if (s.size() != listSize)113fail("Size of [0..n-1] != n");114115List even = clone(s);116Iterator it = even.iterator();117while (it.hasNext())118if (((Integer)it.next()).intValue() % 2 == 1)119it.remove();120it = even.iterator();121while (it.hasNext())122if (((Integer)it.next()).intValue() % 2 == 1)123fail("Failed to remove all odd nubmers.");124125List odd = clone(s);126for (int i=0; i<(listSize/2); i++)127odd.remove(i);128for (int i=0; i<(listSize/2); i++)129if (((Integer)odd.get(i)).intValue() % 2 != 1)130fail("Failed to remove all even nubmers.");131132List all = clone(odd);133for (int i=0; i<(listSize/2); i++)134all.add(2*i, even.get(i));135if (!all.equals(s))136fail("Failed to reconstruct ints from odds and evens.");137138all = clone(odd);139ListIterator itAll = all.listIterator(all.size());140ListIterator itEven = even.listIterator(even.size());141while (itEven.hasPrevious()) {142itAll.previous();143itAll.add(itEven.previous());144itAll.previous(); // ???145}146itAll = all.listIterator();147while (itAll.hasNext()) {148Integer i = (Integer)itAll.next();149itAll.set(new Integer(i.intValue()));150}151itAll = all.listIterator();152it = s.iterator();153while (it.hasNext())154if (it.next()==itAll.next())155fail("Iterator.set failed to change value.");156if (!all.equals(s))157fail("Failed to reconstruct ints with ListIterator.");158159it = all.listIterator();160int i=0;161while (it.hasNext()) {162Object o = it.next();163if (all.indexOf(o) != all.lastIndexOf(o))164fail("Apparent duplicate detected.");165if (all.subList(i, all.size()).indexOf(o) != 0 ||166all.subList(i+1, all.size()).indexOf(o) != -1)167fail("subList/indexOf is screwy.");168if (all.subList(0,i+1).lastIndexOf(o) != i)169fail("subList/lastIndexOf is screwy.");170i++;171}172173List l = newList();174AddRandoms(l, listSize);175Integer[] ia = (Integer[]) l.toArray(new Integer[0]);176if (!l.equals(Arrays.asList(ia)))177fail("toArray(Object[]) is hosed (1)");178ia = new Integer[listSize];179Integer[] ib = (Integer[]) l.toArray(ia);180if (ia != ib || !l.equals(Arrays.asList(ia)))181fail("toArray(Object[]) is hosed (2)");182ia = new Integer[listSize+1];183ia[listSize] = new Integer(69);184ib = (Integer[]) l.toArray(ia);185if (ia != ib || ia[listSize] != null186|| !l.equals(Arrays.asList(ia).subList(0, listSize)))187fail("toArray(Object[]) is hosed (3)");188189}190191// Done inefficiently so as to exercise toArray192static List clone(List s) {193List a = Arrays.asList(s.toArray());194if (s.hashCode() != a.hashCode())195fail("Incorrect hashCode computation.");196197List clone = newList();198clone.addAll(a);199if (!s.equals(clone))200fail("List not equal to copy.");201if (!s.containsAll(clone))202fail("List does not contain copy.");203if (!clone.containsAll(s))204fail("Copy does not contain list.");205206return clone;207}208209static List newList() {210List s = Collections.checkedList(new ArrayList(), Integer.class);211if (!s.isEmpty())212fail("New instance non empty.");213return s;214}215216static void AddRandoms(List s, int n) {217for (int i = 0; i < n; i++) {218Integer e = rnd.nextInt(n);219220int preSize = s.size();221if (!s.add(e))222fail("Add failed.");223int postSize = s.size();224if (postSize - preSize != 1)225fail("Add didn't increase size by 1.");226}227}228229static void fail(String s) {230throw new RuntimeException(s);231}232}233234235