Skip to main content

conjure_oxide/
test_solve.rs

1use std::path::PathBuf;
2use std::process::exit;
3use std::sync::Arc;
4
5use clap::ValueHint;
6use conjure_cp_cli::utils::conjure::{
7    get_solutions, get_solutions_from_conjure, solutions_to_json,
8};
9use conjure_cp_cli::utils::testing::normalize_solutions_for_comparison;
10
11use crate::cli::GlobalArgs;
12use crate::solve::{self, init_solver};
13
14#[derive(Clone, Debug, clap::Args)]
15pub struct Args {
16    /// The input Essence file
17    #[arg(value_name = "INPUT_ESSENCE",value_hint=ValueHint::FilePath)]
18    pub input_file: PathBuf,
19}
20
21pub fn run_test_solve_command(global_args: GlobalArgs, local_args: Args) -> anyhow::Result<()> {
22    // stealing most of the steps of the solve command, except the solver stuff.
23    let input_file = local_args.input_file;
24
25    let context = solve::init_context(&global_args, input_file.clone())?;
26    let model = solve::parse(&global_args, Arc::clone(&context))?;
27    let rewritten_model = solve::rewrite(model, &global_args, Arc::clone(&context))?;
28
29    let solver = init_solver(&global_args);
30
31    // now we are stealing from the integration tester
32
33    let our_solutions = get_solutions(
34        solver,
35        rewritten_model,
36        0,
37        &global_args.save_solver_input_file,
38    )?;
39
40    let conjure_solutions =
41        get_solutions_from_conjure(input_file.to_str().unwrap(), Arc::clone(&context))?;
42
43    let our_solutions = normalize_solutions_for_comparison(&our_solutions);
44    let conjure_solutions = normalize_solutions_for_comparison(&conjure_solutions);
45
46    let mut our_solutions_json = solutions_to_json(&our_solutions);
47    let mut conjure_solutions_json = solutions_to_json(&conjure_solutions);
48
49    our_solutions_json.sort_all_objects();
50    conjure_solutions_json.sort_all_objects();
51
52    if our_solutions_json == conjure_solutions_json {
53        eprintln!("Success: solutions match!");
54        exit(0);
55    } else {
56        eprintln!("=== our solutions:");
57        eprintln!("{our_solutions_json}");
58        eprintln!("=== conjure's solutions:");
59        eprintln!("{conjure_solutions_json}");
60        eprintln!("Failure: solutions do not match!");
61        exit(1);
62    }
63}