Unverified Commit ae4ac3e9 authored by 望哥's avatar 望哥 Committed by GitHub

Merge pull request #7 from aliiohs/feature/DecimalChange

change some decimal method
parents b8f25b42 129db7b4
...@@ -391,8 +391,13 @@ func (d *Decimal) ToString() (str []byte) { ...@@ -391,8 +391,13 @@ func (d *Decimal) ToString() (str []byte) {
return return
} }
// FromString parses decimal from string. // FromBytes parses decimal from string.
func (d *Decimal) FromString(str []byte) error { func (d *Decimal) FromString(str string) error {
return d.FromBytes([]byte(str))
}
// FromBytes parses decimal from bytes.
func (d *Decimal) FromBytes(str []byte) error {
for i := 0; i < len(str); i++ { for i := 0; i < len(str); i++ {
if !isSpace(str[i]) { if !isSpace(str[i]) {
str = str[i:] str = str[i:]
...@@ -1077,7 +1082,7 @@ func (d *Decimal) ToUint() (uint64, error) { ...@@ -1077,7 +1082,7 @@ func (d *Decimal) ToUint() (uint64, error) {
// FromFloat64 creates a decimal from float64 value. // FromFloat64 creates a decimal from float64 value.
func (d *Decimal) FromFloat64(f float64) error { func (d *Decimal) FromFloat64(f float64) error {
s := strconv.FormatFloat(f, 'g', -1, 64) s := strconv.FormatFloat(f, 'g', -1, 64)
return d.FromString([]byte(s)) return d.FromBytes([]byte(s))
} }
// ToFloat64 converts decimal to float64 value. // ToFloat64 converts decimal to float64 value.
...@@ -2277,7 +2282,7 @@ func NewDecFromFloatForTest(f float64) *Decimal { ...@@ -2277,7 +2282,7 @@ func NewDecFromFloatForTest(f float64) *Decimal {
// NewDecFromStringForTest creates a Decimal from string, as it returns no error, it should only be used in test. // NewDecFromStringForTest creates a Decimal from string, as it returns no error, it should only be used in test.
func NewDecFromStringForTest(s string) *Decimal { func NewDecFromStringForTest(s string) *Decimal {
dec := new(Decimal) dec := new(Decimal)
_ = dec.FromString([]byte(s)) _ = dec.FromBytes([]byte(s))
//todo terror.Log(errors.Trace(err)) //todo terror.Log(errors.Trace(err))
return dec return dec
} }
...@@ -2295,7 +2300,7 @@ func NewMaxOrMinDec(negative bool, prec, frac int) *Decimal { ...@@ -2295,7 +2300,7 @@ func NewMaxOrMinDec(negative bool, prec, frac int) *Decimal {
} }
str[1+prec-frac] = '.' str[1+prec-frac] = '.'
dec := new(Decimal) dec := new(Decimal)
_ = dec.FromString(str) _ = dec.FromBytes(str)
//todo terror.Log(errors.Trace(err)) //todo terror.Log(errors.Trace(err))
return dec return dec
} }
...@@ -45,7 +45,7 @@ func BenchmarkRound(b *testing.B) { ...@@ -45,7 +45,7 @@ func BenchmarkRound(b *testing.B) {
} }
for i := 0; i < len(tests); i++ { for i := 0; i < len(tests); i++ {
tests[i].inputDec.FromString([]byte(tests[i].input)) tests[i].inputDec.FromBytes([]byte(tests[i].input))
} }
b.StartTimer() b.StartTimer()
......
...@@ -78,7 +78,7 @@ func TestToInt(t *testing.T) { ...@@ -78,7 +78,7 @@ func TestToInt(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
var dec Decimal var dec Decimal
dec.FromString([]byte(tt.input)) dec.FromBytes([]byte(tt.input))
result, ec := dec.ToInt() result, ec := dec.ToInt()
assert.Equal(t, ec, tt.err) assert.Equal(t, ec, tt.err)
assert.Equal(t, result, tt.output) assert.Equal(t, result, tt.output)
...@@ -102,7 +102,7 @@ func TestToUint(t *testing.T) { ...@@ -102,7 +102,7 @@ func TestToUint(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
var dec Decimal var dec Decimal
_ = dec.FromString([]byte(tt.input)) _ = dec.FromBytes([]byte(tt.input))
result, ec := dec.ToUint() result, ec := dec.ToUint()
assert.Equal(t, ec, tt.err) assert.Equal(t, ec, tt.err)
assert.Equal(t, result, tt.output) assert.Equal(t, result, tt.output)
...@@ -140,7 +140,7 @@ func TestToFloat(t *testing.T) { ...@@ -140,7 +140,7 @@ func TestToFloat(t *testing.T) {
} }
for _, ca := range tests { for _, ca := range tests {
var dec Decimal var dec Decimal
_ = dec.FromString([]byte(ca.s)) _ = dec.FromBytes([]byte(ca.s))
f, _ := dec.ToFloat64() f, _ := dec.ToFloat64()
assert.Equal(t, f, ca.f) assert.Equal(t, f, ca.f)
} }
...@@ -164,7 +164,7 @@ func TestToHashKey(t *testing.T) { ...@@ -164,7 +164,7 @@ func TestToHashKey(t *testing.T) {
keys := make([]string, 0, len(ca.numbers)) keys := make([]string, 0, len(ca.numbers))
for _, num := range ca.numbers { for _, num := range ca.numbers {
var dec Decimal var dec Decimal
assert.Equal(t, dec.FromString([]byte(num)), nil) assert.Equal(t, dec.FromBytes([]byte(num)), nil)
key, err := dec.ToHashKey() key, err := dec.ToHashKey()
assert.Equal(t, err, nil) assert.Equal(t, err, nil)
keys = append(keys, string(key)) keys = append(keys, string(key))
...@@ -200,14 +200,14 @@ func TestToHashKey(t *testing.T) { ...@@ -200,14 +200,14 @@ func TestToHashKey(t *testing.T) {
keys := make([]string, 0, len(ca.hashNumbers)+len(ca.binNumbers)) keys := make([]string, 0, len(ca.hashNumbers)+len(ca.binNumbers))
for _, num := range ca.hashNumbers { for _, num := range ca.hashNumbers {
var dec Decimal var dec Decimal
assert.Equal(t, dec.FromString([]byte(num)), nil) assert.Equal(t, dec.FromBytes([]byte(num)), nil)
key, err := dec.ToHashKey() key, err := dec.ToHashKey()
assert.Equal(t, err, nil) assert.Equal(t, err, nil)
keys = append(keys, string(key)) keys = append(keys, string(key))
} }
for _, num := range ca.binNumbers { for _, num := range ca.binNumbers {
var dec Decimal var dec Decimal
assert.Equal(t, dec.FromString([]byte(num)), nil) assert.Equal(t, dec.FromBytes([]byte(num)), nil)
prec, frac := dec.PrecisionAndFrac() // remove leading zeros but trailing zeros remain prec, frac := dec.PrecisionAndFrac() // remove leading zeros but trailing zeros remain
key, err := dec.ToBin(prec, frac) key, err := dec.ToBin(prec, frac)
assert.Equal(t, err, nil) assert.Equal(t, err, nil)
...@@ -231,7 +231,7 @@ func TestRemoveTrailingZeros(t *testing.T) { ...@@ -231,7 +231,7 @@ func TestRemoveTrailingZeros(t *testing.T) {
} }
for _, ca := range tests { for _, ca := range tests {
var dec Decimal var dec Decimal
assert.Equal(t, dec.FromString([]byte(ca)), nil) assert.Equal(t, dec.FromBytes([]byte(ca)), nil)
// calculate the number of digits after point but trailing zero // calculate the number of digits after point but trailing zero
digitsFracExp := 0 digitsFracExp := 0
...@@ -263,7 +263,7 @@ func TestShift(t *testing.T) { ...@@ -263,7 +263,7 @@ func TestShift(t *testing.T) {
var dotest = func(t *testing.T, tests []tcase) { var dotest = func(t *testing.T, tests []tcase) {
for _, ca := range tests { for _, ca := range tests {
var dec Decimal var dec Decimal
err := dec.FromString([]byte(ca.input)) err := dec.FromBytes([]byte(ca.input))
//assert.Equal(t, err, IsNil) //assert.Equal(t, err, IsNil)
//origin := dec //origin := dec
err = dec.Shift(ca.shift) err = dec.Shift(ca.shift)
...@@ -394,7 +394,7 @@ func TestRoundWithHalfEven(t *testing.T) { ...@@ -394,7 +394,7 @@ func TestRoundWithHalfEven(t *testing.T) {
for _, ca := range tests { for _, ca := range tests {
var dec Decimal var dec Decimal
dec.FromString([]byte(ca.input)) dec.FromBytes([]byte(ca.input))
var rounded Decimal var rounded Decimal
err := dec.Round(&rounded, ca.scale, ModeHalfEven) err := dec.Round(&rounded, ca.scale, ModeHalfEven)
assert.Equal(t, err, ca.err) assert.Equal(t, err, ca.err)
...@@ -428,7 +428,7 @@ func TestRoundWithTruncate(t *testing.T) { ...@@ -428,7 +428,7 @@ func TestRoundWithTruncate(t *testing.T) {
} }
for _, ca := range tests { for _, ca := range tests {
var dec Decimal var dec Decimal
dec.FromString([]byte(ca.input)) dec.FromBytes([]byte(ca.input))
var rounded Decimal var rounded Decimal
err := dec.Round(&rounded, ca.scale, ModeTruncate) err := dec.Round(&rounded, ca.scale, ModeTruncate)
assert.Equal(t, err, ca.err) assert.Equal(t, err, ca.err)
...@@ -463,7 +463,7 @@ func TestRoundWithCeil(t *testing.T) { ...@@ -463,7 +463,7 @@ func TestRoundWithCeil(t *testing.T) {
} }
for _, ca := range tests { for _, ca := range tests {
var dec Decimal var dec Decimal
dec.FromString([]byte(ca.input)) dec.FromBytes([]byte(ca.input))
var rounded Decimal var rounded Decimal
err := dec.Round(&rounded, ca.scale, modeCeiling) err := dec.Round(&rounded, ca.scale, modeCeiling)
assert.Equal(t, err, ca.err) assert.Equal(t, err, ca.err)
...@@ -490,7 +490,7 @@ func TestFromString(t *testing.T) { ...@@ -490,7 +490,7 @@ func TestFromString(t *testing.T) {
} }
for _, ca := range tests { for _, ca := range tests {
var dec Decimal var dec Decimal
err := dec.FromString([]byte(ca.input)) err := dec.FromBytes([]byte(ca.input))
if err != nil { if err != nil {
assert.Equal(t, err, ca.err) assert.Equal(t, err, ca.err)
} }
...@@ -504,7 +504,7 @@ func TestFromString(t *testing.T) { ...@@ -504,7 +504,7 @@ func TestFromString(t *testing.T) {
} }
for _, ca := range tests { for _, ca := range tests {
var dec Decimal var dec Decimal
err := dec.FromString([]byte(ca.input)) err := dec.FromBytes([]byte(ca.input))
assert.Equal(t, err, ca.err) assert.Equal(t, err, ca.err)
result := string(dec.ToString()) result := string(dec.ToString())
assert.Equal(t, result, ca.output) assert.Equal(t, result, ca.output)
...@@ -524,7 +524,7 @@ func TestToString(t *testing.T) { ...@@ -524,7 +524,7 @@ func TestToString(t *testing.T) {
} }
for _, ca := range tests { for _, ca := range tests {
var dec Decimal var dec Decimal
_ = dec.FromString([]byte(ca.input)) _ = dec.FromBytes([]byte(ca.input))
result := dec.ToString() result := dec.ToString()
assert.Equal(t, string(result), ca.output) assert.Equal(t, string(result), ca.output)
} }
...@@ -557,7 +557,7 @@ func TestToBinFromBin(t *testing.T) { ...@@ -557,7 +557,7 @@ func TestToBinFromBin(t *testing.T) {
} }
for _, ca := range tests { for _, ca := range tests {
var dec Decimal var dec Decimal
err := dec.FromString([]byte(ca.input)) err := dec.FromBytes([]byte(ca.input))
assert.Equal(t, err, nil) assert.Equal(t, err, nil)
buf, err := dec.ToBin(ca.precision, ca.frac) buf, err := dec.ToBin(ca.precision, ca.frac)
assert.Equal(t, err, ca.err) assert.Equal(t, err, ca.err)
...@@ -605,8 +605,8 @@ func TestCompare(t *testing.T) { ...@@ -605,8 +605,8 @@ func TestCompare(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
var a, b Decimal var a, b Decimal
_ = a.FromString([]byte(tt.a)) _ = a.FromBytes([]byte(tt.a))
_ = b.FromString([]byte(tt.b)) _ = b.FromBytes([]byte(tt.b))
assert.Equal(t, a.Compare(&b), tt.cmp) assert.Equal(t, a.Compare(&b), tt.cmp)
} }
} }
...@@ -721,8 +721,8 @@ func TestSub(t *testing.T) { ...@@ -721,8 +721,8 @@ func TestSub(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
var a, b, sum Decimal var a, b, sum Decimal
a.FromString([]byte(tt.a)) a.FromBytes([]byte(tt.a))
b.FromString([]byte(tt.b)) b.FromBytes([]byte(tt.b))
err := DecimalSub(&a, &b, &sum) err := DecimalSub(&a, &b, &sum)
assert.Equal(t, err, tt.err) assert.Equal(t, err, tt.err)
result := sum.ToString() result := sum.ToString()
...@@ -752,8 +752,8 @@ func TestMul(t *testing.T) { ...@@ -752,8 +752,8 @@ func TestMul(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
var a, b, product Decimal var a, b, product Decimal
a.FromString([]byte(tt.a)) a.FromBytes([]byte(tt.a))
b.FromString([]byte(tt.b)) b.FromBytes([]byte(tt.b))
err := DecimalMul(&a, &b, &product) err := DecimalMul(&a, &b, &product)
assert.Equal(t, err, tt.err) assert.Equal(t, err, tt.err)
result := product.String() result := product.String()
...@@ -787,8 +787,8 @@ func TestDivMod(t *testing.T) { ...@@ -787,8 +787,8 @@ func TestDivMod(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
var a, b, to Decimal var a, b, to Decimal
a.FromString([]byte(tt.a)) a.FromBytes([]byte(tt.a))
b.FromString([]byte(tt.b)) b.FromBytes([]byte(tt.b))
err := doDivMod(&a, &b, &to, nil, 5) err := doDivMod(&a, &b, &to, nil, 5)
assert.Equal(t, err, tt.err) assert.Equal(t, err, tt.err)
if tt.err == ErrDivByZero { if tt.err == ErrDivByZero {
...@@ -809,8 +809,8 @@ func TestDivMod(t *testing.T) { ...@@ -809,8 +809,8 @@ func TestDivMod(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
var a, b, to Decimal var a, b, to Decimal
a.FromString([]byte(tt.a)) a.FromBytes([]byte(tt.a))
b.FromString([]byte(tt.b)) b.FromBytes([]byte(tt.b))
ec := doDivMod(&a, &b, nil, &to, 0) ec := doDivMod(&a, &b, nil, &to, 0)
assert.Equal(t, ec, tt.err) assert.Equal(t, ec, tt.err)
if tt.err == ErrDivByZero { if tt.err == ErrDivByZero {
...@@ -829,8 +829,8 @@ func TestDivMod(t *testing.T) { ...@@ -829,8 +829,8 @@ func TestDivMod(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
var a, b, to Decimal var a, b, to Decimal
a.FromString([]byte(tt.a)) a.FromBytes([]byte(tt.a))
b.FromString([]byte(tt.b)) b.FromBytes([]byte(tt.b))
ec := DecimalDiv(&a, &b, &to, DivFracIncr) ec := DecimalDiv(&a, &b, &to, DivFracIncr)
assert.Equal(t, ec, tt.err) assert.Equal(t, ec, tt.err)
if tt.err == ErrDivByZero { if tt.err == ErrDivByZero {
...@@ -847,8 +847,8 @@ func TestDivMod(t *testing.T) { ...@@ -847,8 +847,8 @@ func TestDivMod(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
var a, b, to Decimal var a, b, to Decimal
_ = a.FromString([]byte(tt.a)) _ = a.FromBytes([]byte(tt.a))
_ = b.FromString([]byte(tt.b)) _ = b.FromBytes([]byte(tt.b))
ec := DecimalMod(&a, &b, &to) ec := DecimalMod(&a, &b, &to)
assert.Equal(t, ec, tt.err) assert.Equal(t, ec, tt.err)
if tt.err == ErrDivByZero { if tt.err == ErrDivByZero {
...@@ -892,7 +892,7 @@ func BenchmarkBigDecimalToBin(b *testing.B) { ...@@ -892,7 +892,7 @@ func BenchmarkBigDecimalToBin(b *testing.B) {
decs := make([]*Decimal, 0, len(cases)) decs := make([]*Decimal, 0, len(cases))
for _, ca := range cases { for _, ca := range cases {
var dec Decimal var dec Decimal
if err := dec.FromString([]byte(ca)); err != nil { if err := dec.FromBytes([]byte(ca)); err != nil {
b.Fatal(err) b.Fatal(err)
} }
decs = append(decs, &dec) decs = append(decs, &dec)
...@@ -915,7 +915,7 @@ func BenchmarkBigDecimalToHashKey(b *testing.B) { ...@@ -915,7 +915,7 @@ func BenchmarkBigDecimalToHashKey(b *testing.B) {
decs := make([]*Decimal, 0, len(cases)) decs := make([]*Decimal, 0, len(cases))
for _, ca := range cases { for _, ca := range cases {
var dec Decimal var dec Decimal
if err := dec.FromString([]byte(ca)); err != nil { if err := dec.FromBytes([]byte(ca)); err != nil {
b.Fatal(err) b.Fatal(err)
} }
decs = append(decs, &dec) decs = append(decs, &dec)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment