From 6a3102246db1ea2cdc8ccd29ecf318e3b0b50fb4 Mon Sep 17 00:00:00 2001 From: benbierens Date: Mon, 4 Dec 2023 13:35:49 +0100 Subject: [PATCH] Fixes issue where indexing strategy stepped gives wrong values for smallest of ranges --- codex/manifest/indexingstrategy.nim | 4 ++-- tests/codex/testindexingstrategy.nim | 11 ++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/codex/manifest/indexingstrategy.nim b/codex/manifest/indexingstrategy.nim index 69b3f8e8..19f8d9aa 100644 --- a/codex/manifest/indexingstrategy.nim +++ b/codex/manifest/indexingstrategy.nim @@ -54,5 +54,5 @@ method getIndicies*(self: SteppedIndexingStrategy, iteration: int): seq[int] = let first = self.firstIndex + iteration - last = first + (self.step * self.numberOfIterations) - toSeq(countup(first, last - 1, self.numberOfIterations)) + last = max(first, first - 1 + (self.step * self.numberOfIterations)) + toSeq(countup(first, last, self.numberOfIterations)) diff --git a/tests/codex/testindexingstrategy.nim b/tests/codex/testindexingstrategy.nim index 9b173685..c2fe212d 100644 --- a/tests/codex/testindexingstrategy.nim +++ b/tests/codex/testindexingstrategy.nim @@ -27,16 +27,21 @@ for offset in @[0, 1, 100]: stepped.getIndicies(1) == @[1, 4, 7, 10].mapIt(it + offset) stepped.getIndicies(2) == @[2, 5, 8, 11].mapIt(it + offset) -checksuite "Indexing strategies - oob": +checksuite "Indexing strategies": let linear = LinearIndexingStrategy.new(0, 10, 3) stepped = SteppedIndexingStrategy.new(0, 10, 3) - test "linear": + test "smallest range": + let s = SteppedIndexingStrategy.new(0, 0, 1) + check: + s.getIndicies(0) == @[0] + + test "linear - oob": expect AssertionDefect: discard linear.getIndicies(3) - test "stepped": + test "stepped - oob": expect AssertionDefect: discard stepped.getIndicies(3)