/**
 * Copyright 2014 Andreas Schildbach
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.dogecoin.dogecoinj.core;

import static com.dogecoin.dogecoinj.core.Coin.*;
import static com.dogecoin.dogecoinj.core.NetworkParameters.MAX_MONEY;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;

import org.junit.Assert;
import org.junit.Test;

public class CoinTest {

    @Test
    public void testParseCoin() {
        // String version
        assertEquals(CENT, parseCoin("0.01"));
        assertEquals(CENT, parseCoin("1E-2"));
        assertEquals(COIN.add(CENT), parseCoin("1.01"));
        assertEquals(COIN.negate(), parseCoin("-1"));
        try {
            parseCoin("2E-20");
            org.junit.Assert.fail("should not have accepted fractional satoshis");
        } catch (ArithmeticException e) {
        }
    }

    @Test
    public void testValueOf() {
        // int version
        assertEquals(CENT, valueOf(0, 1));
        assertEquals(SATOSHI, valueOf(1));
        assertEquals(NEGATIVE_SATOSHI, valueOf(-1));
        assertEquals(MAX_MONEY, valueOf(MAX_MONEY.value));
        assertEquals(MAX_MONEY.negate(), valueOf(MAX_MONEY.value * -1));
        try {
            valueOf(MAX_MONEY.value + 1);
            org.junit.Assert.fail("should not have accepted too-great a monetary value");
        } catch (IllegalArgumentException e) {
        }
        try {
            valueOf( (MAX_MONEY.value * -1) - 1);
            org.junit.Assert.fail("should not have accepted too-little a monetary value");
        } catch (IllegalArgumentException e) {
        }

        try {
            valueOf(Long.MIN_VALUE);
            fail();
        } catch (IllegalArgumentException e) {}

        try {
            valueOf(1, -1);
            fail();
        } catch (IllegalArgumentException e) {}
        try {
            valueOf(-1, 0);
            fail();
        } catch (IllegalArgumentException e) {}
    }

    @Test
    public void testOperators() {
        assertTrue(SATOSHI.isPositive());
        assertFalse(SATOSHI.isNegative());
        assertFalse(SATOSHI.isZero());
        assertFalse(NEGATIVE_SATOSHI.isPositive());
        assertTrue(NEGATIVE_SATOSHI.isNegative());
        assertFalse(NEGATIVE_SATOSHI.isZero());
        assertFalse(ZERO.isPositive());
        assertFalse(ZERO.isNegative());
        assertTrue(ZERO.isZero());

        assertTrue(valueOf(2).isGreaterThan(valueOf(1)));
        assertFalse(valueOf(2).isGreaterThan(valueOf(2)));
        assertFalse(valueOf(1).isGreaterThan(valueOf(2)));
        assertTrue(valueOf(1).isLessThan(valueOf(2)));
        assertFalse(valueOf(2).isLessThan(valueOf(2)));
        assertFalse(valueOf(2).isLessThan(valueOf(1)));
    }

    @Test
    public void testToFriendlyString() {
        assertEquals("1.00 BTC", COIN.toFriendlyString());
        assertEquals("1.23 BTC", valueOf(1, 23).toFriendlyString());
        assertEquals("0.001 BTC", COIN.divide(1000).toFriendlyString());
        assertEquals("-1.23 BTC", valueOf(1, 23).negate().toFriendlyString());
    }

    /**
     * Test the bitcoinValueToPlainString amount formatter
     */
    @Test
    public void testToPlainString() {
        assertEquals("0.0015", Coin.valueOf(150000).toPlainString());
        assertEquals("1.23", parseCoin("1.23").toPlainString());

        assertEquals("0.1", parseCoin("0.1").toPlainString());
        assertEquals("1.1", parseCoin("1.1").toPlainString());
        assertEquals("21.12", parseCoin("21.12").toPlainString());
        assertEquals("321.123", parseCoin("321.123").toPlainString());
        assertEquals("4321.1234", parseCoin("4321.1234").toPlainString());
        assertEquals("54321.12345", parseCoin("54321.12345").toPlainString());
        assertEquals("654321.123456", parseCoin("654321.123456").toPlainString());
        assertEquals("7654321.1234567", parseCoin("7654321.1234567").toPlainString());
        try {
            assertEquals("87654321.12345678", parseCoin("87654321.12345678").toPlainString());
            Assert.fail();  // More than MAX_MONEY
        } catch (Exception e) {}

        // check there are no trailing zeros
        assertEquals("1", parseCoin("1.0").toPlainString());
        assertEquals("2", parseCoin("2.00").toPlainString());
        assertEquals("3", parseCoin("3.000").toPlainString());
        assertEquals("4", parseCoin("4.0000").toPlainString());
        assertEquals("5", parseCoin("5.00000").toPlainString());
        assertEquals("6", parseCoin("6.000000").toPlainString());
        assertEquals("7", parseCoin("7.0000000").toPlainString());
        assertEquals("8", parseCoin("8.00000000").toPlainString());
    }
}

@@ -3425,19 +3425,20 @@
 s(%221.00 
-BTC
+DOGE
 %22, COIN.
@@ -3481,27 +3481,28 @@
 quals(%221.23 
-BTC
+DOGE
 %22, valueOf(1
@@ -3556,19 +3556,20 @@
 (%220.001 
-BTC
+DOGE
 %22, COIN.
@@ -3634,11 +3634,12 @@
 .23 
-BTC
+DOGE
 %22, v
@@ -4532,32 +4532,33 @@
 nString());%0A
+%0A
         
 try %7B%0A      
@@ -4549,192 +4549,144 @@
     
-try %7B%0A            assertEquals(%2287654321.12345678%22, parseCoin(%2287654321.12345678%22).toPlainString());%0A            Assert.fail();  // More than MAX_MONEY%0A        %7D catch (Exception e) %7B%7D
+// More than MAX_MONEY. This will overlow to 0%0A        assertEquals(%220%22, parseCoin(String.valueOf(Long.MAX_VALUE + 1)).toPlainString());
 %0A%0A  

Fix coin tests