1/*
2 * Copyright (C) 2014 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
17package dalvik.system;
18
19import java.lang.reflect.Array;
20import junit.framework.TestCase;
21
22/**
23 * Test VMRuntime behavior.
24 */
25public final class VMRuntimeTest extends TestCase {
26
27    private void doTestNewNonMovableArray(Class<?> componentType, int step, int maxLength) {
28        // Can't create negative sized arrays.
29        try {
30            Object array = VMRuntime.getRuntime().newNonMovableArray(componentType, -1);
31            assertTrue(false);
32        } catch (NegativeArraySizeException expected) {
33        }
34
35        try {
36            Object array = VMRuntime.getRuntime().newNonMovableArray(componentType, Integer.MIN_VALUE);
37            assertTrue(false);
38        } catch (NegativeArraySizeException expected) {
39        }
40
41        // Allocate arrays in a loop and check their properties.
42        for (int i = 0; i <= maxLength; i += step) {
43            Object array = VMRuntime.getRuntime().newNonMovableArray(componentType, i);
44            assertTrue(array.getClass().isArray());
45            assertEquals(array.getClass().getComponentType(), componentType);
46            assertEquals(Array.getLength(array), i);
47        }
48    }
49
50    public void testNewNonMovableArray() {
51        // Can't create arrays with no component type.
52        try {
53            Object array = VMRuntime.getRuntime().newNonMovableArray(null, 0);
54            assertTrue(false);
55        } catch (NullPointerException expected) {
56        }
57
58        // Can't create arrays of void.
59        try {
60            Object array = VMRuntime.getRuntime().newNonMovableArray(void.class, 0);
61            assertTrue(false);
62        } catch (NoClassDefFoundError expected) {
63        }
64
65        int maxLengthForLoop = 16 * 1024;
66        int step = 67;
67        doTestNewNonMovableArray(boolean.class, step, maxLengthForLoop);
68        doTestNewNonMovableArray(byte.class, step, maxLengthForLoop);
69        doTestNewNonMovableArray(char.class, step, maxLengthForLoop);
70        doTestNewNonMovableArray(short.class, step, maxLengthForLoop);
71        doTestNewNonMovableArray(int.class, step, maxLengthForLoop);
72        doTestNewNonMovableArray(long.class, step, maxLengthForLoop);
73        doTestNewNonMovableArray(float.class, step, maxLengthForLoop);
74        doTestNewNonMovableArray(double.class, step, maxLengthForLoop);
75        doTestNewNonMovableArray(Object.class, step, maxLengthForLoop);
76        doTestNewNonMovableArray(Number.class, step, maxLengthForLoop);
77        doTestNewNonMovableArray(String.class, step, maxLengthForLoop);
78        doTestNewNonMovableArray(Runnable.class, step, maxLengthForLoop);
79    }
80
81    private void doTestNewUnpaddedArray(Class<?> componentType, int step, int maxLength) {
82         // Can't create negative sized arrays.
83        try {
84            Object array = VMRuntime.getRuntime().newUnpaddedArray(componentType, -1);
85            assertTrue(false);
86        } catch (NegativeArraySizeException expected) {
87        }
88
89        try {
90            Object array = VMRuntime.getRuntime().newUnpaddedArray(componentType, Integer.MIN_VALUE);
91            assertTrue(false);
92        } catch (NegativeArraySizeException expected) {
93        }
94
95        // Allocate arrays in a loop and check their properties.
96        for (int i = 0; i <= maxLength; i += step) {
97            Object array = VMRuntime.getRuntime().newUnpaddedArray(componentType, i);
98            assertTrue(array.getClass().isArray());
99            assertEquals(array.getClass().getComponentType(), componentType);
100            assertTrue(Array.getLength(array) >= i);
101        }
102    }
103
104    public void testNewUnpaddedArray() {
105        // Can't create arrays with no component type.
106        try {
107            Object array = VMRuntime.getRuntime().newUnpaddedArray(null, 0);
108            assertTrue(false);
109        } catch (NullPointerException expected) {
110        }
111
112        // Can't create arrays of void.
113        try {
114            Object array = VMRuntime.getRuntime().newUnpaddedArray(void.class, 0);
115            assertTrue(false);
116        } catch (NoClassDefFoundError expected) {
117        }
118
119        int maxLengthForLoop = 16 * 1024;
120        int step = 67;
121        doTestNewUnpaddedArray(boolean.class, step, maxLengthForLoop);
122        doTestNewUnpaddedArray(byte.class, step, maxLengthForLoop);
123        doTestNewUnpaddedArray(char.class, step, maxLengthForLoop);
124        doTestNewUnpaddedArray(short.class, step, maxLengthForLoop);
125        doTestNewUnpaddedArray(int.class, step, maxLengthForLoop);
126        doTestNewUnpaddedArray(long.class, step, maxLengthForLoop);
127        doTestNewUnpaddedArray(float.class, step, maxLengthForLoop);
128        doTestNewUnpaddedArray(double.class, step, maxLengthForLoop);
129        doTestNewUnpaddedArray(Object.class, step, maxLengthForLoop);
130        doTestNewUnpaddedArray(Number.class, step, maxLengthForLoop);
131        doTestNewUnpaddedArray(String.class, step, maxLengthForLoop);
132        doTestNewUnpaddedArray(Runnable.class, step, maxLengthForLoop);
133    }
134}
135
136