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