001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.lang.mutable;
018:
019: import junit.framework.Test;
020: import junit.framework.TestCase;
021: import junit.framework.TestSuite;
022: import junit.textui.TestRunner;
023:
024: /**
025: * JUnit tests.
026: *
027: * @version $Id: MutableDoubleTest.java 437554 2006-08-28 06:21:41Z bayard $
028: * @see MutableDouble
029: */
030: public class MutableDoubleTest extends TestCase {
031:
032: public MutableDoubleTest(String testName) {
033: super (testName);
034: }
035:
036: public static void main(String[] args) {
037: TestRunner.run(suite());
038: }
039:
040: public static Test suite() {
041: return new TestSuite(MutableDoubleTest.class);
042: }
043:
044: // ----------------------------------------------------------------
045: public void testConstructors() {
046: assertEquals(0d, new MutableDouble().doubleValue(), 0.0001d);
047:
048: assertEquals(1d, new MutableDouble(1d).doubleValue(), 0.0001d);
049:
050: assertEquals(2d, new MutableDouble(new Double(2d))
051: .doubleValue(), 0.0001d);
052: assertEquals(3d, new MutableDouble(new MutableDouble(3d))
053: .doubleValue(), 0.0001d);
054: try {
055: new MutableDouble(null);
056: fail();
057: } catch (NullPointerException ex) {
058: }
059: }
060:
061: public void testGetSet() {
062: final MutableDouble mutNum = new MutableDouble(0d);
063: assertEquals(0d, new MutableDouble().doubleValue(), 0.0001d);
064: assertEquals(new Double(0), new MutableDouble().getValue());
065:
066: mutNum.setValue(1);
067: assertEquals(1d, mutNum.doubleValue(), 0.0001d);
068: assertEquals(new Double(1d), mutNum.getValue());
069:
070: mutNum.setValue(new Double(2d));
071: assertEquals(2d, mutNum.doubleValue(), 0.0001d);
072: assertEquals(new Double(2d), mutNum.getValue());
073:
074: mutNum.setValue(new MutableDouble(3d));
075: assertEquals(3d, mutNum.doubleValue(), 0.0001d);
076: assertEquals(new Double(3d), mutNum.getValue());
077: try {
078: mutNum.setValue(null);
079: fail();
080: } catch (NullPointerException ex) {
081: }
082: try {
083: mutNum.setValue("0");
084: fail();
085: } catch (ClassCastException ex) {
086: }
087: }
088:
089: public void testNanInfinite() {
090: MutableDouble mutNum = new MutableDouble(Double.NaN);
091: assertEquals(true, mutNum.isNaN());
092:
093: mutNum = new MutableDouble(Double.POSITIVE_INFINITY);
094: assertEquals(true, mutNum.isInfinite());
095:
096: mutNum = new MutableDouble(Double.NEGATIVE_INFINITY);
097: assertEquals(true, mutNum.isInfinite());
098: }
099:
100: public void testEquals() {
101: final MutableDouble mutNumA = new MutableDouble(0d);
102: final MutableDouble mutNumB = new MutableDouble(0d);
103: final MutableDouble mutNumC = new MutableDouble(1d);
104:
105: assertEquals(true, mutNumA.equals(mutNumA));
106: assertEquals(true, mutNumA.equals(mutNumB));
107: assertEquals(true, mutNumB.equals(mutNumA));
108: assertEquals(true, mutNumB.equals(mutNumB));
109: assertEquals(false, mutNumA.equals(mutNumC));
110: assertEquals(false, mutNumB.equals(mutNumC));
111: assertEquals(true, mutNumC.equals(mutNumC));
112: assertEquals(false, mutNumA.equals(null));
113: assertEquals(false, mutNumA.equals(new Double(0d)));
114: assertEquals(false, mutNumA.equals("0"));
115: }
116:
117: public void testHashCode() {
118: final MutableDouble mutNumA = new MutableDouble(0d);
119: final MutableDouble mutNumB = new MutableDouble(0d);
120: final MutableDouble mutNumC = new MutableDouble(1d);
121:
122: assertEquals(true, mutNumA.hashCode() == mutNumA.hashCode());
123: assertEquals(true, mutNumA.hashCode() == mutNumB.hashCode());
124: assertEquals(false, mutNumA.hashCode() == mutNumC.hashCode());
125: assertEquals(true, mutNumA.hashCode() == new Double(0d)
126: .hashCode());
127: }
128:
129: public void testCompareTo() {
130: final MutableDouble mutNum = new MutableDouble(0d);
131:
132: assertEquals(0, mutNum.compareTo(new MutableDouble(0d)));
133: assertEquals(+1, mutNum.compareTo(new MutableDouble(-1d)));
134: assertEquals(-1, mutNum.compareTo(new MutableDouble(1d)));
135: try {
136: mutNum.compareTo(null);
137: fail();
138: } catch (NullPointerException ex) {
139: }
140: try {
141: mutNum.compareTo(new Double(0d));
142: fail();
143: } catch (ClassCastException ex) {
144: }
145: try {
146: mutNum.compareTo("0");
147: fail();
148: } catch (ClassCastException ex) {
149: }
150: }
151:
152: public void testPrimitiveValues() {
153: MutableDouble mutNum = new MutableDouble(1.7);
154:
155: assertEquals(1.7F, mutNum.floatValue(), 0);
156: assertEquals(1.7, mutNum.doubleValue(), 0);
157: assertEquals((byte) 1, mutNum.byteValue());
158: assertEquals((short) 1, mutNum.shortValue());
159: assertEquals(1, mutNum.intValue());
160: assertEquals(1L, mutNum.longValue());
161: }
162:
163: public void testToDouble() {
164: assertEquals(new Double(0d), new MutableDouble(0d).toDouble());
165: assertEquals(new Double(12.3d), new MutableDouble(12.3d)
166: .toDouble());
167: }
168:
169: public void testIncrement() {
170: MutableDouble mutNum = new MutableDouble(1);
171: mutNum.increment();
172:
173: assertEquals(2, mutNum.intValue());
174: assertEquals(2L, mutNum.longValue());
175: }
176:
177: public void testDecrement() {
178: MutableDouble mutNum = new MutableDouble(1);
179: mutNum.decrement();
180:
181: assertEquals(0, mutNum.intValue());
182: assertEquals(0L, mutNum.longValue());
183: }
184:
185: public void testAddValuePrimitive() {
186: MutableDouble mutNum = new MutableDouble(1);
187: mutNum.add(1.1d);
188:
189: assertEquals(2.1d, mutNum.doubleValue(), 0.01d);
190: }
191:
192: public void testAddValueObject() {
193: MutableDouble mutNum = new MutableDouble(1);
194: mutNum.add(new Double(1.1d));
195:
196: assertEquals(2.1d, mutNum.doubleValue(), 0.01d);
197: }
198:
199: public void testSubtractValuePrimitive() {
200: MutableDouble mutNum = new MutableDouble(1);
201: mutNum.subtract(0.9d);
202:
203: assertEquals(0.1d, mutNum.doubleValue(), 0.01d);
204: }
205:
206: public void testSubtractValueObject() {
207: MutableDouble mutNum = new MutableDouble(1);
208: mutNum.subtract(new Double(0.9d));
209:
210: assertEquals(0.1d, mutNum.doubleValue(), 0.01d);
211: }
212:
213: public void testToString() {
214: assertEquals("0.0", new MutableDouble(0d).toString());
215: assertEquals("10.0", new MutableDouble(10d).toString());
216: assertEquals("-123.0", new MutableDouble(-123d).toString());
217: }
218:
219: }
|