| 1 |
// This file is part of ICU4X. For terms of use, please see the file |
| 2 |
// called LICENSE at the top level of the ICU4X source tree |
| 3 |
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
| 4 |
|
| 5 |
use crate::options::{DateAddOptions, DateDifferenceOptions, DateDurationUnit, Overflow}; |
| 6 |
use crate::types::{DateDuration, Month}; |
| 7 |
use crate::{AsCalendar, Calendar, Date}; |
| 8 |
use core::fmt; |
| 9 |
|
| 10 |
struct ArithmeticDateForLogging { |
| 11 |
pub year: i32, |
| 12 |
pub month: Month, |
| 13 |
pub day: u8, |
| 14 |
} |
| 15 |
|
| 16 |
impl<C> From<Date<C>> for ArithmeticDateForLogging |
| 17 |
where |
| 18 |
C: Calendar, |
| 19 |
{ |
| 20 |
fn from(value: Date<C>) -> Self { |
| 21 |
Self { |
| 22 |
year: value.year().extended_year(), |
| 23 |
month: value.month().to_input(), |
| 24 |
day: value.day_of_month().0, |
| 25 |
} |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
impl fmt::Display for ArithmeticDateForLogging { |
| 30 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 31 |
write!(f, "{:4}.{}.{:<2}", self.year, self.month.code().0, self.day) |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
struct DateDurationForLogging(DateDuration); |
| 36 |
|
| 37 |
impl fmt::Display for DateDurationForLogging { |
| 38 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 39 |
write!( |
| 40 |
f, |
| 41 |
"{} {:2}y {:2}m {:2}w {:2}d", |
| 42 |
if self.0.is_negative { '-' } else { '+' }, |
| 43 |
self.0.years, |
| 44 |
self.0.months, |
| 45 |
self.0.weeks, |
| 46 |
self.0.days |
| 47 |
) |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
struct TestOutput { |
| 52 |
pub cal: &'static str, |
| 53 |
pub start: ArithmeticDateForLogging, |
| 54 |
pub end: ArithmeticDateForLogging, |
| 55 |
pub duration: DateDurationForLogging, |
| 56 |
pub calculated_duration: DateDurationForLogging, |
| 57 |
pub is_rejected: bool, |
| 58 |
} |
| 59 |
|
| 60 |
impl fmt::Display for TestOutput { |
| 61 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 62 |
write!( |
| 63 |
f, |
| 64 |
"{} {} = {} ({})", |
| 65 |
self.start, self.duration, self.end, self.cal |
| 66 |
)?; |
| 67 |
if self.duration.0 != self.calculated_duration.0 { |
| 68 |
write!(f, "; round-trip duration: {}", self.calculated_duration)?; |
| 69 |
} |
| 70 |
if self.is_rejected { |
| 71 |
write!(f, "; rejected")?; |
| 72 |
} |
| 73 |
Ok(()) |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
struct TestOutputs(Vec<TestOutput>); |
| 78 |
|
| 79 |
impl fmt::Display for TestOutputs { |
| 80 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 81 |
for output in self.0.iter() { |
| 82 |
writeln!(f, "{}", output)?; |
| 83 |
} |
| 84 |
Ok(()) |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
super::test_all_cals!( |
| 89 |
fn test_arithmetic<C: Calendar + Copy>(cal: C) { |
| 90 |
fn new_duration(years: i32, months: i32, weeks: i32, days: i32) -> DateDuration { |
| 91 |
let is_negative = years < 0 || months < 0 || weeks < 0 || days < 0; |
| 92 |
// Verify no mixed signs |
| 93 |
if is_negative { |
| 94 |
assert!(years <= 0 && months <= 0 && weeks <= 0 && days <= 0); |
| 95 |
} else { |
| 96 |
assert!(years >= 0 && months >= 0 && weeks >= 0 && days >= 0); |
| 97 |
} |
| 98 |
DateDuration { |
| 99 |
is_negative, |
| 100 |
years: years.unsigned_abs(), |
| 101 |
months: months.unsigned_abs(), |
| 102 |
weeks: weeks.unsigned_abs(), |
| 103 |
days: days.unsigned_abs(), |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
let mut durations = Vec::new(); |
| 108 |
|
| 109 |
// Check +/- 30 months |
| 110 |
for i in (-30..=30).filter(|i| *i != 0) { |
| 111 |
durations.push(new_duration(0, i, 0, 0)); |
| 112 |
} |
| 113 |
|
| 114 |
// Check +/- 30 months with +/- 1 day |
| 115 |
for i in (-30i32..=30).filter(|i| *i != 0) { |
| 116 |
let days = i.signum(); |
| 117 |
durations.push(new_duration(0, i, 0, days)); |
| 118 |
} |
| 119 |
|
| 120 |
// Check +/- 10 years |
| 121 |
for i in (-10..=10).filter(|i| *i != 0) { |
| 122 |
durations.push(new_duration(i, 0, 0, 0)); |
| 123 |
} |
| 124 |
|
| 125 |
// Check +/- 10 years with +/- 1 month |
| 126 |
for i in (-10i32..=10).filter(|i| *i != 0) { |
| 127 |
let months = i.signum(); |
| 128 |
durations.push(new_duration(i, months, 0, 0)); |
| 129 |
} |
| 130 |
|
| 131 |
// Check +/- 10 years with +/- 1 day |
| 132 |
for i in (-10i32..=10).filter(|i| *i != 0) { |
| 133 |
let days = i.signum(); |
| 134 |
durations.push(new_duration(i, 0, 0, days)); |
| 135 |
} |
| 136 |
|
| 137 |
// Check +/- 10 years with +/- 1 month and +/- 1 day |
| 138 |
for i in (-10i32..=10).filter(|i| *i != 0) { |
| 139 |
let s = i.signum(); |
| 140 |
durations.push(new_duration(i, s, 0, s)); |
| 141 |
} |
| 142 |
|
| 143 |
let start_date = Date::try_new_iso(2000, 1, 1).unwrap(); |
| 144 |
let end_date = Date::try_new_iso(2004, 12, 31).unwrap(); |
| 145 |
let start_rd = start_date.to_rata_die(); |
| 146 |
let end_rd = end_date.to_rata_die(); |
| 147 |
|
| 148 |
let add_constrain = DateAddOptions { |
| 149 |
overflow: Some(Overflow::Constrain), |
| 150 |
}; |
| 151 |
let add_reject = DateAddOptions { |
| 152 |
overflow: Some(Overflow::Reject), |
| 153 |
}; |
| 154 |
|
| 155 |
let mut outputs = TestOutputs(Vec::new()); |
| 156 |
|
| 157 |
for rd_offset in 0..=(end_rd - start_rd) { |
| 158 |
let date = Date::from_rata_die(start_rd + rd_offset, cal); |
| 159 |
if date.day_of_month().0 > 2 && date.day_of_month().0 < 27 { |
| 160 |
// Most interesting cases are at the beginning or end of the month |
| 161 |
continue; |
| 162 |
} |
| 163 |
|
| 164 |
for duration in &durations { |
| 165 |
let mut diff_options = DateDifferenceOptions::default(); |
| 166 |
if duration.years != 0 { |
| 167 |
diff_options.largest_unit = Some(DateDurationUnit::Years); |
| 168 |
} else if duration.months != 0 { |
| 169 |
diff_options.largest_unit = Some(DateDurationUnit::Months); |
| 170 |
} else { |
| 171 |
diff_options.largest_unit = Some(DateDurationUnit::Days); |
| 172 |
} |
| 173 |
|
| 174 |
let added_date = date |
| 175 |
.try_added_with_options(*duration, add_constrain) |
| 176 |
.unwrap_or_else(|_| { |
| 177 |
panic!( |
| 178 |
"Failed to add duration {:?} to date {:?} in calendar {:?}", |
| 179 |
duration, |
| 180 |
date, |
| 181 |
cal.as_calendar().debug_name() |
| 182 |
) |
| 183 |
}); |
| 184 |
|
| 185 |
let calculated_duration = date |
| 186 |
.try_until_with_options(&added_date, diff_options) |
| 187 |
.unwrap_or_else(|_| { |
| 188 |
panic!( |
| 189 |
"Failed to calculate difference between {:?} and {:?} in calendar {:?}", |
| 190 |
date, |
| 191 |
added_date, |
| 192 |
cal.as_calendar().debug_name() |
| 193 |
) |
| 194 |
}); |
| 195 |
|
| 196 |
let is_rejected = date.try_added_with_options(*duration, add_reject).is_err(); |
| 197 |
|
| 198 |
let output = TestOutput { |
| 199 |
cal: cal.debug_name(), |
| 200 |
start: date.into(), |
| 201 |
end: added_date.into(), |
| 202 |
duration: DateDurationForLogging(*duration), |
| 203 |
calculated_duration: DateDurationForLogging(calculated_duration), |
| 204 |
is_rejected, |
| 205 |
}; |
| 206 |
|
| 207 |
// The durations should have the same sign! |
| 208 |
assert_eq!( |
| 209 |
duration.is_negative, calculated_duration.is_negative, |
| 210 |
"{output}" |
| 211 |
); |
| 212 |
assert_eq!(duration.is_negative, date > added_date, "{output}"); |
| 213 |
|
| 214 |
// Round-trip check |
| 215 |
let added_back = date |
| 216 |
.try_added_with_options(calculated_duration, add_constrain) |
| 217 |
.unwrap(); |
| 218 |
assert_eq!( |
| 219 |
added_back, added_date, |
| 220 |
"Round trip failed for {:?} + {:?} -> {:?}. Got duration {:?} which led to {:?}", |
| 221 |
date, duration, added_date, calculated_duration, added_back |
| 222 |
); |
| 223 |
|
| 224 |
// Either test the duration against a known result or add it to the snapshot |
| 225 |
assert_eq!(duration.weeks, 0); |
| 226 |
if duration.months == 0 && duration.days == 0 { |
| 227 |
// Years should have arithmetic behavior. |
| 228 |
assert_eq!( |
| 229 |
duration.add_years_to(date.year().extended_year()), |
| 230 |
added_date.year().extended_year(), |
| 231 |
"{output}" |
| 232 |
); |
| 233 |
// Month or day could constrain; add the ones that do to the snapshot. |
| 234 |
if date.month().to_input() != added_date.month().to_input() |
| 235 |
|| date.day_of_month() != added_date.day_of_month() |
| 236 |
{ |
| 237 |
assert!(is_rejected, "should reject: {output}"); |
| 238 |
outputs.0.push(output); |
| 239 |
} else { |
| 240 |
// Make sure we aren't skipping any normalized-duration tests |
| 241 |
assert!(!is_rejected, "should NOT reject: {output}"); |
| 242 |
assert_eq!(*duration, calculated_duration); |
| 243 |
} |
| 244 |
} else if duration.years == 0 && duration.days == 0 { |
| 245 |
// Months should have arithmetic behavior. |
| 246 |
let earlier = std::cmp::min(date, added_date); |
| 247 |
let later = std::cmp::max(date, added_date); |
| 248 |
let mut month_diff = 0; |
| 249 |
month_diff -= earlier.month().ordinal as i32; |
| 250 |
month_diff += later.month().ordinal as i32; |
| 251 |
for y in earlier.year().extended_year()..later.year().extended_year() { |
| 252 |
month_diff += Date::try_new(y.into(), Month::new(1), 1, cal) |
| 253 |
.unwrap() |
| 254 |
.months_in_year() as i32; |
| 255 |
} |
| 256 |
assert_eq!(duration.months, month_diff.unsigned_abs(), "{output}"); |
| 257 |
// Days could constrain; add the ones that do to the snapshot. |
| 258 |
if date.day_of_month() != added_date.day_of_month() { |
| 259 |
assert!(is_rejected, "should reject: {output}"); |
| 260 |
outputs.0.push(output); |
| 261 |
} else { |
| 262 |
// Make sure we aren't skipping any normalized-duration tests |
| 263 |
assert!(!is_rejected, "should NOT reject: {output}"); |
| 264 |
assert_eq!(*duration, calculated_duration); |
| 265 |
} |
| 266 |
} else if duration.years != 0 && duration.months != 0 && duration.days == 0 { |
| 267 |
// These cases are some of the trickiest. Toss them all in the snapshots! |
| 268 |
outputs.0.push(output); |
| 269 |
} else if duration.years == 0 && duration.months != 0 && duration.days == 1 { |
| 270 |
// This should add months, constrain, and add a day. |
| 271 |
// Month-constrain by itself is tested by other cases, |
| 272 |
// which also test Overflow::Reject behavior. |
| 273 |
let signed_months = duration.add_months_to(0); |
| 274 |
let expected_rd = date |
| 275 |
.try_added_with_options( |
| 276 |
DateDuration::for_months(signed_months), |
| 277 |
add_constrain, |
| 278 |
) |
| 279 |
.unwrap() |
| 280 |
.to_rata_die() |
| 281 |
+ i64::from(signed_months.signum()); |
| 282 |
assert_eq!(added_date.to_rata_die(), expected_rd, "{output}"); |
| 283 |
// Only add the snapshot if the duration was normalized |
| 284 |
if *duration != calculated_duration { |
| 285 |
outputs.0.push(output); |
| 286 |
} |
| 287 |
} else if duration.years != 0 && duration.months == 0 && duration.days == 1 { |
| 288 |
// This should add years, constrain, and add a day. |
| 289 |
// Year-constrain by itself is tested by other cases, |
| 290 |
// which also test Overflow::Reject behavior. |
| 291 |
let signed_years = duration.add_years_to(0); |
| 292 |
let expected_rd = date |
| 293 |
.try_added_with_options( |
| 294 |
DateDuration::for_years(signed_years), |
| 295 |
add_constrain, |
| 296 |
) |
| 297 |
.unwrap() |
| 298 |
.to_rata_die() |
| 299 |
+ i64::from(signed_years.signum()); |
| 300 |
assert_eq!(added_date.to_rata_die(), expected_rd, "{output}"); |
| 301 |
// Only add the snapshot if the duration was normalized |
| 302 |
if *duration != calculated_duration { |
| 303 |
outputs.0.push(output); |
| 304 |
} |
| 305 |
} else if duration.years != 0 && duration.months != 0 && duration.days == 1 { |
| 306 |
// This should add years and months, constrain, and add a day. |
| 307 |
// Year-month-constrain by itself is tested by other cases, |
| 308 |
// which also test Overflow::Reject behavior. |
| 309 |
let mut year_month_duration = *duration; |
| 310 |
year_month_duration.days = 0; |
| 311 |
let expected_rd = date |
| 312 |
.try_added_with_options(year_month_duration, add_constrain) |
| 313 |
.unwrap() |
| 314 |
.to_rata_die() |
| 315 |
+ if duration.is_negative { -1i64 } else { 1i64 }; |
| 316 |
assert_eq!(added_date.to_rata_die(), expected_rd, "{output}"); |
| 317 |
// Only add the snapshot if the duration was normalized |
| 318 |
if *duration != calculated_duration { |
| 319 |
outputs.0.push(output); |
| 320 |
} |
| 321 |
} else { |
| 322 |
panic!("Not covered: {output}"); |
| 323 |
} |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
insta::assert_snapshot!(format!("date_arithmetic_{}", cal.debug_name()), outputs); |
| 328 |
} |
| 329 |
); |
| 330 |
|
| 331 |
super::test_all_cals!( |
| 332 |
fn test_day_arithmetic<C: Calendar + Copy>(cal: C) { |
| 333 |
let start_date = Date::try_new_iso(2000, 1, 1).unwrap(); |
| 334 |
let end_date = Date::try_new_iso(2004, 12, 31).unwrap(); |
| 335 |
let start_rd = start_date.to_rata_die(); |
| 336 |
let end_rd = end_date.to_rata_die(); |
| 337 |
|
| 338 |
let add_constrain = DateAddOptions { |
| 339 |
overflow: Some(Overflow::Constrain), |
| 340 |
}; |
| 341 |
let add_reject = DateAddOptions { |
| 342 |
overflow: Some(Overflow::Reject), |
| 343 |
}; |
| 344 |
let diff_options = DateDifferenceOptions { |
| 345 |
largest_unit: Some(DateDurationUnit::Days), |
| 346 |
}; |
| 347 |
|
| 348 |
for rd_offset in 0..=(end_rd - start_rd) { |
| 349 |
let date = Date::from_rata_die(start_rd + rd_offset, cal); |
| 350 |
|
| 351 |
// Check +/- 65 days |
| 352 |
for i in -65..=65 { |
| 353 |
let duration = DateDuration::for_days(i); |
| 354 |
let added_date = date |
| 355 |
.try_added_with_options(duration, add_constrain) |
| 356 |
.unwrap(); |
| 357 |
let is_rejected = date.try_added_with_options(duration, add_reject).is_err(); |
| 358 |
let calculated_duration = date |
| 359 |
.try_until_with_options(&added_date, diff_options) |
| 360 |
.unwrap(); |
| 361 |
assert_eq!(duration, calculated_duration); |
| 362 |
assert_eq!(i, (added_date.to_rata_die() - date.to_rata_die()) as i32); |
| 363 |
assert!(!is_rejected, "should NOT reject"); |
| 364 |
} |
| 365 |
} |
| 366 |
} |
| 367 |
); |