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