Skip to content

Commit

Permalink
ci: also launch tests on i386
Browse files Browse the repository at this point in the history
Fixes #6
Fixes #46
  • Loading branch information
ccoVeille committed Nov 13, 2024
1 parent 1e9618e commit 8d03e59
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 36 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/golang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ jobs:

- name: Launch golangci-lint
uses: golangci/[email protected]

- name: Run tests on i386
run: go test
with:

Check failure on line 39 in .github/workflows/golang.yml

View workflow job for this annotation

GitHub Actions / action-lint

this step is for running shell command since it contains at least one of "run", "shell" keys, but also contains "with" key which is used for running action
goarch: '386'
48 changes: 48 additions & 0 deletions conversion_non386_test.go
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
})
})
}
27 changes: 12 additions & 15 deletions conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -761,10 +761,8 @@ func TestToInt32(t *testing.T) {
{name: "negative within range", input: -10000, want: -10000},
})

assertInt32Error(t, []caseInt32[int]{
{name: "positive out of range", input: math.MaxInt32 + 1},
{name: "negative out of range", input: math.MinInt32 - 1},
})
// There are extra checks in [TestToInt32Extra]
// the tests are separated because they cannot work on i386
})

t.Run("from int8", func(t *testing.T) {
Expand Down Expand Up @@ -916,7 +914,8 @@ func TestToUint32(t *testing.T) {
})

assertUint32Error(t, []caseUint32[int]{
{name: "positive out of range", input: math.MaxUint32 + 1},
// There are extra checks in [TestToUint32Extra]
// the tests are separated because they cannot work on i386
{name: "negative value", input: -1},
})
})
Expand Down Expand Up @@ -972,9 +971,8 @@ func TestToUint32(t *testing.T) {
{name: "positive within range", input: 100, want: 100},
})

assertUint32Error(t, []caseUint32[uint]{
{name: "positive out of range", input: math.MaxUint32 + 1},
})
// There are extra checks in [TestToUint32Extra]
// the tests are separated because they cannot work on i386
})

t.Run("from uint8", func(t *testing.T) {
Expand Down Expand Up @@ -1112,9 +1110,8 @@ func TestToInt64(t *testing.T) {
{name: "positive within range", input: 100, want: 100},
})

assertInt64Error(t, []caseInt64[uint]{
{name: "positive out of range", input: math.MaxInt64 + 1},
})
// There are extra checks in [TestToInt64Extra]
// the tests are separated because they cannot work on i386
})

t.Run("from uint8", func(t *testing.T) {
Expand Down Expand Up @@ -1419,9 +1416,8 @@ func TestToInt(t *testing.T) {
{name: "positive within range", input: 100, want: 100},
})

assertIntError(t, []caseInt[uint]{
{name: "positive out of range", input: math.MaxInt64 + 1},
})
// There are extra checks in [TestToIntExtra]
// the tests are separated because they cannot work on i386
})

t.Run("from uint8", func(t *testing.T) {
Expand Down Expand Up @@ -1477,7 +1473,8 @@ 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
// There are extra checks in [TestToIntExtra]
// the tests are separated because they cannot work on i386
})

assertIntError(t, []caseInt[float64]{
Expand Down
28 changes: 28 additions & 0 deletions examples_386_test.go
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
}
28 changes: 28 additions & 0 deletions examples_non386_test.go
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
}
24 changes: 3 additions & 21 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ func ExampleToInt32() {
}
fmt.Println(i)

a = int(math.MaxUint32 + 1)
_, err = safecast.ToInt32(a)
b := uint32(math.MaxInt32) + 1
_, err = safecast.ToInt32(b)
if err != nil {
fmt.Println(err)
}
// Output:
// 42
// conversion issue: 4294967296 (int) is greater than 2147483647 (int32): maximum value for this type exceeded
// conversion issue: 2147483648 (uint32) is greater than 2147483647 (int32): maximum value for this type exceeded
}

func ExampleToUint32() {
Expand Down Expand Up @@ -151,24 +151,6 @@ func ExampleToUint64() {
// conversion issue: -1 (int8) is less than 0 (uint64): minimum value for this type exceeded
}

func ExampleToInt() {
a := uint64(42)
i, err := safecast.ToInt(a)
if err != nil {
fmt.Println(err)
}
fmt.Println(i)

a = uint64(math.MaxInt64) + 1
_, err = safecast.ToInt(a)
if err != nil {
fmt.Println(err)
}
// Output:
// 42
// conversion issue: 9223372036854775808 (uint64) is greater than 9223372036854775807 (int): maximum value for this type exceeded
}

func ExampleToUint() {
a := int8(42)
i, err := safecast.ToUint(a)
Expand Down

0 comments on commit 8d03e59

Please sign in to comment.