async hashing of channels and flows (big data)

This commit is contained in:
sergeych committed 2024-08-25 02:07:33 +02:00
1 parent 054252a3ce
commit 95c052a22c
4 files changed
+144 -10

No files matched your search

+38
View File
@@ -0,0 +1,38 @@
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.test.runTest
import kotlinx.datetime.Clock
import net.sergeych.crypto2.Hash
import net.sergeych.crypto2.initCrypto
import kotlin.random.Random
import kotlin.random.nextUBytes
import kotlin.test.*
@Suppress("UNUSED_PARAMETER", "UNUSED_VARIABLE")
suspend fun <T> sw(label: String, f: suspend () -> T): T {
val t1 = Clock.System.now()
val result = f()
val t2 = Clock.System.now()
// println("$label: ${t2 - t1}")
return result
}
class HashTest {
@Test
fun testEqualMethods() {
fun testMethod(h: Hash) = runTest {
initCrypto()
val a = Random.Default.nextUBytes(1024)
val b = Random.Default.nextUBytes(1024)
val c = a + b
val h1 = sw("dir $h") { h.digest(c) }
val h2 = sw("ind $h") { h.ofFlow(listOf(a, b).asFlow()) }
assertContentEquals(h1, h2)
}
for (i in 0..10) {
testMethod(Hash.Blake2b)
testMethod(Hash.Sha3_256)
testMethod(Hash.Sha3_384)
}
}
}