1 /* 2 * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4327164 27 * @summary Basic test for new RandomAccess interface 28 */ 29 30 import java.util.ArrayList; 31 import java.util.Arrays; 32 import java.util.Collections; 33 import java.util.LinkedList; 34 import java.util.List; 35 import java.util.Random; 36 import java.util.RandomAccess; 37 import java.util.Vector; 38 39 public class Basic { 40 public static void main(String[] args) throws Exception { 41 List a0 = Arrays.asList(new String[] { "a", "b", "c" }); 42 List a[] = { a0, new ArrayList(a0), new LinkedList(a0), 43 new Vector(a0) }; 44 45 if (!(a[0] instanceof RandomAccess)) 46 throw new Exception("Arrays.asList doesn't implement RandomAccess"); 47 if (!(a[1] instanceof RandomAccess)) 48 throw new Exception("ArrayList doesn't implement RandomAccess"); 49 if (a[2] instanceof RandomAccess) 50 throw new Exception("LinkedList implements RandomAccess"); 51 if (!(a[3] instanceof RandomAccess)) 52 throw new Exception("Vector doesn't implement RandomAccess"); 53 54 for (int i = 0; i < a.length; i++) { 55 List t = a[i]; 56 List ut = Collections.unmodifiableList(t); 57 List st = Collections.synchronizedList(t); 58 59 boolean random = t instanceof RandomAccess; 60 if ((ut instanceof RandomAccess) != random) 61 throw new Exception( 62 "Unmodifiable fails to preserve RandomAccess: " + i); 63 if ((st instanceof RandomAccess) != random) 64 throw new Exception( 65 "Synchronized fails to preserve RandomAccess: " + i); 66 67 while (t.size() > 0) { 68 t = t.subList(0, t.size() - 1); 69 if ((t instanceof RandomAccess) != random) 70 throw new Exception( 71 "SubList fails to preserve RandomAccess: " + i 72 + ", " + t.size()); 73 74 ut = ut.subList(0, ut.size() - 1); 75 if ((ut instanceof RandomAccess) != random) 76 throw new Exception( 77 "SubList(unmodifiable) fails to preserve RandomAccess: " 78 + i + ", " + ut.size()); 79 80 st = st.subList(0, st.size() - 1); 81 if ((st instanceof RandomAccess) != random) 82 throw new Exception( 83 "SubList(synchronized) fails to preserve RandomAccess: " 84 + i + ", " + st.size()); 85 } 86 } 87 88 // Test that shuffle works the same on random and sequential access 89 List al = new ArrayList(); 90 for (int j = 0; j < 100; j++) 91 al.add(Integer.valueOf(2 * j)); 92 List ll = new LinkedList(al); 93 Random r1 = new Random(666), r2 = new Random(666); 94 for (int i = 0; i < 100; i++) { 95 Collections.shuffle(al, r1); 96 Collections.shuffle(ll, r2); 97 if (!al.equals(ll)) 98 throw new Exception("Shuffle failed: " + i); 99 } 100 101 // Test that fill works on random & sequential access 102 List gumbyParade = Collections.nCopies(100, "gumby"); 103 Collections.fill(al, "gumby"); 104 if (!al.equals(gumbyParade)) 105 throw new Exception("ArrayList fill failed"); 106 Collections.fill(ll, "gumby"); 107 if (!ll.equals(gumbyParade)) 108 throw new Exception("LinkedList fill failed"); 109 110 // Test that copy works on random & sequential access 111 List pokeyParade = Collections.nCopies(100, "pokey"); 112 Collections.copy(al, pokeyParade); 113 if (!al.equals(pokeyParade)) 114 throw new Exception("ArrayList copy failed"); 115 Collections.copy(ll, pokeyParade); 116 if (!ll.equals(pokeyParade)) 117 throw new Exception("LinkedList copy failed"); 118 119 // Test that binarySearch works the same on random & sequential access 120 al = new ArrayList(); 121 for (int i = 0; i < 10000; i++) 122 al.add(Integer.valueOf(2 * i)); 123 ll = new LinkedList(al); 124 for (int i = 0; i < 500; i++) { 125 Integer key = Integer.valueOf(r1.nextInt(20000)); 126 if (Collections.binarySearch(al, key) != Collections 127 .binarySearch(ll, key)) 128 throw new Exception("Binary search failed: " + i); 129 } 130 } 131 }