diff options
author | Dominik Picheta <dominikpicheta@googlemail.com> | 2018-04-04 13:54:40 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-04 13:54:40 +0100 |
commit | 7ef0cfebb56e3659f9c2fe07a2edafea96b0ff0d (patch) | |
tree | d3ea26bae4f7d9a5c226dc9ca1a7c9c4b1742814 | |
parent | abfcbaa74c8951b1df573c79f67be65af15e6c22 (diff) | |
parent | 34df046d37f7190834e1807dc4766c7e05b6c431 (diff) | |
download | Nim-7ef0cfebb56e3659f9c2fe07a2edafea96b0ff0d.tar.gz |
Merge pull request #7495 from GULPF/fix-fill-bug
Fix algorithm.fill for empty input
-rw-r--r-- | lib/pure/algorithm.nim | 27 | ||||
-rw-r--r-- | tests/stdlib/talgorithm.nim | 11 |
2 files changed, 24 insertions, 14 deletions
diff --git a/lib/pure/algorithm.nim b/lib/pure/algorithm.nim index 2f7b44b31..45e031574 100644 --- a/lib/pure/algorithm.nim +++ b/lib/pure/algorithm.nim @@ -24,16 +24,20 @@ proc `*`*(x: int, order: SortOrder): int {.inline.} = var y = order.ord - 1 result = (x xor y) - y -proc fill*[T](a: var openArray[T], first, last: Natural, value: T) = - ## fills the array ``a[first..last]`` with `value`. +template fillImpl[T](a: var openArray[T], first, last: int, value: T) = var x = first while x <= last: a[x] = value inc(x) +proc fill*[T](a: var openArray[T], first, last: Natural, value: T) = + ## fills the array ``a[first..last]`` with `value`. + fillImpl(a, first, last, value) + proc fill*[T](a: var openArray[T], value: T) = ## fills the array `a` with `value`. - fill(a, 0, a.high, value) + fillImpl(a, 0, a.high, value) + proc reverse*[T](a: var openArray[T], first, last: Natural) = ## reverses the array ``a[first..last]``. @@ -494,3 +498,20 @@ when isMainModule: doAssert s4 == "xxxefgabcdxxx" doAssert s5.rotateLeft(3 ..< 10, 11) == 6 doAssert s5 == "xxxefgabcdxxx" + + block product: + doAssert product(newSeq[seq[int]]()) == newSeq[seq[int]](), "empty input" + doAssert product(@[newSeq[int](), @[], @[]]) == newSeq[seq[int]](), "bit more empty input" + doAssert product(@[@[1,2]]) == @[@[1,2]], "a simple case of one element" + doAssert product(@[@[1,2], @[3,4]]) == @[@[2,4],@[1,4],@[2,3],@[1,3]], "two elements" + doAssert product(@[@[1,2], @[3,4], @[5,6]]) == @[@[2,4,6],@[1,4,6],@[2,3,6],@[1,3,6], @[2,4,5],@[1,4,5],@[2,3,5],@[1,3,5]], "three elements" + doAssert product(@[@[1,2], @[]]) == newSeq[seq[int]](), "two elements, but one empty" + + block lowerBound: + doAssert lowerBound([1,2,4], 3, system.cmp[int]) == 2 + doAssert lowerBound([1,2,2,3], 4, system.cmp[int]) == 4 + doAssert lowerBound([1,2,3,10], 11) == 4 + + block fillEmptySeq: + var s = newSeq[int]() + s.fill(0) \ No newline at end of file diff --git a/tests/stdlib/talgorithm.nim b/tests/stdlib/talgorithm.nim deleted file mode 100644 index f200e54c5..000000000 --- a/tests/stdlib/talgorithm.nim +++ /dev/null @@ -1,11 +0,0 @@ -import algorithm - -doAssert product[int](newSeq[seq[int]]()) == newSeq[seq[int]](), "empty input" -doAssert product[int](@[newSeq[int](), @[], @[]]) == newSeq[seq[int]](), "bit more empty input" -doAssert product(@[@[1,2]]) == @[@[1,2]], "a simple case of one element" -doAssert product(@[@[1,2], @[3,4]]) == @[@[2,4],@[1,4],@[2,3],@[1,3]], "two elements" -doAssert product(@[@[1,2], @[3,4], @[5,6]]) == @[@[2,4,6],@[1,4,6],@[2,3,6],@[1,3,6], @[2,4,5],@[1,4,5],@[2,3,5],@[1,3,5]], "three elements" -doAssert product(@[@[1,2], @[]]) == newSeq[seq[int]](), "two elements, but one empty" -doAssert lowerBound([1,2,4], 3, system.cmp[int]) == 2 -doAssert lowerBound([1,2,2,3], 4, system.cmp[int]) == 4 -doAssert lowerBound([1,2,3,10], 11) == 4 |