Skip to content
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

Conversation

ReneeVandervelde
Copy link
Contributor

Add Float and Double implementations of assertEquals and assertNotEquals that take a delta for comparing floating point values within a tolerance.

Fixes KT-8364

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.
@JakeWharton
Copy link
Contributor

Have you considered adding a default for delta so that it only need specified if the error-bound is known? There's more reading on it here: https://github.com/google/googletest/blob/master/googletest/docs/advanced.md#floating-point-comparison

@ReneeVandervelde
Copy link
Contributor Author

@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.

@JakeWharton
Copy link
Contributor

Cool just wanted to make sure it had been seen. I learned about it from google/truth#690. I suppose an assertNear could be added in the future if it's desired.

@qurbonzoda
Copy link
Contributor

The issue is updated:

We've discussed this issue and decided to introduce overloads for assertEquals and assertNotEquals taking three numbers, e.g.

assertEquals(expected: Double, actual: Double, absoluteTolerance: Double, message: String? = null)
assertNotEquals(illegal: Double, actual: Double, absoluteTolerance: Double, message: String? = null)

// same for Float

@@ -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")
Copy link
Contributor

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) {
Copy link
Contributor

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) }
}
Copy link
Contributor

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

Copy link
Contributor

@qurbonzoda qurbonzoda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes requested

@qurbonzoda
Copy link
Contributor

Moved to #4275

@qurbonzoda qurbonzoda closed this Apr 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants