Skip to content

Commit

Permalink
Add 'TestSubInt64'.
Browse files Browse the repository at this point in the history
  • Loading branch information
nickeskov committed Dec 28, 2024
1 parent 4001464 commit c9023f2
Showing 1 changed file with 64 additions and 20 deletions.
84 changes: 64 additions & 20 deletions pkg/util/common/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,77 @@ import (
)

func TestAddInt64(t *testing.T) {
zero := int64(0)
max := int64(math.MaxInt64)
min := int64(math.MinInt64)
one := int64(1)
for _, test := range []struct {
const (
zero = int64(0)
maxInt64 = int64(math.MaxInt64)
minInt64 = int64(math.MinInt64)
one = int64(1)
)

tests := []struct {
x, y int64
err bool
r int64
}{
{zero, max, false, max},
{one, max, true, zero},
{-one, -min, true, zero},
{zero, min, false, min},
{one, -max, false, -int64(math.MaxInt64 - 1)},
{-one, -max, false, min},
{-(one * 2), -max, true, zero},
{zero, maxInt64, false, maxInt64},
{one, maxInt64, true, zero},
{-one, minInt64, true, zero},
{zero, minInt64, false, minInt64},
{one, -maxInt64, false, -int64(math.MaxInt64 - 1)},
{-one, -maxInt64, false, minInt64},
{-(one * 2), -maxInt64, true, zero},
{one, one, false, int64(2)},
{one, -one, false, zero},
{-one, -one, false, int64(-2)},
} {
r, err := AddInt(test.x, test.y)
if test.err {
assert.Error(t, err, "AddInt did not fail with arguments causing an overflow")
} else {
require.NoError(t, err)
assert.Equal(t, test.r, r)
}
}
for i, test := range tests {
t.Run(fmt.Sprintf("%d", i+1), func(t *testing.T) {
r, err := AddInt(test.x, test.y)
if test.err {
assert.Error(t, err, "AddInt did not fail with arguments causing an overflow")
} else {
assert.NoError(t, err)
assert.Equal(t, test.r, r)
}
})
}
}

func TestSubInt64(t *testing.T) {
const (
zero = int64(0)
maxInt64 = int64(math.MaxInt64)
minInt64 = int64(math.MinInt64)
one = int64(1)
)

tests := []struct {
x, y int64
err bool
r int64
}{
{zero, maxInt64, false, -maxInt64},
{one, maxInt64, false, -int64(math.MaxInt64 - 1)},
{-one, minInt64, false, int64(math.MaxInt64)},
{zero, minInt64, true, zero}, // Overflow
{one, -maxInt64, true, zero}, // Overflow
{-(one * 2), maxInt64, true, zero}, // Underflow
{-one, -maxInt64, false, int64(math.MaxInt64 - 1)},
{-(one * 2), -maxInt64, false, int64(math.MaxInt64 - 2)},
{one, one, false, zero},
{one, -one, false, int64(2)},
{-one, -one, false, zero},
}
for i, test := range tests {
t.Run(fmt.Sprintf("%d", i+1), func(t *testing.T) {
r, err := SubInt(test.x, test.y)
if test.err {
assert.Error(t, err, "SubInt did not fail with arguments causing an overflow")
} else {
assert.NoError(t, err)
assert.Equal(t, test.r, r)
}
})
}
}

Expand Down

0 comments on commit c9023f2

Please sign in to comment.