option_vec_printer improvements and tests
This commit is contained in:
parent
577162a0b1
commit
7b4f28f78f
62
src/misc.rs
62
src/misc.rs
@ -233,31 +233,27 @@ fn newtons_method(
|
|||||||
/// representation of the Vector
|
/// representation of the Vector
|
||||||
pub fn option_vec_printer<T: ToString>(data: Vec<Option<T>>) -> String {
|
pub fn option_vec_printer<T: ToString>(data: Vec<Option<T>>) -> String {
|
||||||
let none_representation = "None";
|
let none_representation = "None";
|
||||||
let unwrapped: Vec<String> = data
|
let max_i = data.len() - 1;
|
||||||
|
let output: String = data
|
||||||
.iter()
|
.iter()
|
||||||
.map(|x| {
|
.enumerate()
|
||||||
if let Some(inner) = x {
|
.map(|(i, x)| {
|
||||||
inner.to_string()
|
let mut tmp = match x {
|
||||||
} else {
|
Some(inner) => inner.to_string(),
|
||||||
none_representation.to_owned()
|
_ => none_representation.to_string(),
|
||||||
|
};
|
||||||
|
|
||||||
|
// Add comma and space if needed
|
||||||
|
if max_i > i {
|
||||||
|
tmp += ", ";
|
||||||
}
|
}
|
||||||
|
tmp
|
||||||
})
|
})
|
||||||
.collect();
|
.collect::<Vec<String>>()
|
||||||
|
.concat();
|
||||||
|
|
||||||
let mut output: String = String::from("[");
|
format!("[{}]", output)
|
||||||
let mut curr_len: usize = 0;
|
|
||||||
let total_len: usize = unwrapped.len();
|
|
||||||
for ele in unwrapped.iter() {
|
|
||||||
curr_len += 1;
|
|
||||||
output += ele;
|
|
||||||
if total_len > curr_len {
|
|
||||||
output += ", ";
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
output += "]";
|
|
||||||
output
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns a vector of length `max_i` starting at value `min_x` with resolution
|
// Returns a vector of length `max_i` starting at value `min_x` with resolution
|
||||||
// of `resolution`
|
// of `resolution`
|
||||||
pub fn resolution_helper(max_i: usize, min_x: f64, resolution: f64) -> Vec<f64> {
|
pub fn resolution_helper(max_i: usize, min_x: f64, resolution: f64) -> Vec<f64> {
|
||||||
@ -277,6 +273,7 @@ pub fn step_helper(max_i: usize, min_x: f64, step: f64) -> Vec<f64> {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
/// Tests SteppedVector to ensure everything works properly (helped me find
|
/// Tests SteppedVector to ensure everything works properly (helped me find
|
||||||
/// a bunch of issues)
|
/// a bunch of issues)
|
||||||
@ -339,4 +336,29 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(resolution_helper(3, -2.0, 1.0), vec![-2.0, -1.0, 0.0]);
|
assert_eq!(resolution_helper(3, -2.0, 1.0), vec![-2.0, -1.0, 0.0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn option_vec_printer_test() {
|
||||||
|
let values_strings: HashMap<Vec<Option<&str>>, &str> = HashMap::from([
|
||||||
|
(vec![None], "[None]"),
|
||||||
|
(vec![Some("text"), None], "[text, None]"),
|
||||||
|
(vec![None, None], "[None, None]"),
|
||||||
|
(vec![Some("text1"), Some("text2")], "[text1, text2]"),
|
||||||
|
]);
|
||||||
|
|
||||||
|
for (key, value) in values_strings {
|
||||||
|
assert_eq!(option_vec_printer(key), value);
|
||||||
|
}
|
||||||
|
|
||||||
|
let values_floats = HashMap::from([
|
||||||
|
(vec![Some(10)], "[10]"),
|
||||||
|
(vec![Some(10), None], "[10, None]"),
|
||||||
|
(vec![None, Some(10)], "[None, 10]"),
|
||||||
|
(vec![Some(10), Some(100)], "[10, 100]"),
|
||||||
|
]);
|
||||||
|
|
||||||
|
for (key, value) in values_floats {
|
||||||
|
assert_eq!(option_vec_printer(key), value);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user