Skip to content

Commit

Permalink
Adding a test to demonstrate hash collisions in BiMap
Browse files Browse the repository at this point in the history
  • Loading branch information
nh13 committed Jul 24, 2019
1 parent 5d00342 commit 25358b8
Showing 1 changed file with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,22 @@ class BiMapTest extends UnitSpec {
map.add(2, "2")
map.values shouldBe Set("1", "2").toIterable
}

private case class CustomHashCode(code: Int) {
override def hashCode(): Int = code
}

"BiMap" should "handle hash collisions" in {
val map = new BiMap[CustomHashCode, Int]()

val obj1 = CustomHashCode(1)
val obj2 = CustomHashCode(2)
val obj3 = CustomHashCode(2)

map.add(obj1, obj1.code)
map.add(obj2, obj2.code)
map.add(obj3, obj3.code) // collision!

map.size shouldBe 2
}
}

0 comments on commit 25358b8

Please sign in to comment.