talloc: use the system pytalloc-util for python3 as well
[sfrench/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 def dummy_func():
15     pass
16
17
18 class TallocTests(unittest.TestCase):
19
20     def test_report_full(self):
21         # report_full is hardcoded to print to stdout, so use a subprocess
22         process = subprocess.Popen([
23             sys.executable, '-c',
24             """if True:
25             import talloc, _test_pytalloc
26             obj = _test_pytalloc.new()
27             talloc.report_full(obj)
28             """
29         ], stdout=subprocess.PIPE)
30         output, stderr = process.communicate()
31         output = str(output)
32         self.assertTrue("full talloc report on 'talloc.Object" in output)
33         self.assertTrue("This is a test string" in output)
34
35     def test_totalblocks(self):
36         obj = _test_pytalloc.new()
37         # Two blocks: the string, and the name
38         self.assertEqual(talloc.total_blocks(obj), 2)
39
40     def test_repr(self):
41         obj = _test_pytalloc.new()
42         prefix = '<talloc.Object talloc object at'
43         self.assertTrue(repr(obj).startswith(prefix))
44         self.assertEqual(repr(obj), str(obj))
45
46     def test_base_repr(self):
47         obj = _test_pytalloc.base_new()
48         prefix = '<talloc.BaseObject talloc based object at'
49         self.assertTrue(repr(obj).startswith(prefix))
50         self.assertEqual(repr(obj), str(obj))
51
52     def test_destructor(self):
53         # Check correct lifetime of the talloc'd data
54         lst = []
55         obj = _test_pytalloc.DObject(lambda: lst.append('dead'))
56         self.assertEqual(lst, [])
57         del obj
58         gc.collect()
59         self.assertEqual(lst, ['dead'])
60
61     def test_base_destructor(self):
62         # Check correct lifetime of the talloc'd data
63         lst = []
64         obj = _test_pytalloc.DBaseObject(lambda: lst.append('dead'))
65         self.assertEqual(lst, [])
66         del obj
67         gc.collect()
68         self.assertEqual(lst, ['dead'])
69
70
71 class TallocComparisonTests(unittest.TestCase):
72
73     def test_compare_same(self):
74         obj1 = _test_pytalloc.new()
75         self.assertTrue(obj1 == obj1)
76         self.assertFalse(obj1 != obj1)
77         self.assertTrue(obj1 <= obj1)
78         self.assertFalse(obj1 < obj1)
79         self.assertTrue(obj1 >= obj1)
80         self.assertFalse(obj1 > obj1)
81
82     def test_compare_different(self):
83         # object comparison is consistent
84         obj1, obj2 = sorted([
85             _test_pytalloc.new(),
86             _test_pytalloc.new()])
87         self.assertFalse(obj1 == obj2)
88         self.assertTrue(obj1 != obj2)
89         self.assertTrue(obj1 <= obj2)
90         self.assertTrue(obj1 < obj2)
91         self.assertFalse(obj1 >= obj2)
92         self.assertFalse(obj1 > obj2)
93
94     def test_compare_different_types(self):
95         # object comparison falls back to comparing types
96         if sys.version_info >= (3, 0):
97             # In Python 3, types are unorderable -- nothing to test
98             return
99         if talloc.Object < _test_pytalloc.DObject:
100             obj1 = _test_pytalloc.new()
101             obj2 = _test_pytalloc.DObject(dummy_func)
102         else:
103             obj2 = _test_pytalloc.new()
104             obj1 = _test_pytalloc.DObject(dummy_func)
105         self.assertFalse(obj1 == obj2)
106         self.assertTrue(obj1 != obj2)
107         self.assertTrue(obj1 <= obj2)
108         self.assertTrue(obj1 < obj2)
109         self.assertFalse(obj1 >= obj2)
110         self.assertFalse(obj1 > obj2)
111
112 class TallocBaseComparisonTests(unittest.TestCase):
113
114     def test_compare_same(self):
115         obj1 = _test_pytalloc.base_new()
116         self.assertTrue(obj1 == obj1)
117         self.assertFalse(obj1 != obj1)
118         self.assertTrue(obj1 <= obj1)
119         self.assertFalse(obj1 < obj1)
120         self.assertTrue(obj1 >= obj1)
121         self.assertFalse(obj1 > obj1)
122
123     def test_compare_different(self):
124         # object comparison is consistent
125         obj1, obj2 = sorted([
126             _test_pytalloc.base_new(),
127             _test_pytalloc.base_new()])
128         self.assertFalse(obj1 == obj2)
129         self.assertTrue(obj1 != obj2)
130         self.assertTrue(obj1 <= obj2)
131         self.assertTrue(obj1 < obj2)
132         self.assertFalse(obj1 >= obj2)
133         self.assertFalse(obj1 > obj2)
134
135     def test_compare_different_types(self):
136         # object comparison falls back to comparing types
137         if sys.version_info >= (3, 0):
138             # In Python 3, types are unorderable -- nothing to test
139             return
140         if talloc.BaseObject < _test_pytalloc.DBaseObject:
141             obj1 = _test_pytalloc.base_new()
142             obj2 = _test_pytalloc.DBaseObject(dummy_func)
143         else:
144             obj2 = _test_pytalloc.base_new()
145             obj1 = _test_pytalloc.DBaseObject(dummy_func)
146         self.assertFalse(obj1 == obj2)
147         self.assertTrue(obj1 != obj2)
148         self.assertTrue(obj1 <= obj2)
149         self.assertTrue(obj1 < obj2)
150         self.assertFalse(obj1 >= obj2)
151         self.assertFalse(obj1 > obj2)
152
153
154 class TallocUtilTests(unittest.TestCase):
155
156     def test_get_type(self):
157         self.assertTrue(talloc.Object is _test_pytalloc.get_object_type())
158
159     def test_reference(self):
160         # Check correct lifetime of the talloc'd data with multiple references
161         lst = []
162         obj = _test_pytalloc.DObject(lambda: lst.append('dead'))
163         ref = _test_pytalloc.reference(obj)
164         del obj
165         gc.collect()
166         self.assertEqual(lst, [])
167         del ref
168         gc.collect()
169         self.assertEqual(lst, ['dead'])
170
171     def test_get_base_type(self):
172         self.assertTrue(talloc.BaseObject is _test_pytalloc.base_get_object_type())
173
174     def test_base_reference(self):
175         # Check correct lifetime of the talloc'd data with multiple references
176         lst = []
177         obj = _test_pytalloc.DBaseObject(lambda: lst.append('dead'))
178         ref = _test_pytalloc.base_reference(obj)
179         del obj
180         gc.collect()
181         self.assertEqual(lst, [])
182         del ref
183         gc.collect()
184         self.assertEqual(lst, ['dead'])
185
186
187 if __name__ == '__main__':
188     unittest.TestProgram()