Adding unit tests for wrapper
This commit is contained in:
parent
5d768d67c6
commit
8d9b39865a
3 changed files with 158 additions and 45 deletions
|
@ -120,7 +120,7 @@ where
|
|||
/// # }
|
||||
/// ```
|
||||
pub fn new(val: T, path: &Path) -> Result<FileLinked<T>, Error> {
|
||||
let mut temp_file_path = path.clone().to_path_buf();
|
||||
let mut temp_file_path = path.to_path_buf();
|
||||
temp_file_path.set_file_name(format!(
|
||||
".temp{}",
|
||||
path.file_name()
|
||||
|
@ -408,19 +408,17 @@ where
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::{
|
||||
fs,
|
||||
fs::File,
|
||||
};
|
||||
use std::{fs, fs::File};
|
||||
|
||||
struct CleanUp
|
||||
{
|
||||
path: PathBuf
|
||||
struct CleanUp {
|
||||
path: PathBuf,
|
||||
}
|
||||
|
||||
impl CleanUp {
|
||||
fn new(path: &Path) -> CleanUp {
|
||||
CleanUp { path: path.to_path_buf()}
|
||||
CleanUp {
|
||||
path: path.to_path_buf(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run<F: FnOnce(&Path) -> Result<(), Error>>(&self, op: F) -> Result<(), Error> {
|
||||
|
@ -460,7 +458,8 @@ mod tests {
|
|||
FileLinked::new(val, &p)?;
|
||||
|
||||
let file = File::open(&p)?;
|
||||
let result: String = bincode::deserialize_from(file).expect("Unable to deserialize from file");
|
||||
let result: String =
|
||||
bincode::deserialize_from(file).expect("Unable to deserialize from file");
|
||||
assert_eq!(result, val);
|
||||
|
||||
Ok(())
|
||||
|
@ -477,16 +476,10 @@ mod tests {
|
|||
assert_eq!(*file_linked_list.readonly(), vec![1, 2, 3, 4]);
|
||||
|
||||
file_linked_list.mutate(|v1| v1.push(5))?;
|
||||
assert_eq!(
|
||||
*file_linked_list.readonly(),
|
||||
vec![1, 2, 3, 4, 5]
|
||||
);
|
||||
assert_eq!(*file_linked_list.readonly(), vec![1, 2, 3, 4, 5]);
|
||||
|
||||
file_linked_list.mutate(|v1| v1[1] = 1)?;
|
||||
assert_eq!(
|
||||
*file_linked_list.readonly(),
|
||||
vec![1, 1, 3, 4, 5]
|
||||
);
|
||||
assert_eq!(*file_linked_list.readonly(), vec![1, 1, 3, 4, 5]);
|
||||
|
||||
drop(file_linked_list);
|
||||
Ok(())
|
||||
|
@ -504,10 +497,7 @@ mod tests {
|
|||
assert_eq!(*file_linked_list.readonly(), val1);
|
||||
|
||||
file_linked_list.replace(val2.clone())?;
|
||||
assert_eq!(
|
||||
*file_linked_list.readonly(),
|
||||
val2
|
||||
);
|
||||
assert_eq!(*file_linked_list.readonly(), val2);
|
||||
|
||||
drop(file_linked_list);
|
||||
Ok(())
|
||||
|
@ -531,6 +521,5 @@ mod tests {
|
|||
drop(linked_object);
|
||||
Ok(())
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -47,7 +47,7 @@ pub trait GeneticNode {
|
|||
|
||||
/// Used externally to wrap a node implementing the [`GeneticNode`] trait. Processes state transitions for the given node as
|
||||
/// well as signal recovery. Transition states are given by [`GeneticState`]
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
|
||||
pub struct GeneticNodeWrapper<T> {
|
||||
node: Option<T>,
|
||||
state: GeneticState,
|
||||
|
@ -61,7 +61,7 @@ impl<T> Default for GeneticNodeWrapper<T> {
|
|||
GeneticNodeWrapper {
|
||||
node: None,
|
||||
state: GeneticState::Initialize,
|
||||
generation: 0,
|
||||
generation: 1,
|
||||
max_generations: 1,
|
||||
id: Uuid::new_v4(),
|
||||
}
|
||||
|
@ -83,7 +83,7 @@ where
|
|||
GeneticNodeWrapper {
|
||||
node: Some(data),
|
||||
state: GeneticState::Simulate,
|
||||
generation: 0,
|
||||
generation: 1,
|
||||
max_generations,
|
||||
id,
|
||||
}
|
||||
|
@ -135,3 +135,136 @@ where
|
|||
Ok(self.state)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::error::Error;
|
||||
use anyhow::anyhow;
|
||||
|
||||
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
|
||||
struct TestState {
|
||||
pub score: f64,
|
||||
}
|
||||
|
||||
impl GeneticNode for TestState {
|
||||
fn simulate(&mut self) -> Result<(), Error> {
|
||||
self.score += 1.0;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn mutate(&mut self) -> Result<(), Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn initialize() -> Result<Box<TestState>, Error> {
|
||||
Ok(Box::new(TestState { score: 0.0 }))
|
||||
}
|
||||
|
||||
fn merge(_l: &TestState, _r: &TestState) -> Result<Box<TestState>, Error> {
|
||||
Err(Error::Other(anyhow!("Unable to merge")))
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_new() -> Result<(), Error> {
|
||||
let genetic_node = GeneticNodeWrapper::<TestState>::new(10);
|
||||
|
||||
let other_genetic_node = GeneticNodeWrapper::<TestState> {
|
||||
node: None,
|
||||
state: GeneticState::Initialize,
|
||||
generation: 1,
|
||||
max_generations: 10,
|
||||
id: genetic_node.id(),
|
||||
};
|
||||
|
||||
assert_eq!(genetic_node, other_genetic_node);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from() -> Result<(), Error> {
|
||||
let val = TestState { score: 0.0 };
|
||||
let uuid = Uuid::new_v4();
|
||||
let genetic_node = GeneticNodeWrapper::from(val.clone(), 10, uuid);
|
||||
|
||||
let other_genetic_node = GeneticNodeWrapper::<TestState> {
|
||||
node: Some(val),
|
||||
state: GeneticState::Simulate,
|
||||
generation: 1,
|
||||
max_generations: 10,
|
||||
id: genetic_node.id(),
|
||||
};
|
||||
|
||||
assert_eq!(genetic_node, other_genetic_node);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_as_ref() -> Result<(), Error> {
|
||||
let val = TestState { score: 3.0 };
|
||||
let uuid = Uuid::new_v4();
|
||||
let genetic_node = GeneticNodeWrapper::from(val.clone(), 10, uuid);
|
||||
|
||||
let ref_value = genetic_node.as_ref().unwrap();
|
||||
|
||||
assert_eq!(*ref_value, val);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_id() -> Result<(), Error> {
|
||||
let val = TestState { score: 3.0 };
|
||||
let uuid = Uuid::new_v4();
|
||||
let genetic_node = GeneticNodeWrapper::from(val.clone(), 10, uuid);
|
||||
|
||||
let id_value = genetic_node.id();
|
||||
|
||||
assert_eq!(id_value, uuid);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_max_generations() -> Result<(), Error> {
|
||||
let val = TestState { score: 3.0 };
|
||||
let uuid = Uuid::new_v4();
|
||||
let genetic_node = GeneticNodeWrapper::from(val.clone(), 10, uuid);
|
||||
|
||||
let max_generations = genetic_node.max_generations();
|
||||
|
||||
assert_eq!(max_generations, 10);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_state() -> Result<(), Error> {
|
||||
let val = TestState { score: 3.0 };
|
||||
let uuid = Uuid::new_v4();
|
||||
let genetic_node = GeneticNodeWrapper::from(val.clone(), 10, uuid);
|
||||
|
||||
let state = genetic_node.state();
|
||||
|
||||
assert_eq!(state, GeneticState::Simulate);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_process_node() -> Result<(), Error> {
|
||||
let mut genetic_node = GeneticNodeWrapper::<TestState>::new(2);
|
||||
|
||||
assert_eq!(genetic_node.state(), GeneticState::Initialize);
|
||||
assert_eq!(genetic_node.process_node()?, GeneticState::Simulate);
|
||||
assert_eq!(genetic_node.process_node()?, GeneticState::Mutate);
|
||||
assert_eq!(genetic_node.process_node()?, GeneticState::Simulate);
|
||||
assert_eq!(genetic_node.process_node()?, GeneticState::Finish);
|
||||
assert_eq!(genetic_node.process_node()?, GeneticState::Finish);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -299,21 +299,12 @@ mod tests {
|
|||
use crate::core::*;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::str::FromStr;
|
||||
|
||||
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
|
||||
struct TestState {
|
||||
pub score: f64,
|
||||
}
|
||||
|
||||
impl FromStr for TestState {
|
||||
type Err = String;
|
||||
|
||||
fn from_str(s: &str) -> Result<TestState, Self::Err> {
|
||||
serde_json::from_str(s).map_err(|_| format!("Unable to parse string {}", s))
|
||||
}
|
||||
}
|
||||
|
||||
impl genetic_node::GeneticNode for TestState {
|
||||
fn simulate(&mut self) -> Result<(), Error> {
|
||||
self.score += 1.0;
|
||||
|
|
Loading…
Add table
Reference in a new issue