• 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         testArrayCopyChar();
28     }
29 
testObjectCopy()30     public static void testObjectCopy() {
31         String[] stringArray = new String[8];
32         Object[] objectArray = new Object[8];
33 
34         for (int i = 0; i < stringArray.length; i++)
35             stringArray[i] = new String(Integer.toString(i));
36 
37         System.out.println("string -> object");
38         System.arraycopy(stringArray, 0, objectArray, 0, stringArray.length);
39         System.out.println("object -> string");
40         System.arraycopy(objectArray, 0, stringArray, 0, stringArray.length);
41         System.out.println("object -> string (modified)");
42         objectArray[4] = new ImplA();
43         try {
44             System.arraycopy(objectArray, 0, stringArray, 0,stringArray.length);
45         }
46         catch (ArrayStoreException ase) {
47             System.out.println("caught ArrayStoreException (expected)");
48         }
49     }
50 
51     static final int ARRAY_SIZE = 8;
52 
initByteArray(byte[] array)53     static void initByteArray(byte[] array) {
54         for (int i = 0; i < ARRAY_SIZE; i++) {
55             array[i] = (byte) i;
56         }
57     }
initShortArray(short[] array)58     static void initShortArray(short[] array) {
59         for (int i = 0; i < ARRAY_SIZE; i++) {
60             array[i] = (short) i;
61         }
62     }
initIntArray(int[] array)63     static void initIntArray(int[] array) {
64         for (int i = 0; i < ARRAY_SIZE; i++) {
65             array[i] = (int) i;
66         }
67     }
initLongArray(long[] array)68     static void initLongArray(long[] array) {
69         for (int i = 0; i < ARRAY_SIZE; i++) {
70             array[i] = (long) i;
71         }
72     }
initCharArray(char[] array)73     static void initCharArray(char[] array) {
74         for (int i = 0; i < ARRAY_SIZE; i++) {
75             array[i] = (char) i;
76         }
77     }
78 
79     /*
80      * Perform an array copy operation on primitive arrays with different
81      * element widths.
82      */
makeCopies(int srcPos, int dstPos, int length)83     static void makeCopies(int srcPos, int dstPos, int length) {
84         byte[] byteArray = new byte[ARRAY_SIZE];
85         short[] shortArray = new short[ARRAY_SIZE];
86         int[] intArray = new int[ARRAY_SIZE];
87         long[] longArray = new long[ARRAY_SIZE];
88         char[] charArray = new char[ARRAY_SIZE];
89 
90         initByteArray(byteArray);
91         initShortArray(shortArray);
92         initIntArray(intArray);
93         initLongArray(longArray);
94         initCharArray(charArray);
95 
96         System.arraycopy(byteArray, srcPos, byteArray, dstPos, length);
97         System.arraycopy(shortArray, srcPos, shortArray, dstPos, length);
98         System.arraycopy(intArray, srcPos, intArray, dstPos, length);
99         System.arraycopy(longArray, srcPos, longArray, dstPos, length);
100         System.arraycopy(charArray, srcPos, charArray, dstPos, length);
101 
102         for (int i = 0; i < ARRAY_SIZE; i++) {
103             if (intArray[i] != byteArray[i]) {
104                 System.out.println("mismatch int vs byte at " + i + " : " +
105                     Arrays.toString(byteArray));
106                 break;
107             } else if (intArray[i] != shortArray[i]) {
108                 System.out.println("mismatch int vs short at " + i + " : " +
109                     Arrays.toString(shortArray));
110                 break;
111             } else if (intArray[i] != longArray[i]) {
112                 System.out.println("mismatch int vs long at " + i + " : " +
113                     Arrays.toString(longArray));
114                 break;
115             } else if (intArray[i] != charArray[i]) {
116                 System.out.println("mismatch int vs char at " + i + " : " +
117                     Arrays.toString(charArray));
118                 break;
119             }
120         }
121 
122         System.out.println("copy: " + srcPos + "," + dstPos + "," + length +
123             ": " + Arrays.toString(intArray));
124     }
125 
testOverlappingMoves()126     public static void testOverlappingMoves() {
127         /* do nothing */
128         makeCopies(0, 0, 0);
129 
130         /* do more nothing */
131         makeCopies(0, 0, ARRAY_SIZE);
132 
133         /* copy forward, even alignment */
134         makeCopies(0, 2, 4);
135 
136         /* copy backward, even alignment */
137         makeCopies(2, 0, 4);
138 
139         /* copy forward, odd alignment */
140         makeCopies(1, 3, 4);
141 
142         /* copy backward, odd alignment */
143         makeCopies(3, 1, 4);
144 
145         /* copy backward, odd length */
146         makeCopies(3, 1, 5);
147 
148         /* copy forward, odd length */
149         makeCopies(1, 3, 5);
150 
151         /* copy forward, mixed alignment */
152         makeCopies(0, 3, 5);
153 
154         /* copy backward, mixed alignment */
155         makeCopies(3, 0, 5);
156 
157         /* copy forward, mixed alignment, trivial length */
158         makeCopies(0, 5, 1);
159     }
160 
testFloatAndDouble()161     private static void testFloatAndDouble() {
162         // Float & double copies have the same implementation as int & long. However, there are
163         // protective DCHECKs in the code (there is nothing unifying like ByteSizedArray or
164         // ShortSizedArray). Just test that we don't fail those checks.
165         final int len = 10;
166         System.arraycopy(new float[len], 0, new float[len], 0, len);
167         System.arraycopy(new double[len], 0, new double[len], 0, len);
168     }
169 
170     static final char SRC_INIT_CHAR = '1';
171     static final char DST_CHAR = '0';
172 
173     /* Return a char array of the specified length.
174      * If do_increment is true, populate the array with (numerically) ascending
175      * characters starting from initChar (note: char wraps-around on overflow).
176      * If do_increment is false, populate all array elements with initChar.
177      */
createCharArray(int length, char initChar, boolean do_increment)178     public static char[] createCharArray(int length, char initChar, boolean do_increment) {
179         char[] charArr = new char[length];
180         char nextChar = initChar;
181 
182         for (int i = 0; i < length; ++i) {
183             charArr[i] = nextChar;
184             if (do_increment) {
185                 nextChar++;
186             }
187         }
188         return charArr;
189     }
190 
verifyCorrectness(char[] src, char[] dst, int copiedPrefixLength)191     public static boolean verifyCorrectness(char[] src, char[] dst, int copiedPrefixLength) {
192         for (int i = 0; i < dst.length; ++i) {
193             if (i < copiedPrefixLength) {
194                 // Check that we copied source array.
195                 if (dst[i] != src[i]) {
196                     return false;
197                 }
198             } else {
199                 // Check that we didn't write more chars than necessary.
200                 if (dst[i] != DST_CHAR) {
201                     return false;
202                 }
203             }
204         }
205         return true;
206     }
207 
testArrayCopyCharConstCase2()208     public static void testArrayCopyCharConstCase2() {
209         final int copy_length = 2;
210         char[] src = createCharArray(2 * copy_length, SRC_INIT_CHAR, true);
211         char[] dst = createCharArray(4 * copy_length, DST_CHAR, false);
212 
213         System.arraycopy(src, 0, dst, 0, copy_length);
214 
215         boolean passed = verifyCorrectness(src, dst, copy_length);
216         if (!passed) {
217             System.out.println("arraycopy(char) const case 2 failed");
218         } else {
219             System.out.println("arraycopy(char) const case 2 passed");
220         }
221     }
222 
testArrayCopyCharConstCase3()223     public static void testArrayCopyCharConstCase3() {
224         final int copy_length = 3;
225         char[] src = createCharArray(2 * copy_length, SRC_INIT_CHAR, true);
226         char[] dst = createCharArray(4 * copy_length, DST_CHAR, false);
227 
228         System.arraycopy(src, 0, dst, 0, copy_length);
229 
230         boolean passed = verifyCorrectness(src, dst, copy_length);
231         if (!passed) {
232             System.out.println("arraycopy(char) const case 3 failed");
233         } else {
234             System.out.println("arraycopy(char) const case 3 passed");
235         }
236     }
237 
testArrayCopyCharConstCase5()238     public static void testArrayCopyCharConstCase5() {
239         final int copy_length = 5;
240         char[] src = createCharArray(2 * copy_length, SRC_INIT_CHAR, true);
241         char[] dst = createCharArray(4 * copy_length, DST_CHAR, false);
242 
243         System.arraycopy(src, 0, dst, 0, copy_length);
244 
245         boolean passed = verifyCorrectness(src, dst, copy_length);
246         if (!passed) {
247             System.out.println("arraycopy(char) const case 5 failed");
248         } else {
249             System.out.println("arraycopy(char) const case 5 passed");
250         }
251     }
252 
testArrayCopyCharConstCase7()253     public static void testArrayCopyCharConstCase7() {
254         final int copy_length = 7;
255         char[] src = createCharArray(2 * copy_length, SRC_INIT_CHAR, true);
256         char[] dst = createCharArray(4 * copy_length, DST_CHAR, false);
257 
258         System.arraycopy(src, 0, dst, 0, copy_length);
259 
260         boolean passed = verifyCorrectness(src, dst, copy_length);
261         if (!passed) {
262             System.out.println("arraycopy(char) const case 7 failed");
263         } else {
264             System.out.println("arraycopy(char) const case 7 passed");
265         }
266     }
267 
testArrayCopyCharConstCase8()268     public static void testArrayCopyCharConstCase8() {
269         final int copy_length = 8;
270         char[] src = createCharArray(2 * copy_length, SRC_INIT_CHAR, true);
271         char[] dst = createCharArray(4 * copy_length, DST_CHAR, false);
272 
273         System.arraycopy(src, 0, dst, 0, copy_length);
274 
275         boolean passed = verifyCorrectness(src, dst, copy_length);
276         if (!passed) {
277             System.out.println("arraycopy(char) const case 8 failed");
278         } else {
279             System.out.println("arraycopy(char) const case 8 passed");
280         }
281     }
282 
testArrayCopyCharConstCase9()283     public static void testArrayCopyCharConstCase9() {
284         final int copy_length = 9;
285         char[] src = createCharArray(2 * copy_length, SRC_INIT_CHAR, true);
286         char[] dst = createCharArray(4 * copy_length, DST_CHAR, false);
287 
288         System.arraycopy(src, 0, dst, 0, copy_length);
289 
290         boolean passed = verifyCorrectness(src, dst, copy_length);
291         if (!passed) {
292             System.out.println("arraycopy(char) const case 9 failed");
293         } else {
294             System.out.println("arraycopy(char) const case 9 passed");
295         }
296     }
297 
testArrayCopyCharConstCase11()298     public static void testArrayCopyCharConstCase11() {
299         final int copy_length = 11;
300         char[] src = createCharArray(2 * copy_length, SRC_INIT_CHAR, true);
301         char[] dst = createCharArray(4 * copy_length, DST_CHAR, false);
302 
303         System.arraycopy(src, 0, dst, 0, copy_length);
304 
305         boolean passed = verifyCorrectness(src, dst, copy_length);
306         if (!passed) {
307             System.out.println("arraycopy(char) const case 11 failed");
308         } else {
309             System.out.println("arraycopy(char) const case 11 passed");
310         }
311     }
312 
testArrayCopyCharCase(int copy_length)313     public static void testArrayCopyCharCase(int copy_length) {
314         char[] src = createCharArray(2 * copy_length, SRC_INIT_CHAR, true);
315         char[] dst = createCharArray(4 * copy_length, DST_CHAR, false);
316 
317         System.arraycopy(src, 0, dst, 0, copy_length);
318 
319         boolean passed = verifyCorrectness(src, dst, copy_length);
320         if (!passed) {
321             System.out.println("arraycopy(char) " + copy_length + " failed");
322         } else {
323             System.out.println("arraycopy(char) " + copy_length + " passed");
324         }
325     }
326 
testArrayCopyChar()327     public static void testArrayCopyChar() {
328         testArrayCopyCharConstCase2();
329         testArrayCopyCharConstCase3();
330         testArrayCopyCharConstCase5();
331         testArrayCopyCharConstCase7();
332         testArrayCopyCharConstCase8();
333         testArrayCopyCharConstCase9();
334         testArrayCopyCharConstCase11();
335         testArrayCopyCharCase(0);
336         testArrayCopyCharCase(1);
337         testArrayCopyCharCase(3);
338         testArrayCopyCharCase(4);
339         testArrayCopyCharCase(5);
340         testArrayCopyCharCase(7);
341         testArrayCopyCharCase(15);
342         testArrayCopyCharCase(16);
343         testArrayCopyCharCase(17);
344         testArrayCopyCharCase(31);
345         testArrayCopyCharCase(32);
346         testArrayCopyCharCase(33);
347         testArrayCopyCharCase(63);
348         testArrayCopyCharCase(64);
349         testArrayCopyCharCase(65);
350         testArrayCopyCharCase(255);
351         testArrayCopyCharCase(513);
352         testArrayCopyCharCase(1025);
353     }
354 
355 }
356