| 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 std::{collections::HashSet, fmt::Debug}; |
| 6 |
|
| 7 |
use icu_calendar::{ |
| 8 |
Calendar, Date, |
| 9 |
cal::*, |
| 10 |
error::DateFromFieldsError, |
| 11 |
options::{DateFromFieldsOptions, MissingFieldsStrategy, Overflow}, |
| 12 |
types::{DateFields, Month}, |
| 13 |
}; |
| 14 |
|
| 15 |
/// Test that a given calendar produces valid monthdays |
| 16 |
/// |
| 17 |
/// `valid_md_condition`, given `(month_number, is_leap, day_number)`, should return whether or not |
| 18 |
/// that combination is ever possible in that calendar |
| 19 |
fn test_reference_year_impl<C>(cal: C, valid_md_condition: impl Fn(u8, bool, u8) -> ValidityState) |
| 20 |
where |
| 21 |
C: Calendar + Debug + Copy, |
| 22 |
{ |
| 23 |
// Test that all dates in a certain range behave according to Temporal |
| 24 |
let mut month_days_seen = HashSet::new(); |
| 25 |
let mut rd = Date::try_new_iso(1972, 12, 31).unwrap().to_rata_die(); |
| 26 |
for _ in 1..2000 { |
| 27 |
let date = Date::from_rata_die(rd, cal); |
| 28 |
let month_day = (date.month().to_input(), date.day_of_month().0); |
| 29 |
let mut fields = DateFields::default(); |
| 30 |
fields.month = Some(date.month().to_input()); |
| 31 |
fields.day = Some(month_day.1); |
| 32 |
let mut options = DateFromFieldsOptions::default(); |
| 33 |
options.missing_fields_strategy = Some(MissingFieldsStrategy::Ecma); |
| 34 |
let reference_date = Date::try_from_fields(fields, options, cal).unwrap(); |
| 35 |
if month_days_seen.contains(&month_day) { |
| 36 |
assert_ne!(date, reference_date, "{cal:?}"); |
| 37 |
} else { |
| 38 |
assert_eq!(date, reference_date, "{cal:?}"); |
| 39 |
month_days_seen.insert(month_day); |
| 40 |
} |
| 41 |
rd -= 1; |
| 42 |
} |
| 43 |
// Test that all MonthDay values round-trip |
| 44 |
for month_number in 1..=14 { |
| 45 |
for is_leap in [false, true] { |
| 46 |
let mut valid_day_number = 1; |
| 47 |
let month_validity = valid_md_condition(month_number, is_leap, valid_day_number); |
| 48 |
// The last valid monthday produced with Overflow::Reject in this month |
| 49 |
let mut last_valid_date = None; |
| 50 |
for day_number in 1..=32 { |
| 51 |
let md_validity = valid_md_condition(month_number, is_leap, day_number); |
| 52 |
if md_validity == ValidityState::Valid { |
| 53 |
valid_day_number = day_number; |
| 54 |
} |
| 55 |
let mut fields = DateFields::default(); |
| 56 |
fields.month = Some(match is_leap { |
| 57 |
false => Month::new(month_number), |
| 58 |
true => Month::leap(month_number), |
| 59 |
}); |
| 60 |
fields.day = Some(day_number); |
| 61 |
let mut options = DateFromFieldsOptions::default(); |
| 62 |
options.overflow = Some(Overflow::Constrain); |
| 63 |
options.missing_fields_strategy = Some(MissingFieldsStrategy::Ecma); |
| 64 |
let reference_date = match Date::try_from_fields(fields, options, cal) { |
| 65 |
Ok(d) => { |
| 66 |
assert!( |
| 67 |
month_validity != ValidityState::Invalid, |
| 68 |
"try_from_fields passed but should have failed: {fields:?} => {d:?}" |
| 69 |
); |
| 70 |
d |
| 71 |
} |
| 72 |
Err(DateFromFieldsError::MonthNotInCalendar) => { |
| 73 |
assert!( |
| 74 |
month_validity == ValidityState::Invalid, |
| 75 |
"try_from_fields failed but should have passed: {fields:?}" |
| 76 |
); |
| 77 |
continue; |
| 78 |
} |
| 79 |
Err(e) => { |
| 80 |
panic!("Unexpected error in month day from fields: {e}"); |
| 81 |
} |
| 82 |
}; |
| 83 |
|
| 84 |
// Test round-trip (to valid day number) |
| 85 |
if md_validity == ValidityState::ChineseConstrain { |
| 86 |
let input_month = fields.month.unwrap(); |
| 87 |
let output_month = reference_date.month().to_input(); |
| 88 |
// When constraining in the Chinese calendar the month |
| 89 |
// stays the same but loses leapiness. |
| 90 |
assert_eq!( |
| 91 |
input_month.number(), |
| 92 |
output_month.number(), |
| 93 |
"Month number should stay the same: {fields:?} {cal:?}" |
| 94 |
); |
| 95 |
assert_ne!( |
| 96 |
input_month.is_leap(), |
| 97 |
output_month.is_leap(), |
| 98 |
"Leap month should turn into regular month: {fields:?} {cal:?}" |
| 99 |
); |
| 100 |
} else { |
| 101 |
assert_eq!( |
| 102 |
fields.month.unwrap(), |
| 103 |
reference_date.month().to_input(), |
| 104 |
"{fields:?} {cal:?}" |
| 105 |
); |
| 106 |
assert_eq!( |
| 107 |
valid_day_number, |
| 108 |
reference_date.day_of_month().0, |
| 109 |
"{fields:?} {cal:?}" |
| 110 |
); |
| 111 |
} |
| 112 |
|
| 113 |
// Test Overflow::Reject |
| 114 |
options.overflow = Some(Overflow::Reject); |
| 115 |
let reject_result = Date::try_from_fields(fields, options, cal); |
| 116 |
if md_validity == ValidityState::ChineseConstrain { |
| 117 |
assert!(matches!( |
| 118 |
reject_result, |
| 119 |
Err(DateFromFieldsError::MonthNotInYear) |
| 120 |
)) |
| 121 |
} else if valid_day_number == day_number { |
| 122 |
assert_eq!(reject_result, Ok(reference_date)); |
| 123 |
} else { |
| 124 |
assert!(matches!( |
| 125 |
reject_result, |
| 126 |
Err(DateFromFieldsError::InvalidDay { .. }) |
| 127 |
)) |
| 128 |
} |
| 129 |
|
| 130 |
// Constrain *must* constrain to the last valid day in |
| 131 |
// the month, except for when the leap month turns into a regular month |
| 132 |
// in the Chinese/Dangi "nonexistant in modern times" leap month edge case. |
| 133 |
if let Ok(reject_result) = reject_result { |
| 134 |
// We didn't need to constrain here, update the last valid date |
| 135 |
last_valid_date = Some(reject_result); |
| 136 |
} else if let Some(last_valid_date) = last_valid_date |
| 137 |
&& md_validity != ValidityState::ChineseConstrain |
| 138 |
{ |
| 139 |
assert_eq!( |
| 140 |
last_valid_date, reference_date, |
| 141 |
"Constrain should constrain to the last valid date in the month" |
| 142 |
); |
| 143 |
} |
| 144 |
|
| 145 |
// Test that ordinal months cause it to fail (even if the month code is still set) |
| 146 |
fields.ordinal_month = Some(month_number); |
| 147 |
let ordinal_result = Date::try_from_fields(fields, options, cal); |
| 148 |
assert!(matches!( |
| 149 |
ordinal_result, |
| 150 |
Err(DateFromFieldsError::NotEnoughFields) |
| 151 |
)); |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
#[derive(PartialEq, Debug)] |
| 158 |
enum ValidityState { |
| 159 |
Valid, |
| 160 |
Invalid, |
| 161 |
// Chinese/Korean have special behavior where certain leap month-day combos are specced |
| 162 |
// as constraining to the regular month, because they are have not been found in the modern |
| 163 |
// time range where implementations are expected to agree. |
| 164 |
ChineseConstrain, |
| 165 |
} |
| 166 |
|
| 167 |
impl From<bool> for ValidityState { |
| 168 |
fn from(other: bool) -> ValidityState { |
| 169 |
if other { |
| 170 |
ValidityState::Valid |
| 171 |
} else { |
| 172 |
ValidityState::Invalid |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
fn gregorian_md_condition(month_number: u8, is_leap: bool, day_number: u8) -> ValidityState { |
| 178 |
// No leap months |
| 179 |
if is_leap { |
| 180 |
return ValidityState::Invalid; |
| 181 |
} |
| 182 |
|
| 183 |
ValidityState::from(match month_number { |
| 184 |
1 | 3 | 5 | 7 | 8 | 10 | 12 => day_number <= 31, |
| 185 |
2 => day_number <= 29, |
| 186 |
4 | 6 | 9 | 11 => day_number <= 30, |
| 187 |
_ => { |
| 188 |
assert!(month_number > 12); |
| 189 |
// No other months |
| 190 |
false |
| 191 |
} |
| 192 |
}) |
| 193 |
} |
| 194 |
|
| 195 |
fn chinese_md_condition(month_number: u8, is_leap: bool, day_number: u8) -> ValidityState { |
| 196 |
// https://tc39.es/proposal-intl-era-monthcode/#chinese-dangi-iso-reference-years |
| 197 |
|
| 198 |
if (month_number == 1 && is_leap) |
| 199 |
|| (month_number == 2 && is_leap && day_number >= 30) |
| 200 |
|| (month_number == 8 && is_leap && day_number >= 30) |
| 201 |
|| (month_number == 9 && is_leap && day_number >= 30) |
| 202 |
|| (month_number == 10 && is_leap && day_number >= 30) |
| 203 |
|| (month_number == 11 && is_leap && day_number >= 30) |
| 204 |
|| (month_number == 12 && is_leap) |
| 205 |
{ |
| 206 |
return ValidityState::ChineseConstrain; |
| 207 |
} |
| 208 |
ValidityState::from(month_number <= 12 && day_number <= 30) |
| 209 |
} |
| 210 |
|
| 211 |
fn coptic_md_condition(month_number: u8, is_leap: bool, day_number: u8) -> ValidityState { |
| 212 |
// No leap months |
| 213 |
if is_leap { |
| 214 |
return ValidityState::Invalid; |
| 215 |
} |
| 216 |
ValidityState::from(match month_number { |
| 217 |
1..=12 => day_number <= 30, |
| 218 |
13 => day_number <= 6, |
| 219 |
_ => false, |
| 220 |
}) |
| 221 |
} |
| 222 |
|
| 223 |
fn hijri_md_condition(month_number: u8, is_leap: bool, day_number: u8) -> ValidityState { |
| 224 |
// No leap months |
| 225 |
if is_leap { |
| 226 |
return ValidityState::Invalid; |
| 227 |
} |
| 228 |
ValidityState::from(month_number <= 12 && day_number <= 30) |
| 229 |
} |
| 230 |
|
| 231 |
fn hijri_tabular_md_condition(month_number: u8, is_leap: bool, day_number: u8) -> ValidityState { |
| 232 |
// No leap months |
| 233 |
if is_leap { |
| 234 |
return ValidityState::Invalid; |
| 235 |
} |
| 236 |
|
| 237 |
if month_number > 12 { |
| 238 |
return ValidityState::Invalid; |
| 239 |
} |
| 240 |
|
| 241 |
// Odd months have 30 days, even months have 29, except for M12 in a leap year |
| 242 |
ValidityState::from(if month_number.is_multiple_of(2) { |
| 243 |
if month_number == 12 { |
| 244 |
day_number <= 30 |
| 245 |
} else { |
| 246 |
day_number <= 29 |
| 247 |
} |
| 248 |
} else { |
| 249 |
day_number <= 30 |
| 250 |
}) |
| 251 |
} |
| 252 |
|
| 253 |
fn hebrew_md_condition(month_number: u8, is_leap: bool, day_number: u8) -> ValidityState { |
| 254 |
if is_leap { |
| 255 |
return ValidityState::from(month_number == 5 && day_number <= 30); |
| 256 |
} |
| 257 |
ValidityState::from(match month_number { |
| 258 |
1 | 2 | 3 | 5 | 7 | 9 | 11 => day_number <= 30, |
| 259 |
// Tevet, Adar, Iyar, Tammuz, Elul |
| 260 |
4 | 6 | 8 | 10 | 12 => day_number <= 29, |
| 261 |
_ => { |
| 262 |
assert!(month_number > 12); |
| 263 |
// No other months |
| 264 |
false |
| 265 |
} |
| 266 |
}) |
| 267 |
} |
| 268 |
|
| 269 |
#[test] |
| 270 |
fn test_reference_year_buddhist() { |
| 271 |
test_reference_year_impl(Buddhist, gregorian_md_condition) |
| 272 |
} |
| 273 |
|
| 274 |
#[test] |
| 275 |
fn test_reference_year_chinese() { |
| 276 |
test_reference_year_impl(ChineseTraditional::new(), chinese_md_condition) |
| 277 |
} |
| 278 |
|
| 279 |
#[test] |
| 280 |
fn test_reference_year_coptic() { |
| 281 |
test_reference_year_impl(Coptic, coptic_md_condition) |
| 282 |
} |
| 283 |
|
| 284 |
#[test] |
| 285 |
fn test_reference_year_korean() { |
| 286 |
test_reference_year_impl(KoreanTraditional::new(), chinese_md_condition) |
| 287 |
} |
| 288 |
|
| 289 |
#[test] |
| 290 |
fn test_reference_year_ethiopian() { |
| 291 |
test_reference_year_impl(Ethiopian::new(), coptic_md_condition) |
| 292 |
} |
| 293 |
|
| 294 |
#[test] |
| 295 |
fn test_reference_year_ethiopian_amete_alem() { |
| 296 |
test_reference_year_impl( |
| 297 |
Ethiopian::new_with_era_style(EthiopianEraStyle::AmeteAlem), |
| 298 |
coptic_md_condition, |
| 299 |
) |
| 300 |
} |
| 301 |
|
| 302 |
#[test] |
| 303 |
fn test_reference_year_gregorian() { |
| 304 |
test_reference_year_impl(Gregorian, gregorian_md_condition) |
| 305 |
} |
| 306 |
|
| 307 |
#[test] |
| 308 |
fn test_reference_year_julian() { |
| 309 |
test_reference_year_impl(Julian, gregorian_md_condition) |
| 310 |
} |
| 311 |
|
| 312 |
#[test] |
| 313 |
fn test_reference_year_hebrew() { |
| 314 |
test_reference_year_impl(Hebrew, hebrew_md_condition) |
| 315 |
} |
| 316 |
|
| 317 |
#[test] |
| 318 |
fn test_reference_year_indian() { |
| 319 |
test_reference_year_impl(Indian, |month_number, is_leap, day_number| { |
| 320 |
if is_leap { |
| 321 |
// No leap months |
| 322 |
return ValidityState::Invalid; |
| 323 |
} |
| 324 |
// First half of the year has long months, second half short |
| 325 |
ValidityState::from(if month_number <= 6 { |
| 326 |
day_number <= 31 |
| 327 |
} else if month_number <= 12 { |
| 328 |
day_number <= 30 |
| 329 |
} else { |
| 330 |
// No larger months |
| 331 |
false |
| 332 |
}) |
| 333 |
}) |
| 334 |
} |
| 335 |
|
| 336 |
#[test] |
| 337 |
fn test_reference_year_hijri_tabular_type_ii_friday() { |
| 338 |
test_reference_year_impl( |
| 339 |
Hijri::new_tabular(HijriTabularLeapYears::TypeII, HijriTabularEpoch::Friday), |
| 340 |
hijri_tabular_md_condition, |
| 341 |
) |
| 342 |
} |
| 343 |
|
| 344 |
#[test] |
| 345 |
fn test_reference_year_hijri_tabular_type_ii_thursday() { |
| 346 |
test_reference_year_impl( |
| 347 |
Hijri::new_tabular(HijriTabularLeapYears::TypeII, HijriTabularEpoch::Thursday), |
| 348 |
hijri_tabular_md_condition, |
| 349 |
) |
| 350 |
} |
| 351 |
|
| 352 |
#[test] |
| 353 |
fn test_reference_year_hijri_umm_al_qura() { |
| 354 |
test_reference_year_impl(Hijri::new_umm_al_qura(), hijri_md_condition) |
| 355 |
} |
| 356 |
|
| 357 |
#[test] |
| 358 |
fn test_reference_year_iso() { |
| 359 |
test_reference_year_impl(Iso, gregorian_md_condition) |
| 360 |
} |
| 361 |
|
| 362 |
#[test] |
| 363 |
fn test_reference_year_japanese() { |
| 364 |
test_reference_year_impl(Japanese::new(), gregorian_md_condition) |
| 365 |
} |
| 366 |
|
| 367 |
#[test] |
| 368 |
fn test_reference_year_persian() { |
| 369 |
test_reference_year_impl(Persian, |month_number, is_leap, day_number| { |
| 370 |
if is_leap { |
| 371 |
// No leap months |
| 372 |
return ValidityState::Invalid; |
| 373 |
} |
| 374 |
// First half of the year has long months, second half short |
| 375 |
ValidityState::from(if month_number <= 6 { |
| 376 |
day_number <= 31 |
| 377 |
} else if month_number <= 12 { |
| 378 |
day_number <= 30 |
| 379 |
} else { |
| 380 |
// No larger months |
| 381 |
false |
| 382 |
}) |
| 383 |
}) |
| 384 |
} |
| 385 |
|
| 386 |
#[test] |
| 387 |
fn test_reference_year_roc() { |
| 388 |
test_reference_year_impl(Roc, gregorian_md_condition) |
| 389 |
} |