-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add safe integer conversion functions (#74)
- Loading branch information
Showing
3 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package safeconvert | ||
|
||
import ( | ||
"errors" | ||
"math" | ||
) | ||
|
||
// IntToUint converts int to uint safely, checking for negative values. | ||
func IntToUint(i int) (uint, error) { | ||
if i < 0 { | ||
return 0, errors.New("cannot convert negative int to uint") | ||
} | ||
return uint(i), nil | ||
} | ||
|
||
// UintToInt converts uint to int safely, checking for overflow. | ||
func UintToInt(u uint) (int, error) { | ||
if u > math.MaxInt { | ||
return 0, errors.New("integer overflow: uint value exceeds max int value") | ||
} | ||
return int(u), nil | ||
} | ||
|
||
// Int64ToUint64 converts int64 to uint64 safely, checking for negative values. | ||
func Int64ToUint64(i int64) (uint64, error) { | ||
if i < 0 { | ||
return 0, errors.New("cannot convert negative int64 to uint64") | ||
} | ||
return uint64(i), nil | ||
} | ||
|
||
// Uint64ToInt64 converts uint64 to int64 safely, checking for overflow. | ||
func Uint64ToInt64(u uint64) (int64, error) { | ||
if u > math.MaxInt64 { | ||
return 0, errors.New("integer overflow: uint64 value exceeds max int64 value") | ||
} | ||
return int64(u), nil | ||
} | ||
|
||
// SafeUint64ToInt converts uint64 to int safely, checking for overflow. | ||
func Uint64ToInt(u uint64) (int, error) { | ||
if u > uint64(math.MaxInt) { | ||
return 0, errors.New("integer overflow: uint64 value exceeds max int value") | ||
} | ||
return int(u), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package safeconvert | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"math" | ||
"testing" | ||
) | ||
|
||
func TestSafeIntToUint(t *testing.T) { | ||
tests := []struct { | ||
input int | ||
expected uint | ||
errExpected bool | ||
}{ | ||
{input: 10, expected: 10}, | ||
{input: -1, expected: 0, errExpected: true}, | ||
{input: 0, expected: 0}, | ||
} | ||
|
||
for _, test := range tests { | ||
result, err := IntToUint(test.input) | ||
if test.errExpected { | ||
assert.Error(t, err, "Expected an error for input: %d", test.input) | ||
} else { | ||
assert.NoError(t, err, "Did not expect an error for input: %d", test.input) | ||
assert.Equal(t, test.expected, result, "Expected result does not match") | ||
} | ||
} | ||
} | ||
|
||
func TestSafeUintToInt(t *testing.T) { | ||
tests := []struct { | ||
input uint | ||
expected int | ||
errExpected bool | ||
}{ | ||
{input: 10, expected: 10}, | ||
{input: uint(math.MaxInt), expected: math.MaxInt}, | ||
{input: uint(math.MaxInt) + 1, expected: 0, errExpected: true}, | ||
} | ||
|
||
for _, test := range tests { | ||
result, err := UintToInt(test.input) | ||
if test.errExpected { | ||
assert.Error(t, err, "Expected an error for input: %d", test.input) | ||
} else { | ||
assert.NoError(t, err, "Did not expect an error for input: %d", test.input) | ||
assert.Equal(t, test.expected, result, "Expected result does not match") | ||
} | ||
} | ||
} | ||
|
||
func TestSafeInt64ToUint64(t *testing.T) { | ||
tests := []struct { | ||
input int64 | ||
expected uint64 | ||
errExpected bool | ||
}{ | ||
{input: 10, expected: 10}, | ||
{input: -1, expected: 0, errExpected: true}, | ||
{input: 0, expected: 0}, | ||
} | ||
|
||
for _, test := range tests { | ||
result, err := Int64ToUint64(test.input) | ||
if test.errExpected { | ||
assert.Error(t, err, "Expected an error for input: %d", test.input) | ||
} else { | ||
assert.NoError(t, err, "Did not expect an error for input: %d", test.input) | ||
assert.Equal(t, test.expected, result, "Expected result does not match") | ||
} | ||
} | ||
} | ||
|
||
func TestSafeUint64ToInt64(t *testing.T) { | ||
tests := []struct { | ||
input uint64 | ||
expected int64 | ||
errExpected bool | ||
}{ | ||
{input: 10, expected: 10}, | ||
{input: math.MaxInt64, expected: math.MaxInt64}, | ||
{input: math.MaxInt64 + 1, expected: 0, errExpected: true}, | ||
} | ||
|
||
for _, test := range tests { | ||
result, err := Uint64ToInt64(test.input) | ||
if test.errExpected { | ||
assert.Error(t, err, "Expected an error for input: %d", test.input) | ||
} else { | ||
assert.NoError(t, err, "Did not expect an error for input: %d", test.input) | ||
assert.Equal(t, test.expected, result, "Expected result does not match") | ||
} | ||
} | ||
} | ||
|
||
func TestSafeUint64ToInt(t *testing.T) { | ||
tests := []struct { | ||
input uint64 | ||
expected int | ||
errExpected bool | ||
}{ | ||
{input: 10, expected: 10}, | ||
{input: uint64(math.MaxInt), expected: math.MaxInt}, | ||
{input: uint64(math.MaxInt) + 1, expected: 0, errExpected: true}, | ||
} | ||
|
||
for _, test := range tests { | ||
result, err := Uint64ToInt(test.input) | ||
if test.errExpected { | ||
assert.Error(t, err, "Expected an error for input: %d", test.input) | ||
} else { | ||
assert.NoError(t, err, "Did not expect an error for input: %d", test.input) | ||
assert.Equal(t, test.expected, result, "Expected result does not match") | ||
} | ||
} | ||
} |