1
2
3
4
5 package maps
6
7 import (
8 "math"
9 "sort"
10 "strconv"
11 "testing"
12
13 "golang.org/x/exp/slices"
14 )
15
16 var m1 = map[int]int{1: 2, 2: 4, 4: 8, 8: 16}
17 var m2 = map[int]string{1: "2", 2: "4", 4: "8", 8: "16"}
18
19 func TestKeys(t *testing.T) {
20 want := []int{1, 2, 4, 8}
21
22 got1 := Keys(m1)
23 sort.Ints(got1)
24 if !slices.Equal(got1, want) {
25 t.Errorf("Keys(%v) = %v, want %v", m1, got1, want)
26 }
27
28 got2 := Keys(m2)
29 sort.Ints(got2)
30 if !slices.Equal(got2, want) {
31 t.Errorf("Keys(%v) = %v, want %v", m2, got2, want)
32 }
33 }
34
35 func TestValues(t *testing.T) {
36 got1 := Values(m1)
37 want1 := []int{2, 4, 8, 16}
38 sort.Ints(got1)
39 if !slices.Equal(got1, want1) {
40 t.Errorf("Values(%v) = %v, want %v", m1, got1, want1)
41 }
42
43 got2 := Values(m2)
44 want2 := []string{"16", "2", "4", "8"}
45 sort.Strings(got2)
46 if !slices.Equal(got2, want2) {
47 t.Errorf("Values(%v) = %v, want %v", m2, got2, want2)
48 }
49 }
50
51 func TestEqual(t *testing.T) {
52 if !Equal(m1, m1) {
53 t.Errorf("Equal(%v, %v) = false, want true", m1, m1)
54 }
55 if Equal(m1, (map[int]int)(nil)) {
56 t.Errorf("Equal(%v, nil) = true, want false", m1)
57 }
58 if Equal((map[int]int)(nil), m1) {
59 t.Errorf("Equal(nil, %v) = true, want false", m1)
60 }
61 if !Equal[map[int]int, map[int]int](nil, nil) {
62 t.Error("Equal(nil, nil) = false, want true")
63 }
64 if ms := map[int]int{1: 2}; Equal(m1, ms) {
65 t.Errorf("Equal(%v, %v) = true, want false", m1, ms)
66 }
67
68
69 mf := map[int]float64{1: 0, 2: math.NaN()}
70 if Equal(mf, mf) {
71 t.Errorf("Equal(%v, %v) = true, want false", mf, mf)
72 }
73 }
74
75
76 func equal[T comparable](v1, v2 T) bool {
77 return v1 == v2
78 }
79
80
81 func equalNaN[T comparable](v1, v2 T) bool {
82 isNaN := func(f T) bool { return f != f }
83 return v1 == v2 || (isNaN(v1) && isNaN(v2))
84 }
85
86
87 func equalIntStr(v1 int, v2 string) bool {
88 return strconv.Itoa(v1) == v2
89 }
90
91 func TestEqualFunc(t *testing.T) {
92 if !EqualFunc(m1, m1, equal[int]) {
93 t.Errorf("EqualFunc(%v, %v, equal) = false, want true", m1, m1)
94 }
95 if EqualFunc(m1, (map[int]int)(nil), equal[int]) {
96 t.Errorf("EqualFunc(%v, nil, equal) = true, want false", m1)
97 }
98 if EqualFunc((map[int]int)(nil), m1, equal[int]) {
99 t.Errorf("EqualFunc(nil, %v, equal) = true, want false", m1)
100 }
101 if !EqualFunc[map[int]int, map[int]int](nil, nil, equal[int]) {
102 t.Error("EqualFunc(nil, nil, equal) = false, want true")
103 }
104 if ms := map[int]int{1: 2}; EqualFunc(m1, ms, equal[int]) {
105 t.Errorf("EqualFunc(%v, %v, equal) = true, want false", m1, ms)
106 }
107
108
109 mf := map[int]float64{1: 0, 2: math.NaN()}
110 if EqualFunc(mf, mf, equal[float64]) {
111 t.Errorf("EqualFunc(%v, %v, equal) = true, want false", mf, mf)
112 }
113
114 if !EqualFunc(mf, mf, equalNaN[float64]) {
115 t.Errorf("EqualFunc(%v, %v, equalNaN) = false, want true", mf, mf)
116 }
117
118 if !EqualFunc(m1, m2, equalIntStr) {
119 t.Errorf("EqualFunc(%v, %v, equalIntStr) = false, want true", m1, m2)
120 }
121 }
122
123 func TestClear(t *testing.T) {
124 ml := map[int]int{1: 1, 2: 2, 3: 3}
125 Clear(ml)
126 if got := len(ml); got != 0 {
127 t.Errorf("len(%v) = %d after Clear, want 0", ml, got)
128 }
129 if !Equal(ml, (map[int]int)(nil)) {
130 t.Errorf("Equal(%v, nil) = false, want true", ml)
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 }
182
View as plain text