2024-01-08 22:52:46 +00:00
|
|
|
import std/sequtils
|
|
|
|
import pkg/chronos
|
|
|
|
|
2024-01-15 16:45:04 +00:00
|
|
|
import pkg/codex/utils/asynciter
|
|
|
|
|
2024-01-29 20:03:51 +00:00
|
|
|
import ../asynctest
|
2024-01-08 22:52:46 +00:00
|
|
|
import ./helpers
|
|
|
|
|
|
|
|
import pkg/codex/indexingstrategy
|
|
|
|
|
|
|
|
for offset in @[0, 1, 2, 100]:
|
|
|
|
suite "Indexing strategies (Offset: " & $offset & ")":
|
|
|
|
let
|
|
|
|
firstIndex = 0 + offset
|
|
|
|
lastIndex = 12 + offset
|
|
|
|
nIters = 3
|
2024-02-07 19:13:27 +00:00
|
|
|
linear = LinearStrategy.init(firstIndex, lastIndex, nIters)
|
|
|
|
stepped = SteppedStrategy.init(firstIndex, lastIndex, nIters)
|
2024-01-08 22:52:46 +00:00
|
|
|
|
|
|
|
test "linear":
|
|
|
|
check:
|
2024-01-15 16:45:04 +00:00
|
|
|
toSeq(linear.getIndicies(0)) == @[0, 1, 2, 3, 4].mapIt(it + offset)
|
|
|
|
toSeq(linear.getIndicies(1)) == @[5, 6, 7, 8, 9].mapIt(it + offset)
|
|
|
|
toSeq(linear.getIndicies(2)) == @[10, 11, 12].mapIt(it + offset)
|
2024-01-08 22:52:46 +00:00
|
|
|
|
|
|
|
test "stepped":
|
|
|
|
check:
|
2024-01-15 16:45:04 +00:00
|
|
|
toSeq(stepped.getIndicies(0)) == @[0, 3, 6, 9, 12].mapIt(it + offset)
|
|
|
|
toSeq(stepped.getIndicies(1)) == @[1, 4, 7, 10].mapIt(it + offset)
|
|
|
|
toSeq(stepped.getIndicies(2)) == @[2, 5, 8, 11].mapIt(it + offset)
|
2024-01-08 22:52:46 +00:00
|
|
|
|
|
|
|
suite "Indexing strategies":
|
|
|
|
let
|
2024-02-07 19:13:27 +00:00
|
|
|
linear = LinearStrategy.init(0, 10, 3)
|
|
|
|
stepped = SteppedStrategy.init(0, 10, 3)
|
2024-01-08 22:52:46 +00:00
|
|
|
|
|
|
|
test "smallest range 0":
|
|
|
|
let
|
2024-02-07 19:13:27 +00:00
|
|
|
l = LinearStrategy.init(0, 0, 1)
|
|
|
|
s = SteppedStrategy.init(0, 0, 1)
|
2024-01-08 22:52:46 +00:00
|
|
|
check:
|
2024-01-15 16:45:04 +00:00
|
|
|
toSeq(l.getIndicies(0)) == @[0]
|
|
|
|
toSeq(s.getIndicies(0)) == @[0]
|
2024-01-08 22:52:46 +00:00
|
|
|
|
|
|
|
test "smallest range 1":
|
|
|
|
let
|
2024-02-07 19:13:27 +00:00
|
|
|
l = LinearStrategy.init(0, 1, 1)
|
|
|
|
s = SteppedStrategy.init(0, 1, 1)
|
2024-01-08 22:52:46 +00:00
|
|
|
check:
|
2024-01-15 16:45:04 +00:00
|
|
|
toSeq(l.getIndicies(0)) == @[0, 1]
|
|
|
|
toSeq(s.getIndicies(0)) == @[0, 1]
|
2024-01-08 22:52:46 +00:00
|
|
|
|
|
|
|
test "first index must be smaller than last index":
|
2024-01-15 16:45:04 +00:00
|
|
|
expect IndexingWrongIndexError:
|
2024-02-07 19:13:27 +00:00
|
|
|
discard LinearStrategy.init(10, 0, 1)
|
2024-01-08 22:52:46 +00:00
|
|
|
|
2024-02-07 19:13:27 +00:00
|
|
|
test "iterations must be greater than zero":
|
2024-01-15 16:45:04 +00:00
|
|
|
expect IndexingWrongIterationsError:
|
2024-02-07 19:13:27 +00:00
|
|
|
discard LinearStrategy.init(0, 10, 0)
|
2024-01-08 22:52:46 +00:00
|
|
|
|
2024-06-26 02:38:04 +00:00
|
|
|
test "should split elements evenly when possible":
|
|
|
|
let
|
|
|
|
l = LinearStrategy.init(0, 11, 3)
|
|
|
|
check:
|
|
|
|
toSeq(l.getIndicies(0)) == @[0, 1, 2, 3].mapIt(it)
|
|
|
|
toSeq(l.getIndicies(1)) == @[4, 5, 6, 7].mapIt(it)
|
|
|
|
toSeq(l.getIndicies(2)) == @[8, 9, 10, 11].mapIt(it)
|
|
|
|
|
2024-01-08 22:52:46 +00:00
|
|
|
test "linear - oob":
|
2024-01-15 16:45:04 +00:00
|
|
|
expect IndexingError:
|
2024-01-08 22:52:46 +00:00
|
|
|
discard linear.getIndicies(3)
|
|
|
|
|
|
|
|
test "stepped - oob":
|
2024-01-15 16:45:04 +00:00
|
|
|
expect IndexingError:
|
2024-01-08 22:52:46 +00:00
|
|
|
discard stepped.getIndicies(3)
|