merge stdlib tests (#9439)

(cherry picked from commit 9fb212cfdf)
This commit is contained in:
Miran
2018-10-19 11:43:35 +02:00
committed by narimiran
parent 42056874d1
commit 38b391ff62
28 changed files with 458 additions and 522 deletions

View File

@@ -1,7 +0,0 @@
import terminal, colors
let codeFg = ansiForegroundColorCode(colAliceBlue)
let codeBg = ansiBackgroundColorCode(colAliceBlue)
doAssert codeFg == "\27[38;2;240;248;255m"
doAssert codeBg == "\27[48;2;240;248;255m"

View File

@@ -1,11 +0,0 @@
discard """
output: '''
02
'''
"""
import re
let regexp = re"^\/([0-9]{2})\.html$"
var matches: array[1, string]
discard "/02.html".find(regexp, matches)
echo matches[0]

View File

@@ -1,29 +0,0 @@
discard """
output: '''1
2
3
4
5
done'''
"""
# bug #1845, #2224
var arr = [3,2,1,5,4]
# bubble sort
for i in low(arr)..high(arr):
for j in i+1..high(arr): # Error: unhandled exception: value out of range: 5 [RangeError]
if arr[i] > arr[j]:
let tmp = arr[i]
arr[i] = arr[j]
arr[j] = tmp
for i in low(arr)..high(arr):
echo arr[i]
# check this terminates:
for x in countdown('\255', '\0'):
discard
echo "done"

View File

@@ -1,13 +1,11 @@
import times, os
var e = epochTime()
var c = cpuTime()
os.sleep(1500)
os.sleep(1000)
e = epochTime() - e
c = cpuTime() - c
echo "epochTime: ", e, " cpuTime: ", c

View File

@@ -1,28 +0,0 @@
discard """
output: '''abc
def
definition
prefix
xyz
def
definition'''
"""
import critbits
when isMainModule:
var r: TCritBitTree[void]
r.incl "abc"
r.incl "xyz"
r.incl "def"
r.incl "definition"
r.incl "prefix"
doAssert r.contains"def"
#r.del "def"
for w in r.items:
echo w
for w in r.itemsWithPrefix("de"):
echo w

View File

@@ -1,3 +0,0 @@
# Simplest Nim program
echo "Hello, World!"

View File

@@ -1,21 +0,0 @@
discard """
output: '''OK'''
"""
#bug #8468
import encodings, strutils
when defined(windows):
var utf16to8 = open(destEncoding = "utf-16", srcEncoding = "utf-8")
var s = "some string"
var c = utf16to8.convert(s)
var z = newStringOfCap(s.len * 2)
for x in s:
z.add x
z.add chr(0)
doAssert z == c
echo "OK"

View File

@@ -1,14 +0,0 @@
discard """
output: ''''''
"""
import sets
doAssert(toSet(@[1,2,3]) <= toSet(@[1,2,3,4]), "equivalent or subset")
doAssert(toSet(@[1,2,3]) <= toSet(@[1,2,3]), "equivalent or subset")
doAssert((not(toSet(@[1,2,3]) <= toSet(@[1,2]))), "equivalent or subset")
doAssert(toSet(@[1,2,3]) <= toSet(@[1,2,3,4]), "strict subset")
doAssert((not(toSet(@[1,2,3]) < toSet(@[1,2,3]))), "strict subset")
doAssert((not(toSet(@[1,2,3]) < toSet(@[1,2]))), "strict subset")
doAssert((not(toSet(@[1,2,3]) == toSet(@[1,2,3,4]))), "==")
doAssert(toSet(@[1,2,3]) == toSet(@[1,2,3]), "==")
doAssert((not(toSet(@[1,2,3]) == toSet(@[1,2]))), "==")

View File

@@ -1,12 +0,0 @@
discard """
file: "tformat.nim"
output: "Hi Andreas! How do you feel, Rumpf?"
"""
# Tests the new format proc (including the & and &= operators)
import strutils
echo("Hi $1! How do you feel, $2?\n" % ["Andreas", "Rumpf"])
#OUT Hi Andreas! How do you feel, Rumpf?

View File

@@ -0,0 +1,80 @@
discard """
output: '''
@[]
true
'''
"""
import htmlparser
import xmltree
import strutils
from streams import newStringStream
block t2813:
const
html = """
<html>
<head>
<title>Test</title>
</head>
<body>
<table>
<thead>
<tr><td>A</td></tr>
<tr><td>B</td></tr>
</thead>
<tbody>
<tr><td></td>A<td></td></tr>
<tr><td></td>B<td></td></tr>
<tr><td></td>C<td></td></tr>
</tbody>
<tfoot>
<tr><td>A</td></tr>
</tfoot>
</table>
</body>
</html>
"""
var errors: seq[string] = @[]
let tree = parseHtml(newStringStream(html), "test.html", errors)
echo errors # Errors: </thead> expected,...
var len = tree.findAll("tr").len # len = 6
var rows: seq[XmlNode] = @[]
for n in tree.findAll("table"):
n.findAll("tr", rows) # len = 2
break
assert tree.findAll("tr").len == rows.len
block t2814:
## builds the two cases below and test that
## ``//[dd,li]`` has "<p>that</p>" as children
##
## <dl>
## <dt>this</dt>
## <dd>
## <p>that</p>
## </dd>
## </dl>
##
## <ul>
## <li>
## <p>that</p>
## </li>
## </ul>
for ltype in [["dl","dd"], ["ul","li"]]:
let desc_item = if ltype[0]=="dl": "<dt>this</dt>" else: ""
let item = "$1<$2><p>that</p></$2>" % [desc_item, ltype[1]]
let list = """ <$1>
$2
</$1> """ % [ltype[0], item]
var errors : seq[string] = @[]
let parseH = parseHtml(newStringStream(list),"statichtml", errors =errors)
if $parseH.findAll(ltype[1])[0].child("p") != "<p>that</p>":
echo "case " & ltype[0] & " failed !"
quit(2)
echo "true"

View File

@@ -1,45 +0,0 @@
discard """
output: "@[]"
"""
import htmlparser
import xmltree
from streams import newStringStream
const
html = """
<html>
<head>
<title>Test</title>
</head>
<body>
<table>
<thead>
<tr><td>A</td></tr>
<tr><td>B</td></tr>
</thead>
<tbody>
<tr><td></td>A<td></td></tr>
<tr><td></td>B<td></td></tr>
<tr><td></td>C<td></td></tr>
</tbody>
<tfoot>
<tr><td>A</td></tr>
</tfoot>
</table>
</body>
</html>
"""
var errors: seq[string] = @[]
let tree = parseHtml(newStringStream(html), "test.html", errors)
echo errors # Errors: </thead> expected,...
var len = tree.findAll("tr").len # len = 6
var rows: seq[XmlNode] = @[]
for n in tree.findAll("table"):
n.findAll("tr", rows) # len = 2
break
assert tree.findAll("tr").len == rows.len

View File

@@ -1,44 +0,0 @@
discard """
output: true
"""
import htmlparser
import xmltree
import strutils
from streams import newStringStream
## builds the two cases below and test that
## ``//[dd,li]`` has "<p>that</p>" as children
##
## <dl>
## <dt>this</dt>
## <dd>
## <p>that</p>
## </dd>
## </dl>
##
## <ul>
## <li>
## <p>that</p>
## </li>
## </ul>
for ltype in [["dl","dd"], ["ul","li"]]:
let desc_item = if ltype[0]=="dl": "<dt>this</dt>" else: ""
let item = "$1<$2><p>that</p></$2>" % [desc_item, ltype[1]]
let list = """ <$1>
$2
</$1> """ % [ltype[0], item]
var errors : seq[string] = @[]
let parseH = parseHtml(newStringStream(list),"statichtml", errors =errors)
if $parseH.findAll(ltype[1])[0].child("p") != "<p>that</p>":
echo "case " & ltype[0] & " failed !"
quit(2)
echo "true"

View File

@@ -1,49 +0,0 @@
discard """
output: '''9
b = true
123456789
Second readLine raised an exception
123456789
1
2aaaaaaaa
3bbbbbbb
'''
"""
# bug #5349
import os
# test the file-IO
const fn = "file9char.txt"
writeFile(fn, "123456789")
var f = open(fn)
echo getFileSize(f)
var line = newString(10)
try:
let b = readLine(f, line)
echo "b = ", b
except:
echo "First readLine raised an exception"
echo line
try:
line = readLine(f)
let b = readLine(f, line)
echo "b = ", b
except:
echo "Second readLine raised an exception"
echo line
f.close()
removeFile(fn)
# bug #8961
writeFile("test.txt", "1\C\L2aaaaaaaa\C\L3bbbbbbb")
for line in lines("test.txt"):
echo line

105
tests/stdlib/tissues.nim Normal file
View File

@@ -0,0 +1,105 @@
discard """
output: '''
02
1
2
3
4
5
9
b = true
123456789
Second readLine raised an exception
123456789
1
2aaaaaaaa
3bbbbbbb
'''
"""
import terminal, colors, re, encodings, strutils, os
block t9394:
let codeFg = ansiForegroundColorCode(colAliceBlue)
let codeBg = ansiBackgroundColorCode(colAliceBlue)
doAssert codeFg == "\27[38;2;240;248;255m"
doAssert codeBg == "\27[48;2;240;248;255m"
block t5382:
let regexp = re"^\/([0-9]{2})\.html$"
var matches: array[1, string]
discard "/02.html".find(regexp, matches)
echo matches[0]
block tcount:
# bug #1845, #2224
var arr = [3,2,1,5,4]
# bubble sort
for i in low(arr)..high(arr):
for j in i+1..high(arr): # Error: unhandled exception: value out of range: 5 [RangeError]
if arr[i] > arr[j]:
let tmp = arr[i]
arr[i] = arr[j]
arr[j] = tmp
for i in low(arr)..high(arr):
echo arr[i]
# check this terminates:
for x in countdown('\255', '\0'):
discard
block t8468:
when defined(windows):
var utf16to8 = open(destEncoding = "utf-16", srcEncoding = "utf-8")
var s = "some string"
var c = utf16to8.convert(s)
var z = newStringOfCap(s.len * 2)
for x in s:
z.add x
z.add chr(0)
doAssert z == c
block t5349:
const fn = "file9char.txt"
writeFile(fn, "123456789")
var f = system.open(fn)
echo getFileSize(f)
var line = newString(10)
try:
let b = readLine(f, line)
echo "b = ", b
except:
echo "First readLine raised an exception"
echo line
try:
line = readLine(f)
let b = readLine(f, line)
echo "b = ", b
except:
echo "Second readLine raised an exception"
echo line
f.close()
removeFile(fn)
# bug #8961
writeFile("test.txt", "1\C\L2aaaaaaaa\C\L3bbbbbbb")
for line in lines("test.txt"):
echo line

View File

@@ -1,6 +0,0 @@
discard """
output: ""
"""
var x = @["1", "", "3"]
doAssert $x == """@["1", "", "3"]"""

View File

@@ -1,18 +0,0 @@
import tables, random
var t = initOrderedTable[int,string]()
# this tests issue #5917
var data = newSeq[int]()
for i in 0..<1000:
var x = random(1000)
if x notin t: data.add(x)
t[x] = "meh"
# this checks that keys are re-inserted
# in order when table is enlarged.
var i = 0
for k, v in t:
doAssert(k == data[i])
doAssert(v == "meh")
inc(i)

View File

@@ -1,19 +0,0 @@
discard """
output: '''@[0, 2, 1]
@[1, 0, 2]
@[1, 2, 0]
@[2, 0, 1]
@[2, 1, 0]
@[2, 0, 1]
@[1, 2, 0]
@[1, 0, 2]
@[0, 2, 1]
@[0, 1, 2]'''
"""
import algorithm
var v = @[0, 1, 2]
while v.nextPermutation():
echo v
while v.prevPermutation():
echo v

View File

@@ -1,27 +0,0 @@
discard """
file: "treguse.nim"
output: "055this should be the casehugh"
"""
# Test the register usage of the virtual machine and
# the blocks in var statements
proc main(a, b: int) =
var x = 0
write(stdout, x)
if x == 0:
var y = 55
write(stdout, y)
write(stdout, "this should be the case")
var input = "<no input>"
if input == "Andreas":
write(stdout, "wow")
else:
write(stdout, "hugh")
else:
var z = 66
write(stdout, z) # "bug!")
main(45, 1000)
#OUT 055this should be the casehugh

View File

@@ -1,9 +0,0 @@
discard """
output: '''@["(", "+", " 1", " 2", ")"]'''
"""
import re
let str = "(+ 1 2)"
var tokenRE = re"""[\s,]*(~@|[\[\]{}()'`~^@]|"(?:\\.|[^\\"])*"|;.*|[^\s\[\]{}('"`,;)]*)"""
echo str.findAll(tokenRE)

View File

@@ -1,36 +0,0 @@
discard """
file: "tropes.nim"
output: '''0
3
123
3
6
123
123456
2
3'''
"""
import ropes
var
r1 = rope("")
r2 = rope("123")
echo r1.len
echo r2.len
echo r1
echo r2
r1.add("123")
r2.add("456")
echo r1.len
echo r2.len
echo r1
echo r2
echo r1[1]
echo r2[2]

View File

@@ -1,29 +0,0 @@
discard """
output: '''caught a crash!
caught a crash!
caught a crash!
caught a crash!
caught a crash!
caught a crash!
caught a crash!
caught a crash!
caught a crash!
caught a crash!
caught a crash!'''
"""
import segfaults
proc main =
try:
var x: ptr int
echo x[]
try:
raise newException(ValueError, "not a crash")
except ValueError:
discard
except NilAccessError:
echo "caught a crash!"
for i in 0..10:
main()

View File

@@ -1,29 +0,0 @@
discard """
output: '''[5]
[4, 5]
[3, 4, 5]
[2, 3, 4, 5]
[2, 3, 4, 5, 6]
[2, 3, 4, 5, 6, 7]
[2, 3, 4, 5, 6, 7, 8]
[1, 2, 3, 4, 5, 6, 7, 8]'''
"""
import lists
var r = initSinglyLinkedRing[int]()
r.prepend(5)
echo r
r.prepend(4)
echo r
r.prepend(3)
echo r
r.prepend(2)
echo r
r.append(6)
echo r
r.append(7)
echo r
r.append(8)
echo r
r.prepend(1)
echo r

View File

@@ -1,20 +0,0 @@
discard """
file: "tsplit.nim"
output: "true"
"""
import strutils
var s = ""
for w in split("|abc|xy|z", {'|'}):
s.add("#")
s.add(w)
if s == "##abc#xy#z":
echo "true"
else:
echo "false"
#OUT true

View File

@@ -1,19 +0,0 @@
discard """
file: "tsplit2.nim"
output: "true"
"""
import strutils
var s = ""
for w in split("|abc|xy|z", {'|'}):
s.add("#")
s.add(w)
try:
discard "hello".split("")
echo "false"
except AssertionError:
echo "true"
#OUT true

View File

@@ -1,7 +1,27 @@
import streams
var outp = newFileStream(stdout)
var inp = newFileStream(stdin)
write(outp, "Hello! What is your name?")
var line = readLine(inp)
write(outp, "Nice name: " & line)
block tstreams:
var outp = newFileStream(stdout)
var inp = newFileStream(stdin)
write(outp, "Hello! What is your name?")
var line = readLine(inp)
write(outp, "Nice name: " & line)
block tstreams2:
var
fs = newFileStream("amissingfile.txt")
line = ""
echo "fs is: ",repr(fs)
if not isNil(fs):
while fs.readLine(line):
echo line
fs.close()
block tstreams3:
try:
var fs = openFileStream("shouldneverexist.txt")
except IoError:
echo "threw exception"

247
tests/stdlib/tvarious.nim Normal file
View File

@@ -0,0 +1,247 @@
discard """
output: '''
abc
def
definition
prefix
xyz
def
definition
Hi Andreas! How do you feel, Rumpf?
@[0, 2, 1]
@[1, 0, 2]
@[1, 2, 0]
@[2, 0, 1]
@[2, 1, 0]
@[2, 0, 1]
@[1, 2, 0]
@[1, 0, 2]
@[0, 2, 1]
@[0, 1, 2]
055this should be the casehugh@["(", "+", " 1", " 2", ")"]
caught a crash!
caught a crash!
caught a crash!
caught a crash!
caught a crash!
caught a crash!
[5]
[4, 5]
[3, 4, 5]
[2, 3, 4, 5]
[2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]
true
<h1><a href="http://force7.de/nim">Nim</a></h1>
'''
"""
import
critbits, sets, strutils, tables, random, algorithm, re, ropes, segfaults,
lists, parsesql, streams, os, htmlgen, xmltree, strtabs
block tcritbits:
var r: CritBitTree[void]
r.incl "abc"
r.incl "xyz"
r.incl "def"
r.incl "definition"
r.incl "prefix"
doAssert r.contains"def"
#r.del "def"
for w in r.items:
echo w
for w in r.itemsWithPrefix("de"):
echo w
block testequivalence:
doAssert(toSet(@[1,2,3]) <= toSet(@[1,2,3,4]), "equivalent or subset")
doAssert(toSet(@[1,2,3]) <= toSet(@[1,2,3]), "equivalent or subset")
doAssert((not(toSet(@[1,2,3]) <= toSet(@[1,2]))), "equivalent or subset")
doAssert(toSet(@[1,2,3]) <= toSet(@[1,2,3,4]), "strict subset")
doAssert((not(toSet(@[1,2,3]) < toSet(@[1,2,3]))), "strict subset")
doAssert((not(toSet(@[1,2,3]) < toSet(@[1,2]))), "strict subset")
doAssert((not(toSet(@[1,2,3]) == toSet(@[1,2,3,4]))), "==")
doAssert(toSet(@[1,2,3]) == toSet(@[1,2,3]), "==")
doAssert((not(toSet(@[1,2,3]) == toSet(@[1,2]))), "==")
block tformat:
echo("Hi $1! How do you feel, $2?\n" % ["Andreas", "Rumpf"])
block tnilecho:
var x = @["1", "", "3"]
doAssert $x == """@["1", "", "3"]"""
block torderedtable:
var t = initOrderedTable[int,string]()
# this tests issue #5917
var data = newSeq[int]()
for i in 0..<1000:
var x = rand(1000)
if x notin t: data.add(x)
t[x] = "meh"
# this checks that keys are re-inserted
# in order when table is enlarged.
var i = 0
for k, v in t:
doAssert(k == data[i])
doAssert(v == "meh")
inc(i)
block tpermutations:
var v = @[0, 1, 2]
while v.nextPermutation():
echo v
while v.prevPermutation():
echo v
block treguse:
proc main(a, b: int) =
var x = 0
write(stdout, x)
if x == 0:
var y = 55
write(stdout, y)
write(stdout, "this should be the case")
var input = "<no input>"
if input == "Andreas":
write(stdout, "wow")
else:
write(stdout, "hugh")
else:
var z = 66
write(stdout, z) # "bug!")
main(45, 1000)
block treloop:
let str = "(+ 1 2)"
var tokenRE = re"""[\s,]*(~@|[\[\]{}()'`~^@]|"(?:\\.|[^\\"])*"|;.*|[^\s\[\]{}('"`,;)]*)"""
echo str.findAll(tokenRE)
block tropes:
var
r1 = rope("")
r2 = rope("123")
doAssert r1.len == 0
doAssert r2.len == 3
doAssert $r1 == ""
doAssert $r2 == "123"
r1.add("123")
r2.add("456")
doAssert r1.len == 3
doAssert r2.len == 6
doAssert $r1 == "123"
doAssert $r2 == "123456"
doAssert $r1[1] == "2"
doAssert $r2[2] == "3"
block tsegfaults:
proc main =
try:
var x: ptr int
echo x[]
try:
raise newException(ValueError, "not a crash")
except ValueError:
discard
except NilAccessError:
echo "caught a crash!"
for i in 0..5:
main()
block tsinglylinkedring:
var r = initSinglyLinkedRing[int]()
r.prepend(5)
echo r
r.prepend(4)
echo r
r.prepend(3)
echo r
r.prepend(2)
echo r
r.append(6)
echo r
r.prepend(1)
echo r
block tsplit:
var s = ""
for w in split("|abc|xy|z", {'|'}):
s.add("#")
s.add(w)
doAssert s == "##abc#xy#z"
block tsplit2:
var s = ""
for w in split("|abc|xy|z", {'|'}):
s.add("#")
s.add(w)
try:
discard "hello".split("")
echo "false"
except AssertionError:
echo "true"
block tsqlparser:
# Just check that we can parse 'somesql' and render it without crashes.
var tree = parseSql(newFileStream(getAppDir() / "somesql.sql"), "somesql")
discard renderSql(tree)
block txmlgen:
var nim = "Nim"
echo h1(a(href="http://force7.de/nim", nim))
block txmltree:
var x = <>a(href="nim.de", newText("www.nim-test.de"))
doAssert($x == "<a href=\"nim.de\">www.nim-test.de</a>")
doAssert(newText("foo").innerText == "foo")
doAssert(newEntity("bar").innerText == "bar")
doAssert(newComment("baz").innerText == "")
let y = newXmlTree("x", [
newText("foo"),
newXmlTree("y", [
newText("bar")
])
])
doAssert(y.innerText == "foobar")

View File

@@ -1,12 +0,0 @@
discard """
file: "txmlgen.nim"
output: "<h1><a href=\"http://force7.de/nim\">Nim</a></h1>"
"""
import htmlgen
var nim = "Nim"
echo h1(a(href="http://force7.de/nim", nim))

View File

@@ -1,27 +0,0 @@
discard """
file: "txmltree.nim"
output: '''true
true
true
true
true
'''
"""
import xmltree, strtabs
var x = <>a(href="nim.de", newText("www.nim-test.de"))
echo($x == "<a href=\"nim.de\">www.nim-test.de</a>")
echo(newText("foo").innerText == "foo")
echo(newEntity("bar").innerText == "bar")
echo(newComment("baz").innerText == "")
let y = newXmlTree("x", [
newText("foo"),
newXmlTree("y", [
newText("bar")
])
])
echo(y.innerText == "foobar")