Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Firefox/third_party/rust/icu_calendar/tests/   (Firefox Browser Version 153.0.1©)  Datei vom 27.6.2026 mit Größe 9 kB image not shown  

Quelle  arithmetic.rs

  Sprache: Rust
 

// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use std::convert::Infallible;

use icu_calendar::{
    cal::Hebrew,
    options::{DateAddOptions, DateDifferenceOptions, Overflow},
    types::{DateDuration, DateDurationUnit, MonthCode},
    AsCalendar, Calendar, Date, Iso,
};

#[rustfmt::skip]
#[allow(clippy::type_complexity)]
const ISO_DATE_PAIRS: &[(&str, &str, u64, (u32, u64), (u32, u64), (u32, u32, u64))] = &[
    //         d0,           d1, D,    (W, D),   (M, D),   (Y, M, D)
    ("2020-01-03""2020-02-15"43,   (61),   (112),  (0112)),
    ("2020-01-31""2020-06-30"151,  (214),  (430),  (0430)),
    ("2020-03-31""2020-07-30"121,  (172),  (330),  (0330)),
    ("2020-03-31""2020-07-31"122,  (173),  (40),   (040)),
    ("2016-03-20""2020-03-05"1446, (2064), (4714), (31114)),
    ("2020-02-29""2022-03-01"731,  (1043), (241),  (201)),

    // Negative direction:
    ("2020-02-15""2020-01-03"43,   (61),   (112),  (0112)),
    ("2020-06-30""2020-01-31"151,  (214),  (429),  (0429)), // DIFF +/-
    ("2020-07-30""2020-03-31"121,  (172),  (330),  (0330)),
    ("2020-07-31""2020-03-31"122,  (173),  (40),   (040)),
    ("2020-03-05""2016-03-20"1446, (2064), (4716), (31116)), // DIFF +/-
    ("2022-03-01""2020-02-29"731,  (1043), (241),  (201)),
];

fn check<A>(
    d0: &Date<A>,
    d1: &Date<A>,
    exp0: &u64,
    exp1: &(u32, u64),
    exp2: &(u32, u64),
    exp3: &(u32, u32, u64),
where
    A: AsCalendar + Copy,
    <A as AsCalendar>::Calendar: Calendar<DifferenceError = Infallible>,
    <<A as AsCalendar>::Calendar as Calendar>::DateInner: PartialOrd,
{
    let is_negative = d0 > d1;
    let mut add_options = DateAddOptions::default();
    add_options.overflow = Some(Overflow::Constrain);
    let mut until_options0 = DateDifferenceOptions::default();
    until_options0.largest_unit = Some(DateDurationUnit::Days);
    let mut until_options1 = DateDifferenceOptions::default();
    until_options1.largest_unit = Some(DateDurationUnit::Weeks);
    let mut until_options2 = DateDifferenceOptions::default();
    until_options2.largest_unit = Some(DateDurationUnit::Months);
    let mut until_options3 = DateDifferenceOptions::default();
    until_options3.largest_unit = Some(DateDurationUnit::Years);

    let Ok(p0) = d0.try_until_with_options(d1, until_options0);
    assert_eq!(
        p0,
        DateDuration {
            is_negative,
            days: *exp0,
            ..Default::default()
        },
        "{d0:?}/{d1:?}"
    );
    assert_eq!(
        d0.try_added_with_options(p0, add_options).unwrap(),
        *d1,
        "{d0:?}/{d1:?}"
    );

    let Ok(p1) = d0.try_until_with_options(d1, until_options1);
    assert_eq!(
        p1,
        DateDuration {
            is_negative,
            weeks: exp1.0,
            days: exp1.1,
            ..Default::default()
        },
        "{d0:?}/{d1:?}"
    );
    assert_eq!(
        d0.try_added_with_options(p1, add_options).unwrap(),
        *d1,
        "{d0:?}/{d1:?}"
    );

    let Ok(p2) = d0.try_until_with_options(d1, until_options2);
    assert_eq!(
        p2,
        DateDuration {
            is_negative,
            months: exp2.0,
            days: exp2.1,
            ..Default::default()
        },
        "{d0:?}/{d1:?}"
    );
    assert_eq!(
        d0.try_added_with_options(p2, add_options).unwrap(),
        *d1,
        "{d0:?}/{d1:?}"
    );

    let Ok(p3) = d0.try_until_with_options(d1, until_options3);
    assert_eq!(
        p3,
        DateDuration {
            is_negative,
            years: exp3.0,
            months: exp3.1,
            days: exp3.2,
            ..Default::default()
        },
        "{d0:?}/{d1:?}"
    );
    assert_eq!(
        d0.try_added_with_options(p3, add_options).unwrap(),
        *d1,
        "{d0:?}/{d1:?}"
    );

    // RataDie addition should be equivalent for largest unit Days and Weeks
    let rd_diff = d1.to_rata_die() - d0.to_rata_die();
    if is_negative {
        assert!(rd_diff.is_negative());
    }
    assert_eq!(p0.days, rd_diff.unsigned_abs());
    assert_eq!(p1.days + u64::from(p1.weeks) * 7, rd_diff.unsigned_abs());
}

#[test]
fn test_arithmetic_cases() {
    for (d0, d1, exp0, exp1, exp2, exp3) in ISO_DATE_PAIRS {
        let d0 = Date::try_from_str(d0, Iso).unwrap();
        let d1 = Date::try_from_str(d1, Iso).unwrap();
        check(&d0, &d1, exp0, exp1, exp2, exp3);
    }
}

#[test]
fn test_hebrew() {
    let m06z_20 =
        Date::try_new_from_codes(None, 5783, MonthCode::new_normal(6).unwrap(), 20, Hebrew)
            .unwrap();
    let m05l_15 =
        Date::try_new_from_codes(None, 5784, MonthCode::new_leap(5).unwrap(), 15, Hebrew).unwrap();
    let m05l_30 =
        Date::try_new_from_codes(None, 5784, MonthCode::new_leap(5).unwrap(), 30, Hebrew).unwrap();
    let m06a_29 =
        Date::try_new_from_codes(None, 5784, MonthCode::new_normal(6).unwrap(), 29, Hebrew)
            .unwrap();
    let m07a_10 =
        Date::try_new_from_codes(None, 5784, MonthCode::new_normal(7).unwrap(), 10, Hebrew)
            .unwrap();
    let m06b_15 =
        Date::try_new_from_codes(None, 5785, MonthCode::new_normal(6).unwrap(), 15, Hebrew)
            .unwrap();
    let m07b_20 =
        Date::try_new_from_codes(None, 5785, MonthCode::new_normal(7).unwrap(), 20, Hebrew)
            .unwrap();

    #[rustfmt::skip]
    #[allow(clippy::type_complexity)]
    let cases: &[(&Date<Hebrew>, &Date<Hebrew>, u64, (u32, u64), (u32, u64), (u32, u32, u64))] = &[
        (&m06z_20, &m05l_15, 348, (495), (1125), (01125)),
        (&m06z_20, &m05l_30, 363, (516), (1210), (01210)),
        (&m06z_20, &m06a_29, 392, (560), (139),  (109)),
        (&m06z_20, &m07a_10, 402, (573), (1319), (1019)),
        (&m06z_20, &m06b_15, 733, (104,5), (2425), (11125)),
        (&m06z_20, &m07b_20, 767, (109,4), (260),  (210)),

        (&m05l_15, &m05l_30, 15,  (21),  (015),  (0015)),
        (&m05l_15, &m06a_29, 44,  (62),  (114),  (0114)),
        (&m05l_15, &m07a_10, 54,  (75),  (124),  (0124)),
        (&m05l_15, &m06b_15, 385, (550), (130),  (100)), // M05L to M06 common year
        (&m05l_15, &m07b_20, 419, (596), (145),  (115)), // M05L to M07 common year

        (&m05l_30, &m06a_29, 29,  (41),  (029),  (0029)),
        (&m05l_30, &m07a_10, 39,  (54),  (110),  (0110)),
        (&m05l_30, &m06b_15, 370, (526), (1215), (01215)), // M05L to M06 common year
        (&m05l_30, &m07b_20, 404, (575), (1320), (1020)), // M05L to M07 common year

        (&m06a_29, &m07a_10, 10,  (13),  (010),  (0010)),
        (&m06a_29, &m06b_15, 341, (485), (1116), (01116)), // M06 leap year to M06 common year
        (&m06a_29, &m07b_20, 375, (534), (1220), (1020)), // M06 leap year to M07 common year
        (&m07a_10, &m06b_15, 331, (472), (115),  (0115)), // M07 leap year to M06 common year
        (&m07a_10, &m07b_20, 365, (521), (1210), (1010)), // M07 leap year to M06 common year
        (&m06b_15, &m07b_20, 34,  (46), (15),   (015)),
    ];

    for (d0, d1, exp0, exp1, exp2, exp3) in cases {
        check(d0, d1, exp0, exp1, exp2, exp3);
    }
}

#[test]
fn test_tricky_leap_months() {
    let mut add_options = DateAddOptions::default();
    add_options.overflow = Some(Overflow::Constrain);
    let mut until_options = DateDifferenceOptions::default();
    until_options.largest_unit = Some(DateDurationUnit::Years);

    fn hebrew_date(year: i32, month: &str, day: u8) -> Date<Hebrew> {
        Date::try_new_from_codes(None, year, MonthCode(month.parse().unwrap()), day, Hebrew)
            .unwrap()
    }

    // M06 + 1yr = M06 (common to leap)
    let date0 = hebrew_date(5783"M06"20);
    let duration0 = DateDuration::for_years(1);
    let date1 = date0
        .try_added_with_options(duration0, add_options)
        .unwrap();
    assert_eq!(date1, hebrew_date(5784"M06"20));
    let duration0_actual = date0.try_until_with_options(&date1, until_options).unwrap();
    assert_eq!(duration0_actual, duration0);

    // M06 - 1mo = M05L (leap to leap)
    let duration1 = DateDuration::for_months(-1);
    let date2 = date1
        .try_added_with_options(duration1, add_options)
        .unwrap();
    assert_eq!(date2, hebrew_date(5784"M05L"20));
    let duration1_actual = date1.try_until_with_options(&date2, until_options).unwrap();
    assert_eq!(duration1_actual, duration1);

    // M05L + 1yr1mo = M07 (leap to common)
    let duration2 = DateDuration {
        years: 1,
        months: 1,
        ..Default::default()
    };
    let date3 = date2
        .try_added_with_options(duration2, add_options)
        .unwrap();
    assert_eq!(date3, hebrew_date(5785"M07"20));
    let duration2_actual = date2.try_until_with_options(&date3, until_options).unwrap();
    assert_eq!(duration2_actual, duration2);

    // M06 + 1yr1mo = M07 (leap to common)
    let date4 = date1
        .try_added_with_options(duration2, add_options)
        .unwrap();
    assert_eq!(date4, hebrew_date(5785"M07"20));
    let duration2_actual = date1.try_until_with_options(&date4, until_options).unwrap();
    assert_eq!(duration2_actual, duration2);
}

Messung V0.5 in Prozent
C=95 H=98 G=96

¤ Dauer der Verarbeitung: 0.14 Sekunden  (vorverarbeitet am  2026-08-25) ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

PVS Prover

Isabelle Prover

NIST Cobol Testsuite

Cephes Mathematical Library

Vienna Development Method

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.