Adding more tests, plus a rename of lessThan to elementBefore; greaterThan to elementAfter and bugfix so the new code works - LANG-551
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@833358 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
b7a34131b8
commit
da7e4f3840
|
@ -160,7 +160,6 @@ public class Range<T> {
|
|||
*
|
||||
* @param element the element to test, may be <code>null</code>
|
||||
* @return <code>true</code> if the specified element occurs within this range
|
||||
* @throws IllegalArgumentException if the <code>Number</code> cannot be compared
|
||||
*/
|
||||
public boolean contains(T element) {
|
||||
if(element == null) {
|
||||
|
@ -169,20 +168,32 @@ public class Range<T> {
|
|||
return (comparator.compare(element, getMinimum()) > -1) && (comparator.compare(element, getMaximum()) < 1);
|
||||
}
|
||||
|
||||
public boolean lessThan(T element) {
|
||||
/**
|
||||
* <p>Tests whether the specified element occurs before this range.</p>
|
||||
*
|
||||
* @param element the element to test
|
||||
* @return <code>true</code> if the specified element occurs before this range
|
||||
*/
|
||||
public boolean elementBefore(T element) {
|
||||
if (element == null) {
|
||||
return false;
|
||||
return false; // ??
|
||||
}
|
||||
|
||||
return this.comparator.compare(getMinimum(), element) < 1;
|
||||
return this.comparator.compare(element, getMinimum()) < 0;
|
||||
}
|
||||
|
||||
public boolean greaterThan(T element) {
|
||||
/**
|
||||
* <p>Tests whether the specified element occurs after this range.</p>
|
||||
*
|
||||
* @param element the element to test
|
||||
* @return <code>true</code> if the specified element occurs after this range
|
||||
*/
|
||||
public boolean elementAfter(T element) {
|
||||
if (element == null) {
|
||||
return false;
|
||||
return false; // ??
|
||||
}
|
||||
|
||||
return this.comparator.compare(getMaximum(), element) > -1;
|
||||
return this.comparator.compare(element, getMaximum()) > 0;
|
||||
}
|
||||
|
||||
// Range tests
|
||||
|
|
|
@ -51,9 +51,6 @@ public class RangeTest extends TestCase {
|
|||
doubleRange = new Range<Double>((double) 10, (double) 20);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for 'org.apache.commons.lang.Range.equals(Object)'
|
||||
*/
|
||||
public void testEqualsObject() {
|
||||
assertEquals(byteRange, byteRange);
|
||||
assertEquals(byteRange, byteRange2);
|
||||
|
@ -66,22 +63,25 @@ public class RangeTest extends TestCase {
|
|||
assertFalse(byteRange2.equals("Ni!"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for 'org.apache.commons.lang.Range.hashCode()'
|
||||
*/
|
||||
public void testHashCode() {
|
||||
assertEquals(byteRange.hashCode(), byteRange2.hashCode());
|
||||
assertFalse(byteRange.hashCode() == byteRange3.hashCode());
|
||||
|
||||
assertEquals(intRange.hashCode(), intRange.hashCode());
|
||||
assertTrue(intRange.hashCode() != 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for 'org.apache.commons.lang.Range.toString()'
|
||||
*/
|
||||
public void testToString() {
|
||||
assertNotNull(byteRange.toString());
|
||||
|
||||
String str = intRange.toString();
|
||||
assertEquals("Range[10,20]", str);
|
||||
// assertSame(str, intRange.toString()); // no longer passes - does it matter?
|
||||
assertEquals("Range[-20,-10]", new Range<Integer>(-20, -10).toString());
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
public void testGetMinimum() {
|
||||
assertEquals(10, (int) intRange.getMinimum());
|
||||
assertEquals(10L, (long) longRange.getMinimum());
|
||||
|
@ -106,6 +106,28 @@ public class RangeTest extends TestCase {
|
|||
assertFalse(intRange.contains(25));
|
||||
}
|
||||
|
||||
public void testElementBefore() {
|
||||
assertFalse(intRange.elementBefore(null));
|
||||
|
||||
assertTrue(intRange.elementBefore(5));
|
||||
assertFalse(intRange.elementBefore(10));
|
||||
assertFalse(intRange.elementBefore(15));
|
||||
assertFalse(intRange.elementBefore(20));
|
||||
assertFalse(intRange.elementBefore(25));
|
||||
}
|
||||
|
||||
public void testElementAfter() {
|
||||
assertFalse(intRange.elementAfter(null));
|
||||
|
||||
assertFalse(intRange.elementAfter(5));
|
||||
assertFalse(intRange.elementAfter(10));
|
||||
assertFalse(intRange.elementAfter(15));
|
||||
assertFalse(intRange.elementAfter(20));
|
||||
assertTrue(intRange.elementAfter(25));
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
public void testContainsRange() {
|
||||
|
||||
// null handling
|
||||
|
@ -138,4 +160,36 @@ public class RangeTest extends TestCase {
|
|||
|
||||
}
|
||||
|
||||
public void testOverlapsRange() {
|
||||
|
||||
// null handling
|
||||
assertFalse(intRange.overlapsRange(null));
|
||||
|
||||
// easy inside range
|
||||
assertTrue(intRange.overlapsRange(new Range(12, 18)));
|
||||
|
||||
// outside range on each side
|
||||
assertFalse(intRange.overlapsRange(new Range(32, 45)));
|
||||
assertFalse(intRange.overlapsRange(new Range(2, 8)));
|
||||
|
||||
// equals range
|
||||
assertTrue(intRange.overlapsRange(new Range(10, 20)));
|
||||
|
||||
// overlaps
|
||||
assertTrue(intRange.overlapsRange(new Range(9, 14)));
|
||||
assertTrue(intRange.overlapsRange(new Range(16, 21)));
|
||||
|
||||
// touches lower boundary
|
||||
assertTrue(intRange.overlapsRange(new Range(10, 19)));
|
||||
assertTrue(intRange.overlapsRange(new Range(10, 21)));
|
||||
|
||||
// touches upper boundary
|
||||
assertTrue(intRange.overlapsRange(new Range(11, 20)));
|
||||
assertTrue(intRange.overlapsRange(new Range(9, 20)));
|
||||
|
||||
// negative
|
||||
assertFalse(intRange.overlapsRange(new Range(-11, -18)));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue