use super::{BUILT_IN, Guides, Operand, values};
use f_office_units::fixed::ONE;
fn last(guides: &[(&str, &str)], size: [i128; 2]) -> Option<i128> {
let mut list = Guides::default();
for (name, formula) in guides {
list.add(name, formula).unwrap();
}
values(&list.into_formulas(), size.map(|side| side * ONE)).map(|all| *all.last().unwrap())
}
fn formula(text: &str) -> i128 {
last(&[("g", text)], [600, 400]).unwrap()
}
fn near(a: i128, b: i128) -> bool {
(a - b).abs() <= 4
}
fn whole(a: i128, b: i128) -> bool {
near(a, b * ONE)
}
#[test]
fn computes_each_formula_of_the_standard() {
for (text, want) in [
("*/ 3 4 2", 6),
("+- 3 4 2", 5),
("+/ 3 5 2", 4),
("?: 1 7 9", 7),
("?: 0 7 9", 9),
("?: -1 7 9", 9),
("abs -5", 5),
("at2 1 1", 2_700_000),
("at2 -1 0", 10_800_000),
("at2 0 -1", -5_400_000),
("cat2 10 -1 0", -10),
("cat2 10 0 0", 10),
("sat2 10 0 0", 0),
("cos 10 10800000", -10),
("sin 10 5400000", 10),
("tan 10 2700000", 10),
("max 3 4", 4),
("min 3 4", 3),
("mod 2 3 6", 7),
("pin 0 5 10", 5),
("pin 0 -5 10", 0),
("pin 0 15 10", 10),
("sqrt 16", 4),
("val -8333", -8333),
] {
assert!(whole(formula(text), want), "{text}");
}
assert!(near(formula("sat2 10 1 1"), 463_410));
}
#[test]
fn divides_by_zero_to_zero() {
assert_eq!(formula("*/ 3 4 0"), 0);
assert_eq!(formula("+/ 3 4 0"), 0);
assert_eq!(formula("tan 10 5400000"), 0);
}
#[test]
fn has_no_value_beyond_its_limit_or_for_the_root_of_a_value_below_zero() {
let huge = "*/ 27273042316900 27273042316900 1";
assert_eq!(last(&[("g", huge)], [600, 400]), None);
assert_eq!(last(&[("g", "sqrt -1")], [600, 400]), None);
assert_eq!(last(&[("g", "sqrt -1"), ("h", "val 3")], [600, 400]), None);
}
#[test]
fn gives_the_built_in_guides_of_the_shape_size() {
let value = |name: &str| formula(&format!("val {name}"));
for (name, want) in [
("w", 600),
("r", 600),
("h", 400),
("b", 400),
("l", 0),
("t", 0),
("hc", 300),
("vc", 200),
("ss", 400),
("ls", 600),
("wd2", 300),
("wd10", 60),
("ssd16", 25),
("cd4", 5_400_000),
("3cd4", 16_200_000),
("7cd8", 18_900_000),
] {
assert_eq!(value(name), want * ONE, "{name}");
}
assert_eq!(value("wd32"), 1_228_800);
assert_eq!(value("hd3"), 8_738_133);
assert_eq!(value("ssd32"), 819_200);
assert_eq!(BUILT_IN.len(), 37);
}
#[test]
fn reads_a_guide_after_the_guides_that_it_names_and_the_last_of_one_name() {
let guides = [
("adj", "val 25000"),
("a", "pin 0 adj 50000"),
("x1", "*/ ss a 100000"),
("x1", "+- x1 1 0"),
("y", "+- r 0 x1"),
];
assert_eq!(last(&guides, [600, 400]), Some((600 - 101) * ONE));
assert_eq!(
last(&[("w", "val 7"), ("g", "*/ w 2 1")], [600, 400]),
Some(14 * ONE)
);
}
#[test]
fn refuses_a_formula_that_does_not_read() {
let mut guides = Guides::default();
for text in [
"",
"val",
"*/ 1 2",
"add 1 2 3",
"val later",
"val 1.5",
"val 27273042316901",
] {
assert_eq!(guides.add("g", text), None, "{text:?}");
}
assert_eq!(guides.add("g", " val 27273042316900 "), Some(()));
}
#[test]
fn leaves_out_the_words_after_the_arguments_and_reads_an_operand() {
let mut guides = Guides::default();
assert_eq!(guides.add("g", "val 1"), Some(()));
assert_eq!(guides.add("h", "+- g 0 g 0"), Some(()));
assert_eq!(last(&[("h", "+- 5 0 2 0")], [1, 1]), Some(3 * ONE));
assert_eq!(guides.operand("g"), Some(Operand::Guide(BUILT_IN.len())));
assert_eq!(guides.operand("-3"), Some(Operand::Number(-3)));
assert_eq!(guides.operand("nothing"), None);
}