Mercurial > hg
annotate tests/test-rust-revlog.py @ 51274:bec6e9c108fd
matchers: use correct method for finding index in vector
The path matcher has an optimization for when all paths are `rootfilesin:`. This
optimization exists in both Python and Rust. However, the Rust implementation
currently has a bug that makes it fail in most cases. The bug is that it
`rfind()` where it was clearly intended to use `rposition()`. This patch fixes
that and adds a test.
author | Martin von Zweigbergk <martinvonz@google.com> |
---|---|
date | Mon, 18 Dec 2023 14:51:20 -0800 |
parents | f94c10334bcb |
children |
rev | line source |
---|---|
51191
13f58ce70299
rust-revlog: teach the revlog opening code to read the repo options
Raphaël Gomès <rgomes@octobus.net>
parents:
51190
diff
changeset
|
1 import struct |
43961
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
2 import unittest |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
3 |
51249
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
4 from mercurial.node import hex |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
5 |
43961
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
6 try: |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
7 from mercurial import rustext |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
8 |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
9 rustext.__name__ # trigger immediate actual import |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
10 except ImportError: |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
11 rustext = None |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
12 else: |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
13 from mercurial.rustext import revlog |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
14 |
44011
c627f1b2f3c3
rust-index: handle `MixedIndex` in `pyindex_to_graph`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43961
diff
changeset
|
15 # this would fail already without appropriate ancestor.__package__ |
c627f1b2f3c3
rust-index: handle `MixedIndex` in `pyindex_to_graph`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43961
diff
changeset
|
16 from mercurial.rustext.ancestor import LazyAncestors |
c627f1b2f3c3
rust-index: handle `MixedIndex` in `pyindex_to_graph`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43961
diff
changeset
|
17 |
43961
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
18 from mercurial.testing import revlog as revlogtesting |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
19 |
51191
13f58ce70299
rust-revlog: teach the revlog opening code to read the repo options
Raphaël Gomès <rgomes@octobus.net>
parents:
51190
diff
changeset
|
20 header = struct.unpack(">I", revlogtesting.data_non_inlined[:4])[0] |
13f58ce70299
rust-revlog: teach the revlog opening code to read the repo options
Raphaël Gomès <rgomes@octobus.net>
parents:
51190
diff
changeset
|
21 |
43961
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
22 |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
23 @unittest.skipIf( |
45942
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
44012
diff
changeset
|
24 rustext is None, |
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
44012
diff
changeset
|
25 "rustext module revlog relies on is not available", |
43961
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
26 ) |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
27 class RustRevlogIndexTest(revlogtesting.RevlogBasedTestBase): |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
28 def test_heads(self): |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
29 idx = self.parseindex() |
51254
f94c10334bcb
rust-index: renamed `MixedIndex` as `Index`
Georges Racinet on incendie.racinet.fr <georges@racinet.fr>
parents:
51253
diff
changeset
|
30 rustidx = revlog.Index(revlogtesting.data_non_inlined, header) |
43961
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
31 self.assertEqual(rustidx.headrevs(), idx.headrevs()) |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
32 |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
33 def test_len(self): |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
34 idx = self.parseindex() |
51254
f94c10334bcb
rust-index: renamed `MixedIndex` as `Index`
Georges Racinet on incendie.racinet.fr <georges@racinet.fr>
parents:
51253
diff
changeset
|
35 rustidx = revlog.Index(revlogtesting.data_non_inlined, header) |
43961
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
36 self.assertEqual(len(rustidx), len(idx)) |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
37 |
44011
c627f1b2f3c3
rust-index: handle `MixedIndex` in `pyindex_to_graph`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43961
diff
changeset
|
38 def test_ancestors(self): |
51254
f94c10334bcb
rust-index: renamed `MixedIndex` as `Index`
Georges Racinet on incendie.racinet.fr <georges@racinet.fr>
parents:
51253
diff
changeset
|
39 rustidx = revlog.Index(revlogtesting.data_non_inlined, header) |
44011
c627f1b2f3c3
rust-index: handle `MixedIndex` in `pyindex_to_graph`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43961
diff
changeset
|
40 lazy = LazyAncestors(rustidx, [3], 0, True) |
c627f1b2f3c3
rust-index: handle `MixedIndex` in `pyindex_to_graph`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43961
diff
changeset
|
41 # we have two more references to the index: |
c627f1b2f3c3
rust-index: handle `MixedIndex` in `pyindex_to_graph`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43961
diff
changeset
|
42 # - in its inner iterator for __contains__ and __bool__ |
c627f1b2f3c3
rust-index: handle `MixedIndex` in `pyindex_to_graph`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43961
diff
changeset
|
43 # - in the LazyAncestors instance itself (to spawn new iterators) |
c627f1b2f3c3
rust-index: handle `MixedIndex` in `pyindex_to_graph`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43961
diff
changeset
|
44 self.assertTrue(2 in lazy) |
c627f1b2f3c3
rust-index: handle `MixedIndex` in `pyindex_to_graph`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43961
diff
changeset
|
45 self.assertTrue(bool(lazy)) |
c627f1b2f3c3
rust-index: handle `MixedIndex` in `pyindex_to_graph`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43961
diff
changeset
|
46 self.assertEqual(list(lazy), [3, 2, 1, 0]) |
c627f1b2f3c3
rust-index: handle `MixedIndex` in `pyindex_to_graph`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43961
diff
changeset
|
47 # a second time to validate that we spawn new iterators |
c627f1b2f3c3
rust-index: handle `MixedIndex` in `pyindex_to_graph`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43961
diff
changeset
|
48 self.assertEqual(list(lazy), [3, 2, 1, 0]) |
c627f1b2f3c3
rust-index: handle `MixedIndex` in `pyindex_to_graph`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43961
diff
changeset
|
49 |
c627f1b2f3c3
rust-index: handle `MixedIndex` in `pyindex_to_graph`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43961
diff
changeset
|
50 # let's check bool for an empty one |
51239
7eea2e4109ae
rust-index: using the `hg::index::Index` in ancestors iterator and lazy set
Georges Racinet <georges.racinet@octobus.net>
parents:
51191
diff
changeset
|
51 self.assertFalse(LazyAncestors(rustidx, [0], 0, False)) |
44011
c627f1b2f3c3
rust-index: handle `MixedIndex` in `pyindex_to_graph`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43961
diff
changeset
|
52 |
43961
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
53 |
51249
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
54 @unittest.skipIf( |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
55 rustext is None, |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
56 "rustext module revlog relies on is not available", |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
57 ) |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
58 class RustRevlogNodeTreeClassTest(revlogtesting.RustRevlogBasedTestBase): |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
59 def test_standalone_nodetree(self): |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
60 idx = self.parserustindex() |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
61 nt = revlog.NodeTree(idx) |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
62 for i in range(4): |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
63 nt.insert(i) |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
64 |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
65 bin_nodes = [entry[7] for entry in idx] |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
66 hex_nodes = [hex(n) for n in bin_nodes] |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
67 |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
68 for i, node in enumerate(hex_nodes): |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
69 self.assertEqual(nt.prefix_rev_lookup(node), i) |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
70 self.assertEqual(nt.prefix_rev_lookup(node[:5]), i) |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
71 |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
72 # all 4 revisions in idx (standard data set) have different |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
73 # first nybbles in their Node IDs, |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
74 # hence `nt.shortest()` should return 1 for them, except when |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
75 # the leading nybble is 0 (ambiguity with NULL_NODE) |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
76 for i, (bin_node, hex_node) in enumerate(zip(bin_nodes, hex_nodes)): |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
77 shortest = nt.shortest(bin_node) |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
78 expected = 2 if hex_node[0] == ord('0') else 1 |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
79 self.assertEqual(shortest, expected) |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
80 self.assertEqual(nt.prefix_rev_lookup(hex_node[:shortest]), i) |
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
81 |
51251
0409bd6ba663
rust-revlog: add invalidation detection to `NodeTree` class
Georges Racinet <georges.racinet@octobus.net>
parents:
51249
diff
changeset
|
82 # test invalidation (generation poisoning) detection |
0409bd6ba663
rust-revlog: add invalidation detection to `NodeTree` class
Georges Racinet <georges.racinet@octobus.net>
parents:
51249
diff
changeset
|
83 del idx[3] |
0409bd6ba663
rust-revlog: add invalidation detection to `NodeTree` class
Georges Racinet <georges.racinet@octobus.net>
parents:
51249
diff
changeset
|
84 self.assertTrue(nt.is_invalidated()) |
0409bd6ba663
rust-revlog: add invalidation detection to `NodeTree` class
Georges Racinet <georges.racinet@octobus.net>
parents:
51249
diff
changeset
|
85 |
51249
2966b88d4531
rust-revlog: bare minimal NodeTree exposition
Georges Racinet <georges.racinet@octobus.net>
parents:
51239
diff
changeset
|
86 |
43961
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
87 if __name__ == '__main__': |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
88 import silenttestrunner |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
89 |
b69d5f3a41d0
rust-index: add a struct wrapping the C index
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
90 silenttestrunner.main(__name__) |