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