| 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::{types::*, *}; |
| 6 |
|
| 7 |
fn check_continuity<A: AsCalendar + Clone>(mut date: Date<A>, years_to_check: usize) { |
| 8 |
let duration = DateDuration::for_days(1); |
| 9 |
|
| 10 |
let mut rata_die = date.to_rata_die(); |
| 11 |
let mut weekday = date.weekday(); |
| 12 |
let mut year = date.year(); |
| 13 |
let mut is_in_leap_year = date.is_in_leap_year(); |
| 14 |
|
| 15 |
for _ in 0..(366 * years_to_check) { |
| 16 |
let next_date = date |
| 17 |
.try_added_with_options(duration, Default::default()) |
| 18 |
.unwrap(); |
| 19 |
let next_rata_die = next_date.to_rata_die(); |
| 20 |
assert_eq!(next_rata_die, rata_die + 1, "{next_date:?}"); |
| 21 |
let next_weekday = next_date.weekday(); |
| 22 |
let next_year = next_date.year(); |
| 23 |
let next_is_in_leap_year = next_date.is_in_leap_year(); |
| 24 |
assert_eq!( |
| 25 |
(next_weekday as usize) % 7, |
| 26 |
(weekday as usize + 1) % 7, |
| 27 |
"{next_date:?}" |
| 28 |
); |
| 29 |
if year == next_year { |
| 30 |
assert_eq!(is_in_leap_year, next_is_in_leap_year, "{next_date:?}"); |
| 31 |
} |
| 32 |
date = next_date; |
| 33 |
rata_die = next_rata_die; |
| 34 |
weekday = next_weekday; |
| 35 |
year = next_year; |
| 36 |
is_in_leap_year = next_is_in_leap_year; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
fn check_every_250_days<C: Calendar + Copy>(mut date: Date<C>, iters: usize) { |
| 41 |
let duration = DateDuration::for_days(250); |
| 42 |
|
| 43 |
let mut rata_die = date.to_rata_die(); |
| 44 |
|
| 45 |
for _ in 0..iters { |
| 46 |
let next_date = date |
| 47 |
.try_added_with_options(duration, Default::default()) |
| 48 |
.unwrap(); |
| 49 |
let next_rata_die = next_date.to_rata_die(); |
| 50 |
assert_eq!(next_rata_die, rata_die + 250, "{next_date:?}"); |
| 51 |
let next_date_roundtrip = Date::from_rata_die(next_rata_die, *next_date.calendar()); |
| 52 |
assert_eq!(next_date, next_date_roundtrip, "{next_date:?}"); |
| 53 |
date = next_date; |
| 54 |
rata_die = next_rata_die; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
#[test] |
| 59 |
fn test_buddhist_continuity() { |
| 60 |
let date = Date::try_new_buddhist(-10, 1, 1); |
| 61 |
check_continuity(date.unwrap(), 20); |
| 62 |
let date = Date::try_new_buddhist(-300, 1, 1); |
| 63 |
check_every_250_days(date.unwrap(), 2000); |
| 64 |
} |
| 65 |
|
| 66 |
#[test] |
| 67 |
fn test_chinese_continuity() { |
| 68 |
let date = Date::try_new_chinese_traditional(-10, Month::new(1), 1); |
| 69 |
check_continuity(date.unwrap(), 20); |
| 70 |
let date = Date::try_new_chinese_traditional(-300, Month::new(1), 1); |
| 71 |
check_every_250_days(date.unwrap(), 2000); |
| 72 |
let date = Date::try_new_chinese_traditional(-9999, Month::new(1), 1); |
| 73 |
check_every_250_days(date.unwrap(), 2000); |
| 74 |
|
| 75 |
let date = Date::try_new_chinese_traditional(1899, Month::new(1), 1); |
| 76 |
check_continuity(date.unwrap(), 20); |
| 77 |
|
| 78 |
let date = Date::try_new_chinese_traditional(2099, Month::new(1), 1); |
| 79 |
check_continuity(date.unwrap(), 20); |
| 80 |
} |
| 81 |
|
| 82 |
#[test] |
| 83 |
fn test_coptic_continuity() { |
| 84 |
let date = Date::try_new_coptic(-10, 1, 1); |
| 85 |
check_continuity(date.unwrap(), 20); |
| 86 |
let date = Date::try_new_coptic(-300, 1, 1); |
| 87 |
check_every_250_days(date.unwrap(), 2000); |
| 88 |
} |
| 89 |
|
| 90 |
#[test] |
| 91 |
fn test_korean_continuity() { |
| 92 |
let date = Date::try_new_korean_traditional(-10, Month::new(1), 1); |
| 93 |
check_continuity(date.unwrap(), 20); |
| 94 |
let date = Date::try_new_korean_traditional(-300, Month::new(1), 1); |
| 95 |
check_every_250_days(date.unwrap(), 2000); |
| 96 |
|
| 97 |
let date = Date::try_new_korean_traditional(1900, Month::new(1), 1); |
| 98 |
check_continuity(date.unwrap(), 20); |
| 99 |
|
| 100 |
let date = Date::try_new_korean_traditional(2100, Month::new(1), 1); |
| 101 |
check_continuity(date.unwrap(), 20); |
| 102 |
} |
| 103 |
|
| 104 |
#[test] |
| 105 |
fn test_ethiopian_continuity() { |
| 106 |
use cal::EthiopianEraStyle::*; |
| 107 |
let date = Date::try_new_ethiopian(AmeteMihret, -10, 1, 1); |
| 108 |
check_continuity(date.unwrap(), 20); |
| 109 |
let date = Date::try_new_ethiopian(AmeteMihret, -300, 1, 1); |
| 110 |
check_every_250_days(date.unwrap(), 2000); |
| 111 |
} |
| 112 |
|
| 113 |
#[test] |
| 114 |
fn test_ethiopian_amete_alem_continuity() { |
| 115 |
use cal::EthiopianEraStyle::*; |
| 116 |
let date = Date::try_new_ethiopian(AmeteAlem, -10, 1, 1); |
| 117 |
check_continuity(date.unwrap(), 20); |
| 118 |
let date = Date::try_new_ethiopian(AmeteAlem, -300, 1, 1); |
| 119 |
check_every_250_days(date.unwrap(), 2000); |
| 120 |
} |
| 121 |
|
| 122 |
#[test] |
| 123 |
fn test_gregorian_continuity() { |
| 124 |
let date = Date::try_new_gregorian(-10, 1, 1); |
| 125 |
check_continuity(date.unwrap(), 20); |
| 126 |
let date = Date::try_new_gregorian(-300, 1, 1); |
| 127 |
check_every_250_days(date.unwrap(), 2000); |
| 128 |
} |
| 129 |
|
| 130 |
#[test] |
| 131 |
fn test_hebrew_continuity() { |
| 132 |
let date = Date::try_new_hebrew_v2(-10, Month::new(1), 1); |
| 133 |
check_continuity(date.unwrap(), 20); |
| 134 |
let date = Date::try_new_hebrew_v2(-300, Month::new(1), 1); |
| 135 |
check_every_250_days(date.unwrap(), 2000); |
| 136 |
} |
| 137 |
|
| 138 |
#[test] |
| 139 |
fn test_indian_continuity() { |
| 140 |
let date = Date::try_new_indian(-10, 1, 1); |
| 141 |
check_continuity(date.unwrap(), 20); |
| 142 |
let date = Date::try_new_indian(-300, 1, 1); |
| 143 |
check_every_250_days(date.unwrap(), 2000); |
| 144 |
} |
| 145 |
|
| 146 |
#[test] |
| 147 |
fn test_hijri_civil_continuity() { |
| 148 |
let cal = cal::Hijri::new_tabular( |
| 149 |
cal::hijri::TabularAlgorithmLeapYears::TypeII, |
| 150 |
cal::hijri::TabularAlgorithmEpoch::Friday, |
| 151 |
); |
| 152 |
let date = Date::try_new_hijri_with_calendar(-10, 1, 1, cal); |
| 153 |
check_continuity(date.unwrap(), 20); |
| 154 |
let date = Date::try_new_hijri_with_calendar(-300, 1, 1, cal); |
| 155 |
check_every_250_days(date.unwrap(), 2000); |
| 156 |
} |
| 157 |
|
| 158 |
#[test] |
| 159 |
#[allow(deprecated)] |
| 160 |
fn test_hijri_simulated_mecca_continuity() { |
| 161 |
#[cfg(feature = "logging")] |
| 162 |
let _ = simple_logger::SimpleLogger::new().env().init(); |
| 163 |
let cal = cal::Hijri::new_simulated_mecca(); |
| 164 |
let date = Date::try_new_hijri_with_calendar(-10, 1, 1, cal); |
| 165 |
check_continuity(date.unwrap(), 20); |
| 166 |
let date = Date::try_new_hijri_with_calendar(1290, 1, 1, cal); |
| 167 |
check_continuity(date.unwrap(), 20); |
| 168 |
let date = Date::try_new_hijri_with_calendar(1590, 1, 1, cal); |
| 169 |
check_continuity(date.unwrap(), 20); |
| 170 |
let date = Date::try_new_hijri_with_calendar(-300, 1, 1, cal); |
| 171 |
check_every_250_days(date.unwrap(), 2000); |
| 172 |
} |
| 173 |
|
| 174 |
#[test] |
| 175 |
fn test_hijri_tabular_continuity() { |
| 176 |
let cal = cal::Hijri::new_tabular( |
| 177 |
cal::hijri::TabularAlgorithmLeapYears::TypeII, |
| 178 |
cal::hijri::TabularAlgorithmEpoch::Thursday, |
| 179 |
); |
| 180 |
let date = Date::try_new_hijri_with_calendar(-10, 1, 1, cal); |
| 181 |
check_continuity(date.unwrap(), 20); |
| 182 |
let date = Date::try_new_hijri_with_calendar(-300, 1, 1, cal); |
| 183 |
check_every_250_days(date.unwrap(), 2000); |
| 184 |
} |
| 185 |
|
| 186 |
#[test] |
| 187 |
fn test_hijri_umm_al_qura_continuity() { |
| 188 |
#[cfg(feature = "logging")] |
| 189 |
let _ = simple_logger::SimpleLogger::new().env().init(); |
| 190 |
let cal = cal::Hijri::new_umm_al_qura(); |
| 191 |
let date = Date::try_new_hijri_with_calendar(-10, 1, 1, cal); |
| 192 |
check_continuity(date.unwrap(), 20); |
| 193 |
let date = Date::try_new_hijri_with_calendar(1290, 1, 1, cal); |
| 194 |
check_continuity(date.unwrap(), 20); |
| 195 |
let date = Date::try_new_hijri_with_calendar(1590, 1, 1, cal); |
| 196 |
check_continuity(date.unwrap(), 20); |
| 197 |
let date = Date::try_new_hijri_with_calendar(-300, 1, 1, cal); |
| 198 |
check_every_250_days(date.unwrap(), 2000); |
| 199 |
} |
| 200 |
|
| 201 |
#[test] |
| 202 |
fn test_iso_continuity() { |
| 203 |
let date = Date::try_new_iso(-10, 1, 1); |
| 204 |
check_continuity(date.unwrap(), 20); |
| 205 |
let date = Date::try_new_iso(-300, 1, 1); |
| 206 |
check_every_250_days(date.unwrap(), 2000); |
| 207 |
} |
| 208 |
|
| 209 |
#[test] |
| 210 |
fn test_japanese_continuity() { |
| 211 |
let date = Date::try_new_japanese_with_calendar("heisei", 20, 1, 1, cal::Japanese::new()); |
| 212 |
check_continuity(date.unwrap(), 20); |
| 213 |
let date = Date::try_new_japanese_with_calendar("bce", 500, 1, 1, cal::Japanese::new()); |
| 214 |
check_every_250_days(date.unwrap(), 2000); |
| 215 |
} |
| 216 |
|
| 217 |
#[test] |
| 218 |
fn test_persian_continuity() { |
| 219 |
let date = Date::try_new_persian(-10, 1, 1); |
| 220 |
check_continuity(date.unwrap(), 20); |
| 221 |
let date = Date::try_new_persian(-300, 1, 1); |
| 222 |
check_every_250_days(date.unwrap(), 2000); |
| 223 |
} |
| 224 |
|
| 225 |
#[test] |
| 226 |
fn test_roc_continuity() { |
| 227 |
let date = Date::try_new_roc(-10, 1, 1); |
| 228 |
check_continuity(date.unwrap(), 20); |
| 229 |
let date = Date::try_new_roc(-300, 1, 1); |
| 230 |
check_every_250_days(date.unwrap(), 2000); |
| 231 |
} |