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