Merge pull request #134 from lazywei/master

Add InDeltaSlice.
pull/156/head
Davide D'Agostino 2015-04-18 12:10:15 -07:00
commit 3b361f7ebb
2 changed files with 79 additions and 0 deletions

View File

@ -667,6 +667,27 @@ func InDelta(t TestingT, expected, actual interface{}, delta float64, msgAndArgs
return true
}
// InDeltaSlice is the same as InDelta, except it compares two slices.
func InDeltaSlice(t TestingT, expected, actual interface{}, delta float64, msgAndArgs ...interface{}) bool {
if expected == nil || actual == nil ||
reflect.TypeOf(actual).Kind() != reflect.Slice ||
reflect.TypeOf(expected).Kind() != reflect.Slice {
return Fail(t, fmt.Sprintf("Parameters must be slice"), msgAndArgs...)
}
actualSlice := reflect.ValueOf(actual)
expectedSlice := reflect.ValueOf(expected)
for i := 0; i < actualSlice.Len(); i++ {
result := InDelta(t, actualSlice.Index(i).Interface(), expectedSlice.Index(i).Interface(), delta)
if !result {
return result
}
}
return true
}
// min(|expected|, |actual|) * epsilon
func calcEpsilonDelta(expected, actual interface{}, epsilon float64) float64 {
af, aok := toFloat(expected)
@ -701,6 +722,27 @@ func InEpsilon(t TestingT, expected, actual interface{}, epsilon float64, msgAnd
return InDelta(t, expected, actual, delta, msgAndArgs...)
}
// InEpsilonSlice is the same as InEpsilon, except it compares two slices.
func InEpsilonSlice(t TestingT, expected, actual interface{}, delta float64, msgAndArgs ...interface{}) bool {
if expected == nil || actual == nil ||
reflect.TypeOf(actual).Kind() != reflect.Slice ||
reflect.TypeOf(expected).Kind() != reflect.Slice {
return Fail(t, fmt.Sprintf("Parameters must be slice"), msgAndArgs...)
}
actualSlice := reflect.ValueOf(actual)
expectedSlice := reflect.ValueOf(expected)
for i := 0; i < actualSlice.Len(); i++ {
result := InEpsilon(t, actualSlice.Index(i).Interface(), expectedSlice.Index(i).Interface(), delta)
if !result {
return result
}
}
return true
}
/*
Errors
*/

View File

@ -677,6 +677,27 @@ func TestInDelta(t *testing.T) {
}
}
func TestInDeltaSlice(t *testing.T) {
mockT := new(testing.T)
True(t, InDeltaSlice(mockT,
[]float64{1.001, 0.999},
[]float64{1, 1},
0.1), "{1.001, 0.009} is element-wise close to {1, 1} in delta=0.1")
True(t, InDeltaSlice(mockT,
[]float64{1, 2},
[]float64{0, 3},
1), "{1, 2} is element-wise close to {0, 3} in delta=1")
False(t, InDeltaSlice(mockT,
[]float64{1, 2},
[]float64{0, 3},
0.1), "{1, 2} is not element-wise close to {0, 3} in delta=0.1")
False(t, InDeltaSlice(mockT, "", nil, 1), "Expected non numeral slices to fail")
}
func TestInEpsilon(t *testing.T) {
mockT := new(testing.T)
@ -716,6 +737,22 @@ func TestInEpsilon(t *testing.T) {
}
func TestInEpsilonSlice(t *testing.T) {
mockT := new(testing.T)
True(t, InEpsilonSlice(mockT,
[]float64{2.2, 2.0},
[]float64{2.1, 2.1},
0.06), "{2.2, 2.0} is element-wise close to {2.1, 2.1} in espilon=0.06")
False(t, InEpsilonSlice(mockT,
[]float64{2.2, 2.0},
[]float64{2.1, 2.1},
0.04), "{2.2, 2.0} is not element-wise close to {2.1, 2.1} in espilon=0.04")
False(t, InEpsilonSlice(mockT, "", nil, 1), "Expected non numeral slices to fail")
}
func TestRegexp(t *testing.T) {
mockT := new(testing.T)