| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package treemap |
| 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 TestMapMin(t *testing.T) { |
| 54 |
m := New[int, string]() |
| 55 |
|
| 56 |
if k, v, ok := m.Min(); k != 0 || v != "" || ok { |
| 57 |
t.Errorf("Got %v->%v->%v expected %v->%v-%v", k, v, ok, 0, "", false) |
| 58 |
} |
| 59 |
|
| 60 |
m.Put(5, "e") |
| 61 |
m.Put(6, "f") |
| 62 |
m.Put(7, "g") |
| 63 |
m.Put(3, "c") |
| 64 |
m.Put(4, "d") |
| 65 |
m.Put(1, "x") |
| 66 |
m.Put(2, "b") |
| 67 |
m.Put(1, "a") |
| 68 |
|
| 69 |
actualKey, actualValue, actualOk := m.Min() |
| 70 |
expectedKey, expectedValue, expectedOk := 1, "a", true |
| 71 |
if actualKey != expectedKey { |
| 72 |
t.Errorf("Got %v expected %v", actualKey, expectedKey) |
| 73 |
} |
| 74 |
if actualValue != expectedValue { |
| 75 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 76 |
} |
| 77 |
if actualOk != expectedOk { |
| 78 |
t.Errorf("Got %v expected %v", actualOk, expectedOk) |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
func TestMapMax(t *testing.T) { |
| 83 |
m := New[int, string]() |
| 84 |
|
| 85 |
if k, v, ok := m.Max(); k != 0 || v != "" || ok { |
| 86 |
t.Errorf("Got %v->%v->%v expected %v->%v-%v", k, v, ok, 0, "", false) |
| 87 |
} |
| 88 |
|
| 89 |
m.Put(5, "e") |
| 90 |
m.Put(6, "f") |
| 91 |
m.Put(7, "g") |
| 92 |
m.Put(3, "c") |
| 93 |
m.Put(4, "d") |
| 94 |
m.Put(1, "x") |
| 95 |
m.Put(2, "b") |
| 96 |
m.Put(1, "a") |
| 97 |
|
| 98 |
actualKey, actualValue, actualOk := m.Max() |
| 99 |
expectedKey, expectedValue, expectedOk := 7, "g", true |
| 100 |
if actualKey != expectedKey { |
| 101 |
t.Errorf("Got %v expected %v", actualKey, expectedKey) |
| 102 |
} |
| 103 |
if actualValue != expectedValue { |
| 104 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 105 |
} |
| 106 |
if actualOk != expectedOk { |
| 107 |
t.Errorf("Got %v expected %v", actualOk, expectedOk) |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
func TestMapClear(t *testing.T) { |
| 112 |
m := New[int, string]() |
| 113 |
m.Put(5, "e") |
| 114 |
m.Put(6, "f") |
| 115 |
m.Put(7, "g") |
| 116 |
m.Put(3, "c") |
| 117 |
if actualValue, expectedValue := m.Size(), 4; actualValue != expectedValue { |
| 118 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 119 |
} |
| 120 |
m.Clear() |
| 121 |
if actualValue, expectedValue := m.Size(), 0; actualValue != expectedValue { |
| 122 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
func TestMapRemove(t *testing.T) { |
| 127 |
m := New[int, string]() |
| 128 |
m.Put(5, "e") |
| 129 |
m.Put(6, "f") |
| 130 |
m.Put(7, "g") |
| 131 |
m.Put(3, "c") |
| 132 |
m.Put(4, "d") |
| 133 |
m.Put(1, "x") |
| 134 |
m.Put(2, "b") |
| 135 |
m.Put(1, "a") |
| 136 |
|
| 137 |
m.Remove(5) |
| 138 |
m.Remove(6) |
| 139 |
m.Remove(7) |
| 140 |
m.Remove(8) |
| 141 |
m.Remove(5) |
| 142 |
|
| 143 |
testutils.SameElements(t, m.Keys(), []int{1, 2, 3, 4}) |
| 144 |
testutils.SameElements(t, m.Values(), []string{"a", "b", "c", "d"}) |
| 145 |
|
| 146 |
if actualValue := m.Size(); actualValue != 4 { |
| 147 |
t.Errorf("Got %v expected %v", actualValue, 4) |
| 148 |
} |
| 149 |
|
| 150 |
tests2 := [][]interface{}{ |
| 151 |
{1, "a", true}, |
| 152 |
{2, "b", true}, |
| 153 |
{3, "c", true}, |
| 154 |
{4, "d", true}, |
| 155 |
{5, "", false}, |
| 156 |
{6, "", false}, |
| 157 |
{7, "", false}, |
| 158 |
{8, "", false}, |
| 159 |
} |
| 160 |
|
| 161 |
for _, test := range tests2 { |
| 162 |
actualValue, actualFound := m.Get(test[0].(int)) |
| 163 |
if actualValue != test[1] || actualFound != test[2] { |
| 164 |
t.Errorf("Got %v expected %v", actualValue, test[1]) |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
m.Remove(1) |
| 169 |
m.Remove(4) |
| 170 |
m.Remove(2) |
| 171 |
m.Remove(3) |
| 172 |
m.Remove(2) |
| 173 |
m.Remove(2) |
| 174 |
|
| 175 |
testutils.SameElements(t, m.Keys(), nil) |
| 176 |
testutils.SameElements(t, m.Values(), nil) |
| 177 |
if actualValue := m.Size(); actualValue != 0 { |
| 178 |
t.Errorf("Got %v expected %v", actualValue, 0) |
| 179 |
} |
| 180 |
if actualValue := m.Empty(); actualValue != true { |
| 181 |
t.Errorf("Got %v expected %v", actualValue, true) |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
func TestMapFloor(t *testing.T) { |
| 186 |
m := New[int, string]() |
| 187 |
m.Put(7, "g") |
| 188 |
m.Put(3, "c") |
| 189 |
m.Put(1, "a") |
| 190 |
|
| 191 |
|
| 192 |
tests1 := [][]interface{}{ |
| 193 |
{-1, 0, "", false}, |
| 194 |
{0, 0, "", false}, |
| 195 |
{1, 1, "a", true}, |
| 196 |
{2, 1, "a", true}, |
| 197 |
{3, 3, "c", true}, |
| 198 |
{4, 3, "c", true}, |
| 199 |
{7, 7, "g", true}, |
| 200 |
{8, 7, "g", true}, |
| 201 |
} |
| 202 |
|
| 203 |
for _, test := range tests1 { |
| 204 |
|
| 205 |
actualKey, actualValue, actualOk := m.Floor(test[0].(int)) |
| 206 |
if actualKey != test[1] || actualValue != test[2] || actualOk != test[3] { |
| 207 |
t.Errorf("Got %v, %v, %v, expected %v, %v, %v", actualKey, actualValue, actualOk, test[1], test[2], test[3]) |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
func TestMapCeiling(t *testing.T) { |
| 213 |
m := New[int, string]() |
| 214 |
m.Put(7, "g") |
| 215 |
m.Put(3, "c") |
| 216 |
m.Put(1, "a") |
| 217 |
|
| 218 |
|
| 219 |
tests1 := [][]interface{}{ |
| 220 |
{-1, 1, "a", true}, |
| 221 |
{0, 1, "a", true}, |
| 222 |
{1, 1, "a", true}, |
| 223 |
{2, 3, "c", true}, |
| 224 |
{3, 3, "c", true}, |
| 225 |
{4, 7, "g", true}, |
| 226 |
{7, 7, "g", true}, |
| 227 |
{8, 0, "", false}, |
| 228 |
} |
| 229 |
|
| 230 |
for _, test := range tests1 { |
| 231 |
|
| 232 |
actualKey, actualValue, actualOk := m.Ceiling(test[0].(int)) |
| 233 |
if actualKey != test[1] || actualValue != test[2] || actualOk != test[3] { |
| 234 |
t.Errorf("Got %v, %v, %v, expected %v, %v, %v", actualKey, actualValue, actualOk, test[1], test[2], test[3]) |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
func TestMapEach(t *testing.T) { |
| 240 |
m := New[string, int]() |
| 241 |
m.Put("c", 3) |
| 242 |
m.Put("a", 1) |
| 243 |
m.Put("b", 2) |
| 244 |
count := 0 |
| 245 |
m.Each(func(key string, value int) { |
| 246 |
count++ |
| 247 |
if actualValue, expectedValue := count, value; actualValue != expectedValue { |
| 248 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 249 |
} |
| 250 |
switch value { |
| 251 |
case 1: |
| 252 |
if actualValue, expectedValue := key, "a"; actualValue != expectedValue { |
| 253 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 254 |
} |
| 255 |
case 2: |
| 256 |
if actualValue, expectedValue := key, "b"; actualValue != expectedValue { |
| 257 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 258 |
} |
| 259 |
case 3: |
| 260 |
if actualValue, expectedValue := key, "c"; actualValue != expectedValue { |
| 261 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 262 |
} |
| 263 |
default: |
| 264 |
t.Errorf("Too many") |
| 265 |
} |
| 266 |
}) |
| 267 |
} |
| 268 |
|
| 269 |
func TestMapMap(t *testing.T) { |
| 270 |
m := New[string, int]() |
| 271 |
m.Put("c", 3) |
| 272 |
m.Put("a", 1) |
| 273 |
m.Put("b", 2) |
| 274 |
mappedMap := m.Map(func(key1 string, value1 int) (key2 string, value2 int) { |
| 275 |
return key1, value1 * value1 |
| 276 |
}) |
| 277 |
if actualValue, _ := mappedMap.Get("a"); actualValue != 1 { |
| 278 |
t.Errorf("Got %v expected %v", actualValue, "mapped: a") |
| 279 |
} |
| 280 |
if actualValue, _ := mappedMap.Get("b"); actualValue != 4 { |
| 281 |
t.Errorf("Got %v expected %v", actualValue, "mapped: b") |
| 282 |
} |
| 283 |
if actualValue, _ := mappedMap.Get("c"); actualValue != 9 { |
| 284 |
t.Errorf("Got %v expected %v", actualValue, "mapped: c") |
| 285 |
} |
| 286 |
if mappedMap.Size() != 3 { |
| 287 |
t.Errorf("Got %v expected %v", mappedMap.Size(), 3) |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
func TestMapSelect(t *testing.T) { |
| 292 |
m := New[string, int]() |
| 293 |
m.Put("c", 3) |
| 294 |
m.Put("a", 1) |
| 295 |
m.Put("b", 2) |
| 296 |
selectedMap := m.Select(func(key string, value int) bool { |
| 297 |
return key >= "a" && key <= "b" |
| 298 |
}) |
| 299 |
if actualValue, _ := selectedMap.Get("a"); actualValue != 1 { |
| 300 |
t.Errorf("Got %v expected %v", actualValue, "value: a") |
| 301 |
} |
| 302 |
if actualValue, _ := selectedMap.Get("b"); actualValue != 2 { |
| 303 |
t.Errorf("Got %v expected %v", actualValue, "value: b") |
| 304 |
} |
| 305 |
if selectedMap.Size() != 2 { |
| 306 |
t.Errorf("Got %v expected %v", selectedMap.Size(), 2) |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
func TestMapAny(t *testing.T) { |
| 311 |
m := New[string, int]() |
| 312 |
m.Put("c", 3) |
| 313 |
m.Put("a", 1) |
| 314 |
m.Put("b", 2) |
| 315 |
any := m.Any(func(key string, value int) bool { |
| 316 |
return value == 3 |
| 317 |
}) |
| 318 |
if any != true { |
| 319 |
t.Errorf("Got %v expected %v", any, true) |
| 320 |
} |
| 321 |
any = m.Any(func(key string, value int) bool { |
| 322 |
return value == 4 |
| 323 |
}) |
| 324 |
if any != false { |
| 325 |
t.Errorf("Got %v expected %v", any, false) |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
func TestMapAll(t *testing.T) { |
| 330 |
m := New[string, int]() |
| 331 |
m.Put("c", 3) |
| 332 |
m.Put("a", 1) |
| 333 |
m.Put("b", 2) |
| 334 |
all := m.All(func(key string, value int) bool { |
| 335 |
return key >= "a" && key <= "c" |
| 336 |
}) |
| 337 |
if all != true { |
| 338 |
t.Errorf("Got %v expected %v", all, true) |
| 339 |
} |
| 340 |
all = m.All(func(key string, value int) bool { |
| 341 |
return key >= "a" && key <= "b" |
| 342 |
}) |
| 343 |
if all != false { |
| 344 |
t.Errorf("Got %v expected %v", all, false) |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
func TestMapFind(t *testing.T) { |
| 349 |
m := New[string, int]() |
| 350 |
m.Put("c", 3) |
| 351 |
m.Put("a", 1) |
| 352 |
m.Put("b", 2) |
| 353 |
foundKey, foundValue := m.Find(func(key string, value int) bool { |
| 354 |
return key == "c" |
| 355 |
}) |
| 356 |
if foundKey != "c" || foundValue != 3 { |
| 357 |
t.Errorf("Got %v -> %v expected %v -> %v", foundKey, foundValue, "c", 3) |
| 358 |
} |
| 359 |
foundKey, foundValue = m.Find(func(key string, value int) bool { |
| 360 |
return key == "x" |
| 361 |
}) |
| 362 |
if foundKey != "" || foundValue != 0 { |
| 363 |
t.Errorf("Got %v at %v expected %v at %v", foundValue, foundKey, nil, nil) |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
func TestMapChaining(t *testing.T) { |
| 368 |
m := New[string, int]() |
| 369 |
m.Put("c", 3) |
| 370 |
m.Put("a", 1) |
| 371 |
m.Put("b", 2) |
| 372 |
chainedMap := m.Select(func(key string, value int) bool { |
| 373 |
return value > 1 |
| 374 |
}).Map(func(key string, value int) (string, int) { |
| 375 |
return key + key, value * value |
| 376 |
}) |
| 377 |
if actualValue := chainedMap.Size(); actualValue != 2 { |
| 378 |
t.Errorf("Got %v expected %v", actualValue, 2) |
| 379 |
} |
| 380 |
if actualValue, found := chainedMap.Get("aa"); actualValue != 0 || found { |
| 381 |
t.Errorf("Got %v expected %v", actualValue, nil) |
| 382 |
} |
| 383 |
if actualValue, found := chainedMap.Get("bb"); actualValue != 4 || !found { |
| 384 |
t.Errorf("Got %v expected %v", actualValue, 4) |
| 385 |
} |
| 386 |
if actualValue, found := chainedMap.Get("cc"); actualValue != 9 || !found { |
| 387 |
t.Errorf("Got %v expected %v", actualValue, 9) |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
func TestMapIteratorNextOnEmpty(t *testing.T) { |
| 392 |
m := New[string, int]() |
| 393 |
it := m.Iterator() |
| 394 |
it = m.Iterator() |
| 395 |
for it.Next() { |
| 396 |
t.Errorf("Shouldn't iterate on empty map") |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
func TestMapIteratorPrevOnEmpty(t *testing.T) { |
| 401 |
m := New[string, int]() |
| 402 |
it := m.Iterator() |
| 403 |
it = m.Iterator() |
| 404 |
for it.Prev() { |
| 405 |
t.Errorf("Shouldn't iterate on empty map") |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
func TestMapIteratorNext(t *testing.T) { |
| 410 |
m := New[string, int]() |
| 411 |
m.Put("c", 3) |
| 412 |
m.Put("a", 1) |
| 413 |
m.Put("b", 2) |
| 414 |
|
| 415 |
it := m.Iterator() |
| 416 |
count := 0 |
| 417 |
for it.Next() { |
| 418 |
count++ |
| 419 |
key := it.Key() |
| 420 |
value := it.Value() |
| 421 |
switch key { |
| 422 |
case "a": |
| 423 |
if actualValue, expectedValue := value, 1; actualValue != expectedValue { |
| 424 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 425 |
} |
| 426 |
case "b": |
| 427 |
if actualValue, expectedValue := value, 2; actualValue != expectedValue { |
| 428 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 429 |
} |
| 430 |
case "c": |
| 431 |
if actualValue, expectedValue := value, 3; actualValue != expectedValue { |
| 432 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 433 |
} |
| 434 |
default: |
| 435 |
t.Errorf("Too many") |
| 436 |
} |
| 437 |
if actualValue, expectedValue := value, count; actualValue != expectedValue { |
| 438 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 439 |
} |
| 440 |
} |
| 441 |
if actualValue, expectedValue := count, 3; actualValue != expectedValue { |
| 442 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
func TestMapIteratorPrev(t *testing.T) { |
| 447 |
m := New[string, int]() |
| 448 |
m.Put("c", 3) |
| 449 |
m.Put("a", 1) |
| 450 |
m.Put("b", 2) |
| 451 |
|
| 452 |
it := m.Iterator() |
| 453 |
for it.Next() { |
| 454 |
} |
| 455 |
countDown := m.Size() |
| 456 |
for it.Prev() { |
| 457 |
key := it.Key() |
| 458 |
value := it.Value() |
| 459 |
switch key { |
| 460 |
case "a": |
| 461 |
if actualValue, expectedValue := value, 1; actualValue != expectedValue { |
| 462 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 463 |
} |
| 464 |
case "b": |
| 465 |
if actualValue, expectedValue := value, 2; actualValue != expectedValue { |
| 466 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 467 |
} |
| 468 |
case "c": |
| 469 |
if actualValue, expectedValue := value, 3; actualValue != expectedValue { |
| 470 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 471 |
} |
| 472 |
default: |
| 473 |
t.Errorf("Too many") |
| 474 |
} |
| 475 |
if actualValue, expectedValue := value, countDown; actualValue != expectedValue { |
| 476 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 477 |
} |
| 478 |
countDown-- |
| 479 |
} |
| 480 |
if actualValue, expectedValue := countDown, 0; actualValue != expectedValue { |
| 481 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
func TestMapIteratorBegin(t *testing.T) { |
| 486 |
m := New[int, string]() |
| 487 |
it := m.Iterator() |
| 488 |
it.Begin() |
| 489 |
m.Put(3, "c") |
| 490 |
m.Put(1, "a") |
| 491 |
m.Put(2, "b") |
| 492 |
for it.Next() { |
| 493 |
} |
| 494 |
it.Begin() |
| 495 |
it.Next() |
| 496 |
if key, value := it.Key(), it.Value(); key != 1 || value != "a" { |
| 497 |
t.Errorf("Got %v,%v expected %v,%v", key, value, 1, "a") |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
func TestMapIteratorEnd(t *testing.T) { |
| 502 |
m := New[int, string]() |
| 503 |
it := m.Iterator() |
| 504 |
m.Put(3, "c") |
| 505 |
m.Put(1, "a") |
| 506 |
m.Put(2, "b") |
| 507 |
it.End() |
| 508 |
it.Prev() |
| 509 |
if key, value := it.Key(), it.Value(); key != 3 || value != "c" { |
| 510 |
t.Errorf("Got %v,%v expected %v,%v", key, value, 3, "c") |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
func TestMapIteratorFirst(t *testing.T) { |
| 515 |
m := New[int, string]() |
| 516 |
m.Put(3, "c") |
| 517 |
m.Put(1, "a") |
| 518 |
m.Put(2, "b") |
| 519 |
it := m.Iterator() |
| 520 |
if actualValue, expectedValue := it.First(), true; actualValue != expectedValue { |
| 521 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 522 |
} |
| 523 |
if key, value := it.Key(), it.Value(); key != 1 || value != "a" { |
| 524 |
t.Errorf("Got %v,%v expected %v,%v", key, value, 1, "a") |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
func TestMapIteratorLast(t *testing.T) { |
| 529 |
m := New[int, string]() |
| 530 |
m.Put(3, "c") |
| 531 |
m.Put(1, "a") |
| 532 |
m.Put(2, "b") |
| 533 |
it := m.Iterator() |
| 534 |
if actualValue, expectedValue := it.Last(), true; actualValue != expectedValue { |
| 535 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 536 |
} |
| 537 |
if key, value := it.Key(), it.Value(); key != 3 || value != "c" { |
| 538 |
t.Errorf("Got %v,%v expected %v,%v", key, value, 3, "c") |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
func TestMapIteratorNextTo(t *testing.T) { |
| 543 |
|
| 544 |
seek := func(index int, value string) bool { |
| 545 |
return strings.HasSuffix(value, "b") |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
{ |
| 550 |
m := New[int, string]() |
| 551 |
it := m.Iterator() |
| 552 |
for it.NextTo(seek) { |
| 553 |
t.Errorf("Shouldn't iterate on empty map") |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
{ |
| 559 |
m := New[int, string]() |
| 560 |
m.Put(0, "xx") |
| 561 |
m.Put(1, "yy") |
| 562 |
it := m.Iterator() |
| 563 |
for it.NextTo(seek) { |
| 564 |
t.Errorf("Shouldn't iterate on empty map") |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
{ |
| 570 |
m := New[int, string]() |
| 571 |
m.Put(0, "aa") |
| 572 |
m.Put(1, "bb") |
| 573 |
m.Put(2, "cc") |
| 574 |
it := m.Iterator() |
| 575 |
it.Begin() |
| 576 |
if !it.NextTo(seek) { |
| 577 |
t.Errorf("Shouldn't iterate on empty map") |
| 578 |
} |
| 579 |
if index, value := it.Key(), it.Value(); index != 1 || value != "bb" { |
| 580 |
t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb") |
| 581 |
} |
| 582 |
if !it.Next() { |
| 583 |
t.Errorf("Should go to first element") |
| 584 |
} |
| 585 |
if index, value := it.Key(), it.Value(); index != 2 || value != "cc" { |
| 586 |
t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "cc") |
| 587 |
} |
| 588 |
if it.Next() { |
| 589 |
t.Errorf("Should not go past last element") |
| 590 |
} |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
func TestMapIteratorPrevTo(t *testing.T) { |
| 595 |
|
| 596 |
seek := func(index int, value string) bool { |
| 597 |
return strings.HasSuffix(value, "b") |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
{ |
| 602 |
m := New[int, string]() |
| 603 |
it := m.Iterator() |
| 604 |
it.End() |
| 605 |
for it.PrevTo(seek) { |
| 606 |
t.Errorf("Shouldn't iterate on empty map") |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
|
| 611 |
{ |
| 612 |
m := New[int, string]() |
| 613 |
m.Put(0, "xx") |
| 614 |
m.Put(1, "yy") |
| 615 |
it := m.Iterator() |
| 616 |
it.End() |
| 617 |
for it.PrevTo(seek) { |
| 618 |
t.Errorf("Shouldn't iterate on empty map") |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
|
| 623 |
{ |
| 624 |
m := New[int, string]() |
| 625 |
m.Put(0, "aa") |
| 626 |
m.Put(1, "bb") |
| 627 |
m.Put(2, "cc") |
| 628 |
it := m.Iterator() |
| 629 |
it.End() |
| 630 |
if !it.PrevTo(seek) { |
| 631 |
t.Errorf("Shouldn't iterate on empty map") |
| 632 |
} |
| 633 |
if index, value := it.Key(), it.Value(); index != 1 || value != "bb" { |
| 634 |
t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb") |
| 635 |
} |
| 636 |
if !it.Prev() { |
| 637 |
t.Errorf("Should go to first element") |
| 638 |
} |
| 639 |
if index, value := it.Key(), it.Value(); index != 0 || value != "aa" { |
| 640 |
t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "aa") |
| 641 |
} |
| 642 |
if it.Prev() { |
| 643 |
t.Errorf("Should not go before first element") |
| 644 |
} |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
func TestMapSerialization(t *testing.T) { |
| 649 |
for i := 0; i < 10; i++ { |
| 650 |
original := New[string, string]() |
| 651 |
original.Put("d", "4") |
| 652 |
original.Put("e", "5") |
| 653 |
original.Put("c", "3") |
| 654 |
original.Put("b", "2") |
| 655 |
original.Put("a", "1") |
| 656 |
|
| 657 |
assertSerialization(original, "A", t) |
| 658 |
|
| 659 |
serialized, err := original.ToJSON() |
| 660 |
if err != nil { |
| 661 |
t.Errorf("Got error %v", err) |
| 662 |
} |
| 663 |
assertSerialization(original, "B", t) |
| 664 |
|
| 665 |
deserialized := New[string, string]() |
| 666 |
err = deserialized.FromJSON(serialized) |
| 667 |
if err != nil { |
| 668 |
t.Errorf("Got error %v", err) |
| 669 |
} |
| 670 |
assertSerialization(deserialized, "C", t) |
| 671 |
} |
| 672 |
|
| 673 |
m := New[string, float64]() |
| 674 |
m.Put("a", 1.0) |
| 675 |
m.Put("b", 2.0) |
| 676 |
m.Put("c", 3.0) |
| 677 |
|
| 678 |
_, err := json.Marshal([]interface{}{"a", "b", "c", m}) |
| 679 |
if err != nil { |
| 680 |
t.Errorf("Got error %v", err) |
| 681 |
} |
| 682 |
|
| 683 |
err = json.Unmarshal([]byte(`{"a":1,"b":2}`), &m) |
| 684 |
if err != nil { |
| 685 |
t.Errorf("Got error %v", err) |
| 686 |
} |
| 687 |
} |
| 688 |
|
| 689 |
func TestMapString(t *testing.T) { |
| 690 |
c := New[string, int]() |
| 691 |
c.Put("a", 1) |
| 692 |
if !strings.HasPrefix(c.String(), "TreeMap") { |
| 693 |
t.Errorf("String should start with container name") |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
|
| 698 |
func assertSerialization(m *Map[string, string], txt string, t *testing.T) { |
| 699 |
if actualValue := m.Keys(); false || |
| 700 |
actualValue[0] != "a" || |
| 701 |
actualValue[1] != "b" || |
| 702 |
actualValue[2] != "c" || |
| 703 |
actualValue[3] != "d" || |
| 704 |
actualValue[4] != "e" { |
| 705 |
t.Errorf("[%s] Got %v expected %v", txt, actualValue, "[a,b,c,d,e]") |
| 706 |
} |
| 707 |
if actualValue := m.Values(); false || |
| 708 |
actualValue[0] != "1" || |
| 709 |
actualValue[1] != "2" || |
| 710 |
actualValue[2] != "3" || |
| 711 |
actualValue[3] != "4" || |
| 712 |
actualValue[4] != "5" { |
| 713 |
t.Errorf("[%s] Got %v expected %v", txt, actualValue, "[1,2,3,4,5]") |
| 714 |
} |
| 715 |
if actualValue, expectedValue := m.Size(), 5; actualValue != expectedValue { |
| 716 |
t.Errorf("[%s] Got %v expected %v", txt, actualValue, expectedValue) |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
func benchmarkGet(b *testing.B, m *Map[int, struct{}], size int) { |
| 721 |
for i := 0; i < b.N; i++ { |
| 722 |
for n := 0; n < size; n++ { |
| 723 |
m.Get(n) |
| 724 |
} |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
func benchmarkPut(b *testing.B, m *Map[int, struct{}], size int) { |
| 729 |
for i := 0; i < b.N; i++ { |
| 730 |
for n := 0; n < size; n++ { |
| 731 |
m.Put(n, struct{}{}) |
| 732 |
} |
| 733 |
} |
| 734 |
} |
| 735 |
|
| 736 |
func benchmarkRemove(b *testing.B, m *Map[int, struct{}], size int) { |
| 737 |
for i := 0; i < b.N; i++ { |
| 738 |
for n := 0; n < size; n++ { |
| 739 |
m.Remove(n) |
| 740 |
} |
| 741 |
} |
| 742 |
} |
| 743 |
|
| 744 |
func BenchmarkTreeMapGet100(b *testing.B) { |
| 745 |
b.StopTimer() |
| 746 |
size := 100 |
| 747 |
m := New[int, struct{}]() |
| 748 |
for n := 0; n < size; n++ { |
| 749 |
m.Put(n, struct{}{}) |
| 750 |
} |
| 751 |
b.StartTimer() |
| 752 |
benchmarkGet(b, m, size) |
| 753 |
} |
| 754 |
|
| 755 |
func BenchmarkTreeMapGet1000(b *testing.B) { |
| 756 |
b.StopTimer() |
| 757 |
size := 1000 |
| 758 |
m := New[int, struct{}]() |
| 759 |
for n := 0; n < size; n++ { |
| 760 |
m.Put(n, struct{}{}) |
| 761 |
} |
| 762 |
b.StartTimer() |
| 763 |
benchmarkGet(b, m, size) |
| 764 |
} |
| 765 |
|
| 766 |
func BenchmarkTreeMapGet10000(b *testing.B) { |
| 767 |
b.StopTimer() |
| 768 |
size := 10000 |
| 769 |
m := New[int, struct{}]() |
| 770 |
for n := 0; n < size; n++ { |
| 771 |
m.Put(n, struct{}{}) |
| 772 |
} |
| 773 |
b.StartTimer() |
| 774 |
benchmarkGet(b, m, size) |
| 775 |
} |
| 776 |
|
| 777 |
func BenchmarkTreeMapGet100000(b *testing.B) { |
| 778 |
b.StopTimer() |
| 779 |
size := 100000 |
| 780 |
m := New[int, struct{}]() |
| 781 |
for n := 0; n < size; n++ { |
| 782 |
m.Put(n, struct{}{}) |
| 783 |
} |
| 784 |
b.StartTimer() |
| 785 |
benchmarkGet(b, m, size) |
| 786 |
} |
| 787 |
|
| 788 |
func BenchmarkTreeMapPut100(b *testing.B) { |
| 789 |
b.StopTimer() |
| 790 |
size := 100 |
| 791 |
m := New[int, struct{}]() |
| 792 |
b.StartTimer() |
| 793 |
benchmarkPut(b, m, size) |
| 794 |
} |
| 795 |
|
| 796 |
func BenchmarkTreeMapPut1000(b *testing.B) { |
| 797 |
b.StopTimer() |
| 798 |
size := 1000 |
| 799 |
m := New[int, struct{}]() |
| 800 |
for n := 0; n < size; n++ { |
| 801 |
m.Put(n, struct{}{}) |
| 802 |
} |
| 803 |
b.StartTimer() |
| 804 |
benchmarkPut(b, m, size) |
| 805 |
} |
| 806 |
|
| 807 |
func BenchmarkTreeMapPut10000(b *testing.B) { |
| 808 |
b.StopTimer() |
| 809 |
size := 10000 |
| 810 |
m := New[int, struct{}]() |
| 811 |
for n := 0; n < size; n++ { |
| 812 |
m.Put(n, struct{}{}) |
| 813 |
} |
| 814 |
b.StartTimer() |
| 815 |
benchmarkPut(b, m, size) |
| 816 |
} |
| 817 |
|
| 818 |
func BenchmarkTreeMapPut100000(b *testing.B) { |
| 819 |
b.StopTimer() |
| 820 |
size := 100000 |
| 821 |
m := New[int, struct{}]() |
| 822 |
for n := 0; n < size; n++ { |
| 823 |
m.Put(n, struct{}{}) |
| 824 |
} |
| 825 |
b.StartTimer() |
| 826 |
benchmarkPut(b, m, size) |
| 827 |
} |
| 828 |
|
| 829 |
func BenchmarkTreeMapRemove100(b *testing.B) { |
| 830 |
b.StopTimer() |
| 831 |
size := 100 |
| 832 |
m := New[int, struct{}]() |
| 833 |
for n := 0; n < size; n++ { |
| 834 |
m.Put(n, struct{}{}) |
| 835 |
} |
| 836 |
b.StartTimer() |
| 837 |
benchmarkRemove(b, m, size) |
| 838 |
} |
| 839 |
|
| 840 |
func BenchmarkTreeMapRemove1000(b *testing.B) { |
| 841 |
b.StopTimer() |
| 842 |
size := 1000 |
| 843 |
m := New[int, struct{}]() |
| 844 |
for n := 0; n < size; n++ { |
| 845 |
m.Put(n, struct{}{}) |
| 846 |
} |
| 847 |
b.StartTimer() |
| 848 |
benchmarkRemove(b, m, size) |
| 849 |
} |
| 850 |
|
| 851 |
func BenchmarkTreeMapRemove10000(b *testing.B) { |
| 852 |
b.StopTimer() |
| 853 |
size := 10000 |
| 854 |
m := New[int, struct{}]() |
| 855 |
for n := 0; n < size; n++ { |
| 856 |
m.Put(n, struct{}{}) |
| 857 |
} |
| 858 |
b.StartTimer() |
| 859 |
benchmarkRemove(b, m, size) |
| 860 |
} |
| 861 |
|
| 862 |
func BenchmarkTreeMapRemove100000(b *testing.B) { |
| 863 |
b.StopTimer() |
| 864 |
size := 100000 |
| 865 |
m := New[int, struct{}]() |
| 866 |
for n := 0; n < size; n++ { |
| 867 |
m.Put(n, struct{}{}) |
| 868 |
} |
| 869 |
b.StartTimer() |
| 870 |
benchmarkRemove(b, m, size) |
| 871 |
} |