test/jdk/java/util/Arrays/ArraysEqCmpTest.java
author pli
Tue, 16 Jul 2019 00:57:00 +0000
changeset 55689 8c5c9d86e1d6
parent 47216 71c04702a3d5
permissions -rw-r--r--
8227512: [TESTBUG] Fix JTReg javac test failures with Graal Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
     1
/*
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
     4
 *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
     8
 *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    13
 * accompanied this code).
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    14
 *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    18
 *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    21
 * questions.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    22
 */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    23
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    24
/*
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    25
 * @test
33843
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33519
diff changeset
    26
 * @bug 8033148 8141409
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    27
 * @summary tests for array equals and compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    28
 * @run testng ArraysEqCmpTest
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    29
*/
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    30
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    31
import org.testng.Assert;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    32
import org.testng.annotations.DataProvider;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    33
import org.testng.annotations.Test;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    34
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    35
import java.lang.invoke.MethodHandle;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    36
import java.lang.invoke.MethodHandles;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    37
import java.lang.invoke.MethodType;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    38
import java.lang.reflect.Array;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    39
import java.util.Arrays;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    40
import java.util.Comparator;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    41
import java.util.HashMap;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    42
import java.util.List;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    43
import java.util.Map;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    44
import java.util.Objects;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    45
import java.util.function.BiFunction;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    46
import java.util.function.LongFunction;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    47
import java.util.stream.IntStream;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    48
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    49
public class ArraysEqCmpTest {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    50
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    51
    // Maximum width in bits
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    52
    static final int MAX_WIDTH = 512;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    53
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    54
    static final Map<Class, Integer> typeToWidth;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    55
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    56
    static {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    57
        typeToWidth = new HashMap<>();
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    58
        typeToWidth.put(boolean.class, Byte.SIZE);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    59
        typeToWidth.put(byte.class, Byte.SIZE);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    60
        typeToWidth.put(short.class, Short.SIZE);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    61
        typeToWidth.put(char.class, Character.SIZE);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    62
        typeToWidth.put(int.class, Integer.SIZE);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    63
        typeToWidth.put(long.class, Long.SIZE);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    64
        typeToWidth.put(float.class, Float.SIZE);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    65
        typeToWidth.put(double.class, Double.SIZE);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    66
        typeToWidth.put(Object.class, Integer.SIZE); // @@@ 32 or 64?
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    67
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    68
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    69
    static int arraySizeFor(Class<?> type) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    70
        type = type.isPrimitive() ? type : Object.class;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    71
        return 4 * MAX_WIDTH / typeToWidth.get(type);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    72
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    73
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    74
    static abstract class ArrayType<T> {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    75
        final Class<?> arrayType;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    76
        final Class<?> componentType;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    77
        final boolean unsigned;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    78
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    79
        final MethodHandle cpy;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    80
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    81
        final MethodHandle eq;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    82
        final MethodHandle eqr;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    83
        final MethodHandle cmp;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    84
        final MethodHandle cmpr;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    85
        final MethodHandle mm;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    86
        final MethodHandle mmr;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    87
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    88
        final MethodHandle getter;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    89
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    90
        final MethodHandle toString;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    91
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    92
        public ArrayType(Class<T> arrayType) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    93
            this(arrayType, false);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    94
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    95
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    96
        public ArrayType(Class<T> arrayType, boolean unsigned) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    97
            this.arrayType = arrayType;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    98
            this.componentType = arrayType.getComponentType();
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
    99
            this.unsigned = unsigned;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   100
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   101
            try {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   102
                MethodHandles.Lookup l = MethodHandles.lookup();
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   103
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   104
                getter = MethodHandles.arrayElementGetter(arrayType);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   105
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   106
                if (componentType.isPrimitive()) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   107
                    cpy = l.findStatic(Arrays.class, "copyOfRange",
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   108
                                       MethodType.methodType(arrayType, arrayType, int.class, int.class));
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   109
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   110
                    MethodType eqt = MethodType.methodType(
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   111
                            boolean.class, arrayType, arrayType);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   112
                    MethodType eqrt = MethodType.methodType(
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   113
                            boolean.class, arrayType, int.class, int.class, arrayType, int.class, int.class);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   114
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   115
                    eq = l.findStatic(Arrays.class, "equals", eqt);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   116
                    eqr = l.findStatic(Arrays.class, "equals", eqrt);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   117
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   118
                    String compareName = unsigned ? "compareUnsigned" : "compare";
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   119
                    cmp = l.findStatic(Arrays.class, compareName,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   120
                                       eqt.changeReturnType(int.class));
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   121
                    cmpr = l.findStatic(Arrays.class, compareName,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   122
                                        eqrt.changeReturnType(int.class));
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   123
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   124
                    mm = l.findStatic(Arrays.class, "mismatch",
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   125
                                       eqt.changeReturnType(int.class));
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   126
                    mmr = l.findStatic(Arrays.class, "mismatch",
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   127
                                       eqrt.changeReturnType(int.class));
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   128
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   129
                    toString = l.findStatic(Arrays.class, "toString",
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   130
                                            MethodType.methodType(String.class, arrayType));
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   131
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   132
                else {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   133
                    cpy = l.findStatic(Arrays.class, "copyOfRange",
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   134
                                       MethodType.methodType(Object[].class, Object[].class, int.class, int.class));
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   135
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   136
                    MethodType eqt = MethodType.methodType(
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   137
                            boolean.class, Object[].class, Object[].class);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   138
                    MethodType eqrt = MethodType.methodType(
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   139
                            boolean.class, Object[].class, int.class, int.class, Object[].class, int.class, int.class);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   140
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   141
                    eq = l.findStatic(Arrays.class, "equals", eqt);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   142
                    eqr = l.findStatic(Arrays.class, "equals", eqrt);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   143
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   144
                    MethodType cmpt = MethodType.methodType(
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   145
                            int.class, Comparable[].class, Comparable[].class);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   146
                    MethodType cmprt = MethodType.methodType(
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   147
                            int.class, Comparable[].class, int.class, int.class, Comparable[].class, int.class, int.class);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   148
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   149
                    cmp = l.findStatic(Arrays.class, "compare", cmpt);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   150
                    cmpr = l.findStatic(Arrays.class, "compare", cmprt);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   151
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   152
                    mm = l.findStatic(Arrays.class, "mismatch",
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   153
                                      eqt.changeReturnType(int.class));
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   154
                    mmr = l.findStatic(Arrays.class, "mismatch",
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   155
                                       eqrt.changeReturnType(int.class));
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   156
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   157
                    toString = l.findStatic(Arrays.class, "toString",
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   158
                                            MethodType.methodType(String.class, Object[].class));
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   159
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   160
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   161
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   162
            catch (Exception e) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   163
                throw new Error(e);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   164
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   165
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   166
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   167
        @Override
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   168
        public String toString() {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   169
            String s = arrayType.getCanonicalName();
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   170
            return unsigned ? "unsigned " + s : s;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   171
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   172
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   173
        Object construct(int length) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   174
            return Array.newInstance(componentType, length);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   175
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   176
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   177
        Object copyOf(Object a) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   178
            return copyOf(a, 0, Array.getLength(a));
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   179
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   180
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   181
        Object copyOf(Object a, int from, int to) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   182
            try {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   183
                return (Object) cpy.invoke(a, from, to);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   184
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   185
            catch (RuntimeException | Error e) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   186
                throw e;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   187
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   188
            catch (Throwable t) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   189
                throw new Error(t);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   190
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   191
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   192
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   193
        Object get(Object a, int i) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   194
            try {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   195
                return (Object) getter.invoke(a, i);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   196
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   197
            catch (RuntimeException | Error e) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   198
                throw e;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   199
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   200
            catch (Throwable t) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   201
                throw new Error(t);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   202
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   203
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   204
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   205
        abstract void set(Object a, int i, Object v);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   206
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   207
        boolean equals(Object a, Object b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   208
            try {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   209
                return (boolean) eq.invoke(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   210
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   211
            catch (RuntimeException | Error e) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   212
                throw e;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   213
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   214
            catch (Throwable t) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   215
                throw new Error(t);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   216
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   217
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   218
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   219
        boolean equals(Object a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   220
                       Object b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   221
            try {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   222
                return (boolean) eqr.invoke(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   223
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   224
            catch (RuntimeException | Error e) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   225
                throw e;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   226
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   227
            catch (Throwable t) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   228
                throw new Error(t);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   229
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   230
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   231
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   232
        int compare(Object a, Object b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   233
            try {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   234
                return (int) cmp.invoke(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   235
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   236
            catch (RuntimeException | Error e) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   237
                throw e;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   238
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   239
            catch (Throwable t) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   240
                throw new Error(t);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   241
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   242
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   243
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   244
        int compare(Object a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   245
                    Object b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   246
            try {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   247
                return (int) cmpr.invoke(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   248
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   249
            catch (RuntimeException | Error e) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   250
                throw e;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   251
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   252
            catch (Throwable t) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   253
                throw new Error(t);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   254
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   255
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   256
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   257
        int mismatch(Object a, Object b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   258
            try {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   259
                return (int) mm.invoke(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   260
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   261
            catch (RuntimeException | Error e) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   262
                throw e;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   263
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   264
            catch (Throwable t) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   265
                throw new Error(t);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   266
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   267
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   268
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   269
        int mismatch(Object a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   270
                     Object b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   271
            try {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   272
                return (int) mmr.invoke(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   273
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   274
            catch (RuntimeException | Error e) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   275
                throw e;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   276
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   277
            catch (Throwable t) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   278
                throw new Error(t);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   279
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   280
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   281
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   282
        String toString(Object a) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   283
            try {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   284
                return (String) toString.invoke(a);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   285
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   286
            catch (RuntimeException | Error e) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   287
                throw e;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   288
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   289
            catch (Throwable t) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   290
                throw new Error(t);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   291
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   292
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   293
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   294
        static class BoxedIntegers extends ArrayType<Integer[]> {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   295
            public BoxedIntegers() {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   296
                super(Integer[].class);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   297
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   298
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   299
            @Override
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   300
            void set(Object a, int i, Object v) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   301
                // Ensure unique reference
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   302
                ((Integer[]) a)[i] = v != null ? new Integer((Integer) v) : null;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   303
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   304
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   305
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   306
        static class BoxedIntegersWithReverseComparator extends BoxedIntegers {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   307
            final Comparator<Integer> c = (a, b) -> {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   308
                // Nulls sort after non-nulls
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   309
                if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   310
                    return a == null ? b == null ? 0 : 1 : -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   311
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   312
                return Integer.compare(b, a);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   313
            };
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   314
33843
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33519
diff changeset
   315
            final MethodHandle eqc;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33519
diff changeset
   316
            final MethodHandle eqcr;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   317
            final MethodHandle cmpc;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   318
            final MethodHandle cmpcr;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   319
            final MethodHandle mismatchc;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   320
            final MethodHandle mismatchcr;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   321
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   322
            public BoxedIntegersWithReverseComparator() {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   323
                try {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   324
                    MethodHandles.Lookup l = MethodHandles.lookup();
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   325
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   326
                    MethodType cmpt = MethodType.methodType(
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   327
                            int.class, Object[].class, Object[].class, Comparator.class);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   328
                    MethodType cmprt = MethodType.methodType(
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   329
                            int.class, Object[].class, int.class, int.class,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   330
                            Object[].class, int.class, int.class, Comparator.class);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   331
33843
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33519
diff changeset
   332
                    eqc = l.findStatic(Arrays.class, "equals", cmpt.changeReturnType(boolean.class));
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33519
diff changeset
   333
                    eqcr = l.findStatic(Arrays.class, "equals", cmprt.changeReturnType(boolean.class));
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   334
                    cmpc = l.findStatic(Arrays.class, "compare", cmpt);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   335
                    cmpcr = l.findStatic(Arrays.class, "compare", cmprt);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   336
                    mismatchc = l.findStatic(Arrays.class, "mismatch", cmpt);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   337
                    mismatchcr = l.findStatic(Arrays.class, "mismatch", cmprt);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   338
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   339
                catch (Exception e) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   340
                    throw new Error(e);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   341
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   342
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   343
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   344
            @Override
33843
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33519
diff changeset
   345
            boolean equals(Object a, Object b) {
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33519
diff changeset
   346
                try {
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33519
diff changeset
   347
                    return (boolean) eqc.invoke(a, b, c);
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33519
diff changeset
   348
                }
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33519
diff changeset
   349
                catch (RuntimeException | Error e) {
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33519
diff changeset
   350
                    throw e;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33519
diff changeset
   351
                }
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33519
diff changeset
   352
                catch (Throwable t) {
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33519
diff changeset
   353
                    throw new Error(t);
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33519
diff changeset
   354
                }
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33519
diff changeset
   355
            }
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33519
diff changeset
   356
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33519
diff changeset
   357
            @Override
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33519
diff changeset
   358
            boolean equals(Object a, int aFromIndex, int aToIndex,
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33519
diff changeset
   359
                           Object b, int bFromIndex, int bToIndex) {
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33519
diff changeset
   360
                try {
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33519
diff changeset
   361
                    return (boolean) eqcr.invoke(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex, c);
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33519
diff changeset
   362
                }
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33519
diff changeset
   363
                catch (RuntimeException | Error e) {
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33519
diff changeset
   364
                    throw e;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33519
diff changeset
   365
                }
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33519
diff changeset
   366
                catch (Throwable t) {
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33519
diff changeset
   367
                    throw new Error(t);
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33519
diff changeset
   368
                }
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33519
diff changeset
   369
            }
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33519
diff changeset
   370
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33519
diff changeset
   371
            @Override
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   372
            int compare(Object a, Object b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   373
                try {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   374
                    return (int) cmpc.invoke(a, b, c);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   375
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   376
                catch (RuntimeException | Error e) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   377
                    throw e;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   378
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   379
                catch (Throwable t) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   380
                    throw new Error(t);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   381
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   382
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   383
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   384
            @Override
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   385
            int compare(Object a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   386
                        Object b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   387
                try {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   388
                    return (int) cmpcr.invoke(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex, c);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   389
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   390
                catch (RuntimeException | Error e) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   391
                    throw e;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   392
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   393
                catch (Throwable t) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   394
                    throw new Error(t);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   395
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   396
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   397
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   398
            @Override
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   399
            int mismatch(Object a, Object b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   400
                try {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   401
                    return (int) mismatchc.invoke(a, b, c);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   402
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   403
                catch (RuntimeException | Error e) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   404
                    throw e;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   405
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   406
                catch (Throwable t) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   407
                    throw new Error(t);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   408
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   409
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   410
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   411
            @Override
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   412
            int mismatch(Object a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   413
                         Object b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   414
                try {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   415
                    return (int) mismatchcr.invoke(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex, c);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   416
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   417
                catch (RuntimeException | Error e) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   418
                    throw e;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   419
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   420
                catch (Throwable t) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   421
                    throw new Error(t);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   422
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   423
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   424
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   425
            @Override
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   426
            public String toString() {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   427
                return arrayType.getCanonicalName() + " with Comparator";
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   428
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   429
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   430
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   431
        static class Booleans extends ArrayType<boolean[]> {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   432
            public Booleans() {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   433
                super(boolean[].class);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   434
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   435
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   436
            @Override
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   437
            void set(Object a, int i, Object v) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   438
                boolean pv;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   439
                if (v instanceof Boolean) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   440
                    pv = (Boolean) v;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   441
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   442
                else if (v instanceof Integer) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   443
                    pv = ((Integer) v) >= 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   444
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   445
                else throw new IllegalStateException();
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   446
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   447
                ((boolean[]) a)[i] = pv;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   448
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   449
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   450
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   451
        static class Bytes extends ArrayType<byte[]> {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   452
            public Bytes(boolean unsigned) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   453
                super(byte[].class, unsigned);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   454
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   455
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   456
            @Override
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   457
            void set(Object a, int i, Object v) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   458
                byte pv;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   459
                if (v instanceof Byte) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   460
                    pv = (Byte) v;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   461
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   462
                else if (v instanceof Integer) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   463
                    pv = ((Integer) v).byteValue();
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   464
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   465
                else throw new IllegalStateException();
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   466
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   467
                ((byte[]) a)[i] = pv;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   468
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   469
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   470
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   471
        static class Characters extends ArrayType<char[]> {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   472
            public Characters() {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   473
                super(char[].class);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   474
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   475
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   476
            @Override
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   477
            void set(Object a, int i, Object v) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   478
                char pv;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   479
                if (v instanceof Character) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   480
                    pv = (Character) v;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   481
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   482
                else if (v instanceof Integer) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   483
                    pv = (char) ((Integer) v).intValue();
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   484
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   485
                else throw new IllegalStateException();
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   486
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   487
                ((char[]) a)[i] = pv;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   488
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   489
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   490
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   491
        static class Shorts extends ArrayType<short[]> {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   492
            public Shorts(boolean unsigned) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   493
                super(short[].class, unsigned);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   494
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   495
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   496
            @Override
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   497
            void set(Object a, int i, Object v) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   498
                short pv;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   499
                if (v instanceof Short) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   500
                    pv = (Short) v;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   501
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   502
                else if (v instanceof Integer) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   503
                    pv = ((Integer) v).shortValue();
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   504
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   505
                else throw new IllegalStateException();
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   506
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   507
                ((short[]) a)[i] = pv;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   508
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   509
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   510
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   511
        static class Integers extends ArrayType<int[]> {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   512
            public Integers(boolean unsigned) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   513
                super(int[].class, unsigned);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   514
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   515
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   516
            @Override
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   517
            void set(Object a, int i, Object v) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   518
                int pv;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   519
                if (v instanceof Integer) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   520
                    pv = ((Integer) v).shortValue();
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   521
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   522
                else throw new IllegalStateException();
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   523
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   524
                ((int[]) a)[i] = pv;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   525
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   526
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   527
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   528
        static class Longs extends ArrayType<long[]> {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   529
            public Longs(boolean unsigned) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   530
                super(long[].class, unsigned);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   531
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   532
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   533
            @Override
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   534
            void set(Object a, int i, Object v) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   535
                long pv;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   536
                if (v instanceof Long) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   537
                    pv = (Long) v;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   538
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   539
                else if (v instanceof Integer) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   540
                    pv = ((Integer) v).longValue();
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   541
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   542
                else throw new IllegalStateException();
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   543
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   544
                ((long[]) a)[i] = pv;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   545
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   546
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   547
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   548
        static class Floats extends ArrayType<float[]> {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   549
            public Floats() {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   550
                super(float[].class);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   551
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   552
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   553
            @Override
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   554
            void set(Object a, int i, Object v) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   555
                float pv;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   556
                if (v instanceof Float) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   557
                    pv = (Float) v;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   558
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   559
                else if (v instanceof Integer) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   560
                    pv = ((Integer) v).floatValue();
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   561
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   562
                else throw new IllegalStateException();
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   563
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   564
                ((float[]) a)[i] = pv;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   565
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   566
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   567
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   568
        static class Doubles extends ArrayType<double[]> {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   569
            public Doubles() {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   570
                super(double[].class);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   571
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   572
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   573
            @Override
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   574
            void set(Object a, int i, Object v) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   575
                double pv;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   576
                if (v instanceof Double) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   577
                    pv = (Double) v;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   578
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   579
                else if (v instanceof Integer) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   580
                    pv = ((Integer) v).doubleValue();
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   581
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   582
                else throw new IllegalStateException();
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   583
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   584
                ((double[]) a)[i] = pv;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   585
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   586
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   587
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   588
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   589
    static Object[][] arrayTypes;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   590
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   591
    @DataProvider
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   592
    public static Object[][] arrayTypesProvider() {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   593
        if (arrayTypes == null) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   594
            arrayTypes = new Object[][]{
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   595
                    new Object[]{new ArrayType.BoxedIntegers()},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   596
                    new Object[]{new ArrayType.BoxedIntegersWithReverseComparator()},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   597
                    new Object[]{new ArrayType.Booleans()},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   598
                    new Object[]{new ArrayType.Bytes(false)},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   599
                    new Object[]{new ArrayType.Bytes(true)},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   600
                    new Object[]{new ArrayType.Characters()},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   601
                    new Object[]{new ArrayType.Shorts(false)},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   602
                    new Object[]{new ArrayType.Shorts(true)},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   603
                    new Object[]{new ArrayType.Integers(false)},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   604
                    new Object[]{new ArrayType.Integers(true)},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   605
                    new Object[]{new ArrayType.Longs(false)},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   606
                    new Object[]{new ArrayType.Longs(true)},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   607
                    new Object[]{new ArrayType.Floats()},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   608
                    new Object[]{new ArrayType.Doubles()},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   609
            };
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   610
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   611
        return arrayTypes;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   612
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   613
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   614
    static Object[][] floatArrayTypes;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   615
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   616
    @DataProvider
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   617
    public static Object[][] floatArrayTypesProvider() {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   618
        if (floatArrayTypes == null) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   619
            LongFunction<Object> bTof = rb -> Float.intBitsToFloat((int) rb);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   620
            LongFunction<Object> bToD = Double::longBitsToDouble;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   621
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   622
            floatArrayTypes = new Object[][]{
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   623
                    new Object[]{new ArrayType.Floats(), 0x7fc00000L, 0x7f800001L, bTof},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   624
                    new Object[]{new ArrayType.Doubles(), 0x7ff8000000000000L, 0x7ff0000000000001L, bToD},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   625
            };
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   626
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   627
        return floatArrayTypes;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   628
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   629
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   630
    static Object[][] objectArrayTypes;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   631
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   632
    @DataProvider
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   633
    public static Object[][] objectArrayTypesProvider() {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   634
        if (objectArrayTypes == null) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   635
            LongFunction<Object> bTof = rb -> Float.intBitsToFloat((int) rb);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   636
            LongFunction<Object> bToD = Double::longBitsToDouble;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   637
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   638
            objectArrayTypes = new Object[][]{
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   639
                    new Object[]{new ArrayType.BoxedIntegers()},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   640
                    new Object[]{new ArrayType.BoxedIntegersWithReverseComparator()},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   641
            };
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   642
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   643
        return objectArrayTypes;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   644
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   645
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   646
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   647
    static Object[][] signedUnsignedArrayTypes;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   648
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   649
    @DataProvider
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   650
    public static Object[][] signedUnsignedArrayTypes() {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   651
        if (signedUnsignedArrayTypes == null) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   652
            signedUnsignedArrayTypes = new Object[][]{
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   653
                    new Object[]{new ArrayType.Bytes(false), new ArrayType.Bytes(true)},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   654
                    new Object[]{new ArrayType.Shorts(false), new ArrayType.Shorts(true)},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   655
                    new Object[]{new ArrayType.Integers(false), new ArrayType.Integers(true)},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   656
                    new Object[]{new ArrayType.Longs(false), new ArrayType.Longs(true)},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   657
            };
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   658
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   659
        return signedUnsignedArrayTypes;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   660
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   661
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   662
    // Equality and comparison tests
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   663
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   664
    @Test(dataProvider = "arrayTypesProvider")
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   665
    public void testArray(ArrayType<?> arrayType) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   666
        BiFunction<ArrayType<?>, Integer, Object> constructor = (at, s) -> {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   667
            Object a = at.construct(s);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   668
            for (int x = 0; x < s; x++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   669
                at.set(a, x, x % 8);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   670
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   671
            return a;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   672
        };
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   673
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   674
        BiFunction<ArrayType<?>, Object, Object> cloner = (at, a) ->
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   675
                constructor.apply(at, Array.getLength(a));
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   676
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   677
        testArrayType(arrayType, constructor, cloner);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   678
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   679
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   680
    @Test(dataProvider = "floatArrayTypesProvider")
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   681
    public void testPrimitiveFloatArray(
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   682
            ArrayType<?> arrayType,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   683
            long canonicalNanRawBits, long nonCanonicalNanRawBits,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   684
            LongFunction<Object> bitsToFloat) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   685
        Object canonicalNan = bitsToFloat.apply(canonicalNanRawBits);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   686
        // If conversion is a signalling NaN it may be subject to conversion to a
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   687
        // quiet NaN on some processors, even if a copy is performed
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   688
        // The tests assume that if conversion occurs it does not convert to the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   689
        // canonical NaN
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   690
        Object nonCanonicalNan = bitsToFloat.apply(nonCanonicalNanRawBits);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   691
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   692
        BiFunction<ArrayType<?>, Integer, Object> canonicalNaNs = (at, s) -> {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   693
            Object a = at.construct(s);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   694
            for (int x = 0; x < s; x++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   695
                at.set(a, x, canonicalNan);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   696
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   697
            return a;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   698
        };
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   699
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   700
        BiFunction<ArrayType<?>, Object, Object> nonCanonicalNaNs = (at, a) -> {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   701
            int s = Array.getLength(a);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   702
            Object ac = at.construct(s);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   703
            for (int x = 0; x < s; x++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   704
                at.set(ac, x, nonCanonicalNan);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   705
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   706
            return ac;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   707
        };
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   708
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   709
        BiFunction<ArrayType<?>, Object, Object> halfNonCanonicalNaNs = (at, a) -> {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   710
            int s = Array.getLength(a);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   711
            Object ac = at.construct(s);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   712
            for (int x = 0; x < s / 2; x++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   713
                at.set(ac, x, nonCanonicalNan);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   714
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   715
            for (int x = s / 2; x < s; x++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   716
                at.set(ac, x, 1);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   717
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   718
            return ac;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   719
        };
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   720
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   721
        testArrayType(arrayType, canonicalNaNs, nonCanonicalNaNs);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   722
        testArrayType(arrayType, canonicalNaNs, halfNonCanonicalNaNs);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   723
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   724
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   725
    @Test(dataProvider = "objectArrayTypesProvider")
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   726
    public void testNullElementsInObjectArray(ArrayType<?> arrayType) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   727
        BiFunction<ArrayType<?>, Object, Object> cloner = ArrayType::copyOf;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   728
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   729
        // All nulls
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   730
        testArrayType(arrayType,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   731
                      (at, s) -> {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   732
                          Object a = at.construct(s);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   733
                          for (int x = 0; x < s; x++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   734
                              at.set(a, x, null);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   735
                          }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   736
                          return a;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   737
                      },
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   738
                      cloner);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   739
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   740
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   741
        // Some nulls
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   742
        testArrayType(arrayType,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   743
                      (at, s) -> {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   744
                          Object a = at.construct(s);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   745
                          for (int x = 0; x < s; x++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   746
                              int v = x % 8;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   747
                              at.set(a, x, v == 0 ? null : v);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   748
                          }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   749
                          return a;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   750
                      },
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   751
                      cloner);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   752
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   753
        Integer[] a = new Integer[]{null, 0};
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   754
        Integer[] b = new Integer[]{0, 0};
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   755
        Assert.assertTrue(Arrays.compare(a, b) < 0);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   756
        Assert.assertTrue(Arrays.compare(b, a) > 0);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   757
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   758
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   759
    @Test(dataProvider = "objectArrayTypesProvider")
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   760
    public void testSameRefElementsInObjectArray(ArrayType<?> arrayType) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   761
        BiFunction<ArrayType<?>, Object, Object> cloner = ArrayType::copyOf;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   762
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   763
        // One ref
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   764
        Integer one = 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   765
        testArrayType(arrayType,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   766
                      (at, s) -> {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   767
                          Integer[] a = (Integer[]) at.construct(s);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   768
                          for (int x = 0; x < s; x++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   769
                              a[x] = one;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   770
                          }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   771
                          return a;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   772
                      },
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   773
                      cloner);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   774
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   775
        // All ref
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   776
        testArrayType(arrayType,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   777
                      (at, s) -> {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   778
                          Integer[] a = (Integer[]) at.construct(s);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   779
                          for (int x = 0; x < s; x++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   780
                              a[x] = Integer.valueOf(s);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   781
                          }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   782
                          return a;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   783
                      },
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   784
                      cloner);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   785
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   786
        // Some same ref
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   787
        testArrayType(arrayType,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   788
                      (at, s) -> {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   789
                          Integer[] a = (Integer[]) at.construct(s);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   790
                          for (int x = 0; x < s; x++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   791
                              int v = x % 8;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   792
                              a[x] = v == 1 ? one : new Integer(v);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   793
                          }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   794
                          return a;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   795
                      },
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   796
                      cloner);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   797
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   798
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   799
    @Test(dataProvider = "signedUnsignedArrayTypes")
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   800
    public void testSignedUnsignedArray(ArrayType<?> sat, ArrayType<?> uat) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   801
        BiFunction<ArrayType<?>, Integer, Object> constructor = (at, s) -> {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   802
            Object a = at.construct(s);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   803
            for (int x = 0; x < s; x++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   804
                at.set(a, x, 1);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   805
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   806
            return a;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   807
        };
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   808
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   809
        int n = arraySizeFor(sat.componentType);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   810
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   811
        for (int s : ranges(0, n)) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   812
            Object a = constructor.apply(sat, s);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   813
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   814
            for (int aFrom : ranges(0, s)) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   815
                for (int aTo : ranges(aFrom, s)) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   816
                    int aLength = aTo - aFrom;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   817
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   818
                    if (aLength > 0) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   819
                        for (int i = aFrom; i < aTo; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   820
                            Object ac = sat.copyOf(a);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   821
                            // Create common prefix with a length of i - aFrom
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   822
                            sat.set(ac, i, -1);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   823
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   824
                            int sc = sat.compare(ac, aFrom, aTo, a, aFrom, aTo);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   825
                            int uc = uat.compare(ac, aFrom, aTo, a, aFrom, aTo);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   826
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   827
                            Assert.assertTrue(sc < 0);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   828
                            Assert.assertTrue(uc > 0);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   829
                        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   830
                    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   831
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   832
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   833
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   834
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   835
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   836
    void testArrayType(ArrayType<?> at,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   837
                       BiFunction<ArrayType<?>, Integer, Object> constructor,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   838
                       BiFunction<ArrayType<?>, Object, Object> cloner) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   839
        int n = arraySizeFor(at.componentType);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   840
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   841
        for (int s : ranges(0, n)) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   842
            Object a = constructor.apply(at, s);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   843
            Object b = cloner.apply(at, a);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   844
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   845
            for (int aFrom : ranges(0, s)) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   846
                for (int aTo : ranges(aFrom, s)) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   847
                    int aLength = aTo - aFrom;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   848
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   849
                    for (int bFrom : ranges(0, s)) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   850
                        for (int bTo : ranges(bFrom, s)) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   851
                            int bLength = bTo - bFrom;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   852
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   853
                            Object anr = at.copyOf(a, aFrom, aTo);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   854
                            Object bnr = at.copyOf(b, bFrom, bTo);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   855
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   856
                            boolean eq = isEqual(at, a, aFrom, aTo, b, bFrom, bTo);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   857
                            Assert.assertEquals(at.equals(a, aFrom, aTo, b, bFrom, bTo), eq);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   858
                            Assert.assertEquals(at.equals(b, bFrom, bTo, a, aFrom, aTo), eq);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   859
                            Assert.assertEquals(at.equals(anr, bnr), eq);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   860
                            Assert.assertEquals(at.equals(bnr, anr), eq);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   861
                            if (eq) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   862
                                Assert.assertEquals(at.compare(a, aFrom, aTo, b, bFrom, bTo), 0);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   863
                                Assert.assertEquals(at.compare(b, bFrom, bTo, a, aFrom, aTo), 0);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   864
                                Assert.assertEquals(at.compare(anr, bnr), 0);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   865
                                Assert.assertEquals(at.compare(bnr, anr), 0);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   866
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   867
                                Assert.assertEquals(at.mismatch(a, aFrom, aTo, b, bFrom, bTo), -1);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   868
                                Assert.assertEquals(at.mismatch(b, bFrom, bTo, a, aFrom, aTo), -1);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   869
                                Assert.assertEquals(at.mismatch(anr, bnr), -1);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   870
                                Assert.assertEquals(at.mismatch(bnr, anr), -1);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   871
                            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   872
                            else {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   873
                                int aCb = at.compare(a, aFrom, aTo, b, bFrom, bTo);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   874
                                int bCa = at.compare(b, bFrom, bTo, a, aFrom, aTo);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   875
                                int v = Integer.signum(aCb) * Integer.signum(bCa);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   876
                                Assert.assertTrue(v == -1);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   877
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   878
                                int anrCbnr = at.compare(anr, bnr);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   879
                                int bnrCanr = at.compare(bnr, anr);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   880
                                Assert.assertEquals(anrCbnr, aCb);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   881
                                Assert.assertEquals(bnrCanr, bCa);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   882
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   883
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   884
                                int aMb = at.mismatch(a, aFrom, aTo, b, bFrom, bTo);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   885
                                int bMa = at.mismatch(b, bFrom, bTo, a, aFrom, aTo);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   886
                                int anrMbnr = at.mismatch(anr, bnr);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   887
                                int bnrManr = at.mismatch(bnr, anr);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   888
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   889
                                Assert.assertNotEquals(aMb, -1);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   890
                                Assert.assertEquals(aMb, bMa);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   891
                                Assert.assertNotEquals(anrMbnr, -1);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   892
                                Assert.assertEquals(anrMbnr, bnrManr);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   893
                                Assert.assertEquals(aMb, anrMbnr);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   894
                                Assert.assertEquals(bMa, bnrManr);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   895
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   896
                                // Common or proper prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   897
                                Assert.assertTrue(at.equals(a, aFrom, aFrom + aMb, b, bFrom, bFrom + aMb));
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   898
                                if (aMb < Math.min(aLength, bLength)) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   899
                                    // Common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   900
                                    Assert.assertFalse(isEqual(at, a, aFrom + aMb, b, bFrom + aMb));
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   901
                                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   902
                            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   903
                        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   904
                    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   905
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   906
                    if (aLength > 0) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   907
                        for (int i = aFrom; i < aTo; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   908
                            Object ac = at.copyOf(a);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   909
                            // Create common prefix with a length of i - aFrom
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   910
                            at.set(ac, i, -1);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   911
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   912
                            Object acnr = at.copyOf(ac, aFrom, aTo);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   913
                            Object anr = at.copyOf(a, aFrom, aTo);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   914
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   915
                            Assert.assertFalse(at.equals(ac, aFrom, aTo, a, aFrom, aTo));
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   916
                            Assert.assertFalse(at.equals(acnr, anr));
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   917
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   918
                            int acCa = at.compare(ac, aFrom, aTo, a, aFrom, aTo);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   919
                            int aCac = at.compare(a, aFrom, aTo, ac, aFrom, aTo);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   920
                            int v = Integer.signum(acCa) * Integer.signum(aCac);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   921
                            Assert.assertTrue(v == -1);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   922
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   923
                            int acnrCanr = at.compare(acnr, anr);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   924
                            int anrCacnr = at.compare(anr, acnr);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   925
                            Assert.assertEquals(acnrCanr, acCa);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   926
                            Assert.assertEquals(anrCacnr, aCac);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   927
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   928
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   929
                            int acMa = at.mismatch(ac, aFrom, aTo, a, aFrom, aTo);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   930
                            int aMac = at.mismatch(a, aFrom, aTo, ac, aFrom, aTo);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   931
                            Assert.assertEquals(acMa, aMac);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   932
                            Assert.assertEquals(acMa, i - aFrom);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   933
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   934
                            int acnrManr = at.mismatch(acnr, anr);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   935
                            int anrMacnr = at.mismatch(anr, acnr);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   936
                            Assert.assertEquals(acnrManr, anrMacnr);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   937
                            Assert.assertEquals(acnrManr, i - aFrom);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   938
                        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   939
                    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   940
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   941
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   942
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   943
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   944
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   945
    static boolean isEqual(ArrayType<?> at, Object a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   946
                           Object b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   947
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   948
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   949
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   950
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   951
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   952
        for (int i = 0; i < aLength; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   953
            Object av = at.get(a, aFromIndex++);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   954
            Object bv = at.get(b, bFromIndex++);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   955
            if (!Objects.equals(av, bv)) return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   956
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   957
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   958
        return true;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   959
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   960
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   961
    static boolean isEqual(ArrayType<?> at, Object a, int aFrom, Object b, int bFrom) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   962
        Object av = at.get(a, aFrom);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   963
        Object bv = at.get(b, bFrom);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   964
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   965
        return Objects.equals(av, bv);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   966
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   967
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   968
    static int[] ranges(int from, int to) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   969
        int width = to - from;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   970
        switch (width) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   971
            case 0:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   972
                return new int[]{};
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   973
            case 1:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   974
                return new int[]{from, to};
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   975
            case 2:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   976
                return new int[]{from, from + 1, to};
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   977
            case 3:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   978
                return new int[]{from, from + 1, from + 2, to};
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   979
            default:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   980
                return IntStream.of(from, from + 1, from + 2, to / 2 - 1, to / 2, to / 2 + 1, to - 2, to - 1, to)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   981
                        .filter(i -> i >= from && i <= to)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   982
                        .distinct().toArray();
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   983
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   984
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   985
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   986
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   987
    // Null array reference tests
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   988
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   989
    @Test(dataProvider = "arrayTypesProvider")
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   990
    public void testNullArrayRefs(ArrayType<?> arrayType) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   991
        Object n = null;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   992
        Object a = arrayType.construct(0);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   993
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   994
        Assert.assertTrue(arrayType.equals(n, n));
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   995
        Assert.assertFalse(arrayType.equals(n, a));
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   996
        Assert.assertFalse(arrayType.equals(a, n));
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   997
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   998
        Assert.assertEquals(arrayType.compare(n, n), 0);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
   999
        Assert.assertTrue(arrayType.compare(n, a) < 0);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1000
        Assert.assertTrue(arrayType.compare(a, n) > 0);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1001
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1002
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1003
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1004
    // Exception throwing tests
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1005
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1006
    @Test(dataProvider = "arrayTypesProvider")
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1007
    public void testNPEs(ArrayType<?> arrayType) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1008
        Object[] values = new Object[]{null, arrayType.construct(0)};
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1009
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1010
        for (Object o1 : values) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1011
            for (Object o2 : values) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1012
                if (o1 != null && o2 != null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1013
                    continue;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1014
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1015
                testNPE(() -> arrayType.equals(o1, 0, 0, o2, 0, 0));
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1016
                testNPE(() -> arrayType.compare(o1, 0, 0, o2, 0, 0));
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1017
                testNPE(() -> arrayType.mismatch(o1, o2));
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1018
                testNPE(() -> arrayType.mismatch(o1, 0, 0, o2, 0, 0));
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1019
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1020
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1021
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1022
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1023
    @Test
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1024
    public void testObjectNPEs() {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1025
        String[][] values = new String[][]{null, new String[0]};
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1026
        Comparator<String> c = String::compareTo;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1027
        Comparator[] cs = new Comparator[]{null, c};
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1028
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1029
        for (String[] o1 : values) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1030
            for (String[] o2 : values) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1031
                for (Comparator o3 : cs) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1032
                    if (o1 != null && o2 != null && o3 != null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1033
                        continue;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1034
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1035
                    if (o3 == null) {
33843
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33519
diff changeset
  1036
                        testNPE(() -> Arrays.equals(o1, o2, o3));
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1037
                        testNPE(() -> Arrays.compare(o1, o2, o3));
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1038
                        testNPE(() -> Arrays.mismatch(o1, o2, o3));
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1039
                    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1040
33843
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33519
diff changeset
  1041
                    testNPE(() -> Arrays.equals(o1, 0, 0, o2, 0, 0, o3));
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1042
                    testNPE(() -> Arrays.compare(o1, 0, 0, o2, 0, 0, o3));
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1043
                    testNPE(() -> Arrays.mismatch(o1, 0, 0, o2, 0, 0, o3));
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1044
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1045
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1046
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1047
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1048
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1049
    @Test(dataProvider = "arrayTypesProvider")
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1050
    public void testIAEs(ArrayType<?> arrayType) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1051
        List<Integer> values = Arrays.asList(0, 1);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1052
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1053
        for (int s : values) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1054
            Object a = arrayType.construct(s);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1055
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1056
            for (int o1 : values) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1057
                for (int o2 : values) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1058
                    if (o1 <= o2) continue;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1059
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1060
                    testIAE(() -> arrayType.equals(a, o1, 0, a, o2, 0));
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1061
                    testIAE(() -> arrayType.compare(a, o1, 0, a, o2, 0));
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1062
                    testIAE(() -> arrayType.mismatch(a, o1, 0, a, o2, 0));
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1063
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1064
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1065
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1066
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1067
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1068
    @Test(dataProvider = "arrayTypesProvider")
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1069
    public void testAIOBEs(ArrayType<?> arrayType) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1070
        List<Integer> froms = Arrays.asList(-1, 0);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1071
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1072
        for (int s : Arrays.asList(0, 1)) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1073
            List<Integer> tos = Arrays.asList(s, s + 1);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1074
            Object a = arrayType.construct(s);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1075
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1076
            for (int aFrom : froms) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1077
                for (int aTo : tos) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1078
                    for (int bFrom : froms) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1079
                        for (int bTo : tos) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1080
                            if (aFrom >= 0 && aTo <= s &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1081
                                bFrom >= 0 && bTo <= s) continue;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1082
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1083
                            testAIOBE(() -> arrayType.equals(a, aFrom, aTo, a, bFrom, bTo));
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1084
                            testAIOBE(() -> arrayType.compare(a, aFrom, aTo, a, bFrom, bTo));
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1085
                            testAIOBE(() -> arrayType.mismatch(a, aFrom, aTo, a, bFrom, bTo));
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1086
                        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1087
                    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1088
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1089
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1090
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1091
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1092
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1093
    static void testNPE(Runnable r) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1094
        testThrowable(r, NullPointerException.class);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1095
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1096
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1097
    static void testIAE(Runnable r) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1098
        testThrowable(r, IllegalArgumentException.class);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1099
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1100
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1101
    static void testAIOBE(Runnable r) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1102
        testThrowable(r, ArrayIndexOutOfBoundsException.class);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1103
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1104
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1105
    static void testThrowable(Runnable r, Class<? extends Throwable> expected) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1106
        Throwable caught = null;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1107
        try {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1108
            r.run();
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1109
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1110
        catch (Throwable t) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1111
            caught = t;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1112
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1113
        Assert.assertNotNull(caught);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1114
        Assert.assertTrue(expected.isInstance(caught));
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1115
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents:
diff changeset
  1116
}