Test(Coverage) - Adds the final coverage for bindings and corrects the python's side of implementation

This commit is contained in:
Dandaedre 2023-03-09 04:21:09 +01:00 committed by Gitea
parent 2aa56b70e4
commit 753e6cf4e5
6 changed files with 157 additions and 89 deletions

View File

@ -2,9 +2,9 @@ import xiv_csolver_lib
import rich
rich.inspect(xiv_csolver_lib)
rich.inspect(xiv_csolver_lib.test_result)
rich.inspect(xiv_csolver_lib.test_result())
rich.inspect(xiv_csolver_lib.test_result())
rich.inspect(xiv_csolver_lib.test_info())
rich.inspect(xiv_csolver_lib.test_final_results())
class FeedMeDaddy:

View File

@ -116,6 +116,7 @@ class Solver:
self.loggers = loggers
self.solutions = []
self.info = None
self.best_quality = None
self.least_steps = None
self.safe_50 = None
@ -124,7 +125,7 @@ class Solver:
self.compute_all()
def solve(self) -> str:
self.solutions = xcs.solve_from_python(self)
self.solutions,self.info = xcs.solve_from_python(self)
return self.solutions
def compute_all(self) -> True:

View File

@ -131,26 +131,26 @@ fn main() {
Some(ref res) => {
// Show best results
if args.verbose > 0 {
println!("[Final] {} results were found:", res.len());
println!("[Final] {} results were found:", res.0.len());
#[cfg(feature = "verbose")]
if args.verbose > 1 {
xiv_csolver_lib::print_routes(&results);
}
}
println!("\n > SOLUTION [Least steps] <");
xiv_csolver_lib::find_fast_route(&res)
.unwrap()
.pretty_print();
println!("\n > SOLUTION [Most durability] <");
xiv_csolver_lib::find_safe_route(&res)
.unwrap()
.pretty_print();
println!("\n > SOLUTION [Most quality] < ");
xiv_csolver_lib::find_quality_route(&res)
.unwrap()
.pretty_print();
}
}
println!("\n > SOLUTION [Least steps] <");
xiv_csolver_lib::find_fast_route(&results)
.unwrap()
.pretty_print();
println!("\n > SOLUTION [Most durability] <");
xiv_csolver_lib::find_safe_route(&results)
.unwrap()
.pretty_print();
println!("\n > SOLUTION [Most quality] < ");
xiv_csolver_lib::find_quality_route(&results)
.unwrap()
.pretty_print();
// #[cfg(feature = "verbose")]
// if params.verbose>2{

View File

