kotest icon indicating copy to clipboard operation
kotest copied to clipboard

Tolerance matchers fail on edge-case numeric inputs

Open erik opened this issue 2 years ago • 3 comments

While working on #3096, the property-based checker found some edge cases for tolerance matchers which don't appear to work correctly (or at least have somewhat strange behavior).

Cases where we should probably throw an IllegalArgumentException:

(Float.NaN).shouldBeWithinPercentageOf(Float.NaN, 1.0)         // => NaN should be in NaN..NaN
(Double.NaN).shouldBeWithinPercentageOf(Double.NaN, 1.0)       // => NaN should be in NaN..NaN
0.shouldBeWithinPercentageOf(0, Double.POSITIVE_INFINITY)      // => 0 should be in NaN..NaN
0L.shouldBeWithinPercentageOf(0L, Double.POSITIVE_INFINITY)    // => 0 should be in NaN..NaN
0f.shouldBeWithinPercentageOf(0f, Double.POSITIVE_INFINITY)    // => 0.0 should be in NaN..NaN
0.0.shouldBeWithinPercentageOf(0.0, Double.POSITIVE_INFINITY)  // => 0.0 should be in NaN..NaN

Cases where behavior differs from x shouldBe (y plusOrMinus z.percent):

// Applies to both Double and Float

NEGATIVE_INFINITY shouldBe (NEGATIVE_INFINITY plusOrMinus 1.percent) // OK
POSITIVE_INFINITY shouldBe (POSITIVE_INFINITY plusOrMinus 1.percent) // OK

NEGATIVE_INFINITY.shouldBeWithinPercentageOf(NEGATIVE_INFINITY, 1.0) // -Infinity should be in -Infinity..NaN
POSITIVE_INFINITY.shouldBeWithinPercentageOf(POSITIVE_INFINITY, 1.0) // Infinity should be in NaN..Infinity

Which version of Kotest are you using #3096 - branched from 56573a7

erik avatar Jul 07 '22 07:07 erik

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Aug 12 '22 00:08 stale[bot]

Nice catch!

LeoColman avatar Aug 13 '22 15:08 LeoColman

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Sep 20 '22 21:09 stale[bot]

Was fixed in 5.6.0

sksamuel avatar Aug 27 '23 14:08 sksamuel