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

@ -91,14 +91,14 @@ public class Utils {
out[offset + 3] = (byte) (0xFF & (val >> 24));
}
public static void uint32ToByteStreamLE(long val, OutputStream stream) throws IOException {
public static void uint32ToByteStreamLE(long val, OutputStream stream) throws IOException {
stream.write((int)(0xFF & (val >> 0)));
stream.write((int)(0xFF & (val >> 8)));
stream.write((int)(0xFF & (val >> 16)));
stream.write((int)(0xFF & (val >> 24)));
}
public static void uint64ToByteStreamLE( BigInteger val, OutputStream stream) throws IOException {
public static void uint64ToByteStreamLE(BigInteger val, OutputStream stream) throws IOException {
byte[] bytes = val.toByteArray();
if (bytes.length > 8) {
throw new RuntimeException("Input too large to encode into a uint64");
@ -194,8 +194,10 @@ public class Utils {
static void LOG(String msg) {
// Set this to true to see debug prints from the library.
if (logging) {
System.out.print("BitCoin: ");
System.out.println(msg);
System.err.print("BitCoin: ");
System.err.println(msg);
System.err.flush();
System.out.flush();
}
}
@ -217,9 +219,12 @@ public class Utils {
/** Returns the given value in nanocoins as a 0.12 type string. */
public static String bitcoinValueToFriendlyString(BigInteger value) {
boolean negative = value.compareTo(BigInteger.ZERO) < 0;
if (negative)
value = value.negate();
BigInteger coins = value.divide(COIN);
BigInteger cents = value.remainder(COIN);
return String.format("%d.%02d", coins.intValue(), cents.intValue() / 1000000);
return String.format("%s%d.%02d", negative ? "-" : "", coins.intValue(), cents.intValue() / 1000000);
}
/**

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()));
}
}