You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by rw...@apache.org on 2003/12/01 08:19:56 UTC
cvs commit: jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/util BinarySearch.java
rwaldhoff 2003/11/30 23:19:56
Modified: functor/src/test/org/apache/commons/functor/util
TestBinarySearch.java
functor/src/java/org/apache/commons/functor/util
BinarySearch.java
Log:
fix bug, add test, add todo
Revision Changes Path
1.3 +6 -1 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/util/TestBinarySearch.java
Index: TestBinarySearch.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/util/TestBinarySearch.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- TestBinarySearch.java 24 Jun 2003 15:21:02 -0000 1.2
+++ TestBinarySearch.java 1 Dec 2003 07:19:56 -0000 1.3
@@ -66,6 +66,7 @@
/**
* @author Jason Horman (jason@jhorman.org)
+ * @author Rodney Waldhoff
*/
public class TestBinarySearch extends TestCase {
@@ -101,5 +102,9 @@
position = BinarySearch.execute(list, new Integer(86));
assertEquals(new Integer(86), position);
+
+ position = (Integer)new BinarySearch(new ArrayList(), new Integer(10)).recurse();
+ assertEquals(new Integer(-1), position);
+
}
}
1.2 +10 -2 jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/util/BinarySearch.java
Index: BinarySearch.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/util/BinarySearch.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- BinarySearch.java 24 Jun 2003 15:17:01 -0000 1.1
+++ BinarySearch.java 1 Dec 2003 07:19:56 -0000 1.2
@@ -69,6 +69,7 @@
* search.
*
* @author Jason Horman (jason@jhorman.org)
+ * @author Rodney Waldhoff
*/
public class BinarySearch extends RecursiveFunction {
@@ -105,8 +106,15 @@
* when it is found. If the item is not found -1 (as Integer) is returned.
*/
public Object evaluate() {
+ // TODO: should be using compareTo instead of equals
if (lower == upper) {
- return list.get(upper).equals(item) ? new Integer(upper) : new Integer(-1);
+ if(upper >= list.size()) {
+ return new Integer(-1);
+ } else if(list.get(upper).equals(item)) {
+ return new Integer(upper);
+ } else {
+ return new Integer(-1);
+ }
} else {
int middle = (lower + upper) / 2;
if (item.compareTo(list.get(middle)) > 0) {
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org