Skip to content

Commit

Permalink
types: migrate test-infra to testify for overflow_test.go (#28071)
Browse files Browse the repository at this point in the history
  • Loading branch information
unconsolable authored Sep 16, 2021
1 parent f0339ad commit 35ef52c
Showing 1 changed file with 84 additions and 85 deletions.
169 changes: 84 additions & 85 deletions types/overflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,15 @@ package types

import (
"math"
"testing"
"time"

. "github.com/pingcap/check"
"github.com/pingcap/tidb/util/testleak"
"github.com/stretchr/testify/require"
)

var _ = Suite(&testOverflowSuite{})
func TestAdd(t *testing.T) {
t.Parallel()

type testOverflowSuite struct {
}

func (s *testOverflowSuite) TestAdd(c *C) {
defer testleak.AfterTest(c)()
tblUint64 := []struct {
lsh uint64
rsh uint64
Expand All @@ -40,12 +36,12 @@ func (s *testOverflowSuite) TestAdd(c *C) {
{1, 1, 2, false},
}

for _, t := range tblUint64 {
ret, err := AddUint64(t.lsh, t.rsh)
if t.overflow {
c.Assert(err, NotNil)
for _, tt := range tblUint64 {
ret, err := AddUint64(tt.lsh, tt.rsh)
if tt.overflow {
require.Error(t, err)
} else {
c.Assert(ret, Equals, t.ret)
require.Equal(t, tt.ret, ret)
}
}

Expand All @@ -64,18 +60,18 @@ func (s *testOverflowSuite) TestAdd(c *C) {
{1, -1, 0, false},
}

for _, t := range tblInt64 {
ret, err := AddInt64(t.lsh, t.rsh)
if t.overflow {
c.Assert(err, NotNil)
for _, tt := range tblInt64 {
ret, err := AddInt64(tt.lsh, tt.rsh)
if tt.overflow {
require.Error(t, err)
} else {
c.Assert(ret, Equals, t.ret)
require.Equal(t, tt.ret, ret)
}
ret2, err := AddDuration(time.Duration(t.lsh), time.Duration(t.rsh))
if t.overflow {
c.Assert(err, NotNil)
ret2, err := AddDuration(time.Duration(tt.lsh), time.Duration(tt.rsh))
if tt.overflow {
require.Error(t, err)
} else {
c.Assert(ret2, Equals, time.Duration(t.ret))
require.Equal(t, time.Duration(tt.ret), ret2)
}
}

Expand All @@ -93,18 +89,19 @@ func (s *testOverflowSuite) TestAdd(c *C) {
{1, 1, 2, false},
}

for _, t := range tblInt {
ret, err := AddInteger(t.lsh, t.rsh)
if t.overflow {
c.Assert(err, NotNil)
for _, tt := range tblInt {
ret, err := AddInteger(tt.lsh, tt.rsh)
if tt.overflow {
require.Error(t, err)
} else {
c.Assert(ret, Equals, t.ret)
require.Equal(t, tt.ret, ret)
}
}
}

func (s *testOverflowSuite) TestSub(c *C) {
defer testleak.AfterTest(c)()
func TestSub(t *testing.T) {
t.Parallel()

tblUint64 := []struct {
lsh uint64
rsh uint64
Expand All @@ -119,12 +116,12 @@ func (s *testOverflowSuite) TestSub(c *C) {
{1, 1, 0, false},
}

for _, t := range tblUint64 {
ret, err := SubUint64(t.lsh, t.rsh)
if t.overflow {
c.Assert(err, NotNil)
for _, tt := range tblUint64 {
ret, err := SubUint64(tt.lsh, tt.rsh)
if tt.overflow {
require.Error(t, err)
} else {
c.Assert(ret, Equals, t.ret)
require.Equal(t, tt.ret, ret)
}
}

Expand All @@ -145,12 +142,12 @@ func (s *testOverflowSuite) TestSub(c *C) {
{1, 1, 0, false},
}

for _, t := range tblInt64 {
ret, err := SubInt64(t.lsh, t.rsh)
if t.overflow {
c.Assert(err, NotNil)
for _, tt := range tblInt64 {
ret, err := SubInt64(tt.lsh, tt.rsh)
if tt.overflow {
require.Error(t, err)
} else {
c.Assert(ret, Equals, t.ret)
require.Equal(t, tt.ret, ret)
}
}

Expand All @@ -169,12 +166,12 @@ func (s *testOverflowSuite) TestSub(c *C) {
{1, 1, 0, false},
}

for _, t := range tblInt {
ret, err := SubUintWithInt(t.lsh, t.rsh)
if t.overflow {
c.Assert(err, NotNil)
for _, tt := range tblInt {
ret, err := SubUintWithInt(tt.lsh, tt.rsh)
if tt.overflow {
require.Error(t, err)
} else {
c.Assert(ret, Equals, t.ret)
require.Equal(t, tt.ret, ret)
}
}

Expand All @@ -192,18 +189,19 @@ func (s *testOverflowSuite) TestSub(c *C) {
{1, 1, 0, false},
}

for _, t := range tblInt2 {
ret, err := SubIntWithUint(t.lsh, t.rsh)
if t.overflow {
c.Assert(err, NotNil)
for _, tt := range tblInt2 {
ret, err := SubIntWithUint(tt.lsh, tt.rsh)
if tt.overflow {
require.Error(t, err)
} else {
c.Assert(ret, Equals, t.ret)
require.Equal(t, tt.ret, ret)
}
}
}

func (s *testOverflowSuite) TestMul(c *C) {
defer testleak.AfterTest(c)()
func TestMul(t *testing.T) {
t.Parallel()

tblUint64 := []struct {
lsh uint64
rsh uint64
Expand All @@ -216,12 +214,12 @@ func (s *testOverflowSuite) TestMul(c *C) {
{1, 1, 1, false},
}

for _, t := range tblUint64 {
ret, err := MulUint64(t.lsh, t.rsh)
if t.overflow {
c.Assert(err, NotNil)
for _, tt := range tblUint64 {
ret, err := MulUint64(tt.lsh, tt.rsh)
if tt.overflow {
require.Error(t, err)
} else {
c.Assert(ret, Equals, t.ret)
require.Equal(t, tt.ret, ret)
}
}

Expand All @@ -243,12 +241,12 @@ func (s *testOverflowSuite) TestMul(c *C) {
{1, 1, 1, false},
}

for _, t := range tblInt64 {
ret, err := MulInt64(t.lsh, t.rsh)
if t.overflow {
c.Assert(err, NotNil)
for _, tt := range tblInt64 {
ret, err := MulInt64(tt.lsh, tt.rsh)
if tt.overflow {
require.Error(t, err)
} else {
c.Assert(ret, Equals, t.ret)
require.Equal(t, tt.ret, ret)
}
}

Expand All @@ -266,18 +264,19 @@ func (s *testOverflowSuite) TestMul(c *C) {
{1, 1, 1, false},
}

for _, t := range tblInt {
ret, err := MulInteger(t.lsh, t.rsh)
if t.overflow {
c.Assert(err, NotNil)
for _, tt := range tblInt {
ret, err := MulInteger(tt.lsh, tt.rsh)
if tt.overflow {
require.Error(t, err)
} else {
c.Assert(ret, Equals, t.ret)
require.Equal(t, tt.ret, ret)
}
}
}

func (s *testOverflowSuite) TestDiv(c *C) {
defer testleak.AfterTest(c)()
func TestDiv(t *testing.T) {
t.Parallel()

tblInt64 := []struct {
lsh int64
rsh int64
Expand All @@ -294,12 +293,12 @@ func (s *testOverflowSuite) TestDiv(c *C) {
{math.MinInt64, 2, math.MinInt64 / 2, false},
}

for _, t := range tblInt64 {
ret, err := DivInt64(t.lsh, t.rsh)
if t.overflow {
c.Assert(err, NotNil)
for _, tt := range tblInt64 {
ret, err := DivInt64(tt.lsh, tt.rsh)
if tt.overflow {
require.Error(t, err)
} else {
c.Assert(ret, Equals, t.ret)
require.Equal(t, tt.ret, ret)
}
}

Expand All @@ -316,12 +315,12 @@ func (s *testOverflowSuite) TestDiv(c *C) {
{100, 20, 5, false},
}

for _, t := range tblInt {
ret, err := DivUintWithInt(t.lsh, t.rsh)
if t.overflow {
c.Assert(err, NotNil)
for _, tt := range tblInt {
ret, err := DivUintWithInt(tt.lsh, tt.rsh)
if tt.overflow {
require.Error(t, err)
} else {
c.Assert(ret, Equals, t.ret)
require.Equal(t, tt.ret, ret)
}
}

Expand All @@ -332,18 +331,18 @@ func (s *testOverflowSuite) TestDiv(c *C) {
overflow bool
err string
}{
{math.MinInt64, math.MaxInt64, 0, true, "*BIGINT UNSIGNED value is out of range in '\\(-9223372036854775808, 9223372036854775807\\)'"},
{math.MinInt64, math.MaxInt64, 0, true, "^*BIGINT UNSIGNED value is out of range in '\\(-9223372036854775808, 9223372036854775807\\)'$"},
{0, 1, 0, false, ""},
{-1, math.MaxInt64, 0, false, ""},
}

for _, t := range tblInt2 {
ret, err := DivIntWithUint(t.lsh, t.rsh)
if t.overflow {
c.Assert(err, NotNil)
c.Assert(err, ErrorMatches, t.err)
for _, tt := range tblInt2 {
ret, err := DivIntWithUint(tt.lsh, tt.rsh)
if tt.overflow {
require.Error(t, err)
require.Regexp(t, tt.err, err.Error())
} else {
c.Assert(ret, Equals, t.ret)
require.Equal(t, tt.ret, ret)
}
}
}

0 comments on commit 35ef52c

Please sign in to comment.