-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
124 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,3 +33,8 @@ jobs: | |
|
||
- name: Launch golangci-lint | ||
uses: golangci/[email protected] | ||
|
||
- name: Run tests on i386 | ||
run: go test | ||
with: | ||
goarch: '386' |
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,48 @@ | ||
//go:build !386 | ||
|
||
package safecast_test | ||
|
||
import ( | ||
"math" | ||
"testing" | ||
) | ||
|
||
func TestToInt32Extra(t *testing.T) { | ||
t.Run("from int", func(t *testing.T) { | ||
assertInt32Error(t, []caseInt32[int]{ | ||
{name: "positive out of range", input: math.MaxInt32 + 1}, | ||
{name: "negative out of range", input: math.MinInt32 - 1}, | ||
}) | ||
}) | ||
} | ||
|
||
func TestToUint32Extra(t *testing.T) { | ||
t.Run("from int", func(t *testing.T) { | ||
assertUint32Error(t, []caseUint32[int]{ | ||
{name: "positive out of range", input: math.MaxUint32 + 1}, | ||
{name: "negative value", input: -1}, | ||
}) | ||
}) | ||
} | ||
|
||
func TestToInt64Extra(t *testing.T) { | ||
t.Run("from uint", func(t *testing.T) { | ||
assertInt64Error(t, []caseInt64[uint]{ | ||
{name: "positive out of range", input: math.MaxInt64 + 1}, | ||
}) | ||
}) | ||
} | ||
|
||
func TestToIntExtra(t *testing.T) { | ||
t.Run("from uint", func(t *testing.T) { | ||
assertIntError(t, []caseInt[uint]{ | ||
{name: "positive out of range", input: math.MaxInt64 + 1}, | ||
}) | ||
}) | ||
|
||
t.Run("from float64", func(t *testing.T) { | ||
assertIntOK(t, []caseInt[float64]{ | ||
{name: "math.MinInt64", input: math.MinInt64, want: math.MinInt64}, // pass because of float imprecision | ||
}) | ||
}) | ||
} |
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,28 @@ | ||
//go:build 386 | ||
|
||
package safecast_test | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
|
||
"github.com/ccoveille/go-safecast" | ||
) | ||
|
||
func ExampleToInt() { | ||
a := uint64(42) | ||
i, err := safecast.ToInt(a) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
fmt.Println(i) | ||
|
||
b := float32(math.MaxFloat32) | ||
_, err = safecast.ToInt(b) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
// Output: | ||
// 42 | ||
// conversion issue: 3.4028235e+38 (float32) is greater than 2147483647 (int): maximum value for this type exceeded | ||
} |
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,28 @@ | ||
//go:build !386 | ||
|
||
package safecast_test | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
|
||
"github.com/ccoveille/go-safecast" | ||
) | ||
|
||
func ExampleToInt() { | ||
a := uint64(42) | ||
i, err := safecast.ToInt(a) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
fmt.Println(i) | ||
|
||
b := float32(math.MaxFloat32) | ||
_, err = safecast.ToInt(b) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
// Output: | ||
// 42 | ||
// conversion issue: 3.4028235e+38 (float32) is greater than 9223372036854775807 (int): maximum value for this type exceeded | ||
} |
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