def setUp(self): super(Test_MemObjectProxy, self).setUp() self.moc = _loader.MemObjectCollection() self.moc.add(1024, 'bar', 200) self.moc.add(0, 'foo', 100) self.moc.add(255, 'baz', 300) del self.moc[1024]
def test__sizeof__with_dummy(self): moc = _loader.MemObjectCollection() moc.add(0, 'foo', 100, children=[1234], parent_list=[3456, 7890]) moc.add(1, 'foo', 100, children=[1234], parent_list=[3456, 7890]) del moc[1] self.assertSizeOf(4+1024+8+2+3, moc, extra_size=_memobj_extra_size, has_gc=False)
def test_traverse_multiple_and_parents_and_children(self): moc = _loader.MemObjectCollection() moc.add(1, 'foo', 100) moc.add(2, 'bar', 200, children=[8, 9], parent_list=[10, 11], value='test val') self.assertEqual([1, 'foo', None, 2, 'bar', 'test val', 8, 9, 10, 11], _scanner.get_referents(moc))
def _load(source, using_json, show_prog, input_size, max_parents=None): objs = _loader.MemObjectCollection() for memobj in iter_objs(source, using_json, show_prog, input_size, objs, factory=objs.add): # objs.add automatically adds the object as it is created pass return ObjManager(objs, show_progress=show_prog, max_parents=max_parents)
def test__sizeof__with_reflists(self): moc = _loader.MemObjectCollection() # We should assign the memory for ref-lists to the container. A # ref-list allocates the number of entries + 1 # Each _memobject also takes up moc.add(0, 'foo', 100, children=[1234], parent_list=[3456, 7890]) self.assertSizeOf(4+1024+8+2+3, moc, extra_size=_memobj_extra_size, has_gc=False)
def test__lookup_direct(self): moc = _loader.MemObjectCollection() self.assertEqual(1023, moc._table_mask) self.assertEqual(0, moc._test_lookup(0)) self.assertEqual(0, moc._test_lookup(1024)) self.assertEqual(255, moc._test_lookup(255)) self.assertEqual(933, moc._test_lookup(933)) self.assertEqual(933, moc._test_lookup(933+1024)) self.assertEqual(933, moc._test_lookup(933+1024))
def test__iter__(self): moc = _loader.MemObjectCollection() moc.add(0, 'bar', 100) moc.add(1024, 'baz', 102) moc.add(512, 'bing', 103) self.assertEqual([0, 1024, 512], list(moc)) self.assertEqual([0, 1024, 512], list(moc.iterkeys())) del moc[0] self.assertEqual([1024, 512], list(moc)) self.assertEqual([1024, 512], list(moc.iterkeys()))
def test_keys(self): moc = _loader.MemObjectCollection() moc.add(0, 'bar', 100) moc.add(1024, 'baz', 102) moc.add(512, 'bing', 103) keys = moc.keys() self.assertTrue(isinstance(keys, list)) self.assertEqual([0, 1024, 512], keys) del moc[0] self.assertEqual([1024, 512], moc.keys())
def test_iteritems(self): moc = _loader.MemObjectCollection() moc.add(0, 'bar', 100) moc.add(1024, 'baz', 102) moc.add(512, 'bing', 103) self.assertEqual([(0, 0), (1024, 1024), (512, 512)], [(k, v.address) for k,v in moc.iteritems()]) del moc[0] self.assertEqual([(1024, 1024), (512, 512)], [(k, v.address) for k,v in moc.iteritems()])
def test_add_until_resize(self): moc = _loader.MemObjectCollection() for i in range(1025): moc.add(i, 'foo', 100+i) self.assertEqual(1025, moc._filled) self.assertEqual(1025, moc._active) self.assertEqual(2047, moc._table_mask) mop = moc[1024] self.assertEqual(1024, mop.address) self.assertEqual(1124, mop.size)
def test__len__(self): moc = _loader.MemObjectCollection() self.assertEqual(0, len(moc)) moc.add(0, 'foo', 100) self.assertEqual(1, len(moc)) moc.add(1024, 'foo', 100) self.assertEqual(2, len(moc)) del moc[0] self.assertEqual(1, len(moc)) del moc[1024] self.assertEqual(0, len(moc))
def test__lookup_collide(self): moc = _loader.MemObjectCollection() self.assertEqual(1023, moc._table_mask) self.assertEqual(0, moc._test_lookup(0)) self.assertEqual(0, moc._test_lookup(1024)) moc.add(0, 'foo', 100) self.assertEqual(0, moc._test_lookup(0)) self.assertEqual(1, moc._test_lookup(1024)) moc.add(1024, 'bar', 200) self.assertEqual(0, moc._test_lookup(0)) self.assertEqual(1, moc._test_lookup(1024))
def test__lookup_direct(self): moc = _loader.MemObjectCollection() self.assertEqual(1023, moc._table_mask) self.assertEqual(0, moc._test_lookup(0)) self.assertEqual(0, moc._test_lookup(1024)) self.assertEqual(255, moc._test_lookup(255)) self.assertEqual(933, moc._test_lookup(933)) self.assertEqual(933, moc._test_lookup(933+1024)) if sys.version_info[0] < 3: # 1024L was more idiomatic in Python 2, but is a syntax error in # Python 3. self.assertEqual(933, moc._test_lookup(long(933)+long(1024)))
def test__sizeof__base(self): moc = _loader.MemObjectCollection() # Empty table size is 1024 pointers # 1: PyType* # 2: refcnt # 3: vtable* # 4: _table* # 3 4-byte int attributes # Note that on 64-bit platforms, alignment issues mean we will still # round to a multiple-of-8 bytes. self.assertSizeOf(4+1024, moc, extra_size=_memobj_extra_size, has_gc=False)
def test__contains__(self): moc = _loader.MemObjectCollection() self.assertEqual(0, moc._test_lookup(0)) self.assertEqual(0, moc._test_lookup(1024)) self.assertFalse(0 in moc) self.assertFalse(1024 in moc) moc.add(0, 'foo', 100) self.assertTrue(0 in moc) self.assertFalse(1024 in moc) moc.add(1024, 'bar', 200) self.assertTrue(0 in moc) self.assertTrue(1024 in moc)
def test_items(self): moc = _loader.MemObjectCollection() moc.add(0, 'bar', 100) moc.add(1024, 'baz', 102) moc.add(512, 'bing', 103) items = list(moc.items()) self.assertTrue(isinstance(items, list)) self.assertEqual([(0, 0), (1024, 1024), (512, 512)], [(k, v.address) for k,v in items]) del moc[0] self.assertEqual([(1024, 1024), (512, 512)], [(k, v.address) for k,v in list(moc.items())])
def test__getitem__(self): moc = _loader.MemObjectCollection() def get(offset): return moc[offset] self.assertRaises(KeyError, get, 0) self.assertRaises(KeyError, get, 1024) moc.add(0, 'foo', 100) mop = moc[0] self.assertTrue(isinstance(mop, _loader._MemObjectProxy)) self.assertEqual('foo', mop.type_str) self.assertEqual(100, mop.size) self.assertRaises(KeyError, get, 1024) self.assertTrue(mop is moc[mop])
def test_itervalues_to_tip(self): moc = _loader.MemObjectCollection() moc.add(0, 'bar', 100) moc.add(1024, 'baz', 102) moc.add(512, 'bing', 104) self.assertEqual([0, 1024, 512], [x.address for x in moc.itervalues()]) del moc[0] self.assertEqual([1024, 512], [x.address for x in moc.itervalues()]) moc.add(1023, 'booze', 103) self.assertEqual([1024, 512, 1023], [x.address for x in moc.itervalues()]) del moc[1023] self.assertEqual([1024, 512], [x.address for x in moc.itervalues()])
def test__sizeof__one_item(self): moc = _loader.MemObjectCollection() # We also track the size of the referenced _MemObject entries # Which is: # 1: PyObject *address # 2: PyObject *type_str # 3: long size # 4: *child_list # 5: *value # 6: *parent_list # 7: ulong total_size # 8: *proxy moc.add(0, 'foo', 100) self.assertSizeOf(4+1024+8, moc, extra_size=_memobj_extra_size, has_gc=False)
def test__delitem__(self): moc = _loader.MemObjectCollection() def get(offset): return moc[offset] def delete(offset): del moc[offset] self.assertRaises(KeyError, delete, 0) self.assertRaises(KeyError, delete, 1024) moc.add(0, 'foo', 100) self.assertTrue(0 in moc) self.assertFalse(1024 in moc) self.assertRaises(KeyError, delete, 1024) moc.add(1024, 'bar', 200) del moc[0] self.assertFalse(0 in moc) self.assertRaises(KeyError, get, 0) mop = moc[1024] del moc[mop] self.assertRaises(KeyError, get, 1024)
def test_get(self): moc = _loader.MemObjectCollection() self.assertEqual(None, moc.get(0, None)) moc.add(0, 'foo', 100) self.assertEqual(100, moc.get(0, None).size)
def setUp(self): super(Test_MemObjectProxyIterRecursiveRefs, self).setUp() self.moc = _loader.MemObjectCollection() self.moc.add(1024, 'bar', 200) self.moc.add(0, 'foo', 100) self.moc.add(255, 'baz', 300)
def test_traverse_empty(self): # With nothing present, we return no referents moc = _loader.MemObjectCollection() self.assertEqual([], _scanner.get_referents(moc))
def test__init__(self): moc = _loader.MemObjectCollection() self.assertEqual(0, moc._active) self.assertEqual(0, moc._filled) self.assertEqual(1023, moc._table_mask)
def test_traverse_simple_item(self): moc = _loader.MemObjectCollection() moc.add(1234, 'foo', 100) self.assertEqual([1234, 'foo', None], _scanner.get_referents(moc))