kaspad/cmd/kaspawallet/send_test.go
coderofstuff 471b7b2f16 Fix off by small amounts in sent amount kaspa
Floating point math causes inconsistencies when converting kas to sompi.

Use a method that parses the amount as a string, the converts it to
sompi then parse back to uint64
2023-09-22 08:45:26 -06:00

26 lines
816 B
Go

package main
import "testing"
func TestKasToSompi(t *testing.T) {
type testVector struct {
originalAmount float64
convertedAmount uint64
}
testVectors := []testVector{
{originalAmount: 0, convertedAmount: 0},
{originalAmount: 1, convertedAmount: 100000000},
{originalAmount: 33184.1489732, convertedAmount: 3318414897320},
{originalAmount: 21.35808032, convertedAmount: 2135808032},
{originalAmount: 184467440737.09551615, convertedAmount: 18446744073709551615},
}
for _, currentTestVector := range testVectors {
if kasToSompi(currentTestVector.originalAmount) != currentTestVector.convertedAmount {
t.Fail()
t.Logf("Expected %.8f, to convert to %d. Got: %d", currentTestVector.originalAmount, currentTestVector.convertedAmount, kasToSompi(currentTestVector.originalAmount))
}
}
}