Last active
April 14, 2022 11:37
-
-
Save jerryan999/01bd9571bfcd526d7908d0771d330cbb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
func MaxInt(args ...int) int { | |
if len(args) == 0 { | |
return *new(int) | |
} | |
max := args[0] | |
for _, arg := range args[1:] { | |
if arg > max { | |
max = arg | |
} | |
} | |
return max | |
} | |
func MaxFloat(args ...float64) float64 { | |
if len(args) == 0 { | |
return *new(float64) | |
} | |
max := args[0] | |
for _, arg := range args[1:] { | |
if arg > max { | |
max = arg | |
} | |
} | |
return max | |
} |
This file contains hidden or 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
package main | |
import ( | |
"fmt" | |
"golang.org/x/exp/constraints" | |
) | |
func Max[T constraints.Ordered](args ...T) T { | |
if len(args) == 0 { | |
return *new(T) | |
} | |
max := args[0] | |
for _, arg := range args[1:] { | |
if arg > max { | |
max = arg | |
} | |
} | |
return max | |
} | |
func Min[T constraints.Ordered](args ...T) T { | |
if len(args) == 0 { | |
return *new(T) | |
} | |
min := args[0] | |
for _, arg := range args[1:] { | |
if arg < min { | |
min = arg | |
} | |
} | |
return min | |
} |
This file contains hidden or 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
func TestMinInt(t *testing.T) { | |
var tests = []struct { | |
args []int | |
want int | |
}{ | |
{[]int{}, 0}, | |
{[]int{1}, 1}, | |
{[]int{1, 3, 2}, 1}, | |
} | |
for _, test := range tests { | |
if got := Min(test.args...); got != test.want { | |
t.Errorf("Min(%v) = %v, want %v", test.args, got, test.want) | |
} | |
} | |
} | |
func TestMaxFloat64(t *testing.T) { | |
var tests = []struct { | |
args []float64 | |
want float64 | |
}{ | |
{[]float64{}, 0}, | |
{[]float64{1.0}, 1.0}, | |
{[]float64{1, 3.2, -2}, 3.2}, | |
} | |
for _, test := range tests { | |
if got := Max(test.args...); got != test.want { | |
t.Errorf("Max(%v) = %v, want %v", test.args, got, test.want) | |
} | |
} | |
} | |
func TestMaxString(t *testing.T) { | |
var tests = []struct { | |
args []string | |
want string | |
}{ | |
{[]string{}, ""}, | |
{[]string{"a"}, "a"}, | |
{[]string{"a", "b", "c"}, "c"}, | |
} | |
for _, test := range tests { | |
if got := Max(test.args...); got != test.want { | |
t.Errorf("Max(%v) = %v, want %v", test.args, got, test.want) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment