1 package gov.nasa.jpf.jvm; 20 21 24 public class TestNativePeer { 25 static int sdata; 26 27 static { 28 sdata = 0; } 31 32 int idata; 33 34 TestNativePeer () { 35 } 36 37 TestNativePeer (int data) { 38 } 40 41 public static void main (String [] args) { 42 TestNativePeer t = new TestNativePeer(); 43 44 if (args.length > 0) { 45 for (int i = 0; i < args.length; i++) { 47 String func = args[i]; 48 49 if ("testClInit".equals(func)) { 52 t.testClInit(); 53 } else if ("testInit".equals(func)) { 54 t.testInit(); 55 } else if ("testNativeInstanceMethod".equals(func)) { 56 t.testNativeInstanceMethod(); 57 } else if ("testNativeStaticMethod".equals(func)) { 58 t.testNativeStaticMethod(); 59 } else if ("testNativeCreateStringArray".equals(func)) { 60 t.testNativeCreateStringArray(); 61 } else if ("testNativeCreateIntArray".equals(func)) { 62 t.testNativeCreateIntArray(); 63 } else if ("testNativeCreate2DimIntArray".equals(func)) { 64 t.testNativeCreate2DimIntArray(); 65 } else if ("testNativeException".equals(func)) { 66 t.testNativeException(); 67 } else if ("testNativeCrash".equals(func)) { 68 t.testNativeCrash(); 69 } else { 70 throw new IllegalArgumentException ("unknown test function"); 71 } 72 } 73 } else { 74 t.testClInit(); 76 t.testInit(); 77 t.testNativeInstanceMethod(); 78 t.testNativeStaticMethod(); 79 t.testNativeCreateStringArray(); 80 t.testNativeCreateIntArray(); 81 t.testNativeCreate2DimIntArray(); 82 t.testNativeException(); 83 t.testNativeCrash(); 84 } 85 } 86 87 public void testClInit () { 88 if (sdata != 42) { 89 throw new RuntimeException ("native '<clinit>' failed"); 90 } 91 } 92 93 public void testInit () { 94 TestNativePeer t = new TestNativePeer(42); 95 96 if (t.idata != 42) { 97 throw new RuntimeException ("native '<init>' failed"); 98 } 99 } 100 101 public void testNativeCreate2DimIntArray () { 102 int[][] a = nativeCreate2DimIntArray(2, 3); 103 104 if (a == null) { 105 throw new RuntimeException ("native int[][] creation failed: null"); 106 } 107 108 if (!a.getClass().isArray()) { 109 throw new RuntimeException ("native int[][] creation failed: not an array"); 110 } 111 112 if (!a.getClass().getComponentType().getName().equals("[I")) { 113 throw new RuntimeException ( 114 "native int[][] creation failed: wrong component type"); 115 } 116 117 if (!(a[1][1] == 42)) { 118 throw new RuntimeException ("native int[][] element init failed"); 119 } 120 } 121 122 public void testNativeCreateIntArray () { 123 int[] a = nativeCreateIntArray(3); 124 125 if (a == null) { 126 throw new RuntimeException ("native int array creation failed: null"); 127 } 128 129 if (!a.getClass().isArray()) { 130 throw new RuntimeException ( 131 "native int array creation failed: not an array"); 132 } 133 134 if (a.getClass().getComponentType() != int.class) { 135 throw new RuntimeException ( 136 "native int array creation failed: wrong component type"); 137 } 138 139 if (!(a[1] == 1)) { 140 throw new RuntimeException ("native int array element init failed"); 141 } 142 } 143 144 public void testNativeCreateStringArray () { 145 String [] a = nativeCreateStringArray(3); 146 147 if (a == null) { 148 throw new RuntimeException ("native String array creation failed: null"); 149 } 150 151 if (!a.getClass().isArray()) { 152 throw new RuntimeException ( 153 "native String array creation failed: not an array"); 154 } 155 156 if (a.getClass().getComponentType() != String .class) { 157 throw new RuntimeException ( 158 "native String array creation failed: wrong component type"); 159 } 160 161 if (!"one".equals(a[1])) { 162 throw new RuntimeException ("native String array element init failed"); 163 } 164 } 165 166 public void testNativeException () { 167 try { 168 nativeException(); 169 } catch (UnsupportedOperationException ux) { 170 String details = ux.getMessage(); 171 172 if ("caught me".equals(details)) { 173 ux.printStackTrace(); 174 return; 175 } else { 176 throw new RuntimeException ("wrong native exception details: " + 177 details); 178 } 179 } catch (Throwable t) { 180 throw new RuntimeException ("wrong native exception type: " + 181 t.getClass()); 182 } 183 184 throw new RuntimeException ("no native exception thrown"); 185 } 186 187 public void testNativeCrash () { 188 nativeCrash(); 189 } 190 191 public void testNativeInstanceMethod () { 192 int res = nativeInstanceMethod(2.0, '?', true, 40); 193 194 if (res != 42) { 195 throw new RuntimeException ("native instance method failed"); 196 } 197 } 198 199 public void testNativeStaticMethod () { 200 long res = nativeStaticMethod(40, "Blah"); 201 202 if (res != 42) { 203 throw new RuntimeException ("native instance method failed"); 204 } 205 } 206 207 native int[][] nativeCreate2DimIntArray (int s1, int s2); 208 209 native int[] nativeCreateIntArray (int size); 210 211 native String [] nativeCreateStringArray (int size); 212 213 native void nativeException (); 214 215 native int nativeCrash (); 216 217 native int nativeInstanceMethod (double d, char c, boolean b, int i); 218 219 native long nativeStaticMethod (long l, String s); 220 } | Popular Tags |