MCPcopy Create free account
hub / github.com/elliotchance/orderedmap / TestKeys

Function TestKeys

orderedmap_test.go:174–231  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

172}
173
174func TestKeys(t *testing.T) {
175 t.Run("EmptyMap", func(t *testing.T) {
176 m := orderedmap.NewOrderedMap()
177 assert.Empty(t, m.Keys())
178 })
179
180 t.Run("OneElement", func(t *testing.T) {
181 m := orderedmap.NewOrderedMap()
182 m.Set(1, true)
183 assert.Equal(t, []interface{}{1}, m.Keys())
184 })
185
186 t.Run("RetainsOrder", func(t *testing.T) {
187 m := orderedmap.NewOrderedMap()
188 for i := 1; i < 10; i++ {
189 m.Set(i, true)
190 }
191 assert.Equal(t,
192 []interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9},
193 m.Keys())
194 })
195
196 t.Run("ReplacingKeyDoesntChangeOrder", func(t *testing.T) {
197 m := orderedmap.NewOrderedMap()
198 m.Set("foo", true)
199 m.Set("bar", true)
200 m.Set("foo", false)
201 assert.Equal(t,
202 []interface{}{"foo", "bar"},
203 m.Keys())
204 })
205
206 t.Run("KeysAfterDelete", func(t *testing.T) {
207 m := orderedmap.NewOrderedMap()
208 m.Set("foo", true)
209 m.Set("bar", true)
210 m.Delete("foo")
211 assert.Equal(t, []interface{}{"bar"}, m.Keys())
212 })
213
214 t.Run("Performance", func(t *testing.T) {
215 if testing.Short() {
216 t.Skip("performance test skipped in short mode")
217 }
218
219 res1 := testing.Benchmark(benchmarkOrderedMap_Keys(100))
220 res4 := testing.Benchmark(benchmarkOrderedMap_Keys(400))
221
222 // O(1) would mean that res4 should take about 4 times longer than res1
223 // because we are doing 4 times the amount of Set/Delete operations.
224 // Allow for a wide margin, but not too wide that it would permit the
225 // inflection to O(n^2).
226
227 assert.InDelta(t,
228 4*res1.NsPerOp(), res4.NsPerOp(),
229 float64(res4.NsPerOp()))
230 })
231}

Callers

nothing calls this directly

Calls 5

KeysMethod · 0.95
SetMethod · 0.95
DeleteMethod · 0.95
NewOrderedMapFunction · 0.92
benchmarkOrderedMap_KeysFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…