Debian Codegraph @

debian/rust-vendor/time/tests/integration/quickcheck.rs

raw
1 use num_conv::prelude::*;
2 use quickcheck::{Arbitrary, TestResult};
3 use quickcheck_macros::quickcheck;
4 use time::Weekday::*;
5 use time::macros::{format_description, time};
6 use time::{Date, Month, OffsetDateTime, PlainDateTime, SignedDuration, 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: SignedDuration) -> bool {
109 SignedDuration::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 plain_date_time_roundtrip(a: PlainDateTime) -> bool {
119 PlainDateTime::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(PlainDateTime::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 - SignedDuration::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: PlainDateTime, 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: PlainDateTime, 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: PlainDateTime, 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 (SignedDuration::ZERO..SignedDuration::DAY).contains(&a_until_b)
267 && (SignedDuration::ZERO..SignedDuration::DAY).contains(&b_until_a)
268 && (SignedDuration::ZERO..SignedDuration::DAY).contains(&a_since_b)
269 && (SignedDuration::ZERO..SignedDuration::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(SignedDuration::seconds(offset_difference.widen()))
329 .is_none()
330 || Date::MAX
331 .with_time(time!(23:59:59.999_999_999))
332 .assume_utc()
333 .checked_add(SignedDuration::seconds(offset_difference.widen()))
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(SignedDuration::seconds(offset.whole_seconds().widen()))
350 .is_none()
351 || Date::MAX
352 .with_time(time!(23:59:59.999_999_999))
353 .assume_offset(odt.offset())
354 .checked_add(SignedDuration::seconds(offset.whole_seconds().widen()))
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!(SignedDuration, duration_can_shrink_seconds, whole_seconds());
369 test_shrink!(SignedDuration, 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!(PlainDateTime, plain_date_time_can_shrink_year, year());
377 test_shrink!(
378 PlainDateTime,
379 plain_date_time_can_shrink_ordinal,
380 ordinal(),
381 min = 1
382 );
383 test_shrink!(PlainDateTime, plain_date_time_can_shrink_hour, hour());
384 test_shrink!(PlainDateTime, plain_date_time_can_shrink_minute, minute());
385 test_shrink!(PlainDateTime, plain_date_time_can_shrink_second, second());
386 test_shrink!(
387 PlainDateTime,
388 plain_date_time_can_shrink_nanosecond,
389 nanosecond()
390 );
391
392 test_shrink!(UtcOffset, utc_offset_can_shrink, whole_seconds());
393
394 test_shrink!(
395 OffsetDateTime,
396 offset_date_time_can_shrink_offset,
397 offset().whole_seconds()
398 );
399 test_shrink!(OffsetDateTime, offset_date_time_can_shrink_year, year());
400 test_shrink!(
401 OffsetDateTime,
402 offset_date_time_can_shrink_ordinal,
403 ordinal(),
404 min = 1
405 );
406 test_shrink!(OffsetDateTime, offset_date_time_can_shrink_hour, hour());
407 test_shrink!(OffsetDateTime, offset_date_time_can_shrink_minute, minute());
408 test_shrink!(OffsetDateTime, offset_date_time_can_shrink_second, second());
409 test_shrink!(
410 OffsetDateTime,
411 offset_date_time_can_shrink_nanosecond,
412 nanosecond()
413 );