test: Fixup many library unit tests

This commit is contained in:
Alex Crichton 2015-04-10 11:39:53 -07:00
parent dddfbe0441
commit 700e627cf7
26 changed files with 105 additions and 479 deletions

View File

@ -943,7 +943,7 @@ mod test {
use std::clone::Clone;
use std::iter::Iterator;
use std::option::Option::{Some, None, self};
use std::rand;
use std::__rand::{thread_rng, Rng};
use std::thread;
use std::vec::Vec;
@ -1095,7 +1095,7 @@ mod test {
let mut v = vec![];
for i in 0..sz {
check_links(&m);
let r: u8 = rand::random();
let r: u8 = thread_rng().next_u32() as u8;
match r % 6 {
0 => {
m.pop_back();

View File

@ -12,14 +12,13 @@ macro_rules! map_insert_rand_bench {
($name: ident, $n: expr, $map: ident) => (
#[bench]
pub fn $name(b: &mut ::test::Bencher) {
use std::rand;
use std::rand::Rng;
use std::__rand::{thread_rng, Rng};
use test::black_box;
let n: usize = $n;
let mut map = $map::new();
// setup
let mut rng = rand::weak_rng();
let mut rng = thread_rng();
for _ in 0..n {
let i = rng.gen::<usize>() % n;
@ -67,8 +66,7 @@ macro_rules! map_find_rand_bench {
#[bench]
pub fn $name(b: &mut ::test::Bencher) {
use std::iter::Iterator;
use std::rand::Rng;
use std::rand;
use std::__rand::{thread_rng, Rng};
use std::vec::Vec;
use test::black_box;
@ -76,7 +74,7 @@ macro_rules! map_find_rand_bench {
let n: usize = $n;
// setup
let mut rng = rand::weak_rng();
let mut rng = rand::thread_rng();
let mut keys: Vec<_> = (0..n).map(|_| rng.gen::<usize>() % n).collect();
for &k in &keys {

View File

@ -633,15 +633,14 @@ fn test_bit_vec_extend() {
mod bench {
use std::collections::BitVec;
use std::u32;
use std::rand::{Rng, self};
use std::__rand::{Rng, thread_rng};
use test::{Bencher, black_box};
const BENCH_BITS : usize = 1 << 14;
fn rng() -> rand::IsaacRng {
let seed: &[_] = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
rand::SeedableRng::from_seed(seed)
fn rng() -> ThreadRng {
thread_rng()
}
#[bench]

View File

@ -251,7 +251,7 @@ fn test_entry(){
mod bench {
use std::collections::BTreeMap;
use std::rand::{Rng, weak_rng};
use std::rand::{Rng, thread_rng};
use test::{Bencher, black_box};
@ -269,7 +269,7 @@ mod bench {
fn bench_iter(b: &mut Bencher, size: i32) {
let mut map = BTreeMap::<i32, i32>::new();
let mut rng = weak_rng();
let mut rng = thread_rng();
for _ in 0..size {
map.insert(rng.gen(), rng.gen());

View File

@ -1296,7 +1296,7 @@ fn test_to_vec() {
mod bench {
use std::iter::repeat;
use std::{mem, ptr};
use std::rand::{Rng, weak_rng};
use std::rand::{Rng, thread_rng};
use test::{Bencher, black_box};
@ -1465,7 +1465,7 @@ mod bench {
#[bench]
fn random_inserts(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = thread_rng();
b.iter(|| {
let mut v: Vec<_> = repeat((0, 0)).take(30).collect();
for _ in 0..100 {
@ -1477,7 +1477,7 @@ mod bench {
}
#[bench]
fn random_removes(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = thread_rng();
b.iter(|| {
let mut v: Vec<_> = repeat((0, 0)).take(130).collect();
for _ in 0..100 {
@ -1489,7 +1489,7 @@ mod bench {
#[bench]
fn sort_random_small(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = thread_rng();
b.iter(|| {
let mut v: Vec<_> = rng.gen_iter::<u64>().take(5).collect();
v.sort();
@ -1499,7 +1499,7 @@ mod bench {
#[bench]
fn sort_random_medium(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = thread_rng();
b.iter(|| {
let mut v: Vec<_> = rng.gen_iter::<u64>().take(100).collect();
v.sort();
@ -1509,7 +1509,7 @@ mod bench {
#[bench]
fn sort_random_large(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = thread_rng();
b.iter(|| {
let mut v: Vec<_> = rng.gen_iter::<u64>().take(10000).collect();
v.sort();
@ -1530,7 +1530,7 @@ mod bench {
#[bench]
fn sort_big_random_small(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = thread_rng();
b.iter(|| {
let mut v = rng.gen_iter::<BigSortable>().take(5)
.collect::<Vec<BigSortable>>();
@ -1541,7 +1541,7 @@ mod bench {
#[bench]
fn sort_big_random_medium(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = thread_rng();
b.iter(|| {
let mut v = rng.gen_iter::<BigSortable>().take(100)
.collect::<Vec<BigSortable>>();
@ -1552,7 +1552,7 @@ mod bench {
#[bench]
fn sort_big_random_large(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = thread_rng();
b.iter(|| {
let mut v = rng.gen_iter::<BigSortable>().take(10000)
.collect::<Vec<BigSortable>>();

View File

@ -169,42 +169,42 @@ fn test_radix_base_too_large() {
mod u32 {
use test::Bencher;
use core::fmt::radix;
use std::rand::{weak_rng, Rng};
use std::__rand::{thread_rng, Rng};
use std::io::{Write, sink};
#[bench]
fn format_bin(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = thread_rng();
b.iter(|| { write!(&mut sink(), "{:b}", rng.gen::<u32>()) })
}
#[bench]
fn format_oct(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = thread_rng();
b.iter(|| { write!(&mut sink(), "{:o}", rng.gen::<u32>()) })
}
#[bench]
fn format_dec(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = thread_rng();
b.iter(|| { write!(&mut sink(), "{}", rng.gen::<u32>()) })
}
#[bench]
fn format_hex(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = thread_rng();
b.iter(|| { write!(&mut sink(), "{:x}", rng.gen::<u32>()) })
}
#[bench]
fn format_show(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = thread_rng();
b.iter(|| { write!(&mut sink(), "{:?}", rng.gen::<u32>()) })
}
#[bench]
fn format_base_36(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = thread_rng();
b.iter(|| { write!(&mut sink(), "{}", radix(rng.gen::<u32>(), 36)) })
}
}
@ -212,42 +212,42 @@ mod u32 {
mod i32 {
use test::Bencher;
use core::fmt::radix;
use std::rand::{weak_rng, Rng};
use std::__rand::{thread_rng, Rng};
use std::io::{Write, sink};
#[bench]
fn format_bin(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = thread_rng();
b.iter(|| { write!(&mut sink(), "{:b}", rng.gen::<i32>()) })
}
#[bench]
fn format_oct(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = thread_rng();
b.iter(|| { write!(&mut sink(), "{:o}", rng.gen::<i32>()) })
}
#[bench]
fn format_dec(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = thread_rng();
b.iter(|| { write!(&mut sink(), "{}", rng.gen::<i32>()) })
}
#[bench]
fn format_hex(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = thread_rng();
b.iter(|| { write!(&mut sink(), "{:x}", rng.gen::<i32>()) })
}
#[bench]
fn format_show(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = thread_rng();
b.iter(|| { write!(&mut sink(), "{:?}", rng.gen::<i32>()) })
}
#[bench]
fn format_base_36(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = thread_rng();
b.iter(|| { write!(&mut sink(), "{}", radix(rng.gen::<i32>(), 36)) })
}
}

View File

@ -155,12 +155,11 @@ pub fn inflate_bytes_zlib(bytes: &[u8]) -> Result<Bytes,Error> {
mod tests {
#![allow(deprecated)]
use super::{inflate_bytes, deflate_bytes};
use std::rand;
use std::rand::Rng;
use std::__rand::{thread_rng, Rng};
#[test]
fn test_flate_round_trip() {
let mut r = rand::thread_rng();
let mut r = thread_rng();
let mut words = vec![];
for _ in 0..20 {
let range = r.gen_range(1, 10);

View File

@ -516,25 +516,21 @@ pub struct Closed01<F>(pub F);
#[cfg(test)]
mod test {
use std::rand;
use std::__rand as rand;
pub struct MyRng<R> { inner: R }
pub struct MyRng { inner: Box<rand::Rng> }
impl<R: rand::Rng> ::Rng for MyRng<R> {
fn next_u32(&mut self) -> u32 {
fn next<T: rand::Rng>(t: &mut T) -> u32 {
use std::rand::Rng;
t.next_u32()
}
next(&mut self.inner)
rand::Rng::next_u32(&mut self.inner)
}
}
pub fn rng() -> MyRng<rand::ThreadRng> {
MyRng { inner: rand::thread_rng() }
MyRng { inner: Box::new(rand::thread_rng()) }
}
pub fn weak_rng() -> MyRng<rand::XorShiftRng> {
MyRng { inner: rand::weak_rng() }
pub fn weak_rng() -> MyRng<rand::ThreadRng> {
MyRng { inner: Box::new(rand::thread_rng()) }
}
}

View File

@ -211,55 +211,3 @@ impl<T:Rand> Rand for Option<T> {
}
}
}
#[cfg(test)]
mod tests {
use std::rand::{Rng, thread_rng, Open01, Closed01};
struct ConstantRng(u64);
impl Rng for ConstantRng {
fn next_u32(&mut self) -> u32 {
let ConstantRng(v) = *self;
v as u32
}
fn next_u64(&mut self) -> u64 {
let ConstantRng(v) = *self;
v
}
}
#[test]
fn floating_point_edge_cases() {
// the test for exact equality is correct here.
assert!(ConstantRng(0xffff_ffff).gen::<f32>() != 1.0);
assert!(ConstantRng(0xffff_ffff_ffff_ffff).gen::<f64>() != 1.0);
}
#[test]
fn rand_open() {
// this is unlikely to catch an incorrect implementation that
// generates exactly 0 or 1, but it keeps it sane.
let mut rng = thread_rng();
for _ in 0..1_000 {
// strict inequalities
let Open01(f) = rng.gen::<Open01<f64>>();
assert!(0.0 < f && f < 1.0);
let Open01(f) = rng.gen::<Open01<f32>>();
assert!(0.0 < f && f < 1.0);
}
}
#[test]
fn rand_closed() {
let mut rng = thread_rng();
for _ in 0..1_000 {
// strict inequalities
let Closed01(f) = rng.gen::<Closed01<f64>>();
assert!(0.0 <= f && f <= 1.0);
let Closed01(f) = rng.gen::<Closed01<f32>>();
assert!(0.0 <= f && f <= 1.0);
}
}
}

View File

@ -68,7 +68,7 @@ pub fn realpath(original: &Path) -> io::Result<PathBuf> {
mod test {
use tempdir::TempDir;
use std::fs::{self, File};
use std::path::{Path, PathBuf};
use super::realpath;
#[test]
fn realpath_works() {

View File

@ -2627,9 +2627,9 @@ mod tests {
use super::{Json, from_str, DecodeResult, DecoderError, JsonEvent, Parser,
StackElement, Stack, Decoder, Encoder, EncoderError};
use std::{i64, u64, f32, f64};
use std::io::prelude::*;
use std::collections::BTreeMap;
use std::string;
use std::old_io::Writer;
#[derive(RustcDecodable, Eq, PartialEq, Debug)]
struct OptionData {
@ -3464,7 +3464,6 @@ mod tests {
#[test]
fn test_encode_hashmap_with_numeric_key() {
use std::str::from_utf8;
use std::old_io::Writer;
use std::collections::HashMap;
let mut hm: HashMap<usize, bool> = HashMap::new();
hm.insert(1, true);
@ -3480,7 +3479,6 @@ mod tests {
#[test]
fn test_prettyencode_hashmap_with_numeric_key() {
use std::str::from_utf8;
use std::old_io::Writer;
use std::collections::HashMap;
let mut hm: HashMap<usize, bool> = HashMap::new();
hm.insert(1, true);

View File

@ -35,7 +35,7 @@ Core encoding and decoding interfaces.
#![feature(std_misc)]
#![feature(unicode)]
#![feature(str_char)]
#![cfg_attr(test, feature(test, old_io))]
#![cfg_attr(test, feature(test))]
// test harness access
#[cfg(test)] extern crate test;

View File

@ -1631,7 +1631,7 @@ mod test_map {
use super::Entry::{Occupied, Vacant};
use iter::{range_inclusive, range_step_inclusive, repeat};
use cell::RefCell;
use rand::{weak_rng, Rng};
use rand::{thread_rng, Rng};
#[test]
fn test_create_capacity_zero() {
@ -2290,7 +2290,7 @@ mod test_map {
}
let mut m = HashMap::new();
let mut rng = weak_rng();
let mut rng = thread_rng();
// Populate the map with some items.
for _ in 0..50 {

View File

@ -123,7 +123,7 @@ pub struct WalkDir {
/// Opening a file for both reading and writing, as well as creating it if it
/// doesn't exist:
///
/// ```
/// ```no_run
/// use std::fs::OpenOptions;
///
/// let file = OpenOptions::new()
@ -1195,7 +1195,8 @@ mod tests {
pub fn tmpdir() -> TempDir {
let p = env::temp_dir();
let ret = p.join(&format!("rust-{}", rand::random::<u32>()));
let mut r = rand::thread_rng();
let ret = p.join(&format!("rust-{}", r.next_u32()));
check!(fs::create_dir(&ret));
TempDir(ret)
}

View File

@ -288,7 +288,7 @@ mod rand;
#[doc(hidden)]
#[unstable(feature = "rand")]
pub mod __rand {
pub use rand::{thread_rng, Rng};
pub use rand::{thread_rng, ThreadRng, Rng};
}
// Modules that exist purely to document + host impl docs for primitive types

View File

@ -464,7 +464,7 @@ mod bench {
mod usize {
use super::test::Bencher;
use rand::{weak_rng, Rng};
use rand::{thread_rng, Rng};
use std::fmt;
#[inline]
@ -474,38 +474,38 @@ mod bench {
#[bench]
fn to_str_bin(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = thread_rng();
b.iter(|| { to_string(rng.gen::<usize>(), 2); })
}
#[bench]
fn to_str_oct(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = thread_rng();
b.iter(|| { to_string(rng.gen::<usize>(), 8); })
}
#[bench]
fn to_str_dec(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = thread_rng();
b.iter(|| { to_string(rng.gen::<usize>(), 10); })
}
#[bench]
fn to_str_hex(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = thread_rng();
b.iter(|| { to_string(rng.gen::<usize>(), 16); })
}
#[bench]
fn to_str_base_36(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = thread_rng();
b.iter(|| { to_string(rng.gen::<usize>(), 36); })
}
}
mod isize {
use super::test::Bencher;
use rand::{weak_rng, Rng};
use rand::{thread_rng, Rng};
use std::fmt;
#[inline]
@ -515,43 +515,43 @@ mod bench {
#[bench]
fn to_str_bin(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = thread_rng();
b.iter(|| { to_string(rng.gen::<isize>(), 2); })
}
#[bench]
fn to_str_oct(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = thread_rng();
b.iter(|| { to_string(rng.gen::<isize>(), 8); })
}
#[bench]
fn to_str_dec(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = thread_rng();
b.iter(|| { to_string(rng.gen::<isize>(), 10); })
}
#[bench]
fn to_str_hex(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = thread_rng();
b.iter(|| { to_string(rng.gen::<isize>(), 16); })
}
#[bench]
fn to_str_base_36(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = thread_rng();
b.iter(|| { to_string(rng.gen::<isize>(), 36); })
}
}
mod f64 {
use super::test::Bencher;
use rand::{weak_rng, Rng};
use rand::{thread_rng, Rng};
use f64;
#[bench]
fn float_to_string(b: &mut Bencher) {
let mut rng = weak_rng();
let mut rng = thread_rng();
b.iter(|| { f64::to_string(rng.gen()); })
}
}

View File

@ -534,8 +534,6 @@ mod tests {
use io::prelude::*;
use io::ErrorKind;
use old_path::{self, GenericPath};
use old_io::fs::PathExtensions;
use rt::running_on_valgrind;
use str;
use super::{Command, Output, Stdio};
@ -748,43 +746,6 @@ mod tests {
cmd
}
#[cfg(not(target_arch = "aarch64"))]
#[test]
fn test_keep_current_working_dir() {
use os;
let prog = pwd_cmd().spawn().unwrap();
let output = String::from_utf8(prog.wait_with_output().unwrap().stdout).unwrap();
let parent_dir = ::env::current_dir().unwrap().to_str().unwrap().to_string();
let parent_dir = old_path::Path::new(parent_dir);
let child_dir = old_path::Path::new(output.trim());
let parent_stat = parent_dir.stat().unwrap();
let child_stat = child_dir.stat().unwrap();
assert_eq!(parent_stat.unstable.device, child_stat.unstable.device);
assert_eq!(parent_stat.unstable.inode, child_stat.unstable.inode);
}
#[test]
fn test_change_working_directory() {
use os;
// test changing to the parent of os::getcwd() because we know
// the path exists (and os::getcwd() is not expected to be root)
let parent_dir = ::env::current_dir().unwrap().to_str().unwrap().to_string();
let parent_dir = old_path::Path::new(parent_dir).dir_path();
let result = pwd_cmd().current_dir(parent_dir.as_str().unwrap()).output().unwrap();
let output = String::from_utf8(result.stdout).unwrap();
let child_dir = old_path::Path::new(output.trim());
let parent_stat = parent_dir.stat().unwrap();
let child_stat = child_dir.stat().unwrap();
assert_eq!(parent_stat.unstable.device, child_stat.unstable.device);
assert_eq!(parent_stat.unstable.inode, child_stat.unstable.inode);
}
#[cfg(all(unix, not(target_os="android")))]
pub fn env_cmd() -> Command {
Command::new("env")

View File

@ -354,261 +354,3 @@ impl Rng for ThreadRng {
self.rng.borrow_mut().fill_bytes(bytes)
}
}
#[cfg(test)]
mod test {
use prelude::v1::*;
use super::{Rng, thread_rng, random, SeedableRng, StdRng, sample};
use iter::{order, repeat};
struct ConstRng { i: u64 }
impl Rng for ConstRng {
fn next_u32(&mut self) -> u32 { self.i as u32 }
fn next_u64(&mut self) -> u64 { self.i }
// no fill_bytes on purpose
}
#[test]
fn test_fill_bytes_default() {
let mut r = ConstRng { i: 0x11_22_33_44_55_66_77_88 };
// check every remainder mod 8, both in small and big vectors.
let lengths = [0, 1, 2, 3, 4, 5, 6, 7,
80, 81, 82, 83, 84, 85, 86, 87];
for &n in &lengths {
let mut v = repeat(0).take(n).collect::<Vec<_>>();
r.fill_bytes(&mut v);
// use this to get nicer error messages.
for (i, &byte) in v.iter().enumerate() {
if byte == 0 {
panic!("byte {} of {} is zero", i, n)
}
}
}
}
#[test]
fn test_gen_range() {
let mut r = thread_rng();
for _ in 0..1000 {
let a = r.gen_range(-3, 42);
assert!(a >= -3 && a < 42);
assert_eq!(r.gen_range(0, 1), 0);
assert_eq!(r.gen_range(-12, -11), -12);
}
for _ in 0..1000 {
let a = r.gen_range(10, 42);
assert!(a >= 10 && a < 42);
assert_eq!(r.gen_range(0, 1), 0);
assert_eq!(r.gen_range(3_000_000, 3_000_001), 3_000_000);
}
}
#[test]
#[should_panic]
fn test_gen_range_panic_int() {
let mut r = thread_rng();
r.gen_range(5, -2);
}
#[test]
#[should_panic]
fn test_gen_range_panic_uint() {
let mut r = thread_rng();
r.gen_range(5, 2);
}
#[test]
fn test_gen_f64() {
let mut r = thread_rng();
let a = r.gen::<f64>();
let b = r.gen::<f64>();
debug!("{:?}", (a, b));
}
#[test]
fn test_gen_weighted_bool() {
let mut r = thread_rng();
assert_eq!(r.gen_weighted_bool(0), true);
assert_eq!(r.gen_weighted_bool(1), true);
}
#[test]
fn test_gen_ascii_str() {
let mut r = thread_rng();
assert_eq!(r.gen_ascii_chars().take(0).count(), 0);
assert_eq!(r.gen_ascii_chars().take(10).count(), 10);
assert_eq!(r.gen_ascii_chars().take(16).count(), 16);
}
#[test]
fn test_gen_vec() {
let mut r = thread_rng();
assert_eq!(r.gen_iter::<u8>().take(0).count(), 0);
assert_eq!(r.gen_iter::<u8>().take(10).count(), 10);
assert_eq!(r.gen_iter::<f64>().take(16).count(), 16);
}
#[test]
fn test_choose() {
let mut r = thread_rng();
assert_eq!(r.choose(&[1, 1, 1]).cloned(), Some(1));
let v: &[isize] = &[];
assert_eq!(r.choose(v), None);
}
#[test]
fn test_shuffle() {
let mut r = thread_rng();
let empty: &mut [isize] = &mut [];
r.shuffle(empty);
let mut one = [1];
r.shuffle(&mut one);
let b: &[_] = &[1];
assert_eq!(one, b);
let mut two = [1, 2];
r.shuffle(&mut two);
assert!(two == [1, 2] || two == [2, 1]);
let mut x = [1, 1, 1];
r.shuffle(&mut x);
let b: &[_] = &[1, 1, 1];
assert_eq!(x, b);
}
#[test]
fn test_thread_rng() {
let mut r = thread_rng();
r.gen::<isize>();
let mut v = [1, 1, 1];
r.shuffle(&mut v);
let b: &[_] = &[1, 1, 1];
assert_eq!(v, b);
assert_eq!(r.gen_range(0, 1), 0);
}
#[test]
fn test_random() {
// not sure how to test this aside from just getting some values
let _n : usize = random();
let _f : f32 = random();
let _o : Option<Option<i8>> = random();
let _many : ((),
(usize,
isize,
Option<(u32, (bool,))>),
(u8, i8, u16, i16, u32, i32, u64, i64),
(f32, (f64, (f64,)))) = random();
}
#[test]
fn test_sample() {
let min_val = 1;
let max_val = 100;
let mut r = thread_rng();
let vals = (min_val..max_val).collect::<Vec<isize>>();
let small_sample = sample(&mut r, vals.iter(), 5);
let large_sample = sample(&mut r, vals.iter(), vals.len() + 5);
assert_eq!(small_sample.len(), 5);
assert_eq!(large_sample.len(), vals.len());
assert!(small_sample.iter().all(|e| {
**e >= min_val && **e <= max_val
}));
}
#[test]
fn test_std_rng_seeded() {
let s = thread_rng().gen_iter::<usize>().take(256).collect::<Vec<usize>>();
let mut ra: StdRng = SeedableRng::from_seed(&*s);
let mut rb: StdRng = SeedableRng::from_seed(&*s);
assert!(order::equals(ra.gen_ascii_chars().take(100),
rb.gen_ascii_chars().take(100)));
}
#[test]
fn test_std_rng_reseed() {
let s = thread_rng().gen_iter::<usize>().take(256).collect::<Vec<usize>>();
let mut r: StdRng = SeedableRng::from_seed(&*s);
let string1 = r.gen_ascii_chars().take(100).collect::<String>();
r.reseed(&s);
let string2 = r.gen_ascii_chars().take(100).collect::<String>();
assert_eq!(string1, string2);
}
}
#[cfg(test)]
mod bench {
extern crate test;
use prelude::v1::*;
use self::test::Bencher;
use super::{XorShiftRng, StdRng, IsaacRng, Isaac64Rng, Rng};
use super::{OsRng, weak_rng};
use mem::size_of;
const RAND_BENCH_N: u64 = 100;
#[bench]
fn rand_xorshift(b: &mut Bencher) {
let mut rng: XorShiftRng = OsRng::new().unwrap().gen();
b.iter(|| {
for _ in 0..RAND_BENCH_N {
rng.gen::<usize>();
}
});
b.bytes = size_of::<usize>() as u64 * RAND_BENCH_N;
}
#[bench]
fn rand_isaac(b: &mut Bencher) {
let mut rng: IsaacRng = OsRng::new().unwrap().gen();
b.iter(|| {
for _ in 0..RAND_BENCH_N {
rng.gen::<usize>();
}
});
b.bytes = size_of::<usize>() as u64 * RAND_BENCH_N;
}
#[bench]
fn rand_isaac64(b: &mut Bencher) {
let mut rng: Isaac64Rng = OsRng::new().unwrap().gen();
b.iter(|| {
for _ in 0..RAND_BENCH_N {
rng.gen::<usize>();
}
});
b.bytes = size_of::<usize>() as u64 * RAND_BENCH_N;
}
#[bench]
fn rand_std(b: &mut Bencher) {
let mut rng = StdRng::new().unwrap();
b.iter(|| {
for _ in 0..RAND_BENCH_N {
rng.gen::<usize>();
}
});
b.bytes = size_of::<usize>() as u64 * RAND_BENCH_N;
}
#[bench]
fn rand_shuffle_100(b: &mut Bencher) {
let mut rng = weak_rng();
let x : &mut[usize] = &mut [1; 100];
b.iter(|| {
rng.shuffle(x);
})
}
}

View File

@ -67,17 +67,16 @@ mod test {
use prelude::v1::*;
use super::ReaderRng;
use old_io::MemReader;
use num::Int;
use rand::Rng;
#[test]
fn test_reader_rng_u64() {
// transmute from the target to avoid endianness concerns.
let v = vec![0, 0, 0, 0, 0, 0, 0, 1,
0 , 0, 0, 0, 0, 0, 0, 2,
0, 0, 0, 0, 0, 0, 0, 3];
let mut rng = ReaderRng::new(MemReader::new(v));
let v = &[0, 0, 0, 0, 0, 0, 0, 1,
0 , 0, 0, 0, 0, 0, 0, 2,
0, 0, 0, 0, 0, 0, 0, 3][..];
let mut rng = ReaderRng::new(v);
assert_eq!(rng.next_u64(), 1.to_be());
assert_eq!(rng.next_u64(), 2.to_be());
@ -85,8 +84,8 @@ mod test {
}
#[test]
fn test_reader_rng_u32() {
let v = vec![0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3];
let mut rng = ReaderRng::new(MemReader::new(v));
let v = &[0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3][..];
let mut rng = ReaderRng::new(v);
assert_eq!(rng.next_u32(), 1.to_be());
assert_eq!(rng.next_u32(), 2.to_be());
@ -97,7 +96,7 @@ mod test {
let v = [1, 2, 3, 4, 5, 6, 7, 8];
let mut w = [0; 8];
let mut rng = ReaderRng::new(MemReader::new(v.to_vec()));
let mut rng = ReaderRng::new(&v[..]);
rng.fill_bytes(&mut w);
assert!(v == w);
@ -106,7 +105,7 @@ mod test {
#[test]
#[should_panic]
fn test_reader_rng_insufficient_bytes() {
let mut rng = ReaderRng::new(MemReader::new(vec!()));
let mut rng = ReaderRng::new(&[][..]);
let mut v = [0; 3];
rng.fill_bytes(&mut v);
}

View File

@ -728,7 +728,6 @@ mod test {
use any::Any;
use sync::mpsc::{channel, Sender};
use result;
use std::old_io::{ChanReader, ChanWriter};
use super::{Builder};
use thread;
use thunk::Thunk;
@ -967,13 +966,11 @@ mod test {
#[test]
fn test_park_timeout_unpark_called_other_thread() {
use std::old_io;
for _ in 0..10 {
let th = thread::current();
let _guard = thread::spawn(move || {
old_io::timer::sleep(Duration::milliseconds(50));
super::sleep_ms(50);
th.unpark();
});

View File

@ -43,7 +43,6 @@ pub trait Stats <T: Float + FromPrimitive> {
/// Depends on IEEE-754 arithmetic guarantees. See proof of correctness at:
/// ["Adaptive Precision Floating-Point Arithmetic and Fast Robust Geometric Predicates"]
/// (http://www.cs.cmu.edu/~quake-papers/robust-arithmetic.ps)
/// *Discrete & Computational Geometry 18*, 3 (Oct 1997), 305-363, Shewchuk J.R.
fn sum(&self) -> T;
/// Minimum value of the samples.
@ -334,8 +333,9 @@ pub fn winsorize<T: Float + FromPrimitive>(samples: &mut [T], pct: T) {
mod tests {
use stats::Stats;
use stats::Summary;
use std::old_io::{self, Writer};
use std::f64;
use std::io::prelude::*;
use std::io;
macro_rules! assert_approx_eq {
($a:expr, $b:expr) => ({
@ -350,7 +350,7 @@ mod tests {
let summ2 = Summary::new(samples);
let mut w = old_io::stdout();
let mut w = io::sink();
let w = &mut w;
(write!(w, "\n")).unwrap();

View File

@ -15,12 +15,6 @@ extern crate core;
extern crate rand;
extern crate serialize as rustc_serialize;
#[derive(Rand)] //~ ERROR this trait cannot be derived
//~^ WARNING `#[derive(Rand)]` is deprecated
struct Foo {
x: u32,
}
#[derive(RustcEncodable)] //~ ERROR this trait cannot be derived
struct Bar {
x: u32,

View File

@ -12,21 +12,18 @@
use sub::sub2 as msalias;
use sub::sub2;
use std::old_io::stdio::println;
static yy: usize = 25;
mod sub {
pub mod sub2 {
use std::old_io::stdio::println;
pub mod sub3 {
use std::old_io::stdio::println;
pub fn hello() {
println("hello from module 3");
println!("hello from module 3");
}
}
pub fn hello() {
println("hello from a module");
println!("hello from a module");
}
pub struct nested_struct {

View File

@ -10,7 +10,7 @@
#![ crate_name = "test" ]
#![allow(unstable)]
#![feature(box_syntax, old_io, rustc_private, core, zero_one)]
#![feature(box_syntax, rustc_private, core, zero_one)]
extern crate graphviz;
// A simple rust project
@ -19,7 +19,6 @@ extern crate flate as myflate;
use std::collections::{HashMap,HashSet};
use std::cell::RefCell;
use std::old_io::stdio::println;
use sub::sub2 as msalias;
@ -61,15 +60,13 @@ fn test_tup_struct(x: TupStruct) -> isize {
mod sub {
pub mod sub2 {
use std::old_io::stdio::println;
pub mod sub3 {
use std::old_io::stdio::println;
pub fn hello() {
println("hello from module 3");
println!("hello from module 3");
}
}
pub fn hello() {
println("hello from a module");
println!("hello from a module");
}
pub struct nested_struct {
@ -106,7 +103,7 @@ trait SomeTrait: SuperTrait {
fn Method(&self, x: u32) -> u32;
fn prov(&self, x: u32) -> u32 {
println(&x.to_string());
println!("{}", &x.to_string());
42
}
fn provided_method(&self) -> u32 {
@ -122,7 +119,7 @@ trait SubTrait: SomeTrait {
impl SomeTrait for some_fields {
fn Method(&self, x: u32) -> u32 {
println(&x.to_string());
println!("{}", &x.to_string());
self.field1
}
}
@ -134,7 +131,7 @@ impl SubTrait for some_fields {}
impl some_fields {
fn stat(x: u32) -> u32 {
println(&x.to_string());
println!("{}", &x.to_string());
42
}
fn stat2(x: &some_fields) -> u32 {
@ -194,20 +191,20 @@ enum SomeStructEnum {
fn matchSomeEnum(val: SomeEnum) {
match val {
SomeEnum::Ints(int1, int2) => { println(&(int1+int2).to_string()); }
SomeEnum::Floats(float1, float2) => { println(&(float2*float1).to_string()); }
SomeEnum::Strings(_, _, s3) => { println(s3); }
SomeEnum::Ints(int1, int2) => { println!("{}", &(int1+int2).to_string()); }
SomeEnum::Floats(float1, float2) => { println!("{}", &(float2*float1).to_string()); }
SomeEnum::Strings(_, _, s3) => { println!("{}", s3); }
SomeEnum::MyTypes(mt1, mt2) => {
println(&(mt1.field1 - mt2.field1).to_string());
println!("{}", &(mt1.field1 - mt2.field1).to_string());
}
}
}
fn matchSomeStructEnum(se: SomeStructEnum) {
match se {
SomeStructEnum::EnumStruct{a:a, ..} => println(&a.to_string()),
SomeStructEnum::EnumStruct2{f1:f1, f2:f_2} => println(&f_2.field1.to_string()),
SomeStructEnum::EnumStruct3{f1, ..} => println(&f1.field1.to_string()),
SomeStructEnum::EnumStruct{a:a, ..} => println!("{}", &a.to_string()),
SomeStructEnum::EnumStruct2{f1:f1, f2:f_2} => println!("{}", &f_2.field1.to_string()),
SomeStructEnum::EnumStruct3{f1, ..} => println!("{}", &f1.field1.to_string()),
}
}
@ -215,9 +212,9 @@ fn matchSomeStructEnum(se: SomeStructEnum) {
fn matchSomeStructEnum2(se: SomeStructEnum) {
use SomeStructEnum::*;
match se {
EnumStruct{a: ref aaa, ..} => println(&aaa.to_string()),
EnumStruct2{f1, f2: f2} => println(&f1.field1.to_string()),
EnumStruct3{f1, f3: SomeEnum::Ints(_, _), f2} => println(&f1.field1.to_string()),
EnumStruct{a: ref aaa, ..} => println!("{}", &aaa.to_string()),
EnumStruct2{f1, f2: f2} => println!("{}", &f1.field1.to_string()),
EnumStruct3{f1, f3: SomeEnum::Ints(_, _), f2} => println!("{}", &f1.field1.to_string()),
_ => {},
}
}
@ -225,22 +222,22 @@ fn matchSomeStructEnum2(se: SomeStructEnum) {
fn matchSomeOtherEnum(val: SomeOtherEnum) {
use SomeOtherEnum::{SomeConst2, SomeConst3};
match val {
SomeOtherEnum::SomeConst1 => { println("I'm const1."); }
SomeConst2 | SomeConst3 => { println("I'm const2 or const3."); }
SomeOtherEnum::SomeConst1 => { println!("I'm const1."); }
SomeConst2 | SomeConst3 => { println!("I'm const2 or const3."); }
}
}
fn hello<X: SomeTrait>((z, a) : (u32, String), ex: X) {
SameDir2::hello(43);
println(&yy.to_string());
println!("{}", &yy.to_string());
let (x, y): (u32, u32) = (5, 3);
println(&x.to_string());
println(&z.to_string());
println!("{}", &x.to_string());
println!("{}", &z.to_string());
let x: u32 = x;
println(&x.to_string());
println!("{}", &x.to_string());
let x = "hello";
println(x);
println!("{}", x);
let x = 32.0f32;
let _ = (x + ((x * x) + 1.0).sqrt()).ln();
@ -312,7 +309,7 @@ fn main() { // foo
let s3: some_fields = some_fields{ field1: 55};
let s4: msalias::nested_struct = sub::sub2::nested_struct{ field2: 55};
let s4: msalias::nested_struct = sub2::nested_struct{ field2: 55};
println(&s2.field1.to_string());
println!("{}", &s2.field1.to_string());
let s5: MyType = box some_fields{ field1: 55};
let s = SameDir::SameStruct{name: "Bob".to_string()};
let s = SubDir::SubStruct{name:"Bob".to_string()};

View File

@ -14,7 +14,7 @@ use std::fs::File;
use std::io::prelude::*;
use std::path::Path;
use std::process::Command;
use std::rand::{thread_rng, Rng};
use std::__rand::{thread_rng, Rng};
use std::{char, env};
// creates unicode_input_multiple_files_{main,chars}.rs, where the

View File

@ -15,7 +15,7 @@ use std::io::prelude::*;
use std::iter::repeat;
use std::path::Path;
use std::process::Command;
use std::rand::{thread_rng, Rng};
use std::__rand::{thread_rng, Rng};
use std::{char, env};
// creates a file with `fn main() { <random ident> }` and checks the