Skip to content

Commit 5201bf1

Browse files
committed
auto merge of #18031 : huonw/rust/adjust-vec-sort-test, r=alexcrichton
Previously it had some uninituitive conditionals due to the interaction with the Rand construction and Clone reinitialisation to create sequential identifying numbers. This replaces all that with just constructing the DropCounters with the appropriate identifiers.
2 parents ea8a5df + 32513b0 commit 5201bf1

File tree

1 file changed

+43
-31
lines changed

1 file changed

+43
-31
lines changed

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

+43-31
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,56 @@
99
// except according to those terms.
1010

1111
use std::task;
12-
use std::rand::{task_rng, Rng};
12+
use std::sync::atomics::{AtomicUint, INIT_ATOMIC_UINT, Relaxed};
13+
use std::rand::{task_rng, Rng, Rand};
1314

14-
const MAX_LEN: uint = 20;
15-
static mut drop_counts: [uint, .. MAX_LEN] = [0, .. MAX_LEN];
16-
static mut clone_count: uint = 0;
15+
const REPEATS: uint = 5;
16+
const MAX_LEN: uint = 32;
17+
static drop_counts: [AtomicUint, .. MAX_LEN] =
18+
// FIXME #5244: AtomicUint is not Copy.
19+
[
20+
INIT_ATOMIC_UINT, INIT_ATOMIC_UINT, INIT_ATOMIC_UINT, INIT_ATOMIC_UINT,
21+
INIT_ATOMIC_UINT, INIT_ATOMIC_UINT, INIT_ATOMIC_UINT, INIT_ATOMIC_UINT,
22+
INIT_ATOMIC_UINT, INIT_ATOMIC_UINT, INIT_ATOMIC_UINT, INIT_ATOMIC_UINT,
23+
INIT_ATOMIC_UINT, INIT_ATOMIC_UINT, INIT_ATOMIC_UINT, INIT_ATOMIC_UINT,
1724

18-
#[deriving(Rand, PartialEq, PartialOrd, Eq, Ord)]
19-
struct DropCounter { x: uint, clone_num: uint }
25+
INIT_ATOMIC_UINT, INIT_ATOMIC_UINT, INIT_ATOMIC_UINT, INIT_ATOMIC_UINT,
26+
INIT_ATOMIC_UINT, INIT_ATOMIC_UINT, INIT_ATOMIC_UINT, INIT_ATOMIC_UINT,
27+
INIT_ATOMIC_UINT, INIT_ATOMIC_UINT, INIT_ATOMIC_UINT, INIT_ATOMIC_UINT,
28+
INIT_ATOMIC_UINT, INIT_ATOMIC_UINT, INIT_ATOMIC_UINT, INIT_ATOMIC_UINT,
29+
];
2030

21-
impl Clone for DropCounter {
22-
fn clone(&self) -> DropCounter {
23-
let num = unsafe { clone_count };
24-
unsafe { clone_count += 1; }
31+
static creation_count: AtomicUint = INIT_ATOMIC_UINT;
32+
33+
#[deriving(Clone, PartialEq, PartialOrd, Eq, Ord)]
34+
struct DropCounter { x: uint, creation_id: uint }
35+
36+
impl Rand for DropCounter {
37+
fn rand<R: Rng>(rng: &mut R) -> DropCounter {
38+
// (we're not using this concurrently, so Relaxed is fine.)
39+
let num = creation_count.fetch_add(1, Relaxed);
2540
DropCounter {
26-
x: self.x,
27-
clone_num: num
41+
x: rng.gen(),
42+
creation_id: num
2843
}
2944
}
3045
}
3146

3247
impl Drop for DropCounter {
3348
fn drop(&mut self) {
34-
unsafe {
35-
// Rand creates some with arbitrary clone_nums
36-
if self.clone_num < MAX_LEN {
37-
drop_counts[self.clone_num] += 1;
38-
}
39-
}
49+
drop_counts[self.creation_id].fetch_add(1, Relaxed);
4050
}
4151
}
4252

4353
pub fn main() {
54+
assert!(MAX_LEN <= std::uint::BITS);
4455
// len can't go above 64.
45-
for len in range(2u, MAX_LEN) {
46-
for _ in range(0i, 10) {
56+
for len in range(2, MAX_LEN) {
57+
for _ in range(0, REPEATS) {
58+
// reset the count for these new DropCounters, so their
59+
// IDs start from 0.
60+
creation_count.store(0, Relaxed);
61+
4762
let main = task_rng().gen_iter::<DropCounter>()
4863
.take(len)
4964
.collect::<Vec<DropCounter>>();
@@ -56,14 +71,13 @@ pub fn main() {
5671
// ... and then fail on each and every single one.
5772
for fail_countdown in range(0i, count) {
5873
// refresh the counters.
59-
unsafe {
60-
drop_counts = [0, .. MAX_LEN];
61-
clone_count = 0;
74+
for c in drop_counts.iter() {
75+
c.store(0, Relaxed);
6276
}
6377

6478
let v = main.clone();
6579

66-
task::try(proc() {
80+
let _ = task::try(proc() {
6781
let mut v = v;
6882
let mut fail_countdown = fail_countdown;
6983
v.as_mut_slice().sort_by(|a, b| {
@@ -77,13 +91,11 @@ pub fn main() {
7791

7892
// check that the number of things dropped is exactly
7993
// what we expect (i.e. the contents of `v`).
80-
unsafe {
81-
for (i, &c) in drop_counts.iter().enumerate() {
82-
let expected = if i < len {1} else {0};
83-
assert!(c == expected,
84-
"found drop count == {} for i == {}, len == {}",
85-
c, i, len);
86-
}
94+
for (i, c) in drop_counts.iter().enumerate().take(len) {
95+
let count = c.load(Relaxed);
96+
assert!(count == 1,
97+
"found drop count == {} for i == {}, len == {}",
98+
count, i, len);
8799
}
88100
}
89101
}

0 commit comments

Comments
 (0)