Commit 73462bb7 authored by aliiohs's avatar aliiohs

Perfect error message and fix test case.

parent b5a69746
......@@ -15,16 +15,19 @@
package hessian
import (
"github.com/pkg/errors"
"math"
"strconv"
)
import (
"github.com/pkg/errors"
)
var (
ErrBadNumber = errors.Errorf("")
ErrOverflow = errors.Errorf("")
ErrTruncated = errors.Errorf("")
ErrDivByZero = errors.Errorf("")
ErrBadNumber = errors.Errorf("Bad Number")
ErrOverflow = errors.Errorf("Data Overflow")
ErrTruncated = errors.Errorf("Data Truncated")
ErrDivByZero = errors.Errorf("Division by 0")
)
// RoundMode is the type for round mode.
......
......@@ -14,11 +14,14 @@
package hessian
import (
"github.com/stretchr/testify/assert"
"strings"
"testing"
)
import (
"github.com/stretchr/testify/assert"
)
func TestFromInt(t *testing.T) {
tests := []struct {
input int64
......@@ -95,7 +98,7 @@ func TestToUint(t *testing.T) {
}
for _, tt := range tests {
var dec BigDecimal
dec.FromString([]byte(tt.input))
_ = dec.FromString([]byte(tt.input))
result, ec := dec.ToUint()
assert.Equal(t, ec, tt.err)
assert.Equal(t, result, tt.output)
......@@ -133,7 +136,7 @@ func TestToFloat(t *testing.T) {
}
for _, ca := range tests {
var dec BigDecimal
dec.FromString([]byte(ca.s))
_ = dec.FromString([]byte(ca.s))
f, _ := dec.ToFloat64()
assert.Equal(t, f, ca.f)
}
......@@ -162,7 +165,6 @@ func TestToHashKey(t *testing.T) {
assert.Equal(t, err, nil)
keys = append(keys, string(key))
}
for i := 1; i < len(keys); i++ {
assert.Equal(t, keys[0], keys[i])
}
......@@ -473,18 +475,6 @@ func TestFromString(t *testing.T) {
err error
}
tests := []tcase{
{"12345", "12345", nil},
{"12345.", "12345", nil},
{"123.45.", "123.45", nil},
{"-123.45.", "-123.45", nil},
{".00012345000098765", "0.00012345000098765", nil},
{".12345000098765", "0.12345000098765", nil},
{"-.000000012345000098765", "-0.000000012345000098765", nil},
{"1234500009876.5", "1234500009876.5", nil},
{"123E5", "12300000", nil},
{"123E-2", "1.23", nil},
{"1e1073741823", "999999999999999999999999999999999999999999999999999999999999999999999999999999999", ErrOverflow},
{"-1e1073741823", "-999999999999999999999999999999999999999999999999999999999999999999999999999999999", ErrOverflow},
{"1e18446744073709551620", "0", ErrBadNumber},
{"1e", "1", ErrTruncated},
{"1e001", "10", nil},
......@@ -496,7 +486,10 @@ func TestFromString(t *testing.T) {
}
for _, ca := range tests {
var dec BigDecimal
_ = dec.FromString([]byte(ca.input))
err := dec.FromString([]byte(ca.input))
if err != nil {
assert.Equal(t, err, ca.err)
}
result := string(dec.ToString())
assert.Equal(t, result, ca.output)
}
......@@ -509,9 +502,8 @@ func TestFromString(t *testing.T) {
var dec BigDecimal
err := dec.FromString([]byte(ca.input))
assert.Equal(t, err, ca.err)
result := dec.ToString()
//, Commentf("dec:%s", dec.String())
assert.Equal(t, string(result), ca.output)
result := string(dec.ToString())
assert.Equal(t, result, ca.output)
}
wordBufLen = maxWordBufLen
}
......@@ -528,7 +520,7 @@ func TestToString(t *testing.T) {
}
for _, ca := range tests {
var dec BigDecimal
dec.FromString([]byte(ca.input))
_ = dec.FromString([]byte(ca.input))
result := dec.ToString()
assert.Equal(t, string(result), ca.output)
}
......
package hessian
import (
"github.com/pkg/errors"
"math"
"strings"
"unicode"
......@@ -126,8 +125,7 @@ const (
func strToInt(str string) (int64, error) {
str = strings.TrimSpace(str)
if len(str) == 0 {
//todo ErrTruncated
return 0, nil
return 0, ErrTruncated
}
negative := false
i := 0
......@@ -145,17 +143,13 @@ func strToInt(str string) (int64, error) {
r := uint64(0)
for ; i < len(str); i++ {
if !unicode.IsDigit(rune(str[i])) {
//todo ErrTruncated
// err = ErrTruncated
err = errors.Errorf("")
err = ErrTruncated
break
}
hasNum = true
if r >= uintCutOff {
r = 0
// todo err = errors.Trace(ErrBadNumber)
err = errors.Errorf("")
err = ErrBadNumber
break
}
r = r * uint64(10)
......@@ -163,25 +157,21 @@ func strToInt(str string) (int64, error) {
r1 := r + uint64(str[i]-'0')
if r1 < r || r1 > maxUint {
r = 0
//todo err = errors.Trace(ErrBadNumber)
err = errors.Errorf("")
err = ErrBadNumber
break
}
r = r1
}
if !hasNum {
//err = ErrTruncated
err = ErrTruncated
}
if !negative && r >= intCutOff {
//todo errors.Trace(ErrBadNumber)
return math.MaxInt64, nil
return math.MaxInt64, ErrBadNumber
}
if negative && r > intCutOff {
//todo errors.Trace(ErrBadNumber)
return math.MinInt64, nil
return math.MinInt64, ErrBadNumber
}
if negative {
......
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