~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test__simple_set.py

  • Committer: John Arbash Meinel
  • Date: 2010-07-28 21:36:44 UTC
  • mto: This revision was merged to the branch mainline in revision 5369.
  • Revision ID: john@arbash-meinel.com-20100728213644-3k50bbdkq4r1fb9j
SimpleSet now has a __sizeof__ member which knows about its internal table.

Show diffs side-by-side

added added

removed removed

Lines of Context:
379
379
        # And even removing an item still causes it to fail
380
380
        obj.discard(k2)
381
381
        self.assertRaises(RuntimeError, iterator.next)
 
382
 
 
383
    def get_platform_size(self):
 
384
        """Try to determine if we are on 32-bit or 64-bit architecture."""
 
385
        sizeof = getattr(sys, 'getsizeof', None)
 
386
        if sizeof is not None:
 
387
            int_size = sizeof(1)
 
388
            # A python Int is 3 longs
 
389
            # 1: PyType *
 
390
            # 2: refcnt
 
391
            # 3: long val
 
392
            return int_size / 3
 
393
        return None
 
394
 
 
395
    def test__sizeof__(self):
 
396
        # SimpleSet needs a custom sizeof implementation, because it allocates
 
397
        # memory that Python cannot directly see (_table).
 
398
        # The size of a SimpleSet object is (in 32/64-bit objects)
 
399
        # 1: PyType *
 
400
        # 2: refcnt
 
401
        # 3: vtable *
 
402
        # 4: _used
 
403
        # 5: _fill
 
404
        # 6: _mask
 
405
        # 7: _table*
 
406
        # (_mask+1) pointers
 
407
        # Default size is 1024 pointers
 
408
        obj = self.module.SimpleSet()
 
409
        n_words = 7 + 1024
 
410
        s = obj.__sizeof__()
 
411
        plat_size = self.get_platform_size()
 
412
        if plat_size is None:
 
413
            # We should either be 64-bit or 32-bit, but I don't know which
 
414
            if s != n_words * 8 and s != n_words * 4:
 
415
                self.fail("SimpleSet.__sizeof__() returned %s which is not"
 
416
                          " a 32-bit or 64-bit multiple of %s words"
 
417
                          % (s, n_words))
 
418
        else:
 
419
            self.assertEqual(n_words * plat_size, s)