Skip to content

Commit

Permalink
FiatTest: Increase test coverage for Fiat class.
Browse files Browse the repository at this point in the history
  • Loading branch information
denis256 authored and Andreas Schildbach committed Mar 18, 2018
1 parent 6aba962 commit d30d83c
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions core/src/test/java/org/bitcoinj/utils/FiatTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static org.bitcoinj.utils.Fiat.parseFiat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

Expand Down Expand Up @@ -48,6 +49,11 @@ public void testParseFiatInexact() {
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());
Expand All @@ -73,4 +79,66 @@ public void testToPlainString() {
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());
}
}

0 comments on commit d30d83c

Please sign in to comment.