| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package hashmap |
| 6 |
|
| 7 |
import ( |
| 8 |
"encoding/json" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/emirpasic/gods/v2/testutils" |
| 13 |
) |
| 14 |
|
| 15 |
func TestMapPut(t *testing.T) { |
| 16 |
m := New[int, string]() |
| 17 |
m.Put(5, "e") |
| 18 |
m.Put(6, "f") |
| 19 |
m.Put(7, "g") |
| 20 |
m.Put(3, "c") |
| 21 |
m.Put(4, "d") |
| 22 |
m.Put(1, "x") |
| 23 |
m.Put(2, "b") |
| 24 |
m.Put(1, "a") |
| 25 |
|
| 26 |
if actualValue := m.Size(); actualValue != 7 { |
| 27 |
t.Errorf("Got %v expected %v", actualValue, 7) |
| 28 |
} |
| 29 |
testutils.SameElements(t, m.Keys(), []int{1, 2, 3, 4, 5, 6, 7}) |
| 30 |
testutils.SameElements(t, m.Values(), []string{"a", "b", "c", "d", "e", "f", "g"}) |
| 31 |
|
| 32 |
|
| 33 |
tests1 := [][]interface{}{ |
| 34 |
{1, "a", true}, |
| 35 |
{2, "b", true}, |
| 36 |
{3, "c", true}, |
| 37 |
{4, "d", true}, |
| 38 |
{5, "e", true}, |
| 39 |
{6, "f", true}, |
| 40 |
{7, "g", true}, |
| 41 |
{8, "", false}, |
| 42 |
} |
| 43 |
|
| 44 |
for _, test := range tests1 { |
| 45 |
|
| 46 |
actualValue, actualFound := m.Get(test[0].(int)) |
| 47 |
if actualValue != test[1] || actualFound != test[2] { |
| 48 |
t.Errorf("Got %v expected %v", actualValue, test[1]) |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
func TestMapRemove(t *testing.T) { |
| 54 |
m := New[int, string]() |
| 55 |
m.Put(5, "e") |
| 56 |
m.Put(6, "f") |
| 57 |
m.Put(7, "g") |
| 58 |
m.Put(3, "c") |
| 59 |
m.Put(4, "d") |
| 60 |
m.Put(1, "x") |
| 61 |
m.Put(2, "b") |
| 62 |
m.Put(1, "a") |
| 63 |
|
| 64 |
m.Remove(5) |
| 65 |
m.Remove(6) |
| 66 |
m.Remove(7) |
| 67 |
m.Remove(8) |
| 68 |
m.Remove(5) |
| 69 |
|
| 70 |
testutils.SameElements(t, m.Keys(), []int{1, 2, 3, 4}) |
| 71 |
testutils.SameElements(t, m.Values(), []string{"a", "b", "c", "d"}) |
| 72 |
|
| 73 |
if actualValue := m.Size(); actualValue != 4 { |
| 74 |
t.Errorf("Got %v expected %v", actualValue, 4) |
| 75 |
} |
| 76 |
|
| 77 |
tests2 := [][]interface{}{ |
| 78 |
{1, "a", true}, |
| 79 |
{2, "b", true}, |
| 80 |
{3, "c", true}, |
| 81 |
{4, "d", true}, |
| 82 |
{5, "", false}, |
| 83 |
{6, "", false}, |
| 84 |
{7, "", false}, |
| 85 |
{8, "", false}, |
| 86 |
} |
| 87 |
|
| 88 |
for _, test := range tests2 { |
| 89 |
actualValue, actualFound := m.Get(test[0].(int)) |
| 90 |
if actualValue != test[1] || actualFound != test[2] { |
| 91 |
t.Errorf("Got %v expected %v", actualValue, test[1]) |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
m.Remove(1) |
| 96 |
m.Remove(4) |
| 97 |
m.Remove(2) |
| 98 |
m.Remove(3) |
| 99 |
m.Remove(2) |
| 100 |
m.Remove(2) |
| 101 |
|
| 102 |
testutils.SameElements(t, m.Keys(), nil) |
| 103 |
testutils.SameElements(t, m.Values(), nil) |
| 104 |
if actualValue := m.Size(); actualValue != 0 { |
| 105 |
t.Errorf("Got %v expected %v", actualValue, 0) |
| 106 |
} |
| 107 |
if actualValue := m.Empty(); actualValue != true { |
| 108 |
t.Errorf("Got %v expected %v", actualValue, true) |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
func TestMapSerialization(t *testing.T) { |
| 113 |
m := New[string, float64]() |
| 114 |
m.Put("a", 1.0) |
| 115 |
m.Put("b", 2.0) |
| 116 |
m.Put("c", 3.0) |
| 117 |
|
| 118 |
var err error |
| 119 |
assert := func() { |
| 120 |
testutils.SameElements(t, m.Keys(), []string{"a", "b", "c"}) |
| 121 |
testutils.SameElements(t, m.Values(), []float64{1.0, 2.0, 3.0}) |
| 122 |
if actualValue, expectedValue := m.Size(), 3; actualValue != expectedValue { |
| 123 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 124 |
} |
| 125 |
if err != nil { |
| 126 |
t.Errorf("Got error %v", err) |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
assert() |
| 131 |
|
| 132 |
bytes, err := m.ToJSON() |
| 133 |
assert() |
| 134 |
|
| 135 |
err = m.FromJSON(bytes) |
| 136 |
assert() |
| 137 |
|
| 138 |
bytes, err = json.Marshal([]interface{}{"a", "b", "c", m}) |
| 139 |
if err != nil { |
| 140 |
t.Errorf("Got error %v", err) |
| 141 |
} |
| 142 |
|
| 143 |
err = json.Unmarshal([]byte(`{"a":1,"b":2}`), &m) |
| 144 |
if err != nil { |
| 145 |
t.Errorf("Got error %v", err) |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
func TestMapString(t *testing.T) { |
| 150 |
c := New[string, int]() |
| 151 |
c.Put("a", 1) |
| 152 |
if !strings.HasPrefix(c.String(), "HashMap") { |
| 153 |
t.Errorf("String should start with container name") |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
func benchmarkGet(b *testing.B, m *Map[int, int], size int) { |
| 158 |
for i := 0; i < b.N; i++ { |
| 159 |
for n := 0; n < size; n++ { |
| 160 |
m.Get(n) |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
func benchmarkPut(b *testing.B, m *Map[int, int], size int) { |
| 166 |
for i := 0; i < b.N; i++ { |
| 167 |
for n := 0; n < size; n++ { |
| 168 |
m.Put(n, n) |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
func benchmarkRemove(b *testing.B, m *Map[int, int], size int) { |
| 174 |
for i := 0; i < b.N; i++ { |
| 175 |
for n := 0; n < size; n++ { |
| 176 |
m.Remove(n) |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
func BenchmarkHashMapGet100(b *testing.B) { |
| 182 |
b.StopTimer() |
| 183 |
size := 100 |
| 184 |
m := New[int, int]() |
| 185 |
for n := 0; n < size; n++ { |
| 186 |
m.Put(n, n) |
| 187 |
} |
| 188 |
b.StartTimer() |
| 189 |
benchmarkGet(b, m, size) |
| 190 |
} |
| 191 |
|
| 192 |
func BenchmarkHashMapGet1000(b *testing.B) { |
| 193 |
b.StopTimer() |
| 194 |
size := 1000 |
| 195 |
m := New[int, int]() |
| 196 |
for n := 0; n < size; n++ { |
| 197 |
m.Put(n, n) |
| 198 |
} |
| 199 |
b.StartTimer() |
| 200 |
benchmarkGet(b, m, size) |
| 201 |
} |
| 202 |
|
| 203 |
func BenchmarkHashMapGet10000(b *testing.B) { |
| 204 |
b.StopTimer() |
| 205 |
size := 10000 |
| 206 |
m := New[int, int]() |
| 207 |
for n := 0; n < size; n++ { |
| 208 |
m.Put(n, n) |
| 209 |
} |
| 210 |
b.StartTimer() |
| 211 |
benchmarkGet(b, m, size) |
| 212 |
} |
| 213 |
|
| 214 |
func BenchmarkHashMapGet100000(b *testing.B) { |
| 215 |
b.StopTimer() |
| 216 |
size := 100000 |
| 217 |
m := New[int, int]() |
| 218 |
for n := 0; n < size; n++ { |
| 219 |
m.Put(n, n) |
| 220 |
} |
| 221 |
b.StartTimer() |
| 222 |
benchmarkGet(b, m, size) |
| 223 |
} |
| 224 |
|
| 225 |
func BenchmarkHashMapPut100(b *testing.B) { |
| 226 |
b.StopTimer() |
| 227 |
size := 100 |
| 228 |
m := New[int, int]() |
| 229 |
b.StartTimer() |
| 230 |
benchmarkPut(b, m, size) |
| 231 |
} |
| 232 |
|
| 233 |
func BenchmarkHashMapPut1000(b *testing.B) { |
| 234 |
b.StopTimer() |
| 235 |
size := 1000 |
| 236 |
m := New[int, int]() |
| 237 |
for n := 0; n < size; n++ { |
| 238 |
m.Put(n, n) |
| 239 |
} |
| 240 |
b.StartTimer() |
| 241 |
benchmarkPut(b, m, size) |
| 242 |
} |
| 243 |
|
| 244 |
func BenchmarkHashMapPut10000(b *testing.B) { |
| 245 |
b.StopTimer() |
| 246 |
size := 10000 |
| 247 |
m := New[int, int]() |
| 248 |
for n := 0; n < size; n++ { |
| 249 |
m.Put(n, n) |
| 250 |
} |
| 251 |
b.StartTimer() |
| 252 |
benchmarkPut(b, m, size) |
| 253 |
} |
| 254 |
|
| 255 |
func BenchmarkHashMapPut100000(b *testing.B) { |
| 256 |
b.StopTimer() |
| 257 |
size := 100000 |
| 258 |
m := New[int, int]() |
| 259 |
for n := 0; n < size; n++ { |
| 260 |
m.Put(n, n) |
| 261 |
} |
| 262 |
b.StartTimer() |
| 263 |
benchmarkPut(b, m, size) |
| 264 |
} |
| 265 |
|
| 266 |
func BenchmarkHashMapRemove100(b *testing.B) { |
| 267 |
b.StopTimer() |
| 268 |
size := 100 |
| 269 |
m := New[int, int]() |
| 270 |
for n := 0; n < size; n++ { |
| 271 |
m.Put(n, n) |
| 272 |
} |
| 273 |
b.StartTimer() |
| 274 |
benchmarkRemove(b, m, size) |
| 275 |
} |
| 276 |
|
| 277 |
func BenchmarkHashMapRemove1000(b *testing.B) { |
| 278 |
b.StopTimer() |
| 279 |
size := 1000 |
| 280 |
m := New[int, int]() |
| 281 |
for n := 0; n < size; n++ { |
| 282 |
m.Put(n, n) |
| 283 |
} |
| 284 |
b.StartTimer() |
| 285 |
benchmarkRemove(b, m, size) |
| 286 |
} |
| 287 |
|
| 288 |
func BenchmarkHashMapRemove10000(b *testing.B) { |
| 289 |
b.StopTimer() |
| 290 |
size := 10000 |
| 291 |
m := New[int, int]() |
| 292 |
for n := 0; n < size; n++ { |
| 293 |
m.Put(n, n) |
| 294 |
} |
| 295 |
b.StartTimer() |
| 296 |
benchmarkRemove(b, m, size) |
| 297 |
} |
| 298 |
|
| 299 |
func BenchmarkHashMapRemove100000(b *testing.B) { |
| 300 |
b.StopTimer() |
| 301 |
size := 100000 |
| 302 |
m := New[int, int]() |
| 303 |
for n := 0; n < size; n++ { |
| 304 |
m.Put(n, n) |
| 305 |
} |
| 306 |
b.StartTimer() |
| 307 |
benchmarkRemove(b, m, size) |
| 308 |
} |