Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

string float act like float when converted to int* #210

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions cast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ func createNumberTestSteps(zero, one, eight, eightnegative, eightpoint31, eightp
eightpoint31negative_32 = float64(float32(eightpoint31negative.(float64)))
}

streight := eight
streightnegative := eightnegative
if kind == reflect.Float32 || kind == reflect.Float64 {
streight = eightpoint31
streightnegative = eightpoint31negative
}

return []testStep{
{int(8), eight, false},
{int8(8), eight, false},
Expand All @@ -59,6 +66,7 @@ func createNumberTestSteps(zero, one, eight, eightnegative, eightpoint31, eightp
{true, one, false},
{false, zero, false},
{"8", eight, false},
{"8.31", streight, false},
{nil, zero, false},
{int(-8), eightnegative, isUint},
{int8(-8), eightnegative, isUint},
Expand All @@ -68,6 +76,7 @@ func createNumberTestSteps(zero, one, eight, eightnegative, eightpoint31, eightp
{float32(-8.31), eightpoint31negative_32, isUint},
{float64(-8.31), eightpoint31negative, isUint},
{"-8", eightnegative, isUint},
{"-8.31", streightnegative, isUint},
{jeight, eight, false},
{jminuseight, eightnegative, isUint},
{jfloateight, eight, false},
Expand Down Expand Up @@ -897,6 +906,15 @@ func BenchmarkTrimZeroDecimal(b *testing.B) {
}
}

func BenchmarkTruncateDecimals(b *testing.B) {
for i := 0; i < b.N; i++ {
truncateDecimals("")
truncateDecimals("123.")
truncateDecimals("120.1")
truncateDecimals("120.001")
}
}

func BenchmarkCommonTimeLayouts(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, commonLayout := range []string{"2019-04-29", "2017-05-30T00:00:00Z"} {
Expand All @@ -923,9 +941,8 @@ func TestIndirectPointers(t *testing.T) {
func TestToTime(t *testing.T) {
c := qt.New(t)

var jntime, jnetime json.Number
var jntime json.Number
_ = json.Unmarshal([]byte("1234567890"), &jntime)
_ = json.Unmarshal([]byte("123.4567890"), &jnetime)
tests := []struct {
input interface{}
expect time.Time
Expand Down Expand Up @@ -968,7 +985,7 @@ func TestToTime(t *testing.T) {
{time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false},
// errors
{"2006", time.Time{}, true},
{jnetime, time.Time{}, true},
{nil, time.Time{}, true},
{testing.T{}, time.Time{}, true},
}

Expand Down Expand Up @@ -1180,6 +1197,21 @@ func TestTrimZeroDecimal(t *testing.T) {

}

func TestTruncateDecimals(t *testing.T) {
c := qt.New(t)

c.Assert(truncateDecimals("+123.0"), qt.Equals, "+123")
c.Assert(truncateDecimals("-123.0000000000000000000001"), qt.Equals, "-123")
c.Assert(truncateDecimals("123."), qt.Equals, "123")
c.Assert(truncateDecimals("0.123"), qt.Equals, "0")
c.Assert(truncateDecimals("1230"), qt.Equals, "1230")

c.Assert(truncateDecimals("0123"), qt.Equals, "0123")
c.Assert(truncateDecimals("0x123"), qt.Equals, "0x123")
c.Assert(truncateDecimals("foo"), qt.Equals, "foo")

}

func assertTimeEqual(t *testing.T, expected, actual time.Time) {
t.Helper()
// Compare the dates using a numeric zone as there are cases where
Expand Down
46 changes: 36 additions & 10 deletions caste.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func ToInt64E(i interface{}) (int64, error) {
case float32:
return int64(s), nil
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(truncateDecimals(trimZeroDecimal(s)), 0, 0)
if err == nil {
return v, nil
}
Expand Down Expand Up @@ -333,7 +333,7 @@ func ToInt32E(i interface{}) (int32, error) {
case float32:
return int32(s), nil
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(truncateDecimals(trimZeroDecimal(s)), 0, 0)
if err == nil {
return int32(v), nil
}
Expand Down Expand Up @@ -385,7 +385,7 @@ func ToInt16E(i interface{}) (int16, error) {
case float32:
return int16(s), nil
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(truncateDecimals(trimZeroDecimal(s)), 0, 0)
if err == nil {
return int16(v), nil
}
Expand Down Expand Up @@ -437,7 +437,7 @@ func ToInt8E(i interface{}) (int8, error) {
case float32:
return int8(s), nil
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(truncateDecimals(trimZeroDecimal(s)), 0, 0)
if err == nil {
return int8(v), nil
}
Expand Down Expand Up @@ -489,7 +489,7 @@ func ToIntE(i interface{}) (int, error) {
case float32:
return int(s), nil
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(truncateDecimals(trimZeroDecimal(s)), 0, 0)
if err == nil {
return int(v), nil
}
Expand Down Expand Up @@ -522,7 +522,7 @@ func ToUintE(i interface{}) (uint, error) {

switch s := i.(type) {
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(truncateDecimals(trimZeroDecimal(s)), 0, 0)
if err == nil {
if v < 0 {
return 0, errNegativeNotAllowed
Expand Down Expand Up @@ -598,7 +598,7 @@ func ToUint64E(i interface{}) (uint64, error) {

switch s := i.(type) {
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(truncateDecimals(trimZeroDecimal(s)), 0, 0)
if err == nil {
if v < 0 {
return 0, errNegativeNotAllowed
Expand Down Expand Up @@ -674,7 +674,7 @@ func ToUint32E(i interface{}) (uint32, error) {

switch s := i.(type) {
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(truncateDecimals(trimZeroDecimal(s)), 0, 0)
if err == nil {
if v < 0 {
return 0, errNegativeNotAllowed
Expand Down Expand Up @@ -750,7 +750,7 @@ func ToUint16E(i interface{}) (uint16, error) {

switch s := i.(type) {
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(truncateDecimals(trimZeroDecimal(s)), 0, 0)
if err == nil {
if v < 0 {
return 0, errNegativeNotAllowed
Expand Down Expand Up @@ -826,7 +826,7 @@ func ToUint8E(i interface{}) (uint8, error) {

switch s := i.(type) {
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(truncateDecimals(trimZeroDecimal(s)), 0, 0)
if err == nil {
if v < 0 {
return 0, errNegativeNotAllowed
Expand Down Expand Up @@ -1496,3 +1496,29 @@ func trimZeroDecimal(s string) string {
}
return s
}

func truncateDecimals(s string) string {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this function could use a test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, I have added it.

v := -1
for i := len(s) - 1; i >= 0; i-- {
if s[i] == '.' {
v = i
break
}
}
if v == -1 {
return s
}
for i := len(s) - 1; i > v; i-- {
if s[i] < '0' || s[i] > '9' {
return s
}
}
for i := v - 1; i >= 0; i-- {
if s[i] < '0' || s[i] > '9' {
if i != 0 || (s[i] != '+' && s[i] != '-') {
return s
}
}
}
return s[:v]
}
Loading