| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package linkedhashmap |
| 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 TestMapEach(t *testing.T) { |
| 113 |
m := New[string, int]() |
| 114 |
m.Put("c", 1) |
| 115 |
m.Put("a", 2) |
| 116 |
m.Put("b", 3) |
| 117 |
count := 0 |
| 118 |
m.Each(func(key string, value int) { |
| 119 |
count++ |
| 120 |
if actualValue, expectedValue := count, value; actualValue != expectedValue { |
| 121 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 122 |
} |
| 123 |
switch value { |
| 124 |
case 1: |
| 125 |
if actualValue, expectedValue := key, "c"; actualValue != expectedValue { |
| 126 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 127 |
} |
| 128 |
case 2: |
| 129 |
if actualValue, expectedValue := key, "a"; actualValue != expectedValue { |
| 130 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 131 |
} |
| 132 |
case 3: |
| 133 |
if actualValue, expectedValue := key, "b"; actualValue != expectedValue { |
| 134 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 135 |
} |
| 136 |
default: |
| 137 |
t.Errorf("Too many") |
| 138 |
} |
| 139 |
}) |
| 140 |
} |
| 141 |
|
| 142 |
func TestMapMap(t *testing.T) { |
| 143 |
m := New[string, int]() |
| 144 |
m.Put("c", 3) |
| 145 |
m.Put("a", 1) |
| 146 |
m.Put("b", 2) |
| 147 |
mappedMap := m.Map(func(key1 string, value1 int) (key2 string, value2 int) { |
| 148 |
return key1, value1 * value1 |
| 149 |
}) |
| 150 |
if actualValue, _ := mappedMap.Get("c"); actualValue != 9 { |
| 151 |
t.Errorf("Got %v expected %v", actualValue, "mapped: c") |
| 152 |
} |
| 153 |
if actualValue, _ := mappedMap.Get("a"); actualValue != 1 { |
| 154 |
t.Errorf("Got %v expected %v", actualValue, "mapped: a") |
| 155 |
} |
| 156 |
if actualValue, _ := mappedMap.Get("b"); actualValue != 4 { |
| 157 |
t.Errorf("Got %v expected %v", actualValue, "mapped: b") |
| 158 |
} |
| 159 |
if mappedMap.Size() != 3 { |
| 160 |
t.Errorf("Got %v expected %v", mappedMap.Size(), 3) |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
func TestMapSelect(t *testing.T) { |
| 165 |
m := New[string, int]() |
| 166 |
m.Put("c", 3) |
| 167 |
m.Put("b", 1) |
| 168 |
m.Put("a", 2) |
| 169 |
selectedMap := m.Select(func(key string, value int) bool { |
| 170 |
return key >= "a" && key <= "b" |
| 171 |
}) |
| 172 |
if actualValue, _ := selectedMap.Get("b"); actualValue != 1 { |
| 173 |
t.Errorf("Got %v expected %v", actualValue, "value: a") |
| 174 |
} |
| 175 |
if actualValue, _ := selectedMap.Get("a"); actualValue != 2 { |
| 176 |
t.Errorf("Got %v expected %v", actualValue, "value: b") |
| 177 |
} |
| 178 |
if selectedMap.Size() != 2 { |
| 179 |
t.Errorf("Got %v expected %v", selectedMap.Size(), 2) |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
func TestMapAny(t *testing.T) { |
| 184 |
m := New[string, int]() |
| 185 |
m.Put("c", 3) |
| 186 |
m.Put("a", 1) |
| 187 |
m.Put("b", 2) |
| 188 |
any := m.Any(func(key string, value int) bool { |
| 189 |
return value == 3 |
| 190 |
}) |
| 191 |
if any != true { |
| 192 |
t.Errorf("Got %v expected %v", any, true) |
| 193 |
} |
| 194 |
any = m.Any(func(key string, value int) bool { |
| 195 |
return value == 4 |
| 196 |
}) |
| 197 |
if any != false { |
| 198 |
t.Errorf("Got %v expected %v", any, false) |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
func TestMapAll(t *testing.T) { |
| 203 |
m := New[string, int]() |
| 204 |
m.Put("c", 3) |
| 205 |
m.Put("a", 1) |
| 206 |
m.Put("b", 2) |
| 207 |
all := m.All(func(key string, value int) bool { |
| 208 |
return key >= "a" && key <= "c" |
| 209 |
}) |
| 210 |
if all != true { |
| 211 |
t.Errorf("Got %v expected %v", all, true) |
| 212 |
} |
| 213 |
all = m.All(func(key string, value int) bool { |
| 214 |
return key >= "a" && key <= "b" |
| 215 |
}) |
| 216 |
if all != false { |
| 217 |
t.Errorf("Got %v expected %v", all, false) |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
func TestMapFind(t *testing.T) { |
| 222 |
m := New[string, int]() |
| 223 |
m.Put("c", 3) |
| 224 |
m.Put("a", 1) |
| 225 |
m.Put("b", 2) |
| 226 |
foundKey, foundValue := m.Find(func(key string, value int) bool { |
| 227 |
return key == "c" |
| 228 |
}) |
| 229 |
if foundKey != "c" || foundValue != 3 { |
| 230 |
t.Errorf("Got %v -> %v expected %v -> %v", foundKey, foundValue, "c", 3) |
| 231 |
} |
| 232 |
foundKey, foundValue = m.Find(func(key string, value int) bool { |
| 233 |
return key == "x" |
| 234 |
}) |
| 235 |
if foundKey != "" || foundValue != 0 { |
| 236 |
t.Errorf("Got %v at %v expected %v at %v", foundValue, foundKey, "", 0) |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
func TestMapChaining(t *testing.T) { |
| 241 |
m := New[string, int]() |
| 242 |
m.Put("c", 3) |
| 243 |
m.Put("a", 1) |
| 244 |
m.Put("b", 2) |
| 245 |
chainedMap := m.Select(func(key string, value int) bool { |
| 246 |
return value > 1 |
| 247 |
}).Map(func(key string, value int) (string, int) { |
| 248 |
return key + key, value * value |
| 249 |
}) |
| 250 |
if actualValue := chainedMap.Size(); actualValue != 2 { |
| 251 |
t.Errorf("Got %v expected %v", actualValue, 2) |
| 252 |
} |
| 253 |
if actualValue, found := chainedMap.Get("aa"); actualValue != 0 || found { |
| 254 |
t.Errorf("Got %v expected %v", actualValue, nil) |
| 255 |
} |
| 256 |
if actualValue, found := chainedMap.Get("bb"); actualValue != 4 || !found { |
| 257 |
t.Errorf("Got %v expected %v", actualValue, 4) |
| 258 |
} |
| 259 |
if actualValue, found := chainedMap.Get("cc"); actualValue != 9 || !found { |
| 260 |
t.Errorf("Got %v expected %v", actualValue, 9) |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
func TestMapIteratorNextOnEmpty(t *testing.T) { |
| 265 |
m := New[string, int]() |
| 266 |
it := m.Iterator() |
| 267 |
it = m.Iterator() |
| 268 |
for it.Next() { |
| 269 |
t.Errorf("Shouldn't iterate on empty map") |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
func TestMapIteratorPrevOnEmpty(t *testing.T) { |
| 274 |
m := New[string, int]() |
| 275 |
it := m.Iterator() |
| 276 |
it = m.Iterator() |
| 277 |
for it.Prev() { |
| 278 |
t.Errorf("Shouldn't iterate on empty map") |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
func TestMapIteratorNext(t *testing.T) { |
| 283 |
m := New[string, int]() |
| 284 |
m.Put("c", 1) |
| 285 |
m.Put("a", 2) |
| 286 |
m.Put("b", 3) |
| 287 |
|
| 288 |
it := m.Iterator() |
| 289 |
count := 0 |
| 290 |
for it.Next() { |
| 291 |
count++ |
| 292 |
key := it.Key() |
| 293 |
value := it.Value() |
| 294 |
switch key { |
| 295 |
case "c": |
| 296 |
if actualValue, expectedValue := value, 1; actualValue != expectedValue { |
| 297 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 298 |
} |
| 299 |
case "a": |
| 300 |
if actualValue, expectedValue := value, 2; actualValue != expectedValue { |
| 301 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 302 |
} |
| 303 |
case "b": |
| 304 |
if actualValue, expectedValue := value, 3; actualValue != expectedValue { |
| 305 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 306 |
} |
| 307 |
default: |
| 308 |
t.Errorf("Too many") |
| 309 |
} |
| 310 |
if actualValue, expectedValue := value, count; actualValue != expectedValue { |
| 311 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 312 |
} |
| 313 |
} |
| 314 |
if actualValue, expectedValue := count, 3; actualValue != expectedValue { |
| 315 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
func TestMapIteratorPrev(t *testing.T) { |
| 320 |
m := New[string, int]() |
| 321 |
m.Put("c", 1) |
| 322 |
m.Put("a", 2) |
| 323 |
m.Put("b", 3) |
| 324 |
|
| 325 |
it := m.Iterator() |
| 326 |
for it.Next() { |
| 327 |
} |
| 328 |
countDown := m.Size() |
| 329 |
for it.Prev() { |
| 330 |
key := it.Key() |
| 331 |
value := it.Value() |
| 332 |
switch key { |
| 333 |
case "c": |
| 334 |
if actualValue, expectedValue := value, 1; actualValue != expectedValue { |
| 335 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 336 |
} |
| 337 |
case "a": |
| 338 |
if actualValue, expectedValue := value, 2; actualValue != expectedValue { |
| 339 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 340 |
} |
| 341 |
case "b": |
| 342 |
if actualValue, expectedValue := value, 3; actualValue != expectedValue { |
| 343 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 344 |
} |
| 345 |
default: |
| 346 |
t.Errorf("Too many") |
| 347 |
} |
| 348 |
if actualValue, expectedValue := value, countDown; actualValue != expectedValue { |
| 349 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 350 |
} |
| 351 |
countDown-- |
| 352 |
} |
| 353 |
if actualValue, expectedValue := countDown, 0; actualValue != expectedValue { |
| 354 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
func TestMapIteratorBegin(t *testing.T) { |
| 359 |
m := New[int, string]() |
| 360 |
it := m.Iterator() |
| 361 |
it.Begin() |
| 362 |
m.Put(3, "c") |
| 363 |
m.Put(1, "a") |
| 364 |
m.Put(2, "b") |
| 365 |
for it.Next() { |
| 366 |
} |
| 367 |
it.Begin() |
| 368 |
it.Next() |
| 369 |
if key, value := it.Key(), it.Value(); key != 3 || value != "c" { |
| 370 |
t.Errorf("Got %v,%v expected %v,%v", key, value, 3, "c") |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
func TestMapIteratorEnd(t *testing.T) { |
| 375 |
m := New[int, string]() |
| 376 |
it := m.Iterator() |
| 377 |
m.Put(3, "c") |
| 378 |
m.Put(1, "a") |
| 379 |
m.Put(2, "b") |
| 380 |
it.End() |
| 381 |
it.Prev() |
| 382 |
if key, value := it.Key(), it.Value(); key != 2 || value != "b" { |
| 383 |
t.Errorf("Got %v,%v expected %v,%v", key, value, 2, "b") |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
func TestMapIteratorFirst(t *testing.T) { |
| 388 |
m := New[int, string]() |
| 389 |
m.Put(3, "c") |
| 390 |
m.Put(1, "a") |
| 391 |
m.Put(2, "b") |
| 392 |
it := m.Iterator() |
| 393 |
if actualValue, expectedValue := it.First(), true; actualValue != expectedValue { |
| 394 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 395 |
} |
| 396 |
if key, value := it.Key(), it.Value(); key != 3 || value != "c" { |
| 397 |
t.Errorf("Got %v,%v expected %v,%v", key, value, 3, "c") |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
func TestMapIteratorLast(t *testing.T) { |
| 402 |
m := New[int, string]() |
| 403 |
m.Put(3, "c") |
| 404 |
m.Put(1, "a") |
| 405 |
m.Put(2, "b") |
| 406 |
it := m.Iterator() |
| 407 |
if actualValue, expectedValue := it.Last(), true; actualValue != expectedValue { |
| 408 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 409 |
} |
| 410 |
if key, value := it.Key(), it.Value(); key != 2 || value != "b" { |
| 411 |
t.Errorf("Got %v,%v expected %v,%v", key, value, 2, "b") |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
func TestMapIteratorNextTo(t *testing.T) { |
| 416 |
|
| 417 |
seek := func(index int, value string) bool { |
| 418 |
return strings.HasSuffix(value, "b") |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
{ |
| 423 |
m := New[int, string]() |
| 424 |
it := m.Iterator() |
| 425 |
for it.NextTo(seek) { |
| 426 |
t.Errorf("Shouldn't iterate on empty map") |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
{ |
| 432 |
m := New[int, string]() |
| 433 |
m.Put(0, "xx") |
| 434 |
m.Put(1, "yy") |
| 435 |
it := m.Iterator() |
| 436 |
for it.NextTo(seek) { |
| 437 |
t.Errorf("Shouldn't iterate on empty map") |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
{ |
| 443 |
m := New[int, string]() |
| 444 |
m.Put(0, "aa") |
| 445 |
m.Put(1, "bb") |
| 446 |
m.Put(2, "cc") |
| 447 |
it := m.Iterator() |
| 448 |
it.Begin() |
| 449 |
if !it.NextTo(seek) { |
| 450 |
t.Errorf("Shouldn't iterate on empty map") |
| 451 |
} |
| 452 |
if index, value := it.Key(), it.Value(); index != 1 || value != "bb" { |
| 453 |
t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb") |
| 454 |
} |
| 455 |
if !it.Next() { |
| 456 |
t.Errorf("Should go to first element") |
| 457 |
} |
| 458 |
if index, value := it.Key(), it.Value(); index != 2 || value != "cc" { |
| 459 |
t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "cc") |
| 460 |
} |
| 461 |
if it.Next() { |
| 462 |
t.Errorf("Should not go past last element") |
| 463 |
} |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
func TestMapIteratorPrevTo(t *testing.T) { |
| 468 |
|
| 469 |
seek := func(index int, value string) bool { |
| 470 |
return strings.HasSuffix(value, "b") |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
{ |
| 475 |
m := New[int, string]() |
| 476 |
it := m.Iterator() |
| 477 |
it.End() |
| 478 |
for it.PrevTo(seek) { |
| 479 |
t.Errorf("Shouldn't iterate on empty map") |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
{ |
| 485 |
m := New[int, string]() |
| 486 |
m.Put(0, "xx") |
| 487 |
m.Put(1, "yy") |
| 488 |
it := m.Iterator() |
| 489 |
it.End() |
| 490 |
for it.PrevTo(seek) { |
| 491 |
t.Errorf("Shouldn't iterate on empty map") |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
{ |
| 497 |
m := New[int, string]() |
| 498 |
m.Put(0, "aa") |
| 499 |
m.Put(1, "bb") |
| 500 |
m.Put(2, "cc") |
| 501 |
it := m.Iterator() |
| 502 |
it.End() |
| 503 |
if !it.PrevTo(seek) { |
| 504 |
t.Errorf("Shouldn't iterate on empty map") |
| 505 |
} |
| 506 |
if index, value := it.Key(), it.Value(); index != 1 || value != "bb" { |
| 507 |
t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb") |
| 508 |
} |
| 509 |
if !it.Prev() { |
| 510 |
t.Errorf("Should go to first element") |
| 511 |
} |
| 512 |
if index, value := it.Key(), it.Value(); index != 0 || value != "aa" { |
| 513 |
t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "aa") |
| 514 |
} |
| 515 |
if it.Prev() { |
| 516 |
t.Errorf("Should not go before first element") |
| 517 |
} |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
func TestMapSerialization(t *testing.T) { |
| 522 |
for i := 0; i < 10; i++ { |
| 523 |
original := New[string, string]() |
| 524 |
original.Put("d", "4") |
| 525 |
original.Put("e", "5") |
| 526 |
original.Put("c", "3") |
| 527 |
original.Put("b", "2") |
| 528 |
original.Put("a", "1") |
| 529 |
|
| 530 |
serialized, err := original.ToJSON() |
| 531 |
if err != nil { |
| 532 |
t.Errorf("Got error %v", err) |
| 533 |
} |
| 534 |
|
| 535 |
deserialized := New[string, string]() |
| 536 |
err = deserialized.FromJSON(serialized) |
| 537 |
if err != nil { |
| 538 |
t.Errorf("Got error %v", err) |
| 539 |
} |
| 540 |
|
| 541 |
if original.Size() != deserialized.Size() { |
| 542 |
t.Errorf("Got map of size %d, expected %d", original.Size(), deserialized.Size()) |
| 543 |
} |
| 544 |
original.Each(func(key string, expected string) { |
| 545 |
actual, ok := deserialized.Get(key) |
| 546 |
if !ok || actual != expected { |
| 547 |
t.Errorf("Did not find expected value %v for key %v in deserialied map (got %v)", expected, key, actual) |
| 548 |
} |
| 549 |
}) |
| 550 |
} |
| 551 |
|
| 552 |
m := New[string, float64]() |
| 553 |
m.Put("a", 1.0) |
| 554 |
m.Put("b", 2.0) |
| 555 |
m.Put("c", 3.0) |
| 556 |
|
| 557 |
_, err := json.Marshal([]interface{}{"a", "b", "c", m}) |
| 558 |
if err != nil { |
| 559 |
t.Errorf("Got error %v", err) |
| 560 |
} |
| 561 |
|
| 562 |
err = json.Unmarshal([]byte(`{"a":1,"b":2}`), &m) |
| 563 |
if err != nil { |
| 564 |
t.Errorf("Got error %v", err) |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
func TestMapString(t *testing.T) { |
| 569 |
c := New[string, int]() |
| 570 |
c.Put("a", 1) |
| 571 |
if !strings.HasPrefix(c.String(), "LinkedHashMap") { |
| 572 |
t.Errorf("String should start with container name") |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
func benchmarkGet(b *testing.B, m *Map[int, int], size int) { |
| 577 |
for i := 0; i < b.N; i++ { |
| 578 |
for n := 0; n < size; n++ { |
| 579 |
m.Get(n) |
| 580 |
} |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
func benchmarkPut(b *testing.B, m *Map[int, int], size int) { |
| 585 |
for i := 0; i < b.N; i++ { |
| 586 |
for n := 0; n < size; n++ { |
| 587 |
m.Put(n, n) |
| 588 |
} |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
func benchmarkRemove(b *testing.B, m *Map[int, int], size int) { |
| 593 |
for i := 0; i < b.N; i++ { |
| 594 |
for n := 0; n < size; n++ { |
| 595 |
m.Remove(n) |
| 596 |
} |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
func BenchmarkTreeMapGet100(b *testing.B) { |
| 601 |
b.StopTimer() |
| 602 |
size := 100 |
| 603 |
m := New[int, int]() |
| 604 |
for n := 0; n < size; n++ { |
| 605 |
m.Put(n, n) |
| 606 |
} |
| 607 |
b.StartTimer() |
| 608 |
benchmarkGet(b, m, size) |
| 609 |
} |
| 610 |
|
| 611 |
func BenchmarkTreeMapGet1000(b *testing.B) { |
| 612 |
b.StopTimer() |
| 613 |
size := 1000 |
| 614 |
m := New[int, int]() |
| 615 |
for n := 0; n < size; n++ { |
| 616 |
m.Put(n, n) |
| 617 |
} |
| 618 |
b.StartTimer() |
| 619 |
benchmarkGet(b, m, size) |
| 620 |
} |
| 621 |
|
| 622 |
func BenchmarkTreeMapGet10000(b *testing.B) { |
| 623 |
b.StopTimer() |
| 624 |
size := 10000 |
| 625 |
m := New[int, int]() |
| 626 |
for n := 0; n < size; n++ { |
| 627 |
m.Put(n, n) |
| 628 |
} |
| 629 |
b.StartTimer() |
| 630 |
benchmarkGet(b, m, size) |
| 631 |
} |
| 632 |
|
| 633 |
func BenchmarkTreeMapGet100000(b *testing.B) { |
| 634 |
b.StopTimer() |
| 635 |
size := 100000 |
| 636 |
m := New[int, int]() |
| 637 |
for n := 0; n < size; n++ { |
| 638 |
m.Put(n, n) |
| 639 |
} |
| 640 |
b.StartTimer() |
| 641 |
benchmarkGet(b, m, size) |
| 642 |
} |
| 643 |
|
| 644 |
func BenchmarkTreeMapPut100(b *testing.B) { |
| 645 |
b.StopTimer() |
| 646 |
size := 100 |
| 647 |
m := New[int, int]() |
| 648 |
b.StartTimer() |
| 649 |
benchmarkPut(b, m, size) |
| 650 |
} |
| 651 |
|
| 652 |
func BenchmarkTreeMapPut1000(b *testing.B) { |
| 653 |
b.StopTimer() |
| 654 |
size := 1000 |
| 655 |
m := New[int, int]() |
| 656 |
for n := 0; n < size; n++ { |
| 657 |
m.Put(n, n) |
| 658 |
} |
| 659 |
b.StartTimer() |
| 660 |
benchmarkPut(b, m, size) |
| 661 |
} |
| 662 |
|
| 663 |
func BenchmarkTreeMapPut10000(b *testing.B) { |
| 664 |
b.StopTimer() |
| 665 |
size := 10000 |
| 666 |
m := New[int, int]() |
| 667 |
for n := 0; n < size; n++ { |
| 668 |
m.Put(n, n) |
| 669 |
} |
| 670 |
b.StartTimer() |
| 671 |
benchmarkPut(b, m, size) |
| 672 |
} |
| 673 |
|
| 674 |
func BenchmarkTreeMapPut100000(b *testing.B) { |
| 675 |
b.StopTimer() |
| 676 |
size := 100000 |
| 677 |
m := New[int, int]() |
| 678 |
for n := 0; n < size; n++ { |
| 679 |
m.Put(n, n) |
| 680 |
} |
| 681 |
b.StartTimer() |
| 682 |
benchmarkPut(b, m, size) |
| 683 |
} |
| 684 |
|
| 685 |
func BenchmarkTreeMapRemove100(b *testing.B) { |
| 686 |
b.StopTimer() |
| 687 |
size := 100 |
| 688 |
m := New[int, int]() |
| 689 |
for n := 0; n < size; n++ { |
| 690 |
m.Put(n, n) |
| 691 |
} |
| 692 |
b.StartTimer() |
| 693 |
benchmarkRemove(b, m, size) |
| 694 |
} |
| 695 |
|
| 696 |
func BenchmarkTreeMapRemove1000(b *testing.B) { |
| 697 |
b.StopTimer() |
| 698 |
size := 1000 |
| 699 |
m := New[int, int]() |
| 700 |
for n := 0; n < size; n++ { |
| 701 |
m.Put(n, n) |
| 702 |
} |
| 703 |
b.StartTimer() |
| 704 |
benchmarkRemove(b, m, size) |
| 705 |
} |
| 706 |
|
| 707 |
func BenchmarkTreeMapRemove10000(b *testing.B) { |
| 708 |
b.StopTimer() |
| 709 |
size := 10000 |
| 710 |
m := New[int, int]() |
| 711 |
for n := 0; n < size; n++ { |
| 712 |
m.Put(n, n) |
| 713 |
} |
| 714 |
b.StartTimer() |
| 715 |
benchmarkRemove(b, m, size) |
| 716 |
} |
| 717 |
|
| 718 |
func BenchmarkTreeMapRemove100000(b *testing.B) { |
| 719 |
b.StopTimer() |
| 720 |
size := 100000 |
| 721 |
m := New[int, int]() |
| 722 |
for n := 0; n < size; n++ { |
| 723 |
m.Put(n, n) |
| 724 |
} |
| 725 |
b.StartTimer() |
| 726 |
benchmarkRemove(b, m, size) |
| 727 |
} |