commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject svn commit: r833358 - in /commons/proper/lang/trunk/src: java/org/apache/commons/lang/Range.java test/org/apache/commons/lang/RangeTest.java
Date Fri, 06 Nov 2009 11:45:39 GMT
Author: bayard
Date: Fri Nov  6 11:45:38 2009
New Revision: 833358

URL: http://svn.apache.org/viewvc?rev=833358&view=rev
Log:
Adding more tests, plus a rename of lessThan to elementBefore; greaterThan to elementAfter
and bugfix so the new code works - LANG-551

Modified:
    commons/proper/lang/trunk/src/java/org/apache/commons/lang/Range.java
    commons/proper/lang/trunk/src/test/org/apache/commons/lang/RangeTest.java

Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/Range.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/Range.java?rev=833358&r1=833357&r2=833358&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/Range.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/Range.java Fri Nov  6 11:45:38
2009
@@ -160,7 +160,6 @@
      *
      * @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 @@
         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

Modified: commons/proper/lang/trunk/src/test/org/apache/commons/lang/RangeTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang/RangeTest.java?rev=833358&r1=833357&r2=833358&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/org/apache/commons/lang/RangeTest.java (original)
+++ commons/proper/lang/trunk/src/test/org/apache/commons/lang/RangeTest.java Fri Nov  6 11:45:38
2009
@@ -51,9 +51,6 @@
         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 @@
         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 @@
         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 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)));
+
+    }
+
 }



Mime
View raw message