test/jdk/java/util/Arrays/CopyMethods.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:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug     4655503
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test for array cloning and slicing methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author  John Rose
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 5506
diff changeset
    29
 * @key randomness
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class CopyMethods {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    static int muzzle;  // if !=0, suppresses ("muzzles") messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    static int maxLen = 40;  // maximum length of test arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    static int shortStepsNear = 4;  // interesting span near critical values
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static int downShift = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static int testCasesRun = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static long consing = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    // very simple tests, mainly to test the framework itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static void simpleTests() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        int[] a = (int[]) makeArray(3, int.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        if (muzzle == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            System.out.println("int[] a = "+Arrays.toString(a));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        check(a.length == 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        check(a[0] == testValues[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        check(a[1] == testValues[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        check(a[2] == testValues[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        checkArray(a, int.class, 3, 0, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        // negative test of testing framework:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        for (int bad = -2; bad < a.length; bad++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                int[] aa = a.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                if (bad < 0)  aa = new int[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                else          aa[bad] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                ++muzzle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                // the following check should fail!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                if (bad == -2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                    checkArray(new String[3], int.class, 0, 0, a.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                    checkArray(aa, int.class, 0, 0, a.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                throw new Error("Should Not Reach Here");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            } catch (RuntimeException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                --muzzle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                if (muzzle == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                    System.out.println("Expected: "+ee);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        checkArray(Arrays.copyOf(a, 0), int.class, 0, 0, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        checkArray(Arrays.copyOf(a, 1), int.class, 1, 0, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        checkArray(Arrays.copyOf(a, 2), int.class, 2, 0, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        checkArray(Arrays.copyOf(a, 3), int.class, 3, 0, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        checkArray(Arrays.copyOf(a, 4), int.class, 4, 0, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        // quick test of copyOfRange
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        int[] ar = Arrays.copyOfRange(a, 1, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        check(ar.length == 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        check(ar[0] == a[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        check(ar[1] == a[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        checkArray(ar, int.class, 2, 1, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        ar = Arrays.copyOfRange(a, 2, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        check(ar.length == 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        check(ar[0] == a[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        check(ar[1] == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        checkArray(ar, int.class, 2, 2, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        ar = Arrays.copyOfRange(a, 3, 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        check(ar.length == 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        check(ar[0] == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        check(ar[1] == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        checkArray(ar, int.class, 2, 3, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        byte[] ba = (byte[]) makeArray(3, byte.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (muzzle == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            System.out.println("byte[] ba = "+Arrays.toString(ba));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        for (int j = 0; j <= ba.length+2; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            byte[] bb = Arrays.copyOf(ba, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            if (muzzle == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                System.out.println("copyOf(ba,"+j+") = "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                   Arrays.toString(bb));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            checkArray(bb, byte.class, j, 0, ba.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            byte[] bbr = Arrays.copyOfRange(ba, 0, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            check(Arrays.equals(bb, bbr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        for (int i = 0; i <= a.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            for (int j = i; j <= a.length+2; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                byte[] br = Arrays.copyOfRange(ba, i, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                if (muzzle == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    System.out.println("copyOfRange(ba,"+i+","+j+") = "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                       Arrays.toString(br));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                checkArray(br, byte.class, j-i, i, ba.length-i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        String[] sa = (String[]) makeArray(3, String.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (muzzle == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            System.out.println("String[] sa = "+Arrays.toString(sa));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        check(sa[0].equals(Integer.toHexString(testValues[0])));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        check(sa[1].equals(Integer.toHexString(testValues[1])));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        check(sa[2].equals(Integer.toHexString(testValues[2])));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        checkArray(sa, String.class, sa.length, 0, sa.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        String[] sa4 = Arrays.copyOf(sa, sa.length+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        check(sa4[0] == sa[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        check(sa4[1] == sa[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        check(sa4[2] == sa[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        check(sa4[sa.length] == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        checkArray(sa4, String.class, sa4.length, 0, sa.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        String[] sr4 = Arrays.copyOfRange(sa, 1, 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        check(sr4[0] == sa[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        check(sr4[1] == sa[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        check(sr4[2] == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        check(sr4[3] == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        checkArray(sr4, String.class, 4, 1, sa.length-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (muzzle == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            System.out.println("simpleTests done");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    // the framework:  a fixed series of test values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    static final int[] testValues;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        testValues = new int[1000];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        Random r = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        for (int i = 0; i < testValues.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            testValues[i] = r.nextInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /** Return a canonical test value of a desired index and type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *  The original test values are random ints.  Derive other test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *  values as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *  <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *  int tv = testValues[i]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *  (C)tv                    C is byte, short, char, long, float, double
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *  (tv&1)!=0                C is boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *  (Integer)tv              C is Object and tv%16 != 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *  null                     C is Object and tv%16 == 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *  Integer.toHexString(tv)  C is String and tv != 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *  null                     C is String and tv == 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *  </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *  are derived by ordinary Java coercions, except that boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *  samples the LSB of the int value, and String is the hex numeral.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *  (Also, the 0th String is null, and the 0th Object mod 16 is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *  regardless of the original int test value.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    static Object testValue(int i, Class<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        int tv = testValues[i % testValues.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (i >= testValues.length)  tv ^= i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        // Turn the canonical int to a float, boolean, String, whatever:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return invoke(coercers.get(c), tv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /** Build a test array of the given length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *  packed with a subsequence of the test values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *  The first element of the array is always testValue(0).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    static Object makeArray(int len, Class<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        Object a = Array.newInstance(c, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            Array.set(a, i, testValue(i, c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /** Check that the given array has the required length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *  Check also that it is packed, up to firstNull, with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *  a particular subsequence of the canonical test values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *  The subsequence must begin with a[0] == testValue(offset).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *  At a[firstNull] and beyond, the array must contain null values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    static void checkArray(Object a, Class<?> c, int requiredLen, int offset, int firstNull) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        check(c == a.getClass().getComponentType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        Object nullValue = nullValues.get(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        // Note:  asserts in here are not part of the test program.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        // They verify the integrity of the test method itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        assert(nullValues.containsKey(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        int misses = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        int firstMiss = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        // Check required length first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        int length = Array.getLength(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        if (length != requiredLen && requiredLen != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            if (muzzle == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                System.out.println("*** a.length = "+length+" != "+requiredLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            ++misses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        for (int i = 0; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            Object tv = (i >= firstNull) ? nullValue : testValue(i+offset, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            Object ai = Array.get(a, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            if (!eq(ai, tv)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                if (muzzle == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    System.out.println("*** a["+i+"] = "+ai+" != "+tv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                if (misses == 0)  firstMiss = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                if (++misses > 10)  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (misses != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            Method toString = toStrings.get(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            if (toString == null)  toString = toStrings.get(Object.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            throw new RuntimeException("checkArray failed at "+firstMiss
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                                       +" "+c+"[]"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                                       +" : "+invoke(toString, a));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    // Typical comparison helper.  Why isn't this a method somewhere.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    static boolean eq(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        return x == null? y == null: x.equals(y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    // Exception-ignoring invoke function.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    static Object invoke(Method m, Object... args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        Exception ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            return m.invoke(null, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        } catch (InvocationTargetException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            ex = ee;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        } catch (IllegalAccessException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            ex = ee;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        } catch (IllegalArgumentException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            ex = ee;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        ArrayList<Object> call = new ArrayList<Object>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        call.add(m); Collections.addAll(call, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        throw new RuntimeException(call+" : "+ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    // version of assert() that runs unconditionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    static void check(boolean z) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if (!z)  throw new RuntimeException("check failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /** Run about 10**5 distinct parameter combinations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *  on copyOf and copyOfRange.  Use all primitive types,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *  and String and Object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *  Try to all critical values, looking for fencepost errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    static void fullTests(int maxLen, Class<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        Method cloner      = cloners.get(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        assert(cloner != null) : c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        Method cloneRanger = cloneRangers.get(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        // Note:  asserts in here are not part of the test program.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        // They verify the integrity of the test method itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        assert(cloneRanger != null) : c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        for (int src = 0; src <= maxLen; src = inc(src, 0, maxLen)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            Object a = makeArray(src, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            for (int x : new ArrayList<Integer>()) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            for (int j = 0; j <= maxLen; j = inc(j, src, maxLen)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                // b = Arrays.copyOf(a, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                Object b = invoke(cloner, a, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                checkArray(b, c, j, 0, src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                testCasesRun++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                consing += j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                int maxI = Math.min(src, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                for (int i = 0; i <= maxI; i = inc(i, src, maxI)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    // r = Arrays.copyOfRange(a, i, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    Object r = invoke(cloneRanger, a, i, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    checkArray(r, c, j-i, i, src-i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    //System.out.println("case c="+c+" src="+src+" i="+i+" j="+j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    testCasesRun++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    consing += j-i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    // Increment x by at least one.  Increment by a little more unless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    // it is near a critical value, either zero, crit1, or crit2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    static int inc(int x, int crit1, int crit2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        int D = shortStepsNear;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (crit1 > crit2) { int t = crit1; crit1 = crit2; crit2 = t; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        assert(crit1 <= crit2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        assert(x <= crit2);  // next1 or next2 must be the limit value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        x += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        if (x > D) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            if (x < crit1-D) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                x += (x << 1) >> downShift;  // giant step toward crit1-D
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                if (x > crit1-D)  x = crit1-D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            } else if (x >= crit1+D && x < crit2-D) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                x += (x << 1) >> downShift;  // giant step toward crit2-D
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                if (x > crit2-D)  x = crit2-D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public static void main(String[] av) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        boolean verbose = (av.length != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        muzzle = (verbose? 0: 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        if (muzzle == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            System.out.println("test values: "+Arrays.toString(Arrays.copyOf(testValues, 5))+"...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        simpleTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        muzzle = 0;  // turn on print statements (affects failures only)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        fullTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (verbose)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            System.out.println("ran "+testCasesRun+" tests, avg len="
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                               +(float)consing/testCasesRun);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        // test much larger arrays, more sparsely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        maxLen = 500;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        shortStepsNear = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        downShift = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        testCasesRun = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        consing = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        fullTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        if (verbose)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            System.out.println("ran "+testCasesRun+" tests, avg len="
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                               +(float)consing/testCasesRun);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    static void fullTests() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        for (Class<?> c : allTypes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            fullTests(maxLen, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    // We must run all the our tests on each of 8 distinct primitive types,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    // and two reference types (Object, String) for good measure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    // This would be a pain to write out by hand, statically typed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    // So, use reflection.  Following are the tables of methods we use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    // (The initial simple tests exercise enough of the static typing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    // features of the API to ensure that they compile as advertised.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    static Object  coerceToObject(int x) { return (x & 0xF) == 0? null: new Integer(x); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    static String  coerceToString(int x) { return (x == 0)? null: Integer.toHexString(x); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    static Integer coerceToInteger(int x) { return (x == 0)? null: x; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    static byte    coerceToByte(int x) { return (byte)x; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    static short   coerceToShort(int x) { return (short)x; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    static int     coerceToInt(int x) { return x; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    static long    coerceToLong(int x) { return x; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    static char    coerceToChar(int x) { return (char)x; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    static float   coerceToFloat(int x) { return x; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    static double  coerceToDouble(int x) { return x; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    static boolean coerceToBoolean(int x) { return (x&1) != 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    static Integer[] copyOfIntegerArray(Object[] a, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        // This guy exercises the API based on a type-token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        // Note the static typing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        return Arrays.copyOf(a, len, Integer[].class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    static Integer[] copyOfIntegerArrayRange(Object[] a, int m, int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        // This guy exercises the API based on a type-token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        // Note the static typing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        return Arrays.copyOfRange(a, m, n, Integer[].class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    static final List<Class<?>> allTypes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        = Arrays.asList(new Class<?>[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                        {   Object.class, String.class, Integer.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                            byte.class, short.class, int.class, long.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                            char.class, float.class, double.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                            boolean.class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    static final HashMap<Class<?>,Method> coercers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    static final HashMap<Class<?>,Method> cloners;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    static final HashMap<Class<?>,Method> cloneRangers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    static final HashMap<Class<?>,Method> toStrings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    static final HashMap<Class<?>,Object> nullValues;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        coercers = new HashMap<Class<?>,Method>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        Method[] testMethods = CopyMethods.class.getDeclaredMethods();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        Method cia = null, ciar = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        for (int i = 0; i < testMethods.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            Method m = testMethods[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            if (!Modifier.isStatic(m.getModifiers()))  continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            Class<?> rt = m.getReturnType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            if (m.getName().startsWith("coerceTo") && allTypes.contains(rt))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                coercers.put(m.getReturnType(), m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            if (m.getName().equals("copyOfIntegerArray"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                cia = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            if (m.getName().equals("copyOfIntegerArrayRange"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                ciar = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        Method[] arrayMethods = Arrays.class.getDeclaredMethods();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        cloners      = new HashMap<Class<?>,Method>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        cloneRangers = new HashMap<Class<?>,Method>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        toStrings    = new HashMap<Class<?>,Method>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        for (int i = 0; i < arrayMethods.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            Method m = arrayMethods[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            if (!Modifier.isStatic(m.getModifiers()))  continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            Class<?> rt = m.getReturnType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            if (m.getName().equals("copyOf")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                && m.getParameterTypes().length == 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                cloners.put(rt.getComponentType(), m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            if (m.getName().equals("copyOfRange")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                && m.getParameterTypes().length == 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                cloneRangers.put(rt.getComponentType(), m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            if (m.getName().equals("toString")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                Class<?> pt = m.getParameterTypes()[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                toStrings.put(pt.getComponentType(), m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        cloners.put(String.class, cloners.get(Object.class));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        cloneRangers.put(String.class, cloneRangers.get(Object.class));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        assert(cia != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        cloners.put(Integer.class, cia);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        assert(ciar != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        cloneRangers.put(Integer.class, ciar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        nullValues = new HashMap<Class<?>,Object>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        for (Class<?> c : allTypes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            nullValues.put(c, invoke(coercers.get(c), 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
}