MCPcopy
hub / github.com/tailscale/tailscale / TestMap

Function TestMap

syncs/syncs_test.go:200–290  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

198}
199
200func TestMap(t *testing.T) {
201 var m Map[string, int]
202 if v, ok := m.Load("noexist"); v != 0 || ok {
203 t.Errorf(`Load("noexist") = (%v, %v), want (0, false)`, v, ok)
204 }
205 m.LoadFunc("noexist", func(v int, ok bool) {
206 if v != 0 || ok {
207 t.Errorf(`LoadFunc("noexist") = (%v, %v), want (0, false)`, v, ok)
208 }
209 })
210 m.Store("one", 1)
211 if v, ok := m.LoadOrStore("one", -1); v != 1 || !ok {
212 t.Errorf(`LoadOrStore("one", 1) = (%v, %v), want (1, true)`, v, ok)
213 }
214 if v, ok := m.Load("one"); v != 1 || !ok {
215 t.Errorf(`Load("one") = (%v, %v), want (1, true)`, v, ok)
216 }
217 m.LoadFunc("one", func(v int, ok bool) {
218 if v != 1 || !ok {
219 t.Errorf(`LoadFunc("one") = (%v, %v), want (1, true)`, v, ok)
220 }
221 })
222 if v, ok := m.LoadOrStore("two", 2); v != 2 || ok {
223 t.Errorf(`LoadOrStore("two", 2) = (%v, %v), want (2, false)`, v, ok)
224 }
225 if v, ok := m.LoadOrInit("three", func() int { return 3 }); v != 3 || ok {
226 t.Errorf(`LoadOrInit("three", 3) = (%v, %v), want (3, true)`, v, ok)
227 }
228 got := map[string]int{}
229 want := map[string]int{"one": 1, "two": 2, "three": 3}
230 maps.Insert(got, m.All())
231 if d := cmp.Diff(got, want); d != "" {
232 t.Errorf("Range mismatch (-got +want):\n%s", d)
233 }
234 if v, ok := m.LoadAndDelete("two"); v != 2 || !ok {
235 t.Errorf(`LoadAndDelete("two) = (%v, %v), want (2, true)`, v, ok)
236 }
237 if v, ok := m.LoadAndDelete("two"); v != 0 || ok {
238 t.Errorf(`LoadAndDelete("two) = (%v, %v), want (0, false)`, v, ok)
239 }
240 m.Delete("three")
241 m.Delete("one")
242 m.Delete("noexist")
243 got = map[string]int{}
244 want = map[string]int{}
245 maps.Insert(got, m.All())
246 if d := cmp.Diff(got, want); d != "" {
247 t.Errorf("Range mismatch (-got +want):\n%s", d)
248 }
249
250 t.Run("LoadOrStore", func(t *testing.T) {
251 var m Map[string, string]
252 var wg sync.WaitGroup
253 var ok1, ok2 bool
254 wg.Go(func() { _, ok1 = m.LoadOrStore("", "") })
255 wg.Go(func() { _, ok2 = m.LoadOrStore("", "") })
256 wg.Wait()
257 if ok1 == ok2 {

Callers

nothing calls this directly

Calls 15

LoadFuncMethod · 0.80
LoadOrStoreMethod · 0.80
LoadOrInitMethod · 0.80
DiffMethod · 0.80
LoadAndDeleteMethod · 0.80
GoMethod · 0.80
LoadMethod · 0.65
ErrorfMethod · 0.65
StoreMethod · 0.65
InsertMethod · 0.65
AllMethod · 0.65
DeleteMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…