Skip to content

Commit

Permalink
wip: more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
markxoe committed Jun 3, 2024
1 parent 70f92ed commit 5aba90f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
4 changes: 1 addition & 3 deletions src/commands/interactive.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::time::Duration;

use clap::Args;

use crate::{
Expand All @@ -8,7 +6,7 @@ use crate::{
database,
maps::page_map::{PageMap, PageMapResult},
},
indication::{spinner, ProgressBuilder},
indication::ProgressBuilder,
};

use super::ArgExecutor;
Expand Down
28 changes: 28 additions & 0 deletions src/data/algorithm/bfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ use log::debug;
use crate::data::maps::link_map::LinkMap;

pub fn find_shortest_path(start: i32, end: i32, links: &LinkMap) -> Option<Vec<i32>> {
if start == end {
return Some(vec![start]);
}

let mut queue = VecDeque::new();
let mut predecessor = HashMap::new();
let mut visited = HashSet::new(); // note: having a set of visited nodes improves performance by a few percent while increasing memory usage
Expand Down Expand Up @@ -67,6 +71,30 @@ mod test {
assert_eq!(path, Some(vec![1, 2]));
}

#[test]
fn start_is_end() {
let link_map = LinkMap::new_with_progress(
vec![(1, 2), (1, 3), (3, 2)].into_iter().collect(),
ProgressBuilder::empty(),
);

let path = super::find_shortest_path(1, 1, &link_map);

assert_eq!(path, Some(vec![1]));
}

#[test]
fn no_way() {
let link_map = LinkMap::new_with_progress(
vec![(1, 2), (1, 3), (3, 2)].into_iter().collect(),
ProgressBuilder::empty(),
);

let path = super::find_shortest_path(2, 1, &link_map);

assert_eq!(path, None);
}

#[test]
fn single_possibility() {
let link_map = LinkMap::new_with_progress(
Expand Down
2 changes: 1 addition & 1 deletion src/data/maps/link_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl LinkMap {

#[test]
fn new_link_map() {
let links = VecDeque::from(vec![(1, 2), (1, 3), (3, 2)]);
let links = vec![(1, 2), (1, 3), (3, 2)].into_iter().collect();

let map = LinkMap::new_with_progress(links, ProgressBuilder::empty());

Expand Down

0 comments on commit 5aba90f

Please sign in to comment.