diff options
author | Sebastian Geerken <devnull@localhost> | 2013-05-29 15:58:46 +0200 |
---|---|---|
committer | Sebastian Geerken <devnull@localhost> | 2013-05-29 15:58:46 +0200 |
commit | 425e2d4d38d9fa566132363cbb51a83258f710e9 (patch) | |
tree | 7c81b2dfe7e7372d9cafe0e88bec085809efdda2 | |
parent | e2ee1a5c85aedf85a2dbfa1eea046d4e34847bf9 (diff) |
Fixed a bug in Vector::bsearch (empty vector).
-rw-r--r-- | lout/container.cc | 5 | ||||
-rw-r--r-- | test/containers.cc | 19 |
2 files changed, 22 insertions, 2 deletions
diff --git a/lout/container.cc b/lout/container.cc index 7197ba74..908df4ae 100644 --- a/lout/container.cc +++ b/lout/container.cc @@ -208,8 +208,9 @@ int Vector::bsearch(Object *key, bool mustExist, int start, int end, { // The case !mustExist is not handled by bsearch(3), so here is a // new implementation. - if (start >= end) - return mustExist ? -1 : 0; + + if (start > end) + return mustExist ? -1 : start; int low = start, high = end; diff --git a/test/containers.cc b/test/containers.cc index af76bf5e..9b93f158 100644 --- a/test/containers.cc +++ b/test/containers.cc @@ -110,12 +110,31 @@ void testVector2 () } } +void testVector3 () +{ + // Regression test: resulted once incorrently (0, 2, 3), should + // result in (1, 2, 3). + + puts ("--- testVector (3) ---"); + + Vector<String> v (true, 1); + String k ("omega"); + + v.put (new String ("alpha")); + printf (" -> %d\n", v.bsearch (&k, false)); + v.put (new String ("beta")); + printf (" -> %d\n", v.bsearch (&k, false)); + v.put (new String ("gamma")); + printf (" -> %d\n", v.bsearch (&k, false)); +} + int main (int argc, char *argv[]) { testHashSet (); testHashTable (); testVector1 (); testVector2 (); + testVector3 (); return 0; } |