@ -116,7 +116,8 @@ mod tests {
#[test]
pub fn test_debug() {
let def = SolverResult::default();
assert_eq!(format!("{:?}", def),"SolverResult { steps: 0, quality: 0, progression: 0, durability: 0, cp: 0, total_cp: 0, total_progression: 0, total_quality: 0, total_durability: 0, actions: [\"Act1\", \"Act2\"], step1_solutions: 0, step2_solutions: 0, found_100_percent: false }");
assert_eq!(format!("{:?}", def),"SolverResult { steps: 0, quality: 0, progression: 0, durability: 0, cp: 0, total_cp: 0, total_progression: 0, total_quality: 0, total_durability: 0, actions: [\"Act1\", \"Act2\"] }");
//, step1_solutions: 0, step2_solutions: 0, found_100_percent: false }
}
#[test]

View File

@ -98,7 +98,7 @@
use crate::craft::Craft;
use crate::io::Parameters;
use crate::python_bindings::SolverResult;
use crate::python_bindings::{Info, SolverResult};
use crate::specs::{Recipe, Stats};
use std::sync::{Arc, Mutex};
use threadpool::ThreadPool;
@ -115,7 +115,7 @@ pub fn solve_craft<'a>(
recipe: Recipe,
stats: Stats,
params: Parameters,
) -> Option<Vec<SolverResult>> {
) -> Option<(Vec<SolverResult>, Info)> {
// Load the craft with given arguments
let craft = Craft::new(recipe, stats, params);
@ -196,17 +196,17 @@ pub fn solve_craft<'a>(
for route in phase2_routes.iter() {
if route.quality >= route.recipe.quality {
// valid_routes.push(route.clone());
valid_solutions.push(SolverResult::from_craft(route, nb_p1, nb_p2, true));
valid_solutions.push(SolverResult::from_craft(route)); //nb_p1, nb_p2, true
}
}
// If no craft can make it to 100% HQ, fallback to base results
if valid_solutions.len() == 0 {
for route in phase2_routes.iter() {
valid_solutions.push(SolverResult::from_craft(route, nb_p1, nb_p2, false));
valid_solutions.push(SolverResult::from_craft(route));
}
}
Some(valid_solutions)
Some((valid_solutions, Info::new(nb_p1, nb_p2, true)))
}
/// Load the config from args and make a craft from it
@ -323,9 +323,9 @@ pub fn load_from_config<'a>(
}
/// Print all routes in the vect, verbose
pub fn print_routes<'a>(routes: &Option<Vec<SolverResult>>) {
pub fn print_routes<'a>(routes: &Option<(Vec<SolverResult>, Info)>) {
match routes {
Some(r) => {
Some((r, _)) => {
println!("Showing {} routes", r.len());
for c in r.iter() {
println!(
@ -339,38 +339,28 @@ pub fn print_routes<'a>(routes: &Option<Vec<SolverResult>>) {
}
/// Find the route with the least amount of steps
pub fn find_fast_route(routes: &Option<Vec<SolverResult>>) -> Option<&SolverResult> {
match routes {
Some(_routes) => {
if _routes.len() > 0 {
_routes.iter().min_by_key(|key| key.steps)
} else {
None
}
}
None => None,
pub fn find_fast_route<'a>(routes: &'a (Vec<SolverResult>, Info)) -> Option<&'a SolverResult> {
let (_routes, _info) = routes;
if _routes.len() > 0 {
_routes.iter().min_by_key(|key| key.steps)
} else {
None
}
}
/// Find the route with the maximum amount of quality
pub fn find_quality_route(routes: &Option<Vec<SolverResult>>) -> Option<&SolverResult> {
match routes {
Some(_routes) => _routes.iter().max_by_key(|key| key.quality),
None => None,
}
pub fn find_quality_route<'a>(routes: &'a (Vec<SolverResult>, Info)) -> Option<&'a SolverResult> {
let (_routes, _info) = routes;
_routes.iter().max_by_key(|key| key.quality)
}
/// Find the route with the maximum of durability left
pub fn find_safe_route(routes: &Option<Vec<SolverResult>>) -> Option<&SolverResult> {
match routes {
Some(_routes) => {
if _routes.len() > 0 {
_routes.iter().max_by_key(|key| key.durability)
} else {
None
}
}
None => None,
pub fn find_safe_route(routes: &(Vec<SolverResult>, Info)) -> Option<&SolverResult> {
let (_routes, _info) = routes;
if _routes.len() > 0 {
_routes.iter().max_by_key(|key| key.durability)
} else {
None
}
}
@ -383,7 +373,7 @@ mod test {
// Supposed to find a route for the base craft
let mut craft = crate::craft::Craft::two_star();
let res = solve_craft(craft.recipe, craft.stats, craft.args);
assert!(res.unwrap().len() > 2000);
assert!(res.unwrap().0.len() > 2000);
craft.recipe.progress = 0;
let res = solve_craft(craft.recipe, craft.stats, craft.args);
@ -396,8 +386,8 @@ mod test {
let mut res = solve_craft(craft.recipe, craft.stats, craft.args).unwrap();
println!("{:?}", res);
assert_eq!(res.len(), 1);
assert_eq!(res.pop().unwrap().actions, vec!("basicSynth2"));
assert_eq!(res.0.len(), 1);
assert_eq!(res.0.pop().unwrap().actions, vec!("basicSynth2"));
}
#[test]
@ -410,25 +400,24 @@ mod test {
#[test]
pub fn test_lib_best_solutions() {
let craft = crate::craft::Craft::two_star();
let res = solve_craft(craft.recipe, craft.stats, craft.args);
let routes = solve_craft(craft.recipe, craft.stats, craft.args).unwrap();
// assert!(find_fast_route(&(None)).is_none());
// assert!(find_quality_route(&None).is_none());
// assert!(find_safe_route(&None).is_none());
assert!(find_fast_route(&None).is_none());
assert!(find_quality_route(&None).is_none());
assert!(find_safe_route(&None).is_none());
assert!(find_fast_route(&((vec![], Info::default()))).is_none());
assert!(find_quality_route(&((vec![], Info::default()))).is_none());
assert!(find_safe_route(&((vec![], Info::default()))).is_none());
assert!(find_fast_route(&Some(vec![])).is_none());
assert!(find_quality_route(&Some(vec![])).is_none());
assert!(find_safe_route(&&Some(vec![])).is_none());
let sol = find_fast_route(&res).unwrap();
let sol = find_fast_route(&routes).unwrap();
assert_eq!(sol.steps, 17);
assert_eq!(sol.quality, 11129);
assert_eq!(sol.progression, 3750);
assert_eq!(sol.durability, 0);
assert_eq!(sol.cp, 7);
assert_eq!(sol.step1_solutions, 9);
assert_eq!(sol.step2_solutions, 21193);
assert_eq!(sol.found_100_percent, true);
assert_eq!(routes.1.step1_solutions, 9);
assert_eq!(routes.1.step2_solutions, 21193);
assert_eq!(routes.1.found_100_percent, true);
assert_eq!(
sol.actions,
vec!(
@ -453,15 +442,15 @@ mod test {
)
);
let sol = find_quality_route(&res).unwrap();
let sol = find_quality_route(&routes).unwrap();
assert_eq!(sol.steps, 23);
assert_eq!(sol.quality, 12032);
assert_eq!(sol.progression, 3675);
assert_eq!(sol.durability, -5);
assert_eq!(sol.cp, 3);
assert_eq!(sol.step1_solutions, 9);
assert_eq!(sol.step2_solutions, 21193);
assert_eq!(sol.found_100_percent, true);
assert_eq!(routes.1.step1_solutions, 9);
assert_eq!(routes.1.step2_solutions, 21193);
assert_eq!(routes.1.found_100_percent, true);
assert_eq!(
sol.actions,
vec!(
@ -492,15 +481,15 @@ mod test {
)
);
let sol = find_safe_route(&res).unwrap();
let sol = find_safe_route(&routes).unwrap();
assert_eq!(sol.steps, 22);
assert_eq!(sol.quality, 11161);
assert_eq!(sol.progression, 3675);
assert_eq!(sol.durability, 10);
assert_eq!(sol.cp, 4);
assert_eq!(sol.step1_solutions, 9);
assert_eq!(sol.step2_solutions, 21193);
assert_eq!(sol.found_100_percent, true);
assert_eq!(routes.1.step1_solutions, 9);
assert_eq!(routes.1.step2_solutions, 21193);
assert_eq!(routes.1.found_100_percent, true);
assert_eq!(
sol.actions,
vec!(

View File

@ -12,7 +12,6 @@ use pyo3::prelude::*;
/// A final stripped down version of a craft
/// used for final print and talking with python
/// TODO: Half of these parameter are unique and SHOULD NOT be replicated on every solution
#[derive(Debug)]
#[pyclass]
pub struct SolverResult {
@ -46,6 +45,21 @@ pub struct SolverResult {
/// The list of actions, as a string
#[pyo3(get)]
pub actions: Vec<String>,
// /// The amount of solutions found during the first step of the solving (Used for debug) (Deprecated)
// #[pyo3(get)]
// pub step1_solutions: usize,
// /// The amount of solutions found at the end of the second step of solving (Used for debug) (Deprecated)
// #[pyo3(get)]
// pub step2_solutions: usize,
// #[pyo3(get)]
// /// Wether a 100% HQ solution was found (Deprecated)
// pub found_100_percent: bool,
}
/// Global info on the execution
#[derive(Debug)]
#[pyclass]
pub struct Info {
/// The amount of solutions found during the first step of the solving (Used for debug) (Deprecated)
#[pyo3(get)]
pub step1_solutions: usize,
@ -57,6 +71,31 @@ pub struct SolverResult {
pub found_100_percent: bool,
}
/// Solution information, contains information about the execution
impl Info {
/// Make a new information struct
pub fn new(
step1_solutions: usize,
step2_solutions: usize,
found_100_percent: bool,
// params: &'Parameters
) -> Self {
Self {
step1_solutions,
step2_solutions,
found_100_percent,
}
}
/// Default value of the info, only used for debug purposes
pub fn default() -> Self {
Self {
step1_solutions: 0,
step2_solutions: 0,
found_100_percent: false,
}
}
}
/// Pretty display for SolverResult
impl Display for SolverResult {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
@ -67,12 +106,7 @@ impl Display for SolverResult {
impl SolverResult {
/// Create a SolverResult from a solution
/// Using this returns a solution, usable by Python
pub fn from_craft(
craft: &Craft,
step1_solutions: usize,
step2_solutions: usize,
found_100_percent: bool,
) -> SolverResult {
pub fn from_craft(craft: &Craft) -> SolverResult {
let mut actions = craft
.actions
.iter()
@ -97,9 +131,6 @@ impl SolverResult {
quality: craft.quality,
durability: craft.durability,
actions: actions,
step1_solutions,
step2_solutions,
found_100_percent,
total_progression: craft.recipe.progress,
total_quality: craft.recipe.quality,
total_durability: craft.recipe.durability,
@ -128,9 +159,6 @@ impl SolverResult {
total_quality: 0,
total_durability: 0,
actions: vec!["Act1".to_string(), "Act2".to_string()],
step1_solutions: 0,
step2_solutions: 0,
found_100_percent: false,
cp: 0,
total_cp: 0,
}
@ -160,6 +188,8 @@ impl SolverResult {
#[pymodule]
fn xiv_csolver_lib(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(test_result, m)?)?;
m.add_function(wrap_pyfunction!(test_final_results, m)?)?;
m.add_function(wrap_pyfunction!(test_info, m)?)?;
m.add_function(wrap_pyfunction!(solve_from_python, m)?)?;
Ok(())
}
@ -177,9 +207,6 @@ fn xiv_csolver_lib(_py: Python, m: &PyModule) -> PyResult<()> {
/// total_quality: 0,
/// total_durability: 0,
/// actions: vec!["Act1".to_string(), "Act2".to_string()],
/// step1_solutions: 0,
/// step2_solutions: 0,
/// found_100_percent: false,
/// cp: 0,
/// total_cp: 0,
/// };
@ -190,6 +217,41 @@ pub fn test_result() -> SolverResult {
SolverResult::default()
}
/// A test info, useful for debugging, all values are set to zero as per the
/// [`default`](crate::io::Info::default)
/// ```
/// # use xiv_csolver_lib::python_bindings::Info;
/// Info {
/// step1_solutions: 0,
/// step2_solutions: 0,
/// found_100_percent: false,
/// };
/// ```
#[cfg(not(feature = "no_python"))]
#[pyfunction]
pub fn test_info() -> Info {
Info::default()
}
/// A test result array, useful for debugging, all values are set to zero as per the
/// [`default`](crate::io::Parameters::default)
/// ```
/// # use xiv_csolver_lib::python_bindings::{Info};
/// # use xiv_csolver_lib::python_bindings::SolverResult;
/// (
/// vec![SolverResult::default(), SolverResult::default()],
/// Info::default(),
/// );
/// ```
#[cfg(not(feature = "no_python"))]
#[pyfunction]
pub fn test_final_results() -> (Vec<SolverResult>, Info) {
(
vec![SolverResult::default(), SolverResult::default()],
Info::default(),
)
}
/// Create a stat struct stats with the base values
#[cfg(not(feature = "no_python"))]
use crate::{solve_craft, Recipe, Stats};
@ -224,7 +286,10 @@ use crate::{solve_craft, Recipe, Stats};
#[cfg(not(feature = "no_python"))]
#[pyfunction]
pub fn solve_from_python(py: Python<'_>, values: &PyAny) -> PyResult<Option<Vec<SolverResult>>> {
pub fn solve_from_python(
py: Python<'_>,
values: &PyAny,
) -> PyResult<Option<(Vec<SolverResult>, Info)>> {
// Create Recipe
let recipe = Recipe {
durability: values.getattr("durability")?.extract()?,
@ -258,3 +323,15 @@ pub fn solve_from_python(py: Python<'_>, values: &PyAny) -> PyResult<Option<Vec<
Ok(res)
})
}
#[cfg(test)]
mod test {
use crate::python_bindings::{test_final_results, test_info, test_result};
#[test]
pub fn test_function_call() {
test_final_results();
test_info();
test_result();
}
}