3 ms·
I had this kind of thing when it came to property based testing. I built a property based testing library for ActionScript 3 (a fun journey in itself, with ful
by IanCal 3y ago
I had this kind of thing when it came to property based testing.
I built a property based testing library for ActionScript 3 (a fun journey in itself, with full test case reduction).
I was testing my testing library, and tried one of the most basic tests:
For any object A
A == decode(encode(A))
And discovered the fun of floating point values not being perfectly representable as strings.
The more significant one came from testing a UI library we'd built for TVs (so you has up, down, left, right as movement). We had in the spec that if you moved focus by pressing right, pressing left would take you back to the thing you were on before. The test looked something like
For an arbitrary series of API calls generating the UI:
For an arbitrary list of movements the user makes:
If the focus changes, pressing the opposite direction moves your focus back where you came from
Now, this was actually very easy to write as a test, but it's extremely powerful. It found a bug in an interesting corner case, so I fixed it. Fixing the bug broke an existing unit test. I checked and the unit test correctly tested something in the spec.
The spec was inconsistent, but because we'd tested explicit examples it had never been spotted. I've been a convert since.
I've never implemented property tests in an existing project without finding some bug.
I don't recommend building your own property testing library unless you really want to, I highly recommend in python hypothesis: https://hypothesis.readthedocs.io/en/latest/ https://hypothesis.readthedocs.io/en/latest/
- Tainnor 3y agoFloating point values are definitely perfectly representable as strings - worst case, you just output the binary as a string - but what you may refer to is that many exact fractions can't be represented exactly as floats and/or that floating-point arithmetic doesn't obey "normal" rules of arithmetic (e.g. addition isn't associative).
- IanCal 3y agoI'll clarify, as yes it's possible, but not using the built in conversions from strings to floats. That is, the following is not true for all x: Number(String(x)) == x
- UncleMeat 3y agoNaNs also cause problems here, since it is not equal to itself.
- joshjje 3y agoSometimes a framework will do weird things, like convert it to scientific notation, or round it, or add a ton of zeroes. Like BigDecimal in Java and Decimal in C#. They can sneak up on you.
- Tainnor 3y agoBigDecimals aren't floats, though. They're arbitrary precision rational numbers.