I got it!

max(test.vals...)

On Wednesday, May 23, 2018 at 4:09:01 PM UTC-7, Alex Dvoretskiy wrote:
>
> 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 golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to