rust/src/test/run-pass/vector-sort-failure-safe.rs

92 lines
3.0 KiB
Rust
Raw Normal View History

// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
2013-12-22 02:32:17 -06:00
use std::task;
std: Recreate a `rand` module This commit shuffles around some of the `rand` code, along with some reorganization. The new state of the world is as follows: * The librand crate now only depends on libcore. This interface is experimental. * The standard library has a new module, `std::rand`. This interface will eventually become stable. Unfortunately, this entailed more of a breaking change than just shuffling some names around. The following breaking changes were made to the rand library: * Rng::gen_vec() was removed. This has been replaced with Rng::gen_iter() which will return an infinite stream of random values. Previous behavior can be regained with `rng.gen_iter().take(n).collect()` * Rng::gen_ascii_str() was removed. This has been replaced with Rng::gen_ascii_chars() which will return an infinite stream of random ascii characters. Similarly to gen_iter(), previous behavior can be emulated with `rng.gen_ascii_chars().take(n).collect()` * {IsaacRng, Isaac64Rng, XorShiftRng}::new() have all been removed. These all relied on being able to use an OSRng for seeding, but this is no longer available in librand (where these types are defined). To retain the same functionality, these types now implement the `Rand` trait so they can be generated with a random seed from another random number generator. This allows the stdlib to use an OSRng to create seeded instances of these RNGs. * Rand implementations for `Box<T>` and `@T` were removed. These seemed to be pretty rare in the codebase, and it allows for librand to not depend on liballoc. Additionally, other pointer types like Rc<T> and Arc<T> were not supported. If this is undesirable, librand can depend on liballoc and regain these implementations. * The WeightedChoice structure is no longer built with a `Vec<Weighted<T>>`, but rather a `&mut [Weighted<T>]`. This means that the WeightedChoice structure now has a lifetime associated with it. * The `sample` method on `Rng` has been moved to a top-level function in the `rand` module due to its dependence on `Vec`. cc #13851 [breaking-change]
2014-05-25 03:39:37 -05:00
use std::rand::{task_rng, Rng};
const MAX_LEN: uint = 20;
static mut drop_counts: [uint, .. MAX_LEN] = [0, .. MAX_LEN];
static mut clone_count: uint = 0;
#[deriving(Rand, PartialEq, PartialOrd, Eq, Ord)]
struct DropCounter { x: uint, clone_num: uint }
impl Clone for DropCounter {
fn clone(&self) -> DropCounter {
let num = unsafe { clone_count };
unsafe { clone_count += 1; }
DropCounter {
x: self.x,
clone_num: num
}
}
}
impl Drop for DropCounter {
fn drop(&mut self) {
unsafe {
// Rand creates some with arbitrary clone_nums
if self.clone_num < MAX_LEN {
drop_counts[self.clone_num] += 1;
}
}
}
}
pub fn main() {
// len can't go above 64.
for len in range(2u, MAX_LEN) {
for _ in range(0i, 10) {
std: Recreate a `rand` module This commit shuffles around some of the `rand` code, along with some reorganization. The new state of the world is as follows: * The librand crate now only depends on libcore. This interface is experimental. * The standard library has a new module, `std::rand`. This interface will eventually become stable. Unfortunately, this entailed more of a breaking change than just shuffling some names around. The following breaking changes were made to the rand library: * Rng::gen_vec() was removed. This has been replaced with Rng::gen_iter() which will return an infinite stream of random values. Previous behavior can be regained with `rng.gen_iter().take(n).collect()` * Rng::gen_ascii_str() was removed. This has been replaced with Rng::gen_ascii_chars() which will return an infinite stream of random ascii characters. Similarly to gen_iter(), previous behavior can be emulated with `rng.gen_ascii_chars().take(n).collect()` * {IsaacRng, Isaac64Rng, XorShiftRng}::new() have all been removed. These all relied on being able to use an OSRng for seeding, but this is no longer available in librand (where these types are defined). To retain the same functionality, these types now implement the `Rand` trait so they can be generated with a random seed from another random number generator. This allows the stdlib to use an OSRng to create seeded instances of these RNGs. * Rand implementations for `Box<T>` and `@T` were removed. These seemed to be pretty rare in the codebase, and it allows for librand to not depend on liballoc. Additionally, other pointer types like Rc<T> and Arc<T> were not supported. If this is undesirable, librand can depend on liballoc and regain these implementations. * The WeightedChoice structure is no longer built with a `Vec<Weighted<T>>`, but rather a `&mut [Weighted<T>]`. This means that the WeightedChoice structure now has a lifetime associated with it. * The `sample` method on `Rng` has been moved to a top-level function in the `rand` module due to its dependence on `Vec`. cc #13851 [breaking-change]
2014-05-25 03:39:37 -05:00
let main = task_rng().gen_iter::<DropCounter>()
.take(len)
.collect::<Vec<DropCounter>>();
// work out the total number of comparisons required to sort
// this array...
let mut count = 0;
main.clone().as_mut_slice().sort_by(|a, b| { count += 1; a.cmp(b) });
// ... and then fail on each and every single one.
for fail_countdown in range(0i, count) {
// refresh the counters.
unsafe {
drop_counts = [0, .. MAX_LEN];
clone_count = 0;
}
let v = main.clone();
2013-12-22 02:32:17 -06:00
task::try(proc() {
let mut v = v;
let mut fail_countdown = fail_countdown;
v.as_mut_slice().sort_by(|a, b| {
if fail_countdown == 0 {
fail!()
}
fail_countdown -= 1;
a.cmp(b)
})
});
// check that the number of things dropped is exactly
// what we expect (i.e. the contents of `v`).
unsafe {
for (i, &c) in drop_counts.iter().enumerate() {
let expected = if i < len {1} else {0};
assert!(c == expected,
"found drop count == {} for i == {}, len == {}",
c, i, len);
}
}
}
}
}
}