test/jdk/java/util/Collections/NCopies.java
changeset 53107 cfceb4df2499
parent 48541 946e34c2dec9
equal deleted inserted replaced
53106:6c3407eee455 53107:cfceb4df2499
     1 /*
     1 /*
     2  * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    26  * @bug     6267846 6275009
    26  * @bug     6267846 6275009
    27  * @summary Test Collections.nCopies
    27  * @summary Test Collections.nCopies
    28  * @author  Martin Buchholz
    28  * @author  Martin Buchholz
    29  */
    29  */
    30 
    30 
       
    31 import java.util.ArrayList;
    31 import java.util.Collections;
    32 import java.util.Collections;
       
    33 import java.util.AbstractList;
    32 import java.util.List;
    34 import java.util.List;
       
    35 import java.util.Objects;
    33 
    36 
    34 public class NCopies {
    37 public class NCopies {
    35     static volatile int passed = 0, failed = 0;
    38     static volatile int passed = 0, failed = 0;
    36 
    39 
    37     static void fail(String msg) {
    40     static void fail(String msg) {
    80             check(sa[sa.length-1].equals("foo"));
    83             check(sa[sa.length-1].equals("foo"));
    81             check(x.get(x.size()/2).equals("foo"));
    84             check(x.get(x.size()/2).equals("foo"));
    82             checkEmpty(x.subList(x.size()/2, x.size()/2));
    85             checkEmpty(x.subList(x.size()/2, x.size()/2));
    83     }
    86     }
    84 
    87 
       
    88     private static <T> List<T> referenceNCopies(int n, T o) {
       
    89         // A simplest correct implementation of nCopies to compare with the actual optimized implementation
       
    90         return new AbstractList<>() {
       
    91             public int size() { return n; }
       
    92 
       
    93             public T get(int index) {
       
    94                 Objects.checkIndex(index, n);
       
    95                 return o;
       
    96             }
       
    97         };
       
    98     }
       
    99 
       
   100     private static void checkHashCode() {
       
   101         int[] sizes = {0, 1, 2, 3, 5, 10, 31, 32, 100, 1000};
       
   102         String[] elements = {null, "non-null"};
       
   103         for (int size : sizes) {
       
   104             for (String element : elements) {
       
   105                 int expectedHashCode = referenceNCopies(size, element).hashCode();
       
   106                 int actualHashCode = Collections.nCopies(size, element).hashCode();
       
   107                 check(expectedHashCode == actualHashCode,
       
   108                         "Collections.nCopies(" + size + ", " + element + ").hashCode()");
       
   109             }
       
   110         }
       
   111     }
       
   112 
       
   113     private static void checkEquals() {
       
   114         int[][] sizePairs = {{0, 0}, {0, 1}, {1, 0}, {1, 1}, {1, 2}, {2, 1}};
       
   115         String[] elements = {null, "non-null"};
       
   116         for (int[] pair : sizePairs) {
       
   117             for (String element : elements) {
       
   118                 boolean equal = pair[0] == pair[1];
       
   119                 String msg = "[" + pair[0] + ", " + element + "] <=> [" + pair[1] + ", " + element + "]";
       
   120                 check(equal == Collections.nCopies(pair[0], element).equals(Collections.nCopies(pair[1], element)), msg);
       
   121                 check(equal == Collections.nCopies(pair[0], element).equals(referenceNCopies(pair[1], element)), msg);
       
   122                 check(equal == referenceNCopies(pair[0], element).equals(Collections.nCopies(pair[1], element)), msg);
       
   123             }
       
   124         }
       
   125         List<String> nulls = Collections.nCopies(10, null);
       
   126         List<String> nonNulls = Collections.nCopies(10, "non-null");
       
   127         List<String> nullsButOne = new ArrayList<>(nulls);
       
   128         nullsButOne.set(9, "non-null");
       
   129         List<String> nonNullsButOne = new ArrayList<>(nonNulls);
       
   130         nonNullsButOne.set(9, null);
       
   131         check(!nulls.equals(nonNulls));
       
   132         check(!nulls.equals(nullsButOne));
       
   133         check(!nulls.equals(nonNullsButOne));
       
   134         check(!nonNulls.equals(nonNullsButOne));
       
   135         check(Collections.nCopies(0, null).equals(Collections.nCopies(0, "non-null")));
       
   136     }
       
   137 
    85     public static void main(String[] args) {
   138     public static void main(String[] args) {
    86         try {
   139         try {
    87             List<String> empty = Collections.nCopies(0, "foo");
   140             List<String> empty = Collections.nCopies(0, "foo");
    88             checkEmpty(empty);
   141             checkEmpty(empty);
    89             checkEmpty(empty.subList(0,0));
   142             checkEmpty(empty.subList(0,0));
    90 
   143 
    91             List<String> foos = Collections.nCopies(42, "foo");
   144             List<String> foos = Collections.nCopies(42, "foo");
    92             check(foos.size() == 42);
   145             check(foos.size() == 42);
    93             checkFoos(foos.subList(foos.size()/2, foos.size()-1));
   146             checkFoos(foos.subList(foos.size()/2, foos.size()-1));
    94 
   147 
       
   148             checkHashCode();
       
   149 
       
   150             checkEquals();
       
   151 
    95         } catch (Throwable t) { unexpected(t); }
   152         } catch (Throwable t) { unexpected(t); }
    96 
   153 
    97         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
   154         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
    98         if (failed > 0) throw new Error("Some tests failed");
   155         if (failed > 0) throw new Error("Some tests failed");
    99     }
   156     }