philc / shoulda.js

Concise JavaScript unit testing micro framework.
MIT License
92 stars 3 forks source link

Support for tests on floating point numbers. #15

Open smblott-github opened 12 years ago

smblott-github commented 12 years ago

With floating point calculations, the actual result is hardware dependent. Two algebraically equivalent calculations may yield slightly different values.

Suggestion: add explicit support for testing such values.

Support might look something like:

assert.withinEpsillon 0.001, oldWordRelevancy(...), newWordRelevancy(...) * 2
assert.withinFactor 0.001, oldWordRelevancy(...), newWordRelevancy(...) * 2

In the first case, we're looking for an absolute difference less than the given constant. In the second case, we're looking for the ratio of the difference to the absolute value (of the second argument, say) to be less than the given constant.

This came up for me while working on word relevancy in vimium. Amazingly, my floating point tests using isEqual work. But I'd be more comfortable working with test which are more likely to succeed on a variety of hardware platforms.

You might argue that such tests don't make sense for floating point calculations: they're always hardware dependent. And you'd have a point.

Or you might argue that such tests can always be constructed using the existing isTrue. And you'd be right there, too. However, all tests can be constructed in terms of isTrue. But explicit support from shoulda makes such tests easier to write and easier to read.

smblott-github commented 12 years ago

If you buy the suggestion above, it would be natural to also have notWithinEpsillon and notWithinFactor.