1 16 package org.apache.commons.lang.math; 17 18 import junit.framework.Test; 19 import junit.framework.TestSuite; 20 21 27 public final class FloatRangeTest extends AbstractRangeTest { 28 29 public FloatRangeTest(String name) { 30 super(name); 31 } 32 33 public static Test suite() { 34 TestSuite suite = new TestSuite(FloatRangeTest.class); 35 suite.setName("FloatRange Tests"); 36 return suite; 37 } 38 39 public void setUp() { 40 super.setUp(); 41 tenToTwenty = new FloatRange(float10, float20); 42 otherRange = new NumberRange(ten, twenty); 43 } 44 45 protected Range createRange(Integer integer1, Integer integer2) { 46 return new FloatRange(integer1, integer2); 47 } 48 protected Range createRange(Integer integer) { 49 return new NumberRange(integer); 50 } 51 52 54 public void testConstructor1a() { 55 FloatRange nr = new FloatRange(8f); 56 assertEquals(float8, nr.getMinimumNumber()); 57 assertEquals(float8, nr.getMaximumNumber()); 58 59 try { 60 new FloatRange(Float.NaN); 61 fail(); 62 } catch (IllegalArgumentException ex) {} 63 } 64 65 public void testConstructor1b() { 66 FloatRange nr = new FloatRange(float8); 67 assertSame(float8, nr.getMinimumNumber()); 68 assertSame(float8, nr.getMaximumNumber()); 69 70 Range r = new FloatRange(nonComparable); 71 72 try { 73 new FloatRange(null); 74 fail(); 75 } catch (IllegalArgumentException ex) {} 76 try { 77 new FloatRange(new Double (Double.NaN)); 78 fail(); 79 } catch (IllegalArgumentException ex) {} 80 } 81 82 public void testConstructor2a() { 83 FloatRange nr = new FloatRange(8f, 10f); 84 assertEquals(float8, nr.getMinimumNumber()); 85 assertEquals(float10, nr.getMaximumNumber()); 86 87 nr = new FloatRange(10f, 8f); 88 assertEquals(float8, nr.getMinimumNumber()); 89 assertEquals(float10, nr.getMaximumNumber()); 90 91 try { 92 new FloatRange(Float.NaN, 8f); 93 fail(); 94 } catch (IllegalArgumentException ex) {} 95 } 96 97 public void testConstructor2b() { 98 FloatRange nr = new FloatRange(float8, float10); 99 assertSame(float8, nr.getMinimumNumber()); 100 assertSame(float10, nr.getMaximumNumber()); 101 102 nr = new FloatRange(float10, float8); 103 assertSame(float8, nr.getMinimumNumber()); 104 assertSame(float10, nr.getMaximumNumber()); 105 106 nr = new FloatRange(float8, float10); 107 assertSame(float8, nr.getMinimumNumber()); 108 assertEquals(float10, nr.getMaximumNumber()); 109 110 try { 112 new FloatRange(float8, null); 113 fail(); 114 } catch (IllegalArgumentException ex) {} 115 try { 116 new FloatRange(null, float8); 117 fail(); 118 } catch (IllegalArgumentException ex) {} 119 try { 120 new FloatRange(null, null); 121 fail(); 122 } catch (IllegalArgumentException ex) {} 123 124 try { 125 new FloatRange(new Double (Double.NaN), float10); 126 fail(); 127 } catch (IllegalArgumentException ex) {} 128 } 129 130 132 public void testContainsNumber() { 133 assertEquals(false, tenToTwenty.containsNumber(null)); 134 assertEquals(true, tenToTwenty.containsNumber(nonComparable)); 135 136 assertEquals(false, tenToTwenty.containsNumber(five)); 137 assertEquals(true, tenToTwenty.containsNumber(ten)); 138 assertEquals(true, tenToTwenty.containsNumber(fifteen)); 139 assertEquals(true, tenToTwenty.containsNumber(twenty)); 140 assertEquals(false, tenToTwenty.containsNumber(twentyFive)); 141 142 assertEquals(false, tenToTwenty.containsNumber(long8)); 143 assertEquals(true, tenToTwenty.containsNumber(long10)); 144 assertEquals(true, tenToTwenty.containsNumber(long12)); 145 assertEquals(true, tenToTwenty.containsNumber(long20)); 146 assertEquals(false, tenToTwenty.containsNumber(long21)); 147 148 assertEquals(false, tenToTwenty.containsNumber(double8)); 149 assertEquals(true, tenToTwenty.containsNumber(double10)); 150 assertEquals(true, tenToTwenty.containsNumber(double12)); 151 assertEquals(true, tenToTwenty.containsNumber(double20)); 152 assertEquals(false, tenToTwenty.containsNumber(double21)); 153 154 assertEquals(false, tenToTwenty.containsNumber(float8)); 155 assertEquals(true, tenToTwenty.containsNumber(float10)); 156 assertEquals(true, tenToTwenty.containsNumber(float12)); 157 assertEquals(true, tenToTwenty.containsNumber(float20)); 158 assertEquals(false, tenToTwenty.containsNumber(float21)); 159 } 160 161 public void testToString() { 162 String str = tenToTwenty.toString(); 163 assertEquals("Range[10.0,20.0]", str); 164 assertSame(str, tenToTwenty.toString()); 165 assertEquals("Range[-20.0,-10.0]", createRange(new Integer (-20), new Integer (-10)).toString()); 166 } 167 168 170 } 171 | Popular Tags |