office: add formula Rust tests and fix PROPER and DATEDIF
- Commit
ea1febfda80ad836ebcab5044a6ff5968a160d7d- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-17 21:55 UTC (18 days ago)
- Parents
e82ff81
New formula eval and helper tests (85 -> 104) across the previously near-untested function families (IFS family, stats, finance, date/time, text, lookup). Fixes found by the tests: PROPER did not lowercase word tails (PROPER("hELLO wORLD") gave "HELLO WORLD", Excel gives "Hello World"); DATEDIF counted incomplete months/years as complete for the M and Y units. Line coverage 59.5% -> 83.1%. cargo test -p office_engine green.Diff
office/src/main/rust/src/formula.rs
diff --git a/office/src/main/rust/src/formula.rs b/office/src/main/rust/src/formula.rsindex 598ee89672..d0645a4fb7 100644--- a/office/src/main/rust/src/formula.rs+++ b/office/src/main/rust/src/formula.rs@@ -489,4 +489,11 @@ include!("formula_part9.rs"); include!("formula_part10.rs"); include!("formula_part11.rs"); include!("formula_part12.rs");-include!("formula_part13.rs");\ No newline at end of file+include!("formula_part13.rs");++#[cfg(test)]+#[path = "formula_tests.rs"]+mod formula_tests;+#[cfg(test)]+#[path = "formula_helper_tests.rs"]+mod formula_helper_tests;\ No newline at end of file
office/src/main/rust/src/formula_helper_tests.rs
diff --git a/office/src/main/rust/src/formula_helper_tests.rs b/office/src/main/rust/src/formula_helper_tests.rsnew file mode 100644index 0000000000..03550b6765--- /dev/null+++ b/office/src/main/rust/src/formula_helper_tests.rs@@ -0,0 +1,255 @@+//! Direct tests for the pure free helpers (parts 1, 4, 9, 13):+//! civil-date arithmetic, fraction/time formatting, roman numerals,+//! percentile/correlation math, 30/360 day counts and LET helpers.+use super::*;++#[test]+fn civil_date_arithmetic() {+ assert_eq!(days_from_civil(1970, 1, 1), 0);+ assert_eq!(days_from_civil(2000, 1, 1), 10957);+ assert_eq!(civil_from_days(0), (1970, 1, 1));+ assert_eq!(civil_from_days(10957), (2000, 1, 1));+ for &(y, m, d) in &[+ (1900, 3, 1),+ (1999, 12, 31),+ (2000, 2, 29),+ (2020, 2, 29),+ (2023, 11, 14),+ (2400, 2, 29),+ ] {+ assert_eq!(civil_from_days(days_from_civil(y, m, d)), (y, m, d));+ }+ assert!(is_leap(2000));+ assert!(!is_leap(1900));+ assert!(is_leap(2024));+ assert!(!is_leap(2023));+ assert_eq!(days_in_month(2020, 2), 29);+ assert_eq!(days_in_month(2019, 2), 28);+ assert_eq!(days_in_month(2021, 4), 30);+ assert_eq!(days_in_month(2021, 1), 31);+ // Serial epoch: DATE(2020,1,1) = 43831, DATE(2020,2,29) = 43890.+ assert_eq!(date_serial(2020, 1, 1), 43831.0);+ assert_eq!(date_serial(2020, 2, 29), 43890.0);+ // Lenient month overflow rolls into the next year.+ assert_eq!(date_serial(2020, 13, 1), date_serial(2021, 1, 1));+ let dt = serial_to_datetime(43831.0);+ assert_eq!((dt.year, dt.month, dt.day, dt.dow), (2020, 1, 1, 4)); // Wed+ assert_eq!(format_date_iso(43831.0), "2020-01-01");+}++#[test]+fn add_months_clamps_day() {+ assert_eq!(add_months(2020, 1, 31, 1), (2020, 2, 29));+ assert_eq!(add_months(2019, 1, 31, 1), (2019, 2, 28));+ assert_eq!(add_months(2020, 3, 31, -1), (2020, 2, 29));+ assert_eq!(add_months(2020, 12, 15, 1), (2021, 1, 15));+ assert_eq!(add_months(2020, 1, 15, -1), (2019, 12, 15));+ assert_eq!(add_months(2020, 6, 15, 12), (2021, 6, 15));+}++#[test]+fn fraction_and_time_formatting() {+ assert_eq!(format_fraction(1.5, 1), "1 1/2");+ assert_eq!(format_fraction(0.25, 2), "1/4");+ assert_eq!(format_fraction(-0.5, 1), "-1/2");+ assert_eq!(format_fraction(-1.5, 1), "-1 1/2");+ assert_eq!(format_fraction(2.0, 1), "2");+ assert_eq!(format_fraction(0.333, 2), "1/3");+ assert_eq!(format_time(0.5), "12:00:00");+ assert_eq!(format_time(0.25), "06:00:00");+ assert_eq!(format_time(0.0), "00:00:00");+ assert_eq!(text_format(0.5, "0%"), "50%");+ assert_eq!(text_format(1234.5, "#,##0.00"), "1,234.50");+ assert_eq!(text_format(1234.5, "0"), "1235");+ assert_eq!(format_scientific(12345.0, 2), "1.23E+04");+ assert_eq!(format_scientific(0.0, 2), "0.00E+00");+}++#[test]+fn date_time_token_formatting() {+ let tok = |kind: &str, style: Option<&str>, textual: bool| NumberToken {+ kind: kind.to_string(),+ style: style.map(|s| s.to_string()),+ text: None,+ textual,+ };+ let tokens = vec![+ tok("year", Some("long"), false),+ tok("text", None, false),+ tok("month", Some("long"), true),+ tok("text", None, false),+ tok("day", Some("long"), false),+ ];+ let mut tokens = tokens;+ tokens[1].text = Some("-".to_string());+ tokens[3].text = Some("-".to_string());+ assert_eq!(format_date_time(43831.0, &tokens), "2020-January-01");+ let dow = vec![tok("day-of-week", Some("long"), false)];+ assert_eq!(format_date_time(43831.0, &dow), "Wednesday");+ let dow_short = vec![tok("day-of-week", None, false)];+ assert_eq!(format_date_time(43831.0, &dow_short), "Wed");+}++#[test]+fn roman_and_integer_helpers() {+ assert_eq!(roman_to_arabic("XIV"), 14);+ assert_eq!(roman_to_arabic("MCMXCIV"), 1994);+ assert_eq!(roman_to_arabic("xiv"), 14);+ assert_eq!(roman_to_arabic(""), 0);+ assert_eq!(arabic_to_roman(1994), "MCMXCIV");+ assert_eq!(arabic_to_roman(4), "IV");+ assert_eq!(arabic_to_roman(0), "0");+ assert_eq!(arabic_to_roman(4000), "4000");+ assert_eq!(gcd_l(12, 18), 6);+ assert_eq!(gcd_l(0, 5), 5);+ assert_eq!(gcd_l(-12, 18), 6);+ assert_eq!(fact_d(0), 1.0);+ assert_eq!(fact_d(5), 120.0);+ assert!(avg_of(&[]).is_nan());+ assert_eq!(avg_of(&[1.0, 2.0, 3.0]), 2.0);+ assert_eq!(ksign(-2.5), -1.0);+ assert_eq!(ksign(0.0), 0.0);+ assert_eq!(ksign(3.0), 1.0);+ assert_eq!(substring_after_last("Sheet1.A1", '.'), "A1");+ assert_eq!(substring_after_last("A1", '.'), "A1");+ assert_eq!(pipe_min(&[]), 0.0);+ assert_eq!(pipe_max(&[1.0, 9.0, 3.0]), 9.0);+ assert!(index_of("hello", "L", 0, true) == Some(2));+ assert!(index_of("hello", "L", 0, false).is_none());+ assert_eq!(bool_or_null(&Value::Num(0.0)), Some(false));+ assert_eq!(bool_or_null(&Value::Str("x".into())), None);+}++#[test]+fn stats_and_calendar_helpers() {+ let sorted = [1.0, 2.0, 3.0, 4.0];+ assert_eq!(percentile_of(&sorted, 0.0), 1.0);+ assert_eq!(percentile_of(&sorted, 0.5), 2.5);+ assert_eq!(percentile_of(&sorted, 1.0), 4.0);+ assert_eq!(correl(&[1.0, 2.0, 3.0], &[10.0, 20.0, 30.0]).unwrap(), 1.0);+ assert!(correl(&[1.0, 1.0], &[2.0, 2.0]).is_err());+ assert_eq!(slope(&[10.0, 20.0, 30.0], &[1.0, 2.0, 3.0]).unwrap(), 10.0);+ assert!(slope(&[1.0], &[1.0]).is_err());+ // 30/360: Jan 1 -> Feb 1 is 30 days both conventions.+ assert_eq!(days360(date_serial(2020, 1, 1), date_serial(2020, 2, 1), false), 30);+ assert_eq!(days360(date_serial(2020, 1, 31), date_serial(2020, 2, 1), true), 1);+ assert_eq!(weeknum(date_serial(2020, 1, 1)), 1);+ assert_eq!(weeknum(date_serial(2020, 1, 5)), 2);+ assert_eq!(iso_week_num(date_serial(2020, 1, 1)), 1);+ assert_eq!(iso_week_num(date_serial(2020, 12, 31)), 53);+ assert_eq!(year_frac(date_serial(2020, 1, 1), date_serial(2021, 1, 1), 3), 366.0 / 365.0);+ // Financial cores: PMT of a 1-period 10% loan of 1000 is -1100.+ assert!((pmt_calc(0.1, 1.0, 1000.0, 0.0, 0) + 1100.0).abs() < 1e-9);+ assert_eq!(pmt_calc(0.0, 10.0, 1000.0, 0.0, 0), -100.0);+ assert!((fv_of(0.1, 1.0, -100.0, 0.0, 0) - 100.0).abs() < 1e-9);+ assert_eq!(ipmt_calc(0.1, 1, 1.0, 1000.0, 0.0, 0), -100.0);+ assert_eq!(a1_to_coords("B3"), Some((2, 1)));+ assert_eq!(a1_to_coords("Sheet1.B3"), Some((2, 1)));+ assert_eq!(a1_to_coords("$A$1"), Some((0, 0)));+ assert_eq!(a1_to_coords(""), None);+}++#[test]+fn let_and_compare_helpers() {+ assert_eq!(+ split_top_compare("[.A1:.A3]>15"),+ Some((">".to_string(), "[.A1:.A3]".to_string(), "15".to_string()))+ );+ assert_eq!(+ split_top_compare("x<=2"),+ Some(("<=".to_string(), "x".to_string(), "2".to_string()))+ );+ assert!(split_top_compare("SUM(1;2)").is_none());+ assert!(split_top_compare("\"a>b\"").is_none());+ assert!(is_bare_ref_text("[.A1:.A3]"));+ assert!(is_bare_ref_text("MyRange"));+ assert!(!is_bare_ref_text("SUM(A1)"));+ assert!(!is_bare_ref_text("1ABC"));+ assert!(is_let_name("my_var"));+ assert!(!is_let_name("X1"));+ assert!(!is_let_name("TRUE"));+ assert!(!is_let_name("1x"));+ assert_eq!(+ substitute_let_names("x+1", &[("X".to_string(), "2".to_string())]),+ "(2)+1"+ );+ assert_eq!(+ substitute_let_names("\"x\"&x", &[("X".to_string(), "2".to_string())]),+ "\"x\"&(2)"+ );+ assert_eq!(value_literal(&Value::Num(2.5)).unwrap(), "2.5");+ assert_eq!(value_literal(&Value::Str("a\"b".into())).unwrap(), "\"a\"\"b\"");+ assert_eq!(value_literal(&Value::Bool(true)).unwrap(), "TRUE");+ assert!(value_literal(&Value::Err("#N/A".into())).is_err());+ assert_eq!(format_scalar(&Value::Bool(false)), "FALSE");+ assert_eq!(format_scalar(&Value::Blank), "");+ // Excel ordering: numbers < text < logicals < blanks < errors.+ assert_eq!(cmp_values(&Value::Num(1.0), &Value::Str("a".into())), std::cmp::Ordering::Less);+ assert_eq!(cmp_values(&Value::Str("b".into()), &Value::Str("A".into())), std::cmp::Ordering::Greater);+ assert_eq!(cmp_values(&Value::Bool(false), &Value::Bool(true)), std::cmp::Ordering::Less);+ assert!(wildcard_match(&Value::Str("a*".into()), &Value::Str("abc".into())).unwrap());+ assert!(!wildcard_match(&Value::Str("a*".into()), &Value::Str("xbc".into())).unwrap());+}++#[test]+fn address_and_wildcard_helpers() {+ assert_eq!(parse_a1_coords("B12"), Some((11, 1)));+ assert_eq!(parse_a1_coords("$c$3"), Some((2, 2)));+ assert_eq!(parse_a1_coords("A1"), Some((0, 0)));+ assert_eq!(parse_a1_coords("12"), None);+ assert_eq!(parse_a1_coords("A0"), None);+ assert_eq!(parse_a1_coords("A1x"), None);+ assert_eq!(split_sheet_cell("Sheet1.A1"), (Some("Sheet1".to_string()), "A1"));+ assert_eq!(split_sheet_cell("A1"), (None, "A1"));+ assert_eq!(split_sheet_cell("'My Sheet'.$B$2"), (Some("My Sheet".to_string()), "$B$2"));+ assert_eq!(col_to_index("A"), 0);+ assert_eq!(col_to_index("Z"), 25);+ assert_eq!(col_to_index("AA"), 26);+ assert_eq!(index_to_col(0), "A");+ assert_eq!(index_to_col(25), "Z");+ assert_eq!(index_to_col(26), "AA");+ assert_eq!(index_to_col(701), "ZZ");+ for i in [0, 1, 25, 26, 51, 52, 701, 702] {+ assert_eq!(col_to_index(&index_to_col(i)), i);+ }+ assert_eq!(normalize("of:=1+1"), "1+1");+ assert_eq!(normalize("=1+1"), "1+1");+ assert_eq!(normalize(" 1+1 "), "1+1");+ assert!(cmp_ignore_case("abc", "ABC") == 0);+ assert!(cmp_ignore_case("abc", "abd") < 0);+ assert!(cmp_ignore_case("ab", "a") > 0);+ assert!(wildcard_equals("Hello", "h*o"));+ assert!(wildcard_equals("abc", "a?c"));+ assert!(!wildcard_equals("abc", "a?d"));+ assert!(wildcard_equals("abc", "ABC"));+ assert!(!wildcard_equals("abc", "abd"));+ // parse_odf_address against a one-sheet workbook model.+ let sheets = vec![Sheet { name: "Sheet1".to_string(), rows: Vec::new() }];+ let mut idx = HashMap::new();+ idx.insert("Sheet1".to_string(), 0usize);+ let r = parse_odf_address("Sheet1.A1:Sheet1.B2", &sheets, &idx).unwrap();+ assert_eq!((r.r1, r.c1, r.r2, r.c2), (0, 0, 1, 1));+ let r = parse_odf_address("=B2", &sheets, &idx).unwrap();+ assert_eq!((r.r1, r.c1), (1, 1));+ assert!(parse_odf_address("Nope.A1", &sheets, &idx).is_none());+ assert!(parse_odf_address("A1", &[], &idx).is_none());+}++#[test]+fn text_before_after_helper() {+ assert_eq!(+ text_before_after("a,b,c", ",", 1, true).unwrap(),+ Value::Str("a".to_string())+ );+ assert_eq!(+ text_before_after("a,b,c", ",", 2, false).unwrap(),+ Value::Str("c".to_string())+ );+ assert!(text_before_after("abc", ",", 1, true).is_err());+ assert_eq!(+ text_before_after("a--b--c", "--", 1, false).unwrap(),+ Value::Str("b--c".to_string())+ );+ assert_eq!(find_chars(&['a', 'b', 'c'], &['b'], 0), Some(1));+ assert_eq!(find_chars(&['a'], &['b'], 0), None);+}
office/src/main/rust/src/formula_part5.rs
diff --git a/office/src/main/rust/src/formula_part5.rs b/office/src/main/rust/src/formula_part5.rsindex 280a6ddffb..3b80dfd12d 100644--- a/office/src/main/rust/src/formula_part5.rs+++ b/office/src/main/rust/src/formula_part5.rs@@ -316,7 +316,10 @@ impl<'a> Evaluator<'a> { .map(|w| { let mut ch = w.chars(); match ch.next() {- Some(f) => f.to_uppercase().collect::<String>() + ch.as_str(),+ Some(f) => {+ f.to_uppercase().collect::<String>()+ + &ch.flat_map(|c| c.to_lowercase()).collect::<String>()+ } None => String::new(), } })
office/src/main/rust/src/formula_part7.rs
diff --git a/office/src/main/rust/src/formula_part7.rs b/office/src/main/rust/src/formula_part7.rsindex a41b3b5695..4b51b205a5 100644--- a/office/src/main/rust/src/formula_part7.rs+++ b/office/src/main/rust/src/formula_part7.rs@@ -24,12 +24,19 @@ impl<'a> Evaluator<'a> { let unit = self.t_str(&a[2])?.to_uppercase(); let c1 = serial_to_datetime(n0); let c2 = serial_to_datetime(n1);+ // Complete months between the dates (Excel DATEDIF semantics):+ // the day-of-month must have been reached to count the month.+ let complete_months = {+ let mut m = (c2.year - c1.year) * 12 + (c2.month - c1.month);+ if c2.day < c1.day {+ m -= 1;+ }+ m+ }; match unit.as_str() { "D" => Ok(Value::Num(n1.floor() - n0.floor())),- "M" => Ok(Value::Num(- ((c2.year - c1.year) * 12 + (c2.month - c1.month)) as f64,- )),- "Y" => Ok(Value::Num((c2.year - c1.year) as f64)),+ "M" => Ok(Value::Num(complete_months as f64)),+ "Y" => Ok(Value::Num(complete_months.div_euclid(12) as f64)), "MD" => { let d1 = c1.day; let d2 = c2.day;
office/src/main/rust/src/formula_tests.rs
diff --git a/office/src/main/rust/src/formula_tests.rs b/office/src/main/rust/src/formula_tests.rsnew file mode 100644index 0000000000..34264ac976--- /dev/null+++ b/office/src/main/rust/src/formula_tests.rs@@ -0,0 +1,355 @@+//! Evaluation tests for the extended function families (parts 5-9):+//! multi-criteria aggregation, extended math/statistics, extended date,+//! financial, info, bitwise, radix/roman and lookup helpers. Every+//! expectation is the Excel/OpenFormula result for the same call.+use super::*;++// Sheet1: A1..A3 = 1,2,3 ; B1..B3 = 10,20,30 ; probe formula in C1.+fn wb_with(formula: &str) -> Workbook {+ let data = [[1.0, 10.0], [2.0, 20.0], [3.0, 30.0]];+ let mut rows: Vec<String> = Vec::new();+ for (r, row) in data.iter().enumerate() {+ let mut cells: Vec<String> = row+ .iter()+ .map(|v| format!("{{\"text\":\"{}\",\"numberValue\":{}}}", v, v))+ .collect();+ if r == 0 {+ cells.push(format!(+ "{{\"text\":\"\",\"formula\":\"{}\"}}",+ formula.replace('"', "\\\"")+ ));+ }+ rows.push(format!("{{\"cells\":[{}]}}", cells.join(",")));+ }+ let json = format!(+ "{{\"sheets\":[{{\"name\":\"Sheet1\",\"rows\":[{}]}}]}}",+ rows.join(",")+ );+ Workbook::from_json(&json, 1_700_000_000_000).unwrap()+}++fn eval(formula: &str) -> String {+ wb_with(formula).display_value(0, 0, 2)+}++// A-column probe over a caller-supplied single column (for MODE/IRR).+fn eval_col(formula: &str, col_a: &[f64]) -> String {+ let mut rows: Vec<String> = Vec::new();+ for (r, v) in col_a.iter().enumerate() {+ let mut cells = vec![format!("{{\"text\":\"{}\",\"numberValue\":{}}}", v, v)];+ if r == 0 {+ cells.push("{\"text\":\"10\",\"numberValue\":10}".to_string());+ cells.push(format!(+ "{{\"text\":\"\",\"formula\":\"{}\"}}",+ formula.replace('"', "\\\"")+ ));+ }+ rows.push(format!("{{\"cells\":[{}]}}", cells.join(",")));+ }+ let json = format!(+ "{{\"sheets\":[{{\"name\":\"Sheet1\",\"rows\":[{}]}}]}}",+ rows.join(",")+ );+ Workbook::from_json(&json, 1_700_000_000_000)+ .unwrap()+ .display_value(0, 0, 2)+}++#[test]+fn multi_criteria_and_products() {+ assert_eq!(eval("of:=SUMIFS([.B1:.B3];[.A1:.A3];\">1\")"), "50");+ assert_eq!(eval("of:=COUNTIFS([.A1:.A3];\">1\";[.B1:.B3];\"<30\")"), "1");+ assert_eq!(eval("of:=AVERAGEIFS([.B1:.B3];[.A1:.A3];\">1\")"), "25");+ assert_eq!(eval("of:=AVERAGEIFS([.B1:.B3];[.A1:.A3];\">99\")"), "#DIV/0!");+ assert_eq!(eval("of:=SUMPRODUCT([.A1:.A3];[.B1:.B3])"), "140");+ assert_eq!(eval("of:=SUMSQ([.A1:.A3])"), "14");+ assert_eq!(eval("of:=SUMX2PY2([.A1:.A3];[.B1:.B3])"), "1414");+ assert_eq!(eval("of:=SUMX2MY2([.A1:.A3];[.B1:.B3])"), "-1386");+ assert_eq!(eval("of:=SUMXMY2([.A1:.A3];[.B1:.B3])"), "1134");+}++#[test]+fn extended_math() {+ assert_eq!(eval("of:=MROUND(10;3)"), "9");+ assert_eq!(eval("of:=MROUND(11;3)"), "12");+ assert_eq!(eval("of:=MROUND(10;0)"), "0");+ assert_eq!(eval("of:=EVEN(1.5)"), "2");+ assert_eq!(eval("of:=EVEN(-1.5)"), "-2");+ assert_eq!(eval("of:=EVEN(2)"), "2");+ assert_eq!(eval("of:=ODD(2)"), "3");+ assert_eq!(eval("of:=ODD(-2)"), "-3");+ assert_eq!(eval("of:=ODD(0)"), "1");+ assert_eq!(eval("of:=GCD(12;18)"), "6");+ assert_eq!(eval("of:=LCM(4;6)"), "12");+ assert_eq!(eval("of:=FACT(5)"), "120");+ assert_eq!(eval("of:=FACT(-1)"), "#NUM!");+ assert_eq!(eval("of:=COMBIN(5;2)"), "10");+ assert_eq!(eval("of:=COMBIN(5;6)"), "#NUM!");+ assert_eq!(eval("of:=MULTINOMIAL(2;3)"), "10");+ assert_eq!(eval("of:=QUOTIENT(10;3)"), "3");+ assert_eq!(eval("of:=QUOTIENT(-10;3)"), "-3");+ assert_eq!(eval("of:=QUOTIENT(10;0)"), "#DIV/0!");+ assert_eq!(eval("of:=MOD(10;3)"), "1");+ assert_eq!(eval("of:=MOD(-3;2)"), "1");+ assert_eq!(eval("of:=PRODUCT([.A1:.A3])"), "6");+ assert_eq!(eval("of:=MEDIAN([.B1:.B3])"), "20");+ assert_eq!(eval("of:=SIGN(-4)"), "-1");+ assert_eq!(eval("of:=INT(-1.5)"), "-2");+ assert_eq!(eval("of:=TRUNC(-1.5)"), "-1");+ assert_eq!(eval("of:=ROUND(2.567;2)"), "2.57");+ assert_eq!(eval("of:=ROUNDUP(2.561;2)"), "2.57");+ assert_eq!(eval("of:=ROUNDDOWN(2.569;2)"), "2.56");+ assert_eq!(eval("of:=CEILING(2.1;0.5)"), "2.5");+ assert_eq!(eval("of:=FLOOR(2.9;0.5)"), "2.5");+ assert_eq!(eval("of:=LOG(1000)"), "3");+ assert_eq!(eval("of:=LOG(8;2)"), "3");+ assert_eq!(eval("of:=POWER(2;10)"), "1024");+}++#[test]+fn trig_and_hyperbolic() {+ assert_eq!(eval("of:=SIN(0)"), "0");+ assert_eq!(eval("of:=COS(0)"), "1");+ assert_eq!(eval("of:=TAN(0)"), "0");+ assert_eq!(eval("of:=SEC(0)"), "1");+ assert_eq!(eval("of:=SINH(0)"), "0");+ assert_eq!(eval("of:=COSH(0)"), "1");+ assert_eq!(eval("of:=TANH(0)"), "0");+ assert_eq!(eval("of:=ASINH(0)"), "0");+ assert_eq!(eval("of:=ACOSH(1)"), "0");+ assert_eq!(eval("of:=ATANH(0)"), "0");+ assert_eq!(eval("of:=RADIANS(180)"), "3.1416");+ assert_eq!(eval("of:=DEGREES(1)"), "57.2958");+ assert_eq!(eval("of:=ATAN2(1;1)"), "0.7854");+}++#[test]+fn extended_statistics() {+ assert_eq!(eval("of:=STDEVP([.A1:.A3])"), "0.8165");+ assert_eq!(eval("of:=VARP([.A1:.A3])"), "0.6667");+ assert_eq!(eval("of:=STDEV([.A1:.A3])"), "1");+ assert_eq!(eval("of:=VAR([.A1:.A3])"), "1");+ assert_eq!(eval("of:=GEOMEAN([.A1:.A3])"), "1.8171");+ assert_eq!(eval("of:=HARMEAN([.A1:.A3])"), "1.6364");+ assert_eq!(eval("of:=AVEDEV([.A1:.A3])"), "0.6667");+ assert_eq!(eval("of:=DEVSQ([.A1:.A3])"), "2");+ assert_eq!(eval("of:=CORREL([.A1:.A3];[.B1:.B3])"), "1");+ assert_eq!(eval("of:=PEARSON([.A1:.A3];[.B1:.B3])"), "1");+ assert_eq!(eval("of:=COVAR([.A1:.A3];[.B1:.B3])"), "6.6667");+ assert_eq!(eval("of:=SLOPE([.B1:.B3];[.A1:.A3])"), "10");+ assert_eq!(eval("of:=INTERCEPT([.B1:.B3];[.A1:.A3])"), "0");+ assert_eq!(eval("of:=FORECAST(4;[.B1:.B3];[.A1:.A3])"), "40");+ assert_eq!(eval("of:=QUARTILE([.A1:.A3];0)"), "1");+ assert_eq!(eval("of:=QUARTILE([.A1:.A3];2)"), "2");+ assert_eq!(eval("of:=QUARTILE([.A1:.A3];4)"), "3");+ assert_eq!(eval("of:=QUARTILE([.A1:.A3];5)"), "#NUM!");+ assert_eq!(eval("of:=PERCENTRANK([.A1:.A3];2)"), "0.5");+ assert_eq!(eval("of:=PERCENTRANK([.A1:.A3];1)"), "0");+ assert_eq!(eval("of:=PERCENTRANK([.A1:.A3];3)"), "1");+ assert_eq!(eval("of:=PERCENTILE([.A1:.A3];0.5)"), "2");+ assert_eq!(eval("of:=LARGE([.B1:.B3];2)"), "20");+ assert_eq!(eval("of:=SMALL([.B1:.B3];1)"), "10");+ assert_eq!(eval("of:=LARGE([.B1:.B3];9)"), "#NUM!");+ assert_eq!(eval("of:=RANK(3;[.A1:.A3])"), "1");+ assert_eq!(eval("of:=RANK(1;[.A1:.A3];1)"), "1");+ assert_eq!(eval("of:=RANK(9;[.A1:.A3])"), "#N/A");+ assert_eq!(eval("of:=AVERAGEA([.A1:.A3])"), "2");+ assert_eq!(eval("of:=MAXA([.A1:.A3])"), "3");+ assert_eq!(eval("of:=MINA([.A1:.A3])"), "1");+ assert_eq!(eval_col("of:=MODE([.A1:.A4])", &[1.0, 1.0, 2.0, 3.0]), "1");+}++#[test]+fn extended_logical_and_info() {+ assert_eq!(eval("of:=SWITCH(2;1;\"one\";2;\"two\";\"def\")"), "two");+ assert_eq!(eval("of:=SWITCH(9;1;\"one\";\"def\")"), "def");+ assert_eq!(eval("of:=SWITCH(9;1;\"one\")"), "#N/A");+ assert_eq!(eval("of:=XOR(1;0)"), "TRUE");+ assert_eq!(eval("of:=XOR(1;1)"), "FALSE");+ assert_eq!(eval("of:=IFS(1>2;\"a\";2>1;\"b\")"), "b");+ assert_eq!(eval("of:=IFS(1>2;\"a\")"), "#N/A");+ assert_eq!(eval("of:=CHOOSE(2;\"a\";\"b\";\"c\")"), "b");+ assert_eq!(eval("of:=CHOOSE(9;\"a\")"), "#VALUE!");+ assert_eq!(eval("of:=ISEVEN(4)"), "TRUE");+ assert_eq!(eval("of:=ISEVEN(3)"), "FALSE");+ assert_eq!(eval("of:=ISODD(3)"), "TRUE");+ assert_eq!(eval("of:=ISERROR(1/0)"), "TRUE");+ assert_eq!(eval("of:=ISERROR(1)"), "FALSE");+ assert_eq!(eval("of:=ISERR(1/0)"), "TRUE");+ assert_eq!(eval("of:=ISERR(NA())"), "FALSE");+ assert_eq!(eval("of:=ISNA(NA())"), "TRUE");+ assert_eq!(eval("of:=ISNUMBER(1)"), "TRUE");+ assert_eq!(eval("of:=ISTEXT(\"x\")"), "TRUE");+ assert_eq!(eval("of:=ISLOGICAL(TRUE)"), "TRUE");+ assert_eq!(eval("of:=IFNA(NA();\"fallback\")"), "fallback");+ assert_eq!(eval("of:=IFNA(5;\"fallback\")"), "5");+ assert_eq!(eval("of:=N(5)"), "5");+ assert_eq!(eval("of:=N(TRUE)"), "1");+ assert_eq!(eval("of:=N(\"x\")"), "0");+ assert_eq!(eval("of:=TYPE(5)"), "1");+ assert_eq!(eval("of:=TYPE(\"x\")"), "2");+ assert_eq!(eval("of:=TYPE(TRUE)"), "4");+ assert_eq!(eval("of:=TYPE(1/0)"), "16");+ assert_eq!(eval("of:=ERROR.TYPE(1/0)"), "2");+ assert_eq!(eval("of:=ERROR.TYPE(NA())"), "7");+ assert_eq!(eval("of:=ERROR.TYPE(5)"), "#N/A");+ assert_eq!(eval("of:=SHEETS()"), "1");+ assert_eq!(eval("of:=SHEET()"), "1");+ assert_eq!(eval("of:=ISREF([.A1])"), "TRUE");+ assert_eq!(eval("of:=ISFORMULA([.C1])"), "TRUE");+ assert_eq!(eval("of:=ISFORMULA([.A1])"), "FALSE");+}++#[test]+fn extended_text() {+ assert_eq!(eval("of:=TEXTJOIN(\"-\";TRUE;[.A1:.A3])"), "1-2-3");+ assert_eq!(eval("of:=TEXTJOIN(\",\";FALSE;\"a\";\"\";\"b\")"), "a,,b");+ assert_eq!(eval("of:=TEXTJOIN(\",\";TRUE;\"a\";\"\";\"b\")"), "a,b");+ assert_eq!(eval("of:=CHAR(65)"), "A");+ assert_eq!(eval("of:=CODE(\"A\")"), "65");+ assert_eq!(eval("of:=CODE(\"\")"), "#VALUE!");+ assert_eq!(eval("of:=T(\"x\")"), "x");+ assert_eq!(eval("of:=T(5)"), "");+ assert_eq!(eval("of:=CLEAN(CHAR(7)&\"a\")"), "a");+ assert_eq!(eval("of:=NUMBERVALUE(\"1,234\")"), "1234");+ assert_eq!(eval("of:=NUMBERVALUE(\"abc\")"), "#VALUE!");+ assert_eq!(eval("of:=UNICHAR(65)"), "A");+ assert_eq!(eval("of:=UNICODE(\"A\")"), "65");+ assert_eq!(eval("of:=UNICHAR(0)"), "#VALUE!");+ assert_eq!(eval("of:=RIGHT(\"hello\";2)"), "lo");+ assert_eq!(eval("of:=LEFT(\"hello\")"), "h");+ assert_eq!(eval("of:=REPT(\"ab\";3)"), "ababab");+ assert_eq!(eval("of:=EXACT(\"a\";\"a\")"), "TRUE");+ assert_eq!(eval("of:=EXACT(\"a\";\"A\")"), "FALSE");+ assert_eq!(eval("of:=FIND(\"l\";\"hello\")"), "3");+ assert_eq!(eval("of:=FIND(\"L\";\"hello\")"), "#VALUE!");+ assert_eq!(eval("of:=SEARCH(\"L\";\"hello\")"), "3");+ assert_eq!(eval("of:=SUBSTITUTE(\"aaa\";\"a\";\"b\")"), "bbb");+ assert_eq!(eval("of:=REPLACE(\"hello\";2;3;\"a\")"), "hao");+ assert_eq!(eval("of:=VALUE(\"2.5\")"), "2.5");+ assert_eq!(eval("of:=VALUE(\"abc\")"), "#VALUE!");+ assert_eq!(eval("of:=PROPER(\"hello world\")"), "Hello World");+ assert_eq!(eval("of:=PROPER(\"hELLO wORLD\")"), "Hello World");+ assert_eq!(eval("of:=TRIM(\" a b \")"), "a b");+ assert_eq!(eval("of:=FIXED(1234.567;2)"), "1,234.57");+ assert_eq!(eval("of:=FIXED(1234.567;2;TRUE)"), "1234.57");+ assert_eq!(eval("of:=DOLLAR(1234.5)"), "$1,234.50");+ assert_eq!(eval("of:=TEXTBEFORE(\"a,b,c\";\",\")"), "a");+ assert_eq!(eval("of:=TEXTAFTER(\"a,b,c\";\",\")"), "b,c");+ assert_eq!(eval("of:=TEXTAFTER(\"a,b,c\";\",\";2)"), "c");+ assert_eq!(eval("of:=TEXTBEFORE(\"abc\";\",\")"), "#N/A");+ assert_eq!(eval("of:=TEXT(0.5;\"0%\")"), "50%");+ assert_eq!(eval("of:=TEXT(1234.5;\"#,##0.00\")"), "1,234.50");+ assert_eq!(eval("of:=CONCAT(\"a\";[.A1:.A3])"), "a123");+}++#[test]+fn extended_dates() {+ // 2020-01-01 = 43831 (a Wednesday); 2020 is a leap year.+ assert_eq!(eval("of:=DATE(2020;1;1)"), "43831");+ assert_eq!(eval("of:=EDATE(DATE(2020;1;31);1)"), "43890"); // 2020-02-29+ assert_eq!(eval("of:=EDATE(DATE(2020;1;15);-1)"), "43814"); // 2019-12-15+ assert_eq!(eval("of:=EOMONTH(DATE(2020;1;15);0)"), "43861"); // 2020-01-31+ assert_eq!(eval("of:=EOMONTH(DATE(2020;2;3);0)"), "43890"); // 2020-02-29+ assert_eq!(eval("of:=DAYS(DATE(2020;1;10);DATE(2020;1;1))"), "9");+ assert_eq!(eval("of:=TIME(12;0;0)"), "0.5");+ assert_eq!(eval("of:=HOUR(0.5)"), "12");+ assert_eq!(eval("of:=MINUTE(0.5)"), "0");+ assert_eq!(eval("of:=SECOND(0.5)"), "0");+ assert_eq!(eval("of:=WEEKNUM(DATE(2020;1;1))"), "1");+ assert_eq!(eval("of:=WEEKNUM(DATE(2020;1;5))"), "2");+ assert_eq!(eval("of:=ISOWEEKNUM(DATE(2020;1;1))"), "1");+ assert_eq!(eval("of:=ISOWEEKNUM(DATE(2020;12;31))"), "53");+ assert_eq!(eval("of:=NETWORKDAYS(DATE(2020;1;1);DATE(2020;1;1))"), "1");+ assert_eq!(eval("of:=NETWORKDAYS(DATE(2020;1;4);DATE(2020;1;4))"), "0"); // Sat+ assert_eq!(eval("of:=NETWORKDAYS(DATE(2020;1;1);DATE(2020;1;7))"), "5");+ assert_eq!(eval("of:=WORKDAY(DATE(2020;1;3);1)"), "43836"); // Fri -> Mon+ assert_eq!(eval("of:=WORKDAY(DATE(2020;1;6);-1)"), "43833"); // Mon -> Fri+ assert_eq!(eval("of:=DATEDIF(DATE(2020;1;1);DATE(2020;1;10);\"D\")"), "9");+ assert_eq!(eval("of:=DATEDIF(DATE(2020;1;15);DATE(2021;3;20);\"M\")"), "14");+ assert_eq!(eval("of:=DATEDIF(DATE(2020;1;15);DATE(2021;3;20);\"Y\")"), "1");+ assert_eq!(eval("of:=DATEDIF(DATE(2020;1;31);DATE(2020;2;1);\"M\")"), "0");+ assert_eq!(eval("of:=DATEDIF(DATE(2019;6;15);DATE(2020;6;14);\"Y\")"), "0");+ assert_eq!(eval("of:=DATEDIF(DATE(2020;1;15);DATE(2020;3;10);\"MD\")"), "24");+ assert_eq!(eval("of:=DATEDIF(DATE(2020;1;15);DATE(2021;3;10);\"YM\")"), "1");+ assert_eq!(eval("of:=DAYS360(DATE(2020;1;1);DATE(2020;2;1))"), "30");+ assert_eq!(eval("of:=YEARFRAC(DATE(2020;1;1);DATE(2021;1;1);3)"), "1.0027");+ assert_eq!(eval("of:=YEARFRAC(DATE(2020;1;1);DATE(2020;7;1);2)"), "0.5056");+ // now_millis = 2023-11-14T22:13:20Z -> TODAY serial 45244.+ assert_eq!(eval("of:=TODAY()"), "45244");+ assert_eq!(eval("of:=NOW()"), "45244.9259");+ assert_eq!(eval("of:=DATEVALUE(43831)"), "43831");+}++#[test]+fn financial_functions() {+ assert_eq!(eval("of:=PMT(0;10;1000)"), "-100");+ assert_eq!(eval("of:=PMT(0.1;1;1000)"), "-1100");+ assert_eq!(eval("of:=FV(0;10;-100;0)"), "1000");+ assert_eq!(eval("of:=FV(0.1;1;-100)"), "100");+ assert_eq!(eval("of:=PV(0.1;1;-100)"), "90.9091");+ assert_eq!(eval("of:=PV(0;1;-100)"), "100");+ assert_eq!(eval("of:=NPV(0.1;100)"), "90.9091");+ assert_eq!(eval("of:=NPV(0;100;100)"), "200");+ assert_eq!(eval("of:=NPER(0;-100;1000)"), "10");+ assert_eq!(eval("of:=RATE(1;-1100;1000)"), "0.1");+ assert_eq!(eval("of:=IPMT(0.1;1;1;1000)"), "-100");+ assert_eq!(eval("of:=PPMT(0.1;1;1;1000)"), "-1000");+ assert_eq!(eval("of:=CUMIPMT(0.1;1;1000;1;1;0)"), "-100");+ assert_eq!(eval("of:=CUMPRINC(0.1;1;1000;1;1;0)"), "-1000");+ assert_eq!(eval("of:=SLN(1000;100;5)"), "180");+ assert_eq!(eval("of:=SYD(1000;0;5;1)"), "333.3333");+ assert_eq!(eval_col("of:=IRR([.A1:.A2])", &[-100.0, 110.0]), "0.1");+}++#[test]+fn radix_roman_bitwise() {+ assert_eq!(eval("of:=BASE(255;16)"), "FF");+ assert_eq!(eval("of:=BASE(5;2;4)"), "0101");+ assert_eq!(eval("of:=BASE(5;1)"), "#NUM!");+ assert_eq!(eval("of:=DECIMAL(\"FF\";16)"), "255");+ assert_eq!(eval("of:=DECIMAL(\"102\";2)"), "#NUM!");+ assert_eq!(eval("of:=ARABIC(\"XIV\")"), "14");+ assert_eq!(eval("of:=ARABIC(\"MCMXCIV\")"), "1994");+ assert_eq!(eval("of:=ROMAN(14)"), "XIV");+ assert_eq!(eval("of:=ROMAN(1994)"), "MCMXCIV");+ assert_eq!(eval("of:=ROMAN(0)"), "0");+ assert_eq!(eval("of:=BITAND(12;10)"), "8");+ assert_eq!(eval("of:=BITOR(12;10)"), "14");+ assert_eq!(eval("of:=BITXOR(12;10)"), "6");+ assert_eq!(eval("of:=BITLSHIFT(1;3)"), "8");+ assert_eq!(eval("of:=BITLSHIFT(8;-1)"), "4");+ assert_eq!(eval("of:=BITRSHIFT(16;2)"), "4");+ assert_eq!(eval("of:=BITRSHIFT(4;-1)"), "8");+}++#[test]+fn lookup_dimensions() {+ assert_eq!(eval("of:=ROWS([.A1:.B3])"), "3");+ assert_eq!(eval("of:=COLUMNS([.A1:.B3])"), "2");+ assert_eq!(eval("of:=ROW([.B2])"), "2");+ assert_eq!(eval("of:=COLUMN([.B2])"), "2");+ assert_eq!(eval("of:=ROW()"), "1");+ assert_eq!(eval("of:=COLUMN()"), "3");+ assert_eq!(eval("of:=HLOOKUP(10;[.A1:.B3];3;0)"), "30");+ assert_eq!(eval("of:=LOOKUP(2;[.A1:.A3];[.B1:.B3])"), "20");+ assert_eq!(eval("of:=LOOKUP(2.5;[.A1:.A3];[.B1:.B3])"), "20");+ assert_eq!(eval("of:=LOOKUP(0;[.A1:.A3];[.B1:.B3])"), "#N/A");+ assert_eq!(eval("of:=ADDRESS(2;2)"), "$B$2");+ assert_eq!(eval("of:=ADDRESS(2;2;4)"), "B2");+ assert_eq!(eval("of:=ADDRESS(1;1;2)"), "A$1");+ assert_eq!(eval("of:=ADDRESS(1;1;3)"), "$A1");+ assert_eq!(eval("of:=INDIRECT(\"A2\")"), "2");+ assert_eq!(eval("of:=INDIRECT(\"ZZ9\")"), "");+ assert_eq!(eval("of:=HYPERLINK(\"http://x\";\"label\")"), "label");+ assert_eq!(eval("of:=HYPERLINK(\"http://x\")"), "http://x");+ assert_eq!(eval("of:=MATCH(20;[.B1:.B3];0)"), "2");+ assert_eq!(eval("of:=MATCH(25;[.B1:.B3];1)"), "2");+ assert_eq!(eval("of:=INDEX([.A1:.B3];2;2)"), "20");+ assert_eq!(eval("of:=COUNTBLANK([.A1:.B3])"), "0");+ assert_eq!(eval("of:=COUNTA([.A1:.B3])"), "6");+ // RAND is seeded from now_millis: reproducible across workbooks.+ assert_eq!(eval("of:=RAND()"), eval("of:=RAND()"));+ let r: f64 = eval("of:=RANDBETWEEN(1;10)").parse().unwrap();+ assert!((1.0..=10.0).contains(&r));+}