File: test_pytalloc.py

package info (click to toggle)
samba 2%3A4.22.3%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 185,264 kB
  • sloc: ansic: 1,955,394; python: 269,800; sh: 71,598; xml: 50,288; perl: 36,082; makefile: 6,086; yacc: 4,497; exp: 1,582; cpp: 1,224; lex: 989; awk: 589; java: 119; csh: 58; pascal: 54; sed: 45; asm: 30
file content (153 lines) | stat: -rw-r--r-- 4,690 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python3
# Simple tests for the talloc python bindings.
# Copyright (C) 2015 Petr Viktorin <pviktori@redhat.com>

import unittest
import subprocess
import sys
import gc

import talloc
import _test_pytalloc


def dummy_func():
    pass


class TallocTests(unittest.TestCase):

    def test_report_full(self):
        # report_full is hardcoded to print to stdout, so use a subprocess
        process = subprocess.Popen([
            sys.executable, '-c',
            """if True:
            import talloc, _test_pytalloc
            obj = _test_pytalloc.new()
            talloc.report_full(obj)
            """
        ], stdout=subprocess.PIPE)
        output, stderr = process.communicate()
        output = str(output)
        self.assertTrue("full talloc report on 'talloc.Object" in output)
        self.assertTrue("This is a test string" in output)

    def test_totalblocks(self):
        obj = _test_pytalloc.new()
        # Two blocks: the string, and the name
        self.assertEqual(talloc.total_blocks(obj), 2)

    def test_repr(self):
        obj = _test_pytalloc.new()
        prefix = '<talloc.Object talloc object at'
        self.assertTrue(repr(obj).startswith(prefix))
        self.assertEqual(repr(obj), str(obj))

    def test_base_repr(self):
        obj = _test_pytalloc.base_new()
        prefix = '<talloc.BaseObject talloc based object at'
        self.assertTrue(repr(obj).startswith(prefix))
        self.assertEqual(repr(obj), str(obj))

    def test_destructor(self):
        # Check correct lifetime of the talloc'd data
        lst = []
        obj = _test_pytalloc.DObject(lambda: lst.append('dead'))
        self.assertEqual(lst, [])
        del obj
        gc.collect()
        self.assertEqual(lst, ['dead'])

    def test_base_destructor(self):
        # Check correct lifetime of the talloc'd data
        lst = []
        obj = _test_pytalloc.DBaseObject(lambda: lst.append('dead'))
        self.assertEqual(lst, [])
        del obj
        gc.collect()
        self.assertEqual(lst, ['dead'])


class TallocComparisonTests(unittest.TestCase):

    def test_compare_same(self):
        obj1 = _test_pytalloc.new()
        self.assertTrue(obj1 == obj1)
        self.assertFalse(obj1 != obj1)
        self.assertTrue(obj1 <= obj1)
        self.assertFalse(obj1 < obj1)
        self.assertTrue(obj1 >= obj1)
        self.assertFalse(obj1 > obj1)

    def test_compare_different(self):
        # object comparison is consistent
        obj1, obj2 = sorted([
            _test_pytalloc.new(),
            _test_pytalloc.new()])
        self.assertFalse(obj1 == obj2)
        self.assertTrue(obj1 != obj2)
        self.assertTrue(obj1 <= obj2)
        self.assertTrue(obj1 < obj2)
        self.assertFalse(obj1 >= obj2)
        self.assertFalse(obj1 > obj2)


class TallocBaseComparisonTests(unittest.TestCase):

    def test_compare_same(self):
        obj1 = _test_pytalloc.base_new()
        self.assertTrue(obj1 == obj1)
        self.assertFalse(obj1 != obj1)
        self.assertTrue(obj1 <= obj1)
        self.assertFalse(obj1 < obj1)
        self.assertTrue(obj1 >= obj1)
        self.assertFalse(obj1 > obj1)

    def test_compare_different(self):
        # object comparison is consistent
        obj1, obj2 = sorted([
            _test_pytalloc.base_new(),
            _test_pytalloc.base_new()])
        self.assertFalse(obj1 == obj2)
        self.assertTrue(obj1 != obj2)
        self.assertTrue(obj1 <= obj2)
        self.assertTrue(obj1 < obj2)
        self.assertFalse(obj1 >= obj2)
        self.assertFalse(obj1 > obj2)


class TallocUtilTests(unittest.TestCase):

    def test_get_type(self):
        self.assertTrue(talloc.Object is _test_pytalloc.get_object_type())

    def test_reference(self):
        # Check correct lifetime of the talloc'd data with multiple references
        lst = []
        obj = _test_pytalloc.DObject(lambda: lst.append('dead'))
        ref = _test_pytalloc.reference(obj)
        del obj
        gc.collect()
        self.assertEqual(lst, [])
        del ref
        gc.collect()
        self.assertEqual(lst, ['dead'])

    def test_get_base_type(self):
        self.assertTrue(talloc.BaseObject is _test_pytalloc.base_get_object_type())

    def test_base_reference(self):
        # Check correct lifetime of the talloc'd data with multiple references
        lst = []
        obj = _test_pytalloc.DBaseObject(lambda: lst.append('dead'))
        ref = _test_pytalloc.base_reference(obj)
        del obj
        gc.collect()
        self.assertEqual(lst, [])
        del ref
        gc.collect()
        self.assertEqual(lst, ['dead'])


if __name__ == '__main__':
    unittest.TestProgram()