-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AND-9343 Use longValueExact in Amount
- Loading branch information
Showing
7 changed files
with
76 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
blockchain/src/test/java/com/tangem/blockchain/common/AmountTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.tangem.blockchain.common | ||
|
||
import com.google.common.truth.Truth | ||
import org.junit.Test | ||
import java.math.BigDecimal | ||
import kotlin.math.pow | ||
|
||
/** | ||
* @author Andrew Khokhlov on 16/12/2024 | ||
*/ | ||
internal class AmountTests { | ||
|
||
@Test | ||
fun null_amount() { | ||
val amount = Amount(blockchain = Blockchain.Bitcoin).copy(value = null) | ||
|
||
try { | ||
amount.longValue | ||
|
||
error("Should be exception") | ||
} catch (e: Exception) { | ||
Truth.assertThat(e).isInstanceOf(IllegalArgumentException::class.java) | ||
Truth.assertThat(e).hasMessageThat().isEqualTo("Amount value is null") | ||
} | ||
} | ||
|
||
@Test | ||
fun zero_amount() { | ||
val amount = Amount(blockchain = Blockchain.Bitcoin).copy(value = BigDecimal.ZERO) | ||
|
||
Truth.assertThat(amount.longValue).isEqualTo(0L) | ||
} | ||
|
||
@Test | ||
fun simple_amount() { | ||
val value = 1000L | ||
val amount = Amount(blockchain = Blockchain.Bitcoin).copy(value = BigDecimal(value)) | ||
|
||
val expected = value * 10f.pow(Blockchain.Bitcoin.decimals()).toLong() | ||
|
||
Truth.assertThat(amount.longValue).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun long_max_value_amount() { | ||
val amount = Amount(blockchain = Blockchain.Bitcoin).copy(value = Long.MAX_VALUE.toBigDecimal()) | ||
|
||
try { | ||
amount.longValue | ||
|
||
error("Should be exception") | ||
} catch (e: Exception) { | ||
Truth.assertThat(e).isInstanceOf(ArithmeticException::class.java) | ||
Truth.assertThat(e).hasMessageThat().isEqualTo("Overflow") | ||
} | ||
} | ||
} |