Files
rands/ints_test.go
Jim Myhrberg b59d421322 chore(test): enable parallel test execution
Seems to cut overall total test time down to one third the time.
2021-12-16 20:16:03 +00:00

158 lines
2.9 KiB
Go

package rands
import (
"testing"
"github.com/stretchr/testify/assert"
)
var testIntCases = []struct {
name string
max int
errIs error
errStr string
}{
{
name: "n=-2394345",
max: -2394345,
errIs: ErrInvalidMaxInt,
errStr: "rands: max cannot be less than 1",
},
{
name: "n=-409600",
max: -409600,
errIs: ErrInvalidMaxInt,
errStr: "rands: max cannot be less than 1",
},
{
name: "n=-1024",
max: -1024,
errIs: ErrInvalidMaxInt,
errStr: "rands: max cannot be less than 1",
},
{
name: "n=-128",
max: -128,
errIs: ErrInvalidMaxInt,
errStr: "rands: max cannot be less than 1",
},
{
name: "n=-32",
max: -32,
errIs: ErrInvalidMaxInt,
errStr: "rands: max cannot be less than 1",
},
{
name: "n=-16",
max: -16,
errIs: ErrInvalidMaxInt,
errStr: "rands: max cannot be less than 1",
},
{
name: "n=-8",
max: -8,
errIs: ErrInvalidMaxInt,
errStr: "rands: max cannot be less than 1",
},
{
name: "n=-7",
max: -7,
errIs: ErrInvalidMaxInt,
errStr: "rands: max cannot be less than 1",
},
{
name: "n=-2",
max: -2,
errIs: ErrInvalidMaxInt,
errStr: "rands: max cannot be less than 1",
},
{
name: "n=-1",
max: -1,
errIs: ErrInvalidMaxInt,
errStr: "rands: max cannot be less than 1",
},
{
name: "n=0",
max: 0,
errIs: ErrInvalidMaxInt,
errStr: "rands: max cannot be less than 1",
},
{name: "n=1", max: 1},
{name: "n=2", max: 2},
{name: "n=7", max: 7},
{name: "n=8", max: 8},
{name: "n=16", max: 16},
{name: "n=32", max: 32},
{name: "n=128", max: 128},
{name: "n=1024", max: 1024},
{name: "n=409600", max: 409600},
{name: "n=2394345", max: 2394345},
}
func TestInt(t *testing.T) {
t.Parallel()
for _, tt := range testIntCases {
t.Run(tt.name, func(t *testing.T) {
got, err := Int(tt.max)
if tt.errIs == nil || tt.errStr == "" {
assert.GreaterOrEqual(t, got, 0)
assert.LessOrEqual(t, got, tt.max)
}
if tt.errIs != nil {
assert.ErrorIs(t, err, tt.errIs)
}
if tt.errStr != "" {
assert.EqualError(t, err, tt.errStr)
}
})
}
}
func BenchmarkInt(b *testing.B) {
for _, tt := range testIntCases {
b.Run(tt.name, func(b *testing.B) {
for n := 0; n < b.N; n++ {
_, _ = Int(tt.max)
}
})
}
}
func TestInt64(t *testing.T) {
t.Parallel()
for _, tt := range testIntCases {
t.Run(tt.name, func(t *testing.T) {
got, err := Int64(int64(tt.max))
if tt.errIs == nil || tt.errStr == "" {
assert.GreaterOrEqual(t, got, int64(0))
assert.LessOrEqual(t, got, int64(tt.max))
}
if tt.errIs != nil {
assert.ErrorIs(t, err, tt.errIs)
}
if tt.errStr != "" {
assert.EqualError(t, err, tt.errStr)
}
})
}
}
func BenchmarkInt64(b *testing.B) {
for _, tt := range testIntCases {
b.Run(tt.name, func(b *testing.B) {
for n := 0; n < b.N; n++ {
_, _ = Int64(int64(tt.max))
}
})
}
}