use lemma::parsing::ast::DateTimeValue;
use lemma::{Engine, LiteralValue, Target};
use rust_decimal::Decimal;
use std::collections::HashMap;
use std::str::FromStr;
fn setup_engine(code: &str) -> Engine {
let mut engine = Engine::new();
engine
.load(code, lemma::SourceType::Labeled("test"))
.expect("Failed to add code");
engine
}
fn dec(s: &str) -> Decimal {
Decimal::from_str(s).expect("valid decimal")
}
#[test]
fn invert_exp_simple() {
let code = r#"
spec math
fact x: [number]
rule y: exp x
"#;
let engine = setup_engine(code);
let now = DateTimeValue::now();
let solutions = engine
.invert(
"math",
&now,
"y",
Target::value(LiteralValue::number(dec("7.38905609893065"))),
HashMap::new(),
)
.expect("invert OK");
assert!(!solutions.is_empty(), "Expected at least one solution");
}
#[test]
fn invert_power_exponent_unknown() {
let code = r#"
spec math
fact x: [number]
rule y: 2 ^ x
"#;
let engine = setup_engine(code);
let now = DateTimeValue::now();
let solutions = engine
.invert(
"math",
&now,
"y",
Target::value(LiteralValue::number(8.into())),
HashMap::new(),
)
.expect("invert OK");
assert!(!solutions.is_empty(), "Expected at least one solution");
}
#[test]
fn invert_power_base_unknown() {
let code = r#"
spec math
fact x: [number]
rule y: x ^ 2
"#;
let engine = setup_engine(code);
let now = DateTimeValue::now();
let solutions = engine
.invert(
"math",
&now,
"y",
Target::value(LiteralValue::number(9.into())),
HashMap::new(),
)
.expect("invert OK");
assert!(!solutions.is_empty(), "Expected at least one solution");
}