Commits

Armin Rigo committed 95bc992

Bah, fix _length_estimate() and test it.

Comments (0)

Files changed (2)

rpython/memory/support.py

                 cur = next
             free_non_gc_object(self)
 
-        def _length_estimate(self):
+        def length(self):
             chunk = self.chunk
+            result = 0
             count = self.used_in_last_chunk
             while chunk:
+                result += count
                 chunk = chunk.next
-                count += chunk_size
-            return count
+                count = chunk_size
+            return result
 
         def foreach(self, callback, arg):
             """Invoke 'callback(address, arg)' for all addresses in the stack.
         foreach._annspecialcase_ = 'specialize:arg(1)'
 
         def stack2dict(self):
-            result = AddressDict(self._length_estimate())
+            result = AddressDict(self.length())
             self.foreach(_add_in_dict, result)
             return result
 

rpython/memory/test/test_support.py

             assert a == addrs[i]
         assert not ll.non_empty()
 
+    def test_length(self):
+        AddressStack = get_address_stack(10)
+        ll = AddressStack()
+        a = raw_malloc(llmemory.sizeof(lltype.Signed))
+        for i in range(42):
+            assert ll.length() == i
+            ll.append(a)
+        for i in range(42-1, -1, -1):
+            b = ll.pop()
+            assert b == a
+            assert ll.length() == i
+
 
 class TestAddressDeque:
     def test_big_access(self):