001: /*
002: * @(#)simple.java 1.14 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package jittest;
029:
030: class simple {
031:
032: public int simple(int a[], int maxI) {
033: int hadException = 0;
034: int sum = 0;
035: try {
036: for (int i = 0; i < maxI; i++) {
037: sum = sum + a[i];
038: a[i] = 0;
039: }
040: } catch (NullPointerException e) {
041: hadException = 1;
042: }
043: return (hadException == 1) ? -1 : sum;
044: }
045:
046: public void ExceptionTest(int a[], int maxI) throws Exception {
047: boolean flag = false;
048: int hadException = 0;
049: int hadTry = 0;
050: int sum = 0;
051: try {
052: for (int i = 0; i < maxI; i++) {
053: sum = sum + a[i];
054: a[i] = 0;
055: try {
056: sum = 5;
057: hadTry = 5;
058: } catch (Exception e4) {
059: flag = false;
060: }
061: hadException = 1;
062: }
063: try {
064: sum = 6;
065: hadTry = 6;
066: } catch (Exception e4) {
067: flag = false;
068: }
069: hadException = 2;
070: } catch (NullPointerException e) {
071: try {
072: sum = 4;
073: hadTry = 4;
074: } catch (Exception e4) {
075: flag = true;
076: }
077: hadException = 3;
078: } catch (ArrayIndexOutOfBoundsException e3) {
079: hadException = 4;
080: } catch (Exception e3) {
081: hadException = 5;
082: flag = false;
083: throw e3;
084: }
085: }
086:
087: public void arraySet(int a[], int v) {
088: for (int i = 0; i < a.length; i++) {
089: a[i] = v;
090: a[i + v + 1] = 0;
091: i = a[i + 2];
092: }
093: }
094:
095: public void testTypeConvert(int arr[]) {
096: int i = 0;
097: float f = (float) 1.0;
098: double d = 1.0;
099: long l = 1L;
100: short s = 10;
101: char c = 'a';
102: byte b = 'a';
103:
104: f = i;
105: l = i;
106: d = i;
107: b = (byte) i;
108: c = (char) i;
109: s = 20;
110: i = (int) l;
111: f = l;
112: d = l;
113: i = (int) f;
114: l = (long) f;
115: d = f;
116: i = (int) d;
117: f = (float) d;
118: l = (long) d;
119: }
120:
121: public void testConst(int arr[]) {
122: int i1 = 0;
123: int i2 = 1;
124: float f1 = (float) 1.0;
125: float f2 = (float) 2.0;
126: double d1 = 1.0;
127: double d2 = 2.0;
128: long l1 = 1L;
129: long l2 = 2L;
130:
131: i1 = i1 + i2;
132: i2 = i1 + 10;
133: i1 = arr.length;
134: f1 = f1 + f2;
135: f2 = f1 + (float) 3.0;
136: d1 = d1 + d2;
137: d2 = d1 + 3.0;
138: l1 = l1 + l2;
139: l2 = l1 + 10;
140:
141: }
142:
143: public void testNewStringObj(String argv[]) {
144: String s = "abc";
145: s = "test" + "WORD" + s;
146: s = argv[argv.length];
147: s = new String("def");
148: System.out.print(true);
149: System.out.print("*TEST FAILURE: ");
150: System.out.println(s);
151: System.out.println("*Number of command line arguments: "
152: + argv.length);
153: }
154:
155: public void testCast() {
156: simple names = new simple();
157: Object o = new Object();
158: boolean b;
159: if (o instanceof simple) {
160: b = true;
161: }
162: o = (Object) names;
163: names = (simple) o;
164: }
165:
166: static public int testCMP() {
167: long l1 = 1L;
168: long l2 = 2L;
169: float f1 = (float) 3.0;
170: float f2 = (float) 4.0;
171: double d1 = 5.0;
172: double d2 = 6.0;
173: short a = 0;
174: boolean b = false;
175:
176: if (l1 > l2) {
177: a = 1;
178: } else if (l1 == l2) {
179: a = 2;
180: } else if (l1 < l2) {
181: a = 3;
182: } else if (l1 <= l2) {
183: a = 4;
184: } else if (b = l1 >= l2) {
185: a = 5;
186: } else if (l1 > 0) {
187: a = 6;
188: }
189: if (f1 > f2) {
190: f1 = 1;
191: } else if (f1 == f2) {
192: f1 = 2;
193: } else if (f1 < f2) {
194: f1 = 3;
195: } else if (f1 <= f2) {
196: f1 = 4;
197: } else if (f1 >= f2) {
198: f1 = 5;
199: } else if (b = (f1 > 0)) {
200: f1 = 6;
201: }
202: return a;
203: }
204:
205: public int testTableSwitch(char c) {
206:
207: switch ((int) c - '0') {
208: case 0:
209: c = 'a';
210: break;
211: case 1:
212: return 1;
213: case 2:
214: case 3:
215: return 3;
216: case 4:
217: return 4;
218: /* case 5: DON'T LIKE 5 */
219:
220: case 6:
221: return 6;
222: case 7:
223: return 7;
224: case 8:
225: return 8;
226: case 9:
227: return 9;
228: default:
229: return -1;
230: }
231: c = 'b';
232: return 10;
233: }
234:
235: public void testLookupSwitch() {
236: int a = 50;
237: int c = 10;
238: int b;
239: switch (a) {
240: case 5:
241: b = 0;
242: break;
243: case 6:
244: b = 1;
245: break;
246: case 7:
247: b = 2;
248: break;
249: default:
250: b = 10;
251: break;
252: }
253:
254: a++;
255: ++b;
256:
257: switch (b) {
258: case 5:
259: a = 0;
260: break;
261: case 6:
262: a = 1;
263: break;
264: case 20:
265: a = 20;
266: break;
267: case 21:
268: a = 25;
269: break;
270: case 30:
271: a = 30;
272: break;
273: default:
274: a = 40;
275: break;
276: }
277: a = b + c;
278: }
279:
280: static void printString(String a) {
281: System.out.println(a);
282: }
283:
284: public static void helloWorld(String a[]) {
285: for (int i = 0; i < a.length; i++) {
286: printString(a[i]);
287: }
288: }
289:
290: static int findMax(int i, int j) {
291: if (i > j)
292: return i;
293: return j;
294: }
295:
296: public static int positiveValue(int v) {
297: return findMax(v, 0);
298: }
299:
300: public void switch_method(String args[]) {
301: int a = 50;
302: int c = 10;
303: int b;
304: switch (a) {
305: case 5:
306: b = 0;
307: break;
308: case 6:
309: b = 1;
310: break;
311: case 7:
312: b = 2;
313: break;
314: default:
315: b = 10;
316: break;
317: }
318:
319: a++;
320: ++b;
321:
322: switch (b) {
323: case 5:
324: a = 0;
325: break;
326: case 6:
327: a = 1;
328: break;
329: case 20:
330: a = 20;
331: break;
332: case 21:
333: a = 25;
334: break;
335: case 30:
336: a = 30;
337: break;
338: default:
339: a = 40;
340: break;
341: }
342: }
343:
344: }
|