kaspad/util/amount_test.go
stasatdaglabs f46dec449d [NOD-510] Change all references to Bitcoin to Kaspa (#531)
* [NOD-510] Change coinbase flags to kaspad.

* [NOD-510] Removed superfluous spaces after periods in comments.

* [NOD-510] Rename btcd -> kaspad in the root folder.

* [NOD-510] Rename BtcEncode -> KaspaEncode and BtcDecode -> KaspaDecode.

* [NOD-510] Rename BtcEncode -> KaspaEncode and BtcDecode -> KaspaDecode.

* [NOD-510] Continue renaming btcd -> kaspad.

* [NOD-510] Rename btcjson -> kaspajson.

* [NOD-510] Rename file names inside kaspajson.

* [NOD-510] Rename kaspajson -> jsonrpc.

* [NOD-510] Finish renaming in addrmgr.

* [NOD-510] Rename package btcec to ecc.

* [NOD-510] Finish renaming stuff in blockdag.

* [NOD-510] Rename stuff in cmd.

* [NOD-510] Rename stuff in config.

* [NOD-510] Rename stuff in connmgr.

* [NOD-510] Rename stuff in dagconfig.

* [NOD-510] Rename stuff in database.

* [NOD-510] Rename stuff in docker.

* [NOD-510] Rename stuff in integration.

* [NOD-510] Rename jsonrpc to rpcmodel.

* [NOD-510] Rename stuff in limits.

* [NOD-510] Rename stuff in logger.

* [NOD-510] Rename stuff in mempool.

* [NOD-510] Rename stuff in mining.

* [NOD-510] Rename stuff in netsync.

* [NOD-510] Rename stuff in peer.

* [NOD-510] Rename stuff in release.

* [NOD-510] Rename stuff in rpcclient.

* [NOD-510] Rename stuff in server.

* [NOD-510] Rename stuff in signal.

* [NOD-510] Rename stuff in txscript.

* [NOD-510] Rename stuff in util.

* [NOD-510] Rename stuff in wire.

* [NOD-510] Fix failing tests.

* [NOD-510] Fix merge errors.

* [NOD-510] Fix go vet errors.

* [NOD-510] Remove merged file that's no longer relevant.

* [NOD-510] Add a comment above Op0.

* [NOD-510] Fix some comments referencing Bitcoin Core.

* [NOD-510] Fix some more comments referencing Bitcoin Core.

* [NOD-510] Fix bitcoin -> kaspa.

* [NOD-510] Fix more bitcoin -> kaspa.

* [NOD-510] Fix comments, remove DisconnectBlock in addrindex.

* [NOD-510] Rename KSPD to KASD.

* [NOD-510] Fix comments and user agent.
2019-12-12 15:21:41 +02:00

262 lines
5.1 KiB
Go

// Copyright (c) 2013, 2014 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package util_test
import (
"math"
"testing"
. "github.com/kaspanet/kaspad/util"
)
func TestAmountCreation(t *testing.T) {
tests := []struct {
name string
amount float64
valid bool
expected Amount
}{
// Positive tests.
{
name: "zero",
amount: 0,
valid: true,
expected: 0,
},
{
name: "max producible",
amount: 21e6,
valid: true,
expected: MaxSompi,
},
{
name: "exceeds max producible",
amount: 21e6 + 1e-8,
valid: true,
expected: MaxSompi + 1,
},
{
name: "one hundred",
amount: 100,
valid: true,
expected: 100 * SompiPerKaspa,
},
{
name: "fraction",
amount: 0.01234567,
valid: true,
expected: 1234567,
},
{
name: "rounding up",
amount: 54.999999999999943157,
valid: true,
expected: 55 * SompiPerKaspa,
},
{
name: "rounding down",
amount: 55.000000000000056843,
valid: true,
expected: 55 * SompiPerKaspa,
},
// Negative tests.
{
name: "not-a-number",
amount: math.NaN(),
valid: false,
},
{
name: "-infinity",
amount: math.Inf(-1),
valid: false,
},
{
name: "+infinity",
amount: math.Inf(1),
valid: false,
},
}
for _, test := range tests {
a, err := NewAmount(test.amount)
switch {
case test.valid && err != nil:
t.Errorf("%v: Positive test Amount creation failed with: %v", test.name, err)
continue
case !test.valid && err == nil:
t.Errorf("%v: Negative test Amount creation succeeded (value %v) when should fail", test.name, a)
continue
}
if a != test.expected {
t.Errorf("%v: Created amount %v does not match expected %v", test.name, a, test.expected)
continue
}
}
}
func TestAmountUnitConversions(t *testing.T) {
tests := []struct {
name string
amount Amount
unit AmountUnit
converted float64
s string
}{
{
name: "MKAS",
amount: MaxSompi,
unit: AmountMegaKAS,
converted: 21,
s: "21 MKAS",
},
{
name: "kKAS",
amount: 44433322211100,
unit: AmountKiloKAS,
converted: 444.33322211100,
s: "444.333222111 kKAS",
},
{
name: "KAS",
amount: 44433322211100,
unit: AmountKAS,
converted: 444333.22211100,
s: "444333.222111 KAS",
},
{
name: "mKAS",
amount: 44433322211100,
unit: AmountMilliKAS,
converted: 444333222.11100,
s: "444333222.111 mKAS",
},
{
name: "μKAS",
amount: 44433322211100,
unit: AmountMicroKAS,
converted: 444333222111.00,
s: "444333222111 μKAS",
},
{
name: "sompi",
amount: 44433322211100,
unit: AmountSompi,
converted: 44433322211100,
s: "44433322211100 Sompi",
},
{
name: "non-standard unit",
amount: 44433322211100,
unit: AmountUnit(-1),
converted: 4443332.2211100,
s: "4443332.22111 1e-1 KAS",
},
}
for _, test := range tests {
f := test.amount.ToUnit(test.unit)
if f != test.converted {
t.Errorf("%v: converted value %v does not match expected %v", test.name, f, test.converted)
continue
}
s := test.amount.Format(test.unit)
if s != test.s {
t.Errorf("%v: format '%v' does not match expected '%v'", test.name, s, test.s)
continue
}
// Verify that Amount.ToKAS works as advertised.
f1 := test.amount.ToUnit(AmountKAS)
f2 := test.amount.ToKAS()
if f1 != f2 {
t.Errorf("%v: ToKAS does not match ToUnit(AmountKAS): %v != %v", test.name, f1, f2)
}
// Verify that Amount.String works as advertised.
s1 := test.amount.Format(AmountKAS)
s2 := test.amount.String()
if s1 != s2 {
t.Errorf("%v: String does not match Format(AmountKAS): %v != %v", test.name, s1, s2)
}
}
}
func TestAmountMulF64(t *testing.T) {
tests := []struct {
name string
amt Amount
mul float64
res Amount
}{
{
name: "Multiply 0.1 KAS by 2",
amt: 100e5, // 0.1 KAS
mul: 2,
res: 200e5, // 0.2 KAS
},
{
name: "Multiply 0.2 KAS by 0.02",
amt: 200e5, // 0.2 KAS
mul: 1.02,
res: 204e5, // 0.204 KAS
},
{
name: "Round down",
amt: 49, // 49 Sompi
mul: 0.01,
res: 0,
},
{
name: "Round up",
amt: 50, // 50 Sompi
mul: 0.01,
res: 1, // 1 Sompi
},
{
name: "Multiply by 0.",
amt: 1e8, // 1 KAS
mul: 0,
res: 0, // 0 KAS
},
{
name: "Multiply 1 by 0.5.",
amt: 1, // 1 Sompi
mul: 0.5,
res: 1, // 1 Sompi
},
{
name: "Multiply 100 by 66%.",
amt: 100, // 100 Sompi
mul: 0.66,
res: 66, // 66 Sompi
},
{
name: "Multiply 100 by 66.6%.",
amt: 100, // 100 Sompi
mul: 0.666,
res: 67, // 67 Sompi
},
{
name: "Multiply 100 by 2/3.",
amt: 100, // 100 Sompi
mul: 2.0 / 3,
res: 67, // 67 Sompi
},
}
for _, test := range tests {
a := test.amt.MulF64(test.mul)
if a != test.res {
t.Errorf("%v: expected %v got %v", test.name, test.res, a)
}
}
}