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