github.com/mymmsc/gox@v1.3.33/util/arraylist/arraylist_test.go (about) 1 // Copyright (c) 2015, Emir Pasic. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package arraylist 6 7 import ( 8 "fmt" 9 "github.com/mymmsc/gox/util" 10 "testing" 11 ) 12 13 func TestListNew(t *testing.T) { 14 list1 := New() 15 16 if actualValue := list1.Empty(); actualValue != true { 17 t.Errorf("Got %v expected %v", actualValue, true) 18 } 19 20 list2 := New(1, "b") 21 22 if actualValue := list2.Size(); actualValue != 2 { 23 t.Errorf("Got %v expected %v", actualValue, 2) 24 } 25 26 if actualValue, ok := list2.Get(0); actualValue != 1 || !ok { 27 t.Errorf("Got %v expected %v", actualValue, 1) 28 } 29 30 if actualValue, ok := list2.Get(1); actualValue != "b" || !ok { 31 t.Errorf("Got %v expected %v", actualValue, "b") 32 } 33 34 if actualValue, ok := list2.Get(2); actualValue != nil || ok { 35 t.Errorf("Got %v expected %v", actualValue, nil) 36 } 37 } 38 39 func TestListAdd(t *testing.T) { 40 list := New() 41 list.Add("a") 42 list.Add("b", "c") 43 if actualValue := list.Empty(); actualValue != false { 44 t.Errorf("Got %v expected %v", actualValue, false) 45 } 46 if actualValue := list.Size(); actualValue != 3 { 47 t.Errorf("Got %v expected %v", actualValue, 3) 48 } 49 if actualValue, ok := list.Get(2); actualValue != "c" || !ok { 50 t.Errorf("Got %v expected %v", actualValue, "c") 51 } 52 } 53 54 func TestListIndexOf(t *testing.T) { 55 list := New() 56 57 expectedIndex := -1 58 if index := list.IndexOf("a"); index != expectedIndex { 59 t.Errorf("Got %v expected %v", index, expectedIndex) 60 } 61 62 list.Add("a") 63 list.Add("b", "c") 64 65 expectedIndex = 0 66 if index := list.IndexOf("a"); index != expectedIndex { 67 t.Errorf("Got %v expected %v", index, expectedIndex) 68 } 69 70 expectedIndex = 1 71 if index := list.IndexOf("b"); index != expectedIndex { 72 t.Errorf("Got %v expected %v", index, expectedIndex) 73 } 74 75 expectedIndex = 2 76 if index := list.IndexOf("c"); index != expectedIndex { 77 t.Errorf("Got %v expected %v", index, expectedIndex) 78 } 79 } 80 81 func TestListRemove(t *testing.T) { 82 list := New() 83 list.Add("a") 84 list.Add("b", "c") 85 list.Remove(2) 86 if actualValue, ok := list.Get(2); actualValue != nil || ok { 87 t.Errorf("Got %v expected %v", actualValue, nil) 88 } 89 list.Remove(1) 90 list.Remove(0) 91 list.Remove(0) // no effect 92 if actualValue := list.Empty(); actualValue != true { 93 t.Errorf("Got %v expected %v", actualValue, true) 94 } 95 if actualValue := list.Size(); actualValue != 0 { 96 t.Errorf("Got %v expected %v", actualValue, 0) 97 } 98 } 99 100 func TestListGet(t *testing.T) { 101 list := New() 102 list.Add("a") 103 list.Add("b", "c") 104 if actualValue, ok := list.Get(0); actualValue != "a" || !ok { 105 t.Errorf("Got %v expected %v", actualValue, "a") 106 } 107 if actualValue, ok := list.Get(1); actualValue != "b" || !ok { 108 t.Errorf("Got %v expected %v", actualValue, "b") 109 } 110 if actualValue, ok := list.Get(2); actualValue != "c" || !ok { 111 t.Errorf("Got %v expected %v", actualValue, "c") 112 } 113 if actualValue, ok := list.Get(3); actualValue != nil || ok { 114 t.Errorf("Got %v expected %v", actualValue, nil) 115 } 116 list.Remove(0) 117 if actualValue, ok := list.Get(0); actualValue != "b" || !ok { 118 t.Errorf("Got %v expected %v", actualValue, "b") 119 } 120 } 121 122 func TestListSwap(t *testing.T) { 123 list := New() 124 list.Add("a") 125 list.Add("b", "c") 126 list.Swap(0, 1) 127 if actualValue, ok := list.Get(0); actualValue != "b" || !ok { 128 t.Errorf("Got %v expected %v", actualValue, "c") 129 } 130 } 131 132 func TestListSort(t *testing.T) { 133 list := New() 134 list.Sort(util.StringComparator) 135 list.Add("e", "f", "g", "a", "b", "c", "d") 136 list.Sort(util.StringComparator) 137 for i := 1; i < list.Size(); i++ { 138 a, _ := list.Get(i - 1) 139 b, _ := list.Get(i) 140 if a.(string) > b.(string) { 141 t.Errorf("Not sorted! %s > %s", a, b) 142 } 143 } 144 } 145 146 func TestListClear(t *testing.T) { 147 list := New() 148 list.Add("e", "f", "g", "a", "b", "c", "d") 149 list.Clear() 150 if actualValue := list.Empty(); actualValue != true { 151 t.Errorf("Got %v expected %v", actualValue, true) 152 } 153 if actualValue := list.Size(); actualValue != 0 { 154 t.Errorf("Got %v expected %v", actualValue, 0) 155 } 156 } 157 158 func TestListContains(t *testing.T) { 159 list := New() 160 list.Add("a") 161 list.Add("b", "c") 162 if actualValue := list.Contains("a"); actualValue != true { 163 t.Errorf("Got %v expected %v", actualValue, true) 164 } 165 if actualValue := list.Contains("a", "b", "c"); actualValue != true { 166 t.Errorf("Got %v expected %v", actualValue, true) 167 } 168 if actualValue := list.Contains("a", "b", "c", "d"); actualValue != false { 169 t.Errorf("Got %v expected %v", actualValue, false) 170 } 171 list.Clear() 172 if actualValue := list.Contains("a"); actualValue != false { 173 t.Errorf("Got %v expected %v", actualValue, false) 174 } 175 if actualValue := list.Contains("a", "b", "c"); actualValue != false { 176 t.Errorf("Got %v expected %v", actualValue, false) 177 } 178 } 179 180 func TestListValues(t *testing.T) { 181 list := New() 182 list.Add("a") 183 list.Add("b", "c") 184 if actualValue, expectedValue := fmt.Sprintf("%s%s%s", list.Values()...), "abc"; actualValue != expectedValue { 185 t.Errorf("Got %v expected %v", actualValue, expectedValue) 186 } 187 } 188 189 func TestListInsert(t *testing.T) { 190 list := New() 191 list.Insert(0, "b", "c") 192 list.Insert(0, "a") 193 list.Insert(10, "x") // ignore 194 if actualValue := list.Size(); actualValue != 3 { 195 t.Errorf("Got %v expected %v", actualValue, 3) 196 } 197 list.Insert(3, "d") // append 198 if actualValue := list.Size(); actualValue != 4 { 199 t.Errorf("Got %v expected %v", actualValue, 4) 200 } 201 if actualValue, expectedValue := fmt.Sprintf("%s%s%s%s", list.Values()...), "abcd"; actualValue != expectedValue { 202 t.Errorf("Got %v expected %v", actualValue, expectedValue) 203 } 204 } 205 206 func TestListSet(t *testing.T) { 207 list := New() 208 list.Set(0, "a") 209 list.Set(1, "b") 210 if actualValue := list.Size(); actualValue != 2 { 211 t.Errorf("Got %v expected %v", actualValue, 2) 212 } 213 list.Set(2, "c") // append 214 if actualValue := list.Size(); actualValue != 3 { 215 t.Errorf("Got %v expected %v", actualValue, 3) 216 } 217 list.Set(4, "d") // ignore 218 list.Set(1, "bb") // update 219 if actualValue := list.Size(); actualValue != 3 { 220 t.Errorf("Got %v expected %v", actualValue, 3) 221 } 222 if actualValue, expectedValue := fmt.Sprintf("%s%s%s", list.Values()...), "abbc"; actualValue != expectedValue { 223 t.Errorf("Got %v expected %v", actualValue, expectedValue) 224 } 225 } 226 227 func TestListEach(t *testing.T) { 228 list := New() 229 list.Add("a", "b", "c") 230 list.Each(func(index int, value interface{}) { 231 switch index { 232 case 0: 233 if actualValue, expectedValue := value, "a"; actualValue != expectedValue { 234 t.Errorf("Got %v expected %v", actualValue, expectedValue) 235 } 236 case 1: 237 if actualValue, expectedValue := value, "b"; actualValue != expectedValue { 238 t.Errorf("Got %v expected %v", actualValue, expectedValue) 239 } 240 case 2: 241 if actualValue, expectedValue := value, "c"; actualValue != expectedValue { 242 t.Errorf("Got %v expected %v", actualValue, expectedValue) 243 } 244 default: 245 t.Errorf("Too many") 246 } 247 }) 248 } 249 250 func TestListMap(t *testing.T) { 251 list := New() 252 list.Add("a", "b", "c") 253 mappedList := list.Map(func(index int, value interface{}) interface{} { 254 return "mapped: " + value.(string) 255 }) 256 if actualValue, _ := mappedList.Get(0); actualValue != "mapped: a" { 257 t.Errorf("Got %v expected %v", actualValue, "mapped: a") 258 } 259 if actualValue, _ := mappedList.Get(1); actualValue != "mapped: b" { 260 t.Errorf("Got %v expected %v", actualValue, "mapped: b") 261 } 262 if actualValue, _ := mappedList.Get(2); actualValue != "mapped: c" { 263 t.Errorf("Got %v expected %v", actualValue, "mapped: c") 264 } 265 if mappedList.Size() != 3 { 266 t.Errorf("Got %v expected %v", mappedList.Size(), 3) 267 } 268 } 269 270 func TestListSelect(t *testing.T) { 271 list := New() 272 list.Add("a", "b", "c") 273 selectedList := list.Select(func(index int, value interface{}) bool { 274 return value.(string) >= "a" && value.(string) <= "b" 275 }) 276 if actualValue, _ := selectedList.Get(0); actualValue != "a" { 277 t.Errorf("Got %v expected %v", actualValue, "value: a") 278 } 279 if actualValue, _ := selectedList.Get(1); actualValue != "b" { 280 t.Errorf("Got %v expected %v", actualValue, "value: b") 281 } 282 if selectedList.Size() != 2 { 283 t.Errorf("Got %v expected %v", selectedList.Size(), 3) 284 } 285 } 286 287 func TestListAny(t *testing.T) { 288 list := New() 289 list.Add("a", "b", "c") 290 any := list.Any(func(index int, value interface{}) bool { 291 return value.(string) == "c" 292 }) 293 if any != true { 294 t.Errorf("Got %v expected %v", any, true) 295 } 296 any = list.Any(func(index int, value interface{}) bool { 297 return value.(string) == "x" 298 }) 299 if any != false { 300 t.Errorf("Got %v expected %v", any, false) 301 } 302 } 303 func TestListAll(t *testing.T) { 304 list := New() 305 list.Add("a", "b", "c") 306 all := list.All(func(index int, value interface{}) bool { 307 return value.(string) >= "a" && value.(string) <= "c" 308 }) 309 if all != true { 310 t.Errorf("Got %v expected %v", all, true) 311 } 312 all = list.All(func(index int, value interface{}) bool { 313 return value.(string) >= "a" && value.(string) <= "b" 314 }) 315 if all != false { 316 t.Errorf("Got %v expected %v", all, false) 317 } 318 } 319 func TestListFind(t *testing.T) { 320 list := New() 321 list.Add("a", "b", "c") 322 foundIndex, foundValue := list.Find(func(index int, value interface{}) bool { 323 return value.(string) == "c" 324 }) 325 if foundValue != "c" || foundIndex != 2 { 326 t.Errorf("Got %v at %v expected %v at %v", foundValue, foundIndex, "c", 2) 327 } 328 foundIndex, foundValue = list.Find(func(index int, value interface{}) bool { 329 return value.(string) == "x" 330 }) 331 if foundValue != nil || foundIndex != -1 { 332 t.Errorf("Got %v at %v expected %v at %v", foundValue, foundIndex, nil, nil) 333 } 334 } 335 func TestListChaining(t *testing.T) { 336 list := New() 337 list.Add("a", "b", "c") 338 chainedList := list.Select(func(index int, value interface{}) bool { 339 return value.(string) > "a" 340 }).Map(func(index int, value interface{}) interface{} { 341 return value.(string) + value.(string) 342 }) 343 if chainedList.Size() != 2 { 344 t.Errorf("Got %v expected %v", chainedList.Size(), 2) 345 } 346 if actualValue, ok := chainedList.Get(0); actualValue != "bb" || !ok { 347 t.Errorf("Got %v expected %v", actualValue, "b") 348 } 349 if actualValue, ok := chainedList.Get(1); actualValue != "cc" || !ok { 350 t.Errorf("Got %v expected %v", actualValue, "c") 351 } 352 } 353 354 func TestListIteratorNextOnEmpty(t *testing.T) { 355 list := New() 356 it := list.Iterator() 357 for it.Next() { 358 t.Errorf("Shouldn't iterate on empty list") 359 } 360 } 361 362 func TestListIteratorNext(t *testing.T) { 363 list := New() 364 list.Add("a", "b", "c") 365 it := list.Iterator() 366 count := 0 367 for it.Next() { 368 count++ 369 index := it.Index() 370 value := it.Value() 371 switch index { 372 case 0: 373 if actualValue, expectedValue := value, "a"; actualValue != expectedValue { 374 t.Errorf("Got %v expected %v", actualValue, expectedValue) 375 } 376 case 1: 377 if actualValue, expectedValue := value, "b"; actualValue != expectedValue { 378 t.Errorf("Got %v expected %v", actualValue, expectedValue) 379 } 380 case 2: 381 if actualValue, expectedValue := value, "c"; actualValue != expectedValue { 382 t.Errorf("Got %v expected %v", actualValue, expectedValue) 383 } 384 default: 385 t.Errorf("Too many") 386 } 387 } 388 if actualValue, expectedValue := count, 3; actualValue != expectedValue { 389 t.Errorf("Got %v expected %v", actualValue, expectedValue) 390 } 391 } 392 393 func TestListIteratorPrevOnEmpty(t *testing.T) { 394 list := New() 395 it := list.Iterator() 396 for it.Prev() { 397 t.Errorf("Shouldn't iterate on empty list") 398 } 399 } 400 401 func TestListIteratorPrev(t *testing.T) { 402 list := New() 403 list.Add("a", "b", "c") 404 it := list.Iterator() 405 for it.Next() { 406 } 407 count := 0 408 for it.Prev() { 409 count++ 410 index := it.Index() 411 value := it.Value() 412 switch index { 413 case 0: 414 if actualValue, expectedValue := value, "a"; actualValue != expectedValue { 415 t.Errorf("Got %v expected %v", actualValue, expectedValue) 416 } 417 case 1: 418 if actualValue, expectedValue := value, "b"; actualValue != expectedValue { 419 t.Errorf("Got %v expected %v", actualValue, expectedValue) 420 } 421 case 2: 422 if actualValue, expectedValue := value, "c"; actualValue != expectedValue { 423 t.Errorf("Got %v expected %v", actualValue, expectedValue) 424 } 425 default: 426 t.Errorf("Too many") 427 } 428 } 429 if actualValue, expectedValue := count, 3; actualValue != expectedValue { 430 t.Errorf("Got %v expected %v", actualValue, expectedValue) 431 } 432 } 433 434 func TestListIteratorBegin(t *testing.T) { 435 list := New() 436 it := list.Iterator() 437 it.Begin() 438 list.Add("a", "b", "c") 439 for it.Next() { 440 } 441 it.Begin() 442 it.Next() 443 if index, value := it.Index(), it.Value(); index != 0 || value != "a" { 444 t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "a") 445 } 446 } 447 448 func TestListIteratorEnd(t *testing.T) { 449 list := New() 450 it := list.Iterator() 451 452 if index := it.Index(); index != -1 { 453 t.Errorf("Got %v expected %v", index, -1) 454 } 455 456 it.End() 457 if index := it.Index(); index != 0 { 458 t.Errorf("Got %v expected %v", index, 0) 459 } 460 461 list.Add("a", "b", "c") 462 it.End() 463 if index := it.Index(); index != list.Size() { 464 t.Errorf("Got %v expected %v", index, list.Size()) 465 } 466 467 it.Prev() 468 if index, value := it.Index(), it.Value(); index != list.Size()-1 || value != "c" { 469 t.Errorf("Got %v,%v expected %v,%v", index, value, list.Size()-1, "c") 470 } 471 } 472 473 func TestListIteratorFirst(t *testing.T) { 474 list := New() 475 it := list.Iterator() 476 if actualValue, expectedValue := it.First(), false; actualValue != expectedValue { 477 t.Errorf("Got %v expected %v", actualValue, expectedValue) 478 } 479 list.Add("a", "b", "c") 480 if actualValue, expectedValue := it.First(), true; actualValue != expectedValue { 481 t.Errorf("Got %v expected %v", actualValue, expectedValue) 482 } 483 if index, value := it.Index(), it.Value(); index != 0 || value != "a" { 484 t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "a") 485 } 486 } 487 488 func TestListIteratorLast(t *testing.T) { 489 list := New() 490 it := list.Iterator() 491 if actualValue, expectedValue := it.Last(), false; actualValue != expectedValue { 492 t.Errorf("Got %v expected %v", actualValue, expectedValue) 493 } 494 list.Add("a", "b", "c") 495 if actualValue, expectedValue := it.Last(), true; actualValue != expectedValue { 496 t.Errorf("Got %v expected %v", actualValue, expectedValue) 497 } 498 if index, value := it.Index(), it.Value(); index != 2 || value != "c" { 499 t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "c") 500 } 501 } 502 503 func TestListSerialization(t *testing.T) { 504 list := New() 505 list.Add("a", "b", "c") 506 507 var err error 508 assert := func() { 509 if actualValue, expectedValue := fmt.Sprintf("%s%s%s", list.Values()...), "abc"; actualValue != expectedValue { 510 t.Errorf("Got %v expected %v", actualValue, expectedValue) 511 } 512 if actualValue, expectedValue := list.Size(), 3; actualValue != expectedValue { 513 t.Errorf("Got %v expected %v", actualValue, expectedValue) 514 } 515 if err != nil { 516 t.Errorf("Got error %v", err) 517 } 518 } 519 520 assert() 521 522 json, err := list.ToJSON() 523 assert() 524 525 err = list.FromJSON(json) 526 assert() 527 } 528 529 func benchmarkGet(b *testing.B, list *List, size int) { 530 for i := 0; i < b.N; i++ { 531 for n := 0; n < size; n++ { 532 list.Get(n) 533 } 534 } 535 } 536 537 func benchmarkAdd(b *testing.B, list *List, size int) { 538 for i := 0; i < b.N; i++ { 539 for n := 0; n < size; n++ { 540 list.Add(n) 541 } 542 } 543 } 544 545 func benchmarkRemove(b *testing.B, list *List, size int) { 546 for i := 0; i < b.N; i++ { 547 for n := 0; n < size; n++ { 548 list.Remove(n) 549 } 550 } 551 } 552 553 func BenchmarkArrayListGet100(b *testing.B) { 554 b.StopTimer() 555 size := 100 556 list := New() 557 for n := 0; n < size; n++ { 558 list.Add(n) 559 } 560 b.StartTimer() 561 benchmarkGet(b, list, size) 562 } 563 564 func BenchmarkArrayListGet1000(b *testing.B) { 565 b.StopTimer() 566 size := 1000 567 list := New() 568 for n := 0; n < size; n++ { 569 list.Add(n) 570 } 571 b.StartTimer() 572 benchmarkGet(b, list, size) 573 } 574 575 func BenchmarkArrayListGet10000(b *testing.B) { 576 b.StopTimer() 577 size := 10000 578 list := New() 579 for n := 0; n < size; n++ { 580 list.Add(n) 581 } 582 b.StartTimer() 583 benchmarkGet(b, list, size) 584 } 585 586 func BenchmarkArrayListGet100000(b *testing.B) { 587 b.StopTimer() 588 size := 100000 589 list := New() 590 for n := 0; n < size; n++ { 591 list.Add(n) 592 } 593 b.StartTimer() 594 benchmarkGet(b, list, size) 595 } 596 597 func BenchmarkArrayListAdd100(b *testing.B) { 598 b.StopTimer() 599 size := 100 600 list := New() 601 b.StartTimer() 602 benchmarkAdd(b, list, size) 603 } 604 605 func BenchmarkArrayListAdd1000(b *testing.B) { 606 b.StopTimer() 607 size := 1000 608 list := New() 609 for n := 0; n < size; n++ { 610 list.Add(n) 611 } 612 b.StartTimer() 613 benchmarkAdd(b, list, size) 614 } 615 616 func BenchmarkArrayListAdd10000(b *testing.B) { 617 b.StopTimer() 618 size := 10000 619 list := New() 620 for n := 0; n < size; n++ { 621 list.Add(n) 622 } 623 b.StartTimer() 624 benchmarkAdd(b, list, size) 625 } 626 627 func BenchmarkArrayListAdd100000(b *testing.B) { 628 b.StopTimer() 629 size := 100000 630 list := New() 631 for n := 0; n < size; n++ { 632 list.Add(n) 633 } 634 b.StartTimer() 635 benchmarkAdd(b, list, size) 636 } 637 638 func BenchmarkArrayListRemove100(b *testing.B) { 639 b.StopTimer() 640 size := 100 641 list := New() 642 for n := 0; n < size; n++ { 643 list.Add(n) 644 } 645 b.StartTimer() 646 benchmarkRemove(b, list, size) 647 } 648 649 func BenchmarkArrayListRemove1000(b *testing.B) { 650 b.StopTimer() 651 size := 1000 652 list := New() 653 for n := 0; n < size; n++ { 654 list.Add(n) 655 } 656 b.StartTimer() 657 benchmarkRemove(b, list, size) 658 } 659 660 func BenchmarkArrayListRemove10000(b *testing.B) { 661 b.StopTimer() 662 size := 10000 663 list := New() 664 for n := 0; n < size; n++ { 665 list.Add(n) 666 } 667 b.StartTimer() 668 benchmarkRemove(b, list, size) 669 } 670 671 func BenchmarkArrayListRemove100000(b *testing.B) { 672 b.StopTimer() 673 size := 100000 674 list := New() 675 for n := 0; n < size; n++ { 676 list.Add(n) 677 } 678 b.StartTimer() 679 benchmarkRemove(b, list, size) 680 }