new file: makefile
[GalaxyCodeBases.git] / python / lib / pysize / test_pysize.py
blob641719ed7075e2b48e875dc77494bd180c5c1245
1 import sys
2 import unittest
3 from collections import namedtuple
5 import pysize
7 class TestPysize(unittest.TestCase):
8 def test_empty_list(self):
9 empty_list = []
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):
26 rep = ["test1"]
27 obj = [rep, rep]
28 obj2 = [rep]
29 self.assertEqual(pysize.get_size(obj), pysize.get_size(obj2) + 8)
31 def test_gracefully_handles_self_referential_objects(self):
32 class Test(object):
33 pass
34 obj = Test()
35 obj.prop = obj
36 obj2 = Test()
37 self.assertEqual(pysize.get_size(obj), pysize.get_size(obj.prop))
39 def test_strings_pv3_compat(self):
40 test_string = "abc"
41 self.assertEqual(sys.getsizeof(test_string), pysize.get_size(test_string))
43 def test_custom_class(self):
45 class Point(object):
46 def __init__(self, x, y):
47 self.x = x
48 self.y = y
50 point = Point(3, 4)
51 self.assertEqual(pysize.get_size(point),
52 sys.getsizeof(point) +
53 sys.getsizeof(point.__dict__) +
54 sys.getsizeof('x') +
55 sys.getsizeof(3) +
56 sys.getsizeof('y') +
57 sys.getsizeof(4))
59 def test_namedtuple(self):
60 Point = namedtuple('Point', ['x', 'y'])
61 point = Point(3, 4)
62 self.assertEqual(pysize.get_size(point),
63 sys.getsizeof(point) +
64 sys.getsizeof(3) +
65 sys.getsizeof(4))
67 def test_subclass_of_namedtuple(self):
69 class Point(namedtuple('Point', ['x', 'y'])):
70 pass
72 point = Point(3, 4)
73 self.assertEqual(pysize.get_size(point),
74 sys.getsizeof(point) +
75 sys.getsizeof(point.__dict__) +
76 sys.getsizeof(3) +
77 sys.getsizeof(4))
79 def test_subclass_of_namedtuple_with_slots(self):
81 class Point(namedtuple('Point', ['x', 'y'])):
82 __slots__ = ()
84 point = Point(3, 4)
85 self.assertEqual(pysize.get_size(point),
86 sys.getsizeof(point) +
87 sys.getsizeof(3) +
88 sys.getsizeof(4))
90 def test_slots(self):
91 class slots1(object):
92 __slots__ = ["number1"]
93 def __init__(self, number1):
94 self.number1 = number1
96 class slots2(object):
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
109 s1 = slots1(7)
110 s2 = slots2(3, 4)
111 s3 = slots3(4, 5, 6)
113 version_addition = 0
115 if hasattr(sys.version_info, 'major') and sys.version_info.major == 3:
116 version_addition = 4
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__':
125 unittest.main()