Added basis for unit tests. Noted required unit tests to imlpement.

This commit is contained in:
Cutieguwu
2025-02-20 20:33:46 -05:00
parent 35299f3675
commit 58a6cbd689
3 changed files with 87 additions and 13 deletions

View File

@@ -122,9 +122,9 @@ fn main() {
} }
}; };
let recover_tool = Recover::new(config, input, output, map); let mut recover_tool = Recover::new(config, input, output, map);
recover_tool.run_full(); recover_tool.run();
todo!("Recovery, Map saving, and closure of all files."); todo!("Recovery, Map saving, and closure of all files.");
} }
@@ -152,10 +152,25 @@ fn get_path(
/// Get length of data stream. /// Get length of data stream.
/// Physical length of data stream in bytes /// Physical length of data stream in bytes
/// (multiple of sector_size, rather than actual). /// (multiple of sector_size, rather than actual).
fn get_stream_length<S: Seek>(file: &mut S) -> io::Result<u64> { fn get_stream_length<S: Seek>(input: &mut S) -> io::Result<u64> {
let len = file.seek(SeekFrom::End(0))?; let len = input.seek(SeekFrom::End(0))?;
let _ = file.seek(SeekFrom::Start(0)); let _ = input.seek(SeekFrom::Start(0));
Ok(len) Ok(len)
}
#[cfg(test)]
#[allow(unused)]
mod tests {
use super::*;
// Test for get_path
// Need to determine how to package files to test with, or at least
// how to test with PathBuf present.
// Test must also check unwrapping of file name, not just generation.
// Test for get_stream_length
// Need to determine how to test with Seek-able objects.
} }

View File

@@ -69,6 +69,37 @@ impl From<Cluster> for MapCluster {
} }
} }
impl MapCluster {
/// Breaks apart into a vec of clusters,
/// each of cluster_size, excepting last.
pub fn subdivide(&mut self, cluster_len: usize) -> Vec<MapCluster> {
let domain_len = self.domain.len();
let mut start = self.domain.start;
let mut clusters: Vec<MapCluster> = vec![];
for _ in 0..(domain_len as f64 / cluster_len as f64).floor() as usize {
clusters.push(MapCluster {
domain: Domain {
start: start,
end: start + cluster_len,
},
stage: self.stage,
});
start += cluster_len;
}
clusters.push(MapCluster {
domain: Domain {
start: start,
end: self.domain.end,
},
stage: self.stage,
});
clusters
}
}
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, PartialOrd)] #[derive(Clone, Copy, Debug, Deserialize, PartialEq, PartialOrd)]
pub enum Stage { pub enum Stage {
@@ -125,7 +156,7 @@ impl MapFile {
} }
/// Recalculate cluster mappings. /// Recalculate cluster mappings.
fn update(&mut self, new_cluster: Cluster) { fn update(&mut self, new_cluster: Cluster) -> &mut Self {
let mut new_map: Vec<MapCluster> = vec![MapCluster::from(new_cluster.to_owned())]; let mut new_map: Vec<MapCluster> = vec![MapCluster::from(new_cluster.to_owned())];
for map_cluster in self.map.iter() { for map_cluster in self.map.iter() {
@@ -185,6 +216,7 @@ impl MapFile {
} }
self.map = new_map; self.map = new_map;
self
} }
/// Get current recovery stage. /// Get current recovery stage.
@@ -266,4 +298,21 @@ impl MapFile {
self.map = new_map; self.map = new_map;
self self
} }
}
#[cfg(test)]
#[allow(unused)]
mod tests {
use super::*;
// Test for MapCluster::subdivide()
// Test for MapFile::update()
// Test for MapFile::get_stage()
// Test for MapFile::get_clusters()
// Test for MapFile::defrag()
} }

View File

@@ -46,20 +46,30 @@ impl Recover {
} }
/// Recover media from blank slate. /// Recover media from blank slate.
pub fn run_full(self) {} pub fn run(&mut self) -> &mut Self {
self
/// Recover media given a partial recovery. }
pub fn run_limited(self) {}
/// Attempt to copy all untested blocks. /// Attempt to copy all untested blocks.
fn copy_untested(self) { fn copy_untested(&mut self) -> &mut Self {
self
} }
/// Set buffer capacities as cluster length in bytes. /// Set buffer capacities as cluster length in bytes.
/// Varies depending on the recovery stage. /// Varies depending on the recovery stage.
fn set_buf_capacity(&mut self) { fn set_buf_capacity(&mut self) -> &mut Self {
self.buf_capacity = (self.config.sector_size * self.config.cluster_length) as usize; self.buf_capacity = (self.config.sector_size * self.config.cluster_length) as usize;
self
} }
} }
#[cfg(test)]
#[allow(unused)]
mod tests {
use super::*;
// Test for Recover::set_buf_capacity
}