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.dev.jjs.test;
017:
018: import com.google.gwt.junit.client.GWTTestCase;
019:
020: /**
021: * Tests autoboxing.
022: */
023: public class AutoboxTest extends GWTTestCase {
024:
025: private boolean unboxedBoolean = true;
026:
027: private byte unboxedByte = (byte) 0xAB;
028:
029: private char unboxedChar = 'c';
030:
031: private short unboxedShort = 6550;
032:
033: private int unboxedInt = 20000000;
034:
035: private long unboxedLong = 1231231231231L;
036:
037: private float unboxedFloat = 1.2F;
038:
039: private double unboxedDouble = 1.2323;
040:
041: private Boolean boxedBoolean = Boolean.TRUE;
042:
043: private Byte boxedByte = new Byte((byte) 0xAB);
044:
045: private Character boxedChar = new Character('c');
046:
047: private Short boxedShort = new Short((short) 6550);
048:
049: private Integer boxedInt = new Integer(20000000);
050:
051: private Long boxedLong = new Long(1231231231231L);
052:
053: private Float boxedFloat = new Float(1.2F);
054:
055: private Double boxedDouble = new Double(1.2323);
056:
057: public String getModuleName() {
058: return "com.google.gwt.dev.jjs.CompilerSuite";
059: }
060:
061: public void testBoxing() {
062: Boolean boolean_ = unboxedBoolean;
063: assertTrue(boolean_.booleanValue() == unboxedBoolean);
064: Byte byte_ = unboxedByte;
065: assertTrue(byte_.byteValue() == unboxedByte);
066: Character char_ = unboxedChar;
067: assertTrue(char_.charValue() == unboxedChar);
068: Short short_ = unboxedShort;
069: assertTrue(short_.shortValue() == unboxedShort);
070: Integer int_ = unboxedInt;
071: assertTrue(int_.intValue() == unboxedInt);
072: Long long_ = unboxedLong;
073: assertTrue(long_.longValue() == unboxedLong);
074: Float float_ = unboxedFloat;
075: assertTrue(float_.floatValue() == unboxedFloat);
076: Double double_ = unboxedDouble;
077: assertTrue(double_.doubleValue() == unboxedDouble);
078:
079: // test boxing of return values
080: assertTrue(box(unboxedBoolean).booleanValue() == unboxedBoolean);
081: assertTrue(box(unboxedByte).byteValue() == unboxedByte);
082: assertTrue(box(unboxedShort).shortValue() == unboxedShort);
083: assertTrue(box(unboxedChar).charValue() == unboxedChar);
084: assertTrue(box(unboxedInt).intValue() == unboxedInt);
085: assertTrue(box(unboxedLong).longValue() == unboxedLong);
086: assertTrue(box(unboxedFloat).floatValue() == unboxedFloat);
087: assertTrue(box(unboxedDouble).doubleValue() == unboxedDouble);
088:
089: // test boxing of parameters
090: assertTrue(unbox(unboxedBoolean) == unboxedBoolean);
091: assertTrue(unbox(unboxedByte) == unboxedByte);
092: assertTrue(unbox(unboxedShort) == unboxedShort);
093: assertTrue(unbox(unboxedChar) == unboxedChar);
094: assertTrue(unbox(unboxedInt) == unboxedInt);
095: assertTrue(unbox(unboxedLong) == unboxedLong);
096: assertTrue(unbox(unboxedFloat) == unboxedFloat);
097: assertTrue(unbox(unboxedDouble) == unboxedDouble);
098: }
099:
100: /* TODO: Determine whether we fully support the JLS spec in regards to
101: * caching of autoboxed values.
102: *
103: public void testCaching() {
104: }
105: */
106:
107: public void testUnboxing() {
108: boolean boolean_ = boxedBoolean;
109: assertTrue(boolean_ == boxedBoolean.booleanValue());
110: byte byte_ = boxedByte;
111: assertTrue(byte_ == boxedByte.byteValue());
112: char char_ = boxedChar;
113: assertTrue(char_ == boxedChar.charValue());
114: short short_ = boxedShort;
115: assertTrue(short_ == boxedShort.shortValue());
116: int int_ = boxedInt;
117: assertTrue(int_ == boxedInt.intValue());
118: long long_ = boxedLong;
119: assertTrue(long_ == boxedLong.longValue());
120: float float_ = boxedFloat;
121: assertTrue(float_ == boxedFloat.floatValue());
122: double double_ = boxedDouble;
123: assertTrue(double_ == boxedDouble.doubleValue());
124:
125: // test unboxing of return values
126: assertTrue(unbox(boxedBoolean) == unboxedBoolean);
127: assertTrue(unbox(boxedByte) == unboxedByte);
128: assertTrue(unbox(boxedShort) == unboxedShort);
129: assertTrue(unbox(boxedChar) == unboxedChar);
130: assertTrue(unbox(boxedInt) == unboxedInt);
131: assertTrue(unbox(boxedLong) == unboxedLong);
132: assertTrue(unbox(boxedFloat) == unboxedFloat);
133: assertTrue(unbox(boxedDouble) == unboxedDouble);
134:
135: // test unboxing of parameters
136: assertTrue(box(boxedBoolean).booleanValue() == unboxedBoolean);
137: assertTrue(box(boxedByte).byteValue() == unboxedByte);
138: assertTrue(box(boxedShort).shortValue() == unboxedShort);
139: assertTrue(box(boxedChar).charValue() == unboxedChar);
140: assertTrue(box(boxedInt).intValue() == unboxedInt);
141: assertTrue(box(boxedLong).longValue() == unboxedLong);
142: assertTrue(box(boxedFloat).floatValue() == unboxedFloat);
143: assertTrue(box(boxedDouble).doubleValue() == unboxedDouble);
144: }
145:
146: private Boolean box(boolean b) {
147: return b;
148: }
149:
150: private Byte box(byte b) {
151: return b;
152: }
153:
154: private Character box(char c) {
155: return c;
156: }
157:
158: private Short box(short s) {
159: return s;
160: }
161:
162: private Integer box(int i) {
163: return i;
164: }
165:
166: private Long box(long l) {
167: return l;
168: }
169:
170: private Float box(float f) {
171: return f;
172: }
173:
174: private Double box(double d) {
175: return d;
176: }
177:
178: private boolean unbox(Boolean b) {
179: return b;
180: }
181:
182: private byte unbox(Byte b) {
183: return b;
184: }
185:
186: private char unbox(Character c) {
187: return c;
188: }
189:
190: private short unbox(Short s) {
191: return s;
192: }
193:
194: private int unbox(Integer i) {
195: return i;
196: }
197:
198: private long unbox(Long l) {
199: return l;
200: }
201:
202: private float unbox(Float f) {
203: return f;
204: }
205:
206: private double unbox(Double d) {
207: return d;
208: }
209: }
|