Debian Codegraph @

internal/syncx/map_test.go

raw
1 // Copyright 2024 The NATS Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package syncx
15
16 import (
17 "testing"
18 )
19
20 func TestMapLoad(t *testing.T) {
21 var m Map[int, string]
22 m.Store(1, "one")
23
24 v, ok := m.Load(1)
25 if !ok || v != "one" {
26 t.Errorf("Load(1) = %v, %v; want 'one', true", v, ok)
27 }
28
29 v, ok = m.Load(2)
30 if ok || v != "" {
31 t.Errorf("Load(2) = %v, %v; want '', false", v, ok)
32 }
33 }
34
35 func TestMapStore(t *testing.T) {
36 var m Map[int, string]
37 m.Store(1, "one")
38
39 v, ok := m.Load(1)
40 if !ok || v != "one" {
41 t.Errorf("Load(1) after Store(1, 'one') = %v, %v; want 'one', true", v, ok)
42 }
43 }
44
45 func TestMapDelete(t *testing.T) {
46 var m Map[int, string]
47 m.Store(1, "one")
48 m.Delete(1)
49
50 v, ok := m.Load(1)
51 if ok || v != "" {
52 t.Errorf("Load(1) after Delete(1) = %v, %v; want '', false", v, ok)
53 }
54 }
55
56 func TestMapRange(t *testing.T) {
57 var m Map[int, string]
58 m.Store(1, "one")
59 m.Store(2, "two")
60
61 var keys []int
62 var values []string
63 m.Range(func(key int, value string) bool {
64 keys = append(keys, key)
65 values = append(values, value)
66 return true
67 })
68
69 if len(keys) != 2 || len(values) != 2 {
70 t.Errorf("Range() keys = %v, values = %v; want 2 keys and 2 values", keys, values)
71 }
72 }
73
74 func TestMapLoadOrStore(t *testing.T) {
75 var m Map[int, string]
76
77 v, loaded := m.LoadOrStore(1, "one")
78 if loaded || v != "one" {
79 t.Errorf("LoadOrStore(1, 'one') = %v, %v; want 'one', false", v, loaded)
80 }
81
82 v, loaded = m.LoadOrStore(1, "uno")
83 if !loaded || v != "one" {
84 t.Errorf("LoadOrStore(1, 'uno') = %v, %v; want 'one', true", v, loaded)
85 }
86 }
87
88 func TestMapLoadAndDelete(t *testing.T) {
89 var m Map[int, string]
90 m.Store(1, "one")
91
92 v, ok := m.LoadAndDelete(1)
93 if !ok || v != "one" {
94 t.Errorf("LoadAndDelete(1) = %v, %v; want 'one', true", v, ok)
95 }
96
97 v, ok = m.Load(1)
98 if ok || v != "" {
99 t.Errorf("Load(1) after LoadAndDelete(1) = %v, %v; want '', false", v, ok)
100 }
101
102 // Test that LoadAndDelete on a missing key returns the zero value.
103 v, ok = m.LoadAndDelete(2)
104 if ok || v != "" {
105 t.Errorf("LoadAndDelete(2) = %v, %v; want '', false", v, ok)
106 }
107 }
108
109 func TestMapCompareAndSwap(t *testing.T) {
110 var m Map[int, string]
111 m.Store(1, "one")
112
113 ok := m.CompareAndSwap(1, "one", "uno")
114 if !ok {
115 t.Errorf("CompareAndSwap(1, 'one', 'uno') = false; want true")
116 }
117
118 v, _ := m.Load(1)
119 if v != "uno" {
120 t.Errorf("Load(1) after CompareAndSwap = %v; want 'uno'", v)
121 }
122 }
123
124 func TestMapCompareAndDelete(t *testing.T) {
125 var m Map[int, string]
126 m.Store(1, "one")
127
128 ok := m.CompareAndDelete(1, "one")
129 if !ok {
130 t.Errorf("CompareAndDelete(1, 'one') = false; want true")
131 }
132
133 v, _ := m.Load(1)
134 if v != "" {
135 t.Errorf("Load(1) after CompareAndDelete = %v; want ''", v)
136 }
137 }
138
139 func TestMapSwap(t *testing.T) {
140 var m Map[int, string]
141 m.Store(1, "one")
142
143 v, loaded := m.Swap(1, "uno")
144 if !loaded || v != "one" {
145 t.Errorf("Swap(1, 'uno') = %v, %v; want 'one', true", v, loaded)
146 }
147
148 v, _ = m.Load(1)
149 if v != "uno" {
150 t.Errorf("Load(1) after Swap = %v; want 'uno'", v)
151 }
152 }