use doAssert in tests (#16486)

This commit is contained in:
flywind
2020-12-28 07:13:21 -06:00
committed by GitHub
parent f9a15dbae9
commit 6d442a40a6
78 changed files with 1056 additions and 1056 deletions

View File

@@ -6,4 +6,4 @@ test_queue.push(7)
test_queue.push(3)
test_queue.push(9)
let i = test_queue.pushpop(10)
assert i == 3
doAssert i == 3

View File

@@ -20,31 +20,31 @@ test()
block:
# Tests for lowerBound
var arr = @[1, 2, 3, 5, 6, 7, 8, 9]
assert arr.lowerBound(0) == 0
assert arr.lowerBound(4) == 3
assert arr.lowerBound(5) == 3
assert arr.lowerBound(10) == 8
doAssert arr.lowerBound(0) == 0
doAssert arr.lowerBound(4) == 3
doAssert arr.lowerBound(5) == 3
doAssert arr.lowerBound(10) == 8
arr = @[1, 5, 10]
assert arr.lowerBound(4) == 1
assert arr.lowerBound(5) == 1
assert arr.lowerBound(6) == 2
doAssert arr.lowerBound(4) == 1
doAssert arr.lowerBound(5) == 1
doAssert arr.lowerBound(6) == 2
# Tests for isSorted
var srt1 = [1, 2, 3, 4, 4, 4, 4, 5]
var srt2 = ["iello", "hello"]
var srt3 = [1.0, 1.0, 1.0]
var srt4: seq[int]
assert srt1.isSorted(cmp) == true
assert srt2.isSorted(cmp) == false
assert srt3.isSorted(cmp) == true
assert srt4.isSorted(cmp) == true
doAssert srt1.isSorted(cmp) == true
doAssert srt2.isSorted(cmp) == false
doAssert srt3.isSorted(cmp) == true
doAssert srt4.isSorted(cmp) == true
var srtseq = newSeq[int]()
assert srtseq.isSorted(cmp) == true
doAssert srtseq.isSorted(cmp) == true
# Tests for reversed
var arr1 = @[0, 1, 2, 3, 4]
assert arr1.reversed() == @[4, 3, 2, 1, 0]
doAssert arr1.reversed() == @[4, 3, 2, 1, 0]
for i in 0 .. high(arr1):
assert arr1.reversed(0, i) == arr1.reversed()[high(arr1) - i .. high(arr1)]
assert arr1.reversed(i, high(arr1)) == arr1.reversed()[0 .. high(arr1) - i]
doAssert arr1.reversed(0, i) == arr1.reversed()[high(arr1) - i .. high(arr1)]
doAssert arr1.reversed(i, high(arr1)) == arr1.reversed()[0 .. high(arr1) - i]
block:
var list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

View File

@@ -16,48 +16,48 @@ template main =
doAssert r.contains"def"
r.excl "def"
assert r.missingOrExcl("foo") == false
assert "foo" notin toSeq(r.items)
doAssert r.missingOrExcl("foo") == false
doAssert "foo" notin toSeq(r.items)
assert r.missingOrExcl("foo") == true
doAssert r.missingOrExcl("foo") == true
assert toSeq(r.items) == @["abc", "definition", "prefix", "xyz"]
doAssert toSeq(r.items) == @["abc", "definition", "prefix", "xyz"]
assert toSeq(r.itemsWithPrefix("de")) == @["definition"]
doAssert toSeq(r.itemsWithPrefix("de")) == @["definition"]
var c = CritBitTree[int]()
c.inc("a")
assert c["a"] == 1
doAssert c["a"] == 1
c.inc("a", 4)
assert c["a"] == 5
doAssert c["a"] == 5
c.inc("a", -5)
assert c["a"] == 0
doAssert c["a"] == 0
c.inc("b", 2)
assert c["b"] == 2
doAssert c["b"] == 2
c.inc("c", 3)
assert c["c"] == 3
doAssert c["c"] == 3
c.inc("a", 1)
assert c["a"] == 1
doAssert c["a"] == 1
var cf = CritBitTree[float]()
cf.incl("a", 1.0)
assert cf["a"] == 1.0
doAssert cf["a"] == 1.0
cf.incl("b", 2.0)
assert cf["b"] == 2.0
doAssert cf["b"] == 2.0
cf.incl("c", 3.0)
assert cf["c"] == 3.0
doAssert cf["c"] == 3.0
assert cf.len == 3
doAssert cf.len == 3
cf.excl("c")
assert cf.len == 2
doAssert cf.len == 2
var cb: CritBitTree[string]
cb.incl("help", "help")

View File

@@ -11,7 +11,7 @@ block:
proc main =
var testDeque = initDeque[int]()
testDeque.addFirst(1)
assert testDeque.index(0) == 1
doAssert testDeque.index(0) == 1
main()
@@ -35,26 +35,26 @@ block:
deq.addFirst(123)
var first = deq.popFirst()
deq.addLast(56)
assert(deq.peekLast() == 56)
doAssert(deq.peekLast() == 56)
deq.addLast(6)
assert(deq.peekLast() == 6)
doAssert(deq.peekLast() == 6)
var second = deq.popFirst()
deq.addLast(789)
assert(deq.peekLast() == 789)
doAssert(deq.peekLast() == 789)
assert first == 123
assert second == 9
assert($deq == "[4, 56, 6, 789]")
assert deq == [4, 56, 6, 789].toDeque
doAssert first == 123
doAssert second == 9
doAssert($deq == "[4, 56, 6, 789]")
doAssert deq == [4, 56, 6, 789].toDeque
assert deq[0] == deq.peekFirst and deq.peekFirst == 4
#assert deq[^1] == deq.peekLast and deq.peekLast == 789
doAssert deq[0] == deq.peekFirst and deq.peekFirst == 4
#doAssert deq[^1] == deq.peekLast and deq.peekLast == 789
deq[0] = 42
deq[deq.len - 1] = 7
assert 6 in deq and 789 notin deq
assert deq.find(6) >= 0
assert deq.find(789) < 0
doAssert 6 in deq and 789 notin deq
doAssert deq.find(6) >= 0
doAssert deq.find(789) < 0
block:
var d = initDeque[int](1)
@@ -74,21 +74,21 @@ block:
for i in -2 .. 10:
if i in deq:
assert deq.contains(i) and deq.find(i) >= 0
doAssert deq.contains(i) and deq.find(i) >= 0
else:
assert(not deq.contains(i) and deq.find(i) < 0)
doAssert(not deq.contains(i) and deq.find(i) < 0)
when compileOption("boundChecks"):
try:
echo deq[99]
assert false
doAssert false
except IndexDefect:
discard
try:
assert deq.len == 4
doAssert deq.len == 4
for i in 0 ..< 5: deq.popFirst()
assert false
doAssert false
except IndexDefect:
discard
@@ -98,24 +98,24 @@ block:
deq.popFirst()
deq.popLast()
for i in 5 .. 8: deq.addFirst i
assert $deq == "[8, 7, 6, 5, 2, 3]"
doAssert $deq == "[8, 7, 6, 5, 2, 3]"
# Similar to proc from the documentation example
proc foo(a, b: Positive) = # assume random positive values for `a` and `b`.
var deq = initDeque[int]()
assert deq.len == 0
doAssert deq.len == 0
for i in 1 .. a: deq.addLast i
if b < deq.len: # checking before indexed access.
assert deq[b] == b + 1
doAssert deq[b] == b + 1
# The following two lines don't need any checking on access due to the logic
# of the program, but that would not be the case if `a` could be 0.
assert deq.peekFirst == 1
assert deq.peekLast == a
doAssert deq.peekFirst == 1
doAssert deq.peekLast == a
while deq.len > 0: # checking if the deque is empty
assert deq.popFirst() > 0
doAssert deq.popFirst() > 0
#foo(0,0)
foo(8, 5)
@@ -133,7 +133,7 @@ block t13310:
q.addFirst([1'i16].toHashSet)
q.addFirst([2'i16].toHashSet)
q.addFirst([3'i16].toHashSet)
assert $q == "[{3}, {2}, {1}]"
doAssert $q == "[{3}, {2}, {1}]"
static:
main()

View File

@@ -30,11 +30,11 @@ doAssert editDistanceAscii("kitten", "sitting") == 3 # from Wikipedia
doAssert editDistanceAscii("flaw", "lawn") == 2 # from Wikipedia
assert(editDistance("prefix__hallo_suffix", "prefix__hallo_suffix") == 0)
assert(editDistance("prefix__hallo_suffix", "prefix__hallo_suffi1") == 1)
assert(editDistance("prefix__hallo_suffix", "prefix__HALLO_suffix") == 5)
assert(editDistance("prefix__hallo_suffix", "prefix__ha_suffix") == 3)
assert(editDistance("prefix__hallo_suffix", "prefix") == 14)
assert(editDistance("prefix__hallo_suffix", "suffix") == 14)
assert(editDistance("prefix__hallo_suffix", "prefix__hao_suffix") == 2)
assert(editDistance("main", "malign") == 2)
doAssert(editDistance("prefix__hallo_suffix", "prefix__hallo_suffix") == 0)
doAssert(editDistance("prefix__hallo_suffix", "prefix__hallo_suffi1") == 1)
doAssert(editDistance("prefix__hallo_suffix", "prefix__HALLO_suffix") == 5)
doAssert(editDistance("prefix__hallo_suffix", "prefix__ha_suffix") == 3)
doAssert(editDistance("prefix__hallo_suffix", "prefix") == 14)
doAssert(editDistance("prefix__hallo_suffix", "suffix") == 14)
doAssert(editDistance("prefix__hallo_suffix", "prefix__hao_suffix") == 2)
doAssert(editDistance("main", "malign") == 2)

View File

@@ -6,14 +6,14 @@ block:
var res: seq[(int, int)]
for i, x in enumerate(a):
res.add (i, x)
assert res == @[(0, 1), (1, 3), (2, 5), (3, 7)]
doAssert res == @[(0, 1), (1, 3), (2, 5), (3, 7)]
block:
var res: seq[(int, int)]
for (i, x) in enumerate(a.items):
res.add (i, x)
assert res == @[(0, 1), (1, 3), (2, 5), (3, 7)]
doAssert res == @[(0, 1), (1, 3), (2, 5), (3, 7)]
block:
var res: seq[(int, int)]
for i, x in enumerate(3, a):
res.add (i, x)
assert res == @[(3, 1), (4, 3), (5, 5), (6, 7)]
doAssert res == @[(3, 1), (4, 3), (5, 5), (6, 7)]

View File

@@ -47,7 +47,7 @@ block t2813:
for n in tree.findAll("table"):
n.findAll("tr", rows) # len = 2
break
assert tree.findAll("tr").len == rows.len
doAssert tree.findAll("tr").len == rows.len
block t2814:

View File

@@ -2,31 +2,31 @@ import httpcore, strutils
block:
block HttpCode:
assert $Http418 == "418 I'm a teapot"
assert Http418.is4xx() == true
assert Http418.is2xx() == false
doAssert $Http418 == "418 I'm a teapot"
doAssert Http418.is4xx() == true
doAssert Http418.is2xx() == false
block headers:
var h = newHttpHeaders()
assert h.len == 0
doAssert h.len == 0
h.add("Cookie", "foo")
assert h.len == 1
assert h.hasKey("cooKIE")
assert h["Cookie"] == "foo"
assert h["cookie"] == "foo"
doAssert h.len == 1
doAssert h.hasKey("cooKIE")
doAssert h["Cookie"] == "foo"
doAssert h["cookie"] == "foo"
h["cookie"] = @["bar", "x"]
assert h["Cookie"] == "bar"
assert h["Cookie", 1] == "x"
assert h["Cookie"].contains("BaR") == true
assert h["Cookie"].contains("X") == true
assert "baR" in h["cookiE"]
doAssert h["Cookie"] == "bar"
doAssert h["Cookie", 1] == "x"
doAssert h["Cookie"].contains("BaR") == true
doAssert h["Cookie"].contains("X") == true
doAssert "baR" in h["cookiE"]
h.del("coOKie")
assert h.len == 0
doAssert h.len == 0
# Test that header constructor works with repeated values
let h1 = newHttpHeaders({"a": "1", "a": "2", "A": "3"})
assert seq[string](h1["a"]).join(",") == "1,2,3"
doAssert seq[string](h1["a"]).join(",") == "1,2,3"
block test_cookies_with_comma:
doAssert parseHeader("cookie: foo, bar") == ("cookie", @["foo, bar"])

View File

@@ -11,16 +11,16 @@ block SinglyLinkedListTest1:
var L: SinglyLinkedList[int]
for d in items(data): L.prepend(d)
for d in items(data): L.append(d)
assert($L == "[6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6]")
doAssert($L == "[6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6]")
assert(4 in L)
doAssert(4 in L)
block SinglyLinkedListTest2:
var L: SinglyLinkedList[string]
for d in items(data): L.prepend($d)
assert($L == """["6", "5", "4", "3", "2", "1"]""")
doAssert($L == """["6", "5", "4", "3", "2", "1"]""")
assert("4" in L)
doAssert("4" in L)
block DoublyLinkedListTest1:
@@ -28,39 +28,39 @@ block DoublyLinkedListTest1:
for d in items(data): L.prepend(d)
for d in items(data): L.append(d)
L.remove(L.find(1))
assert($L == "[6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6]")
doAssert($L == "[6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6]")
assert(4 in L)
doAssert(4 in L)
block SinglyLinkedRingTest1:
var L: SinglyLinkedRing[int]
L.prepend(4)
assert($L == "[4]")
doAssert($L == "[4]")
L.prepend(4)
assert($L == "[4, 4]")
assert(4 in L)
doAssert($L == "[4, 4]")
doAssert(4 in L)
block DoublyLinkedRingTest1:
var L: DoublyLinkedRing[int]
L.prepend(4)
assert($L == "[4]")
doAssert($L == "[4]")
L.prepend(4)
assert($L == "[4, 4]")
assert(4 in L)
doAssert($L == "[4, 4]")
doAssert(4 in L)
L.append(3)
L.append(5)
assert($L == "[4, 4, 3, 5]")
doAssert($L == "[4, 4, 3, 5]")
L.remove(L.find(3))
L.remove(L.find(5))
L.remove(L.find(4))
L.remove(L.find(4))
assert($L == "[]")
assert(4 notin L)
doAssert($L == "[]")
doAssert(4 notin L)
block tlistsToString:
block:
@@ -105,8 +105,8 @@ template testCommon(initList, toList) =
doAssert a.toSeq == [f0]
doAssert b.toSeq == [f0, f1]
f0.x = 42
assert a.head.value.x == 42
assert b.head.value.x == 42
doAssert a.head.value.x == 42
doAssert b.head.value.x == 42
block: # add, addMoved
block:

View File

@@ -152,10 +152,10 @@ block:
return sqrt(num)
# check gamma function
assert(gamma(5.0) == 24.0) # 4!
assert(lgamma(1.0) == 0.0) # ln(1.0) == 0.0
assert(erf(6.0) > erf(5.0))
assert(erfc(6.0) < erfc(5.0))
doAssert(gamma(5.0) == 24.0) # 4!
doAssert(lgamma(1.0) == 0.0) # ln(1.0) == 0.0
doAssert(erf(6.0) > erf(5.0))
doAssert(erfc(6.0) < erfc(5.0))
# Function for approximate comparison of floats
@@ -215,21 +215,21 @@ block:
doAssert(classify(trunc(0.0'f32)) == fcZero)
block: # sgn() tests
assert sgn(1'i8) == 1
assert sgn(1'i16) == 1
assert sgn(1'i32) == 1
assert sgn(1'i64) == 1
assert sgn(1'u8) == 1
assert sgn(1'u16) == 1
assert sgn(1'u32) == 1
assert sgn(1'u64) == 1
assert sgn(-12342.8844'f32) == -1
assert sgn(123.9834'f64) == 1
assert sgn(0'i32) == 0
assert sgn(0'f32) == 0
assert sgn(NegInf) == -1
assert sgn(Inf) == 1
assert sgn(NaN) == 0
doAssert sgn(1'i8) == 1
doAssert sgn(1'i16) == 1
doAssert sgn(1'i32) == 1
doAssert sgn(1'i64) == 1
doAssert sgn(1'u8) == 1
doAssert sgn(1'u16) == 1
doAssert sgn(1'u32) == 1
doAssert sgn(1'u64) == 1
doAssert sgn(-12342.8844'f32) == -1
doAssert sgn(123.9834'f64) == 1
doAssert sgn(0'i32) == 0
doAssert sgn(0'f32) == 0
doAssert sgn(NegInf) == -1
doAssert sgn(Inf) == 1
doAssert sgn(NaN) == 0
block: # fac() tests
try:

View File

@@ -1,7 +1,7 @@
import md5
assert(getMD5("Franz jagt im komplett verwahrlosten Taxi quer durch Bayern") ==
doAssert(getMD5("Franz jagt im komplett verwahrlosten Taxi quer durch Bayern") ==
"a3cca2b2aa1e3b5b3b5aad99a8529074")
assert(getMD5("Frank jagt im komplett verwahrlosten Taxi quer durch Bayern") ==
doAssert(getMD5("Frank jagt im komplett verwahrlosten Taxi quer durch Bayern") ==
"7e716d0e702df0505fc72e2b89467910")
assert($toMD5("") == "d41d8cd98f00b204e9800998ecf8427e")
doAssert($toMD5("") == "d41d8cd98f00b204e9800998ecf8427e")

View File

@@ -468,19 +468,19 @@ block isRelativeTo:
doAssert not isRelativeTo("/foo2", "/foo")
block: # quoteShellWindows
assert quoteShellWindows("aaa") == "aaa"
assert quoteShellWindows("aaa\"") == "aaa\\\""
assert quoteShellWindows("") == "\"\""
doAssert quoteShellWindows("aaa") == "aaa"
doAssert quoteShellWindows("aaa\"") == "aaa\\\""
doAssert quoteShellWindows("") == "\"\""
block: # quoteShellWindows
assert quoteShellPosix("aaa") == "aaa"
assert quoteShellPosix("aaa a") == "'aaa a'"
assert quoteShellPosix("") == "''"
assert quoteShellPosix("a'a") == "'a'\"'\"'a'"
doAssert quoteShellPosix("aaa") == "aaa"
doAssert quoteShellPosix("aaa a") == "'aaa a'"
doAssert quoteShellPosix("") == "''"
doAssert quoteShellPosix("a'a") == "'a'\"'\"'a'"
block: # quoteShell
when defined(posix):
assert quoteShell("") == "''"
doAssert quoteShell("") == "''"
block: # normalizePathEnd
# handle edge cases correctly: shouldn't affect whether path is

View File

@@ -24,8 +24,8 @@ when not defined(js):
var config2 = loadConfig(file)
let bar = config2.getSectionValue("foo", "bar")
let foo = config2.getSectionValue("foo", "foo")
assert(bar == "-1")
assert(foo == "abc")
doAssert(bar == "-1")
doAssert(foo == "abc")
## Creating a configuration file.
var dict1 = newConfig()

View File

@@ -27,7 +27,7 @@ for kind, key, val in getopt():
of "version", "v": writeVersion()
else:
writeLine(stdout, "Unknown command line option: ", key, ": ", val)
of cmdEnd: assert(false) # cannot happen
of cmdEnd: doAssert(false) # cannot happen
if filename == "":
# no filename has been given, so we show the help:
writeHelp()

View File

@@ -146,4 +146,4 @@ block:
echo "Event parser output"
echo "-------------------"
let pLen = parseArithExpr(txt)
assert txt.len == pLen
doAssert txt.len == pLen

View File

@@ -1,5 +1,5 @@
import punycode
assert(decode(encode("", "bücher")) == "bücher")
assert(decode(encode("münchen")) == "münchen")
assert encode("xn--", "münchen") == "xn--mnchen-3ya"
doAssert(decode(encode("", "bücher")) == "bücher")
doAssert(decode(encode("münchen")) == "münchen")
doAssert encode("xn--", "münchen") == "xn--mnchen-3ya"

View File

@@ -9,90 +9,90 @@ var
m1 = -1 // 1
tt = 10 // 2
assert(a == a)
assert( (a-a) == z)
assert( (a+b) == o)
assert( (a/b) == o)
assert( (a*b) == 1 // 4)
assert( (3/a) == 6 // 1)
assert( (a/3) == 1 // 6)
assert(a*b == 1 // 4)
assert(tt*z == z)
assert(10*a == tt)
assert(a*10 == tt)
assert(tt/10 == a)
assert(a-m1 == 3 // 2)
assert(a+m1 == -1 // 2)
assert(m1+tt == 16 // 4)
assert(m1-tt == 6 // -1)
doAssert(a == a)
doAssert( (a-a) == z)
doAssert( (a+b) == o)
doAssert( (a/b) == o)
doAssert( (a*b) == 1 // 4)
doAssert( (3/a) == 6 // 1)
doAssert( (a/3) == 1 // 6)
doAssert(a*b == 1 // 4)
doAssert(tt*z == z)
doAssert(10*a == tt)
doAssert(a*10 == tt)
doAssert(tt/10 == a)
doAssert(a-m1 == 3 // 2)
doAssert(a+m1 == -1 // 2)
doAssert(m1+tt == 16 // 4)
doAssert(m1-tt == 6 // -1)
assert(z < o)
assert(z <= o)
assert(z == z)
assert(cmp(z, o) < 0)
assert(cmp(o, z) > 0)
doAssert(z < o)
doAssert(z <= o)
doAssert(z == z)
doAssert(cmp(z, o) < 0)
doAssert(cmp(o, z) > 0)
assert(o == o)
assert(o >= o)
assert(not(o > o))
assert(cmp(o, o) == 0)
assert(cmp(z, z) == 0)
assert(hash(o) == hash(o))
doAssert(o == o)
doAssert(o >= o)
doAssert(not(o > o))
doAssert(cmp(o, o) == 0)
doAssert(cmp(z, z) == 0)
doAssert(hash(o) == hash(o))
assert(a == b)
assert(a >= b)
assert(not(b > a))
assert(cmp(a, b) == 0)
assert(hash(a) == hash(b))
doAssert(a == b)
doAssert(a >= b)
doAssert(not(b > a))
doAssert(cmp(a, b) == 0)
doAssert(hash(a) == hash(b))
var x = 1//3
x *= 5//1
assert(x == 5//3)
doAssert(x == 5//3)
x += 2 // 9
assert(x == 17//9)
doAssert(x == 17//9)
x -= 9//18
assert(x == 25//18)
doAssert(x == 25//18)
x /= 1//2
assert(x == 50//18)
doAssert(x == 50//18)
var y = 1//3
y *= 4
assert(y == 4//3)
doAssert(y == 4//3)
y += 5
assert(y == 19//3)
doAssert(y == 19//3)
y -= 2
assert(y == 13//3)
doAssert(y == 13//3)
y /= 9
assert(y == 13//27)
doAssert(y == 13//27)
assert toRational(5) == 5//1
assert abs(toFloat(y) - 0.4814814814814815) < 1.0e-7
assert toInt(z) == 0
doAssert toRational(5) == 5//1
doAssert abs(toFloat(y) - 0.4814814814814815) < 1.0e-7
doAssert toInt(z) == 0
when sizeof(int) == 8:
assert toRational(0.98765432) == 2111111029 // 2137499919
assert toRational(PI) == 817696623 // 260280919
doAssert toRational(0.98765432) == 2111111029 // 2137499919
doAssert toRational(PI) == 817696623 // 260280919
when sizeof(int) == 4:
assert toRational(0.98765432) == 80 // 81
assert toRational(PI) == 355 // 113
doAssert toRational(0.98765432) == 80 // 81
doAssert toRational(PI) == 355 // 113
assert toRational(0.1) == 1 // 10
assert toRational(0.9) == 9 // 10
doAssert toRational(0.1) == 1 // 10
doAssert toRational(0.9) == 9 // 10
assert toRational(0.0) == 0 // 1
assert toRational(-0.25) == 1 // -4
assert toRational(3.2) == 16 // 5
assert toRational(0.33) == 33 // 100
assert toRational(0.22) == 11 // 50
assert toRational(10.0) == 10 // 1
doAssert toRational(0.0) == 0 // 1
doAssert toRational(-0.25) == 1 // -4
doAssert toRational(3.2) == 16 // 5
doAssert toRational(0.33) == 33 // 100
doAssert toRational(0.22) == 11 // 50
doAssert toRational(10.0) == 10 // 1
assert (1//1) div (3//10) == 3
assert (-1//1) div (3//10) == -3
assert (3//10) mod (1//1) == 3//10
assert (-3//10) mod (1//1) == -3//10
assert floorDiv(1//1, 3//10) == 3
assert floorDiv(-1//1, 3//10) == -4
assert floorMod(3//10, 1//1) == 3//10
assert floorMod(-3//10, 1//1) == 7//10
doAssert (1//1) div (3//10) == 3
doAssert (-1//1) div (3//10) == -3
doAssert (3//10) mod (1//1) == 3//10
doAssert (-3//10) mod (1//1) == -3//10
doAssert floorDiv(1//1, 3//10) == 3
doAssert floorDiv(-1//1, 3//10) == -4
doAssert floorMod(3//10, 1//1) == 3//10
doAssert floorMod(-3//10, 1//1) == 7//10

View File

@@ -16,7 +16,7 @@ suite "RST include directive":
test "Include whole":
"other.rst".writeFile("**test1**")
let input = ".. include:: other.rst"
assert "<strong>test1</strong>" == rstTohtml(input, {}, defaultConfig())
doAssert "<strong>test1</strong>" == rstTohtml(input, {}, defaultConfig())
removeFile("other.rst")
test "Include starting from":
@@ -30,7 +30,7 @@ OtherStart
.. include:: other.rst
:start-after: OtherStart
"""
assert "<em>Visible</em>" == rstTohtml(input, {}, defaultConfig())
doAssert "<em>Visible</em>" == rstTohtml(input, {}, defaultConfig())
removeFile("other.rst")
test "Include everything before":
@@ -44,7 +44,7 @@ And this should **NOT** be visible in `docs.html`
.. include:: other.rst
:end-before: OtherEnd
"""
assert "<em>Visible</em>" == rstTohtml(input, {}, defaultConfig())
doAssert "<em>Visible</em>" == rstTohtml(input, {}, defaultConfig())
removeFile("other.rst")
@@ -62,7 +62,7 @@ And this should **NOT** be visible in `docs.html`
:start-after: OtherStart
:end-before: OtherEnd
"""
assert "<em>Visible</em>" == rstTohtml(input, {}, defaultConfig())
doAssert "<em>Visible</em>" == rstTohtml(input, {}, defaultConfig())
removeFile("other.rst")
@@ -82,5 +82,5 @@ And this should **NOT** be visible in `docs.html`
:start-after: OtherStart
:end-before: OtherEnd
"""
assert "<em>Visible</em>" == rstTohtml(input, {}, defaultConfig())
doAssert "<em>Visible</em>" == rstTohtml(input, {}, defaultConfig())
removeFile("other.rst")

View File

@@ -22,7 +22,7 @@ suite "YAML syntax highlighting":
: value
..."""
let output = rstTohtml(input, {}, defaultConfig())
assert output == """<pre class = "listing"><span class="Directive">%YAML 1.2</span>
doAssert output == """<pre class = "listing"><span class="Directive">%YAML 1.2</span>
<span class="Keyword">---</span>
<span class="StringLit">a string</span><span class="Punctuation">:</span> <span class="StringLit">string</span>
<span class="StringLit">a list</span><span class="Punctuation">:</span>
@@ -48,7 +48,7 @@ suite "YAML syntax highlighting":
|+ # comment after header
allowed, since more indented than parent"""
let output = rstToHtml(input, {}, defaultConfig())
assert output == """<pre class = "listing"><span class="StringLit">a literal block scalar</span><span class="Punctuation">:</span> <span class="Command">|</span><span class="Command"></span><span class="LongStringLit">
doAssert output == """<pre class = "listing"><span class="StringLit">a literal block scalar</span><span class="Punctuation">:</span> <span class="Command">|</span><span class="Command"></span><span class="LongStringLit">
some text
# not a comment
</span><span class="Comment"># a comment, since less indented</span>
@@ -74,7 +74,7 @@ suite "YAML syntax highlighting":
...
%TAG ! !foo:"""
let output = rstToHtml(input, {}, defaultConfig())
assert output == """<pre class = "listing"><span class="Directive">%YAML 1.2</span>
doAssert output == """<pre class = "listing"><span class="Directive">%YAML 1.2</span>
<span class="Keyword">---</span>
<span class="StringLit">%not a directive</span>
<span class="Keyword">...</span>
@@ -95,7 +95,7 @@ suite "YAML syntax highlighting":
not numbers: [ 42e, 0023, +32.37, 8 ball]
}"""
let output = rstToHtml(input, {}, defaultConfig())
assert output == """<pre class = "listing"><span class="Punctuation">{</span>
doAssert output == """<pre class = "listing"><span class="Punctuation">{</span>
<span class="StringLit">&quot;</span><span class="StringLit">quoted string&quot;</span><span class="Punctuation">:</span> <span class="DecNumber">42</span><span class="Punctuation">,</span>
<span class="StringLit">'single quoted string'</span><span class="Punctuation">:</span> <span class="StringLit">false</span><span class="Punctuation">,</span>
<span class="Punctuation">[</span> <span class="StringLit">list</span><span class="Punctuation">,</span> <span class="StringLit">&quot;</span><span class="StringLit">with&quot;</span><span class="Punctuation">,</span> <span class="StringLit">'entries'</span> <span class="Punctuation">]</span><span class="Punctuation">:</span> <span class="FloatNumber">73.32e-73</span><span class="Punctuation">,</span>
@@ -112,7 +112,7 @@ suite "YAML syntax highlighting":
alias: *anchor
"""
let output = rstToHtml(input, {}, defaultConfig())
assert output == """<pre class = "listing"><span class="Keyword">---</span> <span class="TagStart">!!map</span>
doAssert output == """<pre class = "listing"><span class="Keyword">---</span> <span class="TagStart">!!map</span>
<span class="TagStart">!!str</span> <span class="StringLit">string</span><span class="Punctuation">:</span> <span class="TagStart">!&lt;tag:yaml.org,2002:int&gt;</span> <span class="DecNumber">42</span>
<span class="Punctuation">?</span> <span class="Label">&amp;anchor</span> <span class="TagStart">!!seq</span> <span class="Punctuation">[</span><span class="Punctuation">]</span><span class="Punctuation">:</span>
<span class="Punctuation">:</span> <span class="TagStart">!localtag</span> <span class="StringLit">foo</span>
@@ -132,7 +132,7 @@ suite "YAML syntax highlighting":
?not a map key
"""
let output = rstToHtml(input, {}, defaultConfig())
assert output == """<pre class = "listing"><span class="Keyword">...</span>
doAssert output == """<pre class = "listing"><span class="Keyword">...</span>
<span class="StringLit">%a string</span><span class="Punctuation">:</span>
<span class="StringLit">a:string:not:a:map</span>
<span class="Keyword">...</span>
@@ -146,7 +146,7 @@ suite "YAML syntax highlighting":
suite "RST/Markdown general":
test "RST emphasis":
assert rstToHtml("*Hello* **world**!", {},
doAssert rstToHtml("*Hello* **world**!", {},
newStringTable(modeStyleInsensitive)) ==
"<em>Hello</em> <strong>world</strong>!"
@@ -156,9 +156,9 @@ suite "RST/Markdown general":
b = rstToHtml("(([Nim](https://nim-lang.org/)))", {roSupportMarkdown}, defaultConfig())
c = rstToHtml("[[Nim](https://nim-lang.org/)]", {roSupportMarkdown}, defaultConfig())
assert a == """(( <a class="reference external" href="https://nim-lang.org/">Nim</a> ))"""
assert b == """((<a class="reference external" href="https://nim-lang.org/">Nim</a>))"""
assert c == """[<a class="reference external" href="https://nim-lang.org/">Nim</a>]"""
doAssert a == """(( <a class="reference external" href="https://nim-lang.org/">Nim</a> ))"""
doAssert b == """((<a class="reference external" href="https://nim-lang.org/">Nim</a>))"""
doAssert c == """[<a class="reference external" href="https://nim-lang.org/">Nim</a>]"""
test "Markdown tables":
let input1 = """
@@ -170,7 +170,7 @@ suite "RST/Markdown general":
| | F2 without pipe
not in table"""
let output1 = rstToHtml(input1, {roSupportMarkdown}, defaultConfig())
assert output1 == """<table border="1" class="docutils"><tr><th>A1 header</th><th>A2 | not fooled</th></tr>
doAssert output1 == """<table border="1" class="docutils"><tr><th>A1 header</th><th>A2 | not fooled</th></tr>
<tr><td>C1</td><td>C2 <strong>bold</strong></td></tr>
<tr><td>D1 <tt class="docutils literal"><span class="pre">code |</span></tt></td><td>D2</td></tr>
<tr><td>E1 | text</td><td></td></tr>
@@ -181,7 +181,7 @@ not in table"""
| A1 header | A2 |
| --- | --- |"""
let output2 = rstToHtml(input2, {roSupportMarkdown}, defaultConfig())
assert output2 == """<table border="1" class="docutils"><tr><th>A1 header</th><th>A2</th></tr>
doAssert output2 == """<table border="1" class="docutils"><tr><th>A1 header</th><th>A2</th></tr>
</table>"""
test "RST tables":
@@ -197,10 +197,10 @@ A2 A3
A4 A5
==== === """
let output1 = rstToLatex(input1, {})
assert "{|X|X|}" in output1 # 2 columns
assert count(output1, "\\\\") == 4 # 4 rows
doAssert "{|X|X|}" in output1 # 2 columns
doAssert count(output1, "\\\\") == 4 # 4 rows
for cell in ["H0", "H1", "A0", "A1", "A2", "A3", "A4", "A5"]:
assert cell in output1
doAssert cell in output1
let input2 = """
Now test 3 columns / 2 rows, and also borders containing 4 =, 3 =, 1 = signs:
@@ -212,10 +212,10 @@ A0 A1 X
Ax Y
==== === = """
let output2 = rstToLatex(input2, {})
assert "{|X|X|X|}" in output2 # 3 columns
assert count(output2, "\\\\") == 2 # 2 rows
doAssert "{|X|X|X|}" in output2 # 3 columns
doAssert count(output2, "\\\\") == 2 # 2 rows
for cell in ["H0", "H1", "H", "A0", "A1", "X", "Ax", "Y"]:
assert cell in output2
doAssert cell in output2
test "RST adornments":
@@ -231,7 +231,7 @@ Long chapter name
'''''''''''''''''''
"""
let output1 = rstToHtml(input1, {roSupportMarkdown}, defaultConfig())
assert "Long chapter name" in output1 and "<h1" in output1
doAssert "Long chapter name" in output1 and "<h1" in output1
let input2 = """
Short chapter name:
@@ -240,7 +240,7 @@ ChA
===
"""
let output2 = rstToHtml(input2, {roSupportMarkdown}, defaultConfig())
assert "ChA" in output2 and "<h1" in output2
doAssert "ChA" in output2 and "<h1" in output2
let input3 = """
Very short chapter name:
@@ -249,7 +249,7 @@ X
~
"""
let output3 = rstToHtml(input3, {roSupportMarkdown}, defaultConfig())
assert "X" in output3 and "<h1" in output3
doAssert "X" in output3 and "<h1" in output3
let input4 = """
Check that short underline is not enough to make section:
@@ -259,7 +259,7 @@ Wrong chapter
"""
let output4 = rstToHtml(input4, {roSupportMarkdown}, defaultConfig())
assert "Wrong chapter" in output4 and "<h1" notin output4
doAssert "Wrong chapter" in output4 and "<h1" notin output4
let input5 = """
Check that punctuation after adornment and indent are not detected as adornment.
@@ -269,7 +269,7 @@ Some chapter
"punctuation symbols" """
let output5 = rstToHtml(input5, {roSupportMarkdown}, defaultConfig())
assert "&quot;punctuation symbols&quot;" in output5 and "<h1" in output5
doAssert "&quot;punctuation symbols&quot;" in output5 and "<h1" in output5
test "RST links":
@@ -278,7 +278,7 @@ Want to learn about `my favorite programming language`_?
.. _my favorite programming language: https://nim-lang.org"""
let output1 = rstToHtml(input1, {roSupportMarkdown}, defaultConfig())
assert "<a" in output1 and "href=\"https://nim-lang.org\"" in output1
doAssert "<a" in output1 and "href=\"https://nim-lang.org\"" in output1
test "RST transitions":
let input1 = """
@@ -289,7 +289,7 @@ context1
context2
"""
let output1 = rstToHtml(input1, {roSupportMarkdown}, defaultConfig())
assert "<hr" in output1
doAssert "<hr" in output1
let input2 = """
This is too short to be a transition:
@@ -299,7 +299,7 @@ This is too short to be a transition:
context2
"""
let output2 = rstToHtml(input2, {roSupportMarkdown}, defaultConfig())
assert "<hr" notin output2
doAssert "<hr" notin output2
test "RST literal block":
let input1 = """
@@ -309,7 +309,7 @@ Test literal block
check """
let output1 = rstToHtml(input1, {roSupportMarkdown}, defaultConfig())
assert "<pre>" in output1
doAssert "<pre>" in output1
test "Markdown code block":
let input1 = """
@@ -317,7 +317,7 @@ Test literal block
let x = 1
``` """
let output1 = rstToHtml(input1, {roSupportMarkdown}, defaultConfig())
assert "<pre" in output1 and "class=\"Keyword\"" notin output1
doAssert "<pre" in output1 and "class=\"Keyword\"" notin output1
let input2 = """
Parse the block with language specifier:
@@ -325,7 +325,7 @@ Parse the block with language specifier:
let x = 1
``` """
let output2 = rstToHtml(input2, {roSupportMarkdown}, defaultConfig())
assert "<pre" in output2 and "class=\"Keyword\"" in output2
doAssert "<pre" in output2 and "class=\"Keyword\"" in output2
test "RST comments":
let input1 = """
@@ -334,7 +334,7 @@ Check that comment disappears:
..
some comment """
let output1 = rstToHtml(input1, {roSupportMarkdown}, defaultConfig())
assert output1 == "Check that comment disappears:"
doAssert output1 == "Check that comment disappears:"
test "RST line blocks":
let input1 = """
@@ -353,13 +353,13 @@ Test1
var output1: string
rstGenera.initRstGenerator(outHtml, defaultConfig(), "input", {})
rstGenera.renderRstToOut(rstParse(input1, "", 1, 1, option, {}), output1)
assert rstGenera.meta[metaTitle] == "Test1"
doAssert rstGenera.meta[metaTitle] == "Test1"
# check that title was not overwritten to '|'
assert "line block<br />" in output1
assert "other line<br />" in output1
doAssert "line block<br />" in output1
doAssert "other line<br />" in output1
let output1l = rstToLatex(input1, {})
assert "line block\\\\" in output1l
assert "other line\\\\" in output1l
doAssert "line block\\\\" in output1l
doAssert "other line\\\\" in output1l
test "RST enumerated lists":
let input1 = dedent """
@@ -382,8 +382,8 @@ Test1
"""
let output1 = rstToHtml(input1, {roSupportMarkdown}, defaultConfig())
for i in 1..5:
assert ($i & ". line" & $i) notin output1
assert ("<li>line" & $i & " " & $i & "</li>") in output1
doAssert ($i & ". line" & $i) notin output1
doAssert ("<li>line" & $i & " " & $i & "</li>") in output1
let input2 = dedent """
3. line3
@@ -404,8 +404,8 @@ Test1
"""
let output2 = rstToHtml(input2, {roSupportMarkdown}, defaultConfig())
for i in [3, 4, 5, 7, 8]:
assert ($i & ". line" & $i) notin output2
assert ("<li>line" & $i & "</li>") in output2
doAssert ($i & ". line" & $i) notin output2
doAssert ("<li>line" & $i & "</li>") in output2
# check that nested enumerated lists work
let input3 = dedent """
@@ -413,9 +413,9 @@ Test1
2. string2
"""
let output3 = rstToHtml(input3, {roSupportMarkdown}, defaultConfig())
assert count(output3, "<ol ") == 2
assert count(output3, "</ol>") == 2
assert "<li>string1</li>" in output3 and "<li>string2</li>" in output3
doAssert count(output3, "<ol ") == 2
doAssert count(output3, "</ol>") == 2
doAssert "<li>string1</li>" in output3 and "<li>string2</li>" in output3
let input4 = dedent """
Check that enumeration specifiers are respected
@@ -429,12 +429,12 @@ Test1
e) string6
"""
let output4 = rstToHtml(input4, {roSupportMarkdown}, defaultConfig())
assert count(output4, "<ol ") == 4
assert count(output4, "</ol>") == 4
doAssert count(output4, "<ol ") == 4
doAssert count(output4, "</ol>") == 4
for enumerator in [9, 12]:
assert "start=\"$1\"" % [$enumerator] in output4
doAssert "start=\"$1\"" % [$enumerator] in output4
for enumerator in [2, 5]: # 2=b, 5=e
assert "start=\"$1\"" % [$enumerator] in output4
doAssert "start=\"$1\"" % [$enumerator] in output4
let input5 = dedent """
Check that auto-numbered enumeration lists work.
@@ -449,9 +449,9 @@ Test1
#) string6
"""
let output5 = rstToHtml(input5, {roSupportMarkdown}, defaultConfig())
assert count(output5, "<ol ") == 2
assert count(output5, "</ol>") == 2
assert count(output5, "<li>") == 5
doAssert count(output5, "<ol ") == 2
doAssert count(output5, "</ol>") == 2
doAssert count(output5, "<li>") == 5
let input5a = dedent """
Auto-numbered RST list can start with 1 even when Markdown support is on.
@@ -461,9 +461,9 @@ Test1
#. string3
"""
let output5a = rstToHtml(input5a, {roSupportMarkdown}, defaultConfig())
assert count(output5a, "<ol ") == 1
assert count(output5a, "</ol>") == 1
assert count(output5a, "<li>") == 3
doAssert count(output5a, "<ol ") == 1
doAssert count(output5a, "</ol>") == 1
doAssert count(output5a, "<li>") == 3
let input6 = dedent """
... And for alphabetic enumerators too!
@@ -473,10 +473,10 @@ Test1
#. string3
"""
let output6 = rstToHtml(input6, {roSupportMarkdown}, defaultConfig())
assert count(output6, "<ol ") == 1
assert count(output6, "</ol>") == 1
assert count(output6, "<li>") == 3
assert "start=\"2\"" in output6 and "class=\"loweralpha simple\"" in output6
doAssert count(output6, "<ol ") == 1
doAssert count(output6, "</ol>") == 1
doAssert count(output6, "<li>") == 3
doAssert "start=\"2\"" in output6 and "class=\"loweralpha simple\"" in output6
let input7 = dedent """
... And for uppercase alphabetic enumerators.
@@ -486,10 +486,10 @@ Test1
#. string3
"""
let output7 = rstToHtml(input7, {roSupportMarkdown}, defaultConfig())
assert count(output7, "<ol ") == 1
assert count(output7, "</ol>") == 1
assert count(output7, "<li>") == 3
assert "start=\"3\"" in output7 and "class=\"upperalpha simple\"" in output7
doAssert count(output7, "<ol ") == 1
doAssert count(output7, "</ol>") == 1
doAssert count(output7, "<li>") == 3
doAssert "start=\"3\"" in output7 and "class=\"upperalpha simple\"" in output7
test "Markdown enumerated lists":
let input1 = dedent """
@@ -505,10 +505,10 @@ Test1
"""
let output1 = rstToHtml(input1, {roSupportMarkdown}, defaultConfig())
for i in 1..5:
assert ($i & ". line" & $i) notin output1
assert ("<li>line" & $i & "</li>") in output1
assert count(output1, "<ol ") == 2
assert count(output1, "</ol>") == 2
doAssert ($i & ". line" & $i) notin output1
doAssert ("<li>line" & $i & "</li>") in output1
doAssert count(output1, "<ol ") == 2
doAssert count(output1, "</ol>") == 2
test "RST bullet lists":
let input1 = dedent """
@@ -531,9 +531,9 @@ Test1
"""
let output1 = rstToHtml(input1, {roSupportMarkdown}, defaultConfig())
for i in 1..5:
assert ("<li>line" & $i & " " & $i & "</li>") in output1
assert count(output1, "<ul ") == 1
assert count(output1, "</ul>") == 1
doAssert ("<li>line" & $i & " " & $i & "</li>") in output1
doAssert count(output1, "<ul ") == 1
doAssert count(output1, "</ul>") == 1
test "RST admonitions":
# check that all admonitions are implemented
@@ -552,7 +552,7 @@ Test1
let output0 = rstToHtml(input0, {roSupportMarkdown}, defaultConfig())
for a in ["admonition", "attention", "caution", "danger", "error", "hint",
"important", "note", "tip", "warning" ]:
assert "endOf " & a & "</div>" in output0
doAssert "endOf " & a & "</div>" in output0
# Test that admonition does not swallow up the next paragraph.
let input1 = dedent """
@@ -561,9 +561,9 @@ Test1
Test paragraph.
"""
let output1 = rstToHtml(input1, {roSupportMarkdown}, defaultConfig())
assert "endOfError</div>" in output1
assert "<p>Test paragraph. </p>" in output1
assert "class=\"admonition admonition-error\"" in output1
doAssert "endOfError</div>" in output1
doAssert "<p>Test paragraph. </p>" in output1
doAssert "class=\"admonition admonition-error\"" in output1
# Test that second line is parsed as continuation of the first line.
let input2 = dedent """
@@ -573,16 +573,16 @@ Test1
Test paragraph.
"""
let output2 = rstToHtml(input2, {roSupportMarkdown}, defaultConfig())
assert "endOfError Test2p.</div>" in output2
assert "<p>Test paragraph. </p>" in output2
assert "class=\"admonition admonition-error\"" in output2
doAssert "endOfError Test2p.</div>" in output2
doAssert "<p>Test paragraph. </p>" in output2
doAssert "class=\"admonition admonition-error\"" in output2
let input3 = dedent """
.. note:: endOfNote
"""
let output3 = rstToHtml(input3, {roSupportMarkdown}, defaultConfig())
assert "endOfNote</div>" in output3
assert "class=\"admonition admonition-info\"" in output3
doAssert "endOfNote</div>" in output3
doAssert "class=\"admonition admonition-info\"" in output3
suite "RST/Code highlight":
test "Basic Python code highlight":

View File

@@ -15,7 +15,7 @@ block: # concat test
s2 = @[4, 5]
s3 = @[6, 7]
total = concat(s1, s2, s3)
assert total == @[1, 2, 3, 4, 5, 6, 7]
doAssert total == @[1, 2, 3, 4, 5, 6, 7]
block: # count test
let
@@ -35,18 +35,18 @@ block: # count test
ar3 = count(a2, 'y')
ar4 = count(a2, 'x')
ar5 = count(a2, 'a')
assert r0 == 0
assert r1 == 1
assert r2 == 2
assert r3 == 0
assert r4 == 1
assert r5 == 2
assert ar0 == 0
assert ar1 == 1
assert ar2 == 2
assert ar3 == 0
assert ar4 == 1
assert ar5 == 2
doAssert r0 == 0
doAssert r1 == 1
doAssert r2 == 2
doAssert r3 == 0
doAssert r4 == 1
doAssert r5 == 2
doAssert ar0 == 0
doAssert ar1 == 1
doAssert ar2 == 2
doAssert ar3 == 0
doAssert ar4 == 1
doAssert ar5 == 2
block: # cycle tests
let
@@ -62,9 +62,9 @@ block: # cycle tests
doAssert c.cycle(0) == @[]
block: # repeat tests
assert repeat(10, 5) == @[10, 10, 10, 10, 10]
assert repeat(@[1, 2, 3], 2) == @[@[1, 2, 3], @[1, 2, 3]]
assert repeat([1, 2, 3], 2) == @[[1, 2, 3], [1, 2, 3]]
doAssert repeat(10, 5) == @[10, 10, 10, 10, 10]
doAssert repeat(@[1, 2, 3], 2) == @[@[1, 2, 3], @[1, 2, 3]]
doAssert repeat([1, 2, 3], 2) == @[[1, 2, 3], [1, 2, 3]]
block: # deduplicates test
let
@@ -80,14 +80,14 @@ block: # deduplicates test
unique6 = deduplicate(dup2, true)
unique7 = deduplicate(dup3.sorted, true)
unique8 = deduplicate(dup4, true)
assert unique1 == @[1, 3, 4, 2, 8]
assert unique2 == @["a", "c", "d"]
assert unique3 == @[1, 3, 4, 2, 8]
assert unique4 == @["a", "c", "d"]
assert unique5 == @[1, 2, 3, 4, 8]
assert unique6 == @["a", "c", "d"]
assert unique7 == @[1, 2, 3, 4, 8]
assert unique8 == @["a", "c", "d"]
doAssert unique1 == @[1, 3, 4, 2, 8]
doAssert unique2 == @["a", "c", "d"]
doAssert unique3 == @[1, 3, 4, 2, 8]
doAssert unique4 == @["a", "c", "d"]
doAssert unique5 == @[1, 2, 3, 4, 8]
doAssert unique6 == @["a", "c", "d"]
doAssert unique7 == @[1, 2, 3, 4, 8]
doAssert unique8 == @["a", "c", "d"]
block: # zip test
let
@@ -100,29 +100,29 @@ block: # zip test
zip1 = zip(short, long)
zip2 = zip(short, words)
zip3 = zip(ashort, along)
assert zip1 == @[(1, 6), (2, 5), (3, 4)]
assert zip2 == @[(1, "one"), (2, "two"), (3, "three")]
assert zip3 == @[(1, 6), (2, 5), (3, 4)]
assert zip1[2][1] == 4
assert zip2[2][1] == "three"
assert zip3[2][1] == 4
doAssert zip1 == @[(1, 6), (2, 5), (3, 4)]
doAssert zip2 == @[(1, "one"), (2, "two"), (3, "three")]
doAssert zip3 == @[(1, 6), (2, 5), (3, 4)]
doAssert zip1[2][1] == 4
doAssert zip2[2][1] == "three"
doAssert zip3[2][1] == 4
when (NimMajor, NimMinor) <= (1, 0):
let
# In Nim 1.0.x and older, zip returned a seq of tuple strictly
# with fields named "a" and "b".
zipAb = zip(ashort, awords)
assert zipAb == @[(a: 1, b: "one"), (2, "two"), (3, "three")]
assert zipAb[2].b == "three"
doAssert zipAb == @[(a: 1, b: "one"), (2, "two"), (3, "three")]
doAssert zipAb[2].b == "three"
else:
let
# As zip returns seq of anonymous tuples, they can be assigned
# to any variable that's a sequence of named tuples too.
zipXy: seq[tuple[x: int, y: string]] = zip(ashort, awords)
zipMn: seq[tuple[m: int, n: string]] = zip(ashort, words)
assert zipXy == @[(x: 1, y: "one"), (2, "two"), (3, "three")]
assert zipMn == @[(m: 1, n: "one"), (2, "two"), (3, "three")]
assert zipXy[2].y == "three"
assert zipMn[2].n == "three"
doAssert zipXy == @[(x: 1, y: "one"), (2, "two"), (3, "three")]
doAssert zipMn == @[(m: 1, n: "one"), (2, "two"), (3, "three")]
doAssert zipXy[2].y == "three"
doAssert zipMn[2].n == "three"
block: # distribute tests
let numbers = @[1, 2, 3, 4, 5, 6, 7]
@@ -156,13 +156,13 @@ block: # map test
anumbers = [1, 4, 5, 8, 9, 7, 4]
m1 = map(numbers, proc(x: int): int = 2*x)
m2 = map(anumbers, proc(x: int): int = 2*x)
assert m1 == @[2, 8, 10, 16, 18, 14, 8]
assert m2 == @[2, 8, 10, 16, 18, 14, 8]
doAssert m1 == @[2, 8, 10, 16, 18, 14, 8]
doAssert m2 == @[2, 8, 10, 16, 18, 14, 8]
block: # apply test
var a = @["1", "2", "3", "4"]
apply(a, proc(x: var string) = x &= "42")
assert a == @["142", "242", "342", "442"]
doAssert a == @["142", "242", "342", "442"]
block: # filter proc test
let
@@ -172,34 +172,34 @@ block: # filter proc test
f2 = filter(colors) do (x: string) -> bool: x.len > 5
f3 = filter(acolors, proc(x: string): bool = x.len < 6)
f4 = filter(acolors) do (x: string) -> bool: x.len > 5
assert f1 == @["red", "black"]
assert f2 == @["yellow"]
assert f3 == @["red", "black"]
assert f4 == @["yellow"]
doAssert f1 == @["red", "black"]
doAssert f2 == @["yellow"]
doAssert f3 == @["red", "black"]
doAssert f4 == @["yellow"]
block: # filter iterator test
let numbers = @[1, 4, 5, 8, 9, 7, 4]
let anumbers = [1, 4, 5, 8, 9, 7, 4]
assert toSeq(filter(numbers, proc (x: int): bool = x mod 2 == 0)) ==
doAssert toSeq(filter(numbers, proc (x: int): bool = x mod 2 == 0)) ==
@[4, 8, 4]
assert toSeq(filter(anumbers, proc (x: int): bool = x mod 2 == 0)) ==
doAssert toSeq(filter(anumbers, proc (x: int): bool = x mod 2 == 0)) ==
@[4, 8, 4]
block: # keepIf test
var floats = @[13.0, 12.5, 5.8, 2.0, 6.1, 9.9, 10.1]
keepIf(floats, proc(x: float): bool = x > 10)
assert floats == @[13.0, 12.5, 10.1]
doAssert floats == @[13.0, 12.5, 10.1]
block: # delete tests
let outcome = @[1, 1, 1, 1, 1, 1, 1, 1]
var dest = @[1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1]
dest.delete(3, 8)
assert outcome == dest, """\
doAssert outcome == dest, """\
Deleting range 3-9 from [1,1,1,2,2,2,2,2,2,1,1,1,1,1]
is [1,1,1,1,1,1,1,1]"""
var x = @[1, 2, 3]
x.delete(100, 100)
assert x == @[1, 2, 3]
doAssert x == @[1, 2, 3]
block: # insert tests
var dest = @[1, 1, 1, 1, 1, 1, 1, 1]
@@ -207,7 +207,7 @@ block: # insert tests
src = @[2, 2, 2, 2, 2, 2]
outcome = @[1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1]
dest.insert(src, 3)
assert dest == outcome, """\
doAssert dest == outcome, """\
Inserting [2,2,2,2,2,2] into [1,1,1,1,1,1,1,1]
at 3 is [1,1,1,2,2,2,2,2,2,1,1,1,1,1]"""
@@ -216,57 +216,57 @@ block: # filterIt test
temperatures = @[-272.15, -2.0, 24.5, 44.31, 99.9, -113.44]
acceptable = filterIt(temperatures, it < 50 and it > -10)
notAcceptable = filterIt(temperatures, it > 50 or it < -10)
assert acceptable == @[-2.0, 24.5, 44.31]
assert notAcceptable == @[-272.15, 99.9, -113.44]
doAssert acceptable == @[-2.0, 24.5, 44.31]
doAssert notAcceptable == @[-272.15, 99.9, -113.44]
block: # keepItIf test
var candidates = @["foo", "bar", "baz", "foobar"]
keepItIf(candidates, it.len == 3 and it[0] == 'b')
assert candidates == @["bar", "baz"]
doAssert candidates == @["bar", "baz"]
block: # all
let
numbers = @[1, 4, 5, 8, 9, 7, 4]
anumbers = [1, 4, 5, 8, 9, 7, 4]
len0seq: seq[int] = @[]
assert all(numbers, proc (x: int): bool = return x < 10) == true
assert all(numbers, proc (x: int): bool = return x < 9) == false
assert all(len0seq, proc (x: int): bool = return false) == true
assert all(anumbers, proc (x: int): bool = return x < 10) == true
assert all(anumbers, proc (x: int): bool = return x < 9) == false
doAssert all(numbers, proc (x: int): bool = return x < 10) == true
doAssert all(numbers, proc (x: int): bool = return x < 9) == false
doAssert all(len0seq, proc (x: int): bool = return false) == true
doAssert all(anumbers, proc (x: int): bool = return x < 10) == true
doAssert all(anumbers, proc (x: int): bool = return x < 9) == false
block: # allIt
let
numbers = @[1, 4, 5, 8, 9, 7, 4]
anumbers = [1, 4, 5, 8, 9, 7, 4]
len0seq: seq[int] = @[]
assert allIt(numbers, it < 10) == true
assert allIt(numbers, it < 9) == false
assert allIt(len0seq, false) == true
assert allIt(anumbers, it < 10) == true
assert allIt(anumbers, it < 9) == false
doAssert allIt(numbers, it < 10) == true
doAssert allIt(numbers, it < 9) == false
doAssert allIt(len0seq, false) == true
doAssert allIt(anumbers, it < 10) == true
doAssert allIt(anumbers, it < 9) == false
block: # any
let
numbers = @[1, 4, 5, 8, 9, 7, 4]
anumbers = [1, 4, 5, 8, 9, 7, 4]
len0seq: seq[int] = @[]
assert any(numbers, proc (x: int): bool = return x > 8) == true
assert any(numbers, proc (x: int): bool = return x > 9) == false
assert any(len0seq, proc (x: int): bool = return true) == false
assert any(anumbers, proc (x: int): bool = return x > 8) == true
assert any(anumbers, proc (x: int): bool = return x > 9) == false
doAssert any(numbers, proc (x: int): bool = return x > 8) == true
doAssert any(numbers, proc (x: int): bool = return x > 9) == false
doAssert any(len0seq, proc (x: int): bool = return true) == false
doAssert any(anumbers, proc (x: int): bool = return x > 8) == true
doAssert any(anumbers, proc (x: int): bool = return x > 9) == false
block: # anyIt
let
numbers = @[1, 4, 5, 8, 9, 7, 4]
anumbers = [1, 4, 5, 8, 9, 7, 4]
len0seq: seq[int] = @[]
assert anyIt(numbers, it > 8) == true
assert anyIt(numbers, it > 9) == false
assert anyIt(len0seq, true) == false
assert anyIt(anumbers, it > 8) == true
assert anyIt(anumbers, it > 9) == false
doAssert anyIt(numbers, it > 8) == true
doAssert anyIt(numbers, it > 9) == false
doAssert anyIt(len0seq, true) == false
doAssert anyIt(anumbers, it > 8) == true
doAssert anyIt(anumbers, it > 9) == false
block: # toSeq test
block:
@@ -275,7 +275,7 @@ block: # toSeq test
oddNumbers = toSeq(filter(numeric) do (x: int) -> bool:
if x mod 2 == 1:
result = true)
assert oddNumbers == @[1, 3, 5, 7, 9]
doAssert oddNumbers == @[1, 3, 5, 7, 9]
block:
doAssert [1, 2].toSeq == @[1, 2]
@@ -350,10 +350,10 @@ block: # foldl tests
multiplication = foldl(numbers, a * b)
words = @["nim", "is", "cool"]
concatenation = foldl(words, a & b)
assert addition == 25, "Addition is (((5)+9)+11)"
assert subtraction == -15, "Subtraction is (((5)-9)-11)"
assert multiplication == 495, "Multiplication is (((5)*9)*11)"
assert concatenation == "nimiscool"
doAssert addition == 25, "Addition is (((5)+9)+11)"
doAssert subtraction == -15, "Subtraction is (((5)-9)-11)"
doAssert multiplication == 495, "Multiplication is (((5)*9)*11)"
doAssert concatenation == "nimiscool"
block: # foldr tests
let
@@ -363,10 +363,10 @@ block: # foldr tests
multiplication = foldr(numbers, a * b)
words = @["nim", "is", "cool"]
concatenation = foldr(words, a & b)
assert addition == 25, "Addition is (5+(9+(11)))"
assert subtraction == 7, "Subtraction is (5-(9-(11)))"
assert multiplication == 495, "Multiplication is (5*(9*(11)))"
assert concatenation == "nimiscool"
doAssert addition == 25, "Addition is (5+(9+(11)))"
doAssert subtraction == 7, "Subtraction is (5-(9-(11)))"
doAssert multiplication == 495, "Multiplication is (5*(9*(11)))"
doAssert concatenation == "nimiscool"
doAssert toSeq(1..3).foldr(a + b) == 6 # issue #14404
block: # mapIt + applyIt test
@@ -376,8 +376,8 @@ block: # mapIt + applyIt test
strings = nums.identity.mapIt($(4 * it))
doAssert counter == 1
nums.applyIt(it * 3)
assert nums[0] + nums[3] == 15
assert strings[2] == "12"
doAssert nums[0] + nums[3] == 15
doAssert strings[2] == "12"
block: # newSeqWith tests
var seq2D = newSeqWith(4, newSeq[bool](2))

View File

@@ -11,9 +11,9 @@ block:
init(table)
table[1] = 10
assert table.mget(1) == 10
assert table.mgetOrPut(3, 7) == 7
assert table.mgetOrPut(3, 99) == 7
doAssert table.mget(1) == 10
doAssert table.mgetOrPut(3, 7) == 7
doAssert table.mgetOrPut(3, 99) == 7
deinitSharedTable(table)
import sequtils, algorithm

View File

@@ -202,10 +202,10 @@ doAssert fmt"{nat=:3X}" == "nat= 40"
proc my_proc =
const value = "value"
const a = &"{value}"
assert a == value
doAssert a == value
const b = &"{value=}"
assert b == "value=" & value
doAssert b == "value=" & value
my_proc()

View File

@@ -105,12 +105,12 @@ writeLine(stdout, "length of table ", $tab.len)
block:
var x = {"k": "v", "11": "22", "565": "67"}.newStringTable
assert x["k"] == "v"
assert x["11"] == "22"
assert x["565"] == "67"
doAssert x["k"] == "v"
doAssert x["11"] == "22"
doAssert x["565"] == "67"
x["11"] = "23"
assert x["11"] == "23"
doAssert x["11"] == "23"
x.clear(modeCaseInsensitive)
x["11"] = "22"
assert x["11"] == "22"
doAssert x["11"] == "22"

View File

@@ -58,9 +58,9 @@ template main() =
block: # splitLines
let fixture = "a\nb\rc\r\nd"
assert len(fixture.splitLines) == 4
assert splitLines(fixture) == @["a", "b", "c", "d"]
assert splitLines(fixture, keepEol=true) == @["a\n", "b\r", "c\r\n", "d"]
doAssert len(fixture.splitLines) == 4
doAssert splitLines(fixture) == @["a", "b", "c", "d"]
doAssert splitLines(fixture, keepEol=true) == @["a\n", "b\r", "c\r\n", "d"]
block: # rsplit
doAssert rsplit("foo bar", seps = Whitespace) == @["foo", "bar"]
@@ -83,207 +83,207 @@ template main() =
block: # removeSuffix
var s = "hello\n\r"
s.removeSuffix
assert s == "hello"
doAssert s == "hello"
s.removeSuffix
assert s == "hello"
doAssert s == "hello"
s = "hello\n\n"
s.removeSuffix
assert s == "hello"
doAssert s == "hello"
s = "hello\r"
s.removeSuffix
assert s == "hello"
doAssert s == "hello"
s = "hello \n there"
s.removeSuffix
assert s == "hello \n there"
doAssert s == "hello \n there"
s = "hello"
s.removeSuffix("llo")
assert s == "he"
doAssert s == "he"
s.removeSuffix('e')
assert s == "h"
doAssert s == "h"
s = "hellos"
s.removeSuffix({'s','z'})
assert s == "hello"
doAssert s == "hello"
s.removeSuffix({'l','o'})
assert s == "he"
doAssert s == "he"
s = "aeiou"
s.removeSuffix("")
assert s == "aeiou"
doAssert s == "aeiou"
s = ""
s.removeSuffix("")
assert s == ""
doAssert s == ""
s = " "
s.removeSuffix
assert s == " "
doAssert s == " "
s = " "
s.removeSuffix("")
assert s == " "
doAssert s == " "
s = " "
s.removeSuffix(" ")
assert s == " "
doAssert s == " "
s = " "
s.removeSuffix(' ')
assert s == ""
doAssert s == ""
# Contrary to Chomp in other languages
# empty string does not change behaviour
s = "hello\r\n\r\n"
s.removeSuffix("")
assert s == "hello\r\n\r\n"
doAssert s == "hello\r\n\r\n"
block: # removePrefix
var s = "\n\rhello"
s.removePrefix
assert s == "hello"
doAssert s == "hello"
s.removePrefix
assert s == "hello"
doAssert s == "hello"
s = "\n\nhello"
s.removePrefix
assert s == "hello"
doAssert s == "hello"
s = "\rhello"
s.removePrefix
assert s == "hello"
doAssert s == "hello"
s = "hello \n there"
s.removePrefix
assert s == "hello \n there"
doAssert s == "hello \n there"
s = "hello"
s.removePrefix("hel")
assert s == "lo"
doAssert s == "lo"
s.removePrefix('l')
assert s == "o"
doAssert s == "o"
s = "hellos"
s.removePrefix({'h','e'})
assert s == "llos"
doAssert s == "llos"
s.removePrefix({'l','o'})
assert s == "s"
doAssert s == "s"
s = "aeiou"
s.removePrefix("")
assert s == "aeiou"
doAssert s == "aeiou"
s = ""
s.removePrefix("")
assert s == ""
doAssert s == ""
s = " "
s.removePrefix
assert s == " "
doAssert s == " "
s = " "
s.removePrefix("")
assert s == " "
doAssert s == " "
s = " "
s.removePrefix(" ")
assert s == " "
doAssert s == " "
s = " "
s.removePrefix(' ')
assert s == ""
doAssert s == ""
# Contrary to Chomp in other languages
# empty string does not change behaviour
s = "\r\n\r\nhello"
s.removePrefix("")
assert s == "\r\n\r\nhello"
doAssert s == "\r\n\r\nhello"
block: # delete
var s = "0123456789ABCDEFGH"
delete(s, 4, 5)
assert s == "01236789ABCDEFGH"
doAssert s == "01236789ABCDEFGH"
delete(s, s.len-1, s.len-1)
assert s == "01236789ABCDEFG"
doAssert s == "01236789ABCDEFG"
delete(s, 0, 0)
assert s == "1236789ABCDEFG"
doAssert s == "1236789ABCDEFG"
block: # find
assert "0123456789ABCDEFGH".find('A') == 10
assert "0123456789ABCDEFGH".find('A', 5) == 10
assert "0123456789ABCDEFGH".find('A', 5, 10) == 10
assert "0123456789ABCDEFGH".find('A', 5, 9) == -1
assert "0123456789ABCDEFGH".find("A") == 10
assert "0123456789ABCDEFGH".find("A", 5) == 10
assert "0123456789ABCDEFGH".find("A", 5, 10) == 10
assert "0123456789ABCDEFGH".find("A", 5, 9) == -1
assert "0123456789ABCDEFGH".find({'A'..'C'}) == 10
assert "0123456789ABCDEFGH".find({'A'..'C'}, 5) == 10
assert "0123456789ABCDEFGH".find({'A'..'C'}, 5, 10) == 10
assert "0123456789ABCDEFGH".find({'A'..'C'}, 5, 9) == -1
doAssert "0123456789ABCDEFGH".find('A') == 10
doAssert "0123456789ABCDEFGH".find('A', 5) == 10
doAssert "0123456789ABCDEFGH".find('A', 5, 10) == 10
doAssert "0123456789ABCDEFGH".find('A', 5, 9) == -1
doAssert "0123456789ABCDEFGH".find("A") == 10
doAssert "0123456789ABCDEFGH".find("A", 5) == 10
doAssert "0123456789ABCDEFGH".find("A", 5, 10) == 10
doAssert "0123456789ABCDEFGH".find("A", 5, 9) == -1
doAssert "0123456789ABCDEFGH".find({'A'..'C'}) == 10
doAssert "0123456789ABCDEFGH".find({'A'..'C'}, 5) == 10
doAssert "0123456789ABCDEFGH".find({'A'..'C'}, 5, 10) == 10
doAssert "0123456789ABCDEFGH".find({'A'..'C'}, 5, 9) == -1
block: # rfind
assert "0123456789ABCDEFGAH".rfind('A') == 17
assert "0123456789ABCDEFGAH".rfind('A', last=13) == 10
assert "0123456789ABCDEFGAH".rfind('H', last=13) == -1
assert "0123456789ABCDEFGAH".rfind("A") == 17
assert "0123456789ABCDEFGAH".rfind("A", last=13) == 10
assert "0123456789ABCDEFGAH".rfind("H", last=13) == -1
assert "0123456789ABCDEFGAH".rfind({'A'..'C'}) == 17
assert "0123456789ABCDEFGAH".rfind({'A'..'C'}, last=13) == 12
assert "0123456789ABCDEFGAH".rfind({'G'..'H'}, last=13) == -1
assert "0123456789ABCDEFGAH".rfind('A', start=18) == -1
assert "0123456789ABCDEFGAH".rfind('A', start=11, last=17) == 17
assert "0123456789ABCDEFGAH".rfind("0", start=0) == 0
assert "0123456789ABCDEFGAH".rfind("0", start=1) == -1
assert "0123456789ABCDEFGAH".rfind("H", start=11) == 18
assert "0123456789ABCDEFGAH".rfind({'0'..'9'}, start=5) == 9
assert "0123456789ABCDEFGAH".rfind({'0'..'9'}, start=10) == -1
doAssert "0123456789ABCDEFGAH".rfind('A') == 17
doAssert "0123456789ABCDEFGAH".rfind('A', last=13) == 10
doAssert "0123456789ABCDEFGAH".rfind('H', last=13) == -1
doAssert "0123456789ABCDEFGAH".rfind("A") == 17
doAssert "0123456789ABCDEFGAH".rfind("A", last=13) == 10
doAssert "0123456789ABCDEFGAH".rfind("H", last=13) == -1
doAssert "0123456789ABCDEFGAH".rfind({'A'..'C'}) == 17
doAssert "0123456789ABCDEFGAH".rfind({'A'..'C'}, last=13) == 12
doAssert "0123456789ABCDEFGAH".rfind({'G'..'H'}, last=13) == -1
doAssert "0123456789ABCDEFGAH".rfind('A', start=18) == -1
doAssert "0123456789ABCDEFGAH".rfind('A', start=11, last=17) == 17
doAssert "0123456789ABCDEFGAH".rfind("0", start=0) == 0
doAssert "0123456789ABCDEFGAH".rfind("0", start=1) == -1
doAssert "0123456789ABCDEFGAH".rfind("H", start=11) == 18
doAssert "0123456789ABCDEFGAH".rfind({'0'..'9'}, start=5) == 9
doAssert "0123456789ABCDEFGAH".rfind({'0'..'9'}, start=10) == -1
assert "/1/2/3".rfind('/') == 4
assert "/1/2/3".rfind('/', last=1) == 0
assert "/1/2/3".rfind('0') == -1
doAssert "/1/2/3".rfind('/') == 4
doAssert "/1/2/3".rfind('/', last=1) == 0
doAssert "/1/2/3".rfind('0') == -1
block: # trimZeros
var x = "1200"
x.trimZeros()
assert x == "1200"
doAssert x == "1200"
x = "120.0"
x.trimZeros()
assert x == "120"
doAssert x == "120"
x = "0."
x.trimZeros()
assert x == "0"
doAssert x == "0"
x = "1.0e2"
x.trimZeros()
assert x == "1e2"
doAssert x == "1e2"
x = "78.90"
x.trimZeros()
assert x == "78.9"
doAssert x == "78.9"
x = "1.23e4"
x.trimZeros()
assert x == "1.23e4"
doAssert x == "1.23e4"
x = "1.01"
x.trimZeros()
assert x == "1.01"
doAssert x == "1.01"
x = "1.1001"
x.trimZeros()
assert x == "1.1001"
doAssert x == "1.1001"
x = "0.0"
x.trimZeros()
assert x == "0"
doAssert x == "0"
x = "0.01"
x.trimZeros()
assert x == "0.01"
doAssert x == "0.01"
x = "1e0"
x.trimZeros()
assert x == "1e0"
doAssert x == "1e0"
block: # countLines
proc assertCountLines(s: string) = assert s.countLines == s.splitLines.len
proc assertCountLines(s: string) = doAssert s.countLines == s.splitLines.len
assertCountLines("")
assertCountLines("\n")
assertCountLines("\n\n")
@@ -295,36 +295,36 @@ template main() =
block: # parseBinInt, parseHexInt, parseOctInt
# binary
assert "0b1111".parseBinInt == 15
assert "0B1111".parseBinInt == 15
assert "1111".parseBinInt == 15
assert "1110".parseBinInt == 14
assert "1_1_1_1".parseBinInt == 15
assert "0b1_1_1_1".parseBinInt == 15
doAssert "0b1111".parseBinInt == 15
doAssert "0B1111".parseBinInt == 15
doAssert "1111".parseBinInt == 15
doAssert "1110".parseBinInt == 14
doAssert "1_1_1_1".parseBinInt == 15
doAssert "0b1_1_1_1".parseBinInt == 15
rejectParse "".parseBinInt
rejectParse "_".parseBinInt
rejectParse "0b".parseBinInt
rejectParse "0b1234".parseBinInt
# hex
assert "0x72".parseHexInt == 114
assert "0X72".parseHexInt == 114
assert "#72".parseHexInt == 114
assert "72".parseHexInt == 114
assert "FF".parseHexInt == 255
assert "ff".parseHexInt == 255
assert "fF".parseHexInt == 255
assert "0x7_2".parseHexInt == 114
doAssert "0x72".parseHexInt == 114
doAssert "0X72".parseHexInt == 114
doAssert "#72".parseHexInt == 114
doAssert "72".parseHexInt == 114
doAssert "FF".parseHexInt == 255
doAssert "ff".parseHexInt == 255
doAssert "fF".parseHexInt == 255
doAssert "0x7_2".parseHexInt == 114
rejectParse "".parseHexInt
rejectParse "_".parseHexInt
rejectParse "0x".parseHexInt
rejectParse "0xFFG".parseHexInt
rejectParse "reject".parseHexInt
# octal
assert "0o17".parseOctInt == 15
assert "0O17".parseOctInt == 15
assert "17".parseOctInt == 15
assert "10".parseOctInt == 8
assert "0o1_0_0".parseOctInt == 64
doAssert "0o17".parseOctInt == 15
doAssert "0O17".parseOctInt == 15
doAssert "17".parseOctInt == 15
doAssert "10".parseOctInt == 8
doAssert "0o1_0_0".parseOctInt == 64
rejectParse "".parseOctInt
rejectParse "_".parseOctInt
rejectParse "0o".parseOctInt
@@ -333,53 +333,53 @@ template main() =
rejectParse "reject".parseOctInt
block: # parseHexStr
assert "".parseHexStr == ""
assert "00Ff80".parseHexStr == "\0\xFF\x80"
doAssert "".parseHexStr == ""
doAssert "00Ff80".parseHexStr == "\0\xFF\x80"
try:
discard "00Ff8".parseHexStr
assert false, "Should raise ValueError"
doAssert false, "Should raise ValueError"
except ValueError:
discard
try:
discard "0k".parseHexStr
assert false, "Should raise ValueError"
doAssert false, "Should raise ValueError"
except ValueError:
discard
assert "".toHex == ""
assert "\x00\xFF\x80".toHex == "00FF80"
assert "0123456789abcdef".parseHexStr.toHex == "0123456789ABCDEF"
doAssert "".toHex == ""
doAssert "\x00\xFF\x80".toHex == "00FF80"
doAssert "0123456789abcdef".parseHexStr.toHex == "0123456789ABCDEF"
block: # toHex
assert(toHex(100i16, 32) == "00000000000000000000000000000064")
assert(toHex(-100i16, 32) == "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF9C")
doAssert(toHex(100i16, 32) == "00000000000000000000000000000064")
doAssert(toHex(-100i16, 32) == "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF9C")
when not defined js:
assert(toHex(high(uint64)) == "FFFFFFFFFFFFFFFF")
assert(toHex(high(uint64), 16) == "FFFFFFFFFFFFFFFF")
assert(toHex(high(uint64), 32) == "0000000000000000FFFFFFFFFFFFFFFF")
doAssert(toHex(high(uint64)) == "FFFFFFFFFFFFFFFF")
doAssert(toHex(high(uint64), 16) == "FFFFFFFFFFFFFFFF")
doAssert(toHex(high(uint64), 32) == "0000000000000000FFFFFFFFFFFFFFFF")
block: # insertSep
assert(insertSep($1000_000) == "1_000_000")
assert(insertSep($232) == "232")
assert(insertSep($12345, ',') == "12,345")
assert(insertSep($0) == "0")
doAssert(insertSep($1000_000) == "1_000_000")
doAssert(insertSep($232) == "232")
doAssert(insertSep($12345, ',') == "12,345")
doAssert(insertSep($0) == "0")
block: # repeat, spaces
assert(' '.repeat(8) == " ")
assert(" ".repeat(8) == " ")
assert(spaces(8) == " ")
doAssert(' '.repeat(8) == " ")
doAssert(" ".repeat(8) == " ")
doAssert(spaces(8) == " ")
assert(' '.repeat(0) == "")
assert(" ".repeat(0) == "")
assert(spaces(0) == "")
doAssert(' '.repeat(0) == "")
doAssert(" ".repeat(0) == "")
doAssert(spaces(0) == "")
block: # toBin, toOct
block:# bug #11369
var num: int64 = -1
when not defined js:
assert num.toBin(64) == "1111111111111111111111111111111111111111111111111111111111111111"
assert num.toOct(24) == "001777777777777777777777"
doAssert num.toBin(64) == "1111111111111111111111111111111111111111111111111111111111111111"
doAssert num.toOct(24) == "001777777777777777777777"
block: # replace
doAssert "oo".replace("", "abc") == "oo"
@@ -499,26 +499,26 @@ template main() =
doAssert parseEnum("invalid enum value", enC) == enC
block: # indentation
assert 0 == indentation """
doAssert 0 == indentation """
hey
low
there
"""
assert 2 == indentation """
doAssert 2 == indentation """
hey
low
there
"""
assert 2 == indentation """ hey
doAssert 2 == indentation """ hey
low
there
"""
assert 2 == indentation """ hey
doAssert 2 == indentation """ hey
low
there"""
assert 0 == indentation ""
assert 0 == indentation " \n \n"
assert 0 == indentation " "
doAssert 0 == indentation ""
doAssert 0 == indentation " \n \n"
doAssert 0 == indentation " "
block: # indent
doAssert " foo\n bar".indent(4, "Q") == "QQQQ foo\nQQQQ bar"

View File

@@ -47,15 +47,15 @@ doAssert b[1] == 1
import sets, tables
let data = @["bird", "word"] # if this gets stuck in your head, its not my fault
assert collect(newSeq, for (i, d) in data.pairs: (if i mod 2 == 0: d)) == @["bird"]
assert collect(initTable(2), for (i, d) in data.pairs: {i: d}) == {0: "bird",
doAssert collect(newSeq, for (i, d) in data.pairs: (if i mod 2 == 0: d)) == @["bird"]
doAssert collect(initTable(2), for (i, d) in data.pairs: {i: d}) == {0: "bird",
1: "word"}.toTable
assert initHashSet.collect(for d in data.items: {d}) == data.toHashSet
doAssert initHashSet.collect(for d in data.items: {d}) == data.toHashSet
let x = collect(newSeqOfCap(4)):
for (i, d) in data.pairs:
if i mod 2 == 0: d
assert x == @["bird"]
doAssert x == @["bird"]
# bug #12874
@@ -69,20 +69,20 @@ let bug1 = collect(
d & d
)
)
assert bug1 == @["bird", "wordword"]
doAssert bug1 == @["bird", "wordword"]
import strutils
let y = collect(newSeq):
for (i, d) in data.pairs:
try: parseInt(d) except: 0
assert y == @[0, 0]
doAssert y == @[0, 0]
let z = collect(newSeq):
for (i, d) in data.pairs:
case d
of "bird": "word"
else: d
assert z == @["word", "word"]
doAssert z == @["word", "word"]
proc tforum =
let ans = collect(newSeq):
@@ -97,12 +97,12 @@ block:
for d in data.items:
when d is int: "word"
else: d
assert x == @["bird", "word"]
assert collect(for (i, d) in pairs(data): (i, d)) == @[(0, "bird"), (1, "word")]
assert collect(for d in data.items: (try: parseInt(d) except: 0)) == @[0, 0]
assert collect(for (i, d) in pairs(data): {i: d}) == {1: "word",
doAssert x == @["bird", "word"]
doAssert collect(for (i, d) in pairs(data): (i, d)) == @[(0, "bird"), (1, "word")]
doAssert collect(for d in data.items: (try: parseInt(d) except: 0)) == @[0, 0]
doAssert collect(for (i, d) in pairs(data): {i: d}) == {1: "word",
0: "bird"}.toTable
assert collect(for d in data.items: {d}) == data.toHashSet
doAssert collect(for d in data.items: {d}) == data.toHashSet
# bug #14332
template foo =

View File

@@ -5,18 +5,18 @@ var epsilon = 1.0
while 1.0 + epsilon != 1.0:
epsilon /= 2.0
let data = @[1.0, epsilon, -epsilon]
assert sumKbn(data) == 1.0
# assert sumPairs(data) != 1.0 # known to fail in 64 bits
assert (1.0 + epsilon) - epsilon != 1.0
doAssert sumKbn(data) == 1.0
# doAssert sumPairs(data) != 1.0 # known to fail in 64 bits
doAssert (1.0 + epsilon) - epsilon != 1.0
var tc1: seq[float]
for n in 1 .. 1000:
tc1.add 1.0 / n.float
assert sumKbn(tc1) == 7.485470860550345
assert sumPairs(tc1) == 7.485470860550345
doAssert sumKbn(tc1) == 7.485470860550345
doAssert sumPairs(tc1) == 7.485470860550345
var tc2: seq[float]
for n in 1 .. 1000:
tc2.add pow(-1.0, n.float) / n.float
assert sumKbn(tc2) == -0.6926474305598203
assert sumPairs(tc2) == -0.6926474305598204
doAssert sumKbn(tc2) == -0.6926474305598203
doAssert sumPairs(tc2) == -0.6926474305598204

View File

@@ -59,8 +59,8 @@ block: # Deletion from OrderedTable should account for collision groups. See iss
}.toOrderedTable()
t.del(key1)
assert(t.len == 1)
assert(key2 in t)
doAssert(t.len == 1)
doAssert(key2 in t)
var
t1 = initCountTable[string]()
@@ -72,9 +72,9 @@ t2.inc("foo", 4)
t2.inc("bar")
t2.inc("baz", 11)
merge(t1, t2)
assert(t1["foo"] == 5)
assert(t1["bar"] == 3)
assert(t1["baz"] == 14)
doAssert(t1["foo"] == 5)
doAssert(t1["bar"] == 3)
doAssert(t1["baz"] == 14)
let
t1r = newCountTable[string]()
@@ -86,9 +86,9 @@ t2r.inc("foo", 4)
t2r.inc("bar")
t2r.inc("baz", 11)
merge(t1r, t2r)
assert(t1r["foo"] == 5)
assert(t1r["bar"] == 3)
assert(t1r["baz"] == 14)
doAssert(t1r["foo"] == 5)
doAssert(t1r["bar"] == 3)
doAssert(t1r["baz"] == 14)
var
t1l = initCountTable[string]()
@@ -127,28 +127,28 @@ block: #5482
var b = newOrderedTable[string, string](initialSize = 2)
b["wrong?"] = "foo"
b["wrong?"] = "foo2"
assert a == b
doAssert a == b
block: #5482
var a = {"wrong?": "foo", "wrong?": "foo2"}.newOrderedTable()
var b = newOrderedTable[string, string](initialSize = 2)
b["wrong?"] = "foo"
b["wrong?"] = "foo2"
assert a == b
doAssert a == b
block: #5487
var a = {"wrong?": "foo", "wrong?": "foo2"}.newOrderedTable()
var b = newOrderedTable[string, string]() # notice, default size!
b["wrong?"] = "foo"
b["wrong?"] = "foo2"
assert a == b
doAssert a == b
block: #5487
var a = [("wrong?", "foo"), ("wrong?", "foo2")].newOrderedTable()
var b = newOrderedTable[string, string]() # notice, default size!
b["wrong?"] = "foo"
b["wrong?"] = "foo2"
assert a == b
doAssert a == b
block:
var a = {"wrong?": "foo", "wrong?": "foo2"}.newOrderedTable()
@@ -156,22 +156,22 @@ block:
var c = newOrderedTable[string, string]() # notice, default size!
c["wrong?"] = "foo"
c["wrong?"] = "foo2"
assert a == b
assert a == c
doAssert a == b
doAssert a == c
block: #6250
let
a = {3: 1}.toOrderedTable
b = {3: 2}.toOrderedTable
assert((a == b) == false)
assert((b == a) == false)
doAssert((a == b) == false)
doAssert((b == a) == false)
block: #6250
let
a = {3: 2}.toOrderedTable
b = {3: 2}.toOrderedTable
assert((a == b) == true)
assert((b == a) == true)
doAssert((a == b) == true)
doAssert((b == a) == true)
block: # CountTable.smallest
let t = toCountTable([0, 0, 5, 5, 5])

View File

@@ -17,16 +17,16 @@ var test = @[0,1,2,3,4]
var x = toAny(test)
var y = 78
x[4] = toAny(y)
assert x[2].getInt == 2
doAssert x[2].getInt == 2
var test2: tuple[name: string, s: int] = ("test", 56)
var x2 = toAny(test2)
var i = 0
for n, a in fields(x2):
case i
of 0: assert n == "Field0" and $a.kind == "akString"
of 1: assert n == "Field1" and $a.kind == "akInt"
else: assert false
of 0: doAssert n == "Field0" and $a.kind == "akString"
of 1: doAssert n == "Field1" and $a.kind == "akInt"
else: doAssert false
inc i
var test3: TestObj
@@ -36,17 +36,17 @@ var x3 = toAny(test3)
i = 0
for n, a in fields(x3):
case i
of 0: assert n == "test" and $a.kind == "akInt"
of 1: assert n == "asd" and $a.kind == "akInt"
of 2: assert n == "test2" and $a.kind == "akEnum"
else: assert false
of 0: doAssert n == "test" and $a.kind == "akInt"
of 1: doAssert n == "asd" and $a.kind == "akInt"
of 2: doAssert n == "test2" and $a.kind == "akEnum"
else: doAssert false
inc i
var test4: ref string
new(test4)
test4[] = "test"
var x4 = toAny(test4)
assert($x4[].kind() == "akString")
doAssert($x4[].kind() == "akString")
block:
# gimme a new scope dammit

View File

@@ -56,7 +56,7 @@ proc defectiveRobot() =
of 1: raise newException(OSError, "CANNOT COMPUTE!")
of 2: discard parseInt("Hello World!")
of 3: raise newException(IOError, "I can't do that Dave.")
else: assert 2 + 2 == 5
else: doAssert 2 + 2 == 5
test "unittest expect":
expect IOError, OSError, ValueError, AssertionDefect:
defectiveRobot()

View File

@@ -6,15 +6,15 @@ block:
x: XmlNode
x = <>a(href = "http://nim-lang.org", newText("Nim rules."))
assert $x == """<a href="http://nim-lang.org">Nim rules.</a>"""
doAssert $x == """<a href="http://nim-lang.org">Nim rules.</a>"""
x = <>outer(<>inner())
assert $x == """<outer>
doAssert $x == """<outer>
<inner />
</outer>"""
x = <>outer(<>middle(<>inner1(), <>inner2(), <>inner3(), <>inner4()))
assert $x == """<outer>
doAssert $x == """<outer>
<middle>
<inner1 />
<inner2 />
@@ -24,7 +24,7 @@ block:
</outer>"""
x = <>l0(<>l1(<>l2(<>l3(<>l4()))))
assert $x == """<l0>
doAssert $x == """<l0>
<l1>
<l2>
<l3>
@@ -35,14 +35,14 @@ block:
</l0>"""
x = <>l0(<>l1p1(), <>l1p2(), <>l1p3())
assert $x == """<l0>
doAssert $x == """<l0>
<l1p1 />
<l1p2 />
<l1p3 />
</l0>"""
x = <>l0(<>l1(<>l2p1(), <>l2p2()))
assert $x == """<l0>
doAssert $x == """<l0>
<l1>
<l2p1 />
<l2p2 />
@@ -50,7 +50,7 @@ block:
</l0>"""
x = <>l0(<>l1(<>l2_1(), <>l2_2(<>l3_1(), <>l3_2(), <>l3_3(<>l4_1(), <>l4_2(), <>l4_3())), <>l2_3(), <>l2_4()))
assert $x == """<l0>
doAssert $x == """<l0>
<l1>
<l2_1 />
<l2_2>
@@ -72,7 +72,7 @@ block:
middle = newXmlTree("middle", [innermost])
innermost.add newText("innermost text")
x = newXmlTree("outer", [middle])
assert $x == """<outer>
doAssert $x == """<outer>
<middle>
<innermost>innermost text</innermost>
</middle>
@@ -82,4 +82,4 @@ block:
x.add newText("my text")
x.add newElement("sonTag")
x.add newEntity("my entity")
assert $x == "<myTag>my text<sonTag />&my entity;</myTag>"
doAssert $x == "<myTag>my text<sonTag />&my entity;</myTag>"