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

Add stable hashCode() calculation to PrimitiveSerialDescriptor #2136

Merged
merged 3 commits into from
Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ internal class PrimitiveSerialDescriptor(
override fun getElementDescriptor(index: Int): SerialDescriptor = error()
override fun getElementAnnotations(index: Int): List<Annotation> = error()
override fun toString(): String = "PrimitiveDescriptor($serialName)"
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is PrimitiveSerialDescriptor) return false
if (serialName == other.serialName && kind == other.kind) return true
return false
}
override fun hashCode() = serialName.hashCode() + 31 * kind.hashCode()
private fun error(): Nothing = throw IllegalStateException("Primitive descriptor does not have elements")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package kotlinx.serialization.internal
sandwwraith marked this conversation as resolved.
Show resolved Hide resolved

import kotlinx.serialization.descriptors.PrimitiveKind
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotSame

class PrimitiveSerialDescriptorTest {

@Test
fun testEqualsImplemented() {
val first = PrimitiveSerialDescriptor("test_name", PrimitiveKind.LONG)
val second = PrimitiveSerialDescriptor("test_name", PrimitiveKind.LONG)

assertNotSame(first, second)
assertEquals(first, second)
}

@Test
fun testHashCodeStability() {
val first = PrimitiveSerialDescriptor("test_name", PrimitiveKind.LONG)
val second = PrimitiveSerialDescriptor("test_name", PrimitiveKind.LONG)

assertNotSame(first, second)
assertEquals(first.hashCode(), second.hashCode())
}

}