Skip to content
Merged
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
213 changes: 115 additions & 98 deletions safe_conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ import (
"github.com/stretchr/testify/require"
)

const (
zeroValueName = "zero value"
positiveValueName = "positive value"
maxUint32Name = "max uint32"
negativeValueName = "negative value"
valueTooLargeName = "value too large"
valueMuchTooLargeName = "value much too large"
maxIntName = "max int"
maxInt32Name = "max int32"
minInt32Name = "min int32"
maxInt64Name = "max int64"
minInt64Name = "min int64"
valueTooSmallName = "value too small"
maxUint16Name = "max uint16"
maxUint64Name = "max uint64"
)

// TestIntToUint32 tests the conversion from int to uint32.
func TestIntToUint32(t *testing.T) {
tests := []struct {
Expand All @@ -20,11 +37,11 @@ func TestIntToUint32(t *testing.T) {
expect uint32
wantErr bool
}{
{"zero value", 0, 0, false},
{"positive value", 100, 100, false},
{"max uint32", math.MaxUint32, math.MaxUint32, false},
{"negative value", -1, 0, true},
{"value too large", math.MaxInt, 0, true}, // Assuming this is on a 64-bit system where MaxInt > MaxUint32
{zeroValueName, 0, 0, false},
{positiveValueName, 100, 100, false},
{maxUint32Name, math.MaxUint32, math.MaxUint32, false},
{negativeValueName, -1, 0, true},
{valueTooLargeName, math.MaxInt, 0, true}, // Assuming this is on a 64-bit system where MaxInt > MaxUint32
}

for _, tt := range tests {
Expand All @@ -49,11 +66,11 @@ func TestUint64ToUint32(t *testing.T) {
expect uint32
wantErr bool
}{
{"zero value", 0, 0, false},
{"positive value", 100, 100, false},
{"max uint32", uint64(math.MaxUint32), math.MaxUint32, false},
{"value too large", uint64(math.MaxUint32) + 1, 0, true},
{"value much too large", math.MaxUint64, 0, true},
{zeroValueName, 0, 0, false},
{positiveValueName, 100, 100, false},
{maxUint32Name, uint64(math.MaxUint32), math.MaxUint32, false},
{valueTooLargeName, uint64(math.MaxUint32) + 1, 0, true},
{valueMuchTooLargeName, math.MaxUint64, 0, true},
}

for _, tt := range tests {
Expand All @@ -78,10 +95,10 @@ func TestInt64ToUint64(t *testing.T) {
expect uint64
wantErr bool
}{
{"zero value", 0, 0, false},
{"positive value", 100, 100, false},
{"max int64", math.MaxInt64, uint64(math.MaxInt64), false},
{"negative value", -1, 0, true},
{zeroValueName, 0, 0, false},
{positiveValueName, 100, 100, false},
{maxInt64Name, math.MaxInt64, uint64(math.MaxInt64), false},
{negativeValueName, -1, 0, true},
}

for _, tt := range tests {
Expand All @@ -106,10 +123,10 @@ func TestIntToUint64(t *testing.T) {
expect uint64
wantErr bool
}{
{"zero value", 0, 0, false},
{"positive value", 100, 100, false},
{"max int", math.MaxInt, uint64(math.MaxInt), false},
{"negative value", -1, 0, true},
{zeroValueName, 0, 0, false},
{positiveValueName, 100, 100, false},
{maxIntName, math.MaxInt, uint64(math.MaxInt), false},
{negativeValueName, -1, 0, true},
}

for _, tt := range tests {
Expand All @@ -134,11 +151,11 @@ func TestUint64ToInt(t *testing.T) {
expect int
wantErr bool
}{
{"zero value", 0, 0, false},
{"positive value", 100, 100, false},
{"max int", uint64(math.MaxInt), math.MaxInt, false},
{"value too large", uint64(math.MaxInt) + 1, 0, true},
{"value much too large", math.MaxUint64, 0, true},
{zeroValueName, 0, 0, false},
{positiveValueName, 100, 100, false},
{maxIntName, uint64(math.MaxInt), math.MaxInt, false},
{valueTooLargeName, uint64(math.MaxInt) + 1, 0, true},
{valueMuchTooLargeName, math.MaxUint64, 0, true},
}

for _, tt := range tests {
Expand All @@ -163,14 +180,14 @@ func TestInt64ToInt32(t *testing.T) {
expect int32
wantErr bool
}{
{"zero value", 0, 0, false},
{"positive value", 100, 100, false},
{"max int32", int64(math.MaxInt32), math.MaxInt32, false},
{"min int32", int64(math.MinInt32), math.MinInt32, false},
{"value too large", int64(math.MaxInt32) + 1, 0, true},
{"value too small", int64(math.MinInt32) - 1, 0, true},
{"max int64", math.MaxInt64, 0, true},
{"min int64", math.MinInt64, 0, true},
{zeroValueName, 0, 0, false},
{positiveValueName, 100, 100, false},
{maxInt32Name, int64(math.MaxInt32), math.MaxInt32, false},
{minInt32Name, int64(math.MinInt32), math.MinInt32, false},
{valueTooLargeName, int64(math.MaxInt32) + 1, 0, true},
{valueTooSmallName, int64(math.MinInt32) - 1, 0, true},
{maxInt64Name, math.MaxInt64, 0, true},
{minInt64Name, math.MinInt64, 0, true},
}

for _, tt := range tests {
Expand All @@ -195,13 +212,13 @@ func TestIntToInt32(t *testing.T) {
expect int32
wantErr bool
}{
{"zero value", 0, 0, false},
{"positive value", 100, 100, false},
{"max int32", math.MaxInt32, math.MaxInt32, false},
{"min int32", math.MinInt32, math.MinInt32, false},
{zeroValueName, 0, 0, false},
{positiveValueName, 100, 100, false},
{maxInt32Name, math.MaxInt32, math.MaxInt32, false},
{minInt32Name, math.MinInt32, math.MinInt32, false},
// These tests below assume a 64-bit system where int can exceed int32 range
{"value too large", int(math.MaxInt32) + 1, 0, true},
{"value too small", int(math.MinInt32) - 1, 0, true},
{valueTooLargeName, int(math.MaxInt32) + 1, 0, true},
{valueTooSmallName, int(math.MinInt32) - 1, 0, true},
}

for _, tt := range tests {
Expand All @@ -226,11 +243,11 @@ func TestInt32ToUint32(t *testing.T) {
expect uint32
wantErr bool
}{
{"zero value", 0, 0, false},
{"positive value", 100, 100, false},
{"max int32", math.MaxInt32, uint32(math.MaxInt32), false},
{"negative value", -1, 0, true},
{"min int32", math.MinInt32, 0, true},
{zeroValueName, 0, 0, false},
{positiveValueName, 100, 100, false},
{maxInt32Name, math.MaxInt32, uint32(math.MaxInt32), false},
{negativeValueName, -1, 0, true},
{minInt32Name, math.MinInt32, 0, true},
}

for _, tt := range tests {
Expand All @@ -255,11 +272,11 @@ func TestInt64ToUint32(t *testing.T) {
expect uint32
wantErr bool
}{
{"zero value", 0, 0, false},
{"positive value", 100, 100, false},
{"max uint32", int64(math.MaxUint32), math.MaxUint32, false},
{"negative value", -1, 0, true},
{"value too large", int64(math.MaxUint32) + 1, 0, true},
{zeroValueName, 0, 0, false},
{positiveValueName, 100, 100, false},
{maxUint32Name, int64(math.MaxUint32), math.MaxUint32, false},
{negativeValueName, -1, 0, true},
{valueTooLargeName, int64(math.MaxUint32) + 1, 0, true},
}

for _, tt := range tests {
Expand All @@ -284,11 +301,11 @@ func TestBigWordToUint32(t *testing.T) {
expect uint32
wantErr bool
}{
{"zero value", big.Word(0), 0, false},
{"positive value", big.Word(100), 100, false},
{"max uint32", big.Word(math.MaxUint32), math.MaxUint32, false},
{zeroValueName, big.Word(0), 0, false},
{positiveValueName, big.Word(100), 100, false},
{maxUint32Name, big.Word(math.MaxUint32), math.MaxUint32, false},
// This test might not work on 32-bit systems where big.Word is uint32
{"value too large", big.Word(uint64(math.MaxUint32) + 1), 0, true},
{valueTooLargeName, big.Word(uint64(math.MaxUint32) + 1), 0, true},
}

for _, tt := range tests {
Expand All @@ -313,11 +330,11 @@ func TestIntToUint16(t *testing.T) {
expect uint16
wantErr bool
}{
{"zero value", 0, 0, false},
{"positive value", 100, 100, false},
{"max uint16", math.MaxUint16, math.MaxUint16, false},
{"negative value", -1, 0, true},
{"value too large", int(math.MaxUint16) + 1, 0, true},
{zeroValueName, 0, 0, false},
{positiveValueName, 100, 100, false},
{maxUint16Name, math.MaxUint16, math.MaxUint16, false},
{negativeValueName, -1, 0, true},
{valueTooLargeName, int(math.MaxUint16) + 1, 0, true},
}

for _, tt := range tests {
Expand All @@ -342,12 +359,12 @@ func TestIntToInt16(t *testing.T) {
expect int16
wantErr bool
}{
{"zero value", 0, 0, false},
{"positive value", 100, 100, false},
{zeroValueName, 0, 0, false},
{positiveValueName, 100, 100, false},
{"max int16", 32767, 32767, false}, // MaxInt16 is 32767
{"min int16", -32768, -32768, false}, // MinInt16 is -32768
{"value too large", 32768, 0, true}, // MaxInt16 + 1
{"value too small", -32769, 0, true}, // MinInt16 - 1
{valueTooLargeName, 32768, 0, true}, // MaxInt16 + 1
{valueTooSmallName, -32769, 0, true}, // MinInt16 - 1
}

for _, tt := range tests {
Expand All @@ -372,11 +389,11 @@ func TestUintToUint32(t *testing.T) {
expect uint32
wantErr bool
}{
{"zero value", 0, 0, false},
{"positive value", 100, 100, false},
{"max uint32", uint(math.MaxUint32), math.MaxUint32, false},
{zeroValueName, 0, 0, false},
{positiveValueName, 100, 100, false},
{maxUint32Name, uint(math.MaxUint32), math.MaxUint32, false},
// This test might not work on 32-bit systems where uint is uint32
{"value too large", uint(uint64(math.MaxUint32) + 1), 0, true},
{valueTooLargeName, uint(uint64(math.MaxUint32) + 1), 0, true},
}

for _, tt := range tests {
Expand Down Expand Up @@ -430,11 +447,11 @@ func TestUint32ToUint8(t *testing.T) {
expect uint8
wantErr bool
}{
{"zero value", 0, 0, false},
{"positive value", 100, 100, false},
{zeroValueName, 0, 0, false},
{positiveValueName, 100, 100, false},
{"max uint8", uint32(math.MaxUint8), math.MaxUint8, false},
{"value too large", uint32(math.MaxUint8) + 1, 0, true},
{"max uint32", math.MaxUint32, 0, true},
{valueTooLargeName, uint32(math.MaxUint8) + 1, 0, true},
{maxUint32Name, math.MaxUint32, 0, true},
}

for _, tt := range tests {
Expand All @@ -459,10 +476,10 @@ func TestUintptrToInt(t *testing.T) {
expect int
wantErr bool
}{
{"zero value", 0, 0, false},
{"positive value", 100, 100, false},
{"max int", uintptr(math.MaxInt), math.MaxInt, false},
{"value too large", uintptr(math.MaxInt) + 1, 0, true},
{zeroValueName, 0, 0, false},
{positiveValueName, 100, 100, false},
{maxIntName, uintptr(math.MaxInt), math.MaxInt, false},
{valueTooLargeName, uintptr(math.MaxInt) + 1, 0, true},
}

for _, tt := range tests {
Expand All @@ -487,11 +504,11 @@ func TestUint64ToInt64(t *testing.T) {
expect int64
wantErr bool
}{
{"zero value", 0, 0, false},
{"positive value", 100, 100, false},
{"max int64", uint64(math.MaxInt64), math.MaxInt64, false},
{"value too large", uint64(math.MaxInt64) + 1, 0, true},
{"max uint64", math.MaxUint64, 0, true},
{zeroValueName, 0, 0, false},
{positiveValueName, 100, 100, false},
{maxInt64Name, uint64(math.MaxInt64), math.MaxInt64, false},
{valueTooLargeName, uint64(math.MaxInt64) + 1, 0, true},
{maxUint64Name, math.MaxUint64, 0, true},
}

for _, tt := range tests {
Expand All @@ -516,11 +533,11 @@ func TestUint32ToInt32(t *testing.T) {
expect int32
wantErr bool
}{
{"zero value", 0, 0, false},
{"positive value", 100, 100, false},
{"max int32", uint32(math.MaxInt32), math.MaxInt32, false},
{"value too large", uint32(math.MaxInt32) + 1, 0, true},
{"max uint32", math.MaxUint32, 0, true},
{zeroValueName, 0, 0, false},
{positiveValueName, 100, 100, false},
{maxInt32Name, uint32(math.MaxInt32), math.MaxInt32, false},
{valueTooLargeName, uint32(math.MaxInt32) + 1, 0, true},
{maxUint32Name, math.MaxUint32, 0, true},
}

for _, tt := range tests {
Expand All @@ -545,11 +562,11 @@ func TestUint64ToInt32(t *testing.T) {
expect int32
wantErr bool
}{
{"zero value", 0, 0, false},
{"positive value", 100, 100, false},
{"max int32", uint64(math.MaxInt32), math.MaxInt32, false},
{"value too large", uint64(math.MaxInt32) + 1, 0, true},
{"max uint64", math.MaxUint64, 0, true},
{zeroValueName, 0, 0, false},
{positiveValueName, 100, 100, false},
{maxInt32Name, uint64(math.MaxInt32), math.MaxInt32, false},
{valueTooLargeName, uint64(math.MaxInt32) + 1, 0, true},
{maxUint64Name, math.MaxUint64, 0, true},
}

for _, tt := range tests {
Expand All @@ -574,9 +591,9 @@ func TestUint32ToInt64(t *testing.T) {
expect int64
wantErr bool
}{
{"zero value", 0, 0, false},
{"positive value", 100, 100, false},
{"max uint32", math.MaxUint32, int64(math.MaxUint32), false},
{zeroValueName, 0, 0, false},
{positiveValueName, 100, 100, false},
{maxUint32Name, math.MaxUint32, int64(math.MaxUint32), false},
}

for _, tt := range tests {
Expand All @@ -601,9 +618,9 @@ func TestUint32ToUint64(t *testing.T) {
expect uint64
wantErr bool
}{
{"zero value", 0, 0, false},
{"positive value", 100, 100, false},
{"max uint32", math.MaxUint32, uint64(math.MaxUint32), false},
{zeroValueName, 0, 0, false},
{positiveValueName, 100, 100, false},
{maxUint32Name, math.MaxUint32, uint64(math.MaxUint32), false},
}

for _, tt := range tests {
Expand All @@ -628,11 +645,11 @@ func TestUint64ToUint16(t *testing.T) {
expect uint16
wantErr bool
}{
{"zero value", 0, 0, false},
{"positive value", 100, 100, false},
{"max uint16", uint64(math.MaxUint16), math.MaxUint16, false},
{"value too large", uint64(math.MaxUint16) + 1, 0, true},
{"max uint64", math.MaxUint64, 0, true},
{zeroValueName, 0, 0, false},
{positiveValueName, 100, 100, false},
{maxUint16Name, uint64(math.MaxUint16), math.MaxUint16, false},
{valueTooLargeName, uint64(math.MaxUint16) + 1, 0, true},
{maxUint64Name, math.MaxUint64, 0, true},
}

for _, tt := range tests {
Expand Down