From 58cafbd3e2e8871b93cc3717d771cf068c10a304 Mon Sep 17 00:00:00 2001 From: Sebastian Geerken Date: Tue, 5 Mar 2013 19:47:09 +0100 Subject: Fixed a bug in Vector::bsearch. --- lout/container.cc | 2 ++ test/containers.cc | 23 ++++++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/lout/container.cc b/lout/container.cc index 5e5eda73..de36a6f7 100644 --- a/lout/container.cc +++ b/lout/container.cc @@ -206,6 +206,8 @@ int Vector::bsearch(Object *key, bool mustExist) { // The case !mustExist is not handled by bsearch(3), so here is a // new implementation. + if (numElements == 0) + return mustExist ? -1 : 0; int high = numElements - 1, low = 0; diff --git a/test/containers.cc b/test/containers.cc index d8107bdf..40b2d8c8 100644 --- a/test/containers.cc +++ b/test/containers.cc @@ -36,9 +36,9 @@ void testHashTable () puts (h.toString()); } -void testVector () +void testVector1 () { - puts ("--- testVector ---"); + puts ("--- testVector (1) ---"); Vector v (true, 1); @@ -49,7 +49,23 @@ void testVector () v.sort (); puts (v.toString()); +} + +void testVector2 () +{ + puts ("--- testVector (2) ---"); + Vector v (true, 1); + + v.insertSorted (new String ("one")); + puts (v.toString()); + + v.insertSorted (new String ("two")); + puts (v.toString()); + + v.insertSorted (new String ("three")); + puts (v.toString()); + v.insertSorted (new String ("five")); puts (v.toString()); @@ -83,7 +99,8 @@ int main (int argc, char *argv[]) { testHashSet (); testHashTable (); - testVector (); + testVector1 (); + testVector2 (); return 0; } -- cgit v1.2.3