Skip to content

Commit

Permalink
enum consistency test
Browse files Browse the repository at this point in the history
  • Loading branch information
iaik-jheher committed Dec 2, 2024
1 parent e3d0a25 commit 7d4430c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
6 changes: 6 additions & 0 deletions indispensable/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ kotlin {
}
}

jvmTest {
dependencies {
implementation(kotlin("reflect"))
}
}

}
}

Expand Down
4 changes: 2 additions & 2 deletions indispensable/src/commonTest/kotlin/CryptoSignatureTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class CryptoSignatureTest : FreeSpec({
rsa1.hashCode() shouldNotBe rsa3.hashCode()

val ec4 = ec3.guessCurve()
ec4.scalarByteLength shouldBe ECCurve.values().minOf { it.scalarLength.bytes }
ec4.scalarByteLength shouldBe ECCurve.entries.minOf { it.scalarLength.bytes }
ec4 shouldBe ec4
ec4 shouldBe ec3
ec4.hashCode() shouldBe ec4.hashCode()
Expand Down Expand Up @@ -81,7 +81,7 @@ class CryptoSignatureTest : FreeSpec({
sig3.scalarByteLength shouldBe sig1.scalarByteLength
sig3.rawByteArray shouldBe encoded

val r2 = BigInteger.ONE.shl(ECCurve.values().maxOf { it.scalarLength.bits }.toInt() + 1)
val r2 = BigInteger.ONE.shl(ECCurve.entries.maxOf { it.scalarLength.bits }.toInt() + 1)
shouldThrow<IllegalArgumentException> { CryptoSignature.EC.fromRS(r2, s).guessCurve() }
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package at.asitplus.signum.indispensable

import at.asitplus.signum.indispensable.mac.MAC
import io.kotest.core.spec.style.FreeSpec
import io.kotest.matchers.nulls.shouldNotBeNull
import io.kotest.matchers.shouldBe
import io.kotest.matchers.types.shouldBeInstanceOf
import java.util.Stack
import kotlin.reflect.KClass
import kotlin.reflect.KProperty1
import kotlin.reflect.full.companionObject
import kotlin.reflect.full.memberProperties

inline fun<reified T: Any> FreeSpec.enumConsistencyTest() {
T::class.simpleName!! {
val listed = T::class.companionObject!!.let { companion ->
@Suppress("UNCHECKED_CAST")
(companion.memberProperties.find { it.name == "entries" }.shouldNotBeNull()
as KProperty1<Any, *>).get(companion.objectInstance!!)
}.shouldBeInstanceOf<Iterable<T>>()

val discovered = mutableSetOf<T>()
val queue = Stack<KClass<out T>>().also { it.push(T::class) }
while (!queue.empty()) {
val cls = queue.pop()
if (cls.java.isEnum) {
discovered.addAll(cls.java.enumConstants!!)
continue
}
val o = cls.objectInstance
if (o != null) {
discovered.add(o)
continue
}
cls.sealedSubclasses.forEach(queue::push)
}

listed.toSet() shouldBe discovered.toSet()
}
}

class EnumConsistencyTests : FreeSpec({
enumConsistencyTest<MAC>()
})

0 comments on commit 7d4430c

Please sign in to comment.