< prev index next >

test/jdk/java/util/HashMap/ToArray.java

Print this page

  1 /*
  2  * Copyright (c) 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 import java.util.ArrayList;
 25 import java.util.Arrays;
 26 import java.util.Collection;
 27 import java.util.HashMap;
 28 import java.util.HashSet;
 29 import java.util.LinkedHashMap;
 30 import java.util.LinkedHashSet;
 31 import java.util.List;
 32 import java.util.Map;
 33 import java.util.stream.LongStream;
 34 
 35 /*
 36  * @test

 37  * @summary HashMap.toArray() behavior tests
 38  * @author tvaleev
 39  */
 40 public class ToArray {








 41     public static void main(String[] args) {
 42         checkMap(false);
 43         checkMap(true);
 44         checkSet(false);
 45         checkSet(true);
 46     }
 47 
 48     private static <T extends Comparable<T>> void checkToArray(String message, T[] expected, Collection<T> collection,
 49                                                                boolean ignoreOrder) {
 50         if (ignoreOrder) {
 51             Arrays.sort(expected);
 52         }
 53         checkToObjectArray(message, expected, collection, ignoreOrder);
 54         checkToTypedArray(message, expected, Arrays.copyOf(expected, 0), collection, ignoreOrder);
 55         checkToTypedArray(message, expected, expected.clone(), collection, ignoreOrder);
 56         if (expected.length > 0) {
 57             T[] biggerArray = Arrays.copyOf(expected, expected.length * 2);
 58             System.arraycopy(expected, 0, biggerArray, expected.length, expected.length);
 59             checkToTypedArray(message, expected, biggerArray, collection, ignoreOrder);
 60         }

  1 /*
  2  * Copyright (c) 2019, 2024, 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 import java.util.ArrayList;
 25 import java.util.Arrays;
 26 import java.util.Collection;
 27 import java.util.HashMap;
 28 import java.util.HashSet;
 29 import java.util.LinkedHashMap;
 30 import java.util.LinkedHashSet;
 31 import java.util.List;
 32 import java.util.Map;
 33 import java.util.stream.LongStream;
 34 
 35 /*
 36  * @test
 37  * @bug 8336669
 38  * @summary HashMap.toArray() behavior tests
 39  * @author tvaleev
 40  */
 41 public class ToArray {
 42     // An interned identity class holding an int (like non-Preview Integer)
 43     record Int(int intValue) implements Comparable<Int> {
 44         @Override
 45         public int compareTo(Int o) {
 46             return Integer.compare(intValue, o.intValue);
 47         }
 48     }
 49 
 50     public static void main(String[] args) {
 51         checkMap(false);
 52         checkMap(true);
 53         checkSet(false);
 54         checkSet(true);
 55     }
 56 
 57     private static <T extends Comparable<T>> void checkToArray(String message, T[] expected, Collection<T> collection,
 58                                                                boolean ignoreOrder) {
 59         if (ignoreOrder) {
 60             Arrays.sort(expected);
 61         }
 62         checkToObjectArray(message, expected, collection, ignoreOrder);
 63         checkToTypedArray(message, expected, Arrays.copyOf(expected, 0), collection, ignoreOrder);
 64         checkToTypedArray(message, expected, expected.clone(), collection, ignoreOrder);
 65         if (expected.length > 0) {
 66             T[] biggerArray = Arrays.copyOf(expected, expected.length * 2);
 67             System.arraycopy(expected, 0, biggerArray, expected.length, expected.length);
 68             checkToTypedArray(message, expected, biggerArray, collection, ignoreOrder);
 69         }
< prev index next >