• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  /*
2   * Copyright (C) 2007 The Android Open Source Project
3   *
4   * Licensed under the Apache License, Version 2.0 (the "License");
5   * you may not use this file except in compliance with the License.
6   * You may obtain a copy of the License at
7   *
8   *      http://www.apache.org/licenses/LICENSE-2.0
9   *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  import java.util.Arrays;
18  
19  /**
20   * System.arraycopy cases
21   */
22  public class Main {
main(String args[])23      public static void main(String args[]) {
24          testObjectCopy();
25          testOverlappingMoves();
26          testFloatAndDouble();
27      }
28  
testObjectCopy()29      public static void testObjectCopy() {
30          String[] stringArray = new String[8];
31          Object[] objectArray = new Object[8];
32  
33          for (int i = 0; i < stringArray.length; i++)
34              stringArray[i] = new String(Integer.toString(i));
35  
36          System.out.println("string -> object");
37          System.arraycopy(stringArray, 0, objectArray, 0, stringArray.length);
38          System.out.println("object -> string");
39          System.arraycopy(objectArray, 0, stringArray, 0, stringArray.length);
40          System.out.println("object -> string (modified)");
41          objectArray[4] = new ImplA();
42          try {
43              System.arraycopy(objectArray, 0, stringArray, 0,stringArray.length);
44          }
45          catch (ArrayStoreException ase) {
46              System.out.println("caught ArrayStoreException (expected)");
47          }
48      }
49  
50      static final int ARRAY_SIZE = 8;
51  
initByteArray(byte[] array)52      static void initByteArray(byte[] array) {
53          for (int i = 0; i < ARRAY_SIZE; i++) {
54              array[i] = (byte) i;
55          }
56      }
initShortArray(short[] array)57      static void initShortArray(short[] array) {
58          for (int i = 0; i < ARRAY_SIZE; i++) {
59              array[i] = (short) i;
60          }
61      }
initIntArray(int[] array)62      static void initIntArray(int[] array) {
63          for (int i = 0; i < ARRAY_SIZE; i++) {
64              array[i] = (int) i;
65          }
66      }
initLongArray(long[] array)67      static void initLongArray(long[] array) {
68          for (int i = 0; i < ARRAY_SIZE; i++) {
69              array[i] = (long) i;
70          }
71      }
initCharArray(char[] array)72      static void initCharArray(char[] array) {
73          for (int i = 0; i < ARRAY_SIZE; i++) {
74              array[i] = (char) i;
75          }
76      }
77  
78      /*
79       * Perform an array copy operation on primitive arrays with different
80       * element widths.
81       */
makeCopies(int srcPos, int dstPos, int length)82      static void makeCopies(int srcPos, int dstPos, int length) {
83          byte[] byteArray = new byte[ARRAY_SIZE];
84          short[] shortArray = new short[ARRAY_SIZE];
85          int[] intArray = new int[ARRAY_SIZE];
86          long[] longArray = new long[ARRAY_SIZE];
87          char[] charArray = new char[ARRAY_SIZE];
88  
89          initByteArray(byteArray);
90          initShortArray(shortArray);
91          initIntArray(intArray);
92          initLongArray(longArray);
93          initCharArray(charArray);
94  
95          System.arraycopy(byteArray, srcPos, byteArray, dstPos, length);
96          System.arraycopy(shortArray, srcPos, shortArray, dstPos, length);
97          System.arraycopy(intArray, srcPos, intArray, dstPos, length);
98          System.arraycopy(longArray, srcPos, longArray, dstPos, length);
99          System.arraycopy(charArray, srcPos, charArray, dstPos, length);
100  
101          for (int i = 0; i < ARRAY_SIZE; i++) {
102              if (intArray[i] != byteArray[i]) {
103                  System.out.println("mismatch int vs byte at " + i + " : " +
104                      Arrays.toString(byteArray));
105                  break;
106              } else if (intArray[i] != shortArray[i]) {
107                  System.out.println("mismatch int vs short at " + i + " : " +
108                      Arrays.toString(shortArray));
109                  break;
110              } else if (intArray[i] != longArray[i]) {
111                  System.out.println("mismatch int vs long at " + i + " : " +
112                      Arrays.toString(longArray));
113                  break;
114              } else if (intArray[i] != charArray[i]) {
115                  System.out.println("mismatch int vs char at " + i + " : " +
116                      Arrays.toString(charArray));
117                  break;
118              }
119          }
120  
121          System.out.println("copy: " + srcPos + "," + dstPos + "," + length +
122              ": " + Arrays.toString(intArray));
123      }
124  
testOverlappingMoves()125      public static void testOverlappingMoves() {
126          /* do nothing */
127          makeCopies(0, 0, 0);
128  
129          /* do more nothing */
130          makeCopies(0, 0, ARRAY_SIZE);
131  
132          /* copy forward, even alignment */
133          makeCopies(0, 2, 4);
134  
135          /* copy backward, even alignment */
136          makeCopies(2, 0, 4);
137  
138          /* copy forward, odd alignment */
139          makeCopies(1, 3, 4);
140  
141          /* copy backward, odd alignment */
142          makeCopies(3, 1, 4);
143  
144          /* copy backward, odd length */
145          makeCopies(3, 1, 5);
146  
147          /* copy forward, odd length */
148          makeCopies(1, 3, 5);
149  
150          /* copy forward, mixed alignment */
151          makeCopies(0, 3, 5);
152  
153          /* copy backward, mixed alignment */
154          makeCopies(3, 0, 5);
155  
156          /* copy forward, mixed alignment, trivial length */
157          makeCopies(0, 5, 1);
158      }
159  
testFloatAndDouble()160      private static void testFloatAndDouble() {
161          // Float & double copies have the same implementation as int & long. However, there are
162          // protective DCHECKs in the code (there is nothing unifying like ByteSizedArray or
163          // ShortSizedArray). Just test that we don't fail those checks.
164          final int len = 10;
165          System.arraycopy(new float[len], 0, new float[len], 0, len);
166          System.arraycopy(new double[len], 0, new double[len], 0, len);
167      }
168  }
169