diff --git a/pkg/util/common/util_test.go b/pkg/util/common/util_test.go index a143ff591..0c7e53d39 100644 --- a/pkg/util/common/util_test.go +++ b/pkg/util/common/util_test.go @@ -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) + } + }) } }