From 1feffc8a114ea7355825d2d8c2560830639e7483 Mon Sep 17 00:00:00 2001 From: Evan Simkowitz Date: Tue, 12 Dec 2023 20:36:16 -0800 Subject: [PATCH] address comment, clean up tests --- wavesrv/pkg/utilfn/utilfn.go | 2 +- wavesrv/pkg/utilfn/utilfn_test.go | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/wavesrv/pkg/utilfn/utilfn.go b/wavesrv/pkg/utilfn/utilfn.go index 5c870ecfe..8df22fe7a 100644 --- a/wavesrv/pkg/utilfn/utilfn.go +++ b/wavesrv/pkg/utilfn/utilfn.go @@ -221,7 +221,7 @@ func AddInt(left, right int) (int, error) { return 0, ErrOverflow } } else { - if left < math.MaxInt-right { + if left < math.MinInt-right { return 0, ErrOverflow } } diff --git a/wavesrv/pkg/utilfn/utilfn_test.go b/wavesrv/pkg/utilfn/utilfn_test.go index 1faf40bf6..a65b46067 100644 --- a/wavesrv/pkg/utilfn/utilfn_test.go +++ b/wavesrv/pkg/utilfn/utilfn_test.go @@ -68,13 +68,18 @@ func testArithmetic(t *testing.T, fn func() (int, error), shouldError bool, expe } } -func testAddInt(t *testing.T, a int, b int, shouldError bool, expected int) { +func testAddInt(t *testing.T, shouldError bool, expected int, a int, b int) { testArithmetic(t, func() (int, error) { return AddInt(a, b) }, shouldError, expected) } func TestAddInt(t *testing.T) { - testAddInt(t, 1, 2, false, 3) - testAddInt(t, 1, math.MaxInt, true, 0) + testAddInt(t, false, 3, 1, 2) + testAddInt(t, true, 0, 1, math.MaxInt) + testAddInt(t, true, 0, math.MinInt, -1) + testAddInt(t, false, math.MaxInt-1, math.MaxInt, -1) + testAddInt(t, false, math.MinInt+1, math.MinInt, 1) + testAddInt(t, false, math.MaxInt, math.MaxInt, 0) + testAddInt(t, true, 0, math.MinInt, -1) } func testAddIntSlice(t *testing.T, shouldError bool, expected int, vals ...int) { @@ -89,4 +94,10 @@ func TestAddIntSlice(t *testing.T) { testAddIntSlice(t, true, 0, 1, math.MaxInt) testAddIntSlice(t, true, 0, 1, 2, math.MaxInt) testAddIntSlice(t, true, 0, math.MaxInt, 2, 1) + testAddIntSlice(t, false, math.MaxInt, 0, 0, math.MaxInt) + testAddIntSlice(t, true, 0, math.MinInt, -1) + testAddIntSlice(t, false, math.MaxInt, math.MaxInt-3, 1, 2) + testAddIntSlice(t, true, 0, math.MaxInt-2, 1, 2) + testAddIntSlice(t, false, math.MinInt, math.MinInt+3, -1, -2) + testAddIntSlice(t, true, 0, math.MinInt+2, -1, -2) }