3 from collections
import namedtuple
7 class TestPysize(unittest
.TestCase
):
8 def test_empty_list(self
):
10 self
.assertEqual(sys
.getsizeof(empty_list
),
11 pysize
.get_size(empty_list
))
13 def test_list_of_collections(self
):
14 collection_list
= [[], {}, ()]
15 pointer_byte_size
= 8*len(collection_list
)
16 empty_list_size
= sys
.getsizeof([])
17 empty_tuple_size
= sys
.getsizeof(())
18 empty_dict_size
= sys
.getsizeof({})
19 expected_size
= empty_list_size
* 2 + empty_tuple_size
+\
20 empty_dict_size
+ pointer_byte_size
22 self
.assertEqual(expected_size
,
23 pysize
.get_size(collection_list
))
25 def test_no_double_counting(self
):
29 self
.assertEqual(pysize
.get_size(obj
), pysize
.get_size(obj2
) + 8)
31 def test_gracefully_handles_self_referential_objects(self
):
37 self
.assertEqual(pysize
.get_size(obj
), pysize
.get_size(obj
.prop
))
39 def test_strings_pv3_compat(self
):
41 self
.assertEqual(sys
.getsizeof(test_string
), pysize
.get_size(test_string
))
43 def test_custom_class(self
):
46 def __init__(self
, x
, y
):
51 self
.assertEqual(pysize
.get_size(point
),
52 sys
.getsizeof(point
) +
53 sys
.getsizeof(point
.__dict
__) +
59 def test_namedtuple(self
):
60 Point
= namedtuple('Point', ['x', 'y'])
62 self
.assertEqual(pysize
.get_size(point
),
63 sys
.getsizeof(point
) +
67 def test_subclass_of_namedtuple(self
):
69 class Point(namedtuple('Point', ['x', 'y'])):
73 self
.assertEqual(pysize
.get_size(point
),
74 sys
.getsizeof(point
) +
75 sys
.getsizeof(point
.__dict
__) +
79 def test_subclass_of_namedtuple_with_slots(self
):
81 class Point(namedtuple('Point', ['x', 'y'])):
85 self
.assertEqual(pysize
.get_size(point
),
86 sys
.getsizeof(point
) +
92 __slots__
= ["number1"]
93 def __init__(self
, number1
):
94 self
.number1
= number1
97 __slots__
= ["number1", "number2"]
98 def __init__(self
, number1
,number2
):
99 self
.number1
= number1
100 self
.number2
= number2
102 class slots3(object):
103 __slots__
= ["number1", "number2", "number3"]
104 def __init__(self
, number1
, number2
, number3
):
105 self
.number1
= number1
106 self
.number2
= number2
107 self
.number3
= number3
115 if hasattr(sys
.version_info
, 'major') and sys
.version_info
.major
== 3:
118 # base 40 for the class, 28 per integer, +8 per element
119 self
.assertEqual(pysize
.get_size(s2
), pysize
.get_size(s1
) + 28 + 4 + version_addition
)
120 self
.assertEqual(pysize
.get_size(s3
), pysize
.get_size(s2
) + 28 + 4 + version_addition
)
121 self
.assertEqual(pysize
.get_size(s3
), pysize
.get_size(s1
) + 56 + 8 + version_addition
* 2) # *2 for the num of variables in difference
124 if __name__
== '__main__':