Updated on 2026-08-14

This commit is contained in:
Tangem 2026-05-26 00:26:27 +04:00
parent ff2559df3e
commit 0bbc78f821
15 changed files with 368 additions and 17 deletions

View file

@ -30,4 +30,8 @@ dependencies {
implementation(deps.kotlin.coroutines)
implementation(deps.jodatime)
/** Tests */
testImplementation(deps.test.junit)
testImplementation(deps.test.truth)
testImplementation(deps.test.mockk)
}

View file

@ -0,0 +1,10 @@
package com.tangem.domain.swap.models
import java.math.BigDecimal
enum class PredefinedPercentAmount(val percent: BigDecimal) {
PERCENT_25(BigDecimal("0.25")),
PERCENT_50(BigDecimal("0.50")),
PERCENT_75(BigDecimal("0.75")),
MAX(BigDecimal.ONE),
}

View file

@ -0,0 +1,14 @@
package com.tangem.domain.swap.usecase
import com.tangem.domain.swap.models.PredefinedPercentAmount
import java.math.BigDecimal
import java.math.RoundingMode
class CalculateAmountUseCase {
operator fun invoke(balance: BigDecimal, decimals: Int, percent: PredefinedPercentAmount): BigDecimal {
return balance
.multiply(percent.percent)
.setScale(decimals, RoundingMode.DOWN)
}
}

View file

@ -0,0 +1,151 @@
package com.tangem.domain.swap.usecase
import com.google.common.truth.Truth.assertThat
import com.tangem.domain.swap.models.PredefinedPercentAmount
import org.junit.Test
import java.math.BigDecimal
class CalculateAmountUseCaseTest {
private val useCase = CalculateAmountUseCase()
@Test
fun `GIVEN balance and PERCENT_25 WHEN invoke THEN return one quarter of balance`() {
val balance = BigDecimal("100")
val decimals = 2
val result = useCase(
balance = balance,
decimals = decimals,
percent = PredefinedPercentAmount.PERCENT_25,
)
assertThat(result).isEqualTo(BigDecimal("25.00"))
}
@Test
fun `GIVEN balance and PERCENT_50 WHEN invoke THEN return half of balance`() {
val balance = BigDecimal("100")
val decimals = 2
val result = useCase(
balance = balance,
decimals = decimals,
percent = PredefinedPercentAmount.PERCENT_50,
)
assertThat(result).isEqualTo(BigDecimal("50.00"))
}
@Test
fun `GIVEN balance and PERCENT_75 WHEN invoke THEN return three quarters of balance`() {
val balance = BigDecimal("100")
val decimals = 2
val result = useCase(
balance = balance,
decimals = decimals,
percent = PredefinedPercentAmount.PERCENT_75,
)
assertThat(result).isEqualTo(BigDecimal("75.00"))
}
@Test
fun `GIVEN balance and MAX WHEN invoke THEN return full balance`() {
val balance = BigDecimal("100")
val decimals = 2
val result = useCase(
balance = balance,
decimals = decimals,
percent = PredefinedPercentAmount.MAX,
)
assertThat(result).isEqualTo(BigDecimal("100.00"))
}
@Test
fun `GIVEN zero balance WHEN invoke THEN return zero with decimals scale`() {
val balance = BigDecimal.ZERO
val decimals = 6
val result = useCase(
balance = balance,
decimals = decimals,
percent = PredefinedPercentAmount.PERCENT_50,
)
assertThat(result).isEqualTo(BigDecimal("0.000000"))
}
@Test
fun `GIVEN balance with more precision than decimals WHEN invoke THEN truncate result with rounding down`() {
val balance = BigDecimal("1.999999999999999999")
val decimals = 6
val result = useCase(
balance = balance,
decimals = decimals,
percent = PredefinedPercentAmount.PERCENT_25,
)
assertThat(result).isEqualTo(BigDecimal("0.499999"))
}
@Test
fun `GIVEN fractional percent product WHEN invoke THEN round down to decimals scale`() {
val balance = BigDecimal("1")
val decimals = 1
val result = useCase(
balance = balance,
decimals = decimals,
percent = PredefinedPercentAmount.PERCENT_75,
)
assertThat(result).isEqualTo(BigDecimal("0.7"))
}
@Test
fun `GIVEN zero decimals WHEN invoke THEN return integer value rounded down`() {
val balance = BigDecimal("9")
val decimals = 0
val result = useCase(
balance = balance,
decimals = decimals,
percent = PredefinedPercentAmount.PERCENT_75,
)
assertThat(result).isEqualTo(BigDecimal("6"))
}
@Test
fun `GIVEN high-precision balance and MAX WHEN invoke THEN preserve balance truncated to decimals`() {
val balance = BigDecimal("12.3456789012345678")
val decimals = 8
val result = useCase(
balance = balance,
decimals = decimals,
percent = PredefinedPercentAmount.MAX,
)
assertThat(result).isEqualTo(BigDecimal("12.34567890"))
}
@Test
fun `GIVEN large balance and PERCENT_50 WHEN invoke THEN return correctly scaled half`() {
val balance = BigDecimal("123456789.987654321")
val decimals = 4
val result = useCase(
balance = balance,
decimals = decimals,
percent = PredefinedPercentAmount.PERCENT_50,
)
assertThat(result).isEqualTo(BigDecimal("61728394.9938"))
}
}