Skip to content

Commit 99fdc8a

Browse files
committed
test: Fix printing bitwise float reprs in error printing
In commit "test: Consolidate `Hexf`, `Hexi`, and the `Hex` trait" we unintentionally lost the difference between float hex and bitwise hex formatting; instead, the float hex was getting printed twice. Resolve this by printing the integer hex whenever the `-` format modifier is specified. This also makes things simpler because we no longer need to keep track of whether an `impl DisplayHex` is a float with `.to_bits()` available or an integer without it, we can always just try to print both forms. As a result, we can use a common error message for all validation checks.
1 parent 70ad484 commit 99fdc8a

2 files changed

Lines changed: 74 additions & 50 deletions

File tree

libm-test/src/test_traits.rs

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -232,19 +232,15 @@ where
232232
None => String::new(),
233233
};
234234

235-
anyhow::ensure!(
236-
result,
237-
"\
238-
\n input: {input:?} {ibits}\
239-
\n expected: {expected:<22?} {expbits}\
240-
\n actual: {actual:<22?} {actbits}\
241-
\n {msg}\
242-
",
243-
actbits = Hex(actual),
244-
expbits = Hex(expected),
245-
ibits = Hex(input),
246-
msg = make_xfail_msg()
247-
);
235+
if !result {
236+
bail!(make_error_message(
237+
input,
238+
expected,
239+
actual,
240+
"",
241+
&make_xfail_msg()
242+
));
243+
}
248244

249245
Ok(())
250246
}
@@ -360,23 +356,7 @@ where
360356
}
361357
}
362358

363-
res.with_context(|| {
364-
format!(
365-
"\
366-
\n input: {input:?}\
367-
\n as hex: {ihex}\
368-
\n as bits: {ibits}\
369-
\n expected: {expected:<22?} {exphex} {expbits}\
370-
\n actual: {actual:<22?} {acthex} {actbits}\
371-
",
372-
ihex = Hex(input),
373-
ibits = Hex(input),
374-
exphex = Hex(expected),
375-
expbits = Hex(expected),
376-
actbits = Hex(actual),
377-
acthex = Hex(actual),
378-
)
379-
})
359+
res.with_context(|| make_error_message(input, expected, actual, "", ""))
380360
}
381361

382362
impl_float!(f32, f64);
@@ -397,28 +377,25 @@ macro_rules! impl_tuples {
397377
where
398378
Input: Copy + DisplayHex + fmt::Debug,
399379
SpecialCase: MaybeOverride<Input>,
400-
{
380+
{
401381
fn validate<'a>(
402382
self,
403383
expected: Self,
404384
input: Input,
405385
ctx: &CheckCtx,
406386
) -> TestResult {
407-
self.0.validate(expected.0, input, ctx)
387+
self.0
388+
.validate(expected.0, input, ctx)
408389
.and_then(|()| self.1.validate(expected.1, input, ctx))
409-
.with_context(|| format!(
410-
"full context:\
411-
\n input: {input:?} {ibits}\
412-
\n as hex: {ihex}\
413-
\n as bits: {ibits}\
414-
\n expected: {expected:?} {expbits}\
415-
\n actual: {self:?} {actbits}\
416-
",
417-
ihex = Hex(input),
418-
ibits = Hex(input),
419-
expbits = Hex(expected),
420-
actbits = Hex(self),
421-
))
390+
.with_context(|| {
391+
make_error_message(
392+
input,
393+
expected,
394+
self,
395+
"full context:",
396+
"",
397+
)
398+
})
422399
}
423400
}
424401
)*
@@ -459,3 +436,33 @@ impl_tuples!(
459436
(f128, i32);
460437
(f128, f128);
461438
);
439+
440+
fn make_error_message<I, E, A>(
441+
input: I,
442+
expected: E,
443+
actual: A,
444+
pre_msg: &str,
445+
post_msg: &str,
446+
) -> String
447+
where
448+
I: Copy + fmt::Debug + DisplayHex,
449+
E: Copy + fmt::Debug + DisplayHex,
450+
A: Copy + fmt::Debug + DisplayHex,
451+
{
452+
let pre_pad = if pre_msg.is_empty() { "" } else { "\n " };
453+
let post_pad = if post_msg.is_empty() { "" } else { "\n " };
454+
format!(
455+
"\
456+
{pre_pad}{pre_msg}\
457+
\n input: {input:?}\
458+
\n as hex: {ihex}\
459+
\n as bits: {ihex:-}\
460+
\n expected: {expected:<16?} {exphex} {exphex:-}\
461+
\n actual: {actual:<16?} {acthex} {acthex:-}\
462+
{post_pad}{post_msg}\
463+
",
464+
ihex = Hex(input),
465+
exphex = Hex(expected),
466+
acthex = Hex(actual),
467+
)
468+
}

libm/src/math/support/hex_float.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,8 @@ mod hex_fmt {
378378
}
379379

380380
/// Types that can be formatted as hex via `Hex`. For ints we always print with a fixed
381-
/// number of leading zeros. For floats we use the IEEE hex (`%a`) representation.
381+
/// number of leading zeros. For floats we use the IEEE hex (`%a`) representation. The `-`
382+
/// format modifier is used to print the integer hex representation rather than hex float.
382383
pub trait DisplayHex {
383384
#[allow(unused)] // Only used for tests and public test internals
384385
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result;
@@ -408,7 +409,11 @@ mod hex_fmt {
408409

409410
impl<F: Float> DisplayHex for F {
410411
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
411-
fmt_any_hex(self, f)
412+
if f.sign_minus() {
413+
self.to_bits().fmt(f)
414+
} else {
415+
fmt_any_hex(self, f)
416+
}
412417
}
413418
}
414419

@@ -459,7 +464,9 @@ mod hex_fmt {
459464
T1: Copy + DisplayHex,
460465
{
461466
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
462-
write!(f, "({},)", Hex(self.0))
467+
write!(f, "(")?;
468+
self.0.fmt(f)?;
469+
write!(f, ",)")
463470
}
464471
}
465472

@@ -469,7 +476,11 @@ mod hex_fmt {
469476
T2: Copy + DisplayHex,
470477
{
471478
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
472-
write!(f, "({}, {})", Hex(self.0), Hex(self.1))
479+
write!(f, "(")?;
480+
self.0.fmt(f)?;
481+
write!(f, ", ")?;
482+
self.1.fmt(f)?;
483+
write!(f, ")")
473484
}
474485
}
475486

@@ -480,7 +491,13 @@ mod hex_fmt {
480491
T3: Copy + DisplayHex,
481492
{
482493
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
483-
write!(f, "({}, {}, {})", Hex(self.0), Hex(self.1), Hex(self.2))
494+
write!(f, "(")?;
495+
self.0.fmt(f)?;
496+
write!(f, ", ")?;
497+
self.1.fmt(f)?;
498+
write!(f, ", ")?;
499+
self.2.fmt(f)?;
500+
write!(f, ")")
484501
}
485502
}
486503
}

0 commit comments

Comments
 (0)