mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-11-25 15:06:07 +00:00
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
26 lines
816 B
Go
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))
|
|
}
|
|
}
|
|
}
|