Skip to content

Commit 25c9687

Browse files
Add tests for rand_jitter README file
1 parent 73ef869 commit 25c9687

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

rand_jitter/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,8 @@ libc = { version = "0.2", default_features = false }
2525
[target.'cfg(target_os = "windows")'.dependencies]
2626
winapi = { version = "0.3", features = ["profileapi"] }
2727

28+
[dev-dependencies]
29+
doc-comment = "0.3"
30+
2831
[features]
2932
std = ["rand_core/std"]

rand_jitter/README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,26 @@ recommend to run the much more stringent
3838

3939
Use the following code using `timer_stats` to collect the data:
4040

41-
```rust
41+
```rust,no_run
4242
use rand_jitter::JitterRng;
4343
4444
use std::error::Error;
4545
use std::fs::File;
4646
use std::io::Write;
4747
48+
fn get_nstime() -> u64 {
49+
use std::time::{SystemTime, UNIX_EPOCH};
50+
51+
let dur = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
52+
// The correct way to calculate the current time is
53+
// `dur.as_secs() * 1_000_000_000 + dur.subsec_nanos() as u64`
54+
// But this is faster, and the difference in terms of entropy is
55+
// negligible (log2(10^9) == 29.9).
56+
dur.as_secs() << 30 | dur.subsec_nanos() as u64
57+
}
58+
4859
fn main() -> Result<(), Box<Error>> {
49-
let mut rng = JitterRng::new()?;
60+
let mut rng = JitterRng::new_with_timer(get_nstime);
5061
5162
// 1_000_000 results are required for the
5263
// NIST SP 800-90B Entropy Estimation Suite

rand_jitter/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ extern crate libc;
6363
#[cfg(target_os = "windows")]
6464
extern crate winapi;
6565

66+
#[cfg(test)]
67+
#[macro_use]
68+
extern crate doc_comment;
69+
70+
#[cfg(test)]
71+
doctest!("../README.md");
6672

6773
#[cfg(not(feature = "log"))]
6874
#[macro_use] mod dummy_log;

0 commit comments

Comments
 (0)