| 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::*; |
| 6 |
use core::marker::PhantomData; |
| 7 |
|
| 8 |
fn check_continuity<A: AsCalendar>(mut date: Date<A>) { |
| 9 |
let one_day_duration = DateDuration::<A::Calendar> { |
| 10 |
years: 0, |
| 11 |
months: 0, |
| 12 |
weeks: 0, |
| 13 |
days: 1, |
| 14 |
marker: PhantomData, |
| 15 |
}; |
| 16 |
|
| 17 |
let mut rata_die = date.to_fixed(); |
| 18 |
let mut weekday = date.day_of_week(); |
| 19 |
let mut year = date.year(); |
| 20 |
let mut is_in_leap_year = date.is_in_leap_year(); |
| 21 |
|
| 22 |
for _ in 0..(366 * 20) { |
| 23 |
let next_date = date.added(one_day_duration); |
| 24 |
let next_rata_die = next_date.to_fixed(); |
| 25 |
assert_eq!(next_rata_die, rata_die + 1, "{next_date:?}"); |
| 26 |
let next_weekday = next_date.day_of_week(); |
| 27 |
let next_year = next_date.year(); |
| 28 |
let next_is_in_leap_year = next_date.is_in_leap_year(); |
| 29 |
assert_eq!( |
| 30 |
(next_weekday as usize) % 7, |
| 31 |
(weekday as usize + 1) % 7, |
| 32 |
"{next_date:?}" |
| 33 |
); |
| 34 |
if year == next_year { |
| 35 |
assert_eq!(is_in_leap_year, next_is_in_leap_year, "{next_date:?}"); |
| 36 |
} |
| 37 |
date = next_date; |
| 38 |
rata_die = next_rata_die; |
| 39 |
weekday = next_weekday; |
| 40 |
year = next_year; |
| 41 |
is_in_leap_year = next_is_in_leap_year; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
fn check_every_250_days<A: AsCalendar>(mut date: Date<A>) { |
| 46 |
let one_thousand_days_duration = DateDuration::<A::Calendar> { |
| 47 |
years: 0, |
| 48 |
months: 0, |
| 49 |
weeks: 0, |
| 50 |
days: 250, |
| 51 |
marker: PhantomData, |
| 52 |
}; |
| 53 |
|
| 54 |
let mut rata_die = date.to_fixed(); |
| 55 |
|
| 56 |
for _ in 0..2000 { |
| 57 |
let next_date = date.added(one_thousand_days_duration); |
| 58 |
let next_iso = next_date.to_iso(); |
| 59 |
let next_rata_die = next_iso.to_fixed(); |
| 60 |
assert_eq!(next_rata_die, rata_die + 250, "{next_date:?}"); |
| 61 |
let next_date_roundtrip = next_iso.to_calendar(Ref(next_date.calendar())); |
| 62 |
assert_eq!(next_date, next_date_roundtrip, "{next_date:?}"); |
| 63 |
date = next_date; |
| 64 |
rata_die = next_rata_die; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
#[test] |
| 69 |
fn test_buddhist_continuity() { |
| 70 |
let date = Date::try_new_buddhist_date(-10, 1, 1); |
| 71 |
check_continuity(date.unwrap()); |
| 72 |
let date = Date::try_new_buddhist_date(-300, 1, 1); |
| 73 |
check_every_250_days(date.unwrap()); |
| 74 |
} |
| 75 |
|
| 76 |
#[test] |
| 77 |
fn test_chinese_continuity() { |
| 78 |
let cal = crate::chinese::Chinese::new(); |
| 79 |
let cal = Ref(&cal); |
| 80 |
let date = Date::try_new_chinese_date_with_calendar(-10, 1, 1, cal); |
| 81 |
check_continuity(date.unwrap()); |
| 82 |
let date = Date::try_new_chinese_date_with_calendar(-300, 1, 1, cal); |
| 83 |
check_every_250_days(date.unwrap()); |
| 84 |
let date = Date::try_new_chinese_date_with_calendar(-10000, 1, 1, cal); |
| 85 |
check_every_250_days(date.unwrap()); |
| 86 |
} |
| 87 |
|
| 88 |
#[test] |
| 89 |
fn test_coptic_continuity() { |
| 90 |
let date = Date::try_new_coptic_date(-10, 1, 1); |
| 91 |
check_continuity(date.unwrap()); |
| 92 |
let date = Date::try_new_coptic_date(-300, 1, 1); |
| 93 |
check_every_250_days(date.unwrap()); |
| 94 |
} |
| 95 |
|
| 96 |
#[test] |
| 97 |
fn test_dangi_continuity() { |
| 98 |
let cal = crate::dangi::Dangi::new(); |
| 99 |
let cal = Ref(&cal); |
| 100 |
let date = Date::try_new_dangi_date_with_calendar(-10, 1, 1, cal); |
| 101 |
check_continuity(date.unwrap()); |
| 102 |
let date = Date::try_new_dangi_date_with_calendar(-300, 1, 1, cal); |
| 103 |
check_every_250_days(date.unwrap()); |
| 104 |
} |
| 105 |
|
| 106 |
#[test] |
| 107 |
fn test_ethiopian_continuity() { |
| 108 |
use crate::ethiopian::EthiopianEraStyle::*; |
| 109 |
let date = Date::try_new_ethiopian_date(AmeteMihret, -10, 1, 1); |
| 110 |
check_continuity(date.unwrap()); |
| 111 |
let date = Date::try_new_ethiopian_date(AmeteMihret, -300, 1, 1); |
| 112 |
check_every_250_days(date.unwrap()); |
| 113 |
} |
| 114 |
|
| 115 |
#[test] |
| 116 |
fn test_ethiopian_amete_alem_continuity() { |
| 117 |
use crate::ethiopian::EthiopianEraStyle::*; |
| 118 |
let date = Date::try_new_ethiopian_date(AmeteAlem, -10, 1, 1); |
| 119 |
check_continuity(date.unwrap()); |
| 120 |
let date = Date::try_new_ethiopian_date(AmeteAlem, -300, 1, 1); |
| 121 |
check_every_250_days(date.unwrap()); |
| 122 |
} |
| 123 |
|
| 124 |
#[test] |
| 125 |
fn test_gregorian_continuity() { |
| 126 |
let date = Date::try_new_gregorian_date(-10, 1, 1); |
| 127 |
check_continuity(date.unwrap()); |
| 128 |
let date = Date::try_new_gregorian_date(-300, 1, 1); |
| 129 |
check_every_250_days(date.unwrap()); |
| 130 |
} |
| 131 |
|
| 132 |
#[test] |
| 133 |
fn test_hebrew_continuity() { |
| 134 |
let date = Date::try_new_hebrew_date(-10, 1, 1); |
| 135 |
check_continuity(date.unwrap()); |
| 136 |
let date = Date::try_new_hebrew_date(-300, 1, 1); |
| 137 |
check_every_250_days(date.unwrap()); |
| 138 |
} |
| 139 |
|
| 140 |
#[test] |
| 141 |
fn test_indian_continuity() { |
| 142 |
let date = Date::try_new_indian_date(-10, 1, 1); |
| 143 |
check_continuity(date.unwrap()); |
| 144 |
let date = Date::try_new_indian_date(-300, 1, 1); |
| 145 |
check_every_250_days(date.unwrap()); |
| 146 |
} |
| 147 |
|
| 148 |
#[test] |
| 149 |
fn test_islamic_civil_continuity() { |
| 150 |
let cal = crate::islamic::IslamicCivil::new(); |
| 151 |
let cal = Ref(&cal); |
| 152 |
let date = Date::try_new_islamic_civil_date_with_calendar(-10, 1, 1, cal); |
| 153 |
check_continuity(date.unwrap()); |
| 154 |
let date = Date::try_new_islamic_civil_date_with_calendar(-300, 1, 1, cal); |
| 155 |
check_every_250_days(date.unwrap()); |
| 156 |
} |
| 157 |
|
| 158 |
#[test] |
| 159 |
fn test_islamic_observational_continuity() { |
| 160 |
#[cfg(feature = "logging")] |
| 161 |
let _ = simple_logger::SimpleLogger::new().env().init(); |
| 162 |
let cal = crate::islamic::IslamicObservational::new(); |
| 163 |
let cal = Ref(&cal); |
| 164 |
let date = Date::try_new_observational_islamic_date(-10, 1, 1, cal); |
| 165 |
check_continuity(date.unwrap()); |
| 166 |
let date = Date::try_new_observational_islamic_date(-300, 1, 1, cal); |
| 167 |
check_every_250_days(date.unwrap()); |
| 168 |
} |
| 169 |
|
| 170 |
#[test] |
| 171 |
fn test_islamic_tabular_continuity() { |
| 172 |
let cal = crate::islamic::IslamicTabular::new(); |
| 173 |
let cal = Ref(&cal); |
| 174 |
let date = Date::try_new_islamic_tabular_date_with_calendar(-10, 1, 1, cal); |
| 175 |
check_continuity(date.unwrap()); |
| 176 |
let date = Date::try_new_islamic_tabular_date_with_calendar(-300, 1, 1, cal); |
| 177 |
check_every_250_days(date.unwrap()); |
| 178 |
} |
| 179 |
|
| 180 |
#[test] |
| 181 |
fn test_islamic_umm_al_qura_continuity() { |
| 182 |
#[cfg(feature = "logging")] |
| 183 |
let _ = simple_logger::SimpleLogger::new().env().init(); |
| 184 |
let cal = crate::islamic::IslamicUmmAlQura::new(); |
| 185 |
let cal = Ref(&cal); |
| 186 |
let date = Date::try_new_ummalqura_date(-10, 1, 1, cal); |
| 187 |
check_continuity(date.unwrap()); |
| 188 |
let date = Date::try_new_ummalqura_date(-300, 1, 1, cal); |
| 189 |
check_every_250_days(date.unwrap()); |
| 190 |
} |
| 191 |
|
| 192 |
#[test] |
| 193 |
fn test_iso_continuity() { |
| 194 |
let date = Date::try_new_iso_date(-10, 1, 1); |
| 195 |
check_continuity(date.unwrap()); |
| 196 |
let date = Date::try_new_iso_date(-300, 1, 1); |
| 197 |
check_every_250_days(date.unwrap()); |
| 198 |
} |
| 199 |
|
| 200 |
#[test] |
| 201 |
fn test_japanese_continuity() { |
| 202 |
use crate::types::Era; |
| 203 |
use tinystr::tinystr; |
| 204 |
let cal = crate::japanese::Japanese::new(); |
| 205 |
let cal = Ref(&cal); |
| 206 |
let date = Date::try_new_japanese_date(Era(tinystr!(16, "heisei")), 20, 1, 1, cal); |
| 207 |
check_continuity(date.unwrap()); |
| 208 |
let date = Date::try_new_japanese_date(Era(tinystr!(16, "bce")), 500, 1, 1, cal); |
| 209 |
check_every_250_days(date.unwrap()); |
| 210 |
} |
| 211 |
|
| 212 |
#[test] |
| 213 |
fn test_japanese_extended_continuity() { |
| 214 |
use crate::types::Era; |
| 215 |
use tinystr::tinystr; |
| 216 |
let cal = crate::japanese::JapaneseExtended::new(); |
| 217 |
let cal = Ref(&cal); |
| 218 |
let date = Date::try_new_japanese_extended_date(Era(tinystr!(16, "heisei")), 20, 1, 1, cal); |
| 219 |
check_continuity(date.unwrap()); |
| 220 |
let date = Date::try_new_japanese_extended_date(Era(tinystr!(16, "bce")), 500, 1, 1, cal); |
| 221 |
check_every_250_days(date.unwrap()); |
| 222 |
} |
| 223 |
|
| 224 |
#[test] |
| 225 |
fn test_persian_continuity() { |
| 226 |
let date = Date::try_new_persian_date(-10, 1, 1); |
| 227 |
check_continuity(date.unwrap()); |
| 228 |
let date = Date::try_new_persian_date(-300, 1, 1); |
| 229 |
check_every_250_days(date.unwrap()); |
| 230 |
} |
| 231 |
|
| 232 |
#[test] |
| 233 |
fn test_roc_continuity() { |
| 234 |
let date = Date::try_new_roc_date(-10, 1, 1); |
| 235 |
check_continuity(date.unwrap()); |
| 236 |
let date = Date::try_new_roc_date(-300, 1, 1); |
| 237 |
check_every_250_days(date.unwrap()); |
| 238 |
} |