diff --git a/pkg/util/integer/integer.go b/pkg/util/integer/integer.go index 81e28cfbf49..99cde5d3633 100644 --- a/pkg/util/integer/integer.go +++ b/pkg/util/integer/integer.go @@ -30,6 +30,20 @@ func IntMin(a, b int) int { return a } +func Int32Max(a, b int32) int32 { + if b > a { + return b + } + return a +} + +func Int32Min(a, b int32) int32 { + if b < a { + return b + } + return a +} + func Int64Max(a, b int64) int64 { if b > a { return b diff --git a/pkg/util/integer/integer_test.go b/pkg/util/integer/integer_test.go index 2000a457058..93ae44c4aae 100644 --- a/pkg/util/integer/integer_test.go +++ b/pkg/util/integer/integer_test.go @@ -80,6 +80,68 @@ func TestIntMin(t *testing.T) { } } +func TestInt32Max(t *testing.T) { + tests := []struct { + nums []int32 + expectedMax int32 + }{ + { + nums: []int32{-1, 0}, + expectedMax: 0, + }, + { + nums: []int32{-1, -2}, + expectedMax: -1, + }, + { + nums: []int32{0, 1}, + expectedMax: 1, + }, + { + nums: []int32{1, 2}, + expectedMax: 2, + }, + } + + for i, test := range tests { + t.Logf("executing scenario %d", i) + if max := Int32Max(test.nums[0], test.nums[1]); max != test.expectedMax { + t.Errorf("expected %v, got %v", test.expectedMax, max) + } + } +} + +func TestInt32Min(t *testing.T) { + tests := []struct { + nums []int32 + expectedMin int32 + }{ + { + nums: []int32{-1, 0}, + expectedMin: -1, + }, + { + nums: []int32{-1, -2}, + expectedMin: -2, + }, + { + nums: []int32{0, 1}, + expectedMin: 0, + }, + { + nums: []int32{1, 2}, + expectedMin: 1, + }, + } + + for i, test := range tests { + t.Logf("executing scenario %d", i) + if min := Int32Min(test.nums[0], test.nums[1]); min != test.expectedMin { + t.Errorf("expected %v, got %v", test.expectedMin, min) + } + } +} + func TestInt64Max(t *testing.T) { tests := []struct { nums []int64