assert() -> doAssert()

and refactored the "test" task in libp2p.nimble
This commit is contained in:
Ștefan Talpalaru 2019-03-14 03:55:47 +01:00
parent a736334fe1
commit 7cbb89c7e4
No known key found for this signature in database
GPG Key ID: CBF7934204F1B6F9
5 changed files with 26 additions and 33 deletions

View File

@ -11,18 +11,11 @@ requires "nim > 0.18.0",
"nimcrypto >= 0.3.9",
"chronos"
import ospaths, strutils
task test, "Runs the test suite":
exec "nim c -r tests/testvarint"
exec "nim c -r tests/testbase58"
exec "nim c -r tests/testbase32"
exec "nim c -r tests/testbase64"
exec "nim c -r tests/testmultiaddress"
exec "nim c -r tests/testmultihash"
exec "nim c -r tests/testmultibase"
exec "nim c -r tests/testcid"
exec "nim c -r tests/testecnist"
exec "nim c -r tests/testrsa"
exec "nim c -r tests/tested25519"
exec "nim c -r tests/testcrypto"
exec "nim c -r tests/testpeer"
exec "nim c -r tests/testdaemon"
for filename in listFiles("tests"):
if filename.startsWith("tests" / "test") and filename.endsWith(".nim"):
exec "nim c -r " & filename
rmFile filename[0..^5]

View File

@ -87,7 +87,7 @@ proc random*(t: typedesc[PrivateKey], scheme: PKScheme,
##
## ``bits`` is number of bits for RSA key, ``bits`` value must be in
## [512, 4096], default value is 2048 bits.
assert(scheme in SupportedSchemes)
doAssert(scheme in SupportedSchemes)
result = PrivateKey(scheme: scheme)
if scheme == RSA:
result.rsakey = RsaPrivateKey.random(bits)
@ -102,7 +102,7 @@ proc random*(t: typedesc[KeyPair], scheme: PKScheme,
##
## ``bits`` is number of bits for RSA key, ``bits`` value must be in
## [512, 4096], default value is 2048 bits.
assert(scheme in SupportedSchemes)
doAssert(scheme in SupportedSchemes)
result.seckey = PrivateKey(scheme: scheme)
result.pubkey = PublicKey(scheme: scheme)
if scheme == RSA:

View File

@ -336,9 +336,9 @@ proc asn1EncodeOid*(dest: var openarray[byte], value: openarray[int]): int =
## but number of bytes (octets) required will be returned.
var buffer: array[16, byte]
result = 1
assert(len(value) >= 2)
assert(value[0] >= 1 and value[0] < 2)
assert(value[1] >= 1 and value[1] <= 39)
doAssert(len(value) >= 2)
doAssert(value[0] >= 1 and value[0] < 2)
doAssert(value[1] >= 1 and value[1] <= 39)
var oidlen = 1
for i in 2..<len(value):
oidlen += asn1EncodeTag(buffer, cast[uint64](value[i]))
@ -693,7 +693,7 @@ proc write*[T: Asn1Buffer|Asn1Composite](abc: var T, tag: Asn1Tag) =
##
## This procedure must be used to write `NULL`, `0` or empty `BIT STRING`,
## `OCTET STRING` types.
assert(tag in {Asn1Tag.Null, Asn1Tag.Integer, Asn1Tag.BitString,
doAssert(tag in {Asn1Tag.Null, Asn1Tag.Integer, Asn1Tag.BitString,
Asn1Tag.OctetString})
var length: int
if tag == Asn1Tag.Null:
@ -739,7 +739,7 @@ proc write*[T: Asn1Buffer|Asn1Composite](abc: var T, tag: Asn1Tag,
##
## For `BIT STRING` you can use ``bits`` argument to specify number of used
## bits.
assert(tag in {Asn1Tag.Integer, Asn1Tag.OctetString, Asn1Tag.BitString,
doAssert(tag in {Asn1Tag.Integer, Asn1Tag.OctetString, Asn1Tag.BitString,
Asn1Tag.Oid})
var length: int
if tag == Asn1Tag.Integer:
@ -761,7 +761,7 @@ proc write*[T: Asn1Buffer|Asn1Composite](abc: var T, tag: Asn1Tag,
abc.offset += length
proc write*[T: Asn1Buffer|Asn1Composite](abc: var T, value: Asn1Composite) =
assert(len(value) > 0, "Composite value not finished")
doAssert(len(value) > 0, "Composite value not finished")
var length: int
if value.tag == Asn1Tag.Sequence:
length = asn1EncodeSequence(abc.toOpenArray(), value.buffer)

View File

@ -135,15 +135,15 @@ proc write*(pb: var ProtoBuffer, field: ProtoField) =
var res: VarintStatus
pb.buffer.setLen(len(pb.buffer) + vsizeof(field))
res = PB.putUVarint(pb.toOpenArray(), length, protoHeader(field))
assert(res == VarintStatus.Success)
doAssert(res == VarintStatus.Success)
pb.offset += length
case field.kind
of ProtoFieldKind.Varint:
res = PB.putUVarint(pb.toOpenArray(), length, field.vint)
assert(res == VarintStatus.Success)
doAssert(res == VarintStatus.Success)
pb.offset += length
of ProtoFieldKind.Fixed64:
assert(pb.isEnough(8))
doAssert(pb.isEnough(8))
var value = cast[uint64](field.vfloat64)
pb.buffer[pb.offset] = byte(value and 0xFF'u32)
pb.buffer[pb.offset + 1] = byte((value shr 8) and 0xFF'u32)
@ -155,7 +155,7 @@ proc write*(pb: var ProtoBuffer, field: ProtoField) =
pb.buffer[pb.offset + 7] = byte((value shr 56) and 0xFF'u32)
pb.offset += 8
of ProtoFieldKind.Fixed32:
assert(pb.isEnough(4))
doAssert(pb.isEnough(4))
var value = cast[uint32](field.vfloat32)
pb.buffer[pb.offset] = byte(value and 0xFF'u32)
pb.buffer[pb.offset + 1] = byte((value shr 8) and 0xFF'u32)
@ -164,9 +164,9 @@ proc write*(pb: var ProtoBuffer, field: ProtoField) =
pb.offset += 4
of ProtoFieldKind.Length:
res = PB.putUVarint(pb.toOpenArray(), length, uint(len(field.vbuffer)))
assert(res == VarintStatus.Success)
doAssert(res == VarintStatus.Success)
pb.offset += length
assert(pb.isEnough(len(field.vbuffer)))
doAssert(pb.isEnough(len(field.vbuffer)))
if len(field.vbuffer) > 0:
copyMem(addr pb.buffer[pb.offset], unsafeAddr field.vbuffer[0],
len(field.vbuffer))
@ -176,13 +176,13 @@ proc write*(pb: var ProtoBuffer, field: ProtoField) =
proc finish*(pb: var ProtoBuffer) =
## Prepare protobuf's buffer ``pb`` for writing to stream.
assert(len(pb.buffer) > 0)
doAssert(len(pb.buffer) > 0)
if WithVarintLength in pb.options:
let size = uint(len(pb.buffer) - 10)
let pos = 10 - vsizeof(size)
var usedBytes = 0
let res = PB.putUVarint(pb.buffer.toOpenArray(pos, 9), usedBytes, size)
assert(res == VarintStatus.Success)
doAssert(res == VarintStatus.Success)
pb.offset = pos
else:
pb.offset = 0
@ -273,6 +273,6 @@ proc enterSubmessage*(pb: var ProtoBuffer): int =
proc skipSubmessage*(pb: var ProtoBuffer) =
## Skip current protobuf's sub-message and adjust internal offset to the
## end of sub-message.
assert(pb.length != 0)
doAssert(pb.length != 0)
pb.offset += pb.length
pb.length = 0

View File

@ -64,7 +64,7 @@ proc writeVarint*(vb: var VBuffer, value: LPSomeUVarint) =
vb.buffer.setLen(len(vb.buffer) + vsizeof(v))
let res = LP.putUVarint(toOpenArray(vb.buffer, vb.offset, len(vb.buffer) - 1),
length, v)
assert(res == VarintStatus.Success)
doAssert(res == VarintStatus.Success)
vb.offset += length
proc writeSeq*[T: byte|char](vb: var VBuffer, value: openarray[T]) =
@ -74,7 +74,7 @@ proc writeSeq*[T: byte|char](vb: var VBuffer, value: openarray[T]) =
vb.buffer.setLen(len(vb.buffer) + vsizeof(len(value)) + len(value))
let res = LP.putUVarint(toOpenArray(vb.buffer, vb.offset, len(vb.buffer) - 1),
length, uint(len(value)))
assert(res == VarintStatus.Success)
doAssert(res == VarintStatus.Success)
vb.offset += length
if len(value) > 0:
copyMem(addr vb.buffer[vb.offset], unsafeAddr value[0], len(value))