Skip to content

Commit

Permalink
For mozilla-mobile#10555 - Fix InputResultDetail hashcode() collisions
Browse files Browse the repository at this point in the history
The previous implementation was affected by the fact that the class has 3
integer properties, all with values [0..4] (-1 was added recently) and it
would just add those value to compute the hashcode.
Collisions were a given.

By using decimal places for each property the new implementation should avoid
collisions while allowing for all the other expected guarantees.
  • Loading branch information
Mugurell authored and Grisha Kruglov committed Sep 11, 2021
1 parent 8676bd6 commit 04c6dda
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ class InputResultDetail private constructor(

@Suppress("MagicNumber")
override fun hashCode(): Int {
var hash = inputResult.hashCode() * 31
hash += (scrollDirections.hashCode()) * 31
hash += (overscrollDirections.hashCode()) * 31
var hash = inputResult.hashCode()
hash += (scrollDirections.hashCode()) * 10
hash += (overscrollDirections.hashCode()) * 100

return hash
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,15 @@ class InputResultDetailTest {
assertTrue(inputResultDetail == inputResultDetail)
}

// @Test
// fun `GIVEN an InputResultDetail WHEN hashCode is called for same values objects THEN it returns the same result`() {
// assertEquals(inputResultDetail.hashCode(), inputResultDetail.hashCode())
//
// assertEquals(inputResultDetail.hashCode(), InputResultDetail.newInstance().hashCode())
//
// inputResultDetail = inputResultDetail.copy(OVERSCROLL_DIRECTIONS_VERTICAL)
// assertEquals(inputResultDetail.hashCode(), InputResultDetail.newInstance(true).hashCode())
// }
@Test
fun `GIVEN an InputResultDetail WHEN hashCode is called for same values objects THEN it returns the same result`() {
assertEquals(inputResultDetail.hashCode(), inputResultDetail.hashCode())

assertEquals(inputResultDetail.hashCode(), InputResultDetail.newInstance().hashCode())

inputResultDetail = inputResultDetail.copy(overscrollDirections = OVERSCROLL_DIRECTIONS_VERTICAL)
assertEquals(inputResultDetail.hashCode(), InputResultDetail.newInstance(true).hashCode())
}

@Test
fun `GIVEN an InputResultDetail WHEN hashCode is called for different values objects THEN it returns different results`() {
Expand Down

0 comments on commit 04c6dda

Please sign in to comment.