KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > mutable > MutableFloatTest


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.lang.mutable;
17
18 import junit.framework.Test;
19 import junit.framework.TestCase;
20 import junit.framework.TestSuite;
21 import junit.textui.TestRunner;
22
23 /**
24  * JUnit tests.
25  *
26  * @version $Id: MutableFloatTest.java 161244 2005-04-14 06:16:36Z ggregory $
27  * @see MutableFloat
28  */

29 public class MutableFloatTest extends TestCase {
30
31     public MutableFloatTest(String JavaDoc testName) {
32         super(testName);
33     }
34
35     public static void main(String JavaDoc[] args) {
36         TestRunner.run(suite());
37     }
38
39     public static Test suite() {
40         return new TestSuite(MutableFloatTest.class);
41     }
42
43     // ----------------------------------------------------------------
44
public void testConstructors() {
45         assertEquals(0f, new MutableFloat().floatValue(), 0.0001f);
46         
47         assertEquals(1f, new MutableFloat(1f).floatValue(), 0.0001f);
48         
49         assertEquals(2f, new MutableFloat(new Float JavaDoc(2f)).floatValue(), 0.0001f);
50         assertEquals(3f, new MutableFloat(new MutableFloat(3f)).floatValue(), 0.0001f);
51         try {
52             new MutableFloat(null);
53             fail();
54         } catch (NullPointerException JavaDoc ex) {}
55     }
56
57     public void testGetSet() {
58         final MutableFloat mutNum = new MutableFloat(0f);
59         assertEquals(0f, new MutableFloat().floatValue(), 0.0001f);
60         assertEquals(new Float JavaDoc(0), new MutableFloat().getValue());
61         
62         mutNum.setValue(1);
63         assertEquals(1f, mutNum.floatValue(), 0.0001f);
64         assertEquals(new Float JavaDoc(1f), mutNum.getValue());
65         
66         mutNum.setValue(new Float JavaDoc(2f));
67         assertEquals(2f, mutNum.floatValue(), 0.0001f);
68         assertEquals(new Float JavaDoc(2f), mutNum.getValue());
69         
70         mutNum.setValue(new MutableFloat(3f));
71         assertEquals(3f, mutNum.floatValue(), 0.0001f);
72         assertEquals(new Float JavaDoc(3f), mutNum.getValue());
73         try {
74             mutNum.setValue(null);
75             fail();
76         } catch (NullPointerException JavaDoc ex) {}
77         try {
78             mutNum.setValue("0");
79             fail();
80         } catch (ClassCastException JavaDoc ex) {}
81     }
82
83     public void testNanInfinite() {
84         MutableFloat mutNum = new MutableFloat(Float.NaN);
85         assertEquals(true, mutNum.isNaN());
86         
87         mutNum = new MutableFloat(Float.POSITIVE_INFINITY);
88         assertEquals(true, mutNum.isInfinite());
89         
90         mutNum = new MutableFloat(Float.NEGATIVE_INFINITY);
91         assertEquals(true, mutNum.isInfinite());
92     }
93
94     public void testEquals() {
95         final MutableFloat mutNumA = new MutableFloat(0f);
96         final MutableFloat mutNumB = new MutableFloat(0f);
97         final MutableFloat mutNumC = new MutableFloat(1f);
98
99         assertEquals(true, mutNumA.equals(mutNumA));
100         assertEquals(true, mutNumA.equals(mutNumB));
101         assertEquals(true, mutNumB.equals(mutNumA));
102         assertEquals(true, mutNumB.equals(mutNumB));
103         assertEquals(false, mutNumA.equals(mutNumC));
104         assertEquals(false, mutNumB.equals(mutNumC));
105         assertEquals(true, mutNumC.equals(mutNumC));
106         assertEquals(false, mutNumA.equals(null));
107         assertEquals(false, mutNumA.equals(new Float JavaDoc(0f)));
108         assertEquals(false, mutNumA.equals("0"));
109     }
110
111     public void testHashCode() {
112         final MutableFloat mutNumA = new MutableFloat(0f);
113         final MutableFloat mutNumB = new MutableFloat(0f);
114         final MutableFloat mutNumC = new MutableFloat(1f);
115
116         assertEquals(true, mutNumA.hashCode() == mutNumA.hashCode());
117         assertEquals(true, mutNumA.hashCode() == mutNumB.hashCode());
118         assertEquals(false, mutNumA.hashCode() == mutNumC.hashCode());
119         assertEquals(true, mutNumA.hashCode() == new Float JavaDoc(0f).hashCode());
120     }
121
122     public void testCompareTo() {
123         final MutableFloat mutNum = new MutableFloat(0f);
124
125         assertEquals(0, mutNum.compareTo(new MutableFloat(0f)));
126         assertEquals(+1, mutNum.compareTo(new MutableFloat(-1f)));
127         assertEquals(-1, mutNum.compareTo(new MutableFloat(1f)));
128         try {
129             mutNum.compareTo(null);
130             fail();
131         } catch (NullPointerException JavaDoc ex) {}
132         try {
133             mutNum.compareTo(new Float JavaDoc(0f));
134             fail();
135         } catch (ClassCastException JavaDoc ex) {}
136         try {
137             mutNum.compareTo("0");
138             fail();
139         } catch (ClassCastException JavaDoc ex) {}
140     }
141
142     public void testPrimitiveValues() {
143         MutableFloat mutNum = new MutableFloat(1.7F);
144         
145         assertEquals( 1, mutNum.intValue() );
146         assertEquals( 1.7, mutNum.doubleValue(), 0.00001 );
147         assertEquals( (byte) 1, mutNum.byteValue() );
148         assertEquals( (short) 1, mutNum.shortValue() );
149         assertEquals( 1, mutNum.intValue() );
150         assertEquals( 1L, mutNum.longValue() );
151     }
152
153     public void testToString() {
154         assertEquals("0.0", new MutableFloat(0f).toString());
155         assertEquals("10.0", new MutableFloat(10f).toString());
156         assertEquals("-123.0", new MutableFloat(-123f).toString());
157     }
158
159 }
160
Popular Tags