Changelog for previous commit
[abartlet/talloc-debian.git] / 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_destructor(self):
47         # Check correct lifetime of the talloc'd data
48         lst = []
49         obj = _test_pytalloc.DObject(lambda: lst.append('dead'))
50         self.assertEqual(lst, [])
51         del obj
52         gc.collect()
53         self.assertEqual(lst, ['dead'])
54
55
56 class TallocComparisonTests(unittest.TestCase):
57
58     def test_compare_same(self):
59         obj1 = _test_pytalloc.new()
60         self.assertTrue(obj1 == obj1)
61         self.assertFalse(obj1 != obj1)
62         self.assertTrue(obj1 <= obj1)
63         self.assertFalse(obj1 < obj1)
64         self.assertTrue(obj1 >= obj1)
65         self.assertFalse(obj1 > obj1)
66
67     def test_compare_different(self):
68         # object comparison is consistent
69         obj1, obj2 = sorted([
70             _test_pytalloc.new(),
71             _test_pytalloc.new()])
72         self.assertFalse(obj1 == obj2)
73         self.assertTrue(obj1 != obj2)
74         self.assertTrue(obj1 <= obj2)
75         self.assertTrue(obj1 < obj2)
76         self.assertFalse(obj1 >= obj2)
77         self.assertFalse(obj1 > obj2)
78
79     def test_compare_different_types(self):
80         # object comparison falls back to comparing types
81         if sys.version_info >= (3, 0):
82             # In Python 3, types are unorderable -- nothing to test
83             return
84         if talloc.Object < _test_pytalloc.DObject:
85             obj1 = _test_pytalloc.new()
86             obj2 = _test_pytalloc.DObject(dummy_func)
87         else:
88             obj2 = _test_pytalloc.new()
89             obj1 = _test_pytalloc.DObject(dummy_func)
90         self.assertFalse(obj1 == obj2)
91         self.assertTrue(obj1 != obj2)
92         self.assertTrue(obj1 <= obj2)
93         self.assertTrue(obj1 < obj2)
94         self.assertFalse(obj1 >= obj2)
95         self.assertFalse(obj1 > obj2)
96
97
98 class TallocUtilTests(unittest.TestCase):
99
100     def test_get_type(self):
101         self.assertTrue(talloc.Object is _test_pytalloc.get_object_type())
102
103     def test_refrence(self):
104         # Check correct lifetime of the talloc'd data with multiple references
105         lst = []
106         obj = _test_pytalloc.DObject(lambda: lst.append('dead'))
107         ref = _test_pytalloc.reference(obj)
108         del obj
109         gc.collect()
110         self.assertEqual(lst, [])
111         del ref
112         gc.collect()
113         self.assertEqual(lst, ['dead'])
114
115
116 if __name__ == '__main__':
117     unittest.TestProgram()