blob: 96e1dbf21a7f585e04dd2106131087f6b74f95aa [file] [log] [blame]
jeffhao5d1ac922011-09-29 17:41:15 -07001/*
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
17import java.util.Arrays;
18
19/**
20 * System.arraycopy cases
21 */
22public class Main {
23 public static void main(String args[]) {
24 testObjectCopy();
25 testOverlappingMoves();
Andreas Gampec952ac92015-07-16 17:41:25 -070026 testFloatAndDouble();
jeffhao5d1ac922011-09-29 17:41:15 -070027 }
28
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
52 static void initByteArray(byte[] array) {
53 for (int i = 0; i < ARRAY_SIZE; i++) {
54 array[i] = (byte) i;
55 }
56 }
57 static void initShortArray(short[] array) {
58 for (int i = 0; i < ARRAY_SIZE; i++) {
59 array[i] = (short) i;
60 }
61 }
62 static void initIntArray(int[] array) {
63 for (int i = 0; i < ARRAY_SIZE; i++) {
64 array[i] = (int) i;
65 }
66 }
67 static void initLongArray(long[] array) {
68 for (int i = 0; i < ARRAY_SIZE; i++) {
69 array[i] = (long) i;
70 }
71 }
72
73 /*
74 * Perform an array copy operation on primitive arrays with different
75 * element widths.
76 */
77 static void makeCopies(int srcPos, int dstPos, int length) {
78 byte[] byteArray = new byte[ARRAY_SIZE];
79 short[] shortArray = new short[ARRAY_SIZE];
80 int[] intArray = new int[ARRAY_SIZE];
81 long[] longArray = new long[ARRAY_SIZE];
82
83 initByteArray(byteArray);
84 initShortArray(shortArray);
85 initIntArray(intArray);
86 initLongArray(longArray);
87
88 System.arraycopy(byteArray, srcPos, byteArray, dstPos, length);
89 System.arraycopy(shortArray, srcPos, shortArray, dstPos, length);
90 System.arraycopy(intArray, srcPos, intArray, dstPos, length);
91 System.arraycopy(longArray, srcPos, longArray, dstPos, length);
92
93 for (int i = 0; i < ARRAY_SIZE; i++) {
94 if (intArray[i] != byteArray[i]) {
95 System.out.println("mismatch int vs byte at " + i + " : " +
96 Arrays.toString(byteArray));
97 break;
98 } else if (intArray[i] != shortArray[i]) {
99 System.out.println("mismatch int vs short at " + i + " : " +
100 Arrays.toString(shortArray));
101 break;
102 } else if (intArray[i] != longArray[i]) {
103 System.out.println("mismatch int vs long at " + i + " : " +
104 Arrays.toString(longArray));
105 break;
106 }
107 }
108
109 System.out.println("copy: " + srcPos + "," + dstPos + "," + length +
110 ": " + Arrays.toString(intArray));
111 }
112
113 public static void testOverlappingMoves() {
114 /* do nothing */
115 makeCopies(0, 0, 0);
116
117 /* do more nothing */
118 makeCopies(0, 0, ARRAY_SIZE);
119
120 /* copy forward, even alignment */
121 makeCopies(0, 2, 4);
122
123 /* copy backward, even alignment */
124 makeCopies(2, 0, 4);
125
126 /* copy forward, odd alignment */
127 makeCopies(1, 3, 4);
128
129 /* copy backward, odd alignment */
130 makeCopies(3, 1, 4);
131
132 /* copy backward, odd length */
133 makeCopies(3, 1, 5);
134
135 /* copy forward, odd length */
136 makeCopies(1, 3, 5);
137
138 /* copy forward, mixed alignment */
139 makeCopies(0, 3, 5);
140
141 /* copy backward, mixed alignment */
142 makeCopies(3, 0, 5);
143
144 /* copy forward, mixed alignment, trivial length */
145 makeCopies(0, 5, 1);
146 }
Andreas Gampec952ac92015-07-16 17:41:25 -0700147
148 private static void testFloatAndDouble() {
149 // Float & double copies have the same implementation as int & long. However, there are
150 // protective DCHECKs in the code (there is nothing unifying like ByteSizedArray or
151 // ShortSizedArray). Just test that we don't fail those checks.
152 final int len = 10;
153 System.arraycopy(new float[len], 0, new float[len], 0, len);
154 System.arraycopy(new double[len], 0, new double[len], 0, len);
155 }
jeffhao5d1ac922011-09-29 17:41:15 -0700156}