35 lines
906 B
Kotlin

import kotlinx.coroutines.test.runTest
import kotlinx.datetime.Instant
import net.sergeych.crypto2.initCrypto
import net.sergeych.utools.nowToSeconds
import net.sergeych.utools.pack
import net.sergeych.utools.unpack
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.time.Duration.Companion.microseconds
class PackTest {
inline fun <reified T>check(x: T?) {
assertEquals(x, unpack<T>(pack(x)))
}
@Test
fun testNullPack() = runTest {
initCrypto()
val d = pack("Hello")
assertEquals(6, d.size)
check(1)
check(2L)
check(1.00)
check("hello")
check<String>(null)
check<Long?>(null)
}
@Test
fun testTimePack() = runTest {
initCrypto()
val t1 = nowToSeconds()
val t2 = t1 + 1.microseconds
assertEquals(t1, unpack<Instant>(pack(t2)))
}
}