Skip to content

Commit

Permalink
fix: correct hash code calculation for case-insensitive map entries (#…
Browse files Browse the repository at this point in the history
…1238)

* fix: correct hash code calculation for case-insensitive map entries

* remove commented-out code
  • Loading branch information
ianbotsf authored Feb 8, 2025
1 parent 1af0e82 commit 120768c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changes/c0040355-ffdc-4813-80e9-baf859ef02b9.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "c0040355-ffdc-4813-80e9-baf859ef02b9",
"type": "bugfix",
"description": "fix: correct hash code calculation for case-insensitive map entries"
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ internal class CaseInsensitiveMap<Value> : MutableMap<String, Value> {
return value
}

override fun hashCode(): Int = 17 * 31 + key!!.hashCode() + value!!.hashCode()
override fun hashCode(): Int = key.hashCode() xor value.hashCode() // Match JVM & K/N stdlib implementations

override fun equals(other: Any?): Boolean {
if (other == null || other !is Map.Entry<*, *>) return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ class CaseInsensitiveMapTest {
assertEquals(left.entries, right.entries)
}

@Test
fun testEntriesEqualityWithNormalMap() {
val left = CaseInsensitiveMap<String>()
left["A"] = "apple"
left["B"] = "banana"
left["C"] = "cherry"

val right = mutableMapOf(
"c" to "cherry",
"b" to "banana",
"a" to "apple",
)

assertEquals(left.entries, right.entries)
}

@Test
fun testToString() {
val map = CaseInsensitiveMap<String>()
Expand Down

0 comments on commit 120768c

Please sign in to comment.