FiatTest: Increase test coverage for Fiat class.
@@ -18,6 +18,7 @@ package org.bitcoinj.utils;
import static org.bitcoinj.utils.Fiat.parseFiat;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
import org.junit.Test;
@@ -48,6 +49,11 @@ public class FiatTest {
assertEquals(1, Fiat.parseFiatInexact("EUR", "0.00011").value);
}
+ @Test(expected = IllegalArgumentException.class)
+ public void testParseFiatInexactInvalidAmount() {
+ Fiat.parseFiatInexact("USD", "33.xx");
+ }
+
@Test
public void testToFriendlyString() {
assertEquals("1.00 EUR", parseFiat("EUR", "1").toFriendlyString());
@@ -73,4 +79,66 @@ public class FiatTest {
assertEquals("3", parseFiat("EUR", "3.000").toPlainString());
assertEquals("4", parseFiat("EUR", "4.0000").toPlainString());
}
+
+ @Test
+ public void testComparing() {
+ assertTrue(parseFiat("EUR", "1.11").isLessThan(parseFiat("EUR", "6.66")));
+ assertTrue(parseFiat("EUR", "6.66").isGreaterThan(parseFiat("EUR", "2.56")));
+ }
+
+ @Test
+ public void testSign() {
+ assertTrue(parseFiat("EUR", "-1").isNegative());
+ assertTrue(parseFiat("EUR", "-1").negate().isPositive());
+ assertTrue(parseFiat("EUR", "1").isPositive());
+ assertTrue(parseFiat("EUR", "0.00").isZero());
+
+ }
+
+ @Test
+ public void testCurrencyCode() {
+ assertEquals("RUB", parseFiat("RUB", "66.6").getCurrencyCode());
+ }
+
+ @Test
+ public void testValueFetching() {
+ Fiat fiat = parseFiat("USD", "666");
+ assertEquals(6660000, fiat.longValue());
+ assertEquals("6660000", fiat.toString());
+ }
+
+ @Test
+ public void testOperations() {
+ Fiat fiatA = parseFiat("USD", "666");
+ Fiat fiatB = parseFiat("USD", "2");
+
+ Fiat sumResult = fiatA.add(fiatB);
+ assertEquals(6680000, sumResult.getValue());
+ assertEquals("USD", sumResult.getCurrencyCode());
+
+ Fiat subResult = fiatA.subtract(fiatB);
+ assertEquals(6640000, subResult.getValue());
+ assertEquals("USD", subResult.getCurrencyCode());
+
+ Fiat divResult = fiatA.divide(2);
+ assertEquals(3330000, divResult.getValue());
+ assertEquals("USD", divResult.getCurrencyCode());
+
+ long ldivResult = fiatA.divide(fiatB);
+ assertEquals(333, ldivResult);
+
+ Fiat mulResult = fiatA.multiply(2);
+ assertEquals(13320000, mulResult.getValue());
+
+ Fiat[] fiats = fiatA.divideAndRemainder(3);
+ assertEquals(2, fiats.length);
+
+ Fiat fiat1 = fiats[0];
+ assertEquals(2220000, fiat1.getValue());
+ assertEquals("USD", fiat1.getCurrencyCode());
+
+ Fiat fiat2 = fiats[1];
+ assertEquals(0, fiat2.getValue());
+ assertEquals("USD", fiat2.getCurrencyCode());
+ }
}
