001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.emultest.java.lang;
017:
018: import com.google.gwt.junit.client.GWTTestCase;
019:
020: /**
021: * Tests java.lang.System.
022: */
023: public class SystemTest extends GWTTestCase {
024:
025: private static class Bar extends Foo {
026: public Bar() {
027: }
028: }
029:
030: private static class Foo {
031: public Foo() {
032: }
033: }
034:
035: public String getModuleName() {
036: return "com.google.gwt.emultest.EmulSuite";
037: }
038:
039: public void testArraycopyFailures() {
040: int[] src = new int[4];
041: int[] dest = new int[] { 1, 1, 1, 1 };
042: double[] destDouble = new double[4];
043: String[] strings = new String[4];
044: try {
045: System.arraycopy(src, 5, dest, 0, 7);
046: fail("Should have thrown IndexOutOfBoundsException: src past end");
047: } catch (IndexOutOfBoundsException e) {
048: }
049: try {
050: System.arraycopy(src, 0, dest, 5, 7);
051: fail("Should have thrown IndexOutOfBoundsException: dest past end");
052: } catch (IndexOutOfBoundsException e) {
053: }
054: try {
055: System.arraycopy(src, -1, dest, 0, 4);
056: fail("Should have thrown IndexOutOfBoundsException: src ofs negative");
057: } catch (IndexOutOfBoundsException e) {
058: }
059: try {
060: System.arraycopy(src, 0, dest, -1, 4);
061: fail("Should have thrown IndexOutOfBoundsException: dest ofs negative");
062: } catch (IndexOutOfBoundsException e) {
063: }
064: try {
065: System.arraycopy(src, 0, dest, 0, -1);
066: fail("Should have thrown IndexOutOfBoundsException: negative length");
067: } catch (IndexOutOfBoundsException e) {
068: }
069: try {
070: System.arraycopy("test", 0, dest, 0, 4);
071: fail("Should have thrown ArrayStoreException: src not array");
072: } catch (ArrayStoreException e) {
073: }
074: try {
075: System.arraycopy(src, 0, "test", 0, 4);
076: fail("Should have thrown ArrayStoreException: dest not array");
077: } catch (ArrayStoreException e) {
078: }
079: try {
080: System.arraycopy(src, 0, destDouble, 0, 4);
081: fail("Should have thrown ArrayStoreException: different primitive types");
082: } catch (ArrayStoreException e) {
083: }
084: try {
085: System.arraycopy(strings, 0, dest, 0, 4);
086: fail("Should have thrown ArrayStoreException: reference/primitive mismatch");
087: } catch (ArrayStoreException e) {
088: }
089: try {
090: System.arraycopy(src, 0, strings, 0, 4);
091: fail("Should have thrown ArrayStoreException: primitive/reference mismatch");
092: } catch (ArrayStoreException e) {
093: }
094: }
095:
096: public void testArraycopyMultidim() {
097: Object[][] objArray = new Object[1][1];
098: String[][] strArray = new String[1][1];
099: strArray[0][0] = "Test";
100: Integer[][] intArray = new Integer[1][1];
101: intArray[0][0] = new Integer(1);
102: System.arraycopy(strArray, 0, objArray, 0, 1);
103: assertEquals("Test", objArray[0][0]);
104: try {
105: System.arraycopy(strArray, 0, intArray, 0, 1);
106: fail("Should have thrown ArrayStoreException: incompatible multidimensional arrays");
107: } catch (ArrayStoreException e) {
108: }
109: try {
110: System.arraycopy(new String[] { "T2" }, 0, objArray, 0, 1);
111: fail("Should have thrown ArrayStoreException: store string array in multi-dim Object array");
112: } catch (ArrayStoreException e) {
113: }
114: }
115:
116: public void testArraycopyNulls() {
117: int[] src = new int[4];
118: int[] dest = new int[] { 1, 1, 1, 1 };
119: try {
120: System.arraycopy(null, 0, dest, 0, 4);
121: fail("Should have thrown NullPointerException: src null");
122: } catch (NullPointerException e) {
123: // verify dest unchanged
124: for (int i = 0; i < dest.length; ++i) {
125: assertEquals(1, dest[i]);
126: }
127: }
128: try {
129: System.arraycopy(src, 0, null, 0, 4);
130: fail("Should have thrown NullPointerException: dest null");
131: } catch (NullPointerException e) {
132: }
133: }
134:
135: public void testArraycopyObjects() {
136: Foo[] fooArray = new Foo[4];
137: Bar[] barArray = new Bar[4];
138: Object[] src = new Object[] { new Bar(), new Bar(), new Foo(),
139: new Bar() };
140: System.arraycopy(src, 0, fooArray, 0, src.length);
141: for (int i = 0; i < src.length; ++i) {
142: assertEquals(src[i], fooArray[i]);
143: }
144: try {
145: System.arraycopy(src, 0, barArray, 0, 4);
146: fail("Should have thrown ArrayStoreException: foo into bar");
147: } catch (ArrayStoreException e) {
148: // verify we changed only up to the element causing the exception
149: assertEquals(src[0], barArray[0]);
150: assertEquals(src[1], barArray[1]);
151: assertNull(barArray[2]);
152: assertNull(barArray[3]);
153: }
154: }
155:
156: public void testArraycopyOverlap() {
157: int[] intArray = new int[] { 0, 1, 2, 3 };
158: String[] strArray = new String[] { "0", "1", "2", "3" };
159:
160: System.arraycopy(intArray, 0, intArray, 1, intArray.length - 1);
161: assertEquals(0, intArray[0]);
162: for (int i = 1; i < intArray.length; ++i) {
163: assertEquals("rev int copy index " + i, i - 1, intArray[i]);
164: }
165: System.arraycopy(intArray, 1, intArray, 0, intArray.length - 1);
166: for (int i = 0; i < intArray.length - 1; ++i) {
167: assertEquals("fwd int copy index " + i, i, intArray[i]);
168: }
169: assertEquals("fwd int copy index " + (intArray.length - 2),
170: intArray.length - 2, intArray[intArray.length - 1]);
171: System.arraycopy(strArray, 0, strArray, 1, strArray.length - 1);
172: assertEquals(0, Integer.valueOf(strArray[0]).intValue());
173: for (int i = 1; i < strArray.length; ++i) {
174: assertEquals("rev str copy index " + i, i - 1, Integer
175: .valueOf(strArray[i]).intValue());
176: }
177: System.arraycopy(strArray, 1, strArray, 0, strArray.length - 1);
178: for (int i = 0; i < strArray.length - 1; ++i) {
179: assertEquals("fwd str copy index " + i, i, Integer.valueOf(
180: strArray[i]).intValue());
181: }
182: assertEquals("fwd str copy index " + (strArray.length - 2),
183: strArray.length - 2, Integer.valueOf(
184: strArray[strArray.length - 1]).intValue());
185: /*
186: * TODO(jat): how is it supposed to behave with overlapped copies if there
187: * is a failure in the middle of the copy? We should figure that out and
188: * test for it.
189: */
190: }
191:
192: public void testArraycopyPrimitives() {
193: int[] src = new int[] { 0, 1, 2, 3, 4, 5, 6, 7 };
194: int[] dest = new int[8];
195:
196: System.arraycopy(src, 0, dest, 0, src.length);
197: for (int i = 0; i < src.length; ++i) {
198: assertEquals(src[i], dest[i]);
199: }
200: System.arraycopy(src, 2, dest, 1, 5);
201: assertEquals(0, dest[0]);
202: for (int i = 1; i < 6; ++i) {
203: assertEquals(src[i + 1], dest[i]);
204: }
205: }
206: }
|