-
Notifications
You must be signed in to change notification settings - Fork 5.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Kotlin-test: floating point assertions. #3580
Kotlin-test: floating point assertions. #3580
Conversation
Add an assertion to compare the equality of two floating-point numbers within a specified delta precision.
…est. Add assertions to compare the equality of floating-point numbers within a specified delta precision.
Have you considered adding a default for |
@JakeWharton Yeah I had that thought too. I saw there's another issue that discussed adding a general extension for that. @elizarov seemed to think it didn't provide much value there, and I somewhat agree. I left it out here to keep it simple and keep it in-line with most testing frameworks' asserters. If we did go that route, it would also introduce a little ambiguity to the signature if that parameter were optional, so I might suggest just having separate methods with different names anyway. |
Cool just wanted to make sure it had been seen. I learned about it from google/truth#690. I suppose an |
The issue is updated:
|
@@ -57,11 +58,35 @@ fun <@OnlyInputTypes T> assertEquals(expected: T, actual: T, message: String? = | |||
asserter.assertEquals(message, expected, actual) | |||
} | |||
|
|||
/** Asserts that [actual] is within a [delta] value of the [expected] value with an optional [message]. */ | |||
@SinceKotlin("1.4") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SinceKotlin("1.5")
@@ -57,11 +58,35 @@ fun <@OnlyInputTypes T> assertEquals(expected: T, actual: T, message: String? = | |||
asserter.assertEquals(message, expected, actual) | |||
} | |||
|
|||
/** Asserts that [actual] is within a [delta] value of the [expected] value with an optional [message]. */ | |||
@SinceKotlin("1.4") | |||
fun assertEquals(expected: Double, actual: Double, delta: Double, message: String? = null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please rename all delta
arguments name to absoluteTolerance
@Test | ||
fun testAssertNotEqualsFloatFails() { | ||
checkFailedAssertion { assertNotEquals(0.1f, 0.11f, .1f) } | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add tests for NaN
and Infinite
values
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes requested
Moved to #4275 |
Add Float and Double implementations of
assertEquals
andassertNotEquals
that take a delta for comparing floating point values within a tolerance.Fixes KT-8364