summaryrefslogtreecommitdiffstats
path: root/third_party/rust/instant/tests/wasm.rs
blob: b1577adfa839155d5df67bc3c873890430a249d0 (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
extern crate wasm_bindgen_test;

use instant::Instant;
use std::time::Duration;
use wasm_bindgen_test::*;

wasm_bindgen_test_configure!(run_in_browser);
// run these tests using: wasm-pack test --chrome --headless -- --features wasm-bindgen

#[wasm_bindgen_test]
fn test_instant_now() {
    let now = Instant::now();
    assert!(now.elapsed().as_nanos() > 0);
}

#[wasm_bindgen_test]
fn test_duration() {
    let now = Instant::now();
    let one_sec = Duration::from_secs(1);
    assert!(now.elapsed() < one_sec);
}

// Duration::new will overflow when you have u64::MAX seconds and one billion nanoseconds.
// <https://doc.rust-lang.org/std/time/struct.Duration.html#method.new>
const ONE_BILLION: u32 = 1_000_000_000;

#[wasm_bindgen_test]
fn test_checked_add() {
    let now = Instant::now();

    assert!(now.checked_add(Duration::from_millis(1)).is_some());
    assert_eq!(
        None,
        now.checked_add(Duration::new(u64::MAX, ONE_BILLION - 1))
    );
}

#[wasm_bindgen_test]
fn test_checked_sub() {
    let now = Instant::now();

    assert!(now.checked_sub(Duration::from_millis(1)).is_some());
    assert!(now
        .checked_sub(Duration::new(u64::MAX, ONE_BILLION - 1))
        .is_none());
}