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: MutableFloatTest.java 437554 2006-08-28 06:21:41Z bayard $
028: * @see MutableFloat
029: */
030: public class MutableFloatTest extends TestCase {
031:
032: public MutableFloatTest(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(MutableFloatTest.class);
042: }
043:
044: // ----------------------------------------------------------------
045: public void testConstructors() {
046: assertEquals(0f, new MutableFloat().floatValue(), 0.0001f);
047:
048: assertEquals(1f, new MutableFloat(1f).floatValue(), 0.0001f);
049:
050: assertEquals(2f, new MutableFloat(new Float(2f)).floatValue(),
051: 0.0001f);
052: assertEquals(3f, new MutableFloat(new MutableFloat(3f))
053: .floatValue(), 0.0001f);
054: try {
055: new MutableFloat(null);
056: fail();
057: } catch (NullPointerException ex) {
058: }
059: }
060:
061: public void testGetSet() {
062: final MutableFloat mutNum = new MutableFloat(0f);
063: assertEquals(0f, new MutableFloat().floatValue(), 0.0001f);
064: assertEquals(new Float(0), new MutableFloat().getValue());
065:
066: mutNum.setValue(1);
067: assertEquals(1f, mutNum.floatValue(), 0.0001f);
068: assertEquals(new Float(1f), mutNum.getValue());
069:
070: mutNum.setValue(new Float(2f));
071: assertEquals(2f, mutNum.floatValue(), 0.0001f);
072: assertEquals(new Float(2f), mutNum.getValue());
073:
074: mutNum.setValue(new MutableFloat(3f));
075: assertEquals(3f, mutNum.floatValue(), 0.0001f);
076: assertEquals(new Float(3f), 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: MutableFloat mutNum = new MutableFloat(Float.NaN);
091: assertEquals(true, mutNum.isNaN());
092:
093: mutNum = new MutableFloat(Float.POSITIVE_INFINITY);
094: assertEquals(true, mutNum.isInfinite());
095:
096: mutNum = new MutableFloat(Float.NEGATIVE_INFINITY);
097: assertEquals(true, mutNum.isInfinite());
098: }
099:
100: public void testEquals() {
101: final MutableFloat mutNumA = new MutableFloat(0f);
102: final MutableFloat mutNumB = new MutableFloat(0f);
103: final MutableFloat mutNumC = new MutableFloat(1f);
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 Float(0f)));
114: assertEquals(false, mutNumA.equals("0"));
115: }
116:
117: public void testHashCode() {
118: final MutableFloat mutNumA = new MutableFloat(0f);
119: final MutableFloat mutNumB = new MutableFloat(0f);
120: final MutableFloat mutNumC = new MutableFloat(1f);
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 Float(0f)
126: .hashCode());
127: }
128:
129: public void testCompareTo() {
130: final MutableFloat mutNum = new MutableFloat(0f);
131:
132: assertEquals(0, mutNum.compareTo(new MutableFloat(0f)));
133: assertEquals(+1, mutNum.compareTo(new MutableFloat(-1f)));
134: assertEquals(-1, mutNum.compareTo(new MutableFloat(1f)));
135: try {
136: mutNum.compareTo(null);
137: fail();
138: } catch (NullPointerException ex) {
139: }
140: try {
141: mutNum.compareTo(new Float(0f));
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: MutableFloat mutNum = new MutableFloat(1.7F);
154:
155: assertEquals(1, mutNum.intValue());
156: assertEquals(1.7, mutNum.doubleValue(), 0.00001);
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 testToFloat() {
164: assertEquals(new Float(0f), new MutableFloat(0f).toFloat());
165: assertEquals(new Float(12.3f), new MutableFloat(12.3f)
166: .toFloat());
167: }
168:
169: public void testIncrement() {
170: MutableFloat mutNum = new MutableFloat(1);
171: mutNum.increment();
172:
173: assertEquals(2, mutNum.intValue());
174: assertEquals(2L, mutNum.longValue());
175: }
176:
177: public void testDecrement() {
178: MutableFloat mutNum = new MutableFloat(1);
179: mutNum.decrement();
180:
181: assertEquals(0, mutNum.intValue());
182: assertEquals(0L, mutNum.longValue());
183: }
184:
185: public void testAddValuePrimitive() {
186: MutableFloat mutNum = new MutableFloat(1);
187: mutNum.add(1.1f);
188:
189: assertEquals(2.1f, mutNum.floatValue(), 0.01f);
190: }
191:
192: public void testAddValueObject() {
193: MutableFloat mutNum = new MutableFloat(1);
194: mutNum.add(new Float(1.1f));
195:
196: assertEquals(2.1f, mutNum.floatValue(), 0.01f);
197: }
198:
199: public void testSubtractValuePrimitive() {
200: MutableFloat mutNum = new MutableFloat(1);
201: mutNum.subtract(0.9f);
202:
203: assertEquals(0.1f, mutNum.floatValue(), 0.01f);
204: }
205:
206: public void testSubtractValueObject() {
207: MutableFloat mutNum = new MutableFloat(1);
208: mutNum.subtract(new Float(0.9f));
209:
210: assertEquals(0.1f, mutNum.floatValue(), 0.01f);
211: }
212:
213: public void testToString() {
214: assertEquals("0.0", new MutableFloat(0f).toString());
215: assertEquals("10.0", new MutableFloat(10f).toString());
216: assertEquals("-123.0", new MutableFloat(-123f).toString());
217: }
218:
219: }
|