Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5cd3b8b

Browse files
committed
Added unit tests for ShortestPathTree functions
1 parent c4bef70 commit 5cd3b8b

File tree

1 file changed

+62
-4
lines changed

1 file changed

+62
-4
lines changed

src/lib.rs

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ impl<T: Display + Clone + Eq + Hash> ShortestPathTree<T> {
193193
}
194194

195195
/// The shortest path from one node to another.
196-
#[derive(Debug, Eq, PartialEq)]
196+
#[derive(Debug, Eq, PartialEq, Clone)]
197197
pub struct ShortestPath<T: Display + Clone> {//TODO check if it is possible to use references instead of T, add documentation
198198
/// Contains a list of node ids, first entry is the start node, last entry is the target node.
199199
path: Vec<T>, //TODO maybe add later that the distance between each node is stored as well
@@ -284,11 +284,17 @@ impl<T: Display + Clone> TryFrom<&Rc<RefCell<Node<T>>>> for ShortestPath<T> {
284284

285285
/// Tries to read the shortest path from the node.
286286
///
287-
/// Will fail when the node does not contain a shortest path or when the distance is `i32::MAX`.
287+
/// Will fail on the following occasions:
288+
///
289+
/// - When the node does not contain a shortest path and the distance is not 0
290+
/// - when the distance is `i32::MAX`.
288291
fn try_from(value: &Rc<RefCell<Node<T>>>) -> Result<ShortestPath<T>, &'static str> {
289-
if value.borrow().distance == i32::MAX || value.borrow().shortest_path.is_empty() {
292+
if value.borrow().distance == 0 {
293+
// It is tried to parse the shortest path to the source node
294+
return Ok(ShortestPath::new(vec![value.borrow().id.clone()]));
295+
} else if value.borrow().distance == i32::MAX || value.borrow().shortest_path.is_empty() {
290296
return Err("Unable to create shortest path from vector, vector is empty!");
291-
}
297+
}
292298
let mut path = Vec::new();
293299
for node in &value.borrow().shortest_path {
294300
path.push(node.borrow().id.clone());
@@ -364,6 +370,58 @@ fn graph_2() -> Graph<char> {
364370
#[cfg(test)]
365371
mod tests {
366372

373+
mod shortest_path_tree {
374+
use crate::{graph_2, algorithms::dijkstra, ShortestPath, graph, ShortestPathTree};
375+
376+
#[test]
377+
fn add_result_test() {
378+
let mut spt = ShortestPathTree::new('a');
379+
let test_path = ShortestPath::new(vec!['a', 'd', 'c']);
380+
spt.add_result('b', Some(5), Some(test_path.clone()));
381+
let path = spt.shortest_path(&'b');
382+
let distance = spt.shortest_distance(&'b');
383+
assert!(path.is_some());
384+
assert_eq!(path.unwrap(), &test_path);
385+
assert_eq!(distance, Some(5));
386+
}
387+
388+
#[test]
389+
fn shortest_path_test() {
390+
let mut graph = graph_2();
391+
let spt = dijkstra(&mut graph, &'a');
392+
assert!(spt.is_ok());
393+
let should_path = vec!['a', 'b', 'd'];
394+
let sp = ShortestPath::new(should_path);
395+
assert_eq!(spt.as_ref().unwrap().shortest_path(&'d'), Some(&sp));
396+
let should_path = vec!['a'];
397+
let sp = ShortestPath::new(should_path);
398+
assert_eq!(spt.unwrap().shortest_path(&'a'), Some(&sp));
399+
let spt = dijkstra(&mut graph, &'d');
400+
let should_path = vec!['d', 'b', 'a'];
401+
let sp = ShortestPath::new(should_path);
402+
assert_eq!(spt.unwrap().shortest_path(&'a'), Some(&sp));
403+
}
404+
405+
#[test]
406+
fn shortest_distance_test() {
407+
let mut graph = graph_2();
408+
let spt = dijkstra(&mut graph, &'c');
409+
assert!(spt.is_ok());
410+
let spt = spt.unwrap();
411+
assert_eq!(spt.shortest_distance(&'b'), Some(4));
412+
assert_eq!(spt.shortest_distance(&'a'), Some(9));
413+
}
414+
415+
#[test]
416+
fn from_graph_test() {
417+
let mut graph = graph_2();
418+
assert!(dijkstra(&mut graph, &'a').is_ok());
419+
let spt = ShortestPathTree::from_graph(&graph, &'a');
420+
assert_eq!(spt.source, 'a');
421+
assert_eq!(spt.results[&'a'], Some((0, ShortestPath::new(vec!['a']))));
422+
}
423+
}
424+
367425
//#[test]
368426
//fn node_shortest_distance_test() {
369427
// let mut graph = graph_2();

0 commit comments

Comments
 (0)