001: //
002: // Copyright (C) 2005 United States Government as represented by the
003: // Administrator of the National Aeronautics and Space Administration
004: // (NASA). All Rights Reserved.
005: //
006: // This software is distributed under the NASA Open Source Agreement
007: // (NOSA), version 1.3. The NOSA has been approved by the Open Source
008: // Initiative. See the file NOSA-1.3-JPF at the top of the distribution
009: // directory tree for the complete NOSA document.
010: //
011: // THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
012: // KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
013: // LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
014: // SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
015: // A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
016: // THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
017: // DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
018: //
019: package gov.nasa.jpf.jvm;
020:
021: /**
022: * model class of MJI test
023: */
024: public class TestNativePeer {
025: static int sdata;
026:
027: static {
028: // only here to be intercepted
029: sdata = 0; // dummy insn required for the Eclipse compiler (skips empty methods)
030: }
031:
032: int idata;
033:
034: TestNativePeer() {
035: }
036:
037: TestNativePeer(int data) {
038: // only here to be intercepted
039: }
040:
041: public static void main(String[] args) {
042: TestNativePeer t = new TestNativePeer();
043:
044: if (args.length > 0) {
045: // just run the specified tests
046: for (int i = 0; i < args.length; i++) {
047: String func = args[i];
048:
049: // note that we don't use reflection here because this would
050: // blow up execution/test scope under JPF
051: if ("testClInit".equals(func)) {
052: t.testClInit();
053: } else if ("testInit".equals(func)) {
054: t.testInit();
055: } else if ("testNativeInstanceMethod".equals(func)) {
056: t.testNativeInstanceMethod();
057: } else if ("testNativeStaticMethod".equals(func)) {
058: t.testNativeStaticMethod();
059: } else if ("testNativeCreateStringArray".equals(func)) {
060: t.testNativeCreateStringArray();
061: } else if ("testNativeCreateIntArray".equals(func)) {
062: t.testNativeCreateIntArray();
063: } else if ("testNativeCreate2DimIntArray".equals(func)) {
064: t.testNativeCreate2DimIntArray();
065: } else if ("testNativeException".equals(func)) {
066: t.testNativeException();
067: } else if ("testNativeCrash".equals(func)) {
068: t.testNativeCrash();
069: } else {
070: throw new IllegalArgumentException(
071: "unknown test function");
072: }
073: }
074: } else {
075: // that's mainly for our standalone test verification
076: t.testClInit();
077: t.testInit();
078: t.testNativeInstanceMethod();
079: t.testNativeStaticMethod();
080: t.testNativeCreateStringArray();
081: t.testNativeCreateIntArray();
082: t.testNativeCreate2DimIntArray();
083: t.testNativeException();
084: t.testNativeCrash();
085: }
086: }
087:
088: public void testClInit() {
089: if (sdata != 42) {
090: throw new RuntimeException("native '<clinit>' failed");
091: }
092: }
093:
094: public void testInit() {
095: TestNativePeer t = new TestNativePeer(42);
096:
097: if (t.idata != 42) {
098: throw new RuntimeException("native '<init>' failed");
099: }
100: }
101:
102: public void testNativeCreate2DimIntArray() {
103: int[][] a = nativeCreate2DimIntArray(2, 3);
104:
105: if (a == null) {
106: throw new RuntimeException(
107: "native int[][] creation failed: null");
108: }
109:
110: if (!a.getClass().isArray()) {
111: throw new RuntimeException(
112: "native int[][] creation failed: not an array");
113: }
114:
115: if (!a.getClass().getComponentType().getName().equals("[I")) {
116: throw new RuntimeException(
117: "native int[][] creation failed: wrong component type");
118: }
119:
120: if (!(a[1][1] == 42)) {
121: throw new RuntimeException(
122: "native int[][] element init failed");
123: }
124: }
125:
126: public void testNativeCreateIntArray() {
127: int[] a = nativeCreateIntArray(3);
128:
129: if (a == null) {
130: throw new RuntimeException(
131: "native int array creation failed: null");
132: }
133:
134: if (!a.getClass().isArray()) {
135: throw new RuntimeException(
136: "native int array creation failed: not an array");
137: }
138:
139: if (a.getClass().getComponentType() != int.class) {
140: throw new RuntimeException(
141: "native int array creation failed: wrong component type");
142: }
143:
144: if (!(a[1] == 1)) {
145: throw new RuntimeException(
146: "native int array element init failed");
147: }
148: }
149:
150: public void testNativeCreateStringArray() {
151: String[] a = nativeCreateStringArray(3);
152:
153: if (a == null) {
154: throw new RuntimeException(
155: "native String array creation failed: null");
156: }
157:
158: if (!a.getClass().isArray()) {
159: throw new RuntimeException(
160: "native String array creation failed: not an array");
161: }
162:
163: if (a.getClass().getComponentType() != String.class) {
164: throw new RuntimeException(
165: "native String array creation failed: wrong component type");
166: }
167:
168: if (!"one".equals(a[1])) {
169: throw new RuntimeException(
170: "native String array element init failed");
171: }
172: }
173:
174: public void testNativeException() {
175: try {
176: nativeException();
177: } catch (UnsupportedOperationException ux) {
178: String details = ux.getMessage();
179:
180: if ("caught me".equals(details)) {
181: ux.printStackTrace();
182: return;
183: } else {
184: throw new RuntimeException(
185: "wrong native exception details: " + details);
186: }
187: } catch (Throwable t) {
188: throw new RuntimeException("wrong native exception type: "
189: + t.getClass());
190: }
191:
192: throw new RuntimeException("no native exception thrown");
193: }
194:
195: public void testNativeCrash() {
196: nativeCrash();
197: }
198:
199: public void testNativeInstanceMethod() {
200: int res = nativeInstanceMethod(2.0, '?', true, 40);
201:
202: if (res != 42) {
203: throw new RuntimeException("native instance method failed");
204: }
205: }
206:
207: public void testNativeStaticMethod() {
208: long res = nativeStaticMethod(40, "Blah");
209:
210: if (res != 42) {
211: throw new RuntimeException("native instance method failed");
212: }
213: }
214:
215: native int[][] nativeCreate2DimIntArray(int s1, int s2);
216:
217: native int[] nativeCreateIntArray(int size);
218:
219: native String[] nativeCreateStringArray(int size);
220:
221: native void nativeException();
222:
223: native int nativeCrash();
224:
225: native int nativeInstanceMethod(double d, char c, boolean b, int i);
226:
227: native long nativeStaticMethod(long l, String s);
228: }
|