use std::collections::BTreeMap;
use std::panic::{catch_unwind, AssertUnwindSafe};
use super::*;
type OutOfRangeCase<'a> = (
&'static str,
Box<dyn FnOnce() -> std::result::Result<Temporal, Error> + 'a>,
);
#[test]
fn temporal_arith_out_of_range_errors_not_panics() {
let date = Temporal::Date(NaiveDate::from_ymd_opt(2024, 1, 1).unwrap());
let civil_far = Temporal::CivilDate {
year: 9_000_000_000_000_000_000,
month: 1,
day: 1,
};
let civil_near = Temporal::CivilDate {
year: -9_000_000_000_000_000_000,
month: 1,
day: 1,
};
let cases: Vec<OutOfRangeCase<'_>> = vec![
(
"add_months huge positive",
Box::new(|| {
add_duration(
&date,
&Temporal::Duration {
months: 1_000_000_000_000,
days: 0,
nanos: 0,
},
)
}),
),
(
"add_months huge negative",
Box::new(|| {
add_duration(
&date,
&Temporal::Duration {
months: -1_000_000_000_000,
days: 0,
nanos: 0,
},
)
}),
),
(
"sub_duration negation overflow",
Box::new(|| {
sub_duration(
&date,
&Temporal::Duration {
months: i64::MIN,
days: 0,
nanos: 0,
},
)
}),
),
(
"add_duration huge days on date",
Box::new(|| {
add_duration(
&date,
&Temporal::Duration {
months: 0,
days: i64::MAX / 10,
nanos: 0,
},
)
}),
),
(
"add_duration huge days on localdatetime",
Box::new(|| {
add_duration(
&Temporal::LocalDateTime(
NaiveDate::from_ymd_opt(2024, 1, 1)
.unwrap()
.and_time(NaiveTime::MIN),
),
&Temporal::Duration {
months: 0,
days: i64::MAX / 10,
nanos: 0,
},
)
}),
),
(
"add_duration huge days on datetime",
Box::new(|| {
add_duration(
&Temporal::DateTime {
utc: NaiveDate::from_ymd_opt(2024, 1, 1)
.unwrap()
.and_time(NaiveTime::MIN),
offset: 0,
zone: Some("Z".into()),
},
&Temporal::Duration {
months: 0,
days: i64::MAX / 10,
nanos: 0,
},
)
}),
),
(
"add_duration nanos beyond i64",
Box::new(|| {
add_duration(
&Temporal::LocalDateTime(
NaiveDate::from_ymd_opt(2024, 1, 1)
.unwrap()
.and_time(NaiveTime::MIN),
),
&Temporal::Duration {
months: 0,
days: 0,
nanos: i128::from(i64::MAX) + 1,
},
)
}),
),
(
"duration + duration months overflow",
Box::new(|| {
add_duration(
&Temporal::Duration {
months: i64::MAX,
days: 0,
nanos: 0,
},
&Temporal::Duration {
months: 1,
days: 0,
nanos: 0,
},
)
}),
),
(
"duration + duration nanos overflow",
Box::new(|| {
add_duration(
&Temporal::Duration {
months: 0,
days: 0,
nanos: i128::MAX,
},
&Temporal::Duration {
months: 0,
days: 0,
nanos: 1,
},
)
}),
),
(
"duration.between civil huge years",
Box::new(|| duration_between(&civil_near, &civil_far)),
),
(
"duration.between civil huge years reversed",
Box::new(|| duration_between(&civil_far, &civil_near)),
),
(
"inmonths civil huge years",
Box::new(|| duration_between_unit(&civil_near, &civil_far, BetweenUnit::Months)),
),
(
"indays civil huge years",
Box::new(|| duration_between_unit(&civil_near, &civil_far, BetweenUnit::Days)),
),
(
"inseconds civil huge years",
Box::new(|| duration_between_unit(&civil_near, &civil_far, BetweenUnit::Seconds)),
),
(
"iso duration huge years",
Box::new(|| parse_iso("duration", "P1000000000000000000000000Y")),
),
(
"iso duration huge repeated days",
Box::new(|| parse_iso("duration", "P9000000000000000000D9000000000000000000D")),
),
(
"iso duration datetime-style huge year",
Box::new(|| parse_iso("duration", "P9000000000000000000-01-01")),
),
(
"iso duration huge hours+minutes",
Box::new(|| {
parse_iso(
"duration",
"PT90000000000000000000000000H90000000000000000000000000M",
)
}),
),
(
"iso duration negation of min",
Box::new(|| parse_iso("duration", "-P9000000000000000000Y")),
),
(
"time offset hour overflow",
Box::new(|| parse_iso("time", "12:00+2147483647:00")),
),
(
"duration map huge years",
Box::new(|| {
construct_duration(Some(&Value::Map(
[("years".into(), Value::Float(1e300))]
.into_iter()
.collect::<BTreeMap<_, _>>()
.into(),
)))
}),
),
(
"time map millisecond u32 overflow",
Box::new(|| {
construct_time(Some(&Value::Map(
[
("hour".into(), Value::Int(1)),
("millisecond".into(), Value::Int(5_000_000_000)),
]
.into_iter()
.collect::<BTreeMap<_, _>>()
.into(),
)))
}),
),
(
"date map quarter u32 overflow",
Box::new(|| {
construct_date(Some(&Value::Map(
[
("year".into(), Value::Int(2024)),
("quarter".into(), Value::Int(2_000_000_000)),
]
.into_iter()
.collect::<BTreeMap<_, _>>()
.into(),
)))
}),
),
(
"truncate dayOfWeek huge",
Box::new(|| {
truncate(
ClockKind::Date,
"week",
&date,
Some(&Value::Map(
[("dayOfWeek".into(), Value::Int(9_000_000_000_000_000_000))]
.into_iter()
.collect::<BTreeMap<_, _>>()
.into(),
)),
)
}),
),
(
"truncate week at chrono min (Thu)",
Box::new(|| {
truncate(
ClockKind::Date,
"week",
&Temporal::Date(NaiveDate::MIN),
None,
)
}),
),
(
"truncate week at chrono min (Fri)",
Box::new(|| {
truncate(
ClockKind::Date,
"week",
&Temporal::Date(NaiveDate::from_ymd_opt(-262143, 1, 2).unwrap()),
None,
)
}),
),
(
"truncate week at chrono min (Sat)",
Box::new(|| {
truncate(
ClockKind::Date,
"week",
&Temporal::Date(NaiveDate::from_ymd_opt(-262143, 1, 3).unwrap()),
None,
)
}),
),
(
"truncate week at chrono min (Sun)",
Box::new(|| {
truncate(
ClockKind::Date,
"week",
&Temporal::Date(NaiveDate::from_ymd_opt(-262143, 1, 4).unwrap()),
None,
)
}),
),
(
"truncate week at chrono min localdatetime",
Box::new(|| {
truncate(
ClockKind::LocalDateTime,
"week",
&Temporal::LocalDateTime(NaiveDateTime::MIN),
None,
)
}),
),
(
"truncate week at chrono min datetime",
Box::new(|| {
truncate(
ClockKind::DateTime,
"week",
&Temporal::DateTime {
utc: NaiveDateTime::MIN,
offset: 0,
zone: Some("Z".into()),
},
None,
)
}),
),
(
"truncate weekyear at chrono min",
Box::new(|| {
truncate(
ClockKind::Date,
"weekyear",
&Temporal::Date(NaiveDate::MIN),
None,
)
}),
),
(
"week date map i32-min year",
Box::new(|| {
construct_date(Some(&Value::Map(
[
("year".into(), Value::Int(2_147_483_648)),
("week".into(), Value::Int(1)),
]
.into_iter()
.collect::<BTreeMap<_, _>>()
.into(),
)))
}),
),
(
"week date map i32-max year",
Box::new(|| {
construct_date(Some(&Value::Map(
[
("year".into(), Value::Int(2_147_483_647)),
("week".into(), Value::Int(52)),
("dayOfWeek".into(), Value::Int(6)),
]
.into_iter()
.collect::<BTreeMap<_, _>>()
.into(),
)))
}),
),
(
"week date string huge year",
Box::new(|| parse_iso("date", "2147483647W526")),
),
(
"week date string negative huge year",
Box::new(|| parse_iso("date", "-2147483648W011")),
),
(
"week date pattern huge year",
Box::new(|| parse_pattern("datetime", "2147483647-W52-6", "YYYY-'W'ww-e")),
),
(
"date + duration with beyond-i64 day-equivalent nanos",
Box::new(|| {
add_duration(
&date,
&Temporal::Duration {
months: 0,
days: 0,
nanos: i128::from(i64::MAX) * 86_400_000_000_000 * 2,
},
)
}),
),
(
"duration.between with i32::MIN time offset",
Box::new(|| {
let t = Temporal::Time {
time: NaiveTime::from_hms_opt(12, 0, 0).unwrap(),
offset: i32::MIN,
};
let dt = parse_iso("datetime", "2015-01-01T00:00[Europe/Stockholm]").unwrap();
duration_between(&t, &dt)
}),
),
(
"duration.between with i32::MIN time offset reversed",
Box::new(|| {
let t = Temporal::Time {
time: NaiveTime::from_hms_opt(12, 0, 0).unwrap(),
offset: i32::MIN,
};
let dt = parse_iso("datetime", "2015-01-01T00:00[Europe/Stockholm]").unwrap();
duration_between(&dt, &t)
}),
),
(
"truncate day on civil year wrapping into range",
Box::new(|| {
truncate(
ClockKind::Date,
"day",
&Temporal::CivilDate {
year: 4_294_967_297,
month: 1,
day: 1,
},
None,
)
}),
),
(
"date map year wraps into range",
Box::new(|| {
construct_date(Some(&Value::Map(
[("year".into(), Value::Int(4_294_967_297))]
.into_iter()
.collect::<BTreeMap<_, _>>()
.into(),
)))
}),
),
(
"date map month wraps into range",
Box::new(|| {
construct_date(Some(&Value::Map(
[
("year".into(), Value::Int(2024)),
("month".into(), Value::Int(4_294_967_297)),
]
.into_iter()
.collect::<BTreeMap<_, _>>()
.into(),
)))
}),
),
(
"time map hour wraps into range",
Box::new(|| {
construct_time(Some(&Value::Map(
[("hour".into(), Value::Int(4_294_967_297))]
.into_iter()
.collect::<BTreeMap<_, _>>()
.into(),
)))
}),
),
(
"truncate extras day wraps into range",
Box::new(|| {
truncate(
ClockKind::Date,
"day",
&date,
Some(&Value::Map(
[("day".into(), Value::Int(4_294_967_297))]
.into_iter()
.collect::<BTreeMap<_, _>>()
.into(),
)),
)
}),
),
(
"datetime map offset underflow at chrono min",
Box::new(|| {
construct_datetime(Some(&Value::Map(
[
("year".into(), Value::Int(-262143)),
("month".into(), Value::Int(1)),
("day".into(), Value::Int(1)),
("timezone".into(), Value::String("+99:99".into())),
]
.into_iter()
.collect::<BTreeMap<_, _>>()
.into(),
)))
}),
),
(
"pattern millis i64::MIN",
Box::new(|| {
parse_pattern(
"datetime",
"2015 Jan 1, -9223372036854775808 0 0",
"yyyy LLL d, A m s",
)
}),
),
(
"scale duration huge factor",
Box::new(|| {
scale_duration(
&Temporal::Duration {
months: 5,
days: 0,
nanos: 0,
},
1e300,
)
}),
),
];
for (name, f) in cases {
let outcome = catch_unwind(std::panic::AssertUnwindSafe(f))
.unwrap_or_else(|_| panic!("{name} panicked"));
assert!(outcome.is_err(), "{name} must fail, got {outcome:?}");
}
}
#[test]
fn duration_component_out_of_range_errors_not_garbage() {
let huge = Temporal::Duration {
months: 0,
days: 0,
nanos: 10_000_000_000_000_000_000_000_000_000_000_000_000i128,
};
let huge_neg = Temporal::Duration {
months: 0,
days: 0,
nanos: -10_000_000_000_000_000_000_000_000_000_000_000_000i128,
};
for k in [
"hours",
"minutes",
"seconds",
"nanoseconds",
"milliseconds",
"microseconds",
] {
for d in [&huge, &huge_neg] {
let outcome = catch_unwind(AssertUnwindSafe(|| d.component(k)))
.unwrap_or_else(|_| panic!("component({k}) panicked"));
assert!(
outcome.is_err(),
"component({k}) on {d:?} must fail, got {outcome:?}"
);
}
}
assert!(huge.component("minutesOfHour").is_ok());
assert!(huge.component("secondsOfMinute").is_ok());
assert!(huge.component("nanosecondsOfSecond").is_ok());
assert!(huge.component("millisecondsOfSecond").is_ok());
assert!(huge.component("microsecondsOfSecond").is_ok());
assert_eq!(huge.component("months"), Ok(Value::Int(0)));
assert!(huge.component("nope").is_err());
}
#[test]
fn week_truncation_at_the_lower_bound() {
let mon = Temporal::Date(NaiveDate::from_ymd_opt(-262143, 1, 5).unwrap());
assert_eq!(truncate(ClockKind::Date, "week", &mon, None).unwrap(), mon);
let tue = Temporal::Date(NaiveDate::from_ymd_opt(-262143, 1, 6).unwrap());
assert_eq!(truncate(ClockKind::Date, "week", &tue, None).unwrap(), mon);
}
#[test]
fn truncation_at_the_chrono_bounds_still_works() {
let min = Temporal::Date(NaiveDate::MIN);
for unit in [
"millennium",
"century",
"decade",
"year",
"quarter",
"month",
"day",
] {
assert!(
truncate(ClockKind::Date, unit, &min, None).is_ok(),
"truncate({unit}) at NaiveDate::MIN must succeed"
);
}
let max = Temporal::Date(NaiveDate::MAX);
for unit in [
"millennium",
"century",
"decade",
"year",
"weekyear",
"quarter",
"month",
"week",
"day",
] {
assert!(
truncate(ClockKind::Date, unit, &max, None).is_ok(),
"truncate({unit}) at NaiveDate::MAX must succeed"
);
}
}
#[test]
fn duration_in_seconds_spans_the_full_chrono_range() {
let span = duration_between_unit(
&Temporal::LocalDateTime(NaiveDateTime::MIN),
&Temporal::LocalDateTime(NaiveDateTime::MAX),
BetweenUnit::Seconds,
)
.unwrap();
let days = (NaiveDate::MAX - NaiveDate::MIN).num_days();
let expected = i128::from(days) * 86_400_000_000_000 + 86_399_999_999_999;
assert_eq!(
span,
Temporal::Duration {
months: 0,
days: 0,
nanos: expected,
}
);
assert_eq!(span.iso_string(), "PT4595796695H59M59.999999999S");
}
#[test]
fn day_of_week_outside_one_to_seven_is_a_typed_rejection() {
let week_map = |dow: i64| {
construct_date(Some(&Value::Map(
[
("year".into(), Value::Int(2024)),
("week".into(), Value::Int(1)),
("dayOfWeek".into(), Value::Int(dow)),
]
.into_iter()
.collect::<BTreeMap<_, _>>()
.into(),
)))
};
for rejected in [i64::MAX, i64::MIN, 0, -1, 8] {
assert!(week_map(rejected).is_err(), "dayOfWeek {rejected}");
}
assert_eq!(week_map(1).unwrap().iso_string(), "2024-01-01");
assert_eq!(week_map(7).unwrap().iso_string(), "2024-01-07");
}
#[test]
fn temporal_arith_in_range_still_works() {
let date = Temporal::Date(NaiveDate::from_ymd_opt(2024, 1, 1).unwrap());
let plus_one_month = add_duration(
&date,
&Temporal::Duration {
months: 1,
days: 0,
nanos: 0,
},
)
.unwrap();
assert_eq!(plus_one_month.iso_string(), "2024-02-01");
let near = Temporal::CivilDate {
year: -999_999_999,
month: 1,
day: 1,
};
let far = Temporal::CivilDate {
year: 999_999_999,
month: 1,
day: 1,
};
let d = duration_between(&far, &near).unwrap();
assert_eq!(
d,
Temporal::Duration {
months: -23_999_999_976,
days: 0,
nanos: 0,
}
);
}