summary refs log tree commit diff
path: root/src/test/ui/consts/duration-consts-2.rs
blob: c8b3939933126996195ac32316a90a129032e295 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// run-pass

#![feature(const_panic)]
#![feature(duration_consts_2)]
#![feature(div_duration)]

use std::time::Duration;

fn duration() {
    const ZERO : Duration = Duration::new(0, 0);
    assert_eq!(ZERO, Duration::from_secs(0));

    const ONE : Duration = Duration::new(0, 1);
    assert_eq!(ONE, Duration::from_nanos(1));

    const MAX : Duration = Duration::new(u64::MAX, 1_000_000_000 - 1);

    const MAX_ADD_ZERO : Option<Duration> = MAX.checked_add(ZERO);
    assert_eq!(MAX_ADD_ZERO, Some(MAX));

    const MAX_ADD_ONE : Option<Duration> = MAX.checked_add(ONE);
    assert_eq!(MAX_ADD_ONE, None);

    const ONE_SUB_ONE : Option<Duration> = ONE.checked_sub(ONE);
    assert_eq!(ONE_SUB_ONE, Some(ZERO));

    const ZERO_SUB_ONE : Option<Duration> = ZERO.checked_sub(ONE);
    assert_eq!(ZERO_SUB_ONE, None);

    const ONE_MUL_ONE : Option<Duration> = ONE.checked_mul(1);
    assert_eq!(ONE_MUL_ONE, Some(ONE));

    const MAX_MUL_TWO : Option<Duration> = MAX.checked_mul(2);
    assert_eq!(MAX_MUL_TWO, None);

    const ONE_DIV_ONE : Option<Duration> = ONE.checked_div(1);
    assert_eq!(ONE_DIV_ONE, Some(ONE));

    const ONE_DIV_ZERO : Option<Duration> = ONE.checked_div(0);
    assert_eq!(ONE_DIV_ZERO, None);

    const MAX_AS_F32 : f32 = MAX.as_secs_f32();
    assert_eq!(MAX_AS_F32, 18446744000000000000.0_f32);

    const MAX_AS_F64 : f64 = MAX.as_secs_f64();
    assert_eq!(MAX_AS_F64, 18446744073709552000.0_f64);

    const ONE_AS_F32 : f32 = ONE.div_duration_f32(ONE);
    assert_eq!(ONE_AS_F32, 1.0_f32);

    const ONE_AS_F64 : f64 = ONE.div_duration_f64(ONE);
    assert_eq!(ONE_AS_F64, 1.0_f64);
}

fn main() {
    duration();
}