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 DoubleRangeTest extends AbstractRangeTest { 28 29 public DoubleRangeTest(String name) { 30 super(name); 31 } 32 33 public static Test suite() { 34 TestSuite suite = new TestSuite(DoubleRangeTest.class); 35 suite.setName("DoubleRange Tests"); 36 return suite; 37 } 38 39 public void setUp() { 40 super.setUp(); 41 tenToTwenty = new DoubleRange(double10, double20); 42 otherRange = new NumberRange(ten, twenty); 43 } 44 45 protected Range createRange(Integer integer1, Integer integer2) { 46 return new DoubleRange(integer1, integer2); 47 } 48 protected Range createRange(Integer integer) { 49 return new NumberRange(integer); 50 } 51 52 54 public void testConstructor1a() { 55 DoubleRange nr = new DoubleRange(8d); 56 assertEquals(double8, nr.getMinimumNumber()); 57 assertEquals(double8, nr.getMaximumNumber()); 58 59 try { 60 new DoubleRange(Double.NaN); 61 fail(); 62 } catch (IllegalArgumentException ex) {} 63 } 64 65 public void testConstructor1b() { 66 DoubleRange nr = new DoubleRange(double8); 67 assertSame(double8, nr.getMinimumNumber()); 68 assertSame(double8, nr.getMaximumNumber()); 69 70 Range r = new DoubleRange(nonComparable); 71 72 try { 73 new DoubleRange(null); 74 fail(); 75 } catch (IllegalArgumentException ex) {} 76 try { 77 new DoubleRange(new Double (Double.NaN)); 78 fail(); 79 } catch (IllegalArgumentException ex) {} 80 } 81 82 public void testConstructor2a() { 83 DoubleRange nr = new DoubleRange(8d, 10d); 84 assertEquals(double8, nr.getMinimumNumber()); 85 assertEquals(double10, nr.getMaximumNumber()); 86 87 nr = new DoubleRange(10d, 8d); 88 assertEquals(double8, nr.getMinimumNumber()); 89 assertEquals(double10, nr.getMaximumNumber()); 90 91 try { 92 new DoubleRange(Double.NaN, 8d); 93 fail(); 94 } catch (IllegalArgumentException ex) {} 95 } 96 97 public void testConstructor2b() { 98 DoubleRange nr = new DoubleRange(double8, double10); 99 assertSame(double8, nr.getMinimumNumber()); 100 assertSame(double10, nr.getMaximumNumber()); 101 102 nr = new DoubleRange(double10, double8); 103 assertSame(double8, nr.getMinimumNumber()); 104 assertSame(double10, nr.getMaximumNumber()); 105 106 nr = new DoubleRange(double8, double10); 107 assertSame(double8, nr.getMinimumNumber()); 108 assertEquals(double10, nr.getMaximumNumber()); 109 110 try { 112 new DoubleRange(double8, null); 113 fail(); 114 } catch (IllegalArgumentException ex) {} 115 try { 116 new DoubleRange(null, double8); 117 fail(); 118 } catch (IllegalArgumentException ex) {} 119 try { 120 new DoubleRange(null, null); 121 fail(); 122 } catch (IllegalArgumentException ex) {} 123 124 try { 125 new DoubleRange(new Double (Double.NaN), double10); 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 |