-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtest_pysize.py
125 lines (99 loc) · 4.12 KB
/
test_pysize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import sys
import unittest
from collections import namedtuple
import pysize
class TestPysize(unittest.TestCase):
def test_empty_list(self):
empty_list = []
self.assertEqual(sys.getsizeof(empty_list),
pysize.get_size(empty_list))
def test_list_of_collections(self):
collection_list = [[], {}, ()]
pointer_byte_size = 8*len(collection_list)
empty_list_size = sys.getsizeof([])
empty_tuple_size = sys.getsizeof(())
empty_dict_size = sys.getsizeof({})
expected_size = empty_list_size * 2 + empty_tuple_size +\
empty_dict_size + pointer_byte_size
self.assertEqual(expected_size,
pysize.get_size(collection_list))
def test_no_double_counting(self):
rep = ["test1"]
obj = [rep, rep]
obj2 = [rep]
self.assertEqual(pysize.get_size(obj), pysize.get_size(obj2) + 8)
def test_gracefully_handles_self_referential_objects(self):
class Test(object):
pass
obj = Test()
obj.prop = obj
obj2 = Test()
self.assertEqual(pysize.get_size(obj), pysize.get_size(obj.prop))
def test_strings_pv3_compat(self):
test_string = "abc"
self.assertEqual(sys.getsizeof(test_string), pysize.get_size(test_string))
def test_custom_class(self):
class Point(object):
def __init__(self, x, y):
self.x = x
self.y = y
point = Point(3, 4)
self.assertEqual(pysize.get_size(point),
sys.getsizeof(point) +
sys.getsizeof(point.__dict__) +
sys.getsizeof('x') +
sys.getsizeof(3) +
sys.getsizeof('y') +
sys.getsizeof(4))
def test_namedtuple(self):
Point = namedtuple('Point', ['x', 'y'])
point = Point(3, 4)
self.assertEqual(pysize.get_size(point),
sys.getsizeof(point) +
sys.getsizeof(3) +
sys.getsizeof(4))
def test_subclass_of_namedtuple(self):
class Point(namedtuple('Point', ['x', 'y'])):
pass
point = Point(3, 4)
self.assertEqual(pysize.get_size(point),
sys.getsizeof(point) +
sys.getsizeof(point.__dict__) +
sys.getsizeof(3) +
sys.getsizeof(4))
def test_subclass_of_namedtuple_with_slots(self):
class Point(namedtuple('Point', ['x', 'y'])):
__slots__ = ()
point = Point(3, 4)
self.assertEqual(pysize.get_size(point),
sys.getsizeof(point) +
sys.getsizeof(3) +
sys.getsizeof(4))
def test_slots(self):
class slots1(object):
__slots__ = ["number1"]
def __init__(self, number1):
self.number1 = number1
class slots2(object):
__slots__ = ["number1", "number2"]
def __init__(self, number1,number2):
self.number1 = number1
self.number2 = number2
class slots3(object):
__slots__ = ["number1", "number2", "number3"]
def __init__(self, number1, number2, number3):
self.number1 = number1
self.number2 = number2
self.number3 = number3
s1 = slots1(7)
s2 = slots2(3, 4)
s3 = slots3(4, 5, 6)
version_addition = 0
if hasattr(sys.version_info, 'major') and sys.version_info.major == 3:
version_addition = 4
# base 40 for the class, 28 per integer, +8 per element
self.assertEqual(pysize.get_size(s2), pysize.get_size(s1) + 28 + 4 + version_addition)
self.assertEqual(pysize.get_size(s3), pysize.get_size(s2) + 28 + 4 + version_addition)
self.assertEqual(pysize.get_size(s3), pysize.get_size(s1) + 56 + 8 + version_addition * 2) # *2 for the num of variables in difference
if __name__ == '__main__':
unittest.main()