Allow formatting of negative BitCoin amounts, add a test for this.

Flush stderr/stdout when logging to try and keep them in sync.
Whitespace fixes.
This commit is contained in:
Mike Hearn
2011-04-25 21:51:59 +00:00
parent 1e2f3ae3e2
commit 84dcfecb5d
2 changed files with 26 additions and 13 deletions

View File

@@ -19,6 +19,8 @@ package com.google.bitcoin.core;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.fail;
import static com.google.bitcoin.core.Utils.*;
import org.junit.Test;
public class UtilsTest {
@@ -26,21 +28,27 @@ public class UtilsTest {
@Test
public void testToNanoCoins() {
// String version
assertEquals(Utils.CENT, Utils.toNanoCoins("0.01"));
assertEquals(Utils.CENT, Utils.toNanoCoins("1E-2"));
assertEquals(Utils.COIN.add(Utils.CENT), Utils.toNanoCoins("1.01"));
assertEquals(CENT, toNanoCoins("0.01"));
assertEquals(CENT, toNanoCoins("1E-2"));
assertEquals(COIN.add(Utils.CENT), toNanoCoins("1.01"));
try {
Utils.toNanoCoins("2E-20");
toNanoCoins("2E-20");
fail("should not have accepted fractional nanocoins");
} catch (ArithmeticException e) {
}
// int version
assertEquals(Utils.CENT, Utils.toNanoCoins(0, 1));
assertEquals(CENT, toNanoCoins(0, 1));
// TODO: should this really pass?
assertEquals(Utils.COIN.subtract(Utils.CENT), Utils.toNanoCoins(1, -1));
assertEquals(Utils.COIN.negate(), Utils.toNanoCoins(-1, 0));
assertEquals(Utils.COIN.negate(), Utils.toNanoCoins("-1"));
assertEquals(COIN.subtract(CENT), toNanoCoins(1, -1));
assertEquals(COIN.negate(), toNanoCoins(-1, 0));
assertEquals(COIN.negate(), toNanoCoins("-1"));
}
@Test
public void testFormatting() {
assertEquals("1.23", bitcoinValueToFriendlyString(toNanoCoins(1, 23)));
assertEquals("-1.23", bitcoinValueToFriendlyString(toNanoCoins(1, 23).negate()));
}
}