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