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: * Unit tests for the Javascript emulation of the Integer/int autoboxed
022: * fundamental type.
023: */
024: public class IntegerTest extends GWTTestCase {
025:
026: public String getModuleName() {
027: return "com.google.gwt.emultest.EmulSuite";
028: }
029:
030: public void testBadStrings() {
031: try {
032: new Integer("05abcd");
033: fail("Constructor should have thrown NumberFormatException");
034: } catch (NumberFormatException e) {
035: // Expected behavior
036: }
037:
038: try {
039: Integer.decode("05abcd");
040: fail("Decode should have thrown NumberFormatException");
041: } catch (NumberFormatException e) {
042: // Expected behavior
043: }
044:
045: try {
046: Integer.parseInt("05abcd");
047: fail("parseInt should have thrown NumberFormatException");
048: } catch (NumberFormatException e) {
049: // Expected behavior
050: }
051:
052: try {
053: Integer.parseInt(String.valueOf(Long.MAX_VALUE));
054: fail("parseInt should reject numbers greater than the range of int");
055: } catch (NumberFormatException e) {
056: // Expected behavior
057: }
058:
059: try {
060: Integer.parseInt(String.valueOf(Long.MIN_VALUE));
061: fail("parseInt should reject numbers less than the range of int");
062: } catch (NumberFormatException e) {
063: // Expected behavior
064: }
065:
066: try {
067: Integer.parseInt(String
068: .valueOf((long) Integer.MAX_VALUE + 1));
069: fail("parseInt should reject numbers greater than the range of int");
070: } catch (NumberFormatException e) {
071: // Expected behavior
072: }
073:
074: try {
075: Integer.parseInt(String
076: .valueOf((long) Integer.MIN_VALUE - 1));
077: fail("parseInt should reject numbers less than the range of int");
078: } catch (NumberFormatException e) {
079: // Expected behavior
080: }
081: }
082:
083: public void testBinaryString() {
084: assertEquals("11000000111001", Integer.toBinaryString(12345));
085: assertEquals("0", Integer.toBinaryString(0));
086: assertEquals("11111111111111111100111111000111", Integer
087: .toBinaryString(-12345));
088: }
089:
090: public void testBitCount() {
091: assertEquals(0, Integer.bitCount(0));
092: assertEquals(1, Integer.bitCount(1));
093: assertEquals(32, Integer.bitCount(-1));
094: assertEquals(31, Integer.bitCount(Integer.MAX_VALUE));
095: assertEquals(1, Integer.bitCount(Integer.MIN_VALUE));
096: }
097:
098: public void testCompareTo() {
099: assertEquals(-1, new Integer(12345)
100: .compareTo(new Integer(12346)));
101: assertEquals(1, new Integer("12345").compareTo(new Integer(
102: 12344)));
103: assertEquals(0, new Integer("12345").compareTo(new Integer(
104: 12345)));
105: }
106:
107: public void testConstants() {
108: assertEquals(32, Integer.SIZE);
109: assertEquals(0x7fffffff, Integer.MAX_VALUE);
110: assertEquals(0x80000000, Integer.MIN_VALUE);
111: }
112:
113: public void testConstructor() {
114: assertEquals(12345, new Integer(12345).intValue());
115: assertEquals(12345, new Integer("12345").intValue());
116: }
117:
118: public void testDecode() {
119: assertEquals(Integer.MAX_VALUE, Integer.decode(
120: String.valueOf(Integer.MAX_VALUE)).intValue());
121: assertEquals(Integer.MIN_VALUE, Integer.decode(
122: String.valueOf(Integer.MIN_VALUE)).intValue());
123: assertEquals(12345, Integer.decode("12345").intValue());
124: assertEquals(31, Integer.decode("0x1f").intValue());
125: assertEquals(-31, Integer.decode("-0X1F").intValue());
126: assertEquals(31, Integer.decode("#1f").intValue());
127: assertEquals(10, Integer.decode("012").intValue());
128: try {
129: Integer.decode("abx");
130: fail();
131: } catch (NumberFormatException e) {
132: // pass
133: }
134: }
135:
136: public void testEquals() {
137: assertFalse(new Integer(12345).equals(new Integer(12346)));
138: assertEquals(new Integer("12345"), new Integer(12345));
139: }
140:
141: public void testHashCode() {
142: assertEquals(1234, new Integer(1234).hashCode());
143: }
144:
145: public void testHighestOneBit() {
146: assertEquals(0, Integer.highestOneBit(0));
147: assertEquals(Integer.MIN_VALUE, Integer.highestOneBit(-1));
148: assertEquals(Integer.MIN_VALUE, Integer.highestOneBit(-256));
149: assertEquals(1, Integer.highestOneBit(1));
150: assertEquals(0x80, Integer.highestOneBit(0x80));
151: assertEquals(0x40000000, Integer
152: .highestOneBit(Integer.MAX_VALUE));
153: }
154:
155: public void testLowestOneBit() {
156: assertEquals(0, Integer.lowestOneBit(0));
157: assertEquals(1, Integer.lowestOneBit(-1));
158: assertEquals(0x100, Integer.lowestOneBit(-256));
159: assertEquals(1, Integer.lowestOneBit(1));
160: assertEquals(0x80, Integer.lowestOneBit(0x80));
161: assertEquals(0x80000000, Integer
162: .lowestOneBit(Integer.MIN_VALUE));
163: }
164:
165: public void testNumberOfLeadingZeros() {
166: assertEquals(32, Integer.numberOfLeadingZeros(0));
167: assertEquals(31, Integer.numberOfLeadingZeros(1));
168: assertEquals(0, Integer.numberOfLeadingZeros(-1));
169: assertEquals(16, Integer.numberOfLeadingZeros(0x8000));
170: assertEquals(1, Integer.numberOfLeadingZeros(Integer.MAX_VALUE));
171: assertEquals(0, Integer.numberOfLeadingZeros(Integer.MIN_VALUE));
172: assertEquals(0, Integer.numberOfLeadingZeros(-0x8000));
173: }
174:
175: public void testNumberOfTrailingZeros() {
176: assertEquals(32, Integer.numberOfTrailingZeros(0));
177: assertEquals(0, Integer.numberOfTrailingZeros(1));
178: assertEquals(0, Integer.numberOfTrailingZeros(-1));
179: assertEquals(15, Integer.numberOfTrailingZeros(0x8000));
180: assertEquals(0, Integer
181: .numberOfTrailingZeros(Integer.MAX_VALUE));
182: assertEquals(31, Integer
183: .numberOfTrailingZeros(Integer.MIN_VALUE));
184: assertEquals(4, Integer.numberOfTrailingZeros(-0x7ff0));
185: }
186:
187: public void testReverse() {
188: assertEquals(0, Integer.reverse(0));
189: assertEquals(-1, Integer.reverse(-1));
190: assertEquals(Integer.MIN_VALUE, Integer.reverse(1));
191: assertEquals(1, Integer.reverse(Integer.MIN_VALUE));
192: assertEquals(0xaaaaaaaa, Integer.reverse(0x55555555));
193: }
194:
195: public void testReverseBytes() {
196: assertEquals(0, Integer.reverseBytes(0));
197: // two-complement bugs?
198: assertEquals(0x84218421, Integer.reverseBytes(0x21842184));
199: assertEquals(0x12481248, Integer.reverseBytes(0x48124812));
200: }
201:
202: public void testRotateLeft() {
203: assertEquals(0, Integer.rotateLeft(0, 4));
204: assertEquals(0x2, Integer.rotateLeft(1, 1));
205: assertEquals(0x10, Integer.rotateLeft(1, 4));
206: assertEquals(-1, Integer.rotateLeft(-1, 4));
207: assertEquals(Integer.MIN_VALUE, Integer.rotateLeft(0x40000000,
208: 1));
209: assertEquals(1, Integer.rotateLeft(Integer.MIN_VALUE, 1));
210: }
211:
212: public void testRotateRight() {
213: assertEquals(0, Integer.rotateRight(0, 4));
214: assertEquals(Integer.MIN_VALUE, Integer.rotateRight(1, 1));
215: assertEquals(0x10000000, Integer.rotateRight(1, 4));
216: assertEquals(-1, Integer.rotateRight(-1, 4));
217: }
218:
219: public void testSignum() {
220: assertEquals(0, Integer.signum(0));
221: assertEquals(1, Integer.signum(1));
222: assertEquals(-1, Integer.signum(-1));
223: assertEquals(1, Integer.signum(Integer.MAX_VALUE));
224: assertEquals(-1, Integer.signum(Integer.MIN_VALUE));
225: }
226:
227: public void testStaticValueOf() {
228: assertEquals(Integer.MIN_VALUE, Integer.valueOf(
229: Integer.MIN_VALUE).intValue());
230: assertEquals(Integer.MAX_VALUE, Integer.valueOf(
231: Integer.MAX_VALUE).intValue());
232: }
233:
234: public void testToHexString() {
235: // TODO: not implemented in our JRE
236: }
237:
238: public void testToString() {
239: assertEquals("12345", new Integer(12345).toString());
240: assertEquals("-12345", new Integer("-12345").toString());
241: }
242:
243: public void testValueOf() {
244: assertEquals(new Integer(12345), Integer.valueOf("12345"));
245: assertEquals(new Integer(1865), Integer.valueOf("12345", 6));
246: assertEquals(12345, Integer.parseInt("12345"));
247: assertEquals(1865, Integer.parseInt("12345", 6));
248: }
249:
250: public void testXValue() {
251: assertEquals("short", (short) 12345, new Integer(12345)
252: .shortValue());
253: assertEquals("long", 1234567890L, new Integer(1234567890)
254: .longValue());
255: assertEquals("double", 12345d,
256: new Integer(12345).doubleValue(), 0.001);
257: assertEquals("float", 12345f, new Integer(12345).floatValue(),
258: 0.01);
259: assertEquals("byte", (byte) 123, new Integer(123).byteValue());
260: assertEquals("integer", 123, new Integer(123).intValue());
261: assertEquals("short overflow", (short) 10713, new Integer(
262: 1234512345).shortValue());
263: assertEquals("double2", 1234512345d, new Integer(1234512345)
264: .doubleValue(), 0.001);
265: // Invalid test right now; we don't coerce to single precision
266: // assertEquals("float2",1234512345f, new
267: // Integer(1234512345).floatValue(),0.001);
268: assertEquals("byte overflow", (byte) -13, new Integer(123123)
269: .byteValue());
270: }
271: }
|