-
Notifications
You must be signed in to change notification settings - Fork 2
/
conversion_64bit_test.go
76 lines (66 loc) · 2.11 KB
/
conversion_64bit_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//go:build !386 && !arm
package safecast_test
// The tests in conversion_test.go are the ones that are not architecture dependent
// The tests in conversion_64bit_test.go complete them for 64-bit systems
//
// This architecture dependent file covers the fact, you can reach a higher value with int and uint
// on 64-bit systems, but you will get a compile error on 32-bit.
// This is why it needs to be tested in an architecture dependent way.
import (
"math"
"testing"
"github.com/ccoveille/go-safecast"
)
func TestToInt32_64bit(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 TestToUint32_64bit(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 TestToInt64_64bit(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 TestToInt_64bit(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
})
})
}
// TestConvert_64bit completes the [TestConvert] tests in conversion_test.go
// it contains the tests that can only works on 64-bit systems
func TestConvert_64bit(t *testing.T) {
t.Run("to uint32", func(t *testing.T) {
for name, tt := range map[string]struct {
input any
want uint32
}{
"positive out of range": {input: uint64(math.MaxUint32 + 1), want: 0},
} {
t.Run(name, func(t *testing.T) {
got, err := safecast.Convert[uint32](tt.input)
assertEqual(t, tt.want, got)
requireErrorIs(t, err, safecast.ErrConversionIssue)
})
}
})
}