mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
reflect: document DeepEqual, fix case of maps with NaN keys
The meaning of DeepEqual has never been specified. Do that. Also fix bug involving maps with NaN keys. Except for the map bug fix, there should be no semantic changes here. Fixes #12025. Change-Id: Ied562cf543a22ec645d42bdb9b41d451c16b1f21 Reviewed-on: https://go-review.googlesource.com/17450 Reviewed-by: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org>
This commit is contained in:
parent
617c93ce74
commit
a77182f47f
2 changed files with 70 additions and 18 deletions
|
|
@ -10,6 +10,7 @@ import (
|
|||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"math/rand"
|
||||
"os"
|
||||
. "reflect"
|
||||
|
|
@ -647,6 +648,8 @@ var (
|
|||
fn3 = func() { fn1() } // Not nil.
|
||||
)
|
||||
|
||||
type self struct{}
|
||||
|
||||
var deepEqualTests = []DeepEqualTest{
|
||||
// Equalities
|
||||
{nil, nil, true},
|
||||
|
|
@ -681,6 +684,13 @@ var deepEqualTests = []DeepEqualTest{
|
|||
{fn1, fn3, false},
|
||||
{fn3, fn3, false},
|
||||
{[][]int{{1}}, [][]int{{2}}, false},
|
||||
{math.NaN(), math.NaN(), false},
|
||||
{&[1]float64{math.NaN()}, &[1]float64{math.NaN()}, false},
|
||||
{&[1]float64{math.NaN()}, self{}, true},
|
||||
{[]float64{math.NaN()}, []float64{math.NaN()}, false},
|
||||
{[]float64{math.NaN()}, self{}, true},
|
||||
{map[float64]float64{math.NaN(): 1}, map[float64]float64{1: 2}, false},
|
||||
{map[float64]float64{math.NaN(): 1}, self{}, true},
|
||||
|
||||
// Nil vs empty: not the same.
|
||||
{[]int{}, []int(nil), false},
|
||||
|
|
@ -702,6 +712,9 @@ var deepEqualTests = []DeepEqualTest{
|
|||
|
||||
func TestDeepEqual(t *testing.T) {
|
||||
for _, test := range deepEqualTests {
|
||||
if test.b == (self{}) {
|
||||
test.b = test.a
|
||||
}
|
||||
if r := DeepEqual(test.a, test.b); r != test.eq {
|
||||
t.Errorf("DeepEqual(%v, %v) = %v, want %v", test.a, test.b, r, test.eq)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue