]> Cypherpunks.ru repositories - gostls13.git/blob - src/maps/maps_test.go
maps: new package
[gostls13.git] / src / maps / maps_test.go
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.
4
5 package maps
6
7 import (
8         "math"
9         "sort"
10         "strconv"
11         "testing"
12 )
13
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) {
17                 return false
18         }
19         for i := range s1 {
20                 if s1[i] != s2[i] {
21                         return false
22                 }
23         }
24         return true
25 }
26
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"}
29
30 func TestKeys(t *testing.T) {
31         want := []int{1, 2, 4, 8}
32
33         got1 := Keys(m1)
34         sort.Ints(got1)
35         if !slicesEqual(got1, want) {
36                 t.Errorf("Keys(%v) = %v, want %v", m1, got1, want)
37         }
38
39         got2 := Keys(m2)
40         sort.Ints(got2)
41         if !slicesEqual(got2, want) {
42                 t.Errorf("Keys(%v) = %v, want %v", m2, got2, want)
43         }
44 }
45
46 func TestValues(t *testing.T) {
47         got1 := Values(m1)
48         want1 := []int{2, 4, 8, 16}
49         sort.Ints(got1)
50         if !slicesEqual(got1, want1) {
51                 t.Errorf("Values(%v) = %v, want %v", m1, got1, want1)
52         }
53
54         got2 := Values(m2)
55         want2 := []string{"16", "2", "4", "8"}
56         sort.Strings(got2)
57         if !slicesEqual(got2, want2) {
58                 t.Errorf("Values(%v) = %v, want %v", m2, got2, want2)
59         }
60 }
61
62 func TestEqual(t *testing.T) {
63         if !Equal(m1, m1) {
64                 t.Errorf("Equal(%v, %v) = false, want true", m1, m1)
65         }
66         if Equal(m1, (map[int]int)(nil)) {
67                 t.Errorf("Equal(%v, nil) = true, want false", m1)
68         }
69         if Equal((map[int]int)(nil), m1) {
70                 t.Errorf("Equal(nil, %v) = true, want false", m1)
71         }
72         if !Equal[map[int]int, map[int]int](nil, nil) {
73                 t.Error("Equal(nil, nil) = false, want true")
74         }
75         if ms := map[int]int{1: 2}; Equal(m1, ms) {
76                 t.Errorf("Equal(%v, %v) = true, want false", m1, ms)
77         }
78
79         // Comparing NaN for equality is expected to fail.
80         mf := map[int]float64{1: 0, 2: math.NaN()}
81         if Equal(mf, mf) {
82                 t.Errorf("Equal(%v, %v) = true, want false", mf, mf)
83         }
84 }
85
86 // equal is simply ==.
87 func equal[T comparable](v1, v2 T) bool {
88         return v1 == v2
89 }
90
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))
95 }
96
97 // equalStr compares ints and strings.
98 func equalIntStr(v1 int, v2 string) bool {
99         return strconv.Itoa(v1) == v2
100 }
101
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)
105         }
106         if EqualFunc(m1, (map[int]int)(nil), equal[int]) {
107                 t.Errorf("EqualFunc(%v, nil, equal) = true, want false", m1)
108         }
109         if EqualFunc((map[int]int)(nil), m1, equal[int]) {
110                 t.Errorf("EqualFunc(nil, %v, equal) = true, want false", m1)
111         }
112         if !EqualFunc[map[int]int, map[int]int](nil, nil, equal[int]) {
113                 t.Error("EqualFunc(nil, nil, equal) = false, want true")
114         }
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)
117         }
118
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)
123         }
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)
127         }
128
129         if !EqualFunc(m1, m2, equalIntStr) {
130                 t.Errorf("EqualFunc(%v, %v, equalIntStr) = false, want true", m1, m2)
131         }
132 }
133
134 func TestClone(t *testing.T) {
135         mc := Clone(m1)
136         if !Equal(mc, m1) {
137                 t.Errorf("Clone(%v) = %v, want %v", m1, mc, m1)
138         }
139         mc[16] = 32
140         if Equal(mc, m1) {
141                 t.Errorf("Equal(%v, %v) = true, want false", mc, m1)
142         }
143 }
144
145 func TestCloneNil(t *testing.T) {
146         var m1 map[string]int
147         mc := Clone(m1)
148         if mc != nil {
149                 t.Errorf("Clone(%v) = %v, want %v", m1, mc, m1)
150         }
151 }
152
153 func TestCopy(t *testing.T) {
154         mc := Clone(m1)
155         Copy(mc, mc)
156         if !Equal(mc, m1) {
157                 t.Errorf("Copy(%v, %v) = %v, want %v", m1, m1, mc, m1)
158         }
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)
163         }
164
165         type M1 map[int]bool
166         type M2 map[int]bool
167         Copy(make(M1), make(M2))
168 }
169
170 func TestDeleteFunc(t *testing.T) {
171         mc := Clone(m1)
172         DeleteFunc(mc, func(int, int) bool { return false })
173         if !Equal(mc, m1) {
174                 t.Errorf("DeleteFunc(%v, true) = %v, want %v", m1, mc, m1)
175         }
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)
180         }
181 }