2013-12-18 16:24:26 -06:00
|
|
|
// 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.
|
|
|
|
|
2015-01-10 15:42:48 -06:00
|
|
|
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
|
2014-12-27 18:20:47 -06:00
|
|
|
use std::rand::{thread_rng, Rng, Rand};
|
2015-02-17 17:10:25 -06:00
|
|
|
use std::thread;
|
2013-12-18 16:24:26 -06:00
|
|
|
|
2015-01-25 15:05:03 -06:00
|
|
|
const REPEATS: usize = 5;
|
|
|
|
const MAX_LEN: usize = 32;
|
2015-01-10 15:42:48 -06:00
|
|
|
static drop_counts: [AtomicUsize; MAX_LEN] =
|
|
|
|
// FIXME #5244: AtomicUsize is not Copy.
|
2014-10-14 06:20:59 -05:00
|
|
|
[
|
2015-01-10 15:42:48 -06:00
|
|
|
ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT,
|
|
|
|
ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT,
|
|
|
|
ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT,
|
|
|
|
ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT,
|
|
|
|
ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT,
|
|
|
|
ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT,
|
|
|
|
ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT,
|
|
|
|
ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT,
|
|
|
|
ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT,
|
|
|
|
ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT,
|
|
|
|
ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT,
|
2014-10-14 06:20:59 -05:00
|
|
|
];
|
2013-12-18 16:24:26 -06:00
|
|
|
|
2015-01-10 15:42:48 -06:00
|
|
|
static creation_count: AtomicUsize = ATOMIC_USIZE_INIT;
|
2014-10-14 06:20:59 -05:00
|
|
|
|
2014-12-30 22:32:49 -06:00
|
|
|
#[derive(Clone, PartialEq, PartialOrd, Eq, Ord)]
|
2015-01-25 15:05:03 -06:00
|
|
|
struct DropCounter { x: usize, creation_id: usize }
|
2014-10-14 06:20:59 -05:00
|
|
|
|
|
|
|
impl Rand for DropCounter {
|
|
|
|
fn rand<R: Rng>(rng: &mut R) -> DropCounter {
|
|
|
|
// (we're not using this concurrently, so Relaxed is fine.)
|
2015-01-02 01:53:35 -06:00
|
|
|
let num = creation_count.fetch_add(1, Ordering::Relaxed);
|
2013-12-18 16:24:26 -06:00
|
|
|
DropCounter {
|
2014-10-14 06:20:59 -05:00
|
|
|
x: rng.gen(),
|
|
|
|
creation_id: num
|
2013-12-18 16:24:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for DropCounter {
|
|
|
|
fn drop(&mut self) {
|
2015-01-02 01:53:35 -06:00
|
|
|
drop_counts[self.creation_id].fetch_add(1, Ordering::Relaxed);
|
2013-12-18 16:24:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() {
|
2015-02-19 23:05:35 -06:00
|
|
|
assert!(MAX_LEN <= std::usize::BITS as usize);
|
2013-12-18 16:24:26 -06:00
|
|
|
// len can't go above 64.
|
2015-01-26 14:46:12 -06:00
|
|
|
for len in 2..MAX_LEN {
|
|
|
|
for _ in 0..REPEATS {
|
2014-10-14 06:20:59 -05:00
|
|
|
// reset the count for these new DropCounters, so their
|
|
|
|
// IDs start from 0.
|
2015-01-02 01:53:35 -06:00
|
|
|
creation_count.store(0, Ordering::Relaxed);
|
2014-10-14 06:20:59 -05:00
|
|
|
|
2014-12-27 18:20:47 -06:00
|
|
|
let main = thread_rng().gen_iter::<DropCounter>()
|
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
|
|
|
.take(len)
|
|
|
|
.collect::<Vec<DropCounter>>();
|
2013-12-18 16:24:26 -06:00
|
|
|
|
|
|
|
// work out the total number of comparisons required to sort
|
|
|
|
// this array...
|
2015-02-18 04:42:01 -06:00
|
|
|
let mut count = 0_usize;
|
2015-02-01 20:53:25 -06:00
|
|
|
main.clone().sort_by(|a, b| { count += 1; a.cmp(b) });
|
2013-12-18 16:24:26 -06:00
|
|
|
|
2014-10-09 14:17:22 -05:00
|
|
|
// ... and then panic on each and every single one.
|
2015-01-25 15:05:03 -06:00
|
|
|
for panic_countdown in 0..count {
|
2013-12-18 16:24:26 -06:00
|
|
|
// refresh the counters.
|
2015-01-31 11:20:46 -06:00
|
|
|
for c in &drop_counts {
|
2015-01-02 01:53:35 -06:00
|
|
|
c.store(0, Ordering::Relaxed);
|
2013-12-18 16:24:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
let v = main.clone();
|
|
|
|
|
2015-02-17 17:10:25 -06:00
|
|
|
let _ = thread::spawn(move|| {
|
2015-01-02 01:53:35 -06:00
|
|
|
let mut v = v;
|
|
|
|
let mut panic_countdown = panic_countdown;
|
2015-02-01 20:53:25 -06:00
|
|
|
v.sort_by(|a, b| {
|
2015-01-02 01:53:35 -06:00
|
|
|
if panic_countdown == 0 {
|
|
|
|
panic!()
|
|
|
|
}
|
|
|
|
panic_countdown -= 1;
|
|
|
|
a.cmp(b)
|
|
|
|
})
|
|
|
|
}).join();
|
2013-12-18 16:24:26 -06:00
|
|
|
|
|
|
|
// check that the number of things dropped is exactly
|
|
|
|
// what we expect (i.e. the contents of `v`).
|
2014-10-14 06:20:59 -05:00
|
|
|
for (i, c) in drop_counts.iter().enumerate().take(len) {
|
2015-01-02 01:53:35 -06:00
|
|
|
let count = c.load(Ordering::Relaxed);
|
2014-10-14 06:20:59 -05:00
|
|
|
assert!(count == 1,
|
|
|
|
"found drop count == {} for i == {}, len == {}",
|
|
|
|
count, i, len);
|
2013-12-18 16:24:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|