Skip to content

Commit

Permalink
transfer ToString to ToBytes
Browse files Browse the repository at this point in the history
  • Loading branch information
aliiohs committed Jul 10, 2019
1 parent ae4ac3e commit 9f6eb47
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
4 changes: 2 additions & 2 deletions math/big/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func (d *Decimal) String() string {
tmp := *d
_ = tmp.Round(&tmp, int(tmp.resultFrac), ModeHalfEven)
//todo terror.Log(errors.Trace(err))
return string(tmp.ToString())
return string(tmp.ToBytes())
}

func (d *Decimal) stringSize() int {
Expand Down Expand Up @@ -314,7 +314,7 @@ func (d *Decimal) removeTrailingZeros() (lastWordIdx int, digitsFrac int) {
// str - result string
// errCode - eDecOK/eDecTruncate/eDecOverflow
//
func (d *Decimal) ToString() (str []byte) {
func (d *Decimal) ToBytes() (str []byte) {
str = make([]byte, d.stringSize())
digitsFrac := int(d.digitsFrac)
wordStartIdx, digitsInt := d.removeLeadingZeros()
Expand Down
38 changes: 19 additions & 19 deletions math/big/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestFromInt(t *testing.T) {
}
for _, tt := range tests {
dec := NewDecFromInt(tt.input)
str := dec.ToString()
str := dec.ToBytes()
assert.Equal(t, string(str), tt.output)
}
}
Expand All @@ -56,7 +56,7 @@ func TestFromUint(t *testing.T) {
for _, tt := range tests {
var dec Decimal
dec.FromUint(tt.input)
str := dec.ToString()
str := dec.ToBytes()
assert.Equal(t, string(str), tt.output)
}
}
Expand Down Expand Up @@ -122,7 +122,7 @@ func TestFromFloat(t *testing.T) {
}
for _, tt := range tests {
dec := NewDecFromFloatForTest(tt.f)
str := dec.ToString()
str := dec.ToBytes()
assert.Equal(t, string(str), tt.s)
}
}
Expand Down Expand Up @@ -235,7 +235,7 @@ func TestRemoveTrailingZeros(t *testing.T) {

// calculate the number of digits after point but trailing zero
digitsFracExp := 0
str := string(dec.ToString())
str := string(dec.ToBytes())
point := strings.Index(str, ".")
if point != -1 {
pos := len(str) - 1
Expand Down Expand Up @@ -268,7 +268,7 @@ func TestShift(t *testing.T) {
//origin := dec
err = dec.Shift(ca.shift)
assert.Equal(t, err, ca.err)
result := dec.ToString()
result := dec.ToBytes()
//, Commentf("origin:%s\ndec:%s", origin.String(), string(result))
assert.Equal(t, string(result), ca.output)
}
Expand Down Expand Up @@ -398,7 +398,7 @@ func TestRoundWithHalfEven(t *testing.T) {
var rounded Decimal
err := dec.Round(&rounded, ca.scale, ModeHalfEven)
assert.Equal(t, err, ca.err)
result := rounded.ToString()
result := rounded.ToBytes()
assert.Equal(t, string(result), ca.output)
}
}
Expand Down Expand Up @@ -432,7 +432,7 @@ func TestRoundWithTruncate(t *testing.T) {
var rounded Decimal
err := dec.Round(&rounded, ca.scale, ModeTruncate)
assert.Equal(t, err, ca.err)
result := rounded.ToString()
result := rounded.ToBytes()
assert.Equal(t, string(result), ca.output)
}
}
Expand Down Expand Up @@ -467,7 +467,7 @@ func TestRoundWithCeil(t *testing.T) {
var rounded Decimal
err := dec.Round(&rounded, ca.scale, modeCeiling)
assert.Equal(t, err, ca.err)
result := rounded.ToString()
result := rounded.ToBytes()
assert.Equal(t, string(result), ca.output)
}
}
Expand All @@ -494,7 +494,7 @@ func TestFromString(t *testing.T) {
if err != nil {
assert.Equal(t, err, ca.err)
}
result := string(dec.ToString())
result := string(dec.ToBytes())
assert.Equal(t, result, ca.output)
}
wordBufLen = 1
Expand All @@ -506,13 +506,13 @@ func TestFromString(t *testing.T) {
var dec Decimal
err := dec.FromBytes([]byte(ca.input))
assert.Equal(t, err, ca.err)
result := string(dec.ToString())
result := string(dec.ToBytes())
assert.Equal(t, result, ca.output)
}
wordBufLen = maxWordBufLen
}

func TestToString(t *testing.T) {
func TestToBytes(t *testing.T) {
type tcase struct {
input string
output string
Expand All @@ -525,7 +525,7 @@ func TestToString(t *testing.T) {
for _, ca := range tests {
var dec Decimal
_ = dec.FromBytes([]byte(ca.input))
result := dec.ToString()
result := dec.ToBytes()
assert.Equal(t, string(result), ca.output)
}
}
Expand Down Expand Up @@ -564,7 +564,7 @@ func TestToBinFromBin(t *testing.T) {
var dec2 Decimal
_, err = dec2.FromBin(buf, ca.precision, ca.frac)
assert.Equal(t, err, nil)
str := dec2.ToString()
str := dec2.ToBytes()
assert.Equal(t, string(str), ca.output)
}
var dec Decimal
Expand Down Expand Up @@ -637,7 +637,7 @@ func TestMaxDecimal(t *testing.T) {
for _, tt := range tests {
var dec Decimal
maxDecimal(tt.prec, tt.frac, &dec)
str := dec.ToString()
str := dec.ToBytes()
assert.Equal(t, string(str), tt.result)
}
}
Expand All @@ -656,7 +656,7 @@ func TestNeg(t *testing.T) {
for _, tt := range tests {
a := NewDecFromStringForTest(tt.a)
negResult := DecimalNeg(a)
result := negResult.ToString()
result := negResult.ToBytes()
assert.Equal(t, string(result), tt.result)
}
}
Expand Down Expand Up @@ -691,7 +691,7 @@ func TestAdd(t *testing.T) {
var sum Decimal
err := DecimalAdd(a, b, &sum)
assert.Equal(t, err, tt.err)
result := sum.ToString()
result := sum.ToBytes()
assert.Equal(t, string(result), tt.result)
}
}
Expand Down Expand Up @@ -725,7 +725,7 @@ func TestSub(t *testing.T) {
b.FromBytes([]byte(tt.b))
err := DecimalSub(&a, &b, &sum)
assert.Equal(t, err, tt.err)
result := sum.ToString()
result := sum.ToBytes()
assert.Equal(t, string(result), tt.result)
}
}
Expand Down Expand Up @@ -794,7 +794,7 @@ func TestDivMod(t *testing.T) {
if tt.err == ErrDivByZero {
continue
}
result := to.ToString()
result := to.ToBytes()
assert.Equal(t, string(result), tt.result)
}

Expand All @@ -816,7 +816,7 @@ func TestDivMod(t *testing.T) {
if tt.err == ErrDivByZero {
continue
}
result := to.ToString()
result := to.ToBytes()
assert.Equal(t, string(result), tt.result)
}

Expand Down

0 comments on commit 9f6eb47

Please sign in to comment.