< prev index next >

test/jdk/java/util/RandomAccess/Basic.java

Print this page


   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 }
   1 /*
   2  * Copyright (c) 2000, 2019, 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  * @run testng Basic
  29  */
  30 
  31 import org.testng.annotations.DataProvider;
  32 import org.testng.annotations.Test;
  33 
  34 import static org.testng.Assert.assertEquals;
  35 
  36 import java.util.ArrayList;
  37 import java.util.Arrays;
  38 import java.util.Collections;
  39 import java.util.LinkedList;
  40 import java.util.List;
  41 import java.util.Random;
  42 import java.util.RandomAccess;
  43 import java.util.Stack;
  44 import java.util.Vector;
  45 import java.util.concurrent.CopyOnWriteArrayList;
  46 
  47 public class Basic {
  48 
  49     @DataProvider(name = "testLists")
  50     public Object[][] testData() {
  51         var list = Arrays.asList(new String[]{"a", "b", "c"});
  52         var stack = new Stack<String>();
  53         stack.push("a");
  54         stack.push("b");
  55         stack.push("c");
  56         return new Object[][]{
  57                 {list, true, "Arrays.asList"},
  58                 {stack, true, "Stack"},
  59                 {new ArrayList<>(list), true, "ArrayList"},
  60                 {new LinkedList<>(list), false, "LinkedList"},
  61                 {new Vector<>(list), true, "Vector"},
  62                 {new CopyOnWriteArrayList<>(list), true, "CopyOnWriteArrayList"}
  63         };
  64     }
  65 
  66     @Test(dataProvider = "testLists")
  67     public void testRandomAccess(List<String> list, boolean expectedRA, String failMsg) {
  68 
  69         var actualRA = list instanceof RandomAccess;
  70         assertEquals(actualRA, expectedRA, failMsg);
  71 
  72         List<String> unmodList =  Collections.unmodifiableList(list);
  73         List<String> syncList = Collections.synchronizedList(list);
  74         assertEquals((unmodList instanceof RandomAccess), actualRA,
  75                 "Unmodifiable fails to preserve RandomAccess");
  76         assertEquals((syncList instanceof RandomAccess), actualRA,
  77                 "Synchronized fails to preserve RandomAccess");
  78 
  79         while (list.size() > 0) {
  80             list = list.subList(0, list.size() - 1);
  81             assertEquals((list instanceof RandomAccess), actualRA,
  82                     "SubList fails to preserve RandomAccess: " + list.size());
  83 
  84             unmodList = unmodList.subList(0, unmodList.size() - 1);
  85             assertEquals((unmodList instanceof RandomAccess), actualRA,
  86                     "SubList(unmodifiable) fails to preserve RandomAccess: "
  87                             + unmodList.size());
  88 
  89             syncList = syncList.subList(0, syncList.size() - 1);
  90             assertEquals((syncList instanceof RandomAccess), actualRA,

  91                     "SubList(synchronized) fails to preserve RandomAccess: "
  92                             + syncList.size());
  93         }
  94     }
  95 
  96     @Test
  97     public void testListCopy() {
  98         ArrayList testCollection = new ArrayList<>(Collections.nCopies(100, "test"));
  99         ArrayList arrayListStr = new ArrayList<>(Collections.nCopies(100, ""));
 100         List linkedListStr = new LinkedList<>(arrayListStr);
 101         // Test that copy works on random & sequential access
 102         Collections.copy(arrayListStr, testCollection);
 103         Collections.copy(linkedListStr, testCollection);
 104         assertEquals(arrayListStr, testCollection, "ArrayList copy failed");
 105         assertEquals(linkedListStr, testCollection, "LinkedList copy failed");
 106     }
 107 
 108     @Test
 109     public void testListFill() {
 110         ArrayList testCollection = new ArrayList<>(Collections.nCopies(100, "test"));
 111         ArrayList arrayListStr = new ArrayList<>(Collections.nCopies(100, ""));
 112         List linkedListStr = new LinkedList<>(arrayListStr);
 113         // Test that fill works on random & sequential access
 114         Collections.fill(arrayListStr, "test");
 115         Collections.fill(linkedListStr, "test");
 116         assertEquals(arrayListStr, testCollection, "ArrayList fill failed");
 117         assertEquals(linkedListStr, testCollection, "LinkedList fill failed");
 118     }
 119 
 120     @Test
 121     public void testListShuffle() {
 122         // Test that shuffle works the same on random and sequential access
 123         ArrayList<Integer> al = new ArrayList<>();
 124         for (int j = 0; j < 100; j++)
 125                al.add(Integer.valueOf(2 * j));
 126         List<Integer> ll = new LinkedList<>(al);

 127         for (int i = 0; i < 100; i++) {
 128             Collections.shuffle(al, new Random(666));
 129             Collections.shuffle(ll, new Random(666));
 130             assertEquals(al, ll, "Shuffle failed: " + i);
 131         }
 132     }
 133 
 134     @Test
 135     public void testListBinarySearch() {
















 136         // Test that binarySearch works the same on random & sequential access
 137         ArrayList<Integer> al = new ArrayList<>();
 138         for (int i = 0; i < 10000; i++)
 139             al.add(Integer.valueOf(2 * i));
 140         List<Integer> ll = new LinkedList<>(al);
 141         for (int i = 0; i < 500; i++) {
 142             Integer key = Integer.valueOf(new Random(666).nextInt(20000));
 143             assertEquals(Collections.binarySearch(al, key), Collections
 144                     .binarySearch(ll, key), "Binary search failed: " + i);

 145         }
 146     }
 147 }
< prev index next >