Skip to content

Commit a25896b

Browse files
committed
share check_all_outcomes impl, and increase max iteration counts
1 parent 5caffb1 commit a25896b

File tree

3 files changed

+45
-60
lines changed

3 files changed

+45
-60
lines changed

src/tools/miri/tests/pass/0weak_memory/weak.rs

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ use std::sync::atomic::Ordering::*;
1313
use std::sync::atomic::{AtomicUsize, fence};
1414
use std::thread::spawn;
1515

16+
#[path = "../../utils/mod.rs"]
17+
mod utils;
18+
use utils::check_all_outcomes;
19+
1620
#[allow(dead_code)]
1721
#[derive(Copy, Clone)]
1822
struct EvilSend<T>(pub T);
@@ -33,35 +37,6 @@ fn spin_until(loc: &AtomicUsize, val: usize) -> usize {
3337
val
3438
}
3539

36-
/// Check that the function produces the intended set of outcomes.
37-
#[track_caller]
38-
fn check_all_outcomes<T: Eq + std::hash::Hash + std::fmt::Debug>(
39-
expected: impl IntoIterator<Item = T>,
40-
generate: impl Fn() -> T,
41-
) {
42-
use std::collections::HashSet;
43-
44-
let expected: HashSet<T> = HashSet::from_iter(expected);
45-
let mut seen = HashSet::new();
46-
// Let's give it N times as many tries as we are expecting values.
47-
let tries = expected.len() * 16;
48-
for i in 0..tries {
49-
let val = generate();
50-
assert!(expected.contains(&val), "got an unexpected value: {val:?}");
51-
seen.insert(val);
52-
if i > tries / 2 && expected.len() == seen.len() {
53-
// We saw everything and we did quite a few tries, let's avoid wasting time.
54-
return;
55-
}
56-
}
57-
// Let's see if we saw them all.
58-
for val in expected {
59-
if !seen.contains(&val) {
60-
panic!("did not get value that should be possible: {val:?}");
61-
}
62-
}
63-
}
64-
6540
fn relaxed() {
6641
check_all_outcomes([0, 1, 2], || {
6742
let x = static_atomic(0);

src/tools/miri/tests/pass/float_nan.rs

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
use std::fmt;
66
use std::hint::black_box;
77

8+
#[path = "../utils/mod.rs"]
9+
mod utils;
10+
use utils::check_all_outcomes;
11+
812
fn ldexp(a: f64, b: i32) -> f64 {
913
extern "C" {
1014
fn ldexp(x: f64, n: i32) -> f64;
@@ -26,35 +30,6 @@ enum NaNKind {
2630
}
2731
use NaNKind::*;
2832

29-
/// Check that the function produces the intended set of outcomes.
30-
#[track_caller]
31-
fn check_all_outcomes<T: Eq + std::hash::Hash + fmt::Display>(
32-
expected: impl IntoIterator<Item = T>,
33-
generate: impl Fn() -> T,
34-
) {
35-
use std::collections::HashSet;
36-
37-
let expected: HashSet<T> = HashSet::from_iter(expected);
38-
let mut seen = HashSet::new();
39-
// Let's give it N times as many tries as we are expecting values.
40-
let tries = expected.len() * 12;
41-
for i in 0..tries {
42-
let val = generate();
43-
assert!(expected.contains(&val), "got an unexpected value: {val}");
44-
seen.insert(val);
45-
if i > tries / 2 && expected.len() == seen.len() {
46-
// We saw everything and we did quite a few tries, let's avoid wasting time.
47-
return;
48-
}
49-
}
50-
// Let's see if we saw them all.
51-
for val in expected {
52-
if !seen.contains(&val) {
53-
panic!("did not get value that should be possible: {val}");
54-
}
55-
}
56-
}
57-
5833
// -- f32 support
5934
#[repr(C)]
6035
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
@@ -81,7 +56,7 @@ const F32_EXP: u32 = 8; // 8 bits of exponent
8156
const F32_MANTISSA: u32 = F32_SIGN_BIT - F32_EXP;
8257
const F32_NAN_PAYLOAD: u32 = F32_MANTISSA - 1;
8358

84-
impl fmt::Display for F32 {
59+
impl fmt::Debug for F32 {
8560
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
8661
// Alaways show raw bits.
8762
write!(f, "0x{:08x} ", self.0)?;
@@ -154,7 +129,7 @@ const F64_EXP: u32 = 11; // 11 bits of exponent
154129
const F64_MANTISSA: u32 = F64_SIGN_BIT - F64_EXP;
155130
const F64_NAN_PAYLOAD: u32 = F64_MANTISSA - 1;
156131

157-
impl fmt::Display for F64 {
132+
impl fmt::Debug for F64 {
158133
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
159134
// Alaways show raw bits.
160135
write!(f, "0x{:08x} ", self.0)?;

src/tools/miri/tests/utils/mod.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,38 @@ pub fn run_provenance_gc() {
1616
// SAFETY: No preconditions. The GC is fine to run at any time.
1717
unsafe { miri_run_provenance_gc() }
1818
}
19+
20+
/// Check that the function produces the intended set of outcomes.
21+
#[track_caller]
22+
pub fn check_all_outcomes<T: Eq + std::hash::Hash + std::fmt::Debug>(
23+
expected: impl IntoIterator<Item = T>,
24+
generate: impl Fn() -> T,
25+
) {
26+
use std::collections::HashSet;
27+
28+
let expected: HashSet<T> = HashSet::from_iter(expected);
29+
let mut seen = HashSet::new();
30+
// Let's give it N times as many tries as we are expecting values.
31+
let min_tries = std::cmp::max(20, expected.len() * 4);
32+
let max_tries = expected.len() * 50;
33+
for i in 0..max_tries {
34+
let val = generate();
35+
assert!(expected.contains(&val), "got an unexpected value: {val:?}");
36+
seen.insert(val);
37+
if i >= min_tries && expected.len() == seen.len() {
38+
// We saw everything and we did enough tries, let's avoid wasting time.
39+
return;
40+
}
41+
}
42+
// Let's see if we saw them all.
43+
if expected.len() == seen.len() {
44+
return;
45+
}
46+
// Find the missing one.
47+
for val in expected {
48+
if !seen.contains(&val) {
49+
panic!("did not get value that should be possible: {val:?}");
50+
}
51+
}
52+
unreachable!()
53+
}

0 commit comments

Comments
 (0)