mirror of
https://github.com/facebook/sapling.git
synced 2024-10-08 07:49:11 +03:00
types: Add Nodes::random_distinct to randomly generate sets of nodes
Summary: Add a utility function for tests to generate a vector of random nodes. This will be used in future tests. Reviewed By: DurhamG Differential Revision: D12980784 fbshipit-source-id: 73fc8643503e11a46a845671df94c912a5e49d23
This commit is contained in:
parent
d0c03f6aaf
commit
88ab626e9a
@ -6,6 +6,9 @@ use std::io::{self, Read, Write};
|
|||||||
#[cfg(any(test, feature = "for-tests"))]
|
#[cfg(any(test, feature = "for-tests"))]
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
|
|
||||||
|
#[cfg(any(test, feature = "for-tests"))]
|
||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
#[derive(Debug, Fail)]
|
#[derive(Debug, Fail)]
|
||||||
#[fail(display = "Node Error: {:?}", _0)]
|
#[fail(display = "Node Error: {:?}", _0)]
|
||||||
struct NodeError(String);
|
struct NodeError(String);
|
||||||
@ -84,6 +87,20 @@ impl Node {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(any(test, feature = "for-tests"))]
|
||||||
|
pub fn random_distinct(rng: &mut RngCore, count: usize) -> Vec<Self> {
|
||||||
|
let mut nodes = Vec::new();
|
||||||
|
let mut nodeset = HashSet::new();
|
||||||
|
while nodes.len() < count {
|
||||||
|
let node = Node::random(rng);
|
||||||
|
if !nodeset.contains(&node) {
|
||||||
|
nodeset.insert(node.clone());
|
||||||
|
nodes.push(node);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
nodes
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for Node {
|
impl Display for Node {
|
||||||
|
Loading…
Reference in New Issue
Block a user