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

refactor #32

Merged
merged 3 commits into from
Nov 13, 2024
Merged
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
102 changes: 71 additions & 31 deletions asserters.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,24 @@
package safecast

func assertNotNegative[T Type, T2 Type](i T, zero T2) error {
if i < 0 {
return Error{
err: ErrExceedMinimumValue,
value: i,
boundary: zero,
}
}
return nil
}

func checkUpperBoundary[T Type, T2 Type](value T, boundary T2) error {
if value <= 0 {
return nil
}

var greater bool
var overflow bool
switch f := any(value).(type) {
case float64:
// for float64, everything fits in float64 without overflow.
// We are using a greater or equal because float cannot be compared easily because of precision loss.
greater = f >= float64(boundary)
overflow = isFloatOverflow(f, boundary)

case float32:
// everything fits in float32, except float64 greater than math.MaxFloat32.
// So, we must convert to float64 and check.
// We are using a greater or equal because float cannot be compared easily because of precision loss.
greater = float64(f) >= float64(boundary)
overflow = isFloatOverflow(f, boundary)

default:
// for all other integer types, it fits in an uint64 without overflow as we know value is positive.
greater = uint64(value) > uint64(boundary)
overflow = uint64(value) > uint64(boundary)
}

if greater {
if overflow {
return Error{
value: value,
boundary: boundary,
Expand All @@ -48,23 +34,18 @@ func checkLowerBoundary[T Type, T2 Type](value T, boundary T2) error {
return nil
}

var smaller bool
var underflow bool
switch f := any(value).(type) {
case float64:
// everything fits in float64 without overflow.
// We are using a lower or equal because float cannot be compared easily because of precision loss.
smaller = f <= float64(boundary)
underflow = isFloatUnderOverflow(f, boundary)
case float32:
// everything fits in float32, except float64 smaller than -math.MaxFloat32.
// So, we must convert to float64 and check.
// We are using a lower or equal because float cannot be compared easily because of precision loss.
smaller = float64(f) <= float64(boundary)
underflow = isFloatUnderOverflow(f, boundary)
default:
// for all other integer types, it fits in an int64 without overflow as we know value is negative.
smaller = int64(value) < int64(boundary)
underflow = int64(value) < int64(boundary)
}

if smaller {
if underflow {
return Error{
value: value,
boundary: boundary,
Expand All @@ -74,3 +55,62 @@ func checkLowerBoundary[T Type, T2 Type](value T, boundary T2) error {

return nil
}

func isFloatOverflow[T Type, T2 Type](value T, boundary T2) bool {
// boundary is positive when checking for an overflow

// everything fits in float64 without overflow.
v := float64(value)
b := float64(boundary)

if v > b*1.01 {
// way greater than the maximum value
return true
}

if v < b*0.99 {
// we are way below the maximum value
return false
}
// we are close to the maximum value

// let's try to create the overflow
// by converting back and forth with type juggling
conv := float64(T(T2(v)))

// the number was between 0.99 and 1.01 of the maximum value
// once converted back and forth, we need to check if the value is in the same range
// if not, so it's an overflow
return conv <= b*0.99
}

func isFloatUnderOverflow[T Type, T2 Type](value T, boundary T2) bool {
// everything fits in float64 without overflow.
v := float64(value)
b := float64(boundary)

if b == 0 {
// boundary is 0
// we can check easily
return value < 0
}

if v < b*1.01 { // please note value and boundary are negative here
// way below than the minimum value, it would underflow
return true
}

if v > b*0.99 { // please note value and boundary are negative here
// way greater than the minimum value
return false
}

// we are just above to the minimum value
// let's try to create the underflow
conv := float64(T(T2(v)))

// the number was between 0.99 and 1.01 of the minimum value
// once converted back and forth, we need to check if the value is in the same range
// if not, so it's an underflow
return conv >= b*0.99
ccoVeille marked this conversation as resolved.
Show resolved Hide resolved
}
14 changes: 9 additions & 5 deletions conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func ToInt[T Type](i T) (int, error) {
// If the conversion results in a value outside the range of an uint,
// an [ErrConversionIssue] error is returned.
func ToUint[T Type](i T) (uint, error) {
if err := assertNotNegative(i, uint(0)); err != nil {
if err := checkLowerBoundary(i, uint(0)); err != nil {
return 0, err
}

Expand Down Expand Up @@ -56,7 +56,7 @@ func ToInt8[T Type](i T) (int8, error) {
// If the conversion results in a value outside the range of an uint8,
// an [ErrConversionIssue] error is returned.
func ToUint8[T Type](i T) (uint8, error) {
if err := assertNotNegative(i, uint8(0)); err != nil {
if err := checkLowerBoundary(i, uint8(0)); err != nil {
return 0, err
}

Expand Down Expand Up @@ -86,7 +86,7 @@ func ToInt16[T Type](i T) (int16, error) {
// If the conversion results in a value outside the range of an uint16,
// an [ErrConversionIssue] error is returned.
func ToUint16[T Type](i T) (uint16, error) {
if err := assertNotNegative(i, uint16(0)); err != nil {
if err := checkLowerBoundary(i, uint16(0)); err != nil {
return 0, err
}

Expand Down Expand Up @@ -116,7 +116,7 @@ func ToInt32[T Type](i T) (int32, error) {
// If the conversion results in a value outside the range of an uint32,
// an [ErrConversionIssue] error is returned.
func ToUint32[T Type](i T) (uint32, error) {
if err := assertNotNegative(i, uint32(0)); err != nil {
if err := checkLowerBoundary(i, uint32(0)); err != nil {
return 0, err
}

Expand All @@ -131,6 +131,10 @@ func ToUint32[T Type](i T) (uint32, error) {
// If the conversion results in a value outside the range of an int64,
// an [ErrConversionIssue] error is returned.
func ToInt64[T Type](i T) (int64, error) {
if err := checkLowerBoundary(i, int64(math.MinInt64)); err != nil {
return 0, err
}

if err := checkUpperBoundary(i, int64(math.MaxInt64)); err != nil {
return 0, err
}
Expand All @@ -142,7 +146,7 @@ func ToInt64[T Type](i T) (int64, error) {
// If the conversion results in a value outside the range of an uint64,
// an [ErrConversionIssue] error is returned.
func ToUint64[T Type](i T) (uint64, error) {
if err := assertNotNegative(i, uint64(0)); err != nil {
if err := checkLowerBoundary(i, uint64(0)); err != nil {
return 0, err
}

Expand Down
42 changes: 33 additions & 9 deletions conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1154,11 +1154,18 @@ func TestToInt64(t *testing.T) {
{name: "zero", input: 0.0, want: 0},
{name: "rounded value", input: 1.1, want: 1},
{name: "positive within range", input: 10000.9, want: 10000},
{name: "big value", input: math.MaxInt16, want: math.MaxInt16},
{name: "max int16", input: math.MaxInt16, want: math.MaxInt16},
{name: "min int16", input: math.MinInt16, want: math.MinInt16},
{name: "max int32", input: math.MaxInt32, want: 2147483648}, // number differs due to float imprecision
{name: "min int32", input: math.MinInt32, want: math.MinInt32},
})

assertInt64Error(t, []caseInt64[float32]{
{name: "positive out of range", input: math.MaxFloat32},
{name: "out of range math.MaxInt64 + 1", input: math.MaxInt64 + 1},
{name: "out of range math.MaxUint64", input: math.MaxUint64},
{name: "out of range math.MinInt64", input: math.MinInt64 * 1.02}, // because of float imprecision, we have to exceed the min int64 to trigger the error
{name: "out of range math.MaxFloat32", input: math.MaxFloat32},
{name: "out of range -math.MaxFloat32", input: -math.MaxFloat32},
})
})

Expand All @@ -1167,11 +1174,20 @@ func TestToInt64(t *testing.T) {
{name: "zero", input: 0.0, want: 0},
{name: "rounded value", input: 1.1, want: 1},
{name: "positive within range", input: 10000.9, want: 10000},
{name: "big value", input: math.MaxInt32, want: math.MaxInt32},
{name: "max int16", input: math.MaxInt16, want: math.MaxInt16},
{name: "min int16", input: math.MinInt16, want: math.MinInt16},
{name: "max int32", input: math.MaxInt32, want: math.MaxInt32},
{name: "min int32", input: math.MinInt32, want: math.MinInt32},
})

assertInt64Error(t, []caseInt64[float64]{
{name: "positive out of range", input: math.MaxInt64},
{name: "out of range math.MaxInt64 + 1", input: math.MaxInt64 + 1},
{name: "out of range math.MaxUint64", input: math.MaxUint64},
{name: "out of range math.MinInt64", input: math.MinInt64 * 1.02}, // because of float imprecision, we have to exceed the min int64 to trigger the error
{name: "out of range math.MaxFloat32", input: math.MaxFloat32},
{name: "out of range -math.MaxFloat32", input: -math.MaxFloat32},
{name: "out of range math.MaxFloat64", input: math.MaxFloat64},
{name: "out of range -math.MaxFloat64", input: -math.MaxFloat64},
})
})
}
Expand Down Expand Up @@ -1307,7 +1323,6 @@ func TestToUint64(t *testing.T) {
assertUint64Error(t, []caseUint64[float32]{
{name: "negative value", input: -1},
{name: "out of range max uint64", input: math.MaxUint64},
{name: "out of range max float32", input: math.MaxFloat32},
})
})

Expand Down Expand Up @@ -1449,8 +1464,11 @@ func TestToInt(t *testing.T) {
})

assertIntError(t, []caseInt[float32]{
{name: "positive out of range", input: math.MaxFloat32},
{name: "negative out of range", input: -math.MaxFloat32},
{name: "out of range math.MaxInt64 + 1", input: math.MaxInt64 + 1},
{name: "out of range math.MaxUint64", input: math.MaxUint64},
{name: "out of range math.MinInt64", input: math.MinInt64 * 1.02}, // because of float imprecision, we have to exceed the min int64 to trigger the error
{name: "out of range math.MaxFloat32", input: math.MaxFloat32},
{name: "out of range -math.MaxFloat32", input: -math.MaxFloat32},
})
})

Expand All @@ -1459,11 +1477,17 @@ func TestToInt(t *testing.T) {
{name: "zero", input: 0.0, want: 0},
{name: "rounded value", input: 1.1, want: 1},
{name: "positive within range", input: 10000.9, want: 10000},
{name: "math.MinInt64", input: math.MinInt64, want: math.MinInt64}, // pass because of float imprecision
})

assertIntError(t, []caseInt[float64]{
{name: "positive out of range", input: math.MaxFloat32},
{name: "negative out of range", input: -math.MaxFloat32},
{name: "out of range math.MaxInt64 + 1", input: math.MaxInt64 + 1},
{name: "out of range math.MaxUint64", input: math.MaxUint64},
{name: "out of range math.MinInt64", input: math.MinInt64 * 1.02}, // because of float imprecision, we have to exceed the min int64 to trigger the error
{name: "out of range math.MaxFloat32", input: math.MaxFloat32},
{name: "out of range -math.MaxFloat32", input: -math.MaxFloat32},
{name: "out of range math.MaxFloat64", input: math.MaxFloat64},
{name: "out of range -math.MaxFloat64", input: -math.MaxFloat64},
})
})
}
Expand Down
Loading