From cbc23a5b79203f9a468f09a79a03b0088fc1060e Mon Sep 17 00:00:00 2001 From: Emir Pasic Date: Sun, 26 Jun 2016 23:58:23 +0200 Subject: [PATCH] - test iterator first on all iterable data structures --- lists/arraylist/arraylist_test.go | 15 +++++++++++ .../doublylinkedlist/doublylinkedlist_test.go | 15 +++++++++++ .../singlylinkedlist/singlylinkedlist_test.go | 15 +++++++++++ maps/treemap/treemap_test.go | 14 ++++++++++ sets/treeset/treeset_test.go | 12 +++++++++ stacks/arraystack/arraystack_test.go | 17 ++++++++++++ .../linkedliststack/linkedliststack_test.go | 17 ++++++++++++ trees/binaryheap/binaryheap_test.go | 27 +++++++++++++++---- trees/redblacktree/redblacktree_test.go | 14 ++++++++++ 9 files changed, 141 insertions(+), 5 deletions(-) diff --git a/lists/arraylist/arraylist_test.go b/lists/arraylist/arraylist_test.go index 66b7499..fa2ba57 100644 --- a/lists/arraylist/arraylist_test.go +++ b/lists/arraylist/arraylist_test.go @@ -393,6 +393,21 @@ func TestListIteratorReset(t *testing.T) { } } +func TestListIteratorFirst(t *testing.T) { + list := New() + it := list.Iterator() + if actualValue, expectedValue := it.First(), false; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) + } + list.Add("a", "b", "c") + if actualValue, expectedValue := it.First(), true; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) + } + if index, value := it.Index(), it.Value(); index != 0 || value != "a" { + t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "a") + } +} + func TestListIteratorLast(t *testing.T) { list := New() it := list.Iterator() diff --git a/lists/doublylinkedlist/doublylinkedlist_test.go b/lists/doublylinkedlist/doublylinkedlist_test.go index 4a6b66a..fd8495d 100644 --- a/lists/doublylinkedlist/doublylinkedlist_test.go +++ b/lists/doublylinkedlist/doublylinkedlist_test.go @@ -393,6 +393,21 @@ func TestListIteratorReset(t *testing.T) { } } +func TestListIteratorFirst(t *testing.T) { + list := New() + it := list.Iterator() + if actualValue, expectedValue := it.First(), false; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) + } + list.Add("a", "b", "c") + if actualValue, expectedValue := it.First(), true; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) + } + if index, value := it.Index(), it.Value(); index != 0 || value != "a" { + t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "a") + } +} + func TestListIteratorLast(t *testing.T) { list := New() it := list.Iterator() diff --git a/lists/singlylinkedlist/singlylinkedlist_test.go b/lists/singlylinkedlist/singlylinkedlist_test.go index 6c0241d..c313347 100644 --- a/lists/singlylinkedlist/singlylinkedlist_test.go +++ b/lists/singlylinkedlist/singlylinkedlist_test.go @@ -352,6 +352,21 @@ func TestListIteratorReset(t *testing.T) { } } +func TestListIteratorFirst(t *testing.T) { + list := New() + it := list.Iterator() + if actualValue, expectedValue := it.First(), false; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) + } + list.Add("a", "b", "c") + if actualValue, expectedValue := it.First(), true; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) + } + if index, value := it.Index(), it.Value(); index != 0 || value != "a" { + t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "a") + } +} + func BenchmarkList(b *testing.B) { for i := 0; i < b.N; i++ { list := New() diff --git a/maps/treemap/treemap_test.go b/maps/treemap/treemap_test.go index 76f13bf..2a4f82d 100644 --- a/maps/treemap/treemap_test.go +++ b/maps/treemap/treemap_test.go @@ -422,6 +422,20 @@ func TestMapIteratorReset(t *testing.T) { } } +func TestMapIteratorFirst(t *testing.T) { + m := NewWithIntComparator() + m.Put(3, "c") + m.Put(1, "a") + m.Put(2, "b") + it := m.Iterator() + if actualValue, expectedValue := it.First(), true; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) + } + if key, value := it.Key(), it.Value(); key != 1 || value != "a" { + t.Errorf("Got %v,%v expected %v,%v", key, value, 1, "a") + } +} + func TestMapIteratorLast(t *testing.T) { m := NewWithIntComparator() m.Put(3, "c") diff --git a/sets/treeset/treeset_test.go b/sets/treeset/treeset_test.go index b645eb0..c419a6b 100644 --- a/sets/treeset/treeset_test.go +++ b/sets/treeset/treeset_test.go @@ -292,6 +292,18 @@ func TestSetIteratorReset(t *testing.T) { } } +func TestSetIteratorFirst(t *testing.T) { + set := NewWithStringComparator() + set.Add("a", "b", "c") + it := set.Iterator() + if actualValue, expectedValue := it.First(), true; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) + } + if index, value := it.Index(), it.Value(); index != 0 || value != "a" { + t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "a") + } +} + func TestSetIteratorLast(t *testing.T) { set := NewWithStringComparator() set.Add("a", "b", "c") diff --git a/stacks/arraystack/arraystack_test.go b/stacks/arraystack/arraystack_test.go index 0fd2118..b744bc1 100644 --- a/stacks/arraystack/arraystack_test.go +++ b/stacks/arraystack/arraystack_test.go @@ -192,6 +192,23 @@ func TestStackIteratorReset(t *testing.T) { } } +func TestStackIteratorFirst(t *testing.T) { + stack := New() + it := stack.Iterator() + if actualValue, expectedValue := it.First(), false; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) + } + stack.Push("a") + stack.Push("b") + stack.Push("c") + if actualValue, expectedValue := it.First(), true; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) + } + if index, value := it.Index(), it.Value(); index != 0 || value != "c" { + t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "c") + } +} + func TestStackIteratorLast(t *testing.T) { stack := New() it := stack.Iterator() diff --git a/stacks/linkedliststack/linkedliststack_test.go b/stacks/linkedliststack/linkedliststack_test.go index 4283da0..ce97b8a 100644 --- a/stacks/linkedliststack/linkedliststack_test.go +++ b/stacks/linkedliststack/linkedliststack_test.go @@ -152,6 +152,23 @@ func TestStackIteratorReset(t *testing.T) { } } +func TestStackIteratorFirst(t *testing.T) { + stack := New() + it := stack.Iterator() + if actualValue, expectedValue := it.First(), false; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) + } + stack.Push("a") + stack.Push("b") + stack.Push("c") + if actualValue, expectedValue := it.First(), true; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) + } + if index, value := it.Index(), it.Value(); index != 0 || value != "c" { + t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "c") + } +} + func BenchmarkStack(b *testing.B) { for i := 0; i < b.N; i++ { stack := New() diff --git a/trees/binaryheap/binaryheap_test.go b/trees/binaryheap/binaryheap_test.go index 3f925f7..17311a3 100644 --- a/trees/binaryheap/binaryheap_test.go +++ b/trees/binaryheap/binaryheap_test.go @@ -192,12 +192,12 @@ func TestBinaryHeapIteratorPrev(t *testing.T) { } func TestBinaryHeapIteratorReset(t *testing.T) { - tree := NewWithIntComparator() - it := tree.Iterator() + heap := NewWithIntComparator() + it := heap.Iterator() it.Reset() - tree.Push(2) - tree.Push(3) - tree.Push(1) + heap.Push(2) + heap.Push(3) + heap.Push(1) for it.Next() { } it.Reset() @@ -207,6 +207,23 @@ func TestBinaryHeapIteratorReset(t *testing.T) { } } +func TestStackIteratorFirst(t *testing.T) { + heap := NewWithIntComparator() + it := heap.Iterator() + if actualValue, expectedValue := it.First(), false; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) + } + heap.Push(3) // [3] + heap.Push(2) // [2,3] + heap.Push(1) // [1,3,2](2 swapped with 1, hence last) + if actualValue, expectedValue := it.First(), true; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) + } + if index, value := it.Index(), it.Value(); index != 0 || value != 1 { + t.Errorf("Got %v,%v expected %v,%v", index, value, 0, 1) + } +} + func TestBinaryHeapIteratorLast(t *testing.T) { tree := NewWithIntComparator() it := tree.Iterator() diff --git a/trees/redblacktree/redblacktree_test.go b/trees/redblacktree/redblacktree_test.go index 9c321f1..495b68e 100644 --- a/trees/redblacktree/redblacktree_test.go +++ b/trees/redblacktree/redblacktree_test.go @@ -512,6 +512,20 @@ func TestRedBlackTreeIteratorReset(t *testing.T) { } } +func TestRedBlackTreeIteratorFirst(t *testing.T) { + tree := NewWithIntComparator() + tree.Put(3, "c") + tree.Put(1, "a") + tree.Put(2, "b") + it := tree.Iterator() + if actualValue, expectedValue := it.First(), true; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) + } + if key, value := it.Key(), it.Value(); key != 1 || value != "a" { + t.Errorf("Got %v,%v expected %v,%v", key, value, 1, "a") + } +} + func TestRedBlackTreeIteratorLast(t *testing.T) { tree := NewWithIntComparator() tree.Put(3, "c")