def test_sizeof_dict(self): thing = {'A': 1, 'B': 2} n = getsizeof(thing) for x in thing: n += getsizeof(x) for x in thing.values(): n += getsizeof(x) self.assertEqual(Memory._sizeof(thing, set()), n)
def test_sizeof_object(self): class A: def __init__(self): self.name = 'elmer' self.age = 10 thing = A() n = getsizeof(thing) n += getsizeof(thing.__dict__) for x in thing.__dict__: n += getsizeof(x) for x in thing.__dict__.values(): n += getsizeof(x) self.assertEqual(Memory._sizeof(thing, set()), n)
def test_format(self): self.assertEqual(Memory.format(20), '20') self.assertEqual(Memory.format(2000), '2 kB') self.assertEqual(Memory.format(2000000), '2 mB') self.assertEqual(Memory.format(2000000000), '2 gB')
def test_sizeof_not_formatted(self): n = Memory.sizeof(1, False) self.assertTrue(isinstance(n, int)) self.assertEqual(n, getsizeof(1))
def test_sizeof(self): s = Memory.sizeof(1) self.assertTrue(isinstance(s, str)) self.assertTrue(len(s))
def test_sizeof_history(self): thing = 1 history = set() history.add(id(thing)) self.assertEqual(Memory._sizeof(thing, history), 0)
def test_sizeof_list(self): thing = ['123', '234'] n = getsizeof(thing) for x in thing: n += getsizeof(x) self.assertEqual(Memory._sizeof(thing, set()), n)
def test_sizeof_string(self): thing = '123' n = getsizeof(thing) self.assertEqual(Memory._sizeof(thing, set()), n)