KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
22  * JPF part of unit test for standard VM array operations.
23  */

24 public class TestArray {
25   TestArray () {
26   }
27
28   public static void main (String JavaDoc[] args) {
29     TestArray t = new TestArray();
30
31     if (args.length > 0) {
32       // just run the specified tests
33
for (int i = 0; i < args.length; i++) {
34         String JavaDoc func = args[i];
35
36         // note that we don't use reflection here because this would
37
// blow up execution/test scope under JPF
38
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 JavaDoc("unknown test function");
52         }
53       }
54     } else {
55       // that's mainly for our standalone test verification
56
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 JavaDoc[][] a = new String JavaDoc[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 JavaDoc aobx) {
95       return;
96     }
97
98     throw new RuntimeException JavaDoc("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 JavaDoc[] 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 JavaDoc.class);
129     assert (a[1].equals("two"));
130   }
131 }
132
Popular Tags