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