How to compare Go slices and NaN elements in unit tests?

WBOY
Release: 2024-02-12 13:00:09
forward
961 people have browsed it

如何在单元测试中比较 Go 切片和 NaN 元素?

#php Editor Baicao Comparing Go slices and NaN elements is a common problem in unit tests. When dealing with slices, we often need to compare two slices for equality, but the comparison becomes complicated when the slice contains NaN elements. NaN is a special floating point number, which means it is not a numeric value. In Go, use the IsNaN function in the math package to determine whether a floating point number is NaN. We can implement slice comparison operations by iterating through each element in the slice and using the IsNaN function to determine whether it is NaN.

Question content

I need to compare 2 slices in a unit test, I think the assert package should solve this problem, but it doesn't work with nan:

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

func callme() []float64 {
    return []float64{math.nan()}
}

func testcallme(t *testing.t) {
    assert.equal(t, []float64{math.nan()}, callme())
}
Copy after login

The output is:

Error:          Not equal: 
                            expected: []float64{NaN}
                            actual  : []float64{NaN}
                            
                            Diff:
            Test:           TestCallMe
Copy after login

I know that one of the properties of nan is not equal to itself, but on the other hand I receive the expected result from the function.

I want to know how to solve this problem - get concise unit test assertions and clear output?

As an alternative, I could avoid using assert and call math.isnan on each element of both slices, but this would look extremely verbose in a unit test .

Solution

github.com/google/go-cmp/cmp<通常建议使用 /a> package for more complex comparisons. cmpopts.equatenans can be used to easily compare data structures that may contain nan.

package calc_test

import (
    "math"
    "testing"

    "github.com/google/go-cmp/cmp"
    "github.com/google/go-cmp/cmp/cmpopts"
)

func calc(n float64) []float64 {
    return []float64{n, math.NaN()}
}

func TestCalc(t *testing.T) {
    want := []float64{1, math.NaN()}

    got := calc(1)  // PASS.
    if !cmp.Equal(got, want, cmpopts.EquateNaNs()) {
        t.Errorf("calc: got = %v; want = %v", got, want)
    }

    got = calc(2)   // FAIL with differences.
    if diff := cmp.Diff(want, got, cmpopts.EquateNaNs()); diff != "" {
        t.Errorf("calc: diff (-want +got) = \n%s", diff)
    }
}
Copy after login

The above is the detailed content of How to compare Go slices and NaN elements in unit tests?. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!