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