Skip to content

Commit

Permalink
Add rand_core::le::test_read unit test
Browse files Browse the repository at this point in the history
Also minor doc fix
  • Loading branch information
dhardy committed Dec 31, 2017
1 parent 1f9ce3a commit 8990da2
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions rand_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
//! `Rng` is the core trait implemented by algorithmic pseudo-random number
//! generators and external random-number sources.
//!
//! `SeedFromRng` and `SeedableRng` are extension traits for construction and
//! reseeding.
//! `SeedableRng` is an extension trait for construction from fixed seeds and
//! other random number generators.
//!
//! `Error` is provided for error-handling. It is safe to use in `no_std`
//! environments.
Expand Down Expand Up @@ -443,4 +443,22 @@ pub mod le {
pub fn read_u64_into(src: &[u8], dst: &mut [u64]) {
read_slice!(src, dst, 8, to_le);
}

#[test]
fn test_read() {
assert_eq!(read_u32(&[1, 2, 3, 4]), 0x04030201);
assert_eq!(read_u64(&[1, 2, 3, 4, 5, 6, 7, 8]), 0x0807060504030201);

let bytes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];

let mut buf = [0u32; 4];
read_u32_into(&bytes, &mut buf);
assert_eq!(buf[0], 0x04030201);
assert_eq!(buf[3], 0x100F0E0D);

let mut buf = [0u64; 2];
read_u64_into(&bytes, &mut buf);
assert_eq!(buf[0], 0x0807060504030201);
assert_eq!(buf[1], 0x100F0E0D0C0B0A09);
}
}

0 comments on commit 8990da2

Please sign in to comment.