diff --git a/lists/arraylist/arraylist_test.go b/lists/arraylist/arraylist_test.go index 9de67da..b4e131b 100644 --- a/lists/arraylist/arraylist_test.go +++ b/lists/arraylist/arraylist_test.go @@ -379,6 +379,20 @@ func TestListIteratorPrev(t *testing.T) { } } +func TestListIteratorReset(t *testing.T) { + list := New() + it := list.Iterator() + it.Reset() + list.Add("a", "b", "c") + for it.Next() { + } + it.Reset() + it.Next() + 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/lists/doublylinkedlist/doublylinkedlist_test.go b/lists/doublylinkedlist/doublylinkedlist_test.go index 1ff73b6..e24f376 100644 --- a/lists/doublylinkedlist/doublylinkedlist_test.go +++ b/lists/doublylinkedlist/doublylinkedlist_test.go @@ -379,6 +379,20 @@ func TestListIteratorPrev(t *testing.T) { } } +func TestListIteratorReset(t *testing.T) { + list := New() + it := list.Iterator() + it.Reset() + list.Add("a", "b", "c") + for it.Next() { + } + it.Reset() + it.Next() + 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/lists/singlylinkedlist/singlylinkedlist_test.go b/lists/singlylinkedlist/singlylinkedlist_test.go index 53c2ad8..6c0241d 100644 --- a/lists/singlylinkedlist/singlylinkedlist_test.go +++ b/lists/singlylinkedlist/singlylinkedlist_test.go @@ -338,6 +338,20 @@ func TestListIteratorNext(t *testing.T) { } } +func TestListIteratorReset(t *testing.T) { + list := New() + it := list.Iterator() + it.Reset() + list.Add("a", "b", "c") + for it.Next() { + } + it.Reset() + it.Next() + 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 ac4d778..228472c 100644 --- a/maps/treemap/treemap_test.go +++ b/maps/treemap/treemap_test.go @@ -406,6 +406,22 @@ func TestMapIteratorPrev(t *testing.T) { } } +func TestListIteratorReset(t *testing.T) { + m := NewWithIntComparator() + it := m.Iterator() + it.Reset() + m.Put(3, "c") + m.Put(1, "a") + m.Put(2, "b") + for it.Next() { + } + it.Reset() + it.Next() + if key, value := it.Key(), it.Value(); key != 1 || value != "a" { + t.Errorf("Got %v,%v expected %v,%v", key, value, 1, "a") + } +} + func BenchmarkMap(b *testing.B) { for i := 0; i < b.N; i++ { m := NewWithIntComparator() diff --git a/sets/treeset/treeset.go b/sets/treeset/treeset.go index 9a590ff..43a1717 100644 --- a/sets/treeset/treeset.go +++ b/sets/treeset/treeset.go @@ -154,6 +154,7 @@ func (iterator *Iterator) Index() int { // Reset sets the iterator to the initial state. // Call Next() to fetch the first element if any. func (iterator *Iterator) Reset() { + iterator.index = -1 iterator.iterator.Reset() } diff --git a/sets/treeset/treeset_test.go b/sets/treeset/treeset_test.go index ccdb16c..990cf3c 100644 --- a/sets/treeset/treeset_test.go +++ b/sets/treeset/treeset_test.go @@ -278,6 +278,20 @@ func TestSetIteratorPrev(t *testing.T) { } } +func TestListIteratorReset(t *testing.T) { + m := NewWithStringComparator() + it := m.Iterator() + it.Reset() + m.Add("a", "b", "c") + for it.Next() { + } + it.Reset() + it.Next() + if index, value := it.Index(), it.Value(); index != 0 || value != "a" { + t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "a") + } +} + func BenchmarkSet(b *testing.B) { for i := 0; i < b.N; i++ { set := NewWithIntComparator() diff --git a/stacks/arraystack/arraystack_test.go b/stacks/arraystack/arraystack_test.go index fad4ac0..89faa43 100644 --- a/stacks/arraystack/arraystack_test.go +++ b/stacks/arraystack/arraystack_test.go @@ -176,6 +176,22 @@ func TestStackIteratorPrev(t *testing.T) { } } +func TestListIteratorReset(t *testing.T) { + stack := New() + it := stack.Iterator() + it.Reset() + stack.Push("a") + stack.Push("b") + stack.Push("c") + for it.Next() { + } + it.Reset() + it.Next() + 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/stacks/linkedliststack/linkedliststack_test.go b/stacks/linkedliststack/linkedliststack_test.go index 4fb0002..f9f1df0 100644 --- a/stacks/linkedliststack/linkedliststack_test.go +++ b/stacks/linkedliststack/linkedliststack_test.go @@ -136,6 +136,22 @@ func TestStackIterator(t *testing.T) { } } +func TestListIteratorReset(t *testing.T) { + stack := New() + it := stack.Iterator() + it.Reset() + stack.Push("a") + stack.Push("b") + stack.Push("c") + for it.Next() { + } + it.Reset() + it.Next() + 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 d72375a..aed82c0 100644 --- a/trees/binaryheap/binaryheap_test.go +++ b/trees/binaryheap/binaryheap_test.go @@ -191,6 +191,22 @@ func TestBinaryHeapIteratorPrev(t *testing.T) { } } +func TestListIteratorReset(t *testing.T) { + tree := NewWithIntComparator() + it := tree.Iterator() + it.Reset() + tree.Push(2) + tree.Push(3) + tree.Push(1) + for it.Next() { + } + it.Reset() + it.Next() + if index, value := it.Index(), it.Value(); index != 0 || value != 1 { + t.Errorf("Got %v,%v expected %v,%v", index, value, 0, 1) + } +} + func BenchmarkBinaryHeap(b *testing.B) { for i := 0; i < b.N; i++ { heap := NewWithIntComparator() diff --git a/trees/redblacktree/redblacktree_test.go b/trees/redblacktree/redblacktree_test.go index 3f5987a..3795859 100644 --- a/trees/redblacktree/redblacktree_test.go +++ b/trees/redblacktree/redblacktree_test.go @@ -496,6 +496,22 @@ func TestRedBlackTreeIterator4(t *testing.T) { } } +func TestListIteratorReset(t *testing.T) { + tree := NewWithIntComparator() + tree.Put(3, "c") + tree.Put(1, "a") + tree.Put(2, "b") + it := tree.Iterator() + it.Reset() + for it.Next() { + } + it.Reset() + it.Next() + if key, value := it.Key(), it.Value(); key != 1 || value != "a" { + t.Errorf("Got %v,%v expected %v,%v", key, value, 1, "a") + } +} + func BenchmarkRedBlackTree(b *testing.B) { for i := 0; i < b.N; i++ { tree := NewWithIntComparator()