Skip to content

Commit 5b31f2d

Browse files
authored
Merge pull request #801 from GuillaumeGomez/add-tests-readme
Add tests for rand_jitter README file
2 parents bb9dc60 + 7a42b5c commit 5b31f2d

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ extern crate libc;
6363
#[cfg(target_os = "windows")]
6464
extern crate winapi;
6565

66+
// Coming from https://crates.io/crates/doc-comment
67+
#[cfg(test)]
68+
macro_rules! doc_comment {
69+
($x:expr) => {
70+
#[doc = $x]
71+
extern {}
72+
};
73+
}
74+
75+
#[cfg(test)]
76+
doc_comment!(include_str!("../README.md"));
6677

6778
#[cfg(not(feature = "log"))]
6879
#[macro_use] mod dummy_log;

0 commit comments

Comments
 (0)