nim-codex/tests/codex/testindexingstrategy.nim

66 lines
1.9 KiB
Nim
Raw Normal View History

2023-12-01 10:20:02 +01:00
import std/sequtils
import pkg/chronos
import pkg/asynctest
import ./helpers
2023-12-22 17:17:15 -06:00
import pkg/codex/indexingstrategy
2023-12-01 10:20:02 +01:00
2023-12-05 10:06:47 +01:00
for offset in @[0, 1, 2, 100]:
2023-12-22 17:17:15 -06:00
suite "Indexing strategies (Offset: " & $offset & ")":
2023-12-01 10:20:02 +01:00
let
firstIndex = 0 + offset
lastIndex = 12 + offset
2023-12-01 10:20:02 +01:00
nIters = 3
linear = LinearIndexingStrategy.new(firstIndex, lastIndex, nIters)
stepped = SteppedIndexingStrategy.new(firstIndex, lastIndex, nIters)
test "linear":
check:
2023-12-05 10:06:47 +01:00
linear.getIndicies(0) == @[0, 1, 2, 3, 4].mapIt(it + offset)
linear.getIndicies(1) == @[5, 6, 7, 8, 9].mapIt(it + offset)
linear.getIndicies(2) == @[10, 11, 12].mapIt(it + offset)
2023-12-01 10:20:02 +01:00
test "stepped":
check:
2023-12-05 10:06:47 +01:00
stepped.getIndicies(0) == @[0, 3, 6, 9, 12].mapIt(it + offset)
stepped.getIndicies(1) == @[1, 4, 7, 10].mapIt(it + offset)
stepped.getIndicies(2) == @[2, 5, 8, 11].mapIt(it + offset)
2023-12-22 17:17:15 -06:00
suite "Indexing strategies":
let
linear = LinearIndexingStrategy.new(0, 10, 3)
stepped = SteppedIndexingStrategy.new(0, 10, 3)
2023-12-05 10:06:47 +01:00
test "smallest range 0":
let
l = LinearIndexingStrategy.new(0, 0, 1)
s = SteppedIndexingStrategy.new(0, 0, 1)
check:
2023-12-05 10:06:47 +01:00
l.getIndicies(0) == @[0]
s.getIndicies(0) == @[0]
2023-12-05 10:06:47 +01:00
test "smallest range 1":
let
l = LinearIndexingStrategy.new(0, 1, 1)
s = SteppedIndexingStrategy.new(0, 1, 1)
check:
l.getIndicies(0) == @[0, 1]
s.getIndicies(0) == @[0, 1]
test "first index must be smaller than last index":
expect AssertionDefect:
discard LinearIndexingStrategy.new(10, 0, 1)
test "numberOfIterations must be greater than zero":
expect AssertionDefect:
discard LinearIndexingStrategy.new(0, 10, 0)
test "linear - oob":
expect AssertionDefect:
discard linear.getIndicies(3)
test "stepped - oob":
expect AssertionDefect:
discard stepped.getIndicies(3)