How do you write test for variadic functions?
For example I have function:
func max(vals ...int) int {
m := 0
for _, v := range vals {
if v > m {
m = v
}
return m
}
and how to write a few tests for it? I can't put vals ...int
in struct
package main
import (
"testing"
)
func TestMax(t *testing.T) {
var tests = []struct {
vals ...int
want int
}{
{[]int{1,2,3}, 3},
{[]int{-1,0}, -1},
{[]int{0}, 0},
}
for _, test := range tests {
if got := max(test.vals); got !=test.want {
t.Errorf("max(%d) = %d", test.vals, got)
}
}
}
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.