Skip to content

Commit

Permalink
Fix rand 0.5 compat
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcrichton committed Jul 5, 2018
1 parent 72b0ac0 commit 1e20fa9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 41 deletions.
29 changes: 7 additions & 22 deletions src/deflate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod tests {
fn roundtrip() {
let mut real = Vec::new();
let mut w = write::DeflateEncoder::new(Vec::new(), Compression::default());
let v = thread_rng().gen_iter::<u8>().take(1024).collect::<Vec<_>>();
let v = ::random_bytes().take(1024).collect::<Vec<_>>();
for _ in 0..200 {
let to_write = &v[..thread_rng().gen_range(0, v.len())];
real.extend(to_write.iter().map(|x| *x));
Expand Down Expand Up @@ -44,7 +44,7 @@ mod tests {
fn total_in() {
let mut real = Vec::new();
let mut w = write::DeflateEncoder::new(Vec::new(), Compression::default());
let v = thread_rng().gen_iter::<u8>().take(1024).collect::<Vec<_>>();
let v = ::random_bytes().take(1024).collect::<Vec<_>>();
for _ in 0..200 {
let to_write = &v[..thread_rng().gen_range(0, v.len())];
real.extend(to_write.iter().map(|x| *x));
Expand All @@ -67,10 +67,7 @@ mod tests {

#[test]
fn roundtrip2() {
let v = thread_rng()
.gen_iter::<u8>()
.take(1024 * 1024)
.collect::<Vec<_>>();
let v = ::random_bytes().take(1024 * 1024).collect::<Vec<_>>();
let mut r =
read::DeflateDecoder::new(read::DeflateEncoder::new(&v[..], Compression::default()));
let mut ret = Vec::new();
Expand All @@ -80,10 +77,7 @@ mod tests {

#[test]
fn roundtrip3() {
let v = thread_rng()
.gen_iter::<u8>()
.take(1024 * 1024)
.collect::<Vec<_>>();
let v = ::random_bytes().take(1024 * 1024).collect::<Vec<_>>();
let mut w = write::DeflateEncoder::new(
write::DeflateDecoder::new(Vec::new()),
Compression::default(),
Expand All @@ -95,10 +89,7 @@ mod tests {

#[test]
fn reset_writer() {
let v = thread_rng()
.gen_iter::<u8>()
.take(1024 * 1024)
.collect::<Vec<_>>();
let v = ::random_bytes().take(1024 * 1024).collect::<Vec<_>>();
let mut w = write::DeflateEncoder::new(Vec::new(), Compression::default());
w.write_all(&v).unwrap();
let a = w.reset(Vec::new()).unwrap();
Expand All @@ -113,10 +104,7 @@ mod tests {

#[test]
fn reset_reader() {
let v = thread_rng()
.gen_iter::<u8>()
.take(1024 * 1024)
.collect::<Vec<_>>();
let v = ::random_bytes().take(1024 * 1024).collect::<Vec<_>>();
let (mut a, mut b, mut c) = (Vec::new(), Vec::new(), Vec::new());
let mut r = read::DeflateEncoder::new(&v[..], Compression::default());
r.read_to_end(&mut a).unwrap();
Expand All @@ -130,10 +118,7 @@ mod tests {

#[test]
fn reset_decoder() {
let v = thread_rng()
.gen_iter::<u8>()
.take(1024 * 1024)
.collect::<Vec<_>>();
let v = ::random_bytes().take(1024 * 1024).collect::<Vec<_>>();
let mut w = write::DeflateEncoder::new(Vec::new(), Compression::default());
w.write_all(&v).unwrap();
let data = w.finish().unwrap();
Expand Down
7 changes: 2 additions & 5 deletions src/gz/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ mod tests {
fn roundtrip_big() {
let mut real = Vec::new();
let mut w = write::GzEncoder::new(Vec::new(), Compression::default());
let v = thread_rng().gen_iter::<u8>().take(1024).collect::<Vec<_>>();
let v = ::random_bytes().take(1024).collect::<Vec<_>>();
for _ in 0..200 {
let to_write = &v[..thread_rng().gen_range(0, v.len())];
real.extend(to_write.iter().map(|x| *x));
Expand All @@ -300,10 +300,7 @@ mod tests {

#[test]
fn roundtrip_big2() {
let v = thread_rng()
.gen_iter::<u8>()
.take(1024 * 1024)
.collect::<Vec<_>>();
let v = ::random_bytes().take(1024 * 1024).collect::<Vec<_>>();
let mut r = read::GzDecoder::new(read::GzEncoder::new(&v[..], Compression::default()));
let mut res = Vec::new();
r.read_to_end(&mut res).unwrap();
Expand Down
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,11 @@ impl Default for Compression {
Compression(6)
}
}

#[cfg(test)]
fn random_bytes() -> impl Iterator<Item = u8> {
use std::iter;
use rand::Rng;

iter::repeat(()).map(|_| rand::thread_rng().gen())
}
19 changes: 5 additions & 14 deletions src/zlib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod tests {
fn roundtrip() {
let mut real = Vec::new();
let mut w = write::ZlibEncoder::new(Vec::new(), Compression::default());
let v = thread_rng().gen_iter::<u8>().take(1024).collect::<Vec<_>>();
let v = ::random_bytes().take(1024).collect::<Vec<_>>();
for _ in 0..200 {
let to_write = &v[..thread_rng().gen_range(0, v.len())];
real.extend(to_write.iter().map(|x| *x));
Expand Down Expand Up @@ -45,7 +45,7 @@ mod tests {
fn total_in() {
let mut real = Vec::new();
let mut w = write::ZlibEncoder::new(Vec::new(), Compression::default());
let v = thread_rng().gen_iter::<u8>().take(1024).collect::<Vec<_>>();
let v = ::random_bytes().take(1024).collect::<Vec<_>>();
for _ in 0..200 {
let to_write = &v[..thread_rng().gen_range(0, v.len())];
real.extend(to_write.iter().map(|x| *x));
Expand All @@ -68,10 +68,7 @@ mod tests {

#[test]
fn roundtrip2() {
let v = thread_rng()
.gen_iter::<u8>()
.take(1024 * 1024)
.collect::<Vec<_>>();
let v = ::random_bytes().take(1024 * 1024).collect::<Vec<_>>();
let mut r = read::ZlibDecoder::new(read::ZlibEncoder::new(&v[..], Compression::default()));
let mut ret = Vec::new();
r.read_to_end(&mut ret).unwrap();
Expand All @@ -80,10 +77,7 @@ mod tests {

#[test]
fn roundtrip3() {
let v = thread_rng()
.gen_iter::<u8>()
.take(1024 * 1024)
.collect::<Vec<_>>();
let v = ::random_bytes().take(1024 * 1024).collect::<Vec<_>>();
let mut w =
write::ZlibEncoder::new(write::ZlibDecoder::new(Vec::new()), Compression::default());
w.write_all(&v).unwrap();
Expand All @@ -93,10 +87,7 @@ mod tests {

#[test]
fn reset_decoder() {
let v = thread_rng()
.gen_iter::<u8>()
.take(1024 * 1024)
.collect::<Vec<_>>();
let v = ::random_bytes().take(1024 * 1024).collect::<Vec<_>>();
let mut w = write::ZlibEncoder::new(Vec::new(), Compression::default());
w.write_all(&v).unwrap();
let data = w.finish().unwrap();
Expand Down

0 comments on commit 1e20fa9

Please sign in to comment.