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 MutableFloatTest extends TestCase { 30 31 public MutableFloatTest(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(MutableFloatTest.class); 41 } 42 43 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 (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 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 (0), new MutableFloat().getValue()); 61 62 mutNum.setValue(1); 63 assertEquals(1f, mutNum.floatValue(), 0.0001f); 64 assertEquals(new Float (1f), mutNum.getValue()); 65 66 mutNum.setValue(new Float (2f)); 67 assertEquals(2f, mutNum.floatValue(), 0.0001f); 68 assertEquals(new Float (2f), mutNum.getValue()); 69 70 mutNum.setValue(new MutableFloat(3f)); 71 assertEquals(3f, mutNum.floatValue(), 0.0001f); 72 assertEquals(new Float (3f), 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 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 (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 (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 ex) {} 132 try { 133 mutNum.compareTo(new Float (0f)); 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 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 |