Skip to content

Commit c9dcc00

Browse files
committed
testing(bisect): extract symbols into testing module
1 parent aff490b commit c9dcc00

File tree

3 files changed

+114
-106
lines changed

3 files changed

+114
-106
lines changed

scm-bisect/src/basic.rs

Lines changed: 9 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -239,41 +239,20 @@ impl<G: BasicSourceControlGraph> search::Strategy<G> for BasicStrategy {
239239

240240
#[cfg(test)]
241241
mod tests {
242-
use std::convert::Infallible;
243-
244-
use itertools::Itertools;
245-
use maplit::{hashmap, hashset};
246-
use proptest::prelude::Strategy as ProptestStrategy;
247-
use proptest::prelude::*;
248-
use proptest::proptest;
242+
use super::*;
249243

250244
use crate::search::Bounds;
251245
use crate::search::EagerSolution;
252246
use crate::search::Search;
253247
use crate::search::Status;
248+
use crate::testing::arb_strategy;
249+
use crate::testing::arb_test_graph_and_nodes;
250+
use crate::testing::TestGraph;
251+
use crate::testing::UsizeGraph;
254252

255-
use super::BasicStrategyKind;
256-
use super::*;
257-
258-
#[derive(Clone, Debug)]
259-
struct UsizeGraph {
260-
max: usize,
261-
}
262-
263-
impl BasicSourceControlGraph for UsizeGraph {
264-
type Node = usize;
265-
type Error = Infallible;
266-
267-
fn ancestors(&self, node: Self::Node) -> Result<HashSet<Self::Node>, Infallible> {
268-
assert!(node < self.max);
269-
Ok((0..=node).collect())
270-
}
271-
272-
fn descendants(&self, node: Self::Node) -> Result<HashSet<Self::Node>, Infallible> {
273-
assert!(node < self.max);
274-
Ok((node..self.max).collect())
275-
}
276-
}
253+
use itertools::Itertools;
254+
use maplit::hashmap;
255+
use maplit::hashset;
277256

278257
#[test]
279258
fn test_search_stick() {
@@ -289,10 +268,6 @@ mod tests {
289268
strategy: BasicStrategyKind::Binary,
290269
};
291270
let mut search = Search::new(graph.clone(), nodes.clone());
292-
// let mut linear_search = Search::new(graph.clone(), linear_strategy, nodes.clone());
293-
// let mut linear_reverse_search =
294-
// Search::new(graph.clone(), linear_reverse_strategy, nodes.clone());
295-
// let mut binary_search = Search::new(graph.clone(), binary_strategy, nodes.clone());
296271

297272
assert_eq!(
298273
search
@@ -455,34 +430,6 @@ mod tests {
455430
"###);
456431
}
457432

458-
#[derive(Clone, Debug)]
459-
struct TestGraph {
460-
nodes: HashMap<char, HashSet<char>>,
461-
}
462-
463-
impl BasicSourceControlGraph for TestGraph {
464-
type Node = char;
465-
type Error = Infallible;
466-
467-
fn ancestors(&self, node: Self::Node) -> Result<HashSet<Self::Node>, Infallible> {
468-
let mut result = hashset! {node};
469-
let parents: HashSet<char> = self
470-
.nodes
471-
.iter()
472-
.filter_map(|(k, v)| if v.contains(&node) { Some(*k) } else { None })
473-
.collect();
474-
result.extend(self.ancestors_all(parents)?);
475-
Ok(result)
476-
}
477-
478-
fn descendants(&self, node: Self::Node) -> Result<HashSet<Self::Node>, Infallible> {
479-
let mut result = hashset! {node};
480-
let children: HashSet<char> = self.nodes[&node].clone();
481-
result.extend(self.descendants_all(children)?);
482-
Ok(result)
483-
}
484-
}
485-
486433
#[test]
487434
fn test_search_dag() {
488435
let graph = TestGraph {
@@ -586,51 +533,7 @@ mod tests {
586533
);
587534
}
588535

589-
fn arb_strategy() -> impl ProptestStrategy<Value = BasicStrategyKind> {
590-
prop_oneof![
591-
Just(BasicStrategyKind::Linear),
592-
Just(BasicStrategyKind::LinearReverse),
593-
Just(BasicStrategyKind::Binary),
594-
]
595-
}
596-
597-
fn arb_test_graph_and_nodes() -> impl ProptestStrategy<Value = (TestGraph, Vec<char>)> {
598-
let nodes = prop::collection::hash_set(
599-
prop::sample::select(vec!['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']),
600-
1..=8,
601-
);
602-
nodes
603-
.prop_flat_map(|nodes| {
604-
let num_nodes = nodes.len();
605-
let nodes_kv = nodes
606-
.iter()
607-
.copied()
608-
.map(|node| (node, HashSet::new()))
609-
.collect();
610-
let graph = TestGraph { nodes: nodes_kv };
611-
let lineages = prop::collection::vec(
612-
prop::sample::subsequence(nodes.into_iter().collect_vec(), 0..num_nodes),
613-
0..num_nodes,
614-
);
615-
(Just(graph), lineages)
616-
})
617-
.prop_map(|(mut graph, lineages)| {
618-
for lineage in lineages {
619-
for (parent, child) in lineage.into_iter().tuple_windows() {
620-
graph.nodes.get_mut(&parent).unwrap().insert(child);
621-
}
622-
}
623-
graph
624-
})
625-
.prop_flat_map(|graph| {
626-
let nodes = graph.nodes.keys().copied().collect::<Vec<_>>();
627-
let num_nodes = nodes.len();
628-
let failure_nodes = prop::sample::subsequence(nodes, 0..num_nodes);
629-
(Just(graph), failure_nodes)
630-
})
631-
}
632-
633-
proptest! {
536+
proptest::proptest! {
634537
#[test]
635538
fn test_search_dag_proptest(strategy in arb_strategy(), (graph, failure_nodes) in arb_test_graph_and_nodes()) {
636539
let nodes = graph.nodes.keys().sorted().copied().collect::<Vec<_>>();

scm-bisect/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@
1313

1414
pub mod basic;
1515
pub mod search;
16+
17+
#[cfg(test)]
18+
pub mod testing;

scm-bisect/src/testing.rs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
use std::collections::{HashMap, HashSet};
2+
use std::convert::Infallible;
3+
4+
use itertools::Itertools;
5+
use proptest::prelude::Strategy as ProptestStrategy;
6+
use proptest::prelude::*;
7+
8+
use crate::basic::{BasicSourceControlGraph, BasicStrategyKind};
9+
10+
#[derive(Clone, Debug)]
11+
pub struct UsizeGraph {
12+
pub max: usize,
13+
}
14+
15+
impl BasicSourceControlGraph for UsizeGraph {
16+
type Node = usize;
17+
type Error = Infallible;
18+
19+
fn ancestors(&self, node: Self::Node) -> Result<HashSet<Self::Node>, Infallible> {
20+
assert!(node < self.max);
21+
Ok((0..=node).collect())
22+
}
23+
24+
fn descendants(&self, node: Self::Node) -> Result<HashSet<Self::Node>, Infallible> {
25+
assert!(node < self.max);
26+
Ok((node..self.max).collect())
27+
}
28+
}
29+
30+
#[derive(Clone, Debug)]
31+
pub struct TestGraph {
32+
pub nodes: HashMap<char, HashSet<char>>,
33+
}
34+
35+
impl BasicSourceControlGraph for TestGraph {
36+
type Node = char;
37+
type Error = Infallible;
38+
39+
fn ancestors(&self, node: Self::Node) -> Result<HashSet<Self::Node>, Infallible> {
40+
let mut result = HashSet::new();
41+
result.insert(node);
42+
let parents: HashSet<char> = self
43+
.nodes
44+
.iter()
45+
.filter_map(|(k, v)| if v.contains(&node) { Some(*k) } else { None })
46+
.collect();
47+
result.extend(self.ancestors_all(parents)?);
48+
Ok(result)
49+
}
50+
51+
fn descendants(&self, node: Self::Node) -> Result<HashSet<Self::Node>, Infallible> {
52+
let mut result = HashSet::new();
53+
result.insert(node);
54+
let children: HashSet<char> = self.nodes[&node].clone();
55+
result.extend(self.descendants_all(children)?);
56+
Ok(result)
57+
}
58+
}
59+
60+
pub fn arb_strategy() -> impl ProptestStrategy<Value = BasicStrategyKind> {
61+
prop_oneof![
62+
Just(BasicStrategyKind::Linear),
63+
Just(BasicStrategyKind::LinearReverse),
64+
Just(BasicStrategyKind::Binary),
65+
]
66+
}
67+
68+
pub fn arb_test_graph_and_nodes() -> impl ProptestStrategy<Value = (TestGraph, Vec<char>)> {
69+
let nodes = prop::collection::hash_set(
70+
prop::sample::select(vec!['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']),
71+
1..=8,
72+
);
73+
nodes
74+
.prop_flat_map(|nodes| {
75+
let num_nodes = nodes.len();
76+
let nodes_kv = nodes
77+
.iter()
78+
.copied()
79+
.map(|node| (node, HashSet::new()))
80+
.collect();
81+
let graph = TestGraph { nodes: nodes_kv };
82+
let lineages = prop::collection::vec(
83+
prop::sample::subsequence(nodes.into_iter().collect_vec(), 0..num_nodes),
84+
0..num_nodes,
85+
);
86+
(Just(graph), lineages)
87+
})
88+
.prop_map(|(mut graph, lineages)| {
89+
for lineage in lineages {
90+
for (parent, child) in lineage.into_iter().tuple_windows() {
91+
graph.nodes.get_mut(&parent).unwrap().insert(child);
92+
}
93+
}
94+
graph
95+
})
96+
.prop_flat_map(|graph| {
97+
let nodes = graph.nodes.keys().copied().collect::<Vec<_>>();
98+
let num_nodes = nodes.len();
99+
let failure_nodes = prop::sample::subsequence(nodes, 0..num_nodes);
100+
(Just(graph), failure_nodes)
101+
})
102+
}

0 commit comments

Comments
 (0)