|
| 1 | +/* |
| 2 | + * Copyright 2014 Andreas Schildbach |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.twostack.bitcoin; |
| 18 | + |
| 19 | +import org.hamcrest.Matchers; |
| 20 | +import org.junit.Test; |
| 21 | + |
| 22 | +import java.math.BigDecimal; |
| 23 | + |
| 24 | +import static org.twostack.bitcoin.Coin.*; |
| 25 | +import static org.twostack.bitcoin.transaction.Transaction.MAX_MONEY; |
| 26 | +import static org.junit.Assert.*; |
| 27 | + |
| 28 | +public class CoinTest { |
| 29 | + |
| 30 | + @Test |
| 31 | + public void testParseCoin() { |
| 32 | + // String version |
| 33 | + assertEquals(CENT, parseCoin("0.01")); |
| 34 | + assertEquals(CENT, parseCoin("1E-2")); |
| 35 | + assertEquals(COIN.add(CENT), parseCoin("1.01")); |
| 36 | + assertEquals(COIN.negate(), parseCoin("-1")); |
| 37 | + try { |
| 38 | + parseCoin("2E-20"); |
| 39 | + org.junit.Assert.fail("should not have accepted fractional satoshis"); |
| 40 | + } catch (IllegalArgumentException expected) { |
| 41 | + } catch (Exception e) { |
| 42 | + org.junit.Assert.fail("should throw IllegalArgumentException"); |
| 43 | + } |
| 44 | + assertEquals(1, parseCoin("0.00000001").value); |
| 45 | + assertEquals(1, parseCoin("0.000000010").value); |
| 46 | + } |
| 47 | + |
| 48 | + @Test(expected = IllegalArgumentException.class) |
| 49 | + public void testParseCoinOverprecise() { |
| 50 | + parseCoin("0.000000011"); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testParseCoinInexact() { |
| 55 | + assertEquals(1, parseCoinInexact("0.00000001").value); |
| 56 | + assertEquals(1, parseCoinInexact("0.000000011").value); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testValueOf() { |
| 61 | + // int version |
| 62 | + assertEquals(CENT, valueOf(0, 1)); |
| 63 | + assertEquals(SATOSHI, valueOf(1)); |
| 64 | + assertEquals(NEGATIVE_SATOSHI, valueOf(-1)); |
| 65 | + assertEquals(Coin.valueOf(MAX_MONEY), valueOf(MAX_MONEY)); |
| 66 | + assertEquals(valueOf(MAX_MONEY).negate().value, valueOf(MAX_MONEY).value * -1); |
| 67 | + valueOf(MAX_MONEY + 1); |
| 68 | + valueOf((MAX_MONEY * -1) - 1); |
| 69 | + valueOf(Long.MAX_VALUE); |
| 70 | + valueOf(Long.MIN_VALUE); |
| 71 | + |
| 72 | + try { |
| 73 | + valueOf(1, -1); |
| 74 | + fail(); |
| 75 | + } catch (IllegalArgumentException e) {} |
| 76 | + try { |
| 77 | + valueOf(-1, 0); |
| 78 | + fail(); |
| 79 | + } catch (IllegalArgumentException e) {} |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testBtcToSatoshi() { |
| 85 | + assertEquals(Long.MIN_VALUE, btcToSatoshi(new BigDecimal("-92233720368.54775808"))); |
| 86 | + assertEquals(0L, btcToSatoshi(BigDecimal.ZERO)); |
| 87 | + assertEquals(COIN.value, btcToSatoshi(BigDecimal.ONE)); |
| 88 | + assertEquals(Long.MAX_VALUE, btcToSatoshi(new BigDecimal("92233720368.54775807"))); |
| 89 | + } |
| 90 | + |
| 91 | + @Test(expected = ArithmeticException.class) |
| 92 | + public void testBtcToSatoshi_tooSmall() { |
| 93 | + btcToSatoshi(new BigDecimal("-92233720368.54775809")); // .00000001 less than minimum value |
| 94 | + } |
| 95 | + |
| 96 | + @Test(expected = ArithmeticException.class) |
| 97 | + public void testBtcToSatoshi_tooBig() { |
| 98 | + btcToSatoshi(new BigDecimal("92233720368.54775808")); // .00000001 more than maximum value |
| 99 | + } |
| 100 | + |
| 101 | + @Test(expected = ArithmeticException.class) |
| 102 | + public void testBtcToSatoshi_tooPrecise1() { |
| 103 | + btcToSatoshi(new BigDecimal("0.000000001")); // More than SMALLEST_UNIT_EXPONENT precision |
| 104 | + } |
| 105 | + |
| 106 | + @Test(expected = ArithmeticException.class) |
| 107 | + public void testBtcToSatoshi_tooPrecise2() { |
| 108 | + btcToSatoshi(new BigDecimal("92233720368.547758079")); // More than SMALLEST_UNIT_EXPONENT precision |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testSatoshiToBtc() { |
| 113 | + assertThat(new BigDecimal("-92233720368.54775808"), Matchers.comparesEqualTo(satoshiToBtc(Long.MIN_VALUE))); |
| 114 | + assertThat(new BigDecimal("-0.00000001"), Matchers.comparesEqualTo(satoshiToBtc(NEGATIVE_SATOSHI.value))); |
| 115 | + assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(satoshiToBtc(0L))); |
| 116 | + assertThat(new BigDecimal("0.00000001"), Matchers.comparesEqualTo(satoshiToBtc(SATOSHI.value))); |
| 117 | + assertThat(BigDecimal.ONE, Matchers.comparesEqualTo(satoshiToBtc(COIN.value))); |
| 118 | + assertThat(new BigDecimal(50), Matchers.comparesEqualTo(satoshiToBtc(FIFTY_COINS.value))); |
| 119 | + assertThat(new BigDecimal("92233720368.54775807"), Matchers.comparesEqualTo(satoshiToBtc(Long.MAX_VALUE))); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void testOfBtc() { |
| 124 | + assertEquals(Coin.valueOf(Long.MIN_VALUE), Coin.ofBtc(new BigDecimal("-92233720368.54775808"))); |
| 125 | + assertEquals(ZERO, Coin.ofBtc(BigDecimal.ZERO)); |
| 126 | + assertEquals(COIN, Coin.ofBtc(BigDecimal.ONE)); |
| 127 | + assertEquals(Coin.valueOf(Long.MAX_VALUE), Coin.ofBtc(new BigDecimal("92233720368.54775807"))); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void testOperators() { |
| 132 | + assertTrue(SATOSHI.isPositive()); |
| 133 | + assertFalse(SATOSHI.isNegative()); |
| 134 | + assertFalse(SATOSHI.isZero()); |
| 135 | + assertFalse(NEGATIVE_SATOSHI.isPositive()); |
| 136 | + assertTrue(NEGATIVE_SATOSHI.isNegative()); |
| 137 | + assertFalse(NEGATIVE_SATOSHI.isZero()); |
| 138 | + assertFalse(ZERO.isPositive()); |
| 139 | + assertFalse(ZERO.isNegative()); |
| 140 | + assertTrue(ZERO.isZero()); |
| 141 | + |
| 142 | + assertTrue(valueOf(2).isGreaterThan(valueOf(1))); |
| 143 | + assertFalse(valueOf(2).isGreaterThan(valueOf(2))); |
| 144 | + assertFalse(valueOf(1).isGreaterThan(valueOf(2))); |
| 145 | + assertTrue(valueOf(1).isLessThan(valueOf(2))); |
| 146 | + assertFalse(valueOf(2).isLessThan(valueOf(2))); |
| 147 | + assertFalse(valueOf(2).isLessThan(valueOf(1))); |
| 148 | + } |
| 149 | + |
| 150 | + @Test(expected = ArithmeticException.class) |
| 151 | + public void testMultiplicationOverflow() { |
| 152 | + Coin.valueOf(Long.MAX_VALUE).multiply(2); |
| 153 | + } |
| 154 | + |
| 155 | + @Test(expected = ArithmeticException.class) |
| 156 | + public void testMultiplicationUnderflow() { |
| 157 | + Coin.valueOf(Long.MIN_VALUE).multiply(2); |
| 158 | + } |
| 159 | + |
| 160 | + @Test(expected = ArithmeticException.class) |
| 161 | + public void testAdditionOverflow() { |
| 162 | + Coin.valueOf(Long.MAX_VALUE).add(Coin.SATOSHI); |
| 163 | + } |
| 164 | + |
| 165 | + @Test(expected = ArithmeticException.class) |
| 166 | + public void testSubtractionUnderflow() { |
| 167 | + Coin.valueOf(Long.MIN_VALUE).subtract(Coin.SATOSHI); |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + public void testToBtc() { |
| 172 | + assertThat(new BigDecimal("-92233720368.54775808"), Matchers.comparesEqualTo(Coin.valueOf(Long.MIN_VALUE).toBtc())); |
| 173 | + assertThat(new BigDecimal("-0.00000001"), Matchers.comparesEqualTo(NEGATIVE_SATOSHI.toBtc())); |
| 174 | + assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(ZERO.toBtc())); |
| 175 | + assertThat(new BigDecimal("0.00000001"), Matchers.comparesEqualTo(SATOSHI.toBtc())); |
| 176 | + assertThat(BigDecimal.ONE, Matchers.comparesEqualTo(COIN.toBtc())); |
| 177 | + assertThat(new BigDecimal(50), Matchers.comparesEqualTo(FIFTY_COINS.toBtc())); |
| 178 | + assertThat(new BigDecimal("92233720368.54775807"), Matchers.comparesEqualTo(Coin.valueOf(Long.MAX_VALUE).toBtc())); |
| 179 | + } |
| 180 | + |
| 181 | + @Test |
| 182 | + public void testToFriendlyString() { |
| 183 | + assertEquals("1.00 BSV", COIN.toFriendlyString()); |
| 184 | + assertEquals("1.23 BSV", valueOf(1, 23).toFriendlyString()); |
| 185 | + assertEquals("0.001 BSV", COIN.divide(1000).toFriendlyString()); |
| 186 | + assertEquals("-1.23 BSV", valueOf(1, 23).negate().toFriendlyString()); |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Test the bitcoinValueToPlainString amount formatter |
| 191 | + */ |
| 192 | + @Test |
| 193 | + public void testToPlainString() { |
| 194 | + assertEquals("0.0015", Coin.valueOf(150000).toPlainString()); |
| 195 | + assertEquals("1.23", parseCoin("1.23").toPlainString()); |
| 196 | + |
| 197 | + assertEquals("0.1", parseCoin("0.1").toPlainString()); |
| 198 | + assertEquals("1.1", parseCoin("1.1").toPlainString()); |
| 199 | + assertEquals("21.12", parseCoin("21.12").toPlainString()); |
| 200 | + assertEquals("321.123", parseCoin("321.123").toPlainString()); |
| 201 | + assertEquals("4321.1234", parseCoin("4321.1234").toPlainString()); |
| 202 | + assertEquals("54321.12345", parseCoin("54321.12345").toPlainString()); |
| 203 | + assertEquals("654321.123456", parseCoin("654321.123456").toPlainString()); |
| 204 | + assertEquals("7654321.1234567", parseCoin("7654321.1234567").toPlainString()); |
| 205 | + assertEquals("87654321.12345678", parseCoin("87654321.12345678").toPlainString()); |
| 206 | + |
| 207 | + // check there are no trailing zeros |
| 208 | + assertEquals("1", parseCoin("1.0").toPlainString()); |
| 209 | + assertEquals("2", parseCoin("2.00").toPlainString()); |
| 210 | + assertEquals("3", parseCoin("3.000").toPlainString()); |
| 211 | + assertEquals("4", parseCoin("4.0000").toPlainString()); |
| 212 | + assertEquals("5", parseCoin("5.00000").toPlainString()); |
| 213 | + assertEquals("6", parseCoin("6.000000").toPlainString()); |
| 214 | + assertEquals("7", parseCoin("7.0000000").toPlainString()); |
| 215 | + assertEquals("8", parseCoin("8.00000000").toPlainString()); |
| 216 | + } |
| 217 | +} |
0 commit comments