Skip to content

Commit

Permalink
XorShift serde
Browse files Browse the repository at this point in the history
  • Loading branch information
UserAB1236872 committed Jan 22, 2018
1 parent 44455f0 commit b7d04ee
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/prng/xorshift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use impls;
/// RNGs"](https://www.jstatsoft.org/v08/i14/paper). *Journal of
/// Statistical Software*. Vol. 8 (Issue 14).
#[derive(Clone)]
#[cfg_attr(feature="serde-1", derive(Serialize,Deserialize))]
pub struct XorShiftRng {
x: w<u32>,
y: w<u32>,
Expand Down Expand Up @@ -115,3 +116,37 @@ impl Rand for XorShiftRng {
XorShiftRng { x: w(x), y: w(y), z: w(z), w: w(w_) }
}
}

#[cfg(test)]
mod tests {
use {Rng, SeedableRng};

#[cfg(feature="serde-1")]
#[test]
fn test_serde() {
use super::XorShiftRng;
use thread_rng;
use bincode;
use std::io::{BufWriter, BufReader};

let seed: [u32; 4] = thread_rng().gen();
let mut rng: XorShiftRng = SeedableRng::from_seed(seed);

let buf: Vec<u8> = Vec::new();
let mut buf = BufWriter::new(buf);
bincode::serialize_into(&mut buf, &rng, bincode::Infinite).expect("Could not serialize");

let buf = buf.into_inner().unwrap();
let mut read = BufReader::new(&buf[..]);
let mut deserialized: XorShiftRng = bincode::deserialize_from(&mut read, bincode::Infinite).expect("Could not deserialize");

assert_eq!(rng.x, deserialized.x);
assert_eq!(rng.y, deserialized.y);
assert_eq!(rng.z, deserialized.z);
assert_eq!(rng.w, deserialized.w);

for _ in 0..16 {
assert_eq!(rng.next_u64(), deserialized.next_u64());
}
}
}

0 comments on commit b7d04ee

Please sign in to comment.