1 /* 2 * Copyright (C) 2008 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 dot.junit.opcodes.new_array; 18 19 import dot.junit.DxTestCase; 20 import dot.junit.DxUtil; 21 import dot.junit.opcodes.new_array.d.T_new_array_1; 22 import dot.junit.opcodes.new_array.d.T_new_array_2; 23 import dot.junit.opcodes.new_array.d.T_new_array_3; 24 25 public class Test_new_array extends DxTestCase { 26 27 @SuppressWarnings("unused") 28 private class TestStub{ 29 // used by testVFE8 30 } 31 32 /** 33 * @title Array of ints 34 */ testN1()35 public void testN1() { 36 T_new_array_1 t = new T_new_array_1(); 37 int[] r = t.run(10); 38 int l = r.length; 39 assertEquals(10, l); 40 41 // check default initialization 42 for (int i = 0; i < l; i++) { 43 assertEquals(0, r[i]); 44 } 45 46 } 47 48 /** 49 * @title Array of booleans 50 */ testN2()51 public void testN2() { 52 T_new_array_2 t = new T_new_array_2(); 53 boolean[] r = t.run(10); 54 int l = r.length; 55 assertEquals(10, l); 56 57 // check default initialization 58 for (int i = 0; i < l; i++) { 59 assertFalse(r[i]); 60 } 61 } 62 63 /** 64 * @title Array of Objects 65 */ testN3()66 public void testN3() { 67 T_new_array_3 t = new T_new_array_3(); 68 Object[] r = t.run(10); 69 int l = r.length; 70 assertEquals(10, l); 71 72 // check default initialization 73 for (int i = 0; i < l; i++) { 74 assertNull(r[i]); 75 } 76 } 77 78 /** 79 * @title Array size = 0 80 */ testB1()81 public void testB1() { 82 T_new_array_1 t = new T_new_array_1(); 83 int[] r = t.run(0); 84 assertNotNull(r); 85 assertEquals(0, r.length); 86 } 87 88 /** 89 * @title expected NegativeArraySizeException 90 */ testE1()91 public void testE1() { 92 T_new_array_2 t = new T_new_array_2(); 93 try { 94 t.run(-1); 95 fail("expected NegativeArraySizeException"); 96 } catch (NegativeArraySizeException nase) { 97 // expected 98 } 99 } 100 101 102 /** 103 * @constraint B1 104 * @title number of registers 105 */ testVFE1()106 public void testVFE1() { 107 try { 108 Class.forName("dot.junit.opcodes.new_array.d.T_new_array_4"); 109 fail("expected a verification exception"); 110 } catch (Throwable t) { 111 DxUtil.checkVerifyException(t); 112 } 113 } 114 115 /** 116 * 117 * @constraint B1 118 * @title size argument - long 119 */ testVFE2()120 public void testVFE2() { 121 try { 122 Class.forName("dot.junit.opcodes.new_array.d.T_new_array_5"); 123 fail("expected a verification exception"); 124 } catch (Throwable t) { 125 DxUtil.checkVerifyException(t); 126 } 127 } 128 129 /** 130 * 131 * @constraint B1 132 * @title size argument - reference 133 */ testVFE3()134 public void testVFE3() { 135 try { 136 Class.forName("dot.junit.opcodes.new_array.d.T_new_array_9"); 137 fail("expected a verification exception"); 138 } catch (Throwable t) { 139 DxUtil.checkVerifyException(t); 140 } 141 } 142 143 /** 144 * 145 * @constraint A19 146 * @title constant pool index 147 */ testVFE4()148 public void testVFE4() { 149 try { 150 Class.forName("dot.junit.opcodes.new_array.d.T_new_array_6"); 151 fail("expected a verification exception"); 152 } catch (Throwable t) { 153 DxUtil.checkVerifyException(t); 154 } 155 } 156 157 /** 158 * 159 * @constraint A22 160 * @title attempt to create object 161 */ testVFE5()162 public void testVFE5() { 163 try { 164 Class.forName("dot.junit.opcodes.new_array.d.T_new_array_7"); 165 fail("expected a verification exception"); 166 } catch (Throwable t) { 167 DxUtil.checkVerifyException(t); 168 } 169 } 170 171 /** 172 * 173 * @constraint A20 174 * @title array of more than 255 dimensions 175 */ testVFE6()176 public void testVFE6() { 177 try { 178 Class.forName("dot.junit.opcodes.new_array.d.T_new_array_8"); 179 fail("expected a verification exception"); 180 } catch (Throwable t) { 181 DxUtil.checkVerifyException(t); 182 } 183 } 184 185 /** 186 * @constraint n/a 187 * @title Attempt to instantiate array of non-existent class. Java 188 * throws NoClassDefFoundError on first access but Dalvik throws VerifyError on class loading. 189 */ testVFE7()190 public void testVFE7() { 191 try { 192 Class.forName("dot.junit.opcodes.new_array.d.T_new_array_11"); 193 fail("expected a verification exception"); 194 } catch (Throwable t) { 195 DxUtil.checkVerifyException(t); 196 } 197 } 198 199 /** 200 * @constraint n/a 201 * @title Attempt to instantiate array of inaccessible class. Java 202 * throws IllegalAccessError on first access but Dalvik throws VerifyError on class loading. 203 */ testVFE8()204 public void testVFE8() { 205 try { 206 Class.forName("dot.junit.opcodes.new_array.d.T_new_array_10"); 207 fail("expected a verification exception"); 208 } catch (Throwable t) { 209 DxUtil.checkVerifyException(t); 210 } 211 } 212 213 } 214