2 # Simple tests for the talloc python bindings.
3 # Copyright (C) 2015 Petr Viktorin <pviktori@redhat.com>
19 class TallocTests(unittest.TestCase):
21 def test_report_full(self):
22 # report_full is hardcoded to print to stdout, so use a subprocess
23 process = subprocess.Popen([
26 import talloc, _test_pytalloc
27 obj = _test_pytalloc.new()
28 talloc.report_full(obj)
30 ], stdout=subprocess.PIPE)
31 output, stderr = process.communicate()
33 self.assertTrue("full talloc report on 'talloc.Object" in output)
34 self.assertTrue("This is a test string" in output)
36 def test_totalblocks(self):
37 obj = _test_pytalloc.new()
38 # Two blocks: the string, and the name
39 self.assertEqual(talloc.total_blocks(obj), 2)
42 obj = _test_pytalloc.new()
43 prefix = '<talloc.Object talloc object at'
44 self.assertTrue(repr(obj).startswith(prefix))
45 self.assertEqual(repr(obj), str(obj))
47 def test_base_repr(self):
48 obj = _test_pytalloc.base_new()
49 prefix = '<talloc.BaseObject talloc based object at'
50 self.assertTrue(repr(obj).startswith(prefix))
51 self.assertEqual(repr(obj), str(obj))
53 def test_destructor(self):
54 # Check correct lifetime of the talloc'd data
56 obj = _test_pytalloc.DObject(lambda: lst.append('dead'))
57 self.assertEqual(lst, [])
60 self.assertEqual(lst, ['dead'])
62 def test_base_destructor(self):
63 # Check correct lifetime of the talloc'd data
65 obj = _test_pytalloc.DBaseObject(lambda: lst.append('dead'))
66 self.assertEqual(lst, [])
69 self.assertEqual(lst, ['dead'])
72 class TallocComparisonTests(unittest.TestCase):
74 def test_compare_same(self):
75 obj1 = _test_pytalloc.new()
76 self.assertTrue(obj1 == obj1)
77 self.assertFalse(obj1 != obj1)
78 self.assertTrue(obj1 <= obj1)
79 self.assertFalse(obj1 < obj1)
80 self.assertTrue(obj1 >= obj1)
81 self.assertFalse(obj1 > obj1)
83 def test_compare_different(self):
84 # object comparison is consistent
87 _test_pytalloc.new()])
88 self.assertFalse(obj1 == obj2)
89 self.assertTrue(obj1 != obj2)
90 self.assertTrue(obj1 <= obj2)
91 self.assertTrue(obj1 < obj2)
92 self.assertFalse(obj1 >= obj2)
93 self.assertFalse(obj1 > obj2)
95 def test_compare_different_types(self):
96 # object comparison falls back to comparing types
97 if sys.version_info >= (3, 0):
98 # In Python 3, types are unorderable -- nothing to test
100 if talloc.Object < _test_pytalloc.DObject:
101 obj1 = _test_pytalloc.new()
102 obj2 = _test_pytalloc.DObject(dummy_func)
104 obj2 = _test_pytalloc.new()
105 obj1 = _test_pytalloc.DObject(dummy_func)
106 self.assertFalse(obj1 == obj2)
107 self.assertTrue(obj1 != obj2)
108 self.assertTrue(obj1 <= obj2)
109 self.assertTrue(obj1 < obj2)
110 self.assertFalse(obj1 >= obj2)
111 self.assertFalse(obj1 > obj2)
114 class TallocBaseComparisonTests(unittest.TestCase):
116 def test_compare_same(self):
117 obj1 = _test_pytalloc.base_new()
118 self.assertTrue(obj1 == obj1)
119 self.assertFalse(obj1 != obj1)
120 self.assertTrue(obj1 <= obj1)
121 self.assertFalse(obj1 < obj1)
122 self.assertTrue(obj1 >= obj1)
123 self.assertFalse(obj1 > obj1)
125 def test_compare_different(self):
126 # object comparison is consistent
127 obj1, obj2 = sorted([
128 _test_pytalloc.base_new(),
129 _test_pytalloc.base_new()])
130 self.assertFalse(obj1 == obj2)
131 self.assertTrue(obj1 != obj2)
132 self.assertTrue(obj1 <= obj2)
133 self.assertTrue(obj1 < obj2)
134 self.assertFalse(obj1 >= obj2)
135 self.assertFalse(obj1 > obj2)
137 def test_compare_different_types(self):
138 # object comparison falls back to comparing types
139 if sys.version_info >= (3, 0):
140 # In Python 3, types are unorderable -- nothing to test
142 if talloc.BaseObject < _test_pytalloc.DBaseObject:
143 obj1 = _test_pytalloc.base_new()
144 obj2 = _test_pytalloc.DBaseObject(dummy_func)
146 obj2 = _test_pytalloc.base_new()
147 obj1 = _test_pytalloc.DBaseObject(dummy_func)
148 self.assertFalse(obj1 == obj2)
149 self.assertTrue(obj1 != obj2)
150 self.assertTrue(obj1 <= obj2)
151 self.assertTrue(obj1 < obj2)
152 self.assertFalse(obj1 >= obj2)
153 self.assertFalse(obj1 > obj2)
156 class TallocUtilTests(unittest.TestCase):
158 def test_get_type(self):
159 self.assertTrue(talloc.Object is _test_pytalloc.get_object_type())
161 def test_reference(self):
162 # Check correct lifetime of the talloc'd data with multiple references
164 obj = _test_pytalloc.DObject(lambda: lst.append('dead'))
165 ref = _test_pytalloc.reference(obj)
168 self.assertEqual(lst, [])
171 self.assertEqual(lst, ['dead'])
173 def test_get_base_type(self):
174 self.assertTrue(talloc.BaseObject is _test_pytalloc.base_get_object_type())
176 def test_base_reference(self):
177 # Check correct lifetime of the talloc'd data with multiple references
179 obj = _test_pytalloc.DBaseObject(lambda: lst.append('dead'))
180 ref = _test_pytalloc.base_reference(obj)
183 self.assertEqual(lst, [])
186 self.assertEqual(lst, ['dead'])
189 if __name__ == '__main__':
190 unittest.TestProgram()