[Gradle, Native] Override equals and hashCode in KonanTarget

"data objects" is only available since language version 1.9. But we should not use LV 1.9, because it is used in the Gradle build classpath and would be incompatible with Gradle Kotlin runtime.
Then only manually override equals/toString/ works here.

#KT-61657 Fixed


Merge-request: KT-MR-12759
Merged-by: Dmitrii Krasnov <Dmitrii.Krasnov@jetbrains.com>
This commit is contained in:
Dmitrii Krasnov
2023-11-06 18:55:40 +00:00
committed by Space Team
parent f8507d5220
commit aad19ab844
2 changed files with 44 additions and 4 deletions
@@ -69,4 +69,19 @@ sealed class KonanTarget(override val name: String, val family: Family, val arch
val deprecatedTargets = setOf(WATCHOS_X86, IOS_ARM32, LINUX_ARM32_HFP, MINGW_X86, LINUX_MIPS32, LINUX_MIPSEL32, WASM32)
val toleratedDeprecatedTargets = setOf(LINUX_ARM32_HFP)
}
override fun equals(other: Any?): Boolean {
return when {
this === other -> true
other !is KonanTarget -> false
else -> this.name == other.name && this.family == other.family && this.architecture == other.architecture
}
}
override fun hashCode(): Int {
var result = name.hashCode()
result = 31 * result + family.hashCode()
result = 31 * result + architecture.hashCode()
return result
}
}
@@ -5,16 +5,41 @@
package org.jetbrains.kotlin.konan.target
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
import java.nio.file.Path
class KonanTargetTest {
@Test
fun allPredefinedTargetsRegistered() {
assertEquals(
"Some of predefined KonanTarget instances are not listed in 'KonanTarget.predefinedTargets'",
KonanTarget::class.sealedSubclasses.mapNotNull { it.objectInstance }.toSet(),
KonanTarget.predefinedTargets.values.toSet()
KonanTarget.predefinedTargets.values.toSet(),
"Some of predefined KonanTarget instances are not listed in 'KonanTarget.predefinedTargets'",
)
}
@DisplayName("Test checks that all KonanTarget objects can be successfully serialized and deserialized")
@Test
fun checkKonanTargetsSerializationAndDeserialization(@TempDir serializedTargetsDir: Path) {
val konanTargetsBeforeSerialization = KonanTarget::class.sealedSubclasses.mapNotNull { it.objectInstance }.toSet()
val serializedTargetsFile = serializedTargetsDir.resolve("serializedTargets.txt")
ObjectOutputStream(FileOutputStream(serializedTargetsFile.toFile())).use {
it.writeObject(konanTargetsBeforeSerialization)
}
val konanTargetsAfterDeserialization = ObjectInputStream(FileInputStream(serializedTargetsFile.toFile())).use {
it.readObject()
}
assertEquals(konanTargetsBeforeSerialization, konanTargetsAfterDeserialization)
}
}