PEP8: fix E302: expected 2 blank lines, found 1
[garming/samba-autobuild/.git] / lib / talloc / test_pytalloc.py
1 #!/usr/bin/env python
2 # Simple tests for the talloc python bindings.
3 # Copyright (C) 2015 Petr Viktorin <pviktori@redhat.com>
4
5 import unittest
6 import subprocess
7 import sys
8 import re
9 import gc
10
11 import talloc
12 import _test_pytalloc
13
14
15 def dummy_func():
16     pass
17
18
19 class TallocTests(unittest.TestCase):
20
21     def test_report_full(self):
22         # report_full is hardcoded to print to stdout, so use a subprocess
23         process = subprocess.Popen([
24             sys.executable, '-c',
25             """if True:
26             import talloc, _test_pytalloc
27             obj = _test_pytalloc.new()
28             talloc.report_full(obj)
29             """
30         ], stdout=subprocess.PIPE)
31         output, stderr = process.communicate()
32         output = str(output)
33         self.assertTrue("full talloc report on 'talloc.Object" in output)
34         self.assertTrue("This is a test string" in output)
35
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)
40
41     def test_repr(self):
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))
46
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))
52
53     def test_destructor(self):
54         # Check correct lifetime of the talloc'd data
55         lst = []
56         obj = _test_pytalloc.DObject(lambda: lst.append('dead'))
57         self.assertEqual(lst, [])
58         del obj
59         gc.collect()
60         self.assertEqual(lst, ['dead'])
61
62     def test_base_destructor(self):
63         # Check correct lifetime of the talloc'd data
64         lst = []
65         obj = _test_pytalloc.DBaseObject(lambda: lst.append('dead'))
66         self.assertEqual(lst, [])
67         del obj
68         gc.collect()
69         self.assertEqual(lst, ['dead'])
70
71
72 class TallocComparisonTests(unittest.TestCase):
73
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)
82
83     def test_compare_different(self):
84         # object comparison is consistent
85         obj1, obj2 = sorted([
86             _test_pytalloc.new(),
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)
94
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
99             return
100         if talloc.Object < _test_pytalloc.DObject:
101             obj1 = _test_pytalloc.new()
102             obj2 = _test_pytalloc.DObject(dummy_func)
103         else:
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)
112
113
114 class TallocBaseComparisonTests(unittest.TestCase):
115
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)
124
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)
136
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
141             return
142         if talloc.BaseObject < _test_pytalloc.DBaseObject:
143             obj1 = _test_pytalloc.base_new()
144             obj2 = _test_pytalloc.DBaseObject(dummy_func)
145         else:
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)
154
155
156 class TallocUtilTests(unittest.TestCase):
157
158     def test_get_type(self):
159         self.assertTrue(talloc.Object is _test_pytalloc.get_object_type())
160
161     def test_reference(self):
162         # Check correct lifetime of the talloc'd data with multiple references
163         lst = []
164         obj = _test_pytalloc.DObject(lambda: lst.append('dead'))
165         ref = _test_pytalloc.reference(obj)
166         del obj
167         gc.collect()
168         self.assertEqual(lst, [])
169         del ref
170         gc.collect()
171         self.assertEqual(lst, ['dead'])
172
173     def test_get_base_type(self):
174         self.assertTrue(talloc.BaseObject is _test_pytalloc.base_get_object_type())
175
176     def test_base_reference(self):
177         # Check correct lifetime of the talloc'd data with multiple references
178         lst = []
179         obj = _test_pytalloc.DBaseObject(lambda: lst.append('dead'))
180         ref = _test_pytalloc.base_reference(obj)
181         del obj
182         gc.collect()
183         self.assertEqual(lst, [])
184         del ref
185         gc.collect()
186         self.assertEqual(lst, ['dead'])
187
188
189 if __name__ == '__main__':
190     unittest.TestProgram()