Skip to content

Commit

Permalink
for tt.wantInt64
Browse files Browse the repository at this point in the history
  • Loading branch information
hyorigo committed Jun 20, 2024
1 parent fc5c79c commit 2837a72
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
28 changes: 25 additions & 3 deletions dataconv/types/hybrid.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types

import (
"fmt"
"math"

"go.starlark.net/starlark"
)
Expand Down Expand Up @@ -53,17 +54,38 @@ func (p FloatOrInt) GoFloat64() float64 {

// GoInt returns the Go int representation of the FloatOrInt.
func (p FloatOrInt) GoInt() int {
return int(p)
f := float64(p)
if f < float64(math.MinInt) || f > float64(math.MaxInt) {
if f < 0 {
return math.MinInt
}
return math.MaxInt

Check warning on line 62 in dataconv/types/hybrid.go

View check run for this annotation

Codecov / codecov/patch

dataconv/types/hybrid.go#L59-L62

Added lines #L59 - L62 were not covered by tests
}
return int(f)
}

// GoInt32 returns the Go int32 representation of the FloatOrInt.
func (p FloatOrInt) GoInt32() int32 {
return int32(p)
f := float64(p)
if f < float64(math.MinInt32) || f > float64(math.MaxInt32) {
if f < 0 {
return math.MinInt32
}
return math.MaxInt32
}
return int32(f)
}

// GoInt64 returns the Go int64 representation of the FloatOrInt.
func (p FloatOrInt) GoInt64() int64 {
return int64(p)
f := float64(p)
if f < float64(math.MinInt64) || f > float64(math.MaxInt64) {
if f < 0 {
return math.MinInt64
}
return math.MaxInt64

Check warning on line 86 in dataconv/types/hybrid.go

View check run for this annotation

Codecov / codecov/patch

dataconv/types/hybrid.go#L83-L86

Added lines #L83 - L86 were not covered by tests
}
return int64(f)
}

// NumericValue holds a Starlark numeric value and tracks its type.
Expand Down
17 changes: 15 additions & 2 deletions dataconv/types/hybrid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,36 +48,49 @@ func TestFloatOrInt_Value(t *testing.T) {
v FloatOrInt
wantInt int
wantInt32 int32
wantInt64 int64
wantFlt float64
}{
{
name: "zero",
v: 0,
wantInt: 0,
wantInt32: 0,
wantInt64: 0,
wantFlt: 0,
},
{
name: "int",
v: 1,
wantInt: 1,
wantInt32: 1,
wantInt64: 1,
wantFlt: 1,
},
{
name: "float",
v: 1.2,
wantInt: 1,
wantInt32: 1,
wantInt64: 1,
wantFlt: 1.2,
},
{
name: "large",
v: 1e12 + 1,
wantInt: 1000000000001,
wantInt32: 2147483647,
wantInt64: 1000000000001,
wantFlt: 1e12 + 1,
},
{
name: "underflow",
v: -1e12 - 1,
wantInt: -1000000000001,
wantInt32: -2147483648,
wantInt64: -1000000000001,
wantFlt: -1e12 - 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -87,8 +100,8 @@ func TestFloatOrInt_Value(t *testing.T) {
if got := tt.v.GoInt32(); got != tt.wantInt32 {
t.Errorf("FloatOrInt.GoInt32() = %v, want %v", got, tt.wantInt32)
}
if got := tt.v.GoInt64(); got != int64(tt.wantInt) {
t.Errorf("FloatOrInt.GoInt64() = %v, want %v", got, int64(tt.wantInt))
if got := tt.v.GoInt64(); got != tt.wantInt64 {
t.Errorf("FloatOrInt.GoInt64() = %v, want %v", got, tt.wantInt64)
}

if got := tt.v.GoFloat(); got != tt.wantFlt {
Expand Down

0 comments on commit 2837a72

Please sign in to comment.