github.com/alexandrestein/gods@v1.0.1/lists/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/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 TestListIteratorPrevOnEmpty(t *testing.T) {
   347  	list := New()
   348  	it := list.Iterator()
   349  	for it.Prev() {
   350  		t.Errorf("Shouldn't iterate on empty list")
   351  	}
   352  }
   353  
   354  func TestListIteratorPrev(t *testing.T) {
   355  	list := New()
   356  	list.Add("a", "b", "c")
   357  	it := list.Iterator()
   358  	for it.Next() {
   359  	}
   360  	count := 0
   361  	for it.Prev() {
   362  		count++
   363  		index := it.Index()
   364  		value := it.Value()
   365  		switch index {
   366  		case 0:
   367  			if actualValue, expectedValue := value, "a"; actualValue != expectedValue {
   368  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
   369  			}
   370  		case 1:
   371  			if actualValue, expectedValue := value, "b"; actualValue != expectedValue {
   372  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
   373  			}
   374  		case 2:
   375  			if actualValue, expectedValue := value, "c"; actualValue != expectedValue {
   376  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
   377  			}
   378  		default:
   379  			t.Errorf("Too many")
   380  		}
   381  	}
   382  	if actualValue, expectedValue := count, 3; actualValue != expectedValue {
   383  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   384  	}
   385  }
   386  
   387  func TestListIteratorBegin(t *testing.T) {
   388  	list := New()
   389  	it := list.Iterator()
   390  	it.Begin()
   391  	list.Add("a", "b", "c")
   392  	for it.Next() {
   393  	}
   394  	it.Begin()
   395  	it.Next()
   396  	if index, value := it.Index(), it.Value(); index != 0 || value != "a" {
   397  		t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "a")
   398  	}
   399  }
   400  
   401  func TestListIteratorEnd(t *testing.T) {
   402  	list := New()
   403  	it := list.Iterator()
   404  
   405  	if index := it.Index(); index != -1 {
   406  		t.Errorf("Got %v expected %v", index, -1)
   407  	}
   408  
   409  	it.End()
   410  	if index := it.Index(); index != 0 {
   411  		t.Errorf("Got %v expected %v", index, 0)
   412  	}
   413  
   414  	list.Add("a", "b", "c")
   415  	it.End()
   416  	if index := it.Index(); index != list.Size() {
   417  		t.Errorf("Got %v expected %v", index, list.Size())
   418  	}
   419  
   420  	it.Prev()
   421  	if index, value := it.Index(), it.Value(); index != list.Size()-1 || value != "c" {
   422  		t.Errorf("Got %v,%v expected %v,%v", index, value, list.Size()-1, "c")
   423  	}
   424  }
   425  
   426  func TestListIteratorFirst(t *testing.T) {
   427  	list := New()
   428  	it := list.Iterator()
   429  	if actualValue, expectedValue := it.First(), false; actualValue != expectedValue {
   430  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   431  	}
   432  	list.Add("a", "b", "c")
   433  	if actualValue, expectedValue := it.First(), true; actualValue != expectedValue {
   434  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   435  	}
   436  	if index, value := it.Index(), it.Value(); index != 0 || value != "a" {
   437  		t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "a")
   438  	}
   439  }
   440  
   441  func TestListIteratorLast(t *testing.T) {
   442  	list := New()
   443  	it := list.Iterator()
   444  	if actualValue, expectedValue := it.Last(), false; actualValue != expectedValue {
   445  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   446  	}
   447  	list.Add("a", "b", "c")
   448  	if actualValue, expectedValue := it.Last(), true; actualValue != expectedValue {
   449  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   450  	}
   451  	if index, value := it.Index(), it.Value(); index != 2 || value != "c" {
   452  		t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "c")
   453  	}
   454  }
   455  
   456  func TestListSerialization(t *testing.T) {
   457  	list := New()
   458  	list.Add("a", "b", "c")
   459  
   460  	var err error
   461  	assert := func() {
   462  		if actualValue, expectedValue := fmt.Sprintf("%s%s%s", list.Values()...), "abc"; actualValue != expectedValue {
   463  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   464  		}
   465  		if actualValue, expectedValue := list.Size(), 3; actualValue != expectedValue {
   466  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   467  		}
   468  		if err != nil {
   469  			t.Errorf("Got error %v", err)
   470  		}
   471  	}
   472  
   473  	assert()
   474  
   475  	json, err := list.ToJSON()
   476  	assert()
   477  
   478  	err = list.FromJSON(json)
   479  	assert()
   480  }
   481  
   482  func benchmarkGet(b *testing.B, list *List, size int) {
   483  	for i := 0; i < b.N; i++ {
   484  		for n := 0; n < size; n++ {
   485  			list.Get(n)
   486  		}
   487  	}
   488  }
   489  
   490  func benchmarkAdd(b *testing.B, list *List, size int) {
   491  	for i := 0; i < b.N; i++ {
   492  		for n := 0; n < size; n++ {
   493  			list.Add(n)
   494  		}
   495  	}
   496  }
   497  
   498  func benchmarkRemove(b *testing.B, list *List, size int) {
   499  	for i := 0; i < b.N; i++ {
   500  		for n := 0; n < size; n++ {
   501  			list.Remove(n)
   502  		}
   503  	}
   504  }
   505  
   506  func BenchmarkDoublyLinkedListGet100(b *testing.B) {
   507  	b.StopTimer()
   508  	size := 100
   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 BenchmarkDoublyLinkedListGet1000(b *testing.B) {
   518  	b.StopTimer()
   519  	size := 1000
   520  	list := New()
   521  	for n := 0; n < size; n++ {
   522  		list.Add(n)
   523  	}
   524  	b.StartTimer()
   525  	benchmarkGet(b, list, size)
   526  }
   527  
   528  func BenchmarkDoublyLinkedListGet10000(b *testing.B) {
   529  	b.StopTimer()
   530  	size := 10000
   531  	list := New()
   532  	for n := 0; n < size; n++ {
   533  		list.Add(n)
   534  	}
   535  	b.StartTimer()
   536  	benchmarkGet(b, list, size)
   537  }
   538  
   539  func BenchmarkDoublyLinkedListGet100000(b *testing.B) {
   540  	b.StopTimer()
   541  	size := 100000
   542  	list := New()
   543  	for n := 0; n < size; n++ {
   544  		list.Add(n)
   545  	}
   546  	b.StartTimer()
   547  	benchmarkGet(b, list, size)
   548  }
   549  
   550  func BenchmarkDoublyLinkedListAdd100(b *testing.B) {
   551  	b.StopTimer()
   552  	size := 100
   553  	list := New()
   554  	b.StartTimer()
   555  	benchmarkAdd(b, list, size)
   556  }
   557  
   558  func BenchmarkDoublyLinkedListAdd1000(b *testing.B) {
   559  	b.StopTimer()
   560  	size := 1000
   561  	list := New()
   562  	for n := 0; n < size; n++ {
   563  		list.Add(n)
   564  	}
   565  	b.StartTimer()
   566  	benchmarkAdd(b, list, size)
   567  }
   568  
   569  func BenchmarkDoublyLinkedListAdd10000(b *testing.B) {
   570  	b.StopTimer()
   571  	size := 10000
   572  	list := New()
   573  	for n := 0; n < size; n++ {
   574  		list.Add(n)
   575  	}
   576  	b.StartTimer()
   577  	benchmarkAdd(b, list, size)
   578  }
   579  
   580  func BenchmarkDoublyLinkedListAdd100000(b *testing.B) {
   581  	b.StopTimer()
   582  	size := 100000
   583  	list := New()
   584  	for n := 0; n < size; n++ {
   585  		list.Add(n)
   586  	}
   587  	b.StartTimer()
   588  	benchmarkAdd(b, list, size)
   589  }
   590  
   591  func BenchmarkDoublyLinkedListRemove100(b *testing.B) {
   592  	b.StopTimer()
   593  	size := 100
   594  	list := New()
   595  	for n := 0; n < size; n++ {
   596  		list.Add(n)
   597  	}
   598  	b.StartTimer()
   599  	benchmarkRemove(b, list, size)
   600  }
   601  
   602  func BenchmarkDoublyLinkedListRemove1000(b *testing.B) {
   603  	b.StopTimer()
   604  	size := 1000
   605  	list := New()
   606  	for n := 0; n < size; n++ {
   607  		list.Add(n)
   608  	}
   609  	b.StartTimer()
   610  	benchmarkRemove(b, list, size)
   611  }
   612  
   613  func BenchmarkDoublyLinkedListRemove10000(b *testing.B) {
   614  	b.StopTimer()
   615  	size := 10000
   616  	list := New()
   617  	for n := 0; n < size; n++ {
   618  		list.Add(n)
   619  	}
   620  	b.StartTimer()
   621  	benchmarkRemove(b, list, size)
   622  }
   623  
   624  func BenchmarkDoublyLinkedListRemove100000(b *testing.B) {
   625  	b.StopTimer()
   626  	size := 100000
   627  	list := New()
   628  	for n := 0; n < size; n++ {
   629  		list.Add(n)
   630  	}
   631  	b.StartTimer()
   632  	benchmarkRemove(b, list, size)
   633  }