2021-03-17 14:20:25 +00:00
|
|
|
package fixedpoint
|
|
|
|
|
2021-06-08 16:46:43 +00:00
|
|
|
import (
|
2022-06-17 04:01:15 +00:00
|
|
|
"encoding/json"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2022-07-26 07:42:34 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
2022-02-10 12:39:20 +00:00
|
|
|
"math/big"
|
|
|
|
"testing"
|
2021-06-08 16:46:43 +00:00
|
|
|
)
|
|
|
|
|
2022-02-07 03:40:30 +00:00
|
|
|
const Delta = 1e-9
|
|
|
|
|
2021-06-08 16:46:43 +00:00
|
|
|
func BenchmarkMul(b *testing.B) {
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
b.Run("mul-float64", func(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
x := NewFromFloat(20.0)
|
|
|
|
y := NewFromFloat(20.0)
|
2022-06-17 11:19:51 +00:00
|
|
|
x = x.Mul(y) // nolint
|
2021-06-08 16:46:43 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-06-08 16:55:43 +00:00
|
|
|
b.Run("mul-float64-large-numbers", func(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
x := NewFromFloat(88.12345678)
|
|
|
|
y := NewFromFloat(88.12345678)
|
2022-06-17 11:19:51 +00:00
|
|
|
x = x.Mul(y) // nolint
|
2021-06-08 16:55:43 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
b.Run("mul-big-small-numbers", func(b *testing.B) {
|
2021-06-08 16:46:43 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2022-02-02 11:37:18 +00:00
|
|
|
x := big.NewFloat(20.0)
|
|
|
|
y := big.NewFloat(20.0)
|
2022-06-17 11:19:51 +00:00
|
|
|
x = new(big.Float).Mul(x, y) // nolint
|
2021-06-08 16:46:43 +00:00
|
|
|
}
|
|
|
|
})
|
2021-06-08 16:55:43 +00:00
|
|
|
|
|
|
|
b.Run("mul-big-large-numbers", func(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
2022-02-02 11:37:18 +00:00
|
|
|
x := big.NewFloat(88.12345678)
|
|
|
|
y := big.NewFloat(88.12345678)
|
2022-06-17 11:19:51 +00:00
|
|
|
x = new(big.Float).Mul(x, y) // nolint
|
2021-06-08 16:55:43 +00:00
|
|
|
}
|
|
|
|
})
|
2021-06-08 16:46:43 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 11:37:18 +00:00
|
|
|
func TestMulString(t *testing.T) {
|
2021-06-08 16:46:43 +00:00
|
|
|
x := NewFromFloat(10.55)
|
2022-02-02 11:37:18 +00:00
|
|
|
assert.Equal(t, "10.55", x.String())
|
2021-06-08 16:46:43 +00:00
|
|
|
y := NewFromFloat(10.55)
|
2022-02-02 11:37:18 +00:00
|
|
|
x = x.Mul(y)
|
|
|
|
assert.Equal(t, "111.3025", x.String())
|
2022-02-03 04:55:25 +00:00
|
|
|
assert.Equal(t, "111.30", x.FormatString(2))
|
2022-02-07 03:40:30 +00:00
|
|
|
assert.InDelta(t, 111.3025, x.Float64(), Delta)
|
2021-06-08 16:46:43 +00:00
|
|
|
}
|
2021-03-17 14:20:25 +00:00
|
|
|
|
2022-02-04 03:56:49 +00:00
|
|
|
func TestMulExp(t *testing.T) {
|
|
|
|
x, _ := NewFromString("166")
|
|
|
|
digits := x.NumIntDigits()
|
|
|
|
assert.Equal(t, digits, 3)
|
2022-02-10 12:39:20 +00:00
|
|
|
step := x.MulExp(-digits + 1)
|
2022-02-04 03:56:49 +00:00
|
|
|
assert.Equal(t, "1.66", step.String())
|
2022-02-07 03:40:30 +00:00
|
|
|
}
|
2022-02-04 03:56:49 +00:00
|
|
|
|
2022-02-07 03:40:30 +00:00
|
|
|
func TestNew(t *testing.T) {
|
|
|
|
f := NewFromFloat(0.001)
|
|
|
|
assert.Equal(t, "0.001", f.String())
|
|
|
|
assert.Equal(t, "0.0010", f.FormatString(4))
|
|
|
|
assert.Equal(t, "0.1%", f.Percentage())
|
|
|
|
assert.Equal(t, "0.10%", f.FormatPercentage(2))
|
2022-06-13 02:05:55 +00:00
|
|
|
f = NewFromFloat(0.1)
|
|
|
|
assert.Equal(t, "10%", f.Percentage())
|
|
|
|
assert.Equal(t, "10%", f.FormatPercentage(0))
|
|
|
|
f = NewFromFloat(0.01)
|
|
|
|
assert.Equal(t, "1%", f.Percentage())
|
|
|
|
assert.Equal(t, "1%", f.FormatPercentage(0))
|
|
|
|
f = NewFromFloat(0.111)
|
|
|
|
assert.Equal(t, "11.1%", f.Percentage())
|
|
|
|
assert.Equal(t, "11.1%", f.FormatPercentage(1))
|
2022-02-04 03:56:49 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 10:03:28 +00:00
|
|
|
func TestFormatString(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
value Value
|
2022-02-25 10:25:44 +00:00
|
|
|
prec int
|
|
|
|
out string
|
2022-02-25 10:03:28 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
value: NewFromFloat(0.001),
|
2022-02-25 10:25:44 +00:00
|
|
|
prec: 8,
|
|
|
|
out: "0.00100000",
|
2022-02-25 10:03:28 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
value: NewFromFloat(0.123456789),
|
2022-02-25 10:25:44 +00:00
|
|
|
prec: 4,
|
|
|
|
out: "0.1234",
|
2022-02-25 10:03:28 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
value: NewFromFloat(0.123456789),
|
2022-02-25 10:25:44 +00:00
|
|
|
prec: 5,
|
|
|
|
out: "0.12345",
|
2022-02-25 10:03:28 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
value: NewFromFloat(20.0),
|
2022-02-25 10:25:44 +00:00
|
|
|
prec: 0,
|
|
|
|
out: "20",
|
2022-02-25 10:03:28 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
assert.Equal(t, testCase.out, testCase.value.FormatString(testCase.prec))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-08 04:41:24 +00:00
|
|
|
func TestRound(t *testing.T) {
|
|
|
|
f := NewFromFloat(1.2345)
|
|
|
|
f = f.Round(0, Down)
|
|
|
|
assert.Equal(t, "1", f.String())
|
|
|
|
w := NewFromFloat(1.2345)
|
|
|
|
w = w.Trunc()
|
|
|
|
assert.Equal(t, "1", w.String())
|
|
|
|
s := NewFromFloat(1.2345)
|
|
|
|
assert.Equal(t, "1.23", s.Round(2, Down).String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFromString(t *testing.T) {
|
|
|
|
f := MustNewFromString("0.004075")
|
|
|
|
assert.Equal(t, "0.004075", f.String())
|
2022-02-09 10:23:35 +00:00
|
|
|
f = MustNewFromString("0.03")
|
|
|
|
assert.Equal(t, "0.03", f.String())
|
2022-02-08 04:41:24 +00:00
|
|
|
|
2022-02-15 02:56:09 +00:00
|
|
|
f = MustNewFromString("0.75%")
|
|
|
|
assert.Equal(t, "0.0075", f.String())
|
2022-02-17 03:45:06 +00:00
|
|
|
f = MustNewFromString("1.1e-7")
|
|
|
|
assert.Equal(t, "0.00000011", f.String())
|
|
|
|
f = MustNewFromString(".0%")
|
|
|
|
assert.Equal(t, Zero, f)
|
2022-03-07 03:46:03 +00:00
|
|
|
f = MustNewFromString("")
|
|
|
|
assert.Equal(t, Zero, f)
|
2022-09-02 05:37:11 +00:00
|
|
|
|
|
|
|
for _, s := range []string{"inf", "Inf", "INF", "iNF"} {
|
|
|
|
f = MustNewFromString(s)
|
|
|
|
assert.Equal(t, PosInf, f)
|
|
|
|
f = MustNewFromString("+" + s)
|
|
|
|
assert.Equal(t, PosInf, f)
|
|
|
|
f = MustNewFromString("-" + s)
|
|
|
|
assert.Equal(t, NegInf, f)
|
|
|
|
}
|
2022-02-10 12:39:20 +00:00
|
|
|
}
|
|
|
|
|
2022-02-26 20:48:50 +00:00
|
|
|
func TestJson(t *testing.T) {
|
|
|
|
p := MustNewFromString("0")
|
|
|
|
e, err := json.Marshal(p)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "0.00000000", string(e))
|
|
|
|
p = MustNewFromString("1.00000003")
|
|
|
|
e, err = json.Marshal(p)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "1.00000003", string(e))
|
|
|
|
p = MustNewFromString("1.000000003")
|
|
|
|
e, err = json.Marshal(p)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "1.00000000", string(e))
|
|
|
|
p = MustNewFromString("1.000000008")
|
|
|
|
e, err = json.Marshal(p)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "1.00000000", string(e))
|
|
|
|
p = MustNewFromString("0.999999999")
|
|
|
|
e, err = json.Marshal(p)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "0.99999999", string(e))
|
|
|
|
|
|
|
|
p = MustNewFromString("1.2e-9")
|
|
|
|
e, err = json.Marshal(p)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "0.00000000", p.FormatString(8))
|
|
|
|
assert.Equal(t, "0.00000000", string(e))
|
|
|
|
|
|
|
|
_ = json.Unmarshal([]byte("0.00153917575"), &p)
|
|
|
|
assert.Equal(t, "0.00153917", p.FormatString(8))
|
|
|
|
|
2022-06-17 11:19:51 +00:00
|
|
|
q := NewFromFloat(0.00153917575)
|
2022-02-26 20:48:50 +00:00
|
|
|
assert.Equal(t, p, q)
|
|
|
|
_ = json.Unmarshal([]byte("6e-8"), &p)
|
|
|
|
_ = json.Unmarshal([]byte("0.000062"), &q)
|
|
|
|
assert.Equal(t, "0.00006194", q.Sub(p).String())
|
2022-07-26 07:42:34 +00:00
|
|
|
|
2022-09-02 05:37:11 +00:00
|
|
|
assert.NoError(t, json.Unmarshal([]byte(`"inf"`), &p))
|
|
|
|
assert.NoError(t, json.Unmarshal([]byte(`"+Inf"`), &q))
|
|
|
|
assert.Equal(t, PosInf, p)
|
|
|
|
assert.Equal(t, p, q)
|
2022-07-26 07:42:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestYaml(t *testing.T) {
|
|
|
|
p := MustNewFromString("0")
|
|
|
|
e, err := yaml.Marshal(p)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "\"0.00000000\"\n", string(e))
|
|
|
|
p = MustNewFromString("1.00000003")
|
|
|
|
e, err = yaml.Marshal(p)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "\"1.00000003\"\n", string(e))
|
|
|
|
p = MustNewFromString("1.000000003")
|
|
|
|
e, err = yaml.Marshal(p)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "\"1.00000000\"\n", string(e))
|
|
|
|
p = MustNewFromString("1.000000008")
|
|
|
|
e, err = yaml.Marshal(p)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "\"1.00000000\"\n", string(e))
|
|
|
|
p = MustNewFromString("0.999999999")
|
|
|
|
e, err = yaml.Marshal(p)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "\"0.99999999\"\n", string(e))
|
|
|
|
|
|
|
|
p = MustNewFromString("1.2e-9")
|
|
|
|
e, err = yaml.Marshal(p)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "0.00000000", p.FormatString(8))
|
|
|
|
assert.Equal(t, "\"0.00000000\"\n", string(e))
|
|
|
|
|
|
|
|
_ = yaml.Unmarshal([]byte("0.00153917575"), &p)
|
|
|
|
assert.Equal(t, "0.00153917", p.FormatString(8))
|
|
|
|
|
|
|
|
q := NewFromFloat(0.00153917575)
|
|
|
|
assert.Equal(t, p, q)
|
|
|
|
_ = yaml.Unmarshal([]byte("6e-8"), &p)
|
|
|
|
_ = yaml.Unmarshal([]byte("0.000062"), &q)
|
|
|
|
assert.Equal(t, "0.00006194", q.Sub(p).String())
|
|
|
|
|
2022-09-02 05:37:11 +00:00
|
|
|
assert.NoError(t, json.Unmarshal([]byte(`"inf"`), &p))
|
|
|
|
assert.NoError(t, json.Unmarshal([]byte(`"+Inf"`), &q))
|
|
|
|
assert.Equal(t, PosInf, p)
|
|
|
|
assert.Equal(t, p, q)
|
2022-02-26 20:48:50 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 03:28:47 +00:00
|
|
|
func TestNumFractionalDigits(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
v Value
|
|
|
|
want int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "ignore the integer part",
|
|
|
|
v: MustNewFromString("123.4567"),
|
|
|
|
want: 4,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ignore the sign",
|
|
|
|
v: MustNewFromString("-123.4567"),
|
|
|
|
want: 4,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ignore the trailing zero",
|
|
|
|
v: MustNewFromString("-123.45000000"),
|
|
|
|
want: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no fractional parts",
|
|
|
|
v: MustNewFromString("-1"),
|
|
|
|
want: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no fractional parts",
|
|
|
|
v: MustNewFromString("-1.0"),
|
|
|
|
want: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "only fractional part",
|
|
|
|
v: MustNewFromString(".123456"),
|
|
|
|
want: 6,
|
|
|
|
},
|
2021-05-16 07:16:04 +00:00
|
|
|
{
|
|
|
|
name: "percentage",
|
2021-05-24 17:35:38 +00:00
|
|
|
v: MustNewFromString("0.075%"), // 0.075 * 0.01
|
|
|
|
want: 5,
|
2021-05-16 07:16:04 +00:00
|
|
|
},
|
2022-02-17 03:45:06 +00:00
|
|
|
{
|
|
|
|
name: "scientific notation",
|
|
|
|
v: MustNewFromString("1.1e-7"),
|
|
|
|
want: 8,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "zero",
|
|
|
|
v: MustNewFromString("0"),
|
|
|
|
want: 0,
|
|
|
|
},
|
2021-03-21 03:28:47 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-02-04 03:56:49 +00:00
|
|
|
if got := tt.v.NumFractionalDigits(); got != tt.want {
|
2021-03-21 03:28:47 +00:00
|
|
|
t.Errorf("NumFractionalDigits() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|