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 }