1 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 29 public class MutableDoubleTest extends TestCase { 30 31 public MutableDoubleTest(String testName) { 32 super(testName); 33 } 34 35 public static void main(String [] args) { 36 TestRunner.run(suite()); 37 } 38 39 public static Test suite() { 40 return new TestSuite(MutableDoubleTest.class); 41 } 42 43 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 (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 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 (0), new MutableDouble().getValue()); 61 62 mutNum.setValue(1); 63 assertEquals(1d, mutNum.doubleValue(), 0.0001d); 64 assertEquals(new Double (1d), mutNum.getValue()); 65 66 mutNum.setValue(new Double (2d)); 67 assertEquals(2d, mutNum.doubleValue(), 0.0001d); 68 assertEquals(new Double (2d), mutNum.getValue()); 69 70 mutNum.setValue(new MutableDouble(3d)); 71 assertEquals(3d, mutNum.doubleValue(), 0.0001d); 72 assertEquals(new Double (3d), mutNum.getValue()); 73 try { 74 mutNum.setValue(null); 75 fail(); 76 } catch (NullPointerException ex) {} 77 try { 78 mutNum.setValue("0"); 79 fail(); 80 } catch (ClassCastException 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 (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 (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 ex) {} 132 try { 133 mutNum.compareTo(new Double (0d)); 134 fail(); 135 } catch (ClassCastException ex) {} 136 try { 137 mutNum.compareTo("0"); 138 fail(); 139 } catch (ClassCastException 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 |