Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Collections/CheckedListBash.java
38812 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.*;3233public class CheckedListBash {34static Random rnd = new Random();3536public static void main(String[] args) {37int numItr = 100;38int listSize = 100;3940for (int i=0; i<numItr; i++) {41List s1 = newList();42AddRandoms(s1, listSize);4344List s2 = newList();45AddRandoms(s2, listSize);4647List intersection = clone(s1); intersection.retainAll(s2);48List diff1 = clone(s1); diff1.removeAll(s2);49List diff2 = clone(s2); diff2.removeAll(s1);50List union = clone(s1); union.addAll(s2);5152if (diff1.removeAll(diff2))53fail("List algebra identity 2 failed");54if (diff1.removeAll(intersection))55fail("List algebra identity 3 failed");56if (diff2.removeAll(diff1))57fail("List algebra identity 4 failed");58if (diff2.removeAll(intersection))59fail("List algebra identity 5 failed");60if (intersection.removeAll(diff1))61fail("List algebra identity 6 failed");62if (intersection.removeAll(diff1))63fail("List algebra identity 7 failed");6465intersection.addAll(diff1); intersection.addAll(diff2);66if (!(intersection.containsAll(union) &&67union.containsAll(intersection)))68fail("List algebra identity 1 failed");6970Iterator e = union.iterator();71while (e.hasNext())72intersection.remove(e.next());73if (!intersection.isEmpty())74fail("Copy nonempty after deleting all elements.");7576e = union.iterator();77while (e.hasNext()) {78Object o = e.next();79if (!union.contains(o))80fail("List doesn't contain one of its elements.");81e.remove();82}83if (!union.isEmpty())84fail("List nonempty after deleting all elements.");8586s1.clear();87if (s1.size() != 0)88fail("Clear didn't reduce size to zero.");8990s1.addAll(0, s2);91if (!(s1.equals(s2) && s2.equals(s1)))92fail("addAll(int, Collection) doesn't work.");93// Reverse List94for (int j=0, n=s1.size(); j<n; j++)95s1.set(j, s1.set(n-j-1, s1.get(j)));96// Reverse it again97for (int j=0, n=s1.size(); j<n; j++)98s1.set(j, s1.set(n-j-1, s1.get(j)));99if (!(s1.equals(s2) && s2.equals(s1)))100fail("set(int, Object) doesn't work");101}102103List s = newList();104for (int i=0; i<listSize; i++)105s.add(new Integer(i));106if (s.size() != listSize)107fail("Size of [0..n-1] != n");108109List even = clone(s);110Iterator it = even.iterator();111while(it.hasNext())112if(((Integer)it.next()).intValue() % 2 == 1)113it.remove();114it = even.iterator();115while(it.hasNext())116if(((Integer)it.next()).intValue() % 2 == 1)117fail("Failed to remove all odd nubmers.");118119List odd = clone(s);120for (int i=0; i<(listSize/2); i++)121odd.remove(i);122for (int i=0; i<(listSize/2); i++)123if(((Integer)odd.get(i)).intValue() % 2 != 1)124fail("Failed to remove all even nubmers.");125126List all = clone(odd);127for (int i=0; i<(listSize/2); i++)128all.add(2*i, even.get(i));129if (!all.equals(s))130fail("Failed to reconstruct ints from odds and evens.");131132all = clone(odd);133ListIterator itAll = all.listIterator(all.size());134ListIterator itEven = even.listIterator(even.size());135while (itEven.hasPrevious()) {136itAll.previous();137itAll.add(itEven.previous());138itAll.previous(); // ???139}140itAll = all.listIterator();141while (itAll.hasNext()) {142Integer i = (Integer)itAll.next();143itAll.set(new Integer(i.intValue()));144}145itAll = all.listIterator();146it = s.iterator();147while(it.hasNext())148if(it.next()==itAll.next())149fail("Iterator.set failed to change value.");150if (!all.equals(s))151fail("Failed to reconstruct ints with ListIterator.");152153it = all.listIterator();154int i=0;155while (it.hasNext()) {156Object o = it.next();157if (all.indexOf(o) != all.lastIndexOf(o))158fail("Apparent duplicate detected.");159if (all.subList(i, all.size()).indexOf(o) != 0 ||160all.subList(i+1, all.size()).indexOf(o) != -1)161fail("subList/indexOf is screwy.");162if (all.subList(0,i+1).lastIndexOf(o) != i)163fail("subList/lastIndexOf is screwy.");164i++;165}166167List l = newList();168AddRandoms(l, listSize);169Integer[] ia = (Integer[]) l.toArray(new Integer[0]);170if (!l.equals(Arrays.asList(ia)))171fail("toArray(Object[]) is hosed (1)");172ia = new Integer[listSize];173Integer[] ib = (Integer[]) l.toArray(ia);174if (ia != ib || !l.equals(Arrays.asList(ia)))175fail("toArray(Object[]) is hosed (2)");176ia = new Integer[listSize+1];177ia[listSize] = new Integer(69);178ib = (Integer[]) l.toArray(ia);179if (ia != ib || ia[listSize] != null180|| !l.equals(Arrays.asList(ia).subList(0, listSize)))181fail("toArray(Object[]) is hosed (3)");182183}184185// Done inefficiently so as to exercise toArray186static List clone(List s) {187List a = Arrays.asList(s.toArray());188if (s.hashCode() != a.hashCode())189fail("Incorrect hashCode computation.");190191List clone = newList();192clone.addAll(a);193if (!s.equals(clone))194fail("List not equal to copy.");195if (!s.containsAll(clone))196fail("List does not contain copy.");197if (!clone.containsAll(s))198fail("Copy does not contain list.");199200return clone;201}202203static List newList() {204List s = Collections.checkedList(new ArrayList(), Integer.class);205if (!s.isEmpty())206fail("New instance non empty.");207return s;208}209210static void AddRandoms(List s, int n) {211for (int i=0; i<n; i++) {212int r = rnd.nextInt() % n;213Integer e = new Integer(r < 0 ? -r : r);214215int preSize = s.size();216if (!s.add(e))217fail ("Add failed.");218int postSize = s.size();219if (postSize-preSize != 1)220fail ("Add didn't increase size by 1.");221}222}223224static void fail(String s) {225throw new RuntimeException(s);226}227}228229230