annotate rust/hg-core/src/testing.rs @ 41241:168041fa6d5f

rust: factorized testing Graphs it will useful to use these outside of `ancestors`, too. Differential Revision: https://phab.mercurial-scm.org/D5579
author Georges Racinet <georges.racinet@octobus.net>
date Mon, 14 Jan 2019 20:42:25 +0100
parents
children 4c5f6e95df84
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
41241
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
1 // testing.rs
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
2 //
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
3 // Copyright 2018 Georges Racinet <georges.racinet@octobus.net>
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
4 //
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
5 // This software may be used and distributed according to the terms of the
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
6 // GNU General Public License version 2 or any later version.
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
7
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
8 use crate::{Graph, GraphError, Revision, NULL_REVISION};
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
9
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
10 /// A stub `Graph`, same as the one from `test-ancestor.py`
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
11 ///
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
12 /// o 13
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
13 /// |
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
14 /// | o 12
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
15 /// | |
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
16 /// | | o 11
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
17 /// | | |\
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
18 /// | | | | o 10
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
19 /// | | | | |
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
20 /// | o---+ | 9
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
21 /// | | | | |
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
22 /// o | | | | 8
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
23 /// / / / /
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
24 /// | | o | 7
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
25 /// | | | |
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
26 /// o---+ | 6
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
27 /// / / /
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
28 /// | | o 5
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
29 /// | |/
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
30 /// | o 4
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
31 /// | |
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
32 /// o | 3
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
33 /// | |
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
34 /// | o 2
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
35 /// |/
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
36 /// o 1
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
37 /// |
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
38 /// o 0
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
39 #[derive(Clone, Debug)]
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
40 pub struct SampleGraph;
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
41
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
42 impl Graph for SampleGraph {
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
43 fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError> {
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
44 match rev {
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
45 0 => Ok([NULL_REVISION, NULL_REVISION]),
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
46 1 => Ok([0, NULL_REVISION]),
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
47 2 => Ok([1, NULL_REVISION]),
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
48 3 => Ok([1, NULL_REVISION]),
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
49 4 => Ok([2, NULL_REVISION]),
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
50 5 => Ok([4, NULL_REVISION]),
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
51 6 => Ok([4, NULL_REVISION]),
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
52 7 => Ok([4, NULL_REVISION]),
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
53 8 => Ok([NULL_REVISION, NULL_REVISION]),
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
54 9 => Ok([6, 7]),
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
55 10 => Ok([5, NULL_REVISION]),
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
56 11 => Ok([3, 7]),
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
57 12 => Ok([9, NULL_REVISION]),
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
58 13 => Ok([8, NULL_REVISION]),
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
59 r => Err(GraphError::ParentOutOfRange(r)),
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
60 }
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
61 }
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
62 }
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
63
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
64 // A Graph represented by a vector whose indices are revisions
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
65 // and values are parents of the revisions
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
66 pub type VecGraph = Vec<[Revision; 2]>;
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
67
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
68 impl Graph for VecGraph {
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
69 fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError> {
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
70 Ok(self[rev as usize])
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
71 }
168041fa6d5f rust: factorized testing Graphs
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
72 }