testify icon indicating copy to clipboard operation
testify copied to clipboard

assert two NaN values

Open ghost opened this issue 6 years ago • 5 comments

when i try to assert two NaN values the assert result was

$ go test
--- FAIL: TestNaN (0.00s)
	main_test.go:10: 
			Error Trace:	main_test.go:10
			Error:      	Not equal: 
			            	expected: NaN
			            	actual  : NaN
			Test:       	TestNaN
FAIL
FAIL	command-line-arguments	0.230s

the code that genrate this error is

package test

import (
        "github.com/stretchr/testify/assert"
        "math"
        "testing"
)

func TestNaN(t *testing.T) {
        assert.Equal(t, math.NaN(), math.NaN())
}

because the assert.Equal is based on reflerct.DeepEqual in it documentations written that two floats a,b will be equal if a == b is true. the golang the act according IEEE 754 is defined NaN == NaN as false. so this may need discussion about adding assert checking for NaN values.

ghost avatar Jun 28 '18 17:06 ghost

https://github.com/stretchr/testify/issues/632#issuecomment-404729847

mlkr avatar Aug 24 '18 09:08 mlkr

@yahelmanor2 NaN its not a number, so you can't compare those 2 values. math.NaN() == math.NaN() its false, so the assert Equal output you've got is as per expectations.

devdinu avatar Aug 26 '18 06:08 devdinu

thanks @devdinu,but there is any way I can test that value equal to NaN within the assert package. I know that assert.True(t,math.IsNaN(i)) will work but I think that it will be better if a solution will be exist within the assert package(i can open PR if needed).

ghost avatar Sep 13 '18 18:09 ghost

@yahelmanor2 I think that you should do that :)

bkielbasa avatar Nov 21 '18 15:11 bkielbasa

This NaN comparison logic, though not necessarily this function, would be useful to have when comparing deeply nested structs and slices. For example, if I expected to get a slice [0.0, NaN, 8.0] there is nothing I can do (that I know of) except loop through elements.

andremarianiello avatar Apr 10 '19 21:04 andremarianiello