| 1 |
use num_conv::prelude::*; |
| 2 |
use quickcheck::{Arbitrary, TestResult}; |
| 3 |
use quickcheck_macros::quickcheck; |
| 4 |
use time::macros::{format_description, time}; |
| 5 |
use time::Weekday::*; |
| 6 |
use time::{Date, Duration, Month, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset, Weekday}; |
| 7 |
|
| 8 |
macro_rules! test_shrink { |
| 9 |
($type:ty, |
| 10 |
$fn_name:ident, |
| 11 |
$($method:ident()).+ |
| 12 |
$(, min=$min_value:literal)? |
| 13 |
) => { |
| 14 |
#[quickcheck] |
| 15 |
fn $fn_name(v: $type) -> TestResult { |
| 16 |
let method_value = v.$($method()).+; |
| 17 |
if method_value == test_shrink!(@min_or_zero $($min_value)?) { |
| 18 |
TestResult::discard() |
| 19 |
} else { |
| 20 |
TestResult::from_bool(v.shrink().any(|shrunk| |
| 21 |
if method_value > 0 { |
| 22 |
shrunk.$($method()).+ < v.$($method()).+ |
| 23 |
} else { |
| 24 |
shrunk.$($method()).+ > v.$($method()).+ |
| 25 |
} |
| 26 |
)) |
| 27 |
} |
| 28 |
} |
| 29 |
}; |
| 30 |
(@min_or_zero) => { 0 }; |
| 31 |
(@min_or_zero $min:literal) => { $min }; |
| 32 |
} |
| 33 |
|
| 34 |
macro_rules! no_panic { |
| 35 |
($($x:tt)*) => { |
| 36 |
std::panic::catch_unwind(|| { |
| 37 |
$($x)* |
| 38 |
}) |
| 39 |
.is_ok() |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
#[quickcheck] |
| 44 |
fn date_yo_roundtrip(d: Date) -> bool { |
| 45 |
Date::from_ordinal_date(d.year(), d.ordinal()) == Ok(d) |
| 46 |
} |
| 47 |
|
| 48 |
#[quickcheck] |
| 49 |
fn date_ymd_roundtrip(d: Date) -> bool { |
| 50 |
Date::from_calendar_date(d.year(), d.month(), d.day()) == Ok(d) |
| 51 |
} |
| 52 |
|
| 53 |
#[quickcheck] |
| 54 |
fn date_ywd_roundtrip(d: Date) -> bool { |
| 55 |
let (year, week, weekday) = d.to_iso_week_date(); |
| 56 |
Date::from_iso_week_date(year, week, weekday) == Ok(d) |
| 57 |
} |
| 58 |
|
| 59 |
#[quickcheck] |
| 60 |
fn date_format_century_last_two_equivalent(d: Date) -> bool { |
| 61 |
let split_format = format_description!("[year repr:century][year repr:last_two]-[month]-[day]"); |
| 62 |
let split = d.format(&split_format).expect("formatting failed"); |
| 63 |
|
| 64 |
let combined_format = format_description!("[year]-[month]-[day]"); |
| 65 |
let combined = d.format(&combined_format).expect("formatting failed"); |
| 66 |
|
| 67 |
split == combined |
| 68 |
} |
| 69 |
|
| 70 |
#[quickcheck] |
| 71 |
fn date_parse_century_last_two_equivalent_extended(d: Date) -> TestResult { |
| 72 |
// With the extended range, there is an ambiguity when parsing a year with fewer than six |
| 73 |
// digits, as the first four are consumed by the century, leaving at most one for the last |
| 74 |
// two digits. |
| 75 |
if !matches!(d.year().unsigned_abs().to_string().len(), 6) { |
| 76 |
return TestResult::discard(); |
| 77 |
} |
| 78 |
|
| 79 |
let split_format = format_description!("[year repr:century][year repr:last_two]-[month]-[day]"); |
| 80 |
let combined_format = format_description!("[year]-[month]-[day]"); |
| 81 |
let combined = d.format(&combined_format).expect("formatting failed"); |
| 82 |
|
| 83 |
TestResult::from_bool(Date::parse(&combined, &split_format).expect("parsing failed") == d) |
| 84 |
} |
| 85 |
|
| 86 |
#[quickcheck] |
| 87 |
fn date_parse_century_last_two_equivalent_standard(d: Date) -> TestResult { |
| 88 |
// With the standard range, the year must be at most four digits. |
| 89 |
if !matches!(d.year(), -9999..=9999) { |
| 90 |
return TestResult::discard(); |
| 91 |
} |
| 92 |
|
| 93 |
let split_format = format_description!( |
| 94 |
"[year repr:century range:standard][year repr:last_two range:standard]-[month]-[day]" |
| 95 |
); |
| 96 |
let combined_format = format_description!("[year range:standard]-[month]-[day]"); |
| 97 |
let combined = d.format(&combined_format).expect("formatting failed"); |
| 98 |
|
| 99 |
TestResult::from_bool(Date::parse(&combined, &split_format).expect("parsing failed") == d) |
| 100 |
} |
| 101 |
|
| 102 |
#[quickcheck] |
| 103 |
fn julian_day_roundtrip(d: Date) -> bool { |
| 104 |
Date::from_julian_day(d.to_julian_day()) == Ok(d) |
| 105 |
} |
| 106 |
|
| 107 |
#[quickcheck] |
| 108 |
fn duration_roundtrip(d: Duration) -> bool { |
| 109 |
Duration::new(d.whole_seconds(), d.subsec_nanoseconds()) == d |
| 110 |
} |
| 111 |
|
| 112 |
#[quickcheck] |
| 113 |
fn time_roundtrip(t: Time) -> bool { |
| 114 |
Time::from_hms_nano(t.hour(), t.minute(), t.second(), t.nanosecond()) == Ok(t) |
| 115 |
} |
| 116 |
|
| 117 |
#[quickcheck] |
| 118 |
fn primitive_date_time_roundtrip(a: PrimitiveDateTime) -> bool { |
| 119 |
PrimitiveDateTime::new(a.date(), a.time()) == a |
| 120 |
} |
| 121 |
|
| 122 |
#[quickcheck] |
| 123 |
fn utc_offset_roundtrip(o: UtcOffset) -> bool { |
| 124 |
let (hours, minutes, seconds) = o.as_hms(); |
| 125 |
UtcOffset::from_hms(hours, minutes, seconds) == Ok(o) |
| 126 |
} |
| 127 |
|
| 128 |
#[quickcheck] |
| 129 |
fn offset_date_time_roundtrip(a: OffsetDateTime) -> TestResult { |
| 130 |
// Values near the edge of what is allowed may panic if the conversion brings the underlying |
| 131 |
// value outside the valid range of values. |
| 132 |
if a.date() == Date::MIN || a.date() == Date::MAX { |
| 133 |
return TestResult::discard(); |
| 134 |
} |
| 135 |
|
| 136 |
TestResult::from_bool(PrimitiveDateTime::new(a.date(), a.time()).assume_offset(a.offset()) == a) |
| 137 |
} |
| 138 |
|
| 139 |
#[quickcheck] |
| 140 |
fn unix_timestamp_roundtrip(odt: OffsetDateTime) -> TestResult { |
| 141 |
match odt.date() { |
| 142 |
Date::MIN | Date::MAX => TestResult::discard(), |
| 143 |
_ => TestResult::from_bool({ |
| 144 |
// nanoseconds are not stored in the basic Unix timestamp |
| 145 |
let odt = odt - Duration::nanoseconds(odt.nanosecond().into()); |
| 146 |
OffsetDateTime::from_unix_timestamp(odt.unix_timestamp()) == Ok(odt) |
| 147 |
}), |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
#[quickcheck] |
| 152 |
fn unix_timestamp_nanos_roundtrip(odt: OffsetDateTime) -> TestResult { |
| 153 |
match odt.date() { |
| 154 |
Date::MIN | Date::MAX => TestResult::discard(), |
| 155 |
_ => TestResult::from_bool( |
| 156 |
OffsetDateTime::from_unix_timestamp_nanos(odt.unix_timestamp_nanos()) == Ok(odt), |
| 157 |
), |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
#[quickcheck] |
| 162 |
fn number_from_monday_roundtrip(w: Weekday) -> bool { |
| 163 |
Monday.nth_next(w.number_from_monday() + 7 - 1) == w |
| 164 |
} |
| 165 |
|
| 166 |
#[quickcheck] |
| 167 |
fn number_from_sunday_roundtrip(w: Weekday) -> bool { |
| 168 |
Sunday.nth_next(w.number_from_sunday() + 7 - 1) == w |
| 169 |
} |
| 170 |
|
| 171 |
#[quickcheck] |
| 172 |
fn number_days_from_monday_roundtrip(w: Weekday) -> bool { |
| 173 |
Monday.nth_next(w.number_days_from_monday()) == w |
| 174 |
} |
| 175 |
|
| 176 |
#[quickcheck] |
| 177 |
fn number_days_from_sunday_roundtrip(w: Weekday) -> bool { |
| 178 |
Sunday.nth_next(w.number_days_from_sunday()) == w |
| 179 |
} |
| 180 |
|
| 181 |
#[quickcheck] |
| 182 |
fn weekday_supports_arbitrary(w: Weekday) -> bool { |
| 183 |
(1..=7).contains(&w.number_from_monday()) |
| 184 |
} |
| 185 |
|
| 186 |
#[quickcheck] |
| 187 |
fn weekday_can_shrink(w: Weekday) -> bool { |
| 188 |
match w { |
| 189 |
Monday => w.shrink().next().is_none(), |
| 190 |
_ => w.shrink().next() == Some(w.previous()), |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
#[quickcheck] |
| 195 |
fn month_supports_arbitrary(m: Month) -> bool { |
| 196 |
(1..=12).contains(&u8::from(m)) |
| 197 |
} |
| 198 |
|
| 199 |
#[quickcheck] |
| 200 |
fn month_can_shrink(m: Month) -> bool { |
| 201 |
match m { |
| 202 |
Month::January => m.shrink().next().is_none(), |
| 203 |
_ => m.shrink().next() == Some(m.previous()), |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
#[quickcheck] |
| 208 |
fn date_replace_year(date: Date, year: i32) -> bool { |
| 209 |
date.replace_year(year) == Date::from_calendar_date(year, date.month(), date.day()) |
| 210 |
} |
| 211 |
|
| 212 |
#[quickcheck] |
| 213 |
fn date_replace_month(date: Date, month: Month) -> bool { |
| 214 |
date.replace_month(month) == Date::from_calendar_date(date.year(), month, date.day()) |
| 215 |
} |
| 216 |
|
| 217 |
#[quickcheck] |
| 218 |
fn date_replace_day(date: Date, day: u8) -> bool { |
| 219 |
date.replace_day(day) == Date::from_calendar_date(date.year(), date.month(), day) |
| 220 |
} |
| 221 |
|
| 222 |
#[quickcheck] |
| 223 |
fn time_replace_hour(time: Time, hour: u8) -> bool { |
| 224 |
time.replace_hour(hour) |
| 225 |
== Time::from_hms_nano(hour, time.minute(), time.second(), time.nanosecond()) |
| 226 |
} |
| 227 |
|
| 228 |
#[quickcheck] |
| 229 |
fn pdt_replace_year(pdt: PrimitiveDateTime, year: i32) -> bool { |
| 230 |
pdt.replace_year(year) |
| 231 |
== Date::from_calendar_date(year, pdt.month(), pdt.day()) |
| 232 |
.map(|date| date.with_time(pdt.time())) |
| 233 |
} |
| 234 |
|
| 235 |
#[quickcheck] |
| 236 |
fn pdt_replace_month(pdt: PrimitiveDateTime, month: Month) -> bool { |
| 237 |
pdt.replace_month(month) |
| 238 |
== Date::from_calendar_date(pdt.year(), month, pdt.day()) |
| 239 |
.map(|date| date.with_time(pdt.time())) |
| 240 |
} |
| 241 |
|
| 242 |
#[quickcheck] |
| 243 |
fn pdt_replace_day(pdt: PrimitiveDateTime, day: u8) -> bool { |
| 244 |
pdt.replace_day(day) |
| 245 |
== Date::from_calendar_date(pdt.year(), pdt.month(), day) |
| 246 |
.map(|date| date.with_time(pdt.time())) |
| 247 |
} |
| 248 |
|
| 249 |
// Regression test for #481 |
| 250 |
#[quickcheck] |
| 251 |
fn time_sub_time_no_panic(time_a: Time, time_b: Time) -> bool { |
| 252 |
no_panic! { |
| 253 |
let _ = time_a - time_b; |
| 254 |
let _ = time_b - time_a; |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
#[quickcheck] |
| 259 |
fn time_duration_until_since_range(time_a: Time, time_b: Time) -> bool { |
| 260 |
let a_until_b = time_a.duration_until(time_b); |
| 261 |
let b_until_a = time_b.duration_until(time_a); |
| 262 |
|
| 263 |
let a_since_b = time_a.duration_since(time_b); |
| 264 |
let b_since_a = time_b.duration_since(time_a); |
| 265 |
|
| 266 |
(Duration::ZERO..Duration::DAY).contains(&a_until_b) |
| 267 |
&& (Duration::ZERO..Duration::DAY).contains(&b_until_a) |
| 268 |
&& (Duration::ZERO..Duration::DAY).contains(&a_since_b) |
| 269 |
&& (Duration::ZERO..Duration::DAY).contains(&b_since_a) |
| 270 |
} |
| 271 |
|
| 272 |
#[quickcheck] |
| 273 |
fn time_duration_until_since_arithmetic(time_a: Time, time_b: Time) -> bool { |
| 274 |
let a_until_b = time_a.duration_until(time_b); |
| 275 |
let b_until_a = time_b.duration_until(time_a); |
| 276 |
|
| 277 |
let a_since_b = time_a.duration_since(time_b); |
| 278 |
let b_since_a = time_b.duration_since(time_a); |
| 279 |
|
| 280 |
time_a + a_until_b == time_b |
| 281 |
&& time_b + b_until_a == time_a |
| 282 |
&& time_b + a_since_b == time_a |
| 283 |
&& time_a + b_since_a == time_b |
| 284 |
} |
| 285 |
|
| 286 |
#[quickcheck] |
| 287 |
fn from_julian_day_no_panic(julian_day: i32) -> TestResult { |
| 288 |
if !(Date::MIN.to_julian_day()..=Date::MAX.to_julian_day()).contains(&julian_day) { |
| 289 |
return TestResult::discard(); |
| 290 |
} |
| 291 |
|
| 292 |
TestResult::from_bool( |
| 293 |
std::panic::catch_unwind(|| { |
| 294 |
let _ = Date::from_julian_day(julian_day); |
| 295 |
}) |
| 296 |
.is_ok(), |
| 297 |
) |
| 298 |
} |
| 299 |
|
| 300 |
#[quickcheck] |
| 301 |
fn odt_eq_no_panic(left: OffsetDateTime, right: OffsetDateTime) -> bool { |
| 302 |
no_panic! { |
| 303 |
let _ = left == right; |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
#[quickcheck] |
| 308 |
fn odt_ord_no_panic(left: OffsetDateTime, right: OffsetDateTime) -> bool { |
| 309 |
no_panic! { |
| 310 |
let _ = left < right; |
| 311 |
let _ = left > right; |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
#[quickcheck] |
| 316 |
fn odt_sub_no_panic(left: OffsetDateTime, right: OffsetDateTime) -> bool { |
| 317 |
no_panic! { |
| 318 |
let _ = left - right; |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
#[quickcheck] |
| 323 |
fn odt_to_offset_no_panic(odt: OffsetDateTime, offset: UtcOffset) -> TestResult { |
| 324 |
let offset_difference = offset.whole_seconds() - odt.offset().whole_seconds(); |
| 325 |
if Date::MIN |
| 326 |
.midnight() |
| 327 |
.assume_utc() |
| 328 |
.checked_add(Duration::seconds(offset_difference.extend())) |
| 329 |
.is_none() |
| 330 |
|| Date::MAX |
| 331 |
.with_time(time!(23:59:59.999_999_999)) |
| 332 |
.assume_utc() |
| 333 |
.checked_add(Duration::seconds(offset_difference.extend())) |
| 334 |
.is_none() |
| 335 |
{ |
| 336 |
return TestResult::discard(); |
| 337 |
} |
| 338 |
|
| 339 |
TestResult::from_bool(no_panic! { |
| 340 |
let _ = odt.to_offset(offset); |
| 341 |
}) |
| 342 |
} |
| 343 |
|
| 344 |
#[quickcheck] |
| 345 |
fn odt_replace_offset_no_panic(odt: OffsetDateTime, offset: UtcOffset) -> TestResult { |
| 346 |
if Date::MIN |
| 347 |
.midnight() |
| 348 |
.assume_offset(odt.offset()) |
| 349 |
.checked_add(Duration::seconds(offset.whole_seconds().extend())) |
| 350 |
.is_none() |
| 351 |
|| Date::MAX |
| 352 |
.with_time(time!(23:59:59.999_999_999)) |
| 353 |
.assume_offset(odt.offset()) |
| 354 |
.checked_add(Duration::seconds(offset.whole_seconds().extend())) |
| 355 |
.is_none() |
| 356 |
{ |
| 357 |
return TestResult::discard(); |
| 358 |
} |
| 359 |
|
| 360 |
TestResult::from_bool(no_panic! { |
| 361 |
let _ = odt.replace_offset(offset); |
| 362 |
}) |
| 363 |
} |
| 364 |
|
| 365 |
test_shrink!(Date, date_can_shrink_year, year()); |
| 366 |
test_shrink!(Date, date_can_shrink_ordinal, ordinal(), min = 1); |
| 367 |
|
| 368 |
test_shrink!(Duration, duration_can_shrink_seconds, whole_seconds()); |
| 369 |
test_shrink!(Duration, duration_can_shrink_ns, subsec_nanoseconds()); |
| 370 |
|
| 371 |
test_shrink!(Time, time_can_shrink_hour, hour()); |
| 372 |
test_shrink!(Time, time_can_shrink_minute, minute()); |
| 373 |
test_shrink!(Time, time_can_shrink_second, second()); |
| 374 |
test_shrink!(Time, time_can_shrink_nanosecond, nanosecond()); |
| 375 |
|
| 376 |
test_shrink!( |
| 377 |
PrimitiveDateTime, |
| 378 |
primitive_date_time_can_shrink_year, |
| 379 |
year() |
| 380 |
); |
| 381 |
test_shrink!( |
| 382 |
PrimitiveDateTime, |
| 383 |
primitive_date_time_can_shrink_ordinal, |
| 384 |
ordinal(), |
| 385 |
min = 1 |
| 386 |
); |
| 387 |
test_shrink!( |
| 388 |
PrimitiveDateTime, |
| 389 |
primitive_date_time_can_shrink_hour, |
| 390 |
hour() |
| 391 |
); |
| 392 |
test_shrink!( |
| 393 |
PrimitiveDateTime, |
| 394 |
primitive_date_time_can_shrink_minute, |
| 395 |
minute() |
| 396 |
); |
| 397 |
test_shrink!( |
| 398 |
PrimitiveDateTime, |
| 399 |
primitive_date_time_can_shrink_second, |
| 400 |
second() |
| 401 |
); |
| 402 |
test_shrink!( |
| 403 |
PrimitiveDateTime, |
| 404 |
primitive_date_time_can_shrink_nanosecond, |
| 405 |
nanosecond() |
| 406 |
); |
| 407 |
|
| 408 |
test_shrink!(UtcOffset, utc_offset_can_shrink, whole_seconds()); |
| 409 |
|
| 410 |
test_shrink!( |
| 411 |
OffsetDateTime, |
| 412 |
offset_date_time_can_shrink_offset, |
| 413 |
offset().whole_seconds() |
| 414 |
); |
| 415 |
test_shrink!(OffsetDateTime, offset_date_time_can_shrink_year, year()); |
| 416 |
test_shrink!( |
| 417 |
OffsetDateTime, |
| 418 |
offset_date_time_can_shrink_ordinal, |
| 419 |
ordinal(), |
| 420 |
min = 1 |
| 421 |
); |
| 422 |
test_shrink!(OffsetDateTime, offset_date_time_can_shrink_hour, hour()); |
| 423 |
test_shrink!(OffsetDateTime, offset_date_time_can_shrink_minute, minute()); |
| 424 |
test_shrink!(OffsetDateTime, offset_date_time_can_shrink_second, second()); |
| 425 |
test_shrink!( |
| 426 |
OffsetDateTime, |
| 427 |
offset_date_time_can_shrink_nanosecond, |
| 428 |
nanosecond() |
| 429 |
); |