| 1 |
mod numerical_duration { |
| 2 |
use rstest::rstest; |
| 3 |
use time::SignedDuration; |
| 4 |
use time::ext::NumericalDuration; |
| 5 |
|
| 6 |
#[rstest] |
| 7 |
#[case(5.nanoseconds(), SignedDuration::nanoseconds(5))] |
| 8 |
#[case(5.microseconds(), SignedDuration::microseconds(5))] |
| 9 |
#[case(5.milliseconds(), SignedDuration::milliseconds(5))] |
| 10 |
#[case(5.seconds(), SignedDuration::seconds(5))] |
| 11 |
#[case(5.minutes(), SignedDuration::minutes(5))] |
| 12 |
#[case(5.hours(), SignedDuration::hours(5))] |
| 13 |
#[case(5.days(), SignedDuration::days(5))] |
| 14 |
#[case(5.weeks(), SignedDuration::weeks(5))] |
| 15 |
fn unsigned(#[case] ext_trait: SignedDuration, #[case] expected: SignedDuration) { |
| 16 |
assert_eq!(ext_trait, expected); |
| 17 |
} |
| 18 |
|
| 19 |
#[rstest] |
| 20 |
#[case((-5).nanoseconds(), SignedDuration::nanoseconds(-5))] |
| 21 |
#[case((-5).microseconds(), SignedDuration::microseconds(-5))] |
| 22 |
#[case((-5).milliseconds(), SignedDuration::milliseconds(-5))] |
| 23 |
#[case((-5).seconds(), SignedDuration::seconds(-5))] |
| 24 |
#[case((-5).minutes(), SignedDuration::minutes(-5))] |
| 25 |
#[case((-5).hours(), SignedDuration::hours(-5))] |
| 26 |
#[case((-5).days(), SignedDuration::days(-5))] |
| 27 |
#[case((-5).weeks(), SignedDuration::weeks(-5))] |
| 28 |
fn signed(#[case] ext_trait: SignedDuration, #[case] expected: SignedDuration) { |
| 29 |
assert_eq!(ext_trait, expected); |
| 30 |
} |
| 31 |
|
| 32 |
#[rstest] |
| 33 |
#[case::truncate_not_round(1.9.nanoseconds(), SignedDuration::nanoseconds(1))] |
| 34 |
#[case(1.0.nanoseconds(), SignedDuration::nanoseconds(1))] |
| 35 |
#[case(1.0.microseconds(), SignedDuration::microseconds(1))] |
| 36 |
#[case(1.0.milliseconds(), SignedDuration::milliseconds(1))] |
| 37 |
#[case(1.0.seconds(), SignedDuration::seconds(1))] |
| 38 |
#[case(1.0.minutes(), SignedDuration::minutes(1))] |
| 39 |
#[case(1.0.hours(), SignedDuration::hours(1))] |
| 40 |
#[case(1.0.days(), SignedDuration::days(1))] |
| 41 |
#[case(1.0.weeks(), SignedDuration::weeks(1))] |
| 42 |
#[case(1.5.nanoseconds(), SignedDuration::nanoseconds(1))] |
| 43 |
#[case(1.5.microseconds(), SignedDuration::nanoseconds(1_500))] |
| 44 |
#[case(1.5.milliseconds(), SignedDuration::microseconds(1_500))] |
| 45 |
#[case(1.5.seconds(), SignedDuration::milliseconds(1_500))] |
| 46 |
#[case(1.5.minutes(), SignedDuration::seconds(90))] |
| 47 |
#[case(1.5.hours(), SignedDuration::minutes(90))] |
| 48 |
#[case(1.5.days(), SignedDuration::hours(36))] |
| 49 |
#[case(1.5.weeks(), SignedDuration::hours(252))] |
| 50 |
fn float(#[case] ext_trait: SignedDuration, #[case] expected: SignedDuration) { |
| 51 |
assert_eq!(ext_trait, expected); |
| 52 |
} |
| 53 |
|
| 54 |
#[rstest] |
| 55 |
#[case(2.seconds() + 500.milliseconds(), 2_500.milliseconds())] |
| 56 |
#[case(2.seconds() - 500.milliseconds(), 1_500.milliseconds())] |
| 57 |
fn arithmetic(#[case] arithmetic_: SignedDuration, #[case] expected: SignedDuration) { |
| 58 |
assert_eq!(arithmetic_, expected); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
mod numerical_std_duration { |
| 63 |
use std::time::Duration as StdDuration; |
| 64 |
|
| 65 |
use rstest::rstest; |
| 66 |
use time::ext::NumericalStdDuration; |
| 67 |
|
| 68 |
#[rstest] |
| 69 |
#[case(5.std_nanoseconds(), StdDuration::from_nanos(5))] |
| 70 |
#[case(5.std_microseconds(), StdDuration::from_micros(5))] |
| 71 |
#[case(5.std_milliseconds(), StdDuration::from_millis(5))] |
| 72 |
#[case(5.std_seconds(), StdDuration::from_secs(5))] |
| 73 |
#[case(5.std_minutes(), StdDuration::from_secs(5 * 60))] |
| 74 |
#[case(5.std_hours(), StdDuration::from_secs(5 * 3_600))] |
| 75 |
#[case(5.std_days(), StdDuration::from_secs(5 * 86_400))] |
| 76 |
#[case(5.std_weeks(), StdDuration::from_secs(5 * 604_800))] |
| 77 |
fn unsigned(#[case] ext_trait: StdDuration, #[case] expected: StdDuration) { |
| 78 |
assert_eq!(ext_trait, expected); |
| 79 |
} |
| 80 |
|
| 81 |
#[rstest] |
| 82 |
#[case::truncate_not_round(1.9.std_nanoseconds(), StdDuration::from_nanos(1))] |
| 83 |
#[case(1.0.std_nanoseconds(), StdDuration::from_nanos(1))] |
| 84 |
#[case(1.0.std_microseconds(), StdDuration::from_micros(1))] |
| 85 |
#[case(1.0.std_milliseconds(), StdDuration::from_millis(1))] |
| 86 |
#[case(1.0.std_seconds(), StdDuration::from_secs(1))] |
| 87 |
#[case(1.0.std_minutes(), StdDuration::from_secs(60))] |
| 88 |
#[case(1.0.std_hours(), StdDuration::from_secs(3_600))] |
| 89 |
#[case(1.0.std_days(), StdDuration::from_secs(86_400))] |
| 90 |
#[case(1.0.std_weeks(), StdDuration::from_secs(604_800))] |
| 91 |
#[case(1.5.std_nanoseconds(), StdDuration::from_nanos(1))] |
| 92 |
#[case(1.5.std_microseconds(), StdDuration::from_nanos(1_500))] |
| 93 |
#[case(1.5.std_milliseconds(), StdDuration::from_micros(1_500))] |
| 94 |
#[case(1.5.std_seconds(), StdDuration::from_millis(1_500))] |
| 95 |
#[case(1.5.std_minutes(), StdDuration::from_secs(90))] |
| 96 |
#[case(1.5.std_hours(), StdDuration::from_secs(90 * 60))] |
| 97 |
#[case(1.5.std_days(), StdDuration::from_secs(36 * 3_600))] |
| 98 |
#[case(1.5.std_weeks(), StdDuration::from_secs(252 * 3_600))] |
| 99 |
fn float(#[case] ext_trait: StdDuration, #[case] expected: StdDuration) { |
| 100 |
assert_eq!(ext_trait, expected); |
| 101 |
} |
| 102 |
|
| 103 |
#[rstest] |
| 104 |
#[case(2.std_seconds() + 500.std_milliseconds(), 2_500.std_milliseconds())] |
| 105 |
#[case(2.std_seconds() - 500.std_milliseconds(), 1_500.std_milliseconds())] |
| 106 |
fn arithmetic(#[case] arithmetic_: StdDuration, #[case] expected: StdDuration) { |
| 107 |
assert_eq!(arithmetic_, expected); |
| 108 |
} |
| 109 |
} |