Debian Codegraph @

tests/test-rust-ancestor.py

raw
1 import sys
2
3 from mercurial.node import wdirrev
4
5 from mercurial.testing import revlog as revlogtesting
6
7 try:
8 from mercurial import pyo3_rustext
9
10 pyo3_rustext.__name__
11 except ImportError:
12 pyo3_rustext = None
13
14 try:
15 from mercurial.cext import parsers as cparsers
16 except ImportError:
17 cparsers = None
18
19
20 class RustAncestorsTestMixin:
21 """Test the correctness of binding to Rust code.
22
23 This test is merely for the binding to Rust itself: extraction of
24 Python variable, giving back the results etc.
25
26 It is not meant to test the algorithmic correctness of the operations
27 on ancestors it provides. Hence the very simple embedded index data is
28 good enough.
29
30 Algorithmic correctness is asserted by the Rust unit tests.
31 """
32
33 @classmethod
34 def ancestors_mod(cls):
35 return pyo3_rustext.ancestor
36
37 @classmethod
38 def dagop_mod(cls):
39 return pyo3_rustext.dagop
40
41 @classmethod
42 def graph_error(cls):
43 return pyo3_rustext.GraphError
44
45 def testiteratorrevlist(self):
46 AncestorsIterator = self.ancestors_mod().AncestorsIterator
47
48 idx = self.parserustindex()
49 # checking test assumption about the index binary data:
50 self.assertEqual(
51 {i: (r[5], r[6]) for i, r in enumerate(idx)},
52 {0: (-1, -1), 1: (0, -1), 2: (1, -1), 3: (2, -1)},
53 )
54 ait = AncestorsIterator(idx, [3], 0, True)
55 self.assertEqual([r for r in ait], [3, 2, 1, 0])
56
57 ait = AncestorsIterator(idx, [3], 0, False)
58 self.assertEqual([r for r in ait], [2, 1, 0])
59
60 ait = AncestorsIterator(idx, [3], 0, False)
61 # tainting the index with a mutation, let's see what happens
62 # (should be more critical with AncestorsIterator)
63 del idx[0:2]
64 try:
65 next(ait)
66 except RuntimeError as exc:
67 assert "leaked reference after mutation" in exc.args[0]
68 else:
69 raise AssertionError("Expected an exception")
70
71 def testlazyancestors(self):
72 LazyAncestors = self.ancestors_mod().LazyAncestors
73
74 idx = self.parserustindex()
75 start_count = sys.getrefcount(idx.inner) # should be 2 (see Python doc)
76 self.assertEqual(
77 {i: (r[5], r[6]) for i, r in enumerate(idx)},
78 {0: (-1, -1), 1: (0, -1), 2: (1, -1), 3: (2, -1)},
79 )
80 lazy = LazyAncestors(idx, [3], 0, True)
81 # the LazyAncestors instance holds just one reference to the
82 # inner revlog. TODO check that this is normal
83 self.assertEqual(sys.getrefcount(idx.inner), start_count + 1)
84
85 self.assertTrue(2 in lazy)
86 self.assertTrue(bool(lazy))
87 self.assertFalse(None in lazy)
88 self.assertEqual(list(lazy), [3, 2, 1, 0])
89 # a second time to validate that we spawn new iterators
90 self.assertEqual(list(lazy), [3, 2, 1, 0])
91
92 # now let's watch the refcounts closer
93 ait = iter(lazy)
94 self.assertEqual(sys.getrefcount(idx.inner), start_count + 2)
95 del ait
96 self.assertEqual(sys.getrefcount(idx.inner), start_count + 1)
97 del lazy
98 self.assertEqual(sys.getrefcount(idx.inner), start_count)
99
100 # let's check bool for an empty one
101 self.assertFalse(LazyAncestors(idx, [0], 0, False))
102
103 def testrefcount(self):
104 AncestorsIterator = self.ancestors_mod().AncestorsIterator
105
106 idx = self.parserustindex()
107 start_count = sys.getrefcount(idx.inner)
108
109 # refcount increases upon iterator init...
110 ait = AncestorsIterator(idx, [3], 0, True)
111 self.assertEqual(sys.getrefcount(idx.inner), start_count + 1)
112 self.assertEqual(next(ait), 3)
113
114 # and decreases once the iterator is removed
115 del ait
116 self.assertEqual(sys.getrefcount(idx.inner), start_count)
117
118 # and removing ref to the index after iterator init is no issue
119 ait = AncestorsIterator(idx, [3], 0, True)
120 del idx
121 self.assertEqual(list(ait), [3, 2, 1, 0])
122
123 # the index is not tracked by the GC, hence there is nothing more
124 # we can assert to check that it is properly deleted once its refcount
125 # drops to 0
126
127 def testgrapherror(self):
128 AncestorsIterator = self.ancestors_mod().AncestorsIterator
129 GraphError = self.graph_error()
130
131 data = (
132 revlogtesting.data_non_inlined[: 64 + 27]
133 + b'\xf2'
134 + revlogtesting.data_non_inlined[64 + 28 :]
135 )
136 idx = self.parserustindex(data=data)
137 with self.assertRaises(GraphError) as arc:
138 AncestorsIterator(idx, [1], -1, False)
139 exc = arc.exception
140 self.assertIsInstance(exc, ValueError)
141 self.assertEqual(exc.args, ('ParentOutOfRange', 1))
142
143 def testwdirunsupported(self):
144 AncestorsIterator = self.ancestors_mod().AncestorsIterator
145 GraphError = self.graph_error()
146
147 # trying to access ancestors of the working directory raises
148 idx = self.parserustindex()
149 with self.assertRaises(GraphError) as arc:
150 list(AncestorsIterator(idx, [wdirrev], -1, False))
151
152 exc = arc.exception
153 self.assertIsInstance(exc, ValueError)
154 self.assertEqual(exc.args, ('InvalidRevision', wdirrev))
155
156 def testheadrevs(self):
157 dagop = self.dagop_mod()
158
159 idx = self.parserustindex()
160 self.assertEqual(dagop.headrevs(idx, [1, 2, 3]), {3})
161
162 def testmissingancestors(self):
163 MissingAncestors = self.ancestors_mod().MissingAncestors
164
165 idx = self.parserustindex()
166 missanc = MissingAncestors(idx, [1])
167 self.assertTrue(missanc.hasbases())
168 self.assertEqual(missanc.missingancestors([3]), [2, 3])
169 missanc.addbases({2})
170 self.assertEqual(missanc.bases(), {1, 2})
171 self.assertEqual(missanc.missingancestors([3]), [3])
172 self.assertEqual(missanc.basesheads(), {2})
173
174 def testmissingancestorsremove(self):
175 MissingAncestors = self.ancestors_mod().MissingAncestors
176
177 idx = self.parserustindex()
178 missanc = MissingAncestors(idx, [1])
179 revs = {0, 1, 2, 3}
180 missanc.removeancestorsfrom(revs)
181 self.assertEqual(revs, {2, 3})
182
183 def test_rank(self):
184 dagop = self.dagop_mod()
185
186 idx = self.parserustindex()
187 try:
188 dagop.rank(idx, 1, 2)
189 except pyo3_rustext.GraphError as exc:
190 self.assertEqual(exc.args, ("InconsistentGraphData",))
191
192
193 if __name__ == '__main__':
194 import silenttestrunner
195
196 silenttestrunner.main(__name__)