merge_iterator_test.go
merge_iterator_test.go - Overview
This file contains tests for the MergeIterator
implementation, ensuring it correctly merges multiple iterators, handles duplicates, and supports both forward and reverse iteration. It also includes a SimpleIterator
to facilitate testing.
Detailed Documentation
SimpleIterator
type SimpleIterator struct {
keys [][]byte
vals [][]byte
idx int
reversed bool
}
A simple iterator implementation for testing purposes.
keys
: The keys to iterate over.vals
: The values corresponding to the keys.idx
: The current index.reversed
: Indicates whether the iterator is reversed.
closeCount
var (
closeCount int
)
A package-level variable to track the number of times the Close
method is called on SimpleIterator
instances.
(*SimpleIterator) Close() error
func (s *SimpleIterator) Close() error
Increments closeCount
and returns nil.
- Returns:
error
- Always nil.
(*SimpleIterator) Next()
func (s *SimpleIterator) Next()
Moves the iterator to the next position, either forward or backward depending on the reversed
flag.
(*SimpleIterator) Rewind()
func (s *SimpleIterator) Rewind()
Resets the iterator to the beginning (or end if reversed).
(*SimpleIterator) Seek(key []byte)
func (s *SimpleIterator) Seek(key []byte)
Seeks to the given key.
- Parameters:
key
: The key to seek to.
(*SimpleIterator) Key() []byte
func (s *SimpleIterator) Key() []byte
Returns the key at the current index.
- Returns:
[]byte
- The key at the current index.
(*SimpleIterator) Value() y.ValueStruct
func (s *SimpleIterator) Value() y.ValueStruct
Returns the value at the current index.
- Returns:
y.ValueStruct
- The value at the current index.
(*SimpleIterator) Valid() bool
func (s *SimpleIterator) Valid() bool
Checks if the current index is valid.
- Returns:
bool
- True if the current index is valid, false otherwise.
newSimpleIterator(keys []string, vals []string, reversed bool) *SimpleIterator
func newSimpleIterator(keys []string, vals []string, reversed bool) *SimpleIterator
Creates a new SimpleIterator
.
- Parameters:
keys
: The keys for the iterator.vals
: The values for the iterator.reversed
: Whether the iterator should be reversed.
- Returns:
*SimpleIterator
- A pointer to the newSimpleIterator
.
getAll(it y.Iterator) ([]string, []string)
func getAll(it y.Iterator) ([]string, []string)
Collects all keys and values from an iterator into string slices.
- Parameters:
it
: The iterator to read from.
- Returns:
([]string, []string)
- A tuple of string slices containing the keys and values.
closeAndCheck(t *testing.T, it y.Iterator, expected int)
func closeAndCheck(t *testing.T, it y.Iterator, expected int)
Closes the iterator and asserts that the closeCount
matches the expected value.
- Parameters:
t
: The testing object.it
: The iterator to close.expected
: The expected value ofcloseCount
.
TestSimpleIterator(t *testing.T)
func TestSimpleIterator(t *testing.T)
Tests the basic functionality of the SimpleIterator
.
reversed(a []string) []string
func reversed(a []string) []string
Reverses a string slice.
- Parameters:
a
: The string slice to reverse.
- Returns:
[]string
- The reversed string slice.
TestMergeSingle(t *testing.T)
func TestMergeSingle(t *testing.T)
Tests merging a single iterator using NewMergeIterator
.
TestMergeSingleReversed(t *testing.T)
func TestMergeSingleReversed(t *testing.T)
Tests merging a single reversed iterator.
TestMergeMore(t *testing.T)
func TestMergeMore(t *testing.T)
Tests merging multiple iterators, including cases with and without duplicates, in both forward and reverse directions.
TestMergeIteratorNested(t *testing.T)
func TestMergeIteratorNested(t *testing.T)
Tests nested MergeIterator
to ensure the Iterator
interface is satisfied.
TestMergeIteratorSeek(t *testing.T)
func TestMergeIteratorSeek(t *testing.T)
Tests the Seek
functionality of the MergeIterator
.
TestMergeIteratorSeekReversed(t *testing.T)
func TestMergeIteratorSeekReversed(t *testing.T)
Tests the Seek
functionality of the MergeIterator
in reversed order.
TestMergeIteratorSeekInvalid(t *testing.T)
func TestMergeIteratorSeekInvalid(t *testing.T)
Tests Seek
with an invalid key, ensuring the iterator becomes invalid.
TestMergeIteratorSeekInvalidReversed(t *testing.T)
func TestMergeIteratorSeekInvalidReversed(t *testing.T)
Tests Seek
with an invalid key in reversed order, ensuring the iterator becomes invalid.
TestMergeIteratorDuplicate(t *testing.T)
func TestMergeIteratorDuplicate(t *testing.T)
Tests the MergeIterator
with duplicate keys in the input iterators.
TestMergeDuplicates(t *testing.T)
func TestMergeDuplicates(t *testing.T)
Tests merging iterators containing duplicate keys.