-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Jordan Krage <[email protected]> Co-authored-by: Dmytro Haidashenko <[email protected]>
- Loading branch information
Showing
2 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package mathutil | ||
|
||
import "golang.org/x/exp/constraints" | ||
|
||
func Max[V constraints.Ordered](first V, vals ...V) V { | ||
max := first | ||
for _, v := range vals { | ||
if v > max { | ||
max = v | ||
} | ||
} | ||
return max | ||
} | ||
|
||
func Min[V constraints.Ordered](first V, vals ...V) V { | ||
min := first | ||
for _, v := range vals { | ||
if v < min { | ||
min = v | ||
} | ||
} | ||
return min | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package mathutil | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMax(t *testing.T) { | ||
// Happy path | ||
assert.Equal(t, 3, Max(3, 2, 1)) | ||
// Single element | ||
assert.Equal(t, 3, Max(3)) | ||
// Signed | ||
assert.Equal(t, -1, Max(-2, -1)) | ||
// Uint64 | ||
assert.Equal(t, uint64(2), Max(uint64(0), uint64(2))) | ||
// String | ||
assert.Equal(t, "c", Max("a", []string{"b", "c"}...)) | ||
} | ||
|
||
func TestMin(t *testing.T) { | ||
// Happy path | ||
assert.Equal(t, 1, Min(3, 2, 1)) | ||
// Single element | ||
assert.Equal(t, 3, Min(3)) | ||
// Signed | ||
assert.Equal(t, -2, Min(-2, -1)) | ||
// Uint64 | ||
assert.Equal(t, uint64(0), Min(uint64(0), uint64(2))) | ||
// String | ||
assert.Equal(t, "a", Min("a", []string{"b", "c"}...)) | ||
} |