KickJava   Java API By Example, From Geeks To Geeks.

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


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: MutableDoubleTest.java 161244 2005-04-14 06:16:36Z ggregory $
27  * @see MutableDouble
28  */

29 public class MutableDoubleTest extends TestCase {
30
31     public MutableDoubleTest(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(MutableDoubleTest.class);
41     }
42
43     // ----------------------------------------------------------------
44
public void testConstructors() {
45         assertEquals(0d, new MutableDouble().doubleValue(), 0.0001d);
46         
47         assertEquals(1d, new MutableDouble(1d).doubleValue(), 0.0001d);
48         
49         assertEquals(2d, new MutableDouble(new Double JavaDoc(2d)).doubleValue(), 0.0001d);
50         assertEquals(3d, new MutableDouble(new MutableDouble(3d)).doubleValue(), 0.0001d);
51         try {
52             new MutableDouble(null);
53             fail();
54         } catch (NullPointerException JavaDoc ex) {}
55     }
56
57     public void testGetSet() {
58         final MutableDouble mutNum = new MutableDouble(0d);
59         assertEquals(0d, new MutableDouble().doubleValue(), 0.0001d);
60         assertEquals(new Double JavaDoc(0), new MutableDouble().getValue());
61         
62         mutNum.setValue(1);
63         assertEquals(1d, mutNum.doubleValue(), 0.0001d);
64         assertEquals(new Double JavaDoc(1d), mutNum.getValue());
65         
66         mutNum.setValue(new Double JavaDoc(2d));
67         assertEquals(2d, mutNum.doubleValue(), 0.0001d);
68         assertEquals(new Double JavaDoc(2d), mutNum.getValue());
69         
70         mutNum.setValue(new MutableDouble(3d));
71         assertEquals(3d, mutNum.doubleValue(), 0.0001d);
72         assertEquals(new Double JavaDoc(3d), 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         MutableDouble mutNum = new MutableDouble(Double.NaN);
85         assertEquals(true, mutNum.isNaN());
86         
87         mutNum = new MutableDouble(Double.POSITIVE_INFINITY);
88         assertEquals(true, mutNum.isInfinite());
89         
90         mutNum = new MutableDouble(Double.NEGATIVE_INFINITY);
91         assertEquals(true, mutNum.isInfinite());
92     }
93
94     public void testEquals() {
95         final MutableDouble mutNumA = new MutableDouble(0d);
96         final MutableDouble mutNumB = new MutableDouble(0d);
97         final MutableDouble mutNumC = new MutableDouble(1d);
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 Double JavaDoc(0d)));
108         assertEquals(false, mutNumA.equals("0"));
109     }
110
111     public void testHashCode() {
112         final MutableDouble mutNumA = new MutableDouble(0d);
113         final MutableDouble mutNumB = new MutableDouble(0d);
114         final MutableDouble mutNumC = new MutableDouble(1d);
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 Double JavaDoc(0d).hashCode());
120     }
121
122     public void testCompareTo() {
123         final MutableDouble mutNum = new MutableDouble(0d);
124
125         assertEquals(0, mutNum.compareTo(new MutableDouble(0d)));
126         assertEquals(+1, mutNum.compareTo(new MutableDouble(-1d)));
127         assertEquals(-1, mutNum.compareTo(new MutableDouble(1d)));
128         try {
129             mutNum.compareTo(null);
130             fail();
131         } catch (NullPointerException JavaDoc ex) {}
132         try {
133             mutNum.compareTo(new Double JavaDoc(0d));
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         MutableDouble mutNum = new MutableDouble(1.7);
144         
145         assertEquals( 1.7F, mutNum.floatValue(), 0 );
146         assertEquals( 1.7, mutNum.doubleValue(), 0 );
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 MutableDouble(0d).toString());
155         assertEquals("10.0", new MutableDouble(10d).toString());
156         assertEquals("-123.0", new MutableDouble(-123d).toString());
157     }
158
159 }
160
Popular Tags