Created
May 20, 2013 17:37
-
-
Save mwarkentin/5613822 to your computer and use it in GitHub Desktop.
Go test failure
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 math | |
import m "math" | |
// Finds the minimum value in a slice of numbers | |
func Min(xs []float64) float64 { | |
if len(xs) == 0 { | |
return m.NaN() | |
} | |
min := xs[0] | |
for _, v := range xs { | |
if v < min { | |
min = v | |
} | |
} | |
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
package math | |
import m "math" | |
import "testing" | |
type testpair struct { | |
values []float64 | |
average float64 | |
} | |
var minTests = []testpair{ | |
{ []float64{1,2}, 1 }, | |
{ []float64{1,1,1,1,1,1}, 1 }, | |
{ []float64{-1,1}, -1 }, | |
{ []float64{}, m.NaN() }, | |
} | |
func TestMin(t *testing.T) { | |
for _, pair := range minTests { | |
v := Min(pair.values) | |
if v != pair.average { | |
t.Error( | |
"For", pair.values, | |
"expected", pair.average, | |
"got", v, | |
) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Splitting it out like this worked: