summaryrefslogtreecommitdiffstats
path: root/luni/src/test/java/dalvik/system/VMRuntimeTest.java
blob: 251ecd841f95fb00169b17c8a731fdd499a905d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package dalvik.system;

import java.lang.reflect.Array;
import junit.framework.TestCase;

/**
 * Test VMRuntime behavior.
 */
public final class VMRuntimeTest extends TestCase {

    private void doTestNewNonMovableArray(Class<?> componentType, int step, int maxLength) {
        // Can't create negative sized arrays.
        try {
            Object array = VMRuntime.getRuntime().newNonMovableArray(componentType, -1);
            assertTrue(false);
        } catch (NegativeArraySizeException expected) {
        }

        try {
            Object array = VMRuntime.getRuntime().newNonMovableArray(componentType, Integer.MIN_VALUE);
            assertTrue(false);
        } catch (NegativeArraySizeException expected) {
        }

        // Allocate arrays in a loop and check their properties.
        for (int i = 0; i <= maxLength; i += step) {
            Object array = VMRuntime.getRuntime().newNonMovableArray(componentType, i);
            assertTrue(array.getClass().isArray());
            assertEquals(array.getClass().getComponentType(), componentType);
            assertEquals(Array.getLength(array), i);
        }
    }

    public void testNewNonMovableArray() {
        // Can't create arrays with no component type.
        try {
            Object array = VMRuntime.getRuntime().newNonMovableArray(null, 0);
            assertTrue(false);
        } catch (NullPointerException expected) {
        }

        // Can't create arrays of void.
        try {
            Object array = VMRuntime.getRuntime().newNonMovableArray(void.class, 0);
            assertTrue(false);
        } catch (NoClassDefFoundError expected) {
        }

        int maxLengthForLoop = 16 * 1024;
        int step = 67;
        doTestNewNonMovableArray(boolean.class, step, maxLengthForLoop);
        doTestNewNonMovableArray(byte.class, step, maxLengthForLoop);
        doTestNewNonMovableArray(char.class, step, maxLengthForLoop);
        doTestNewNonMovableArray(short.class, step, maxLengthForLoop);
        doTestNewNonMovableArray(int.class, step, maxLengthForLoop);
        doTestNewNonMovableArray(long.class, step, maxLengthForLoop);
        doTestNewNonMovableArray(float.class, step, maxLengthForLoop);
        doTestNewNonMovableArray(double.class, step, maxLengthForLoop);
        doTestNewNonMovableArray(Object.class, step, maxLengthForLoop);
        doTestNewNonMovableArray(Number.class, step, maxLengthForLoop);
        doTestNewNonMovableArray(String.class, step, maxLengthForLoop);
        doTestNewNonMovableArray(Runnable.class, step, maxLengthForLoop);
    }

    private void doTestNewUnpaddedArray(Class<?> componentType, int step, int maxLength) {
         // Can't create negative sized arrays.
        try {
            Object array = VMRuntime.getRuntime().newUnpaddedArray(componentType, -1);
            assertTrue(false);
        } catch (NegativeArraySizeException expected) {
        }

        try {
            Object array = VMRuntime.getRuntime().newUnpaddedArray(componentType, Integer.MIN_VALUE);
            assertTrue(false);
        } catch (NegativeArraySizeException expected) {
        }

        // Allocate arrays in a loop and check their properties.
        for (int i = 0; i <= maxLength; i += step) {
            Object array = VMRuntime.getRuntime().newUnpaddedArray(componentType, i);
            assertTrue(array.getClass().isArray());
            assertEquals(array.getClass().getComponentType(), componentType);
            assertTrue(Array.getLength(array) >= i);
        }
    }

    public void testNewUnpaddedArray() {
        // Can't create arrays with no component type.
        try {
            Object array = VMRuntime.getRuntime().newUnpaddedArray(null, 0);
            assertTrue(false);
        } catch (NullPointerException expected) {
        }

        // Can't create arrays of void.
        try {
            Object array = VMRuntime.getRuntime().newUnpaddedArray(void.class, 0);
            assertTrue(false);
        } catch (NoClassDefFoundError expected) {
        }

        int maxLengthForLoop = 16 * 1024;
        int step = 67;
        doTestNewUnpaddedArray(boolean.class, step, maxLengthForLoop);
        doTestNewUnpaddedArray(byte.class, step, maxLengthForLoop);
        doTestNewUnpaddedArray(char.class, step, maxLengthForLoop);
        doTestNewUnpaddedArray(short.class, step, maxLengthForLoop);
        doTestNewUnpaddedArray(int.class, step, maxLengthForLoop);
        doTestNewUnpaddedArray(long.class, step, maxLengthForLoop);
        doTestNewUnpaddedArray(float.class, step, maxLengthForLoop);
        doTestNewUnpaddedArray(double.class, step, maxLengthForLoop);
        doTestNewUnpaddedArray(Object.class, step, maxLengthForLoop);
        doTestNewUnpaddedArray(Number.class, step, maxLengthForLoop);
        doTestNewUnpaddedArray(String.class, step, maxLengthForLoop);
        doTestNewUnpaddedArray(Runnable.class, step, maxLengthForLoop);
    }
}