KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > jvm > TestMethod


1 //
2
// Copyright (C) 2005 United States Government as represented by the
3
// Administrator of the National Aeronautics and Space Administration
4
// (NASA). All Rights Reserved.
5
//
6
// This software is distributed under the NASA Open Source Agreement
7
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
8
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
// directory tree for the complete NOSA document.
10
//
11
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18
//
19
package gov.nasa.jpf.jvm;
20
21 interface TMI {
22   void gna();
23 }
24
25 class TestMethodBase implements TMI {
26   
27   int baseData;
28
29   static int sData;
30   
31   static {
32     sData = -1;
33   }
34   
35   static void taz () {
36     sData = 24;
37   }
38   
39   TestMethodBase (int a) {
40     assert a == 42;
41     baseData = 42;
42   }
43
44   boolean baz (boolean p, byte b, char c, short s, int i, long l, float f, double d, Object JavaDoc o) {
45     assert p;
46     assert b == 4;
47     assert c == '?';
48     assert s == 42;
49     assert i == 4242;
50     assert l == 424242;
51     assert f == 4.2f;
52     assert d == 4.242;
53     assert o.equals(new Integer JavaDoc(42));
54
55     baseData = 44;
56
57     return p;
58   }
59   
60   void faz () {
61     gna();
62   }
63   
64   public void gna () {
65     baseData = 0;
66   }
67   
68   int har () {
69     return priv();
70   }
71   
72   private int priv () {
73     return 7;
74   }
75 }
76
77 /**
78  * method invocation test
79  */

80 public class TestMethod extends TestMethodBase {
81   
82   int data;
83   
84   static void taz () {
85     sData = 9;
86   }
87   
88   TestMethod () {
89     super(42);
90     
91     data = 42;
92   }
93
94   TestMethod (int a) {
95     super(a);
96     
97     data = a;
98   }
99   
100   double foo (boolean p, byte b, char c, short s, int i, long l, float f, double d, Object JavaDoc o) {
101     assert p;
102     assert b == 4;
103     assert c == '?';
104     assert s == 42;
105     assert i == 4242;
106     assert l == 424242;
107     assert f == 4.2f;
108     assert d == 4.242;
109     assert o.equals(new Integer JavaDoc(42));
110
111     data = 43;
112
113     return d;
114   }
115
116   public void gna () {
117     baseData = 45;
118   }
119   
120   int priv () {
121     return 8;
122   }
123   
124   public static void main (String JavaDoc[] args) {
125     TestMethod t = new TestMethod();
126
127     if (args.length > 0) {
128       // just run the specified tests
129
for (int i = 0; i < args.length; i++) {
130         String JavaDoc func = args[i];
131
132         // note that we don't use reflection here because this would
133
// blow up execution/test scope under JPF
134
if ("testCtor".equals(func)) { testCtor(); }
135         else if ( "testCall".equals(func)) { testCall(); }
136         else if ( "testInheritedCall".equals(func)) { testInheritedCall(); }
137         else if ( "testVirtualCall".equals(func)) { testVirtualCall(); }
138         else if ( "testSpecialCall".equals(func)) { testSpecialCall(); }
139         else if ( "testStaticCall".equals( func)) { testStaticCall(); }
140         else {
141           throw new IllegalArgumentException JavaDoc("unknown test function");
142         }
143       }
144     } else {
145       testCtor();
146       testCall();
147       testInheritedCall();
148       testVirtualCall();
149       testSpecialCall();
150       testStaticCall();
151     }
152   }
153
154   public static void testCtor () {
155     TestMethod o1 = new TestMethod();
156     assert o1.data == 42;
157     assert o1.baseData == 42;
158     
159     TestMethod o2 = new TestMethod(42);
160     assert o2.data == 42;
161     assert o2.baseData == 42;
162   }
163   
164   public static void testCall () {
165     TestMethod o = new TestMethod();
166     assert o.foo( true, (byte)4, '?', (short)42, 4242, 424242, 4.2f, 4.242, new Integer JavaDoc(42)) == 4.242;
167     assert o.data == 43;
168   }
169   
170   public static void testInheritedCall () {
171     TestMethod o = new TestMethod();
172     assert o.baz( true, (byte)4, '?', (short)42, 4242, 424242, 4.2f, 4.242, new Integer JavaDoc(42));
173     assert o.baseData == 44;
174   }
175   
176   public static void testVirtualCall () {
177     TestMethod o = new TestMethod();
178     TestMethodBase b = (TestMethodBase) o;
179     
180     b.faz();
181     assert o.baseData == 45;
182   }
183   
184   public static void testSpecialCall () {
185     TestMethod o = new TestMethod();
186     assert o.har() == 7;
187   }
188   
189   public static void testStaticCall () {
190     assert TestMethodBase.sData == -1;
191     
192     TestMethod.taz();
193     assert TestMethodBase.sData == 9;
194     
195     TestMethodBase.taz();
196     assert TestMethodBase.sData == 24;
197     
198     TestMethod o = new TestMethod();
199     o.taz();
200     assert TestMethodBase.sData == 9;
201   }
202   
203   public static void testInterfaceCall () {
204     TestMethod o = new TestMethod();
205     TMI ifc = (TMI) o;
206     
207     ifc.gna();
208     assert o.baseData == 45;
209   }
210 }
211
Popular Tags