Skip to content

Instantly share code, notes, and snippets.

@mwarkentin
Created May 20, 2013 17:37
Show Gist options
  • Save mwarkentin/5613822 to your computer and use it in GitHub Desktop.
Save mwarkentin/5613822 to your computer and use it in GitHub Desktop.
Go test failure
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
}
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,
)
}
}
}
@mwarkentin
Copy link
Author

Here's the output when I run go test:

mwarkentin@Michaels-iMac math  $ go test
--- FAIL: TestMin (0.00 seconds)
    math_test.go:46: For [] expected NaN got NaN
FAIL
exit status 1
FAIL    golang-book/chapter-11/math 0.027s

@mwarkentin
Copy link
Author

Splitting it out like this worked:

var emptySlice = []float64{}
x := Min(emptySlice)
if math.IsNaN(x) != true {
    t.Error("For", emptySlice, "expected", math.NaN(), "got", x)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment