zcash-android-wallet-sdk/sdk-lib/src/test/java/cash/z/ecc/android/sdk/ext/ConversionsTest.kt

99 lines
3.1 KiB
Kotlin
Raw Normal View History

package cash.z.ecc.android.sdk.ext
2022-07-07 05:52:07 -07:00
import cash.z.ecc.android.sdk.model.Zatoshi
import org.junit.jupiter.api.Test
import java.math.BigDecimal
import java.math.MathContext
[#366] Fix Detekt warnings * Disable baseline file. Too many functions. * CurrencyFormatter.kt suppress too many functions * PersistentTransactionManager.kt suppress too many functions * OutboundTransactionManager suppress too many functions * Suppress long parameter list * Too many functions * Add log to avoid empty block warning * Fix several magic number warnings * Solve max line length warnings * Solve max line length warnings * Suppress too long method warnings * Suppress too complex method warnings * Suppress large class warning * Fixed empty catch block * Changed directory path to the file * Fix too generic and swallowed exception * Fix print stack trace warning * Suppressed single top level file name declaration * Change parameters name * Suppress Spread operator warning * Remove unused private code * Add Locale to suppress default locale used warning * Solve several forbidden TODOs warnings * Fixed another max line length warning * Simplify return statement * Suppress class to object change * Make DemoConstants variables const * Use error() instead of throwing an IllegalStateException * Solve too complex condition * Suppress intentionally generic and swallowed exception * Suppress TooGenericExceptionCaught * Solve or suppress several TooGenericExceptionCaught * Fix swallowed exception * Suppress warning TooGenericExceptionCaught of PersistentTransactionManager * Suppress warning TooGenericExceptionCaught of WalletTransactionEncoder * Suppress TooGenericExceptionCaught of SdkSynchronizer * Suppress TooGenericExceptionCaught in SaplingParamTool * Suppress TooGenericExceptionCaught in CompactBlockDownloader * Suppress TooGenericExceptionCaught in CheckpointTool * Fix TooGenericExceptionCaught in WalletService * Suppress TooGenericExceptionCaught in DerivedDataDb * Suppress TooGenericExceptionCaught in CompactBlockProcessor * Apply ktlint format after all the previous changes * Remove detekt baseline file * Set Android studio right margin * Address comments from review * Suppress failing tests on CI
2022-08-23 06:49:00 -07:00
import kotlin.test.assertEquals
class ConversionsTest {
@Test
fun `default right padding is 6`() {
2022-07-07 05:52:07 -07:00
assertEquals(1.13.toZec(6), Zatoshi(113000000L).convertZatoshiToZec())
assertEquals(1.13.toZec(6), 1.13.toZec())
}
@Test
fun `toZec uses banker's rounding`() {
assertEquals("1.004", 1.0035.toZecString(3))
assertEquals("1.004", 1.0045.toZecString(3))
}
@Test
fun `toZecString defaults to 6 digits`() {
2022-07-07 05:52:07 -07:00
assertEquals("1.123457", Zatoshi(112345678L).convertZatoshiToZecString())
}
@Test
fun `toZecString uses banker's rounding`() {
2022-07-07 05:52:07 -07:00
assertEquals("1.123456", Zatoshi(112345650L).convertZatoshiToZecString())
}
@Test
fun `toZecString honors minimum digits`() {
assertEquals("1.1000", 1.1.toZecString(6, 4))
}
@Test
fun `toZecString drops trailing zeros`() {
assertEquals("1.1", 1.10000000.toZecString(6, 0))
}
@Test
fun `toZecString limits trailing zeros`() {
assertEquals("1.10", 1.10000000.toZecString(6, 2))
}
@Test
fun `toZecString hides decimal when min is zero`() {
assertEquals("1", 1.0.toZecString(6, 0))
}
@Test
2019-03-28 23:05:45 -07:00
fun `toZecString defaults are reasonable`() {
// basically check for no extra zeros and banker's rounding
assertEquals("1", 1.0000000.toZecString())
assertEquals("0", 0.0000000.toZecString())
assertEquals("1.01", 1.0100000.toZecString())
assertEquals("1.000004", 1.0000035.toZecString())
assertEquals("1.000004", 1.0000045.toZecString())
assertEquals("1.000006", 1.0000055.toZecString())
}
@Test
2019-03-28 23:05:45 -07:00
fun `toUsdString defaults are reasonable`() {
// basically check for no extra zeros and banker's rounding
assertEquals("1.00", 1.0000000.toUsdString())
assertEquals("0", 0.0000000.toUsdString())
assertEquals("1.01", 1.0100000.toUsdString())
assertEquals("0.01", .0100000.toUsdString())
assertEquals("1.02", 1.025.toUsdString())
}
@Test
fun `toZecString zatoshi converts`() {
2022-07-07 05:52:07 -07:00
assertEquals("1.123456", Zatoshi(112345650L).convertZatoshiToZecString(6, 0))
}
@Test
fun `toZecString big decimal formats`() {
assertEquals("1.123", BigDecimal(1.123456789).toZecString(3, 0))
}
@Test
fun `toZec reduces precision`() {
val amount = "20.37905033625433054819645404524149".safelyConvertToBigDecimal('.')
val expected = "20.379050".safelyConvertToBigDecimal('.')
assertEquals(expected, amount.toZec(6))
assertEquals("20.37905", amount.toZecString(6))
}
@Test
fun `convert usd to zec`() {
val price = BigDecimal("49.07", MathContext.DECIMAL128)
val usdValue = "1000".safelyConvertToBigDecimal('.')
val zecValue = usdValue.convertUsdToZec(price)
assertEquals("20.379050".safelyConvertToBigDecimal('.'), zecValue.toZec(6))
}
2020-06-09 20:28:21 -07:00
}