r/softwaretesting • • 4d ago

Golden-value tests for formula code: assert on the raw number or on the rounded value users see?

I maintain a side project with a few dozen numeric formulas (published equations, things like body surface area from height and weight). For regression tests I keep a JSON file of golden cases. The expected values are computed independently, with a separate re-implementation or the paper's own worked example, not by running the app's code.

The app rounds for display, so right now the test parses the displayed string and allows one unit in the last displayed decimal place:

val shown = displayed.substringBefore(" ").replace(",", "").toDouble()
val tol = 10.0.pow(-places) + 1e-9   // places = decimals shown, e.g. 2
if (abs(shown - expected) > tol) failures += "$id expected $expected got $shown"

It catches real formula mistakes (wrong exponent, wrong constant), but I'm not sure about the trade-off:

  • A tolerance of one display unit means 2.62 vs 2.63 passes. That's fine for half-up vs half-even at a .xx5 boundary, but it would also hide a small real error.
  • Asserting on the raw Double with a tight epsilon would be stricter, but then the test isn't checking what the user actually reads. Formatting or rounding bugs would slip through.

Is it normal to do both: a tight check on the raw value plus an exact string check on the formatted output? Or do people pick one? And for cases sitting right on a rounding boundary, do you avoid them in golden data or keep them on purpose?

6 Upvotes

13 comments sorted by

2

u/mystique0712 3d ago

Asserting on the raw Double with a tight epsilon and also doing a separate string check on the formatted output is a common approach.

This way you catch both formula logic errors and display/rounding bugs.

For boundary cases, it's usually best to either avoid them or include them with a specific tolerance that accounts for the expected rounding behavior.

1

u/ClassKlutzy7633 3d ago

Thanks, that's the split I landed on too. Raw value with a tolerance for the formula, and a separate check on the formatted string for display and rounding. Keeping them separate makes failures much easier to read.

4

u/Roboman20000 4d ago

I think the issue I found with asserting on exact values for calculations is that it's way too finicky. Floating point math is just nuts to deal with so I tend to make sure that the calculations are "within spec" meaning that either I choose or I get a Stakeholder to choose an amount of accuracy they want in their calculations and I assert to that instead. Even if the user doesn't see the full accuracy of the calculation there should be some sort of specification. Cause you don't want to assert = 1.0 to fail when the value comes to 1.00000000001 for example. So I'll assert to 1.0 +/- "some %" instead. Unless you do want that sort of accuracy. It's up to you.

1

u/zaphodikus 3d ago

Read up on rounding, its more usefull once you understand where rounding errors come from and to then write code to test to allow for that as a standard fixture or assert in your arsenal.

1

u/ClassKlutzy7633 3d ago

Good pointer, thanks. I've been bitten by rounding enough that a shared tolerance helper in the test fixtures makes sense instead of ad hoc epsilons in each test.

1

u/zaphodikus 3d ago

A lot of languages share the same math code, so the rounding errors they make will be the same. There is bankers rounding, and also integer rounding, as well as the 0.5 type rounding, which, incredibly, actually rounds up in some cases for 0.4999999999. Which when you pause, makes sense. I'm using a function that kinda hacks it still myself, but im not in financial services.

1

u/ClassKlutzy7633 2d ago

That 0.4999999999 case is what worries me for the x.xx5 rows. 2.675 isn't stored exactly, so it can land just under the boundary. Do you pick inputs that are exactly representable, like .125 or .375, or round through BigDecimal in the test?

1

u/Brainy-Cos 3d ago

doing both is pretty normal imo, they're testing two different things. raw value with a tight relative tolerance catches the formula bugs (relative not absolute, since your formulas probably live on pretty different scales). then a smaller set of exact string asserts on the formatted output catches rounding/formatting bugs. with only the display check and one unit of slack you can't really tell a small formula drift from a rounding mode difference

for boundary cases i'd keep them on purpose but label them, like a few golden rows that land exactly on x.xx5 where you assert the exact string you expect for whatever rounding mode you picked. those are the ones that break when someone changes the formatter. just make sure the raw input actually lands on the boundary and floating point doesn't turn it into x.xx4999, or that test gets flaky

also worth one test in a comma-decimal locale if you have users in e.g. germany. "2,63" with replace(",", "") turns into 263 lol

1

u/ClassKlutzy7633 2d ago edited 2d ago

Good call on the comma locale. "2,63" parsing as 263 is exactly the kind of thing I wouldn't notice on my own phone, so that's going in as its own test, and I'll label the x.xx5 rows so it's clear they're there on purpose.

1

u/Glad_Butterfly_4424 2d ago

Your current tolerance seems a bit generous. If you're displaying two decimal places, allowing a difference of 0.01 could let a genuine calculation error through. I'd keep the numerical accuracy check separate from whatever rounding you use for display.

1

u/ClassKlutzy7633 2d ago

Yeah, one display unit is loose enough that a real 0.01 drift would slip through. I'll tighten the raw check and keep the string compare as its own test.