Change all uint32/16/8 to 32_t/16_t/8_t in winbindd.
[Samba.git] / lib / talloc / test_pytalloc.py
blob961bfcb51b6bb98ac20d74320b4d2c3c811866f6
1 #!/usr/bin/env python
2 # Simple tests for the talloc python bindings.
3 # Copyright (C) 2015 Petr Viktorin <pviktori@redhat.com>
5 import unittest
6 import subprocess
7 import sys
8 import re
9 import gc
11 import talloc
12 import _test_pytalloc
14 def dummy_func():
15 pass
18 class TallocTests(unittest.TestCase):
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)
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)
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))
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'])
56 class TallocComparisonTests(unittest.TestCase):
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)
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)
79 def test_compare_different_types(self):
80 # object comparison falls back to comparing types
81 if talloc.Object < _test_pytalloc.DObject:
82 obj1 = _test_pytalloc.new()
83 obj2 = _test_pytalloc.DObject(dummy_func)
84 else:
85 obj2 = _test_pytalloc.new()
86 obj1 = _test_pytalloc.DObject(dummy_func)
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)
95 class TallocUtilTests(unittest.TestCase):
97 def test_get_type(self):
98 self.assertTrue(talloc.Object is _test_pytalloc.get_object_type())
100 def test_refrence(self):
101 # Check correct lifetime of the talloc'd data with multiple references
102 lst = []
103 obj = _test_pytalloc.DObject(lambda: lst.append('dead'))
104 ref = _test_pytalloc.reference(obj)
105 del obj
106 gc.collect()
107 self.assertEqual(lst, [])
108 del ref
109 gc.collect()
110 self.assertEqual(lst, ['dead'])
113 if __name__ == '__main__':
114 unittest.TestProgram()