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: * JPF part of unit test for standard VM array operations.
023: */
024: public class TestArray {
025: TestArray() {
026: }
027:
028: public static void main(String[] args) {
029: TestArray t = new TestArray();
030:
031: if (args.length > 0) {
032: // just run the specified tests
033: for (int i = 0; i < args.length; i++) {
034: String func = args[i];
035:
036: // note that we don't use reflection here because this would
037: // blow up execution/test scope under JPF
038: if ("testIntArray".equals(func)) {
039: t.testIntArray();
040: } else if ("testCharArray".equals(func)) {
041: t.testCharArray();
042: } else if ("testStringArray".equals(func)) {
043: t.testStringArray();
044: } else if ("test2DArray".equals(func)) {
045: t.test2DArray();
046: } else if ("test2DStringArray".equals(func)) {
047: t.test2DStringArray();
048: } else if ("testAoBX".equals(func)) {
049: t.testAoBX();
050: } else {
051: throw new IllegalArgumentException(
052: "unknown test function");
053: }
054: }
055: } else {
056: // that's mainly for our standalone test verification
057: t.testIntArray();
058: t.testCharArray();
059: t.testStringArray();
060: t.test2DArray();
061: t.test2DStringArray();
062: t.testAoBX();
063: }
064: }
065:
066: void test2DArray() {
067: long[][] a = new long[2][3];
068:
069: a[0][1] = 42;
070:
071: assert (a.getClass().isArray());
072: assert (a.getClass().getName().equals("[[J"));
073: assert (a.getClass().getComponentType().getName().equals("[J"));
074: assert (a[0][1] == 42);
075: }
076:
077: void test2DStringArray() {
078: String[][] a = new String[3][5];
079:
080: a[2][2] = "fortytwo";
081:
082: assert (a.getClass().isArray());
083: assert (a.getClass().getName().equals("[[Ljava.lang.String;"));
084: assert (a.getClass().getComponentType().getName()
085: .equals("[Ljava.lang.String;"));
086: assert (a[2][2].equals("fortytwo"));
087: }
088:
089: void testAoBX() {
090: int[] a = new int[2];
091:
092: assert (a.length == 2);
093:
094: try {
095: a[2] = 42;
096: } catch (ArrayIndexOutOfBoundsException aobx) {
097: return;
098: }
099:
100: throw new RuntimeException("array bounds check failed");
101: }
102:
103: void testCharArray() {
104: char[] a = new char[5];
105:
106: a[2] = 'Z';
107:
108: assert (a.getClass().isArray());
109: assert (a.getClass().getName().equals("[C"));
110: assert (a.getClass().getComponentType() == char.class);
111: assert (a[2] == 'Z');
112: }
113:
114: void testIntArray() {
115: int[] a = new int[10];
116:
117: a[1] = 42;
118:
119: assert (a.getClass().isArray());
120: assert (a.getClass().getName().equals("[I"));
121: assert (a.getClass().getComponentType() == int.class);
122: assert (a[1] == 42);
123: }
124:
125: void testStringArray() {
126: String[] a = { "one", "two", "three" };
127:
128: assert (a.getClass().isArray());
129: assert (a.getClass().getName().equals("[Ljava.lang.String;"));
130: assert (a.getClass().getComponentType() == String.class);
131: assert (a[1].equals("two"));
132: }
133: }
|