gitee.com/quant1x/gox@v1.21.2/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  	"gitee.com/quant1x/gox/util/internal"
    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(internal.StringComparator)
   108  	list.Add("e", "f", "g", "a", "b", "c", "d")
   109  	list.Sort(internal.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  }
   226  
   227  func TestListEach(t *testing.T) {
   228  	list := New()
   229  	list.Add("a", "b", "c")
   230  	list.Each(func(index int, value interface{}) {
   231  		switch index {
   232  		case 0:
   233  			if actualValue, expectedValue := value, "a"; actualValue != expectedValue {
   234  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
   235  			}
   236  		case 1:
   237  			if actualValue, expectedValue := value, "b"; actualValue != expectedValue {
   238  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
   239  			}
   240  		case 2:
   241  			if actualValue, expectedValue := value, "c"; actualValue != expectedValue {
   242  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
   243  			}
   244  		default:
   245  			t.Errorf("Too many")
   246  		}
   247  	})
   248  }
   249  
   250  func TestListMap(t *testing.T) {
   251  	list := New()
   252  	list.Add("a", "b", "c")
   253  	mappedList := list.Map(func(index int, value interface{}) interface{} {
   254  		return "mapped: " + value.(string)
   255  	})
   256  	if actualValue, _ := mappedList.Get(0); actualValue != "mapped: a" {
   257  		t.Errorf("Got %v expected %v", actualValue, "mapped: a")
   258  	}
   259  	if actualValue, _ := mappedList.Get(1); actualValue != "mapped: b" {
   260  		t.Errorf("Got %v expected %v", actualValue, "mapped: b")
   261  	}
   262  	if actualValue, _ := mappedList.Get(2); actualValue != "mapped: c" {
   263  		t.Errorf("Got %v expected %v", actualValue, "mapped: c")
   264  	}
   265  	if mappedList.Size() != 3 {
   266  		t.Errorf("Got %v expected %v", mappedList.Size(), 3)
   267  	}
   268  }
   269  
   270  func TestListSelect(t *testing.T) {
   271  	list := New()
   272  	list.Add("a", "b", "c")
   273  	selectedList := list.Select(func(index int, value interface{}) bool {
   274  		return value.(string) >= "a" && value.(string) <= "b"
   275  	})
   276  	if actualValue, _ := selectedList.Get(0); actualValue != "a" {
   277  		t.Errorf("Got %v expected %v", actualValue, "value: a")
   278  	}
   279  	if actualValue, _ := selectedList.Get(1); actualValue != "b" {
   280  		t.Errorf("Got %v expected %v", actualValue, "value: b")
   281  	}
   282  	if selectedList.Size() != 2 {
   283  		t.Errorf("Got %v expected %v", selectedList.Size(), 3)
   284  	}
   285  }
   286  
   287  func TestListAny(t *testing.T) {
   288  	list := New()
   289  	list.Add("a", "b", "c")
   290  	any := list.Any(func(index int, value interface{}) bool {
   291  		return value.(string) == "c"
   292  	})
   293  	if any != true {
   294  		t.Errorf("Got %v expected %v", any, true)
   295  	}
   296  	any = list.Any(func(index int, value interface{}) bool {
   297  		return value.(string) == "x"
   298  	})
   299  	if any != false {
   300  		t.Errorf("Got %v expected %v", any, false)
   301  	}
   302  }
   303  func TestListAll(t *testing.T) {
   304  	list := New()
   305  	list.Add("a", "b", "c")
   306  	all := list.All(func(index int, value interface{}) bool {
   307  		return value.(string) >= "a" && value.(string) <= "c"
   308  	})
   309  	if all != true {
   310  		t.Errorf("Got %v expected %v", all, true)
   311  	}
   312  	all = list.All(func(index int, value interface{}) bool {
   313  		return value.(string) >= "a" && value.(string) <= "b"
   314  	})
   315  	if all != false {
   316  		t.Errorf("Got %v expected %v", all, false)
   317  	}
   318  }
   319  func TestListFind(t *testing.T) {
   320  	list := New()
   321  	list.Add("a", "b", "c")
   322  	foundIndex, foundValue := list.Find(func(index int, value interface{}) bool {
   323  		return value.(string) == "c"
   324  	})
   325  	if foundValue != "c" || foundIndex != 2 {
   326  		t.Errorf("Got %v at %v expected %v at %v", foundValue, foundIndex, "c", 2)
   327  	}
   328  	foundIndex, foundValue = list.Find(func(index int, value interface{}) bool {
   329  		return value.(string) == "x"
   330  	})
   331  	if foundValue != nil || foundIndex != -1 {
   332  		t.Errorf("Got %v at %v expected %v at %v", foundValue, foundIndex, nil, nil)
   333  	}
   334  }
   335  func TestListChaining(t *testing.T) {
   336  	list := New()
   337  	list.Add("a", "b", "c")
   338  	chainedList := list.Select(func(index int, value interface{}) bool {
   339  		return value.(string) > "a"
   340  	}).Map(func(index int, value interface{}) interface{} {
   341  		return value.(string) + value.(string)
   342  	})
   343  	if chainedList.Size() != 2 {
   344  		t.Errorf("Got %v expected %v", chainedList.Size(), 2)
   345  	}
   346  	if actualValue, ok := chainedList.Get(0); actualValue != "bb" || !ok {
   347  		t.Errorf("Got %v expected %v", actualValue, "b")
   348  	}
   349  	if actualValue, ok := chainedList.Get(1); actualValue != "cc" || !ok {
   350  		t.Errorf("Got %v expected %v", actualValue, "c")
   351  	}
   352  }
   353  
   354  func TestListIteratorNextOnEmpty(t *testing.T) {
   355  	list := New()
   356  	it := list.Iterator()
   357  	for it.Next() {
   358  		t.Errorf("Shouldn't iterate on empty list")
   359  	}
   360  }
   361  
   362  func TestListIteratorNext(t *testing.T) {
   363  	list := New()
   364  	list.Add("a", "b", "c")
   365  	it := list.Iterator()
   366  	count := 0
   367  	for it.Next() {
   368  		count++
   369  		index := it.Index()
   370  		value := it.Value()
   371  		switch index {
   372  		case 0:
   373  			if actualValue, expectedValue := value, "a"; actualValue != expectedValue {
   374  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
   375  			}
   376  		case 1:
   377  			if actualValue, expectedValue := value, "b"; actualValue != expectedValue {
   378  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
   379  			}
   380  		case 2:
   381  			if actualValue, expectedValue := value, "c"; actualValue != expectedValue {
   382  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
   383  			}
   384  		default:
   385  			t.Errorf("Too many")
   386  		}
   387  	}
   388  	if actualValue, expectedValue := count, 3; actualValue != expectedValue {
   389  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   390  	}
   391  }
   392  
   393  func TestListIteratorBegin(t *testing.T) {
   394  	list := New()
   395  	it := list.Iterator()
   396  	it.Begin()
   397  	list.Add("a", "b", "c")
   398  	for it.Next() {
   399  	}
   400  	it.Begin()
   401  	it.Next()
   402  	if index, value := it.Index(), it.Value(); index != 0 || value != "a" {
   403  		t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "a")
   404  	}
   405  }
   406  
   407  func TestListIteratorFirst(t *testing.T) {
   408  	list := New()
   409  	it := list.Iterator()
   410  	if actualValue, expectedValue := it.First(), false; actualValue != expectedValue {
   411  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   412  	}
   413  	list.Add("a", "b", "c")
   414  	if actualValue, expectedValue := it.First(), true; actualValue != expectedValue {
   415  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   416  	}
   417  	if index, value := it.Index(), it.Value(); index != 0 || value != "a" {
   418  		t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "a")
   419  	}
   420  }
   421  
   422  func TestListSerialization(t *testing.T) {
   423  	list := New()
   424  	list.Add("a", "b", "c")
   425  
   426  	var err error
   427  	assert := func() {
   428  		if actualValue, expectedValue := fmt.Sprintf("%s%s%s", list.Values()...), "abc"; actualValue != expectedValue {
   429  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   430  		}
   431  		if actualValue, expectedValue := list.Size(), 3; actualValue != expectedValue {
   432  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   433  		}
   434  		if err != nil {
   435  			t.Errorf("Got error %v", err)
   436  		}
   437  	}
   438  
   439  	assert()
   440  
   441  	json, err := list.ToJSON()
   442  	assert()
   443  
   444  	err = list.FromJSON(json)
   445  	assert()
   446  }
   447  
   448  func benchmarkGet(b *testing.B, list *List, size int) {
   449  	for i := 0; i < b.N; i++ {
   450  		for n := 0; n < size; n++ {
   451  			list.Get(n)
   452  		}
   453  	}
   454  }
   455  
   456  func benchmarkAdd(b *testing.B, list *List, size int) {
   457  	for i := 0; i < b.N; i++ {
   458  		for n := 0; n < size; n++ {
   459  			list.Add(n)
   460  		}
   461  	}
   462  }
   463  
   464  func benchmarkRemove(b *testing.B, list *List, size int) {
   465  	for i := 0; i < b.N; i++ {
   466  		for n := 0; n < size; n++ {
   467  			list.Remove(n)
   468  		}
   469  	}
   470  }
   471  
   472  func BenchmarkSinglyLinkedListGet100(b *testing.B) {
   473  	b.StopTimer()
   474  	size := 100
   475  	list := New()
   476  	for n := 0; n < size; n++ {
   477  		list.Add(n)
   478  	}
   479  	b.StartTimer()
   480  	benchmarkGet(b, list, size)
   481  }
   482  
   483  func BenchmarkSinglyLinkedListGet1000(b *testing.B) {
   484  	b.StopTimer()
   485  	size := 1000
   486  	list := New()
   487  	for n := 0; n < size; n++ {
   488  		list.Add(n)
   489  	}
   490  	b.StartTimer()
   491  	benchmarkGet(b, list, size)
   492  }
   493  
   494  func BenchmarkSinglyLinkedListGet10000(b *testing.B) {
   495  	b.StopTimer()
   496  	size := 10000
   497  	list := New()
   498  	for n := 0; n < size; n++ {
   499  		list.Add(n)
   500  	}
   501  	b.StartTimer()
   502  	benchmarkGet(b, list, size)
   503  }
   504  
   505  func BenchmarkSinglyLinkedListGet100000(b *testing.B) {
   506  	b.StopTimer()
   507  	size := 100000
   508  	list := New()
   509  	for n := 0; n < size; n++ {
   510  		list.Add(n)
   511  	}
   512  	b.StartTimer()
   513  	benchmarkGet(b, list, size)
   514  }
   515  
   516  func BenchmarkSinglyLinkedListAdd100(b *testing.B) {
   517  	b.StopTimer()
   518  	size := 100
   519  	list := New()
   520  	b.StartTimer()
   521  	benchmarkAdd(b, list, size)
   522  }
   523  
   524  func BenchmarkSinglyLinkedListAdd1000(b *testing.B) {
   525  	b.StopTimer()
   526  	size := 1000
   527  	list := New()
   528  	for n := 0; n < size; n++ {
   529  		list.Add(n)
   530  	}
   531  	b.StartTimer()
   532  	benchmarkAdd(b, list, size)
   533  }
   534  
   535  func BenchmarkSinglyLinkedListAdd10000(b *testing.B) {
   536  	b.StopTimer()
   537  	size := 10000
   538  	list := New()
   539  	for n := 0; n < size; n++ {
   540  		list.Add(n)
   541  	}
   542  	b.StartTimer()
   543  	benchmarkAdd(b, list, size)
   544  }
   545  
   546  func BenchmarkSinglyLinkedListAdd100000(b *testing.B) {
   547  	b.StopTimer()
   548  	size := 100000
   549  	list := New()
   550  	for n := 0; n < size; n++ {
   551  		list.Add(n)
   552  	}
   553  	b.StartTimer()
   554  	benchmarkAdd(b, list, size)
   555  }
   556  
   557  func BenchmarkSinglyLinkedListRemove100(b *testing.B) {
   558  	b.StopTimer()
   559  	size := 100
   560  	list := New()
   561  	for n := 0; n < size; n++ {
   562  		list.Add(n)
   563  	}
   564  	b.StartTimer()
   565  	benchmarkRemove(b, list, size)
   566  }
   567  
   568  func BenchmarkSinglyLinkedListRemove1000(b *testing.B) {
   569  	b.StopTimer()
   570  	size := 1000
   571  	list := New()
   572  	for n := 0; n < size; n++ {
   573  		list.Add(n)
   574  	}
   575  	b.StartTimer()
   576  	benchmarkRemove(b, list, size)
   577  }
   578  
   579  func BenchmarkSinglyLinkedListRemove10000(b *testing.B) {
   580  	b.StopTimer()
   581  	size := 10000
   582  	list := New()
   583  	for n := 0; n < size; n++ {
   584  		list.Add(n)
   585  	}
   586  	b.StartTimer()
   587  	benchmarkRemove(b, list, size)
   588  }
   589  
   590  func BenchmarkSinglyLinkedListRemove100000(b *testing.B) {
   591  	b.StopTimer()
   592  	size := 100000
   593  	list := New()
   594  	for n := 0; n < size; n++ {
   595  		list.Add(n)
   596  	}
   597  	b.StartTimer()
   598  	benchmarkRemove(b, list, size)
   599  }