1 // Copyright 2021 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
14 // TODO: replace with slices.Equal when slices is in GOROOT.
15 func slicesEqual[E comparable](s1, s2 []E) bool {
16 if len(s1) != len(s2) {
27 var m1 = map[int]int{1: 2, 2: 4, 4: 8, 8: 16}
28 var m2 = map[int]string{1: "2", 2: "4", 4: "8", 8: "16"}
30 func TestKeys(t *testing.T) {
31 want := []int{1, 2, 4, 8}
35 if !slicesEqual(got1, want) {
36 t.Errorf("Keys(%v) = %v, want %v", m1, got1, want)
41 if !slicesEqual(got2, want) {
42 t.Errorf("Keys(%v) = %v, want %v", m2, got2, want)
46 func TestValues(t *testing.T) {
48 want1 := []int{2, 4, 8, 16}
50 if !slicesEqual(got1, want1) {
51 t.Errorf("Values(%v) = %v, want %v", m1, got1, want1)
55 want2 := []string{"16", "2", "4", "8"}
57 if !slicesEqual(got2, want2) {
58 t.Errorf("Values(%v) = %v, want %v", m2, got2, want2)
62 func TestEqual(t *testing.T) {
64 t.Errorf("Equal(%v, %v) = false, want true", m1, m1)
66 if Equal(m1, (map[int]int)(nil)) {
67 t.Errorf("Equal(%v, nil) = true, want false", m1)
69 if Equal((map[int]int)(nil), m1) {
70 t.Errorf("Equal(nil, %v) = true, want false", m1)
72 if !Equal[map[int]int, map[int]int](nil, nil) {
73 t.Error("Equal(nil, nil) = false, want true")
75 if ms := map[int]int{1: 2}; Equal(m1, ms) {
76 t.Errorf("Equal(%v, %v) = true, want false", m1, ms)
79 // Comparing NaN for equality is expected to fail.
80 mf := map[int]float64{1: 0, 2: math.NaN()}
82 t.Errorf("Equal(%v, %v) = true, want false", mf, mf)
86 // equal is simply ==.
87 func equal[T comparable](v1, v2 T) bool {
91 // equalNaN is like == except that all NaNs are equal.
92 func equalNaN[T comparable](v1, v2 T) bool {
93 isNaN := func(f T) bool { return f != f }
94 return v1 == v2 || (isNaN(v1) && isNaN(v2))
97 // equalStr compares ints and strings.
98 func equalIntStr(v1 int, v2 string) bool {
99 return strconv.Itoa(v1) == v2
102 func TestEqualFunc(t *testing.T) {
103 if !EqualFunc(m1, m1, equal[int]) {
104 t.Errorf("EqualFunc(%v, %v, equal) = false, want true", m1, m1)
106 if EqualFunc(m1, (map[int]int)(nil), equal[int]) {
107 t.Errorf("EqualFunc(%v, nil, equal) = true, want false", m1)
109 if EqualFunc((map[int]int)(nil), m1, equal[int]) {
110 t.Errorf("EqualFunc(nil, %v, equal) = true, want false", m1)
112 if !EqualFunc[map[int]int, map[int]int](nil, nil, equal[int]) {
113 t.Error("EqualFunc(nil, nil, equal) = false, want true")
115 if ms := map[int]int{1: 2}; EqualFunc(m1, ms, equal[int]) {
116 t.Errorf("EqualFunc(%v, %v, equal) = true, want false", m1, ms)
119 // Comparing NaN for equality is expected to fail.
120 mf := map[int]float64{1: 0, 2: math.NaN()}
121 if EqualFunc(mf, mf, equal[float64]) {
122 t.Errorf("EqualFunc(%v, %v, equal) = true, want false", mf, mf)
124 // But it should succeed using equalNaN.
125 if !EqualFunc(mf, mf, equalNaN[float64]) {
126 t.Errorf("EqualFunc(%v, %v, equalNaN) = false, want true", mf, mf)
129 if !EqualFunc(m1, m2, equalIntStr) {
130 t.Errorf("EqualFunc(%v, %v, equalIntStr) = false, want true", m1, m2)
134 func TestClone(t *testing.T) {
137 t.Errorf("Clone(%v) = %v, want %v", m1, mc, m1)
141 t.Errorf("Equal(%v, %v) = true, want false", mc, m1)
145 func TestCloneNil(t *testing.T) {
146 var m1 map[string]int
149 t.Errorf("Clone(%v) = %v, want %v", m1, mc, m1)
153 func TestCopy(t *testing.T) {
157 t.Errorf("Copy(%v, %v) = %v, want %v", m1, m1, mc, m1)
159 Copy(mc, map[int]int{16: 32})
160 want := map[int]int{1: 2, 2: 4, 4: 8, 8: 16, 16: 32}
161 if !Equal(mc, want) {
162 t.Errorf("Copy result = %v, want %v", mc, want)
167 Copy(make(M1), make(M2))
170 func TestDeleteFunc(t *testing.T) {
172 DeleteFunc(mc, func(int, int) bool { return false })
174 t.Errorf("DeleteFunc(%v, true) = %v, want %v", m1, mc, m1)
176 DeleteFunc(mc, func(k, v int) bool { return k > 3 })
177 want := map[int]int{1: 2, 2: 4}
178 if !Equal(mc, want) {
179 t.Errorf("DeleteFunc result = %v, want %v", mc, want)