Skip to content

Commit

Permalink
assert() -> doAssert()
Browse files Browse the repository at this point in the history
Also fixed a test and added the other to the Nimble task.
  • Loading branch information
stefantalpalaru committed Mar 14, 2019
1 parent 7903f16 commit acd5a60
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 13 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
/tests/test_output_stream

9 changes: 7 additions & 2 deletions faststreams.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ requires "nim >= 0.17.0",
"ranges",
"std_shims"

task test, "run tests":
exec "nim c -r tests/test_output_stream.nim"
import ospaths, strutils

task test, "Run tests":
for filename in listFiles("tests"):
if filename.startsWith("tests" / "test_") and filename.endsWith(".nim"):
exec "nim c -r " & filename
rmFile filename[0..^5]

10 changes: 5 additions & 5 deletions faststreams/input_stream.nim
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ proc eob*(s: ByteStream): bool {.inline.} =
s.head != s.bufferEnd

proc peek*(s: ByteStream): byte {.inline.} =
assert s.head != s.bufferEnd
doAssert s.head != s.bufferEnd
return s.head[]

when debugHelpers:
Expand All @@ -121,7 +121,7 @@ proc read*(s: var ByteStream): byte =

proc checkReadAhead(s: ByteStreamVar, n: int): ptr byte =
result = s.head
assert distance(s.head, s.bufferEnd) >= n
doAssert distance(s.head, s.bufferEnd) >= n
s.head = s.head.shift(n)

template readBytes*(s: ByteStreamVar, n: int): auto =
Expand All @@ -133,9 +133,9 @@ proc next*(s: var ByteStream): Option[byte] =

proc bufferPos(s: ByteStream, pos: int): ptr byte =
let shiftFromEnd = pos - s.bufferEndPos
assert shiftFromEnd < 0
doAssert shiftFromEnd < 0
result = s.bufferEnd.shift shiftFromEnd
assert result >= s.bufferStart
doAssert result >= s.bufferStart

proc pos*(s: ByteStream): int {.inline.} =
s.bufferEndPos - distance(s.head, s.bufferEnd)
Expand All @@ -148,7 +148,7 @@ proc `[]`*(s: ByteStream, pos: int): byte {.inline.} =

proc rewind*(s: var ByteStream, delta: int) =
s.head = s.head.shift(-delta)
assert s.head >= s.bufferStart
doAssert s.head >= s.bufferStart

proc rewindTo*(s: var ByteStream, pos: int) {.inline.} =
s.head = s.bufferPos(pos)
Expand Down
6 changes: 3 additions & 3 deletions faststreams/output_stream.nim
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ proc getOutput*(s: OutputStreamVar, T: type string): string =
else:
result = newStringOfCap(s.pos)
for page in s.pages:
assert page.delayedWrites == 0
doAssert page.delayedWrites == 0
result.add page.buffer.toOpenArray(page.startOffset.int,
page.buffer.len - 1)

Expand All @@ -110,7 +110,7 @@ proc flushDelayedPages*(s: OutputStreamVar) =
# Send to output

proc delayFixedSizeWrite*(s: OutputStreamVar, size: int): DelayedWriteCursor =
assert size < pageSize
doAssert size < pageSize

let remainingBytesInPage = distance(s.head, s.bufferEnd)
if size > remainingBytesInPage:
Expand All @@ -132,7 +132,7 @@ proc delayVarSizeWrite*(s: OutputStreamVar, maxSize: int): DelayedWriteCursor =

proc decRef(x: var int16): int16 =
result = x - 1
assert result >= 0
doAssert result >= 0
x = result

proc endWrite*(cursor: DelayedWriteCursor, data: openarray[byte]) =
Expand Down
2 changes: 1 addition & 1 deletion tests/test_input_stream.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import
../faststreams

suite "input stream":
test "string output":
test "string input":
var input = repeat("1234 5678 90AB CDEF\n", 1000)
var stream = memoryStream(input)

Expand Down
3 changes: 2 additions & 1 deletion tests/test_output_stream.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ suite "output stream":
altOutput.add '\n'

let streamOutput = s.getOutput
check streamOutput == altOutput
# without the cast, in nim-0.19.0: "Error: type mismatch: got <seq[byte], string>" (for the `==` proc)
check cast[string](streamOutput) == altOutput

0 comments on commit acd5a60

Please sign in to comment.