Skip to content

Commit 8c88114

Browse files
change the way we test non-determinism
1 parent 74068f2 commit 8c88114

File tree

1 file changed

+62
-80
lines changed

1 file changed

+62
-80
lines changed

tests/pass/float.rs

Lines changed: 62 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,51 +1275,35 @@ fn test_non_determinism() {
12751275
use std::{f32, f64};
12761276
// FIXME(powi_powf): add when supported
12771277

1278-
// TODO: should we do an `assert_ne!` on res1 and res2?
1279-
// it is possible that the same error is applied on
1280-
// seperate operations (unlikely but possible)
1281-
// and thus this `assert_ne!` can "sometimes" fail.
1282-
macro_rules! test_operation {
1283-
($a:expr, $b:expr, $op:path $(, $ulp:expr)?) => {
1284-
// 5 times enough?
1285-
let results: [_; 5] = ::core::array::from_fn(|i| (i, $op($a, $b)));
1286-
for (idx1, res1) in results {
1287-
for (idx2, res2) in results {
1288-
if idx1 == idx2 {
1289-
continue;
1290-
}
1291-
assert_approx_eq!(res1, res2$(, $ulp)?);
1292-
}
1293-
}
1294-
};
1295-
($a:expr, $op:path $(, $ulp:expr)?) => {
1296-
// 5 times enough?
1297-
let results: [_; 5] = ::core::array::from_fn(|i| (i, $op($a)));
1298-
for (idx1, res1) in results {
1299-
for (idx2, res2) in results {
1300-
if idx1 == idx2 {
1301-
continue;
1302-
}
1303-
assert_approx_eq!(res1, res2 $(, $ulp)?);
1304-
}
1278+
/// Ensure that the operation is non-deterministic
1279+
#[track_caller]
1280+
fn ensure<T: PartialEq + std::fmt::Debug>(f: impl Fn() -> T) {
1281+
let rounds = 16;
1282+
let first = f();
1283+
for _ in 1..rounds {
1284+
if f() != first {
1285+
// We saw two different values!
1286+
return;
13051287
}
1306-
};
1288+
}
1289+
// We saw the same thing N times.
1290+
panic!("expected non-determinism, got {rounds} times the same result: {first:?}");
13071291
}
13081292

13091293
macro_rules! test_operations_f {
13101294
($a:expr, $b:expr) => {
1311-
test_operation!($a, $b, fadd_algebraic);
1312-
test_operation!($a, $b, fsub_algebraic);
1313-
test_operation!($a, $b, fmul_algebraic);
1314-
test_operation!($a, $b, fdiv_algebraic);
1315-
test_operation!($a, $b, frem_algebraic);
1295+
ensure(|| fadd_algebraic($a, $b));
1296+
ensure(|| fsub_algebraic($a, $b));
1297+
ensure(|| fmul_algebraic($a, $b));
1298+
ensure(|| fdiv_algebraic($a, $b));
1299+
ensure(|| frem_algebraic($a, $b));
13161300

13171301
unsafe {
1318-
test_operation!($a, $b, fadd_fast);
1319-
test_operation!($a, $b, fsub_fast);
1320-
test_operation!($a, $b, fmul_fast);
1321-
test_operation!($a, $b, fdiv_fast);
1322-
test_operation!($a, $b, frem_fast);
1302+
ensure(|| fadd_fast($a, $b));
1303+
ensure(|| fsub_fast($a, $b));
1304+
ensure(|| fmul_fast($a, $b));
1305+
ensure(|| fdiv_fast($a, $b));
1306+
ensure(|| frem_fast($a, $b));
13231307
}
13241308
};
13251309
}
@@ -1329,63 +1313,61 @@ fn test_non_determinism() {
13291313
}
13301314
pub fn test_operations_f32(a: f32, b: f32) {
13311315
test_operations_f!(a, b);
1332-
// custom ulp for log
1333-
test_operation!(a, b, f32::log, 128);
1334-
test_operation!(a, f32::exp);
1316+
ensure(|| a.log(b));
1317+
ensure(|| a.exp());
13351318
}
13361319
pub fn test_operations_f64(a: f64, b: f64) {
13371320
test_operations_f!(a, b);
1338-
// custom ulp for log
1339-
test_operation!(a, b, f64::log, 128);
1340-
test_operation!(a, f64::exp);
1321+
ensure(|| a.log(b));
1322+
ensure(|| a.exp());
13411323
}
13421324
pub fn test_operations_f128(a: f128, b: f128) {
13431325
test_operations_f!(a, b);
13441326
}
13451327

13461328
pub fn test_extras() {
13471329
// f32
1348-
test_operation!(10f32, f32::exp2);
1349-
test_operation!(f32::consts::E, f32::ln);
1350-
test_operation!(0f32, f32::ln_1p);
1351-
test_operation!(10f32, f32::log10);
1352-
test_operation!(8f32, f32::log2);
1353-
test_operation!(27.0f32, f32::cbrt);
1354-
test_operation!(3.0f32, 4.0f32, f32::hypot);
1355-
test_operation!(0f32, f32::sin);
1356-
test_operation!(0f32, f32::cos);
1357-
test_operation!(1.0f32, f32::sinh);
1358-
test_operation!(1.0f32, f32::asinh);
1359-
test_operation!(1.0f32, f32::cosh);
1360-
test_operation!(2.0f32, f32::acosh);
1361-
test_operation!(1.0f32, f32::tan);
1362-
test_operation!(1.0f32, f32::tanh);
1363-
test_operation!(1.0f32, 2.0f32, f32::atan2);
1364-
test_operation!(0.5f32, f32::atanh);
1365-
test_operation!(5.0f32, f32::gamma);
1330+
ensure(|| 10f32.exp2());
1331+
ensure(|| f32::consts::E.ln());
1332+
ensure(|| 1f32.ln_1p());
1333+
ensure(|| 10f32.log10());
1334+
ensure(|| 8f32.log2());
1335+
ensure(|| 27.0f32.cbrt());
1336+
ensure(|| 3.0f32.hypot(4.0f32));
1337+
ensure(|| 1f32.sin());
1338+
ensure(|| 0f32.cos());
1339+
ensure(|| 1.0f32.sinh());
1340+
ensure(|| 1.0f32.asinh());
1341+
ensure(|| 1.0f32.cosh());
1342+
ensure(|| 2.0f32.acosh());
1343+
ensure(|| 1.0f32.tan());
1344+
ensure(|| 1.0f32.tanh());
1345+
ensure(|| 1.0f32.atan2(2.0f32));
1346+
ensure(|| 0.5f32.atanh());
1347+
ensure(|| 5.0f32.gamma());
13661348

13671349
// f64
1368-
test_operation!(50f64, f64::exp2);
1369-
test_operation!(1f64, f64::ln);
1370-
test_operation!(0f64, f64::ln_1p);
1371-
test_operation!(f64::consts::E, f64::log10);
1372-
test_operation!(f64::consts::E, f64::log2);
1373-
test_operation!(0f64, f64::sin);
1374-
test_operation!(0f64, f64::cos);
1375-
test_operation!(27.0f64, f64::cbrt);
1376-
test_operation!(3.0f64, 4.0f64, f64::hypot);
1377-
test_operation!(1.0f64, f64::sinh);
1378-
test_operation!(1.0f64, f64::asinh);
1379-
test_operation!(1.0f64, f64::cosh);
1380-
test_operation!(3.0f64, f64::acosh);
1381-
test_operation!(1.0f64, f64::tan);
1382-
test_operation!(1.0f64, f64::tanh);
1383-
test_operation!(0.5f64, f64::atanh);
1384-
test_operation!(5.0f64, f64::gamma);
1350+
ensure(|| 50f64.exp2());
1351+
ensure(|| 3f64.ln());
1352+
ensure(|| 1f64.ln_1p());
1353+
ensure(|| f64::consts::E.log10());
1354+
ensure(|| f64::consts::E.log2());
1355+
ensure(|| 1f64.sin());
1356+
ensure(|| 0f64.cos());
1357+
ensure(|| 27.0f64.cbrt());
1358+
ensure(|| 3.0f64.hypot(4.0f64));
1359+
ensure(|| 1.0f64.sinh());
1360+
ensure(|| 1.0f64.asinh());
1361+
ensure(|| 1.0f64.cosh());
1362+
ensure(|| 3.0f64.acosh());
1363+
ensure(|| 1.0f64.tan());
1364+
ensure(|| 1.0f64.tanh());
1365+
ensure(|| 0.5f64.atanh());
1366+
ensure(|| 5.0f64.gamma());
13851367
}
13861368

13871369
test_operations_f16(5., 7.);
1388-
test_operations_f32(12., 4.);
1370+
test_operations_f32(12., 5.);
13891371
test_operations_f64(19., 11.);
13901372
test_operations_f128(25., 18.);
13911373
test_extras();

0 commit comments

Comments
 (0)