Fix for a bug Andrew Reynolds found for iterators that affects empty CDList<> objects...
authorMorgan Deters <mdeters@gmail.com>
Fri, 25 Mar 2011 05:20:56 +0000 (05:20 +0000)
committerMorgan Deters <mdeters@gmail.com>
Fri, 25 Mar 2011 05:20:56 +0000 (05:20 +0000)
src/context/cdlist_context_memory.h
test/unit/context/cdlist_black.h

index 20d672d31a32c630eb0750c490d18dc19d869218..2fd1ceb8c124ba8f8b1440b52ae84926c914391d 100644 (file)
@@ -476,7 +476,11 @@ public:
    * Returns an iterator pointing to the first item in the list.
    */
   const_iterator begin() const {
-    return const_iterator(&d_headSegment, 0);
+    // This looks curious, but we have to make sure that begin() == end()
+    // for an empty list, and begin() == (head,0) for a nonempty one.
+    // Since the segment spill-over is implemented in
+    // iterator::operator++(), let's reuse it. */
+    return ++const_iterator(&d_headSegment, -1);
   }
 
   /**
index aa25ba9ebe18a20eb20710d5695a5e7fca034f46..6541973bf662747c0dc13dcaae7d174737f14589 100644 (file)
 
 #include "memory.h"
 
+#include "util/exception.h"
 #include "context/context.h"
 #include "context/cdlist.h"
+#include "context/cdlist_context_memory.h"
 
 using namespace std;
 using namespace CVC4::context;
 using namespace CVC4::test;
+using namespace CVC4;
 
 struct DtorSensitiveObject {
   bool& d_dtorCalled;
@@ -133,6 +136,17 @@ public:
     TS_ASSERT_EQUALS(aThirdFalse, false);
   }
 
+  void testEmptyIterators() {
+    CDList<int>* list1 = new(true) CDList<int>(d_context);
+    CDList< int, ContextMemoryAllocator<int> >* list2 =
+      new(d_context->getCMM())
+        CDList< int, ContextMemoryAllocator<int> >(true, d_context, false,
+                                                   ContextMemoryAllocator<int>(d_context->getCMM()));
+    TS_ASSERT_EQUALS(list1->begin(), list1->end());
+    TS_ASSERT_EQUALS(list2->begin(), list2->end());
+    list1->deleteSelf();
+  }
+
   /* setrlimit() totally broken on Mac OS X */
   void testOutOfMemory() {
 #ifdef __APPLE__