diff --git a/lists/arraylist/arraylist_test.go b/lists/arraylist/arraylist_test.go index c3f0c58..0dea8d8 100644 --- a/lists/arraylist/arraylist_test.go +++ b/lists/arraylist/arraylist_test.go @@ -32,114 +32,149 @@ import ( "testing" ) -func TestArrayList(t *testing.T) { - +func TestListAdd(t *testing.T) { list := New() - - list.Sort(utils.StringComparator) - - list.Add("e", "f", "g", "a", "b", "c", "d") - - list.Sort(utils.StringComparator) - for i := 1; i < list.Size(); i++ { - a, _ := list.Get(i - 1) - b, _ := list.Get(i) - if a.(string) > b.(string) { - t.Errorf("Not sorted! %s > %s", a, b) - } + list.Add("a") + list.Add("b", "c") + if actualValue := list.Empty(); actualValue != false { + t.Errorf("Got %v expected %v", actualValue, false) } + if actualValue := list.Size(); actualValue != 3 { + t.Errorf("Got %v expected %v", actualValue, 3) + } + if actualValue, ok := list.Get(2); actualValue != "c" || !ok { + t.Errorf("Got %v expected %v", actualValue, "c") + } +} - list.Clear() - +func TestListRemove(t *testing.T) { + list := New() + list.Add("a") + list.Add("b", "c") + list.Remove(2) + if actualValue, ok := list.Get(2); actualValue != nil || ok { + t.Errorf("Got %v expected %v", actualValue, nil) + } + list.Remove(1) + list.Remove(0) + list.Remove(0) // no effect if actualValue := list.Empty(); actualValue != true { t.Errorf("Got %v expected %v", actualValue, true) } - if actualValue := list.Size(); actualValue != 0 { t.Errorf("Got %v expected %v", actualValue, 0) } +} +func TestListGet(t *testing.T) { + list := New() list.Add("a") list.Add("b", "c") - - if actualValue := list.Empty(); actualValue != false { - t.Errorf("Got %v expected %v", actualValue, false) + if actualValue, ok := list.Get(0); actualValue != "a" || !ok { + t.Errorf("Got %v expected %v", actualValue, "a") } - - if actualValue := list.Size(); actualValue != 3 { - t.Errorf("Got %v expected %v", actualValue, 3) + if actualValue, ok := list.Get(1); actualValue != "b" || !ok { + t.Errorf("Got %v expected %v", actualValue, "b") } - if actualValue, ok := list.Get(2); actualValue != "c" || !ok { t.Errorf("Got %v expected %v", actualValue, "c") } + if actualValue, ok := list.Get(3); actualValue != nil || ok { + t.Errorf("Got %v expected %v", actualValue, nil) + } + list.Remove(0) + if actualValue, ok := list.Get(0); actualValue != "b" || !ok { + t.Errorf("Got %v expected %v", actualValue, "b") + } +} +func TestListSwap(t *testing.T) { + list := New() + list.Add("a") + list.Add("b", "c") list.Swap(0, 1) - if actualValue, ok := list.Get(0); actualValue != "b" || !ok { t.Errorf("Got %v expected %v", actualValue, "c") } +} - list.Remove(2) - - if actualValue, ok := list.Get(2); actualValue != nil || ok { - t.Errorf("Got %v expected %v", actualValue, nil) +func TestListSort(t *testing.T) { + list := New() + list.Sort(utils.StringComparator) + list.Add("e", "f", "g", "a", "b", "c", "d") + list.Sort(utils.StringComparator) + for i := 1; i < list.Size(); i++ { + a, _ := list.Get(i - 1) + b, _ := list.Get(i) + if a.(string) > b.(string) { + t.Errorf("Not sorted! %s > %s", a, b) + } } +} - list.Remove(1) - list.Remove(0) - +func TestListClear(t *testing.T) { + list := New() + list.Add("e", "f", "g", "a", "b", "c", "d") + list.Clear() if actualValue := list.Empty(); actualValue != true { t.Errorf("Got %v expected %v", actualValue, true) } - if actualValue := list.Size(); actualValue != 0 { t.Errorf("Got %v expected %v", actualValue, 0) } +} - list.Add("a", "b", "c") - +func TestListContains(t *testing.T) { + list := New() + list.Add("a") + list.Add("b", "c") + if actualValue := list.Contains("a"); actualValue != true { + t.Errorf("Got %v expected %v", actualValue, true) + } if actualValue := list.Contains("a", "b", "c"); actualValue != true { t.Errorf("Got %v expected %v", actualValue, true) } - if actualValue := list.Contains("a", "b", "c", "d"); actualValue != false { t.Errorf("Got %v expected %v", actualValue, false) } - list.Clear() - if actualValue := list.Contains("a"); actualValue != false { t.Errorf("Got %v expected %v", actualValue, false) } - - if actualValue, ok := list.Get(0); actualValue != nil || ok { + if actualValue := list.Contains("a", "b", "c"); actualValue != false { t.Errorf("Got %v expected %v", actualValue, false) } +} - if actualValue := list.Empty(); actualValue != true { - t.Errorf("Got %v expected %v", actualValue, true) +func TestListValues(t *testing.T) { + list := New() + list.Add("a") + list.Add("b", "c") + if actualValue, expectedValue := fmt.Sprintf("%s%s%s", list.Values()...), "abc"; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) } +} - list.Insert(0, "h") - list.Insert(0, "e") - list.Insert(1, "f") - list.Insert(2, "g") - list.Insert(4, "i") - list.Insert(0, "a", "b") - list.Insert(list.Size(), "j", "k") - list.Insert(2, "c", "d") - - if actualValue, expectedValue := fmt.Sprintf("%s%s%s%s%s%s%s%s%s%s%s", list.Values()...), "abcdefghijk"; actualValue != expectedValue { +func TestListInsert(t *testing.T) { + list := New() + list.Insert(0, "b", "c") + list.Insert(0, "a") + list.Insert(10, "x") // ignore + if actualValue := list.Size(); actualValue != 3 { + t.Errorf("Got %v expected %v", actualValue, 3) + } + list.Insert(3, "d") // append + if actualValue := list.Size(); actualValue != 4 { + t.Errorf("Got %v expected %v", actualValue, 4) + } + if actualValue, expectedValue := fmt.Sprintf("%s%s%s%s", list.Values()...), "abcd"; actualValue != expectedValue { t.Errorf("Got %v expected %v", actualValue, expectedValue) } } -func TestArrayListEnumerableAndIterator(t *testing.T) { +func TestListEach(t *testing.T) { list := New() list.Add("a", "b", "c") - - // Each list.Each(func(index int, value interface{}) { switch index { case 0: @@ -158,8 +193,11 @@ func TestArrayListEnumerableAndIterator(t *testing.T) { t.Errorf("Too many") } }) +} - // Map +func TestListMap(t *testing.T) { + list := New() + list.Add("a", "b", "c") mappedList := list.Map(func(index int, value interface{}) interface{} { return "mapped: " + value.(string) }) @@ -175,8 +213,11 @@ func TestArrayListEnumerableAndIterator(t *testing.T) { if mappedList.Size() != 3 { t.Errorf("Got %v expected %v", mappedList.Size(), 3) } +} - // Select +func TestListSelect(t *testing.T) { + list := New() + list.Add("a", "b", "c") selectedList := list.Select(func(index int, value interface{}) bool { return value.(string) >= "a" && value.(string) <= "b" }) @@ -189,8 +230,11 @@ func TestArrayListEnumerableAndIterator(t *testing.T) { if selectedList.Size() != 2 { t.Errorf("Got %v expected %v", selectedList.Size(), 3) } +} - // Any +func TestListAny(t *testing.T) { + list := New() + list.Add("a", "b", "c") any := list.Any(func(index int, value interface{}) bool { return value.(string) == "c" }) @@ -203,8 +247,10 @@ func TestArrayListEnumerableAndIterator(t *testing.T) { if any != false { t.Errorf("Got %v expected %v", any, false) } - - // All +} +func TestListAll(t *testing.T) { + list := New() + list.Add("a", "b", "c") all := list.All(func(index int, value interface{}) bool { return value.(string) >= "a" && value.(string) <= "c" }) @@ -217,8 +263,10 @@ func TestArrayListEnumerableAndIterator(t *testing.T) { if all != false { t.Errorf("Got %v expected %v", all, false) } - - // Find +} +func TestListFind(t *testing.T) { + list := New() + list.Add("a", "b", "c") foundIndex, foundValue := list.Find(func(index int, value interface{}) bool { return value.(string) == "c" }) @@ -231,8 +279,29 @@ func TestArrayListEnumerableAndIterator(t *testing.T) { if foundValue != nil || foundIndex != -1 { t.Errorf("Got %v at %v expected %v at %v", foundValue, foundIndex, nil, nil) } +} +func TestListChaining(t *testing.T) { + list := New() + list.Add("a", "b", "c") + chainedList := list.Select(func(index int, value interface{}) bool { + return value.(string) > "a" + }).Map(func(index int, value interface{}) interface{} { + return value.(string) + value.(string) + }) + if chainedList.Size() != 2 { + t.Errorf("Got %v expected %v", chainedList.Size(), 2) + } + if actualValue, ok := chainedList.Get(0); actualValue != "bb" || !ok { + t.Errorf("Got %v expected %v", actualValue, "b") + } + if actualValue, ok := chainedList.Get(1); actualValue != "cc" || !ok { + t.Errorf("Got %v expected %v", actualValue, "c") + } +} - // Iterator +func TestListIterator(t *testing.T) { + list := New() + list.Add("a", "b", "c") it := list.Iterator() for it.Next() { index := it.Index() @@ -261,7 +330,7 @@ func TestArrayListEnumerableAndIterator(t *testing.T) { } } -func BenchmarkArrayList(b *testing.B) { +func BenchmarkList(b *testing.B) { for i := 0; i < b.N; i++ { list := New() for n := 0; n < 1000; n++ { @@ -271,5 +340,4 @@ func BenchmarkArrayList(b *testing.B) { list.Remove(0) } } - } diff --git a/lists/doublylinkedlist/doublylinkedlist_test.go b/lists/doublylinkedlist/doublylinkedlist_test.go index 5e8331d..e199102 100644 --- a/lists/doublylinkedlist/doublylinkedlist_test.go +++ b/lists/doublylinkedlist/doublylinkedlist_test.go @@ -32,116 +32,149 @@ import ( "testing" ) -func TestDoublyLinkedList(t *testing.T) { - +func TestListAdd(t *testing.T) { list := New() - - list.Sort(utils.StringComparator) - - list.Add("e", "f", "g", "a", "b", "c", "d") - - list.Sort(utils.StringComparator) - for i := 1; i < list.Size(); i++ { - a, _ := list.Get(i - 1) - b, _ := list.Get(i) - if a.(string) > b.(string) { - t.Errorf("Not sorted! %s > %s", a, b) - } + list.Add("a") + list.Add("b", "c") + if actualValue := list.Empty(); actualValue != false { + t.Errorf("Got %v expected %v", actualValue, false) } + if actualValue := list.Size(); actualValue != 3 { + t.Errorf("Got %v expected %v", actualValue, 3) + } + if actualValue, ok := list.Get(2); actualValue != "c" || !ok { + t.Errorf("Got %v expected %v", actualValue, "c") + } +} - list.Clear() - +func TestListRemove(t *testing.T) { + list := New() + list.Add("a") + list.Add("b", "c") + list.Remove(2) + if actualValue, ok := list.Get(2); actualValue != nil || ok { + t.Errorf("Got %v expected %v", actualValue, nil) + } + list.Remove(1) + list.Remove(0) + list.Remove(0) // no effect if actualValue := list.Empty(); actualValue != true { t.Errorf("Got %v expected %v", actualValue, true) } - if actualValue := list.Size(); actualValue != 0 { t.Errorf("Got %v expected %v", actualValue, 0) } +} +func TestListGet(t *testing.T) { + list := New() list.Add("a") list.Add("b", "c") - - if actualValue := list.Empty(); actualValue != false { - t.Errorf("Got %v expected %v", actualValue, false) + if actualValue, ok := list.Get(0); actualValue != "a" || !ok { + t.Errorf("Got %v expected %v", actualValue, "a") } - - if actualValue := list.Size(); actualValue != 3 { - t.Errorf("Got %v expected %v", actualValue, 3) + if actualValue, ok := list.Get(1); actualValue != "b" || !ok { + t.Errorf("Got %v expected %v", actualValue, "b") } - if actualValue, ok := list.Get(2); actualValue != "c" || !ok { t.Errorf("Got %v expected %v", actualValue, "c") } + if actualValue, ok := list.Get(3); actualValue != nil || ok { + t.Errorf("Got %v expected %v", actualValue, nil) + } + list.Remove(0) + if actualValue, ok := list.Get(0); actualValue != "b" || !ok { + t.Errorf("Got %v expected %v", actualValue, "b") + } +} - list.Swap(0, 2) - list.Swap(0, 2) +func TestListSwap(t *testing.T) { + list := New() + list.Add("a") + list.Add("b", "c") list.Swap(0, 1) - if actualValue, ok := list.Get(0); actualValue != "b" || !ok { t.Errorf("Got %v expected %v", actualValue, "c") } +} - list.Remove(2) - - if actualValue, ok := list.Get(2); actualValue != nil || ok { - t.Errorf("Got %v expected %v", actualValue, nil) +func TestListSort(t *testing.T) { + list := New() + list.Sort(utils.StringComparator) + list.Add("e", "f", "g", "a", "b", "c", "d") + list.Sort(utils.StringComparator) + for i := 1; i < list.Size(); i++ { + a, _ := list.Get(i - 1) + b, _ := list.Get(i) + if a.(string) > b.(string) { + t.Errorf("Not sorted! %s > %s", a, b) + } } +} - list.Remove(1) - list.Remove(0) - +func TestListClear(t *testing.T) { + list := New() + list.Add("e", "f", "g", "a", "b", "c", "d") + list.Clear() if actualValue := list.Empty(); actualValue != true { t.Errorf("Got %v expected %v", actualValue, true) } - if actualValue := list.Size(); actualValue != 0 { t.Errorf("Got %v expected %v", actualValue, 0) } +} - list.Add("a", "b", "c") - +func TestListContains(t *testing.T) { + list := New() + list.Add("a") + list.Add("b", "c") + if actualValue := list.Contains("a"); actualValue != true { + t.Errorf("Got %v expected %v", actualValue, true) + } if actualValue := list.Contains("a", "b", "c"); actualValue != true { t.Errorf("Got %v expected %v", actualValue, true) } - if actualValue := list.Contains("a", "b", "c", "d"); actualValue != false { t.Errorf("Got %v expected %v", actualValue, false) } - list.Clear() - if actualValue := list.Contains("a"); actualValue != false { t.Errorf("Got %v expected %v", actualValue, false) } - - if actualValue, ok := list.Get(0); actualValue != nil || ok { + if actualValue := list.Contains("a", "b", "c"); actualValue != false { t.Errorf("Got %v expected %v", actualValue, false) } +} - if actualValue := list.Empty(); actualValue != true { - t.Errorf("Got %v expected %v", actualValue, true) +func TestListValues(t *testing.T) { + list := New() + list.Add("a") + list.Add("b", "c") + if actualValue, expectedValue := fmt.Sprintf("%s%s%s", list.Values()...), "abc"; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) } +} - list.Insert(0, "h") - list.Insert(0, "e") - list.Insert(1, "f") - list.Insert(2, "g") - list.Insert(4, "i") - list.Insert(0, "a", "b") - list.Insert(list.Size(), "j", "k") - list.Insert(2, "c", "d") - - if actualValue, expectedValue := fmt.Sprintf("%s%s%s%s%s%s%s%s%s%s%s", list.Values()...), "abcdefghijk"; actualValue != expectedValue { +func TestListInsert(t *testing.T) { + list := New() + list.Insert(0, "b", "c") + list.Insert(0, "a") + list.Insert(10, "x") // ignore + if actualValue := list.Size(); actualValue != 3 { + t.Errorf("Got %v expected %v", actualValue, 3) + } + list.Insert(3, "d") // append + if actualValue := list.Size(); actualValue != 4 { + t.Errorf("Got %v expected %v", actualValue, 4) + } + if actualValue, expectedValue := fmt.Sprintf("%s%s%s%s", list.Values()...), "abcd"; actualValue != expectedValue { t.Errorf("Got %v expected %v", actualValue, expectedValue) } } -func TestDoublyLinkedListEnumerableAndIterator(t *testing.T) { +func TestListEach(t *testing.T) { list := New() list.Add("a", "b", "c") - - // Each list.Each(func(index int, value interface{}) { switch index { case 0: @@ -160,8 +193,11 @@ func TestDoublyLinkedListEnumerableAndIterator(t *testing.T) { t.Errorf("Too many") } }) +} - // Map +func TestListMap(t *testing.T) { + list := New() + list.Add("a", "b", "c") mappedList := list.Map(func(index int, value interface{}) interface{} { return "mapped: " + value.(string) }) @@ -177,8 +213,11 @@ func TestDoublyLinkedListEnumerableAndIterator(t *testing.T) { if mappedList.Size() != 3 { t.Errorf("Got %v expected %v", mappedList.Size(), 3) } +} - // Select +func TestListSelect(t *testing.T) { + list := New() + list.Add("a", "b", "c") selectedList := list.Select(func(index int, value interface{}) bool { return value.(string) >= "a" && value.(string) <= "b" }) @@ -191,8 +230,11 @@ func TestDoublyLinkedListEnumerableAndIterator(t *testing.T) { if selectedList.Size() != 2 { t.Errorf("Got %v expected %v", selectedList.Size(), 3) } +} - // Any +func TestListAny(t *testing.T) { + list := New() + list.Add("a", "b", "c") any := list.Any(func(index int, value interface{}) bool { return value.(string) == "c" }) @@ -205,8 +247,10 @@ func TestDoublyLinkedListEnumerableAndIterator(t *testing.T) { if any != false { t.Errorf("Got %v expected %v", any, false) } - - // All +} +func TestListAll(t *testing.T) { + list := New() + list.Add("a", "b", "c") all := list.All(func(index int, value interface{}) bool { return value.(string) >= "a" && value.(string) <= "c" }) @@ -219,8 +263,10 @@ func TestDoublyLinkedListEnumerableAndIterator(t *testing.T) { if all != false { t.Errorf("Got %v expected %v", all, false) } - - // Find +} +func TestListFind(t *testing.T) { + list := New() + list.Add("a", "b", "c") foundIndex, foundValue := list.Find(func(index int, value interface{}) bool { return value.(string) == "c" }) @@ -233,8 +279,29 @@ func TestDoublyLinkedListEnumerableAndIterator(t *testing.T) { if foundValue != nil || foundIndex != -1 { t.Errorf("Got %v at %v expected %v at %v", foundValue, foundIndex, nil, nil) } +} +func TestListChaining(t *testing.T) { + list := New() + list.Add("a", "b", "c") + chainedList := list.Select(func(index int, value interface{}) bool { + return value.(string) > "a" + }).Map(func(index int, value interface{}) interface{} { + return value.(string) + value.(string) + }) + if chainedList.Size() != 2 { + t.Errorf("Got %v expected %v", chainedList.Size(), 2) + } + if actualValue, ok := chainedList.Get(0); actualValue != "bb" || !ok { + t.Errorf("Got %v expected %v", actualValue, "b") + } + if actualValue, ok := chainedList.Get(1); actualValue != "cc" || !ok { + t.Errorf("Got %v expected %v", actualValue, "c") + } +} - // Iterator +func TestListIterator(t *testing.T) { + list := New() + list.Add("a", "b", "c") it := list.Iterator() for it.Next() { index := it.Index() @@ -263,7 +330,7 @@ func TestDoublyLinkedListEnumerableAndIterator(t *testing.T) { } } -func BenchmarkDoublyLinkedList(b *testing.B) { +func BenchmarkList(b *testing.B) { for i := 0; i < b.N; i++ { list := New() for n := 0; n < 1000; n++ { diff --git a/lists/singlylinkedlist/singlylinkedlist_test.go b/lists/singlylinkedlist/singlylinkedlist_test.go index d5879c3..c546217 100644 --- a/lists/singlylinkedlist/singlylinkedlist_test.go +++ b/lists/singlylinkedlist/singlylinkedlist_test.go @@ -32,116 +32,149 @@ import ( "testing" ) -func TestSinglyLinkedList(t *testing.T) { - +func TestListAdd(t *testing.T) { list := New() - - list.Sort(utils.StringComparator) - - list.Add("e", "f", "g", "a", "b", "c", "d") - - list.Sort(utils.StringComparator) - for i := 1; i < list.Size(); i++ { - a, _ := list.Get(i - 1) - b, _ := list.Get(i) - if a.(string) > b.(string) { - t.Errorf("Not sorted! %s > %s", a, b) - } + list.Add("a") + list.Add("b", "c") + if actualValue := list.Empty(); actualValue != false { + t.Errorf("Got %v expected %v", actualValue, false) } + if actualValue := list.Size(); actualValue != 3 { + t.Errorf("Got %v expected %v", actualValue, 3) + } + if actualValue, ok := list.Get(2); actualValue != "c" || !ok { + t.Errorf("Got %v expected %v", actualValue, "c") + } +} - list.Clear() - +func TestListRemove(t *testing.T) { + list := New() + list.Add("a") + list.Add("b", "c") + list.Remove(2) + if actualValue, ok := list.Get(2); actualValue != nil || ok { + t.Errorf("Got %v expected %v", actualValue, nil) + } + list.Remove(1) + list.Remove(0) + list.Remove(0) // no effect if actualValue := list.Empty(); actualValue != true { t.Errorf("Got %v expected %v", actualValue, true) } - if actualValue := list.Size(); actualValue != 0 { t.Errorf("Got %v expected %v", actualValue, 0) } +} +func TestListGet(t *testing.T) { + list := New() list.Add("a") list.Add("b", "c") - - if actualValue := list.Empty(); actualValue != false { - t.Errorf("Got %v expected %v", actualValue, false) + if actualValue, ok := list.Get(0); actualValue != "a" || !ok { + t.Errorf("Got %v expected %v", actualValue, "a") } - - if actualValue := list.Size(); actualValue != 3 { - t.Errorf("Got %v expected %v", actualValue, 3) + if actualValue, ok := list.Get(1); actualValue != "b" || !ok { + t.Errorf("Got %v expected %v", actualValue, "b") } - if actualValue, ok := list.Get(2); actualValue != "c" || !ok { t.Errorf("Got %v expected %v", actualValue, "c") } + if actualValue, ok := list.Get(3); actualValue != nil || ok { + t.Errorf("Got %v expected %v", actualValue, nil) + } + list.Remove(0) + if actualValue, ok := list.Get(0); actualValue != "b" || !ok { + t.Errorf("Got %v expected %v", actualValue, "b") + } +} - list.Swap(0, 2) - list.Swap(0, 2) +func TestListSwap(t *testing.T) { + list := New() + list.Add("a") + list.Add("b", "c") list.Swap(0, 1) - if actualValue, ok := list.Get(0); actualValue != "b" || !ok { t.Errorf("Got %v expected %v", actualValue, "c") } +} - list.Remove(2) - - if actualValue, ok := list.Get(2); actualValue != nil || ok { - t.Errorf("Got %v expected %v", actualValue, nil) +func TestListSort(t *testing.T) { + list := New() + list.Sort(utils.StringComparator) + list.Add("e", "f", "g", "a", "b", "c", "d") + list.Sort(utils.StringComparator) + for i := 1; i < list.Size(); i++ { + a, _ := list.Get(i - 1) + b, _ := list.Get(i) + if a.(string) > b.(string) { + t.Errorf("Not sorted! %s > %s", a, b) + } } +} - list.Remove(1) - list.Remove(0) - +func TestListClear(t *testing.T) { + list := New() + list.Add("e", "f", "g", "a", "b", "c", "d") + list.Clear() if actualValue := list.Empty(); actualValue != true { t.Errorf("Got %v expected %v", actualValue, true) } - if actualValue := list.Size(); actualValue != 0 { t.Errorf("Got %v expected %v", actualValue, 0) } +} - list.Add("a", "b", "c") - +func TestListContains(t *testing.T) { + list := New() + list.Add("a") + list.Add("b", "c") + if actualValue := list.Contains("a"); actualValue != true { + t.Errorf("Got %v expected %v", actualValue, true) + } if actualValue := list.Contains("a", "b", "c"); actualValue != true { t.Errorf("Got %v expected %v", actualValue, true) } - if actualValue := list.Contains("a", "b", "c", "d"); actualValue != false { t.Errorf("Got %v expected %v", actualValue, false) } - list.Clear() - if actualValue := list.Contains("a"); actualValue != false { t.Errorf("Got %v expected %v", actualValue, false) } - - if actualValue, ok := list.Get(0); actualValue != nil || ok { + if actualValue := list.Contains("a", "b", "c"); actualValue != false { t.Errorf("Got %v expected %v", actualValue, false) } +} - if actualValue := list.Empty(); actualValue != true { - t.Errorf("Got %v expected %v", actualValue, true) +func TestListValues(t *testing.T) { + list := New() + list.Add("a") + list.Add("b", "c") + if actualValue, expectedValue := fmt.Sprintf("%s%s%s", list.Values()...), "abc"; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) } +} - list.Insert(0, "h") - list.Insert(0, "e") - list.Insert(1, "f") - list.Insert(2, "g") - list.Insert(4, "i") - list.Insert(0, "a", "b") - list.Insert(list.Size(), "j", "k") - list.Insert(2, "c", "d") - - if actualValue, expectedValue := fmt.Sprintf("%s%s%s%s%s%s%s%s%s%s%s", list.Values()...), "abcdefghijk"; actualValue != expectedValue { +func TestListInsert(t *testing.T) { + list := New() + list.Insert(0, "b", "c") + list.Insert(0, "a") + list.Insert(10, "x") // ignore + if actualValue := list.Size(); actualValue != 3 { + t.Errorf("Got %v expected %v", actualValue, 3) + } + list.Insert(3, "d") // append + if actualValue := list.Size(); actualValue != 4 { + t.Errorf("Got %v expected %v", actualValue, 4) + } + if actualValue, expectedValue := fmt.Sprintf("%s%s%s%s", list.Values()...), "abcd"; actualValue != expectedValue { t.Errorf("Got %v expected %v", actualValue, expectedValue) } } -func TestSinglyLinkedListEnumerableAndIterator(t *testing.T) { +func TestListEach(t *testing.T) { list := New() list.Add("a", "b", "c") - - // Each list.Each(func(index int, value interface{}) { switch index { case 0: @@ -160,8 +193,11 @@ func TestSinglyLinkedListEnumerableAndIterator(t *testing.T) { t.Errorf("Too many") } }) +} - // Map +func TestListMap(t *testing.T) { + list := New() + list.Add("a", "b", "c") mappedList := list.Map(func(index int, value interface{}) interface{} { return "mapped: " + value.(string) }) @@ -177,8 +213,11 @@ func TestSinglyLinkedListEnumerableAndIterator(t *testing.T) { if mappedList.Size() != 3 { t.Errorf("Got %v expected %v", mappedList.Size(), 3) } +} - // Select +func TestListSelect(t *testing.T) { + list := New() + list.Add("a", "b", "c") selectedList := list.Select(func(index int, value interface{}) bool { return value.(string) >= "a" && value.(string) <= "b" }) @@ -191,8 +230,11 @@ func TestSinglyLinkedListEnumerableAndIterator(t *testing.T) { if selectedList.Size() != 2 { t.Errorf("Got %v expected %v", selectedList.Size(), 3) } +} - // Any +func TestListAny(t *testing.T) { + list := New() + list.Add("a", "b", "c") any := list.Any(func(index int, value interface{}) bool { return value.(string) == "c" }) @@ -205,8 +247,10 @@ func TestSinglyLinkedListEnumerableAndIterator(t *testing.T) { if any != false { t.Errorf("Got %v expected %v", any, false) } - - // All +} +func TestListAll(t *testing.T) { + list := New() + list.Add("a", "b", "c") all := list.All(func(index int, value interface{}) bool { return value.(string) >= "a" && value.(string) <= "c" }) @@ -219,8 +263,10 @@ func TestSinglyLinkedListEnumerableAndIterator(t *testing.T) { if all != false { t.Errorf("Got %v expected %v", all, false) } - - // Find +} +func TestListFind(t *testing.T) { + list := New() + list.Add("a", "b", "c") foundIndex, foundValue := list.Find(func(index int, value interface{}) bool { return value.(string) == "c" }) @@ -233,8 +279,29 @@ func TestSinglyLinkedListEnumerableAndIterator(t *testing.T) { if foundValue != nil || foundIndex != -1 { t.Errorf("Got %v at %v expected %v at %v", foundValue, foundIndex, nil, nil) } +} +func TestListChaining(t *testing.T) { + list := New() + list.Add("a", "b", "c") + chainedList := list.Select(func(index int, value interface{}) bool { + return value.(string) > "a" + }).Map(func(index int, value interface{}) interface{} { + return value.(string) + value.(string) + }) + if chainedList.Size() != 2 { + t.Errorf("Got %v expected %v", chainedList.Size(), 2) + } + if actualValue, ok := chainedList.Get(0); actualValue != "bb" || !ok { + t.Errorf("Got %v expected %v", actualValue, "b") + } + if actualValue, ok := chainedList.Get(1); actualValue != "cc" || !ok { + t.Errorf("Got %v expected %v", actualValue, "c") + } +} - // Iterator +func TestListIterator(t *testing.T) { + list := New() + list.Add("a", "b", "c") it := list.Iterator() for it.Next() { index := it.Index() @@ -263,7 +330,7 @@ func TestSinglyLinkedListEnumerableAndIterator(t *testing.T) { } } -func BenchmarkSinglyLinkedList(b *testing.B) { +func BenchmarkList(b *testing.B) { for i := 0; i < b.N; i++ { list := New() for n := 0; n < 1000; n++ {