[#595] Make Zatoshi comparable

This commit is contained in:
Carter Jernigan 2022-07-07 08:58:15 -04:00 committed by Carter Jernigan
parent a1cc8f0b69
commit 6764f7baf8
2 changed files with 19 additions and 1 deletions

View File

@ -7,7 +7,7 @@ package cash.z.ecc.android.sdk.model
* with ZEC, which is a decimal value represented only as a String. ZEC are not used internally,
* to avoid floating point imprecision.
*/
data class Zatoshi(val value: Long) {
data class Zatoshi(val value: Long) : Comparable<Zatoshi> {
init {
require(value >= MIN_INCLUSIVE) { "Zatoshi must be in the range [$MIN_INCLUSIVE, $MAX_INCLUSIVE]" }
require(value <= MAX_INCLUSIVE) { "Zatoshi must be in the range [$MIN_INCLUSIVE, $MAX_INCLUSIVE]" }
@ -16,6 +16,8 @@ data class Zatoshi(val value: Long) {
operator fun plus(other: Zatoshi) = Zatoshi(value + other.value)
operator fun minus(other: Zatoshi) = Zatoshi(value - other.value)
override fun compareTo(other: Zatoshi) = value.compareTo(other.value)
companion object {
/**
* The number of Zatoshi that equal 1 ZEC.

View File

@ -3,6 +3,7 @@ package cash.z.ecc.android.sdk.model
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertTrue
class ZatoshiTest {
@Test
@ -29,6 +30,21 @@ class ZatoshiTest {
assertEquals(Zatoshi(3), Zatoshi(4) - Zatoshi(1))
}
@Test
fun compare_equal() {
assertEquals(0, Zatoshi(1).compareTo(Zatoshi(1)))
}
@Test
fun compare_greater() {
assertTrue(Zatoshi(2) > Zatoshi(1))
}
@Test
fun compare_less() {
assertTrue(Zatoshi(1) < Zatoshi(2))
}
@Test
fun minus_fail() {
assertFailsWith<IllegalArgumentException> {