skl_test.go
skl_test.go - Overview
This file contains tests for the Skiplist
data structure, covering basic operations, concurrency, iterators, and benchmarking.
Detailed Documentation
func (s *Skiplist) valid() bool
- Purpose: Checks if the skiplist is valid by verifying that the arena is not nil.
- Returns:
true
if the arena is not nil,false
otherwise.
func newValue(v int) []byte
- Purpose: Creates a byte slice from an integer, padding it with leading zeros to a length of 5.
- Parameters:
v
(int): The integer to convert.
- Returns: A byte slice representing the integer.
func length(s *Skiplist) int
- Purpose: Iterates over the skiplist to determine its exact size (number of elements).
- Parameters:
s
(*Skiplist): The skiplist to measure.
- Returns: The number of elements in the skiplist.
func TestEmpty(t *testing.T)
- Purpose: Tests the behavior of an empty skiplist, including Get, findNear, and iterator operations.
- Parameters:
t
(*testing.T): Testing object for reporting errors.
- Returns: None.
func TestBasic(t *testing.T)
- Purpose: Tests basic single-threaded insert, update, and get operations on the skiplist.
- Parameters:
t
(*testing.T): Testing object for reporting errors.
- Returns: None.
func TestConcurrentBasic(t *testing.T)
- Purpose: Tests concurrent writes followed by concurrent reads on the skiplist.
- Parameters:
t
(*testing.T): Testing object for reporting errors.
- Returns: None.
func TestConcurrentBasicBigValues(t *testing.T)
- Purpose: Tests concurrent writes of large values (1MB) followed by concurrent reads on the skiplist.
- Parameters:
t
(*testing.T): Testing object for reporting errors.
- Returns: None.
func TestOneKey(t *testing.T)
- Purpose: Tests concurrent read and write operations on a single key in the skiplist.
- Parameters:
t
(*testing.T): Testing object for reporting errors.
- Returns: None.
func TestFindNear(t *testing.T)
- Purpose: Tests the
findNear
function for locating keys near a given key. - Parameters:
t
(*testing.T): Testing object for reporting errors.
- Returns: None.
func TestIteratorNext(t *testing.T)
- Purpose: Tests iterating over all nodes in the skiplist from the beginning using
Next()
. - Parameters:
t
(*testing.T): Testing object for reporting errors.
- Returns: None.
func TestIteratorPrev(t *testing.T)
- Purpose: Tests iterating over all nodes in the skiplist from the end using
Prev()
. - Parameters:
t
(*testing.T): Testing object for reporting errors.
- Returns: None.
func TestIteratorSeek(t *testing.T)
- Purpose: Tests the
Seek
andSeekForPrev
functions of the iterator. - Parameters:
t
(*testing.T): Testing object for reporting errors.
- Returns: None.
func randomKey(rng *rand.Rand) []byte
- Purpose: Generates a random key as a byte slice using a random number generator.
- Parameters:
rng
(*rand.Rand): Random number generator.
- Returns: A random key as a byte slice.
func BenchmarkReadWrite(b *testing.B)
- Purpose: Benchmarks concurrent read and write operations on the skiplist.
- Parameters:
b
(*testing.B): Benchmarking object.
- Returns: None.
func BenchmarkReadWriteMap(b *testing.B)
- Purpose: Benchmarks concurrent read and write operations on a Go map with a mutex. Used for comparison with the skiplist.
- Parameters:
b
(*testing.B): Benchmarking object.
- Returns: None.
func BenchmarkWrite(b *testing.B)
- Purpose: Benchmarks concurrent write operations on the skiplist.
- Parameters:
b
(*testing.B): Benchmarking object.
- Returns: None.
Code Examples
// Example of creating a new value.
const val = newValue(10);
console.log(val); // Output: [48 48 48 49 48] (byte representation of "00010")