github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/structure/lists/arraylist/arraylist_test.go (about)

     1  package arraylist
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"github.com/songzhibin97/go-baseutils/base/bcomparator"
     7  	"strings"
     8  
     9  	"testing"
    10  )
    11  
    12  func TestListNew(t *testing.T) {
    13  	list1 := New[int]()
    14  
    15  	if actualValue := list1.Empty(); actualValue != true {
    16  		t.Errorf("Got %v expected %v", actualValue, true)
    17  	}
    18  
    19  	list2 := New[any](1, "b")
    20  
    21  	if actualValue := list2.Size(); actualValue != 2 {
    22  		t.Errorf("Got %v expected %v", actualValue, 2)
    23  	}
    24  
    25  	if actualValue, ok := list2.Get(0); actualValue != 1 || !ok {
    26  		t.Errorf("Got %v expected %v", actualValue, 1)
    27  	}
    28  
    29  	if actualValue, ok := list2.Get(1); actualValue != "b" || !ok {
    30  		t.Errorf("Got %v expected %v", actualValue, "b")
    31  	}
    32  
    33  	if actualValue, ok := list2.Get(2); actualValue != nil || ok {
    34  		t.Errorf("Got %v expected %v", actualValue, nil)
    35  	}
    36  }
    37  
    38  func TestListAdd(t *testing.T) {
    39  	list := New[string]()
    40  	list.Add("a")
    41  	list.Add("b", "c")
    42  	if actualValue := list.Empty(); actualValue != false {
    43  		t.Errorf("Got %v expected %v", actualValue, false)
    44  	}
    45  	if actualValue := list.Size(); actualValue != 3 {
    46  		t.Errorf("Got %v expected %v", actualValue, 3)
    47  	}
    48  	if actualValue, ok := list.Get(2); actualValue != "c" || !ok {
    49  		t.Errorf("Got %v expected %v", actualValue, "c")
    50  	}
    51  }
    52  
    53  func TestListIndexOf(t *testing.T) {
    54  	list := New[string]()
    55  
    56  	expectedIndex := -1
    57  	if index := list.IndexOf("a"); index != expectedIndex {
    58  		t.Errorf("Got %v expected %v", index, expectedIndex)
    59  	}
    60  
    61  	list.Add("a")
    62  	list.Add("b", "c")
    63  
    64  	expectedIndex = 0
    65  	if index := list.IndexOf("a"); index != expectedIndex {
    66  		t.Errorf("Got %v expected %v", index, expectedIndex)
    67  	}
    68  
    69  	expectedIndex = 1
    70  	if index := list.IndexOf("b"); index != expectedIndex {
    71  		t.Errorf("Got %v expected %v", index, expectedIndex)
    72  	}
    73  
    74  	expectedIndex = 2
    75  	if index := list.IndexOf("c"); index != expectedIndex {
    76  		t.Errorf("Got %v expected %v", index, expectedIndex)
    77  	}
    78  }
    79  
    80  func TestListRemove(t *testing.T) {
    81  	list := New[string]()
    82  	list.Add("a")
    83  	list.Add("b", "c")
    84  	list.Remove(2)
    85  	if actualValue, ok := list.Get(2); actualValue != "" || ok {
    86  		t.Errorf("Got %v expected %v", actualValue, nil)
    87  	}
    88  	list.Remove(1)
    89  	list.Remove(0)
    90  	list.Remove(0) // no effect
    91  	if actualValue := list.Empty(); actualValue != true {
    92  		t.Errorf("Got %v expected %v", actualValue, true)
    93  	}
    94  	if actualValue := list.Size(); actualValue != 0 {
    95  		t.Errorf("Got %v expected %v", actualValue, 0)
    96  	}
    97  }
    98  
    99  func TestListGet(t *testing.T) {
   100  	list := New[string]()
   101  	list.Add("a")
   102  	list.Add("b", "c")
   103  	if actualValue, ok := list.Get(0); actualValue != "a" || !ok {
   104  		t.Errorf("Got %v expected %v", actualValue, "a")
   105  	}
   106  	if actualValue, ok := list.Get(1); actualValue != "b" || !ok {
   107  		t.Errorf("Got %v expected %v", actualValue, "b")
   108  	}
   109  	if actualValue, ok := list.Get(2); actualValue != "c" || !ok {
   110  		t.Errorf("Got %v expected %v", actualValue, "c")
   111  	}
   112  	if actualValue, ok := list.Get(3); actualValue != "" || ok {
   113  		t.Errorf("Got %v expected %v", actualValue, nil)
   114  	}
   115  	list.Remove(0)
   116  	if actualValue, ok := list.Get(0); actualValue != "b" || !ok {
   117  		t.Errorf("Got %v expected %v", actualValue, "b")
   118  	}
   119  }
   120  
   121  func TestListSwap(t *testing.T) {
   122  	list := New[string]()
   123  	list.Add("a")
   124  	list.Add("b", "c")
   125  	list.Swap(0, 1)
   126  	if actualValue, ok := list.Get(0); actualValue != "b" || !ok {
   127  		t.Errorf("Got %v expected %v", actualValue, "b")
   128  	}
   129  }
   130  
   131  func TestListSort(t *testing.T) {
   132  	list := New[string]()
   133  	list.Sort(bcomparator.StringComparator())
   134  	list.Add("e", "f", "g", "a", "b", "c", "d")
   135  	list.Sort(bcomparator.StringComparator())
   136  	for i := 1; i < list.Size(); i++ {
   137  		a, _ := list.Get(i - 1)
   138  		b, _ := list.Get(i)
   139  		if a > b {
   140  			t.Errorf("Not sorted! %s > %s", a, b)
   141  		}
   142  	}
   143  }
   144  
   145  func TestListClear(t *testing.T) {
   146  	list := New[string]()
   147  	list.Add("e", "f", "g", "a", "b", "c", "d")
   148  	list.Clear()
   149  	if actualValue := list.Empty(); actualValue != true {
   150  		t.Errorf("Got %v expected %v", actualValue, true)
   151  	}
   152  	if actualValue := list.Size(); actualValue != 0 {
   153  		t.Errorf("Got %v expected %v", actualValue, 0)
   154  	}
   155  }
   156  
   157  func TestListContains(t *testing.T) {
   158  	list := New[string]()
   159  	list.Add("a")
   160  	list.Add("b", "c")
   161  	if actualValue := list.Contains("a"); actualValue != true {
   162  		t.Errorf("Got %v expected %v", actualValue, true)
   163  	}
   164  	if actualValue := list.Contains(""); actualValue != false {
   165  		t.Errorf("Got %v expected %v", actualValue, false)
   166  	}
   167  	if actualValue := list.Contains("a", "b", "c"); actualValue != true {
   168  		t.Errorf("Got %v expected %v", actualValue, true)
   169  	}
   170  	if actualValue := list.Contains("a", "b", "c", "d"); actualValue != false {
   171  		t.Errorf("Got %v expected %v", actualValue, false)
   172  	}
   173  	list.Clear()
   174  	if actualValue := list.Contains("a"); actualValue != false {
   175  		t.Errorf("Got %v expected %v", actualValue, false)
   176  	}
   177  	if actualValue := list.Contains("a", "b", "c"); actualValue != false {
   178  		t.Errorf("Got %v expected %v", actualValue, false)
   179  	}
   180  }
   181  
   182  func TestListValues(t *testing.T) {
   183  	list := New[any]()
   184  	list.Add("a")
   185  	list.Add("b", "c")
   186  	if actualValue, expectedValue := fmt.Sprintf("%s%s%s", list.Values()...), "abc"; actualValue != expectedValue {
   187  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   188  	}
   189  }
   190  
   191  func TestListInsert(t *testing.T) {
   192  	list := New[any]()
   193  	list.Insert(0, "b", "c")
   194  	list.Insert(0, "a")
   195  	list.Insert(10, "x") // ignore
   196  	if actualValue := list.Size(); actualValue != 3 {
   197  		t.Errorf("Got %v expected %v", actualValue, 3)
   198  	}
   199  	list.Insert(3, "d") // append
   200  	if actualValue := list.Size(); actualValue != 4 {
   201  		t.Errorf("Got %v expected %v", actualValue, 4)
   202  	}
   203  	if actualValue, expectedValue := fmt.Sprintf("%s%s%s%s", list.Values()...), "abcd"; actualValue != expectedValue {
   204  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   205  	}
   206  }
   207  
   208  func TestListSet(t *testing.T) {
   209  	list := New[any]()
   210  	list.Set(0, "a")
   211  	list.Set(1, "b")
   212  	if actualValue := list.Size(); actualValue != 2 {
   213  		t.Errorf("Got %v expected %v", actualValue, 2)
   214  	}
   215  	list.Set(2, "c") // append
   216  	if actualValue := list.Size(); actualValue != 3 {
   217  		t.Errorf("Got %v expected %v", actualValue, 3)
   218  	}
   219  	list.Set(4, "d")  // ignore
   220  	list.Set(1, "bb") // update
   221  	if actualValue := list.Size(); actualValue != 3 {
   222  		t.Errorf("Got %v expected %v", actualValue, 3)
   223  	}
   224  	if actualValue, expectedValue := fmt.Sprintf("%s%s%s", list.Values()...), "abbc"; actualValue != expectedValue {
   225  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   226  	}
   227  }
   228  
   229  func TestListEach(t *testing.T) {
   230  	list := New[string]()
   231  	list.Add("a", "b", "c")
   232  	list.Each(func(index int, value string) {
   233  		switch index {
   234  		case 0:
   235  			if actualValue, expectedValue := value, "a"; actualValue != expectedValue {
   236  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
   237  			}
   238  		case 1:
   239  			if actualValue, expectedValue := value, "b"; actualValue != expectedValue {
   240  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
   241  			}
   242  		case 2:
   243  			if actualValue, expectedValue := value, "c"; actualValue != expectedValue {
   244  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
   245  			}
   246  		default:
   247  			t.Errorf("Too many")
   248  		}
   249  	})
   250  }
   251  
   252  func TestListMap(t *testing.T) {
   253  	list := New[string]()
   254  	list.Add("a", "b", "c")
   255  	mappedList := list.Map(func(index int, value string) string {
   256  		return "mapped: " + value
   257  	})
   258  	if actualValue, _ := mappedList.Get(0); actualValue != "mapped: a" {
   259  		t.Errorf("Got %v expected %v", actualValue, "mapped: a")
   260  	}
   261  	if actualValue, _ := mappedList.Get(1); actualValue != "mapped: b" {
   262  		t.Errorf("Got %v expected %v", actualValue, "mapped: b")
   263  	}
   264  	if actualValue, _ := mappedList.Get(2); actualValue != "mapped: c" {
   265  		t.Errorf("Got %v expected %v", actualValue, "mapped: c")
   266  	}
   267  	if mappedList.Size() != 3 {
   268  		t.Errorf("Got %v expected %v", mappedList.Size(), 3)
   269  	}
   270  }
   271  
   272  func TestListSelect(t *testing.T) {
   273  	list := New[string]()
   274  	list.Add("a", "b", "c")
   275  	selectedList := list.Select(func(index int, value string) bool {
   276  		return value >= "a" && value <= "b"
   277  	})
   278  	if actualValue, _ := selectedList.Get(0); actualValue != "a" {
   279  		t.Errorf("Got %v expected %v", actualValue, "value: a")
   280  	}
   281  	if actualValue, _ := selectedList.Get(1); actualValue != "b" {
   282  		t.Errorf("Got %v expected %v", actualValue, "value: b")
   283  	}
   284  	if selectedList.Size() != 2 {
   285  		t.Errorf("Got %v expected %v", selectedList.Size(), 3)
   286  	}
   287  }
   288  
   289  func TestListAny(t *testing.T) {
   290  	list := New[string]()
   291  	list.Add("a", "b", "c")
   292  	any := list.Any(func(index int, value string) bool {
   293  		return value == "c"
   294  	})
   295  	if any != true {
   296  		t.Errorf("Got %v expected %v", any, true)
   297  	}
   298  	any = list.Any(func(index int, value string) bool {
   299  		return value == "x"
   300  	})
   301  	if any != false {
   302  		t.Errorf("Got %v expected %v", any, false)
   303  	}
   304  }
   305  func TestListAll(t *testing.T) {
   306  	list := New[string]()
   307  	list.Add("a", "b", "c")
   308  	all := list.All(func(index int, value string) bool {
   309  		return value >= "a" && value <= "c"
   310  	})
   311  	if all != true {
   312  		t.Errorf("Got %v expected %v", all, true)
   313  	}
   314  	all = list.All(func(index int, value string) bool {
   315  		return value >= "a" && value <= "b"
   316  	})
   317  	if all != false {
   318  		t.Errorf("Got %v expected %v", all, false)
   319  	}
   320  }
   321  func TestListFind(t *testing.T) {
   322  	list := New[string]()
   323  	list.Add("a", "b", "c")
   324  	foundIndex, foundValue := list.Find(func(index int, value string) bool {
   325  		return value == "c"
   326  	})
   327  	if foundValue != "c" || foundIndex != 2 {
   328  		t.Errorf("Got %v at %v expected %v at %v", foundValue, foundIndex, "c", 2)
   329  	}
   330  	foundIndex, foundValue = list.Find(func(index int, value string) bool {
   331  		return value == "x"
   332  	})
   333  	if foundValue != "" || foundIndex != -1 {
   334  		t.Errorf("Got %v at %v expected %v at %v", foundValue, foundIndex, nil, nil)
   335  	}
   336  }
   337  func TestListChaining(t *testing.T) {
   338  	list := New[string]()
   339  	list.Add("a", "b", "c")
   340  	chainedList := list.Select(func(index int, value string) bool {
   341  		return value > "a"
   342  	}).Map(func(index int, value string) string {
   343  		return value + value
   344  	})
   345  	if chainedList.Size() != 2 {
   346  		t.Errorf("Got %v expected %v", chainedList.Size(), 2)
   347  	}
   348  	if actualValue, ok := chainedList.Get(0); actualValue != "bb" || !ok {
   349  		t.Errorf("Got %v expected %v", actualValue, "b")
   350  	}
   351  	if actualValue, ok := chainedList.Get(1); actualValue != "cc" || !ok {
   352  		t.Errorf("Got %v expected %v", actualValue, "c")
   353  	}
   354  }
   355  
   356  func TestListIteratorNextOnEmpty(t *testing.T) {
   357  	list := New[string]()
   358  	it := list.Iterator()
   359  	for it.Next() {
   360  		t.Errorf("Shouldn't iterate on empty list")
   361  	}
   362  }
   363  
   364  func TestListIteratorNext(t *testing.T) {
   365  	list := New[string]()
   366  	list.Add("a", "b", "c")
   367  	it := list.Iterator()
   368  	count := 0
   369  	for it.Next() {
   370  		count++
   371  		index := it.Index()
   372  		value := it.Value()
   373  		switch index {
   374  		case 0:
   375  			if actualValue, expectedValue := value, "a"; actualValue != expectedValue {
   376  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
   377  			}
   378  		case 1:
   379  			if actualValue, expectedValue := value, "b"; actualValue != expectedValue {
   380  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
   381  			}
   382  		case 2:
   383  			if actualValue, expectedValue := value, "c"; actualValue != expectedValue {
   384  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
   385  			}
   386  		default:
   387  			t.Errorf("Too many")
   388  		}
   389  	}
   390  	if actualValue, expectedValue := count, 3; actualValue != expectedValue {
   391  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   392  	}
   393  }
   394  
   395  func TestListIteratorPrevOnEmpty(t *testing.T) {
   396  	list := New[string]()
   397  	it := list.Iterator()
   398  	for it.Prev() {
   399  		t.Errorf("Shouldn't iterate on empty list")
   400  	}
   401  }
   402  
   403  func TestListIteratorPrev(t *testing.T) {
   404  	list := New[string]()
   405  	list.Add("a", "b", "c")
   406  	it := list.Iterator()
   407  	for it.Next() {
   408  	}
   409  	count := 0
   410  	for it.Prev() {
   411  		count++
   412  		index := it.Index()
   413  		value := it.Value()
   414  		switch index {
   415  		case 0:
   416  			if actualValue, expectedValue := value, "a"; actualValue != expectedValue {
   417  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
   418  			}
   419  		case 1:
   420  			if actualValue, expectedValue := value, "b"; actualValue != expectedValue {
   421  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
   422  			}
   423  		case 2:
   424  			if actualValue, expectedValue := value, "c"; actualValue != expectedValue {
   425  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
   426  			}
   427  		default:
   428  			t.Errorf("Too many")
   429  		}
   430  	}
   431  	if actualValue, expectedValue := count, 3; actualValue != expectedValue {
   432  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   433  	}
   434  }
   435  
   436  func TestListIteratorBegin(t *testing.T) {
   437  	list := New[string]()
   438  	it := list.Iterator()
   439  	it.Begin()
   440  	list.Add("a", "b", "c")
   441  	for it.Next() {
   442  	}
   443  	it.Begin()
   444  	it.Next()
   445  	if index, value := it.Index(), it.Value(); index != 0 || value != "a" {
   446  		t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "a")
   447  	}
   448  }
   449  
   450  func TestListIteratorEnd(t *testing.T) {
   451  	list := New[string]()
   452  	it := list.Iterator()
   453  
   454  	if index := it.Index(); index != -1 {
   455  		t.Errorf("Got %v expected %v", index, -1)
   456  	}
   457  
   458  	it.End()
   459  	if index := it.Index(); index != 0 {
   460  		t.Errorf("Got %v expected %v", index, 0)
   461  	}
   462  
   463  	list.Add("a", "b", "c")
   464  	it.End()
   465  	if index := it.Index(); index != list.Size() {
   466  		t.Errorf("Got %v expected %v", index, list.Size())
   467  	}
   468  
   469  	it.Prev()
   470  	if index, value := it.Index(), it.Value(); index != list.Size()-1 || value != "c" {
   471  		t.Errorf("Got %v,%v expected %v,%v", index, value, list.Size()-1, "c")
   472  	}
   473  }
   474  
   475  func TestListIteratorFirst(t *testing.T) {
   476  	list := New[string]()
   477  	it := list.Iterator()
   478  	if actualValue, expectedValue := it.First(), false; actualValue != expectedValue {
   479  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   480  	}
   481  	list.Add("a", "b", "c")
   482  	if actualValue, expectedValue := it.First(), true; actualValue != expectedValue {
   483  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   484  	}
   485  	if index, value := it.Index(), it.Value(); index != 0 || value != "a" {
   486  		t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "a")
   487  	}
   488  }
   489  
   490  func TestListIteratorLast(t *testing.T) {
   491  	list := New[string]()
   492  	it := list.Iterator()
   493  	if actualValue, expectedValue := it.Last(), false; actualValue != expectedValue {
   494  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   495  	}
   496  	list.Add("a", "b", "c")
   497  	if actualValue, expectedValue := it.Last(), true; actualValue != expectedValue {
   498  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   499  	}
   500  	if index, value := it.Index(), it.Value(); index != 2 || value != "c" {
   501  		t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "c")
   502  	}
   503  }
   504  
   505  func TestListIteratorNextTo(t *testing.T) {
   506  	// Sample seek function, i.e. string starting with "b"
   507  	seek := func(index int, value string) bool {
   508  		return strings.HasSuffix(value, "b")
   509  	}
   510  
   511  	// NextTo (empty)
   512  	{
   513  		list := New[string]()
   514  		it := list.Iterator()
   515  		for it.NextTo(seek) {
   516  			t.Errorf("Shouldn't iterate on empty list")
   517  		}
   518  	}
   519  
   520  	// NextTo (not found)
   521  	{
   522  		list := New[string]()
   523  		list.Add("xx", "yy")
   524  		it := list.Iterator()
   525  		for it.NextTo(seek) {
   526  			t.Errorf("Shouldn't iterate on empty list")
   527  		}
   528  	}
   529  
   530  	// NextTo (found)
   531  	{
   532  		list := New[string]()
   533  		list.Add("aa", "bb", "cc")
   534  		it := list.Iterator()
   535  		it.Begin()
   536  		if !it.NextTo(seek) {
   537  			t.Errorf("Shouldn't iterate on empty list")
   538  		}
   539  		if index, value := it.Index(), it.Value(); index != 1 || value != "bb" {
   540  			t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb")
   541  		}
   542  		if !it.Next() {
   543  			t.Errorf("Should go to first element")
   544  		}
   545  		if index, value := it.Index(), it.Value(); index != 2 || value != "cc" {
   546  			t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "cc")
   547  		}
   548  		if it.Next() {
   549  			t.Errorf("Should not go past last element")
   550  		}
   551  	}
   552  }
   553  
   554  func TestListIteratorPrevTo(t *testing.T) {
   555  	// Sample seek function, i.e. string starting with "b"
   556  	seek := func(index int, value string) bool {
   557  		return strings.HasSuffix(value, "b")
   558  	}
   559  
   560  	// PrevTo (empty)
   561  	{
   562  		list := New[string]()
   563  		it := list.Iterator()
   564  		it.End()
   565  		for it.PrevTo(seek) {
   566  			t.Errorf("Shouldn't iterate on empty list")
   567  		}
   568  	}
   569  
   570  	// PrevTo (not found)
   571  	{
   572  		list := New[string]()
   573  		list.Add("xx", "yy")
   574  		it := list.Iterator()
   575  		it.End()
   576  		for it.PrevTo(seek) {
   577  			t.Errorf("Shouldn't iterate on empty list")
   578  		}
   579  	}
   580  
   581  	// PrevTo (found)
   582  	{
   583  		list := New[string]()
   584  		list.Add("aa", "bb", "cc")
   585  		it := list.Iterator()
   586  		it.End()
   587  		if !it.PrevTo(seek) {
   588  			t.Errorf("Shouldn't iterate on empty list")
   589  		}
   590  		if index, value := it.Index(), it.Value(); index != 1 || value != "bb" {
   591  			t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb")
   592  		}
   593  		if !it.Prev() {
   594  			t.Errorf("Should go to first element")
   595  		}
   596  		if index, value := it.Index(), it.Value(); index != 0 || value != "aa" {
   597  			t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "aa")
   598  		}
   599  		if it.Prev() {
   600  			t.Errorf("Should not go before first element")
   601  		}
   602  	}
   603  }
   604  
   605  func TestListSerialization(t *testing.T) {
   606  	list := New[any]()
   607  	list.Add("a", "b", "c")
   608  
   609  	var err error
   610  	assert := func() {
   611  		if actualValue, expectedValue := fmt.Sprintf("%s%s%s", list.Values()...), "abc"; actualValue != expectedValue {
   612  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   613  		}
   614  		if actualValue, expectedValue := list.Size(), 3; actualValue != expectedValue {
   615  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   616  		}
   617  		if err != nil {
   618  			t.Errorf("Got error %v", err)
   619  		}
   620  	}
   621  
   622  	assert()
   623  
   624  	bytes, err := list.MarshalJSON()
   625  	assert()
   626  
   627  	err = list.UnmarshalJSON(bytes)
   628  	assert()
   629  
   630  	bytes, err = json.Marshal([]interface{}{"a", "b", "c", list})
   631  	if err != nil {
   632  		t.Errorf("Got error %v", err)
   633  	}
   634  
   635  	err = json.Unmarshal([]byte(`[1,2,3]`), &list)
   636  	if err != nil {
   637  		t.Errorf("Got error %v", err)
   638  	}
   639  }
   640  
   641  func TestListString(t *testing.T) {
   642  	c := New[int]()
   643  	c.Add(1)
   644  	if !strings.HasPrefix(c.String(), "ArrayList") {
   645  		t.Errorf("String should start with container name")
   646  	}
   647  }
   648  
   649  func benchmarkGet[E int](b *testing.B, list *List[E], size int) {
   650  	for i := 0; i < b.N; i++ {
   651  		for n := 0; n < size; n++ {
   652  			list.Get(n)
   653  		}
   654  	}
   655  }
   656  
   657  func benchmarkAdd[E int](b *testing.B, list *List[E], size int) {
   658  	for i := 0; i < b.N; i++ {
   659  		for n := 0; n < size; n++ {
   660  			list.Add(E(n))
   661  		}
   662  	}
   663  }
   664  
   665  func benchmarkRemove[E int](b *testing.B, list *List[E], size int) {
   666  	for i := 0; i < b.N; i++ {
   667  		for n := 0; n < size; n++ {
   668  			list.Remove(n)
   669  		}
   670  	}
   671  }
   672  
   673  func BenchmarkArrayListGet100(b *testing.B) {
   674  	b.StopTimer()
   675  	size := 100
   676  	list := New[int]()
   677  	for n := 0; n < size; n++ {
   678  		list.Add(n)
   679  	}
   680  	b.StartTimer()
   681  	benchmarkGet(b, list, size)
   682  }
   683  
   684  func BenchmarkArrayListGet1000(b *testing.B) {
   685  	b.StopTimer()
   686  	size := 1000
   687  	list := New[int]()
   688  	for n := 0; n < size; n++ {
   689  		list.Add(n)
   690  	}
   691  	b.StartTimer()
   692  	benchmarkGet(b, list, size)
   693  }
   694  
   695  func BenchmarkArrayListGet10000(b *testing.B) {
   696  	b.StopTimer()
   697  	size := 10000
   698  	list := New[int]()
   699  	for n := 0; n < size; n++ {
   700  		list.Add(n)
   701  	}
   702  	b.StartTimer()
   703  	benchmarkGet(b, list, size)
   704  }
   705  
   706  func BenchmarkArrayListGet100000(b *testing.B) {
   707  	b.StopTimer()
   708  	size := 100000
   709  	list := New[int]()
   710  	for n := 0; n < size; n++ {
   711  		list.Add(n)
   712  	}
   713  	b.StartTimer()
   714  	benchmarkGet(b, list, size)
   715  }
   716  
   717  func BenchmarkArrayListAdd100(b *testing.B) {
   718  	b.StopTimer()
   719  	size := 100
   720  	list := New[int]()
   721  	b.StartTimer()
   722  	benchmarkAdd(b, list, size)
   723  }
   724  
   725  func BenchmarkArrayListAdd1000(b *testing.B) {
   726  	b.StopTimer()
   727  	size := 1000
   728  	list := New[int]()
   729  	for n := 0; n < size; n++ {
   730  		list.Add(n)
   731  	}
   732  	b.StartTimer()
   733  	benchmarkAdd(b, list, size)
   734  }
   735  
   736  func BenchmarkArrayListAdd10000(b *testing.B) {
   737  	b.StopTimer()
   738  	size := 10000
   739  	list := New[int]()
   740  	for n := 0; n < size; n++ {
   741  		list.Add(n)
   742  	}
   743  	b.StartTimer()
   744  	benchmarkAdd(b, list, size)
   745  }
   746  
   747  func BenchmarkArrayListAdd100000(b *testing.B) {
   748  	b.StopTimer()
   749  	size := 100000
   750  	list := New[int]()
   751  	for n := 0; n < size; n++ {
   752  		list.Add(n)
   753  	}
   754  	b.StartTimer()
   755  	benchmarkAdd(b, list, size)
   756  }
   757  
   758  func BenchmarkArrayListRemove100(b *testing.B) {
   759  	b.StopTimer()
   760  	size := 100
   761  	list := New[int]()
   762  	for n := 0; n < size; n++ {
   763  		list.Add(n)
   764  	}
   765  	b.StartTimer()
   766  	benchmarkRemove(b, list, size)
   767  }
   768  
   769  func BenchmarkArrayListRemove1000(b *testing.B) {
   770  	b.StopTimer()
   771  	size := 1000
   772  	list := New[int]()
   773  	for n := 0; n < size; n++ {
   774  		list.Add(n)
   775  	}
   776  	b.StartTimer()
   777  	benchmarkRemove(b, list, size)
   778  }
   779  
   780  func BenchmarkArrayListRemove10000(b *testing.B) {
   781  	b.StopTimer()
   782  	size := 10000
   783  	list := New[int]()
   784  	for n := 0; n < size; n++ {
   785  		list.Add(n)
   786  	}
   787  	b.StartTimer()
   788  	benchmarkRemove(b, list, size)
   789  }
   790  
   791  func BenchmarkArrayListRemove100000(b *testing.B) {
   792  	b.StopTimer()
   793  	size := 100000
   794  	list := New[int]()
   795  	for n := 0; n < size; n++ {
   796  		list.Add(n)
   797  	}
   798  	b.StartTimer()
   799  	benchmarkRemove(b, list, size)
   800  }