2018-03-25 10:45:54 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2018-05-09 05:20:16 +00:00
|
|
|
"encoding/json"
|
2020-03-04 10:26:08 +00:00
|
|
|
"math"
|
2018-11-26 15:56:45 +00:00
|
|
|
"strconv"
|
2018-03-25 10:45:54 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-01-22 06:58:24 +00:00
|
|
|
"github.com/go-yaml/yaml"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2018-03-25 10:45:54 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-12-12 15:52:23 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-03-25 10:45:54 +00:00
|
|
|
)
|
|
|
|
|
2019-08-23 12:41:22 +00:00
|
|
|
func TestFixed8FromInt64(t *testing.T) {
|
2019-08-23 10:35:34 +00:00
|
|
|
values := []int64{9000, 100000000, 5, 10945, -42}
|
2018-03-25 10:45:54 +00:00
|
|
|
|
|
|
|
for _, val := range values {
|
2019-08-23 12:41:22 +00:00
|
|
|
assert.Equal(t, Fixed8(val*decimals), Fixed8FromInt64(val))
|
2020-03-04 10:26:08 +00:00
|
|
|
assert.Equal(t, val, Fixed8FromInt64(val).IntegralValue())
|
|
|
|
assert.Equal(t, int32(0), Fixed8FromInt64(val).FractionalValue())
|
2019-08-23 10:35:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFixed8Add(t *testing.T) {
|
2019-08-23 12:41:22 +00:00
|
|
|
a := Fixed8FromInt64(1)
|
|
|
|
b := Fixed8FromInt64(2)
|
2019-08-23 10:35:34 +00:00
|
|
|
|
|
|
|
c := a.Add(b)
|
|
|
|
expected := int64(3)
|
|
|
|
assert.Equal(t, strconv.FormatInt(expected, 10), c.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFixed8Sub(t *testing.T) {
|
|
|
|
|
2019-08-23 12:41:22 +00:00
|
|
|
a := Fixed8FromInt64(42)
|
|
|
|
b := Fixed8FromInt64(34)
|
2019-08-23 10:35:34 +00:00
|
|
|
|
|
|
|
c := a.Sub(b)
|
2020-03-04 10:26:08 +00:00
|
|
|
assert.Equal(t, int64(8), c.IntegralValue())
|
|
|
|
assert.Equal(t, int32(0), c.FractionalValue())
|
2019-08-23 10:35:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFixed8FromFloat(t *testing.T) {
|
|
|
|
inputs := []float64{12.98, 23.87654333, 100.654322, 456789.12345665, -3.14159265}
|
|
|
|
|
|
|
|
for _, val := range inputs {
|
2019-08-23 12:41:22 +00:00
|
|
|
assert.Equal(t, Fixed8(val*decimals), Fixed8FromFloat(val))
|
|
|
|
assert.Equal(t, val, Fixed8FromFloat(val).FloatValue())
|
2020-03-04 10:26:08 +00:00
|
|
|
trunc := math.Trunc(val)
|
|
|
|
rem := (val - trunc) * decimals
|
|
|
|
assert.Equal(t, int64(trunc), Fixed8FromFloat(val).IntegralValue())
|
|
|
|
assert.Equal(t, int32(math.Round(rem)), Fixed8FromFloat(val).FractionalValue())
|
2018-03-25 10:45:54 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-04 17:41:42 +00:00
|
|
|
|
2019-08-23 12:41:22 +00:00
|
|
|
func TestFixed8FromString(t *testing.T) {
|
|
|
|
// Fixed8FromString works correctly with integers
|
2019-08-23 10:15:09 +00:00
|
|
|
ivalues := []string{"9000", "100000000", "5", "10945", "20.45", "0.00000001", "-42"}
|
2018-05-09 05:20:16 +00:00
|
|
|
for _, val := range ivalues {
|
2019-08-23 12:41:22 +00:00
|
|
|
n, err := Fixed8FromString(val)
|
2018-05-04 17:41:42 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, val, n.String())
|
|
|
|
}
|
|
|
|
|
2019-08-23 12:41:22 +00:00
|
|
|
// Fixed8FromString parses number with maximal precision
|
2018-05-04 17:41:42 +00:00
|
|
|
val := "123456789.12345678"
|
2019-08-23 12:41:22 +00:00
|
|
|
n, err := Fixed8FromString(val)
|
2018-05-04 17:41:42 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, Fixed8(12345678912345678), n)
|
|
|
|
|
2019-08-23 12:41:22 +00:00
|
|
|
// Fixed8FromString parses number with non-maximal precision
|
2018-05-04 17:41:42 +00:00
|
|
|
val = "901.2341"
|
2019-08-23 12:41:22 +00:00
|
|
|
n, err = Fixed8FromString(val)
|
2018-05-04 17:41:42 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, Fixed8(90123410000), n)
|
2019-11-24 14:37:44 +00:00
|
|
|
|
|
|
|
// Fixed8FromString with errors
|
|
|
|
val = "90n1"
|
|
|
|
_, err = Fixed8FromString(val)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
val = "90.1s"
|
|
|
|
_, err = Fixed8FromString(val)
|
|
|
|
assert.Error(t, err)
|
2018-05-04 17:41:42 +00:00
|
|
|
}
|
2018-05-09 05:20:16 +00:00
|
|
|
|
2019-08-23 12:37:01 +00:00
|
|
|
func TestSatoshi(t *testing.T) {
|
|
|
|
satoshif8 := Satoshi()
|
|
|
|
assert.Equal(t, "0.00000001", satoshif8.String())
|
|
|
|
}
|
|
|
|
|
2018-05-09 05:20:16 +00:00
|
|
|
func TestFixed8UnmarshalJSON(t *testing.T) {
|
2018-11-26 15:56:45 +00:00
|
|
|
var testCases = []float64{
|
|
|
|
123.45,
|
|
|
|
-123.45,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fl := range testCases {
|
|
|
|
str := strconv.FormatFloat(fl, 'g', -1, 64)
|
2019-08-23 12:41:22 +00:00
|
|
|
expected, _ := Fixed8FromString(str)
|
2018-11-26 15:56:45 +00:00
|
|
|
|
|
|
|
// UnmarshalJSON should decode floats
|
|
|
|
var u1 Fixed8
|
|
|
|
s, _ := json.Marshal(fl)
|
|
|
|
assert.Nil(t, json.Unmarshal(s, &u1))
|
|
|
|
assert.Equal(t, expected, u1)
|
|
|
|
|
|
|
|
// UnmarshalJSON should decode strings
|
|
|
|
var u2 Fixed8
|
|
|
|
s, _ = json.Marshal(str)
|
|
|
|
assert.Nil(t, json.Unmarshal(s, &u2))
|
|
|
|
assert.Equal(t, expected, u2)
|
|
|
|
}
|
2019-11-24 14:37:44 +00:00
|
|
|
|
|
|
|
errorCases := []string{
|
|
|
|
`"123.u"`,
|
|
|
|
"13.j",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range errorCases {
|
|
|
|
var u Fixed8
|
|
|
|
assert.Error(t, u.UnmarshalJSON([]byte(tc)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFixed8_MarshalJSON(t *testing.T) {
|
|
|
|
u, err := Fixed8FromString("123.4")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
s, err := json.Marshal(u)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, []byte(`"123.4"`), s)
|
|
|
|
}
|
|
|
|
|
2020-01-22 06:58:24 +00:00
|
|
|
func TestFixed8_UnmarshalYAML(t *testing.T) {
|
|
|
|
u, err := Fixed8FromString("123.4")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
s, err := yaml.Marshal(u)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, []byte("\"123.4\"\n"), s) // yaml marshaler inserts LF at the end
|
|
|
|
|
|
|
|
var f Fixed8
|
|
|
|
assert.NoError(t, yaml.Unmarshal([]byte(`"123.4"`), &f))
|
|
|
|
assert.Equal(t, u, f)
|
|
|
|
}
|
|
|
|
|
2019-11-24 14:37:44 +00:00
|
|
|
func TestFixed8_Arith(t *testing.T) {
|
|
|
|
u1 := Fixed8FromInt64(3)
|
|
|
|
u2 := Fixed8FromInt64(8)
|
|
|
|
|
|
|
|
assert.True(t, u1.LessThan(u2))
|
|
|
|
assert.True(t, u2.GreaterThan(u1))
|
|
|
|
assert.True(t, u1.Equal(u1))
|
|
|
|
assert.NotZero(t, u1.CompareTo(u2))
|
|
|
|
assert.Zero(t, u1.CompareTo(u1))
|
|
|
|
assert.EqualValues(t, Fixed8(2), u2.Div(3))
|
2018-05-09 05:20:16 +00:00
|
|
|
}
|
2019-12-12 15:52:23 +00:00
|
|
|
|
|
|
|
func TestFixed8_Serializable(t *testing.T) {
|
|
|
|
a := Fixed8(0x0102030405060708)
|
|
|
|
|
|
|
|
w := io.NewBufBinWriter()
|
|
|
|
a.EncodeBinary(w.BinWriter)
|
|
|
|
require.NoError(t, w.Err)
|
|
|
|
|
|
|
|
var b Fixed8
|
|
|
|
r := io.NewBinReaderFromBuf(w.Bytes())
|
|
|
|
b.DecodeBinary(r)
|
|
|
|
require.Equal(t, a, b)
|
|
|
|
}
|