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     }
27 
testObjectCopy()28     public static void testObjectCopy() {
29         String[] stringArray = new String[8];
30         Object[] objectArray = new Object[8];
31 
32         for (int i = 0; i < stringArray.length; i++)
33             stringArray[i] = new String(Integer.toString(i));
34 
35         System.out.println("string -> object");
36         System.arraycopy(stringArray, 0, objectArray, 0, stringArray.length);
37         System.out.println("object -> string");
38         System.arraycopy(objectArray, 0, stringArray, 0, stringArray.length);
39         System.out.println("object -> string (modified)");
40         objectArray[4] = new ImplA();
41         try {
42             System.arraycopy(objectArray, 0, stringArray, 0,stringArray.length);
43         }
44         catch (ArrayStoreException ase) {
45             System.out.println("caught ArrayStoreException (expected)");
46         }
47     }
48 
49     static final int ARRAY_SIZE = 8;
50 
initByteArray(byte[] array)51     static void initByteArray(byte[] array) {
52         for (int i = 0; i < ARRAY_SIZE; i++) {
53             array[i] = (byte) i;
54         }
55     }
initShortArray(short[] array)56     static void initShortArray(short[] array) {
57         for (int i = 0; i < ARRAY_SIZE; i++) {
58             array[i] = (short) i;
59         }
60     }
initIntArray(int[] array)61     static void initIntArray(int[] array) {
62         for (int i = 0; i < ARRAY_SIZE; i++) {
63             array[i] = (int) i;
64         }
65     }
initLongArray(long[] array)66     static void initLongArray(long[] array) {
67         for (int i = 0; i < ARRAY_SIZE; i++) {
68             array[i] = (long) i;
69         }
70     }
71 
72     /*
73      * Perform an array copy operation on primitive arrays with different
74      * element widths.
75      */
makeCopies(int srcPos, int dstPos, int length)76     static void makeCopies(int srcPos, int dstPos, int length) {
77         byte[] byteArray = new byte[ARRAY_SIZE];
78         short[] shortArray = new short[ARRAY_SIZE];
79         int[] intArray = new int[ARRAY_SIZE];
80         long[] longArray = new long[ARRAY_SIZE];
81 
82         initByteArray(byteArray);
83         initShortArray(shortArray);
84         initIntArray(intArray);
85         initLongArray(longArray);
86 
87         System.arraycopy(byteArray, srcPos, byteArray, dstPos, length);
88         System.arraycopy(shortArray, srcPos, shortArray, dstPos, length);
89         System.arraycopy(intArray, srcPos, intArray, dstPos, length);
90         System.arraycopy(longArray, srcPos, longArray, dstPos, length);
91 
92         for (int i = 0; i < ARRAY_SIZE; i++) {
93             if (intArray[i] != byteArray[i]) {
94                 System.out.println("mismatch int vs byte at " + i + " : " +
95                     Arrays.toString(byteArray));
96                 break;
97             } else if (intArray[i] != shortArray[i]) {
98                 System.out.println("mismatch int vs short at " + i + " : " +
99                     Arrays.toString(shortArray));
100                 break;
101             } else if (intArray[i] != longArray[i]) {
102                 System.out.println("mismatch int vs long at " + i + " : " +
103                     Arrays.toString(longArray));
104                 break;
105             }
106         }
107 
108         System.out.println("copy: " + srcPos + "," + dstPos + "," + length +
109             ": " + Arrays.toString(intArray));
110     }
111 
testOverlappingMoves()112     public static void testOverlappingMoves() {
113         /* do nothing */
114         makeCopies(0, 0, 0);
115 
116         /* do more nothing */
117         makeCopies(0, 0, ARRAY_SIZE);
118 
119         /* copy forward, even alignment */
120         makeCopies(0, 2, 4);
121 
122         /* copy backward, even alignment */
123         makeCopies(2, 0, 4);
124 
125         /* copy forward, odd alignment */
126         makeCopies(1, 3, 4);
127 
128         /* copy backward, odd alignment */
129         makeCopies(3, 1, 4);
130 
131         /* copy backward, odd length */
132         makeCopies(3, 1, 5);
133 
134         /* copy forward, odd length */
135         makeCopies(1, 3, 5);
136 
137         /* copy forward, mixed alignment */
138         makeCopies(0, 3, 5);
139 
140         /* copy backward, mixed alignment */
141         makeCopies(3, 0, 5);
142 
143         /* copy forward, mixed alignment, trivial length */
144         makeCopies(0, 5, 1);
145     }
146 }
147