45 lines
1.2 KiB
Kotlin

/*
* Copyright (c) 2025. Sergey S. Chernov - All Rights Reserved
*
* You may use, distribute and modify this code under the
* terms of the private license, which you must obtain from the author
*
* To obtain the license, contact the author: https://t.me/real_sergeych or email to
* real dot sergeych at gmail.
*/
import kotlinx.coroutines.test.runTest
import kotlin.time.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)))
}
}