1 package gov.nasa.jpf.jvm; 20 21 24 public class TestArray { 25 TestArray () { 26 } 27 28 public static void main (String [] args) { 29 TestArray t = new TestArray(); 30 31 if (args.length > 0) { 32 for (int i = 0; i < args.length; i++) { 34 String func = args[i]; 35 36 if ("testIntArray".equals(func)) { 39 t.testIntArray(); 40 } else if ("testCharArray".equals(func)) { 41 t.testCharArray(); 42 } else if ("testStringArray".equals(func)) { 43 t.testStringArray(); 44 } else if ("test2DArray".equals(func)) { 45 t.test2DArray(); 46 } else if ("test2DStringArray".equals(func)) { 47 t.test2DStringArray(); 48 } else if ("testAoBX".equals(func)) { 49 t.testAoBX(); 50 } else { 51 throw new IllegalArgumentException ("unknown test function"); 52 } 53 } 54 } else { 55 t.testIntArray(); 57 t.testCharArray(); 58 t.testStringArray(); 59 t.test2DArray(); 60 t.test2DStringArray(); 61 t.testAoBX(); 62 } 63 } 64 65 void test2DArray () { 66 long[][] a = new long[2][3]; 67 68 a[0][1] = 42; 69 70 assert (a.getClass().isArray()); 71 assert (a.getClass().getName().equals("[[J")); 72 assert (a.getClass().getComponentType().getName().equals("[J")); 73 assert (a[0][1] == 42); 74 } 75 76 void test2DStringArray () { 77 String [][] a = new String [3][5]; 78 79 a[2][2] = "fortytwo"; 80 81 assert (a.getClass().isArray()); 82 assert (a.getClass().getName().equals("[[Ljava.lang.String;")); 83 assert (a.getClass().getComponentType().getName().equals("[Ljava.lang.String;")); 84 assert (a[2][2].equals("fortytwo")); 85 } 86 87 void testAoBX () { 88 int[] a = new int[2]; 89 90 assert (a.length == 2); 91 92 try { 93 a[2] = 42; 94 } catch (ArrayIndexOutOfBoundsException aobx) { 95 return; 96 } 97 98 throw new RuntimeException ("array bounds check failed"); 99 } 100 101 void testCharArray () { 102 char[] a = new char[5]; 103 104 a[2] = 'Z'; 105 106 assert (a.getClass().isArray()); 107 assert (a.getClass().getName().equals("[C")); 108 assert (a.getClass().getComponentType() == char.class); 109 assert (a[2] == 'Z'); 110 } 111 112 void testIntArray () { 113 int[] a = new int[10]; 114 115 a[1] = 42; 116 117 assert (a.getClass().isArray()); 118 assert (a.getClass().getName().equals("[I")); 119 assert (a.getClass().getComponentType() == int.class); 120 assert (a[1] == 42); 121 } 122 123 void testStringArray () { 124 String [] a = { "one", "two", "three" }; 125 126 assert (a.getClass().isArray()); 127 assert (a.getClass().getName().equals("[Ljava.lang.String;")); 128 assert (a.getClass().getComponentType() == String .class); 129 assert (a[1].equals("two")); 130 } 131 } 132 | Popular Tags |