got rid of 'accept' dir in the tests
This commit is contained in:
parent
d0772feb08
commit
a274f3bf5b
264 changed files with 5 additions and 7 deletions
7
tests/run/mambsys1.nim
Executable file
7
tests/run/mambsys1.nim
Executable file
|
|
@ -0,0 +1,7 @@
|
|||
import mambsys2 # import TExport
|
||||
|
||||
type
|
||||
TExport* = enum x, y, z
|
||||
|
||||
proc foo*(x: int) =
|
||||
nil
|
||||
4
tests/run/mambsys2.nim
Executable file
4
tests/run/mambsys2.nim
Executable file
|
|
@ -0,0 +1,4 @@
|
|||
type
|
||||
TExport* = enum x, y, z # exactly the same type!
|
||||
|
||||
proc foo*(x: int) = nil
|
||||
10
tests/run/mbind3.nim
Executable file
10
tests/run/mbind3.nim
Executable file
|
|
@ -0,0 +1,10 @@
|
|||
# Module A
|
||||
var
|
||||
lastId = 0
|
||||
|
||||
template genId*: expr =
|
||||
bind lastId
|
||||
inc(lastId)
|
||||
lastId
|
||||
|
||||
|
||||
11
tests/run/mfriends.nim
Normal file
11
tests/run/mfriends.nim
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
|
||||
type
|
||||
TMyObj = object
|
||||
x: int
|
||||
|
||||
proc gen*[T](): T =
|
||||
var d: TMyObj
|
||||
# access private field here
|
||||
d.x = 3
|
||||
result = d.x
|
||||
|
||||
2
tests/run/minit.nim
Executable file
2
tests/run/minit.nim
Executable file
|
|
@ -0,0 +1,2 @@
|
|||
# Test the new initialization for modules
|
||||
write(stdout, "Hello from module! ")
|
||||
12
tests/run/mmultim3.nim
Executable file
12
tests/run/mmultim3.nim
Executable file
|
|
@ -0,0 +1,12 @@
|
|||
type
|
||||
TObj* = object
|
||||
|
||||
var myObj* : ref TObj
|
||||
|
||||
method test123(a : ref TObj) =
|
||||
echo("Hi base!")
|
||||
|
||||
proc testMyObj*() =
|
||||
test123(myObj)
|
||||
|
||||
|
||||
21
tests/run/tack.nim
Executable file
21
tests/run/tack.nim
Executable file
|
|
@ -0,0 +1,21 @@
|
|||
discard """
|
||||
file: "tack.nim"
|
||||
output: "125"
|
||||
"""
|
||||
# the Ackermann function
|
||||
|
||||
proc ack(x, y: int): int =
|
||||
if x != 0:
|
||||
if y != 0:
|
||||
return ack(x-1, ack(x, y-1))
|
||||
return ack(x-1, 1)
|
||||
else:
|
||||
return y + 1
|
||||
# if x == 0: return y + 1
|
||||
# elif y == 0: return ack(x-1, 1)
|
||||
# else: return ack(x-1, ack(x, y-1))
|
||||
|
||||
# echo(ack(0, 0))
|
||||
write(stdout, ack(3, 4)) #OUT 125
|
||||
|
||||
|
||||
27
tests/run/tactiontable.nim
Normal file
27
tests/run/tactiontable.nim
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
discard """
|
||||
output: "action 3 arg"
|
||||
"""
|
||||
|
||||
import tables
|
||||
|
||||
proc action1(arg: string) =
|
||||
echo "action 1 ", arg
|
||||
|
||||
proc action2(arg: string) =
|
||||
echo "action 2 ", arg
|
||||
|
||||
proc action3(arg: string) =
|
||||
echo "action 3 ", arg
|
||||
|
||||
proc action4(arg: string) =
|
||||
echo "action 4 ", arg
|
||||
|
||||
var
|
||||
actionTable = {
|
||||
"A": action1,
|
||||
"B": action2,
|
||||
"C": action3,
|
||||
"D": action4}.toTable
|
||||
|
||||
actionTable["C"]("arg")
|
||||
|
||||
24
tests/run/tambsym2.nim
Executable file
24
tests/run/tambsym2.nim
Executable file
|
|
@ -0,0 +1,24 @@
|
|||
discard """
|
||||
file: "tambsym2.nim"
|
||||
output: "7"
|
||||
"""
|
||||
# Test overloading of procs with locals
|
||||
|
||||
type
|
||||
TMyType = object
|
||||
len: int
|
||||
data: string
|
||||
|
||||
proc len(x: TMyType): int {.inline.} = return x.len
|
||||
|
||||
proc x(s: TMyType, len: int) =
|
||||
writeln(stdout, len(s))
|
||||
|
||||
var
|
||||
m: TMyType
|
||||
m.len = 7
|
||||
m.data = "1234"
|
||||
|
||||
x(m, 5) #OUT 7
|
||||
|
||||
|
||||
13
tests/run/tambsys.nim
Executable file
13
tests/run/tambsys.nim
Executable file
|
|
@ -0,0 +1,13 @@
|
|||
discard """
|
||||
file: "tambsys.nim"
|
||||
output: ""
|
||||
"""
|
||||
# Test ambiguous symbols
|
||||
|
||||
import mambsys1, mambsys2
|
||||
|
||||
var
|
||||
v: mambsys1.TExport
|
||||
mambsys2.foo(3) #OUT
|
||||
|
||||
|
||||
15
tests/run/tanontuples.nim
Normal file
15
tests/run/tanontuples.nim
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
discard """
|
||||
output: "61, 125"
|
||||
"""
|
||||
|
||||
proc `^` (a, b: int): int =
|
||||
result = 1
|
||||
for i in 1..b: result = result * a
|
||||
|
||||
var m = (0, 5)
|
||||
var n = (56, 3)
|
||||
|
||||
m = (n[0] + m[1], m[1] ^ n[1])
|
||||
|
||||
echo m[0], ", ", m[1]
|
||||
|
||||
33
tests/run/tarray.nim
Executable file
33
tests/run/tarray.nim
Executable file
|
|
@ -0,0 +1,33 @@
|
|||
discard """
|
||||
file: "tarray.nim"
|
||||
output: "10012"
|
||||
"""
|
||||
# simple check for one dimensional arrays
|
||||
|
||||
type
|
||||
TMyArray = array[0..2, int]
|
||||
TMyRecord = tuple[x, y: int]
|
||||
|
||||
proc sum(a: TMyarray): int =
|
||||
result = 0
|
||||
var i = 0
|
||||
while i < len(a):
|
||||
inc(result, a[i])
|
||||
inc(i)
|
||||
|
||||
proc sum(a: openarray[int]): int =
|
||||
result = 0
|
||||
var i = 0
|
||||
while i < len(a):
|
||||
inc(result, a[i])
|
||||
inc(i)
|
||||
|
||||
proc getPos(r: TMyRecord): int =
|
||||
result = r.x + r.y
|
||||
|
||||
write(stdout, sum([1, 2, 3, 4]))
|
||||
write(stdout, sum([]))
|
||||
write(stdout, getPos( (x: 5, y: 7) ))
|
||||
#OUT 10012
|
||||
|
||||
|
||||
24
tests/run/tarray2.nim
Executable file
24
tests/run/tarray2.nim
Executable file
|
|
@ -0,0 +1,24 @@
|
|||
discard """
|
||||
file: "tarray2.nim"
|
||||
output: "[16, 25, 36]"
|
||||
"""
|
||||
# simple check for one dimensional arrays
|
||||
|
||||
type
|
||||
TMyArray = array[0..2, int]
|
||||
|
||||
proc mul(a, b: TMyarray): TMyArray =
|
||||
result = a
|
||||
for i in 0..len(a)-1:
|
||||
result[i] = a[i] * b[i]
|
||||
|
||||
var
|
||||
x, y, z: TMyArray
|
||||
|
||||
x = [ 4, 5, 6 ]
|
||||
y = x
|
||||
echo repr(mul(x, y))
|
||||
|
||||
#OUT [16, 25, 36]
|
||||
|
||||
|
||||
13
tests/run/tarray3.nim
Executable file
13
tests/run/tarray3.nim
Executable file
|
|
@ -0,0 +1,13 @@
|
|||
discard """
|
||||
file: "tarray3.nim"
|
||||
output: "3"
|
||||
"""
|
||||
# simple check for two dimensional arrays
|
||||
|
||||
const
|
||||
myData = [[1,2,3], [4, 5, 6]]
|
||||
|
||||
echo myData[0][2] #OUT 3
|
||||
|
||||
|
||||
|
||||
23
tests/run/tarraycons.nim
Executable file
23
tests/run/tarraycons.nim
Executable file
|
|
@ -0,0 +1,23 @@
|
|||
discard """
|
||||
file: "tarraycons.nim"
|
||||
output: "6"
|
||||
"""
|
||||
|
||||
type
|
||||
TEnum = enum
|
||||
eA, eB, eC, eD, eE, eF
|
||||
|
||||
const
|
||||
myMapping: array[TEnum, array[0..1, int]] = [
|
||||
eA: [1, 2],
|
||||
eB: [3, 4],
|
||||
[5, 6],
|
||||
eD: [0: 8, 1: 9],
|
||||
eE: [0: 8, 9],
|
||||
eF: [2, 1: 9]
|
||||
]
|
||||
|
||||
echo myMapping[eC][1]
|
||||
|
||||
|
||||
|
||||
23
tests/run/tassert.nim
Executable file
23
tests/run/tassert.nim
Executable file
|
|
@ -0,0 +1,23 @@
|
|||
discard """
|
||||
file: "tassert.nim"
|
||||
outputsub: "assertion failure!this shall be always written"
|
||||
exitcode: "1"
|
||||
"""
|
||||
# test assert and exception handling
|
||||
|
||||
proc callB() = assert(False)
|
||||
proc callA() = callB()
|
||||
proc callC() = callA()
|
||||
|
||||
try:
|
||||
callC()
|
||||
except EAssertionFailed:
|
||||
write(stdout, "assertion failure!")
|
||||
except:
|
||||
write(stdout, "unknown exception!")
|
||||
finally:
|
||||
system.write(stdout, "this shall be always written")
|
||||
|
||||
assert(false) #OUT assertion failure!this shall be always written
|
||||
|
||||
|
||||
21
tests/run/tbind1.nim
Executable file
21
tests/run/tbind1.nim
Executable file
|
|
@ -0,0 +1,21 @@
|
|||
discard """
|
||||
file: "tbind1.nim"
|
||||
output: "3"
|
||||
"""
|
||||
# Test the new ``bind`` keyword for templates
|
||||
|
||||
proc p1(x: int8, y: int): int = return x + y
|
||||
|
||||
template tempBind(x, y: expr): expr =
|
||||
bind p1
|
||||
p1(x, y)
|
||||
|
||||
proc p1(x: int, y: int8): int = return x - y
|
||||
|
||||
# This is tricky: the call to ``p1(1'i8, 2'i8)`` should not fail in line 6,
|
||||
# because it is not ambiguous there. But it is ambiguous after line 8.
|
||||
|
||||
echo tempBind(1'i8, 2'i8) #OUT 3
|
||||
|
||||
|
||||
|
||||
11
tests/run/tbind3.nim
Executable file
11
tests/run/tbind3.nim
Executable file
|
|
@ -0,0 +1,11 @@
|
|||
discard """
|
||||
file: "tbind3.nim"
|
||||
output: "1"
|
||||
"""
|
||||
# Module B
|
||||
import mbind3
|
||||
|
||||
echo genId() #OUT 1
|
||||
|
||||
|
||||
|
||||
31
tests/run/tbintre2.nim
Executable file
31
tests/run/tbintre2.nim
Executable file
|
|
@ -0,0 +1,31 @@
|
|||
discard """
|
||||
file: "tbintre2.nim"
|
||||
output: "helloworld99110223"
|
||||
"""
|
||||
# Same test, but check module boundaries
|
||||
|
||||
import tbintree
|
||||
|
||||
var
|
||||
root: PBinaryTree[string]
|
||||
x = newNode("hello")
|
||||
add(root, x)
|
||||
add(root, "world")
|
||||
if find(root, "world"):
|
||||
for str in items(root):
|
||||
stdout.write(str)
|
||||
else:
|
||||
stdout.writeln("BUG")
|
||||
|
||||
var
|
||||
r2: PBinaryTree[int]
|
||||
add(r2, newNode(110))
|
||||
add(r2, 223)
|
||||
add(r2, 99)
|
||||
for y in items(r2):
|
||||
stdout.write(y)
|
||||
|
||||
#OUT helloworld99110223
|
||||
|
||||
|
||||
|
||||
107
tests/run/tbintree.nim
Executable file
107
tests/run/tbintree.nim
Executable file
|
|
@ -0,0 +1,107 @@
|
|||
discard """
|
||||
file: "tbintree.nim"
|
||||
output: "helloworld99110223"
|
||||
"""
|
||||
type
|
||||
TBinaryTree[T] = object # TBinaryTree is a generic type with
|
||||
# with generic param ``T``
|
||||
le, ri: ref TBinaryTree[T] # left and right subtrees; may be nil
|
||||
data: T # the data stored in a node
|
||||
PBinaryTree*[A] = ref TBinaryTree[A] # type that is exported
|
||||
|
||||
proc newNode*[T](data: T): PBinaryTree[T] =
|
||||
# constructor for a node
|
||||
new(result)
|
||||
result.data = data
|
||||
|
||||
proc add*[Ty](root: var PBinaryTree[Ty], n: PBinaryTree[Ty]) =
|
||||
# insert a node into the tree
|
||||
if root == nil:
|
||||
root = n
|
||||
else:
|
||||
var it = root
|
||||
while it != nil:
|
||||
# compare the data items; uses the generic ``cmp`` proc that works for
|
||||
# any type that has a ``==`` and ``<`` operator
|
||||
var c = cmp(n.data, it.data)
|
||||
if c < 0:
|
||||
if it.le == nil:
|
||||
it.le = n
|
||||
return
|
||||
it = it.le
|
||||
else:
|
||||
if it.ri == nil:
|
||||
it.ri = n
|
||||
return
|
||||
it = it.ri
|
||||
|
||||
proc add*[Ty](root: var PBinaryTree[Ty], data: Ty) =
|
||||
# convenience proc:
|
||||
add(root, newNode(data))
|
||||
|
||||
proc find*[Ty2](b: PBinaryTree[Ty2], data: Ty2): bool =
|
||||
# for testing this needs to be recursive, so that the
|
||||
# instantiated type is checked for proper tyGenericInst envelopes
|
||||
if b == nil:
|
||||
result = false
|
||||
else:
|
||||
var c = cmp(data, b.data)
|
||||
if c < 0: result = find(b.le, data)
|
||||
elif c > 0: result = find(b.ri, data)
|
||||
else: result = true
|
||||
|
||||
iterator preorder*[T](root: PBinaryTree[T]): T =
|
||||
# Preorder traversal of a binary tree.
|
||||
# Since recursive iterators are not yet implemented,
|
||||
# this uses an explicit stack:
|
||||
var stack: seq[PBinaryTree[T]] = @[root]
|
||||
while stack.len > 0:
|
||||
var n = stack.pop()
|
||||
while n != nil:
|
||||
yield n.data
|
||||
add(stack, n.ri) # push right subtree onto the stack
|
||||
n = n.le # and follow the left pointer
|
||||
|
||||
iterator items*[T](root: PBinaryTree[T]): T =
|
||||
## Inorder traversal of the binary tree.
|
||||
var stack: seq[PBinaryTree[T]] = @[]
|
||||
var n = root
|
||||
while true:
|
||||
while n != nil:
|
||||
add(stack, n)
|
||||
n = n.le
|
||||
if stack.len > 0:
|
||||
n = stack.pop()
|
||||
yield n.data
|
||||
n = n.ri
|
||||
if stack.len == 0 and n == nil: break
|
||||
|
||||
proc debug[T](a: PBinaryTree[T]) =
|
||||
if a != nil:
|
||||
debug(a.le)
|
||||
echo a.data
|
||||
debug(a.ri)
|
||||
|
||||
when isMainModule:
|
||||
var
|
||||
root: PBinaryTree[string]
|
||||
x = newNode("hello")
|
||||
add(root, x)
|
||||
add(root, "world")
|
||||
if find(root, "world"):
|
||||
for str in items(root):
|
||||
stdout.write(str)
|
||||
else:
|
||||
stdout.writeln("BUG")
|
||||
|
||||
var
|
||||
r2: PBinaryTree[int]
|
||||
add(r2, newNode(110))
|
||||
add(r2, 223)
|
||||
add(r2, 99)
|
||||
for y in items(r2):
|
||||
stdout.write(y)
|
||||
|
||||
#OUT helloworld99110223
|
||||
|
||||
|
||||
12
tests/run/tbug499771.nim
Executable file
12
tests/run/tbug499771.nim
Executable file
|
|
@ -0,0 +1,12 @@
|
|||
discard """
|
||||
file: "tbug499771.nim"
|
||||
output: "TSubRange: 5 from 1 to 10"
|
||||
"""
|
||||
type TSubRange = range[1 .. 10]
|
||||
var sr: TSubRange = 5
|
||||
echo("TSubRange: " & $sr & " from " & $low(TSubRange) & " to " &
|
||||
$high(TSubRange))
|
||||
|
||||
|
||||
|
||||
|
||||
16
tests/run/tbug511622.nim
Executable file
16
tests/run/tbug511622.nim
Executable file
|
|
@ -0,0 +1,16 @@
|
|||
discard """
|
||||
file: "tbug511622.nim"
|
||||
output: "3"
|
||||
"""
|
||||
import StrUtils, Math
|
||||
|
||||
proc FibonacciA(n: int): int64 =
|
||||
var fn = float64(n)
|
||||
var p: float64 = (1.0 + sqrt(5.0)) / 2.0
|
||||
var q: float64 = 1.0 / p
|
||||
return int64((pow(p, fn) + pow(q, fn)) / sqrt(5.0))
|
||||
|
||||
echo FibonacciA(4) #OUT 3
|
||||
|
||||
|
||||
|
||||
15
tests/run/tcase_setconstr.nim
Executable file
15
tests/run/tcase_setconstr.nim
Executable file
|
|
@ -0,0 +1,15 @@
|
|||
discard """
|
||||
output: "an identifier"
|
||||
"""
|
||||
|
||||
const
|
||||
SymChars: set[char] = {'a'..'z', 'A'..'Z', '\x80'..'\xFF'}
|
||||
|
||||
proc classify(s: string) =
|
||||
case s[0]
|
||||
of SymChars, '_': echo "an identifier"
|
||||
of {'0'..'9'}: echo "a number"
|
||||
else: echo "other"
|
||||
|
||||
classify("Hurra")
|
||||
|
||||
38
tests/run/tcasestm.nim
Executable file
38
tests/run/tcasestm.nim
Executable file
|
|
@ -0,0 +1,38 @@
|
|||
discard """
|
||||
file: "tcasestm.nim"
|
||||
output: "ayyy"
|
||||
"""
|
||||
# Test the case statement
|
||||
|
||||
type
|
||||
tenum = enum eA, eB, eC
|
||||
|
||||
var
|
||||
x: string = "yyy"
|
||||
y: Tenum = eA
|
||||
i: int
|
||||
|
||||
case y
|
||||
of eA: write(stdout, "a")
|
||||
of eB, eC: write(stdout, "b or c")
|
||||
|
||||
case x
|
||||
of "Andreas", "Rumpf": write(stdout, "Hallo Meister!")
|
||||
of "aa", "bb": write(stdout, "Du bist nicht mein Meister")
|
||||
of "cc", "hash", "when": nil
|
||||
of "will", "it", "finally", "be", "generated": nil
|
||||
|
||||
case i
|
||||
of 1..5, 8, 9: nil
|
||||
of 6, 7: nil
|
||||
elif x == "Ha":
|
||||
nil
|
||||
elif x == "yyy":
|
||||
write(stdout, x)
|
||||
else:
|
||||
nil
|
||||
|
||||
#OUT ayyy
|
||||
|
||||
|
||||
|
||||
23
tests/run/tcgbug.nim
Executable file
23
tests/run/tcgbug.nim
Executable file
|
|
@ -0,0 +1,23 @@
|
|||
discard """
|
||||
file: "tcgbug.nim"
|
||||
output: "success"
|
||||
"""
|
||||
|
||||
type
|
||||
TObj = object
|
||||
x, y: int
|
||||
PObj = ref TObj
|
||||
|
||||
proc p(a: PObj) =
|
||||
a.x = 0
|
||||
|
||||
proc q(a: var PObj) =
|
||||
a.p()
|
||||
|
||||
var
|
||||
a: PObj
|
||||
new(a)
|
||||
q(a)
|
||||
|
||||
echo "success"
|
||||
|
||||
33
tests/run/tclosure.nim
Executable file
33
tests/run/tclosure.nim
Executable file
|
|
@ -0,0 +1,33 @@
|
|||
discard """
|
||||
file: "tclosure.nim"
|
||||
output: "2 4 6 8 10"
|
||||
disabled: true
|
||||
"""
|
||||
# Test the closure implementation
|
||||
|
||||
proc map(n: var openarray[int], fn: proc (x: int): int {.closure}) =
|
||||
for i in 0..n.len-1: n[i] = fn(n[i])
|
||||
|
||||
proc foldr(n: openarray[int], fn: proc (x, y: int): int {.closure}): int =
|
||||
for i in 0..n.len-1:
|
||||
result = fn(result, n[i])
|
||||
|
||||
var
|
||||
myData: array[0..4, int] = [0, 1, 2, 3, 4]
|
||||
|
||||
proc testA() =
|
||||
var p = 0
|
||||
map(myData, proc (x: int): int =
|
||||
result = x + 1 shl (proc (y: int): int =
|
||||
return y + p
|
||||
)(0)
|
||||
inc(p))
|
||||
|
||||
testA()
|
||||
for x in items(myData):
|
||||
write(stout, x)
|
||||
#OUT 2 4 6 8 10
|
||||
|
||||
|
||||
|
||||
|
||||
17
tests/run/tcnstseq.nim
Executable file
17
tests/run/tcnstseq.nim
Executable file
|
|
@ -0,0 +1,17 @@
|
|||
discard """
|
||||
file: "tcnstseq.nim"
|
||||
output: "AngelikaAnneAnnaAnkaAnja"
|
||||
"""
|
||||
# Test the new implicit conversion from sequences to arrays in a constant
|
||||
# context.
|
||||
|
||||
import strutils
|
||||
|
||||
const
|
||||
myWords = "Angelika Anne Anna Anka Anja".split()
|
||||
|
||||
for x in items(myWords):
|
||||
write(stdout, x) #OUT AngelikaAnneAnnaAnkaAnja
|
||||
|
||||
|
||||
|
||||
12
tests/run/tcnstseq2.nim
Normal file
12
tests/run/tcnstseq2.nim
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
discard """
|
||||
output: "AngelikaAnneAnnaAnkaAnja"
|
||||
"""
|
||||
|
||||
const
|
||||
myWords = @["Angelika", "Anne", "Anna", "Anka", "Anja"]
|
||||
|
||||
for i in 0 .. high(myWords):
|
||||
write(stdout, myWords[i]) #OUT AngelikaAnneAnnaAnkaAnja
|
||||
|
||||
|
||||
|
||||
7
tests/run/tcnstseq3.nim
Normal file
7
tests/run/tcnstseq3.nim
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
discard """
|
||||
output: "AngelikaAnneAnnaAnkaAnja"
|
||||
"""
|
||||
|
||||
for w in items(["Angelika", "Anne", "Anna", "Anka", "Anja"]):
|
||||
write(stdout, w) #OUT AngelikaAnneAnnaAnkaAnja
|
||||
|
||||
26
tests/run/tconstr2.nim
Executable file
26
tests/run/tconstr2.nim
Executable file
|
|
@ -0,0 +1,26 @@
|
|||
discard """
|
||||
file: "tconstr2.nim"
|
||||
output: "69"
|
||||
"""
|
||||
# Test array, record constructors
|
||||
|
||||
type
|
||||
TComplexRecord = tuple[
|
||||
s: string,
|
||||
x, y: int,
|
||||
z: float,
|
||||
chars: set[char]]
|
||||
|
||||
const
|
||||
things: array [0..1, TComplexRecord] = [
|
||||
(s: "hi", x: 69, y: 45, z: 0.0, chars: {'a', 'b', 'c'}),
|
||||
(s: "hi", x: 69, y: 45, z: 1.0, chars: {})]
|
||||
otherThings = [ # the same
|
||||
(s: "hi", x: 69, y: 45, z: 0.0, chars: {'a', 'b', 'c'}),
|
||||
(s: "hi", x: 69, y: 45, z: 1.0, chars: {'a'})]
|
||||
|
||||
write(stdout, things[0].x)
|
||||
#OUT 69
|
||||
|
||||
|
||||
|
||||
30
tests/run/tcontinuexc.nim
Executable file
30
tests/run/tcontinuexc.nim
Executable file
|
|
@ -0,0 +1,30 @@
|
|||
discard """
|
||||
file: "tcontinuexc.nim"
|
||||
outputsub: "ECcaught"
|
||||
exitcode: "1"
|
||||
"""
|
||||
type
|
||||
ESomething = object of E_Base
|
||||
ESomeOtherErr = object of E_Base
|
||||
|
||||
proc genErrors(s: string) =
|
||||
if s == "error!":
|
||||
raise newException(ESomething, "Test")
|
||||
else:
|
||||
raise newException(EsomeotherErr, "bla")
|
||||
|
||||
try:
|
||||
for i in 0..3:
|
||||
try:
|
||||
genErrors("error!")
|
||||
except ESomething:
|
||||
stdout.write("E")
|
||||
stdout.write("C")
|
||||
raise newException(EsomeotherErr, "bla")
|
||||
finally:
|
||||
echo "caught"
|
||||
|
||||
#OUT ECcaught
|
||||
|
||||
|
||||
|
||||
25
tests/run/tcopy.nim
Executable file
25
tests/run/tcopy.nim
Executable file
|
|
@ -0,0 +1,25 @@
|
|||
discard """
|
||||
file: "tcopy.nim"
|
||||
output: "TEMP=C:\\Programs\\xyz\\bin"
|
||||
"""
|
||||
# tests the substr proc
|
||||
|
||||
import
|
||||
strutils
|
||||
|
||||
proc main() =
|
||||
const
|
||||
example = r"TEMP=C:\Programs\xyz\bin"
|
||||
var
|
||||
a, b: string
|
||||
p: int
|
||||
p = find(example, "=")
|
||||
a = substr(example, 0, p-1)
|
||||
b = substr(example, p+1)
|
||||
writeln(stdout, a & '=' & b)
|
||||
#writeln(stdout, b)
|
||||
|
||||
main()
|
||||
#OUT TEMP=C:\Programs\xyz\bin
|
||||
|
||||
|
||||
14
tests/run/tcountup.nim
Executable file
14
tests/run/tcountup.nim
Executable file
|
|
@ -0,0 +1,14 @@
|
|||
discard """
|
||||
file: "tcountup.nim"
|
||||
output: "0123456789"
|
||||
"""
|
||||
|
||||
# Test new countup and unary <
|
||||
|
||||
for i in 0 .. < 10'i64:
|
||||
stdout.write(i)
|
||||
|
||||
#OUT 0123456789
|
||||
|
||||
|
||||
|
||||
38
tests/run/tcurrncy.nim
Executable file
38
tests/run/tcurrncy.nim
Executable file
|
|
@ -0,0 +1,38 @@
|
|||
discard """
|
||||
file: "tcurrncy.nim"
|
||||
output: "25"
|
||||
"""
|
||||
template Additive(typ: typeDesc): stmt =
|
||||
proc `+` *(x, y: typ): typ {.borrow.}
|
||||
proc `-` *(x, y: typ): typ {.borrow.}
|
||||
|
||||
# unary operators:
|
||||
proc `+` *(x: typ): typ {.borrow.}
|
||||
proc `-` *(x: typ): typ {.borrow.}
|
||||
|
||||
template Multiplicative(typ, base: typeDesc): stmt =
|
||||
proc `*` *(x: typ, y: base): typ {.borrow.}
|
||||
proc `*` *(x: base, y: typ): typ {.borrow.}
|
||||
proc `div` *(x: typ, y: base): typ {.borrow.}
|
||||
proc `mod` *(x: typ, y: base): typ {.borrow.}
|
||||
|
||||
template Comparable(typ: typeDesc): stmt =
|
||||
proc `<` * (x, y: typ): bool {.borrow.}
|
||||
proc `<=` * (x, y: typ): bool {.borrow.}
|
||||
proc `==` * (x, y: typ): bool {.borrow.}
|
||||
|
||||
template DefineCurrency(typ, base: expr): stmt =
|
||||
type
|
||||
typ* = distinct base
|
||||
Additive(typ)
|
||||
Multiplicative(typ, base)
|
||||
Comparable(typ)
|
||||
|
||||
proc `$` * (t: typ): string {.borrow.}
|
||||
|
||||
DefineCurrency(TDollar, int)
|
||||
DefineCurrency(TEuro, int)
|
||||
echo($( 12.TDollar + 13.TDollar )) #OUT 25
|
||||
|
||||
|
||||
|
||||
20
tests/run/temit.nim
Executable file
20
tests/run/temit.nim
Executable file
|
|
@ -0,0 +1,20 @@
|
|||
discard """
|
||||
file: "temit.nim"
|
||||
output: "509"
|
||||
"""
|
||||
# Test the new ``emit`` pragma:
|
||||
|
||||
{.emit: """
|
||||
static int cvariable = 420;
|
||||
|
||||
""".}
|
||||
|
||||
proc embedsC() {.noStackFrame.} =
|
||||
var nimrodVar = 89
|
||||
{.emit: """fprintf(stdout, "%d\n", cvariable + (int)`nimrodVar`);""".}
|
||||
|
||||
embedsC()
|
||||
|
||||
|
||||
|
||||
|
||||
22
tests/run/tenumhole.nim
Executable file
22
tests/run/tenumhole.nim
Executable file
|
|
@ -0,0 +1,22 @@
|
|||
discard """
|
||||
file: "tenumhole.nim"
|
||||
output: "my value A1my value Bconc2valueCabc4abc"
|
||||
"""
|
||||
|
||||
const
|
||||
strValB = "my value B"
|
||||
|
||||
type
|
||||
TMyEnum = enum
|
||||
valueA = (1, "my value A"),
|
||||
valueB = strValB & "conc",
|
||||
valueC,
|
||||
valueD = (4, "abc")
|
||||
|
||||
# trick the optimizer with a variable:
|
||||
var x = valueD
|
||||
echo valueA, ord(valueA), valueB, ord(valueB), valueC, valueD, ord(valueD), x
|
||||
|
||||
|
||||
|
||||
|
||||
31
tests/run/teventemitter.nim
Normal file
31
tests/run/teventemitter.nim
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
discard """
|
||||
output: "pie"
|
||||
"""
|
||||
|
||||
import tables, lists
|
||||
|
||||
type
|
||||
TEventArgs = object of TObject
|
||||
TEventEmitter = object of TObject
|
||||
events*: TTable[string, TDoublyLinkedList[proc(e: TEventArgs)]]
|
||||
|
||||
proc emit*(emitter: TEventEmitter, event: string, args: TEventArgs) =
|
||||
for func in nodes(emitter.events[event]):
|
||||
func.value(args) #call function with args.
|
||||
|
||||
proc on*(emitter: var TEventEmitter, event: string, func: proc(e: TEventArgs)) =
|
||||
if not hasKey(emitter.events, event):
|
||||
var list: TDoublyLinkedList[proc(e: TEventArgs)]
|
||||
add(emitter.events, event, list) #if not, add it.
|
||||
append(emitter.events.mget(event), func)
|
||||
|
||||
proc initEmitter(emitter: var TEventEmitter) =
|
||||
emitter.events = initTable[string, TDoublyLinkedList[proc(e: TEventArgs)]]()
|
||||
|
||||
var
|
||||
ee: TEventEmitter
|
||||
args: TEventArgs
|
||||
initEmitter(ee)
|
||||
ee.on("print", proc(e: TEventArgs) = echo("pie"))
|
||||
ee.emit("print", args)
|
||||
|
||||
48
tests/run/tevents.nim
Normal file
48
tests/run/tevents.nim
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
discard """
|
||||
file: "tevents.nim"
|
||||
output: '''HandlePrintEvent: Output -> Handled print event
|
||||
HandlePrintEvent2: Output -> printing for ME
|
||||
HandlePrintEvent2: Output -> printing for ME'''
|
||||
"""
|
||||
|
||||
import events
|
||||
|
||||
type
|
||||
TPrintEventArgs = object of TEventArgs
|
||||
user*: string
|
||||
|
||||
proc handleprintevent*(e: TEventArgs) =
|
||||
write(stdout, "HandlePrintEvent: Output -> Handled print event\n")
|
||||
|
||||
proc handleprintevent2*(e: TEventArgs) =
|
||||
var args: TPrintEventArgs = TPrintEventArgs(e)
|
||||
write(stdout, "HandlePrintEvent2: Output -> printing for " & args.user)
|
||||
|
||||
var ee = initEventEmitter()
|
||||
|
||||
var eventargs: TPrintEventArgs
|
||||
eventargs.user = "ME\n"
|
||||
|
||||
##method one test
|
||||
|
||||
ee.on("print", handleprintevent)
|
||||
ee.on("print", handleprintevent2)
|
||||
|
||||
ee.emit("print", eventargs)
|
||||
|
||||
##method two test
|
||||
|
||||
type
|
||||
TSomeObject = object of TObject
|
||||
PrintEvent: TEventHandler
|
||||
|
||||
var obj: TSomeObject
|
||||
obj.PrintEvent = initEventHandler("print")
|
||||
obj.PrintEvent.addHandler(handleprintevent2)
|
||||
|
||||
ee.emit(obj.PrintEvent, eventargs)
|
||||
|
||||
obj.PrintEvent.removeHandler(handleprintevent2)
|
||||
|
||||
ee.emit(obj.PrintEvent, eventargs)
|
||||
|
||||
17
tests/run/texcsub.nim
Executable file
17
tests/run/texcsub.nim
Executable file
|
|
@ -0,0 +1,17 @@
|
|||
discard """
|
||||
file: "texcsub.nim"
|
||||
output: "caught!"
|
||||
"""
|
||||
# Test inheritance for exception matching:
|
||||
|
||||
try:
|
||||
raise newException(EOS, "dummy message")
|
||||
except E_Base:
|
||||
echo "caught!"
|
||||
except:
|
||||
echo "wtf!?"
|
||||
|
||||
#OUT caught!
|
||||
|
||||
|
||||
|
||||
38
tests/run/texplicitgeneric1.nim
Executable file
38
tests/run/texplicitgeneric1.nim
Executable file
|
|
@ -0,0 +1,38 @@
|
|||
discard """
|
||||
file: "texplicitgeneric1.nim"
|
||||
output: "Key: 12 value: 12Key: 13 value: 13 Key: A value: 12 Key: B value: 13"
|
||||
"""
|
||||
# test explicit type instantiation
|
||||
|
||||
type
|
||||
TDict*[TKey, TValue] = object
|
||||
data: seq[tuple[k: TKey, v: TValue]]
|
||||
PDict*[TKey, #with `==`(a, b: TKey): bool
|
||||
# hash(a: TKey): int,
|
||||
TValue] = ref TDict[TKey, TValue]
|
||||
|
||||
proc newDict*[TKey, TValue](): PDict[TKey, TValue] =
|
||||
new(result)
|
||||
result.data = @[]
|
||||
|
||||
proc add*[TKey, TValue](d: PDict[TKey, TValue], k: TKey, v: TValue) =
|
||||
d.data.add((k, v))
|
||||
|
||||
iterator items*[Tkey, tValue](d: PDict[TKey, TValue]): tuple[k: TKey,
|
||||
v: TValue] =
|
||||
for k, v in items(d.data): yield (k, v)
|
||||
|
||||
var d = newDict[int, string]()
|
||||
d.add(12, "12")
|
||||
d.add(13, "13")
|
||||
for k, v in items(d):
|
||||
stdout.write("Key: ", $k, " value: ", v)
|
||||
|
||||
var c = newDict[char, string]()
|
||||
c.add('A', "12")
|
||||
c.add('B', "13")
|
||||
for k, v in items(c):
|
||||
stdout.write(" Key: ", $k, " value: ", v)
|
||||
|
||||
|
||||
|
||||
35
tests/run/texplicitgeneric2.nim
Executable file
35
tests/run/texplicitgeneric2.nim
Executable file
|
|
@ -0,0 +1,35 @@
|
|||
discard """
|
||||
output: "Key: 12 value: 12Key: 13 value: 13 Key: A value: 12 Key: B value: 13"
|
||||
disabled: true
|
||||
"""
|
||||
|
||||
# test explicit type instantiation
|
||||
|
||||
type
|
||||
TDict*[TKey, TValue] = object
|
||||
data: seq[tuple[k: TKey, v: TValue]]
|
||||
PDict*[TKey, TValue] = ref TDict[TKey, TValue]
|
||||
|
||||
proc newDict*[TKey, TValue](): PDict[TKey, TValue] =
|
||||
new(result)
|
||||
result.data = @[]
|
||||
|
||||
proc add*(d: PDict, k: TKey, v: TValue) =
|
||||
d.data.add((k, v))
|
||||
|
||||
|
||||
#iterator items*(d: PDict): tuple[k: TKey, v: TValue] =
|
||||
# for k, v in items(d.data): yield (k, v)
|
||||
|
||||
var d = newDict[int, string]()
|
||||
d.add(12, "12")
|
||||
d.add(13, "13")
|
||||
for k, v in items(d):
|
||||
stdout.write("Key: ", $k, " value: ", v)
|
||||
|
||||
var c = newDict[char, string]()
|
||||
c.add('A', "12")
|
||||
c.add('B', "13")
|
||||
for k, v in items(c):
|
||||
stdout.write(" Key: ", $k, " value: ", v)
|
||||
|
||||
46
tests/run/tfielditerator.nim
Executable file
46
tests/run/tfielditerator.nim
Executable file
|
|
@ -0,0 +1,46 @@
|
|||
discard """
|
||||
output: '''
|
||||
a char: true
|
||||
a char: false
|
||||
an int: 5
|
||||
an int: 6
|
||||
a string: abc
|
||||
false
|
||||
true
|
||||
true
|
||||
false
|
||||
true
|
||||
a: a
|
||||
b: b
|
||||
x: 5
|
||||
y: 6
|
||||
z: abc
|
||||
'''
|
||||
"""
|
||||
|
||||
type
|
||||
TMyTuple = tuple[a, b: char, x, y: int, z: string]
|
||||
|
||||
proc p(x: char) = echo "a char: ", x <= 'a'
|
||||
proc p(x: int) = echo "an int: ", x
|
||||
proc p(x: string) = echo "a string: ", x
|
||||
|
||||
var x: TMyTuple = ('a', 'b', 5, 6, "abc")
|
||||
var y: TMyTuple = ('A', 'b', 5, 9, "abc")
|
||||
|
||||
for f in fields(x):
|
||||
p f
|
||||
|
||||
for a, b in fields(x, y):
|
||||
echo a == b
|
||||
|
||||
for key, val in fieldPairs(x):
|
||||
echo key, ": ", val
|
||||
|
||||
assert x != y
|
||||
assert x == x
|
||||
assert(not (x < x))
|
||||
assert x <= x
|
||||
assert y < x
|
||||
assert y <= x
|
||||
|
||||
21
tests/run/tfinally.nim
Executable file
21
tests/run/tfinally.nim
Executable file
|
|
@ -0,0 +1,21 @@
|
|||
discard """
|
||||
file: "tfinally.nim"
|
||||
output: "came here 3"
|
||||
"""
|
||||
# Test return in try statement:
|
||||
|
||||
proc main: int =
|
||||
try:
|
||||
try:
|
||||
return 1
|
||||
finally:
|
||||
stdout.write("came ")
|
||||
return 2
|
||||
finally:
|
||||
stdout.write("here ")
|
||||
return 3
|
||||
|
||||
echo main() #OUT came here 3
|
||||
|
||||
|
||||
|
||||
27
tests/run/tfinally2.nim
Executable file
27
tests/run/tfinally2.nim
Executable file
|
|
@ -0,0 +1,27 @@
|
|||
discard """
|
||||
file: "tfinally2.nim"
|
||||
output: "ABCD"
|
||||
"""
|
||||
# Test break in try statement:
|
||||
|
||||
proc main: int =
|
||||
try:
|
||||
block AB:
|
||||
try:
|
||||
try:
|
||||
break AB
|
||||
finally:
|
||||
stdout.write("A")
|
||||
stdout.write("skipped")
|
||||
finally:
|
||||
block B:
|
||||
stdout.write("B")
|
||||
stdout.write("skipped")
|
||||
stdout.write("C")
|
||||
finally:
|
||||
stdout.writeln("D")
|
||||
|
||||
discard main() #OUT ABCD
|
||||
|
||||
|
||||
|
||||
18
tests/run/tfinally3.nim
Executable file
18
tests/run/tfinally3.nim
Executable file
|
|
@ -0,0 +1,18 @@
|
|||
discard """
|
||||
file: "tfinally3.nim"
|
||||
output: "false"
|
||||
"""
|
||||
# Test break in try statement:
|
||||
|
||||
proc main: bool =
|
||||
while true:
|
||||
try:
|
||||
return true
|
||||
finally:
|
||||
break
|
||||
return false
|
||||
|
||||
echo main() #OUT false
|
||||
|
||||
|
||||
|
||||
15
tests/run/tfloat1.nim
Executable file
15
tests/run/tfloat1.nim
Executable file
|
|
@ -0,0 +1,15 @@
|
|||
discard """
|
||||
file: "tfloat1.nim"
|
||||
outputsub: "Error: unhandled exception: FPU operation caused an overflow [EFloatOverflow]"
|
||||
exitcode: "1"
|
||||
"""
|
||||
# Test new floating point exceptions
|
||||
|
||||
{.floatChecks: on.}
|
||||
|
||||
var x = 0.8
|
||||
var y = 0.0
|
||||
|
||||
echo x / y #OUT Error: unhandled exception: FPU operation caused an overflow [EFloatOverflow]
|
||||
|
||||
|
||||
15
tests/run/tfloat2.nim
Executable file
15
tests/run/tfloat2.nim
Executable file
|
|
@ -0,0 +1,15 @@
|
|||
discard """
|
||||
file: "tfloat2.nim"
|
||||
outputsub: "Error: unhandled exception: FPU operation caused a NaN result [EFloatInvalidOp]"
|
||||
exitcode: "1"
|
||||
"""
|
||||
# Test new floating point exceptions
|
||||
|
||||
{.floatChecks: on.}
|
||||
|
||||
var x = 0.0
|
||||
var y = 0.0
|
||||
|
||||
echo x / y #OUT Error: unhandled exception: FPU operation caused a NaN result [EFloatInvalidOp]
|
||||
|
||||
|
||||
24
tests/run/tfloat3.nim
Executable file
24
tests/run/tfloat3.nim
Executable file
|
|
@ -0,0 +1,24 @@
|
|||
discard """
|
||||
file: "tfloat3.nim"
|
||||
output: "Nimrod 3.4368930843, 0.3299290698 C double: 3.4368930843, 0.3299290698"
|
||||
"""
|
||||
|
||||
import math, strutils
|
||||
|
||||
{.emit: """
|
||||
void printFloats(void) {
|
||||
double y = 1.234567890123456789;
|
||||
|
||||
printf("C double: %.10f, %.10f ", exp(y), cos(y));
|
||||
}
|
||||
""".}
|
||||
|
||||
proc c_printf(frmt: CString) {.importc: "printf", header: "<stdio.h>", varargs.}
|
||||
proc printFloats {.importc, nodecl.}
|
||||
|
||||
var x: float = 1.234567890123456789
|
||||
c_printf("Nimrod %.10f, %.10f ", exp(x), cos(x))
|
||||
printFloats()
|
||||
|
||||
|
||||
|
||||
12
tests/run/tformat.nim
Executable file
12
tests/run/tformat.nim
Executable file
|
|
@ -0,0 +1,12 @@
|
|||
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?
|
||||
|
||||
|
||||
11
tests/run/tfriends.nim
Normal file
11
tests/run/tfriends.nim
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
discard """
|
||||
output: "3"
|
||||
"""
|
||||
|
||||
# Tests that a generic instantiation from a different module may access
|
||||
# private object fields:
|
||||
|
||||
import mfriends
|
||||
|
||||
echo gen[int]()
|
||||
|
||||
24
tests/run/tgenericassign.nim
Executable file
24
tests/run/tgenericassign.nim
Executable file
|
|
@ -0,0 +1,24 @@
|
|||
discard """
|
||||
output: '''came here'''
|
||||
"""
|
||||
|
||||
type
|
||||
TAny* = object {.pure.}
|
||||
value*: pointer
|
||||
rawType: pointer
|
||||
|
||||
proc newAny(value, rawType: pointer): TAny =
|
||||
result.value = value
|
||||
result.rawType = rawType
|
||||
|
||||
var name: cstring = "example"
|
||||
|
||||
var ret: seq[tuple[name: string, a: TAny]] = @[]
|
||||
for i in 0..8000:
|
||||
var tup = ($name, newAny(nil, nil))
|
||||
assert(tup[0] == "example")
|
||||
ret.add(tup)
|
||||
assert(ret[ret.len()-1][0] == "example")
|
||||
|
||||
echo "came here"
|
||||
|
||||
16
tests/run/tgenericassigntuples.nim
Executable file
16
tests/run/tgenericassigntuples.nim
Executable file
|
|
@ -0,0 +1,16 @@
|
|||
discard """
|
||||
output: '''abc232'''
|
||||
"""
|
||||
|
||||
var t, s: tuple[x: string, c: int]
|
||||
|
||||
proc ugh: seq[tuple[x: string, c: int]] =
|
||||
result = @[("abc", 232)]
|
||||
|
||||
t = ugh()[0]
|
||||
s = t
|
||||
s = ugh()[0]
|
||||
|
||||
echo s[0], t[1]
|
||||
|
||||
|
||||
19
tests/run/tgenericprocvar.nim
Normal file
19
tests/run/tgenericprocvar.nim
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
discard """
|
||||
output: "0false"
|
||||
"""
|
||||
|
||||
# Test multiple generic instantiation of generic proc vars:
|
||||
|
||||
proc threadProcWrapper[TMsg]() =
|
||||
var x: TMsg
|
||||
stdout.write($x)
|
||||
|
||||
#var x = threadProcWrapper[int]
|
||||
#x()
|
||||
|
||||
#var y = threadProcWrapper[bool]
|
||||
#y()
|
||||
|
||||
threadProcWrapper[int]()
|
||||
threadProcWrapper[bool]()
|
||||
|
||||
53
tests/run/tgenerics1.nim
Executable file
53
tests/run/tgenerics1.nim
Executable file
|
|
@ -0,0 +1,53 @@
|
|||
discard """
|
||||
output: "100 0"
|
||||
"""
|
||||
|
||||
# A min-heap.
|
||||
type
|
||||
TNode[T] = tuple[priority: int, data: T]
|
||||
|
||||
TBinHeap[T] = object
|
||||
heap: seq[TNode[T]]
|
||||
last: int
|
||||
|
||||
PBinHeap[T] = ref TBinHeap[T]
|
||||
|
||||
proc newBinHeap*[T](heap: var PBinHeap[T], size: int) =
|
||||
new(heap)
|
||||
heap.last = 0
|
||||
newSeq(heap.heap, size)
|
||||
#newSeq(heap.seq, size)
|
||||
|
||||
proc parent(elem: int): int {.inline.} =
|
||||
return (elem-1) div 2
|
||||
|
||||
proc siftUp[T](heap: PBinHeap[T], elem: int) =
|
||||
var idx = elem
|
||||
while idx != 0:
|
||||
var p = parent(idx)
|
||||
if heap.heap[idx].priority < heap.heap[p].priority:
|
||||
swap(heap.heap[idx], heap.heap[p])
|
||||
idx = p
|
||||
else:
|
||||
break
|
||||
|
||||
proc add*[T](heap: PBinHeap[T], priority: int, data: T) =
|
||||
var node: TNode[T]
|
||||
node.priority = priority
|
||||
node.data = data
|
||||
heap.heap[heap.last] = node
|
||||
siftUp(heap, heap.last)
|
||||
inc(heap.last)
|
||||
|
||||
proc print*[T](heap: PBinHeap[T]) =
|
||||
for i in countup(0, heap.last):
|
||||
stdout.write($heap.heap[i].data, " ")
|
||||
|
||||
var
|
||||
heap: PBinHeap[int]
|
||||
|
||||
newBinHeap(heap, 256)
|
||||
add(heap, 1, 100)
|
||||
print(heap)
|
||||
|
||||
|
||||
12
tests/run/thintoff.nim
Executable file
12
tests/run/thintoff.nim
Executable file
|
|
@ -0,0 +1,12 @@
|
|||
discard """
|
||||
file: "thintoff.nim"
|
||||
output: "0"
|
||||
"""
|
||||
|
||||
{.hint[XDeclaredButNotUsed]: off.}
|
||||
var
|
||||
x: int
|
||||
|
||||
echo x #OUT 0
|
||||
|
||||
|
||||
19
tests/run/tidgen.nim
Normal file
19
tests/run/tidgen.nim
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
discard """
|
||||
output: "3 4"
|
||||
"""
|
||||
|
||||
import macros
|
||||
|
||||
# Test compile-time state in same module
|
||||
|
||||
var gid = 3
|
||||
|
||||
macro genId(invokation: expr): expr =
|
||||
result = newIntLitNode(gid)
|
||||
inc gid
|
||||
|
||||
proc Id1(): int = return genId()
|
||||
proc Id2(): int = return genId()
|
||||
|
||||
echo Id1(), " ", Id2()
|
||||
|
||||
12
tests/run/tinit.nim
Executable file
12
tests/run/tinit.nim
Executable file
|
|
@ -0,0 +1,12 @@
|
|||
discard """
|
||||
file: "tinit.nim"
|
||||
output: "Hello from module! Hello from main module!"
|
||||
"""
|
||||
# Test the new init section in modules
|
||||
|
||||
import minit
|
||||
|
||||
write(stdout, "Hello from main module!\n")
|
||||
#OUT Hello from module! Hello from main module!
|
||||
|
||||
|
||||
47
tests/run/tints.nim
Executable file
47
tests/run/tints.nim
Executable file
|
|
@ -0,0 +1,47 @@
|
|||
discard """
|
||||
file: "tints.nim"
|
||||
output: "Success"
|
||||
"""
|
||||
# Test the different integer operations
|
||||
|
||||
var testNumber = 0
|
||||
|
||||
template test(opr, a, b, c: expr): stmt =
|
||||
# test the expression at compile and runtime
|
||||
block:
|
||||
const constExpr = opr(a, b)
|
||||
when constExpr != c:
|
||||
{.error: "Test failed " & $constExpr & " " & $c.}
|
||||
inc(testNumber)
|
||||
#Echo("Test: " & $testNumber)
|
||||
var aa = a
|
||||
var bb = b
|
||||
var varExpr = opr(aa, bb)
|
||||
assert(varExpr == c)
|
||||
|
||||
test(`+`, 12'i8, -13'i16, -1'i16)
|
||||
test(`shl`, 0b11, 0b100, 0b110000)
|
||||
test(`shl`, 0b11'i32, 0b100'i64, 0b110000'i64)
|
||||
test(`shl`, 0b11'i32, 0b100'i32, 0b110000'i32)
|
||||
|
||||
test(`or`, 0xf0f0'i16, 0x0d0d'i16, 0xfdfd'i16)
|
||||
test(`and`, 0xf0f0'i16, 0xfdfd'i16, 0xf0f0'i16)
|
||||
|
||||
test(`shr`, 0xffffffffffffffff'i64, 0x4'i64, 0x0fffffffffffffff'i64)
|
||||
test(`shr`, 0xffff'i16, 0x4'i16, 0x0fff'i16)
|
||||
test(`shr`, 0xff'i8, 0x4'i8, 0x0f'i8)
|
||||
|
||||
test(`shr`, 0xffffffff'i64, 0x4'i64, 0x0fffffff'i64)
|
||||
test(`shr`, 0xffffffff'i32, 0x4'i32, 0x0fffffff'i32)
|
||||
|
||||
test(`shl`, 0xffffffffffffffff'i64, 0x4'i64, 0xfffffffffffffff0'i64)
|
||||
test(`shl`, 0xffff'i16, 0x4'i16, 0xfff0'i16)
|
||||
test(`shl`, 0xff'i8, 0x4'i8, 0xf0'i8)
|
||||
|
||||
test(`shl`, 0xffffffff'i64, 0x4'i64, 0xffffffff0'i64)
|
||||
test(`shl`, 0xffffffff'i32, 0x4'i32, 0xfffffff0'i32)
|
||||
|
||||
Echo("Success") #OUT Success
|
||||
|
||||
|
||||
|
||||
13
tests/run/tisopr.nim
Normal file
13
tests/run/tisopr.nim
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
discard """
|
||||
output: "true true false yes"
|
||||
"""
|
||||
|
||||
proc IsVoid[T](): string =
|
||||
when T is void:
|
||||
result = "yes"
|
||||
else:
|
||||
result = "no"
|
||||
|
||||
const x = int is int
|
||||
echo x, " ", float is float, " ", float is string, " ", IsVoid[void]()
|
||||
|
||||
22
tests/run/titer3.nim
Executable file
22
tests/run/titer3.nim
Executable file
|
|
@ -0,0 +1,22 @@
|
|||
discard """
|
||||
file: "titer3.nim"
|
||||
output: "1231"
|
||||
"""
|
||||
|
||||
iterator count1_3: int =
|
||||
yield 1
|
||||
yield 2
|
||||
yield 3
|
||||
|
||||
for x in count1_3():
|
||||
write(stdout, $x)
|
||||
|
||||
# yield inside an iterator, but not in a loop:
|
||||
iterator iter1(a: openArray[int]): int =
|
||||
yield a[0]
|
||||
|
||||
var x = [[1, 2, 3], [4, 5, 6]]
|
||||
for y in iter1(x[0]): write(stdout, $y)
|
||||
|
||||
#OUT 1231
|
||||
|
||||
16
tests/run/titer5.nim
Executable file
16
tests/run/titer5.nim
Executable file
|
|
@ -0,0 +1,16 @@
|
|||
discard """
|
||||
file: "titer5.nim"
|
||||
output: "abcxyz"
|
||||
"""
|
||||
# Test method call syntax for iterators:
|
||||
import strutils
|
||||
|
||||
const lines = """abc xyz"""
|
||||
|
||||
for x in lines.split():
|
||||
stdout.write(x)
|
||||
|
||||
#OUT abcxyz
|
||||
|
||||
|
||||
|
||||
37
tests/run/titer6.nim
Executable file
37
tests/run/titer6.nim
Executable file
|
|
@ -0,0 +1,37 @@
|
|||
discard """
|
||||
file: "titer6.nim"
|
||||
output: "000"
|
||||
"""
|
||||
# Test iterator with more than 1 yield statement
|
||||
|
||||
import strutils
|
||||
|
||||
iterator tokenize2(s: string, seps: set[char] = Whitespace): tuple[
|
||||
token: string, isSep: bool] =
|
||||
var i = 0
|
||||
while i < s.len:
|
||||
var j = i
|
||||
if s[j] in seps:
|
||||
while j < s.len and s[j] in seps: inc(j)
|
||||
if j > i:
|
||||
yield (substr(s, i, j-1), true)
|
||||
else:
|
||||
while j < s.len and s[j] notin seps: inc(j)
|
||||
if j > i:
|
||||
yield (substr(s, i, j-1), false)
|
||||
i = j
|
||||
|
||||
for word, isSep in tokenize2("ta da", whiteSpace):
|
||||
var titer2TestVar = 0
|
||||
stdout.write(titer2TestVar)
|
||||
|
||||
proc wordWrap2(s: string, maxLineWidth = 80,
|
||||
splitLongWords = true,
|
||||
seps: set[char] = whitespace,
|
||||
newLine = "\n"): string =
|
||||
result = ""
|
||||
for word, isSep in tokenize2(s, seps):
|
||||
var w = 0
|
||||
|
||||
|
||||
|
||||
16
tests/run/titervaropenarray.nim
Executable file
16
tests/run/titervaropenarray.nim
Executable file
|
|
@ -0,0 +1,16 @@
|
|||
discard """
|
||||
file: "titer2.nim"
|
||||
output: "123"
|
||||
"""
|
||||
# Try to break the transformation pass:
|
||||
iterator iterAndZero(a: var openArray[int]): int =
|
||||
for i in 0..len(a)-1:
|
||||
yield a[i]
|
||||
a[i] = 0
|
||||
|
||||
var x = [[1, 2, 3], [4, 5, 6]]
|
||||
for y in iterAndZero(x[0]): write(stdout, $y)
|
||||
#OUT 123
|
||||
|
||||
|
||||
|
||||
17
tests/run/tkoeniglookup.nim
Executable file
17
tests/run/tkoeniglookup.nim
Executable file
|
|
@ -0,0 +1,17 @@
|
|||
discard """
|
||||
output: '''x: 0 y: 0'''
|
||||
"""
|
||||
|
||||
proc ToString*[T](x: T): string = return $x
|
||||
|
||||
|
||||
type
|
||||
TMyObj = object
|
||||
x, y: int
|
||||
|
||||
proc `$`*(a: TMyObj): string =
|
||||
result = "x: " & $a.x & " y: " & $a.y
|
||||
|
||||
var a: TMyObj
|
||||
echo toString(a)
|
||||
|
||||
11
tests/run/tlenopenarray.nim
Executable file
11
tests/run/tlenopenarray.nim
Executable file
|
|
@ -0,0 +1,11 @@
|
|||
discard """
|
||||
file: "tlenopenarray.nim"
|
||||
output: "1"
|
||||
"""
|
||||
|
||||
# len(x) --> len([x]) --> match!
|
||||
echo len(1_000_000) #OUT 1
|
||||
|
||||
|
||||
|
||||
|
||||
66
tests/run/tlists.nim
Executable file
66
tests/run/tlists.nim
Executable file
|
|
@ -0,0 +1,66 @@
|
|||
discard """
|
||||
output: '''true'''
|
||||
"""
|
||||
|
||||
import lists
|
||||
|
||||
const
|
||||
data = [1, 2, 3, 4, 5, 6]
|
||||
|
||||
block SinglyLinkedListTest1:
|
||||
var L: TSinglyLinkedList[int]
|
||||
for d in items(data): L.prepend(d)
|
||||
assert($L == "[6, 5, 4, 3, 2, 1]")
|
||||
|
||||
assert(4 in L)
|
||||
|
||||
block SinglyLinkedListTest2:
|
||||
var L: TSinglyLinkedList[string]
|
||||
for d in items(data): L.prepend($d)
|
||||
assert($L == "[6, 5, 4, 3, 2, 1]")
|
||||
|
||||
assert("4" in L)
|
||||
|
||||
|
||||
block DoublyLinkedListTest1:
|
||||
var L: TDoublyLinkedList[int]
|
||||
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]")
|
||||
|
||||
assert(4 in L)
|
||||
|
||||
block SinglyLinkedRingTest1:
|
||||
var L: TSinglyLinkedRing[int]
|
||||
L.prepend(4)
|
||||
assert($L == "[4]")
|
||||
L.prepend(4)
|
||||
|
||||
assert($L == "[4, 4]")
|
||||
assert(4 in L)
|
||||
|
||||
|
||||
block DoublyLinkedRingTest1:
|
||||
var L: TDoublyLinkedRing[int]
|
||||
L.prepend(4)
|
||||
assert($L == "[4]")
|
||||
L.prepend(4)
|
||||
|
||||
assert($L == "[4, 4]")
|
||||
assert(4 in L)
|
||||
|
||||
L.append(3)
|
||||
L.append(5)
|
||||
assert($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)
|
||||
|
||||
|
||||
echo "true"
|
||||
|
||||
24
tests/run/tlowhigh.nim
Executable file
24
tests/run/tlowhigh.nim
Executable file
|
|
@ -0,0 +1,24 @@
|
|||
discard """
|
||||
file: "tlowhigh.nim"
|
||||
output: "10"
|
||||
"""
|
||||
# Test the magic low() and high() procs
|
||||
|
||||
type
|
||||
myEnum = enum e1, e2, e3, e4, e5
|
||||
|
||||
var
|
||||
a: array [myEnum, int]
|
||||
|
||||
for i in low(a) .. high(a):
|
||||
a[i] = 0
|
||||
|
||||
proc sum(a: openarray[int]): int =
|
||||
result = 0
|
||||
for i in low(a)..high(a):
|
||||
inc(result, a[i])
|
||||
|
||||
write(stdout, sum([1, 2, 3, 4]))
|
||||
#OUT 10
|
||||
|
||||
|
||||
27
tests/run/tmacro2.nim
Executable file
27
tests/run/tmacro2.nim
Executable file
|
|
@ -0,0 +1,27 @@
|
|||
discard """
|
||||
output: "ta-da Your value sir: 'HE!!!!o Wor!!d'"
|
||||
"""
|
||||
|
||||
import macros, strutils
|
||||
|
||||
proc testBlock(): string {.compileTime.} =
|
||||
block myBlock:
|
||||
while true:
|
||||
echo "inner block"
|
||||
break myBlock
|
||||
echo "outer block"
|
||||
result = "ta-da"
|
||||
|
||||
macro mac(n: expr): expr =
|
||||
expectKind(n, nnkCall)
|
||||
expectLen(n, 2)
|
||||
expectKind(n[1], nnkStrLit)
|
||||
var s: string = n[1].strVal
|
||||
s = s.replace("l", "!!")
|
||||
result = newStrLitNode("Your value sir: '$#'" % [s])
|
||||
|
||||
const s = testBlock()
|
||||
const t = mac("HEllo World")
|
||||
echo s, " ", t
|
||||
|
||||
|
||||
31
tests/run/tmacro3.nim
Executable file
31
tests/run/tmacro3.nim
Executable file
|
|
@ -0,0 +1,31 @@
|
|||
discard """
|
||||
output: ""
|
||||
"""
|
||||
|
||||
import macros
|
||||
|
||||
type
|
||||
TA = tuple[a: int]
|
||||
PA = ref TA
|
||||
|
||||
macro test*(a: stmt): stmt =
|
||||
var val: PA
|
||||
new(val)
|
||||
val.a = 4
|
||||
|
||||
test:
|
||||
"hi"
|
||||
|
||||
macro test2*(a: stmt): stmt =
|
||||
proc testproc(recurse: int) =
|
||||
echo "Thats weird"
|
||||
var o : PNimrodNode = nil
|
||||
echo " no its not!"
|
||||
o = newNimNode(nnkNone)
|
||||
if recurse > 0:
|
||||
testproc(recurse - 1)
|
||||
testproc(5)
|
||||
|
||||
test2:
|
||||
"hi"
|
||||
|
||||
19
tests/run/tmacro4.nim
Executable file
19
tests/run/tmacro4.nim
Executable file
|
|
@ -0,0 +1,19 @@
|
|||
discard """
|
||||
output: "after"
|
||||
"""
|
||||
|
||||
import
|
||||
macros, strutils
|
||||
|
||||
macro test_macro*(n: stmt): stmt =
|
||||
result = newNimNode(nnkStmtList)
|
||||
var ass : PNimrodNode = newNimNode(nnkAsgn)
|
||||
add(ass, newIdentNode("str"))
|
||||
add(ass, newStrLitNode("after"))
|
||||
add(result, ass)
|
||||
when isMainModule:
|
||||
var str: string = "before"
|
||||
test_macro(str):
|
||||
var i : integer = 123
|
||||
echo str
|
||||
|
||||
32
tests/run/tmacros1.nim
Executable file
32
tests/run/tmacros1.nim
Executable file
|
|
@ -0,0 +1,32 @@
|
|||
discard """
|
||||
output: "Got: 'nnkIntLit' hi"
|
||||
"""
|
||||
|
||||
import
|
||||
macros, strutils
|
||||
|
||||
macro outterMacro*(n: stmt): stmt =
|
||||
var j : string = "hi"
|
||||
proc innerProc(i: int): string =
|
||||
echo "Using arg ! " & n.repr
|
||||
result = "Got: '" & $n.kind & "' " & $j
|
||||
if n.kind != TNimrodNodeKind.nnkMacroStmt:
|
||||
error("Macro " & n[0].repr & " requires a block.")
|
||||
var callNode = n[0]
|
||||
expectKind(callNode, TNimrodNodeKind.nnkCall)
|
||||
if callNode.len != 2 or callNode[1].kind != TNimrodNodeKind.nnkIdent:
|
||||
error("Macro " & callNode.repr &
|
||||
" requires the ident passed as parameter (eg: " & callNode.repr &
|
||||
"(the_name_you_want)): statements.")
|
||||
result = newNimNode(TNimrodNodeKind.nnkStmtList)
|
||||
var ass : PNimrodNode = newNimNode(TNimrodNodeKind.nnkAsgn)
|
||||
ass.add(newIdentNode(callNode[1].ident))
|
||||
ass.add(newStrLitNode(innerProc(4)))
|
||||
result.add(ass)
|
||||
|
||||
var str: string
|
||||
outterMacro(str):
|
||||
"hellow"
|
||||
echo str
|
||||
|
||||
|
||||
48
tests/run/tmatrix.nim
Executable file
48
tests/run/tmatrix.nim
Executable file
|
|
@ -0,0 +1,48 @@
|
|||
discard """
|
||||
file: "tmatrix.nim"
|
||||
output: "111"
|
||||
"""
|
||||
# Test overloading of [] with multiple indices
|
||||
|
||||
type
|
||||
TMatrix* = object
|
||||
data: seq[float]
|
||||
fWidth, fHeight: int
|
||||
|
||||
template `|`(x, y: int): expr = y * m.fWidth + x
|
||||
|
||||
proc createMatrix*(width, height: int): TMatrix =
|
||||
result.fWidth = width
|
||||
result.fHeight = height
|
||||
newSeq(result.data, width*height)
|
||||
|
||||
proc width*(m: TMatrix): int {.inline.} = return m.fWidth
|
||||
proc height*(m: TMatrix): int {.inline.} = return m.fHeight
|
||||
|
||||
proc `[]`*(m: TMatrix, x, y: int): float {.inline.} =
|
||||
result = m.data[x|y]
|
||||
|
||||
proc `[]=`*(m: var TMatrix, x, y: int, val: float) {.inline.} =
|
||||
m.data[x|y] = val
|
||||
|
||||
proc `-|`*(m: TMatrix): TMatrix =
|
||||
## transposes a matrix
|
||||
result = createMatrix(m.height, m.width)
|
||||
for x in 0..m.width-1:
|
||||
for y in 0..m.height-1: result[y,x] = m[x,y]
|
||||
|
||||
#m.row(0, 2) # select row
|
||||
#m.col(0, 89) # select column
|
||||
|
||||
const
|
||||
w = 3
|
||||
h = 20
|
||||
|
||||
var m = createMatrix(w, h)
|
||||
for i in 0..w-1:
|
||||
m[i, i] = 1.0
|
||||
|
||||
for i in 0..w-1:
|
||||
stdout.write(m[i,i]) #OUT 111
|
||||
|
||||
|
||||
10
tests/run/tmethods1.nim
Executable file
10
tests/run/tmethods1.nim
Executable file
|
|
@ -0,0 +1,10 @@
|
|||
discard """
|
||||
output: "do nothing"
|
||||
"""
|
||||
|
||||
method somethin(obj: TObject) =
|
||||
echo "do nothing"
|
||||
|
||||
var o: TObject
|
||||
o.somethin()
|
||||
|
||||
29
tests/run/tmoditer.nim
Normal file
29
tests/run/tmoditer.nim
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
discard """
|
||||
output: "XXXXX01234"
|
||||
"""
|
||||
|
||||
iterator modPairs(a: var array[0..4,string]): tuple[key: int, val: var string] =
|
||||
for i in 0..a.high:
|
||||
yield (i, a[i])
|
||||
|
||||
iterator modItems*[T](a: var array[0..4,T]): var T =
|
||||
for i in 0..a.high:
|
||||
yield a[i]
|
||||
|
||||
var
|
||||
arr = ["a", "b", "c", "d", "e"]
|
||||
|
||||
for a in modItems(arr):
|
||||
a = "X"
|
||||
|
||||
for a in items(arr):
|
||||
stdout.write(a)
|
||||
|
||||
for i, a in modPairs(arr):
|
||||
a = $i
|
||||
|
||||
for a in items(arr):
|
||||
stdout.write(a)
|
||||
|
||||
echo ""
|
||||
|
||||
29
tests/run/tmultim1.nim
Executable file
29
tests/run/tmultim1.nim
Executable file
|
|
@ -0,0 +1,29 @@
|
|||
discard """
|
||||
file: "tmultim1.nim"
|
||||
output: "7"
|
||||
"""
|
||||
# Test multi methods
|
||||
|
||||
type
|
||||
TExpr = object
|
||||
TLiteral = object of TExpr
|
||||
x: int
|
||||
TPlusExpr = object of TExpr
|
||||
a, b: ref TExpr
|
||||
|
||||
method eval(e: ref TExpr): int = quit "to override!"
|
||||
method eval(e: ref TLiteral): int = return e.x
|
||||
method eval(e: ref TPlusExpr): int = return eval(e.a) + eval(e.b)
|
||||
|
||||
proc newLit(x: int): ref TLiteral =
|
||||
new(result)
|
||||
result.x = x
|
||||
|
||||
proc newPlus(a, b: ref TExpr): ref TPlusExpr =
|
||||
new(result)
|
||||
result.a = a
|
||||
result.b = b
|
||||
|
||||
echo eval(newPlus(newPlus(newLit(1), newLit(2)), newLit(4))) #OUT 7
|
||||
|
||||
|
||||
36
tests/run/tmultim2.nim
Executable file
36
tests/run/tmultim2.nim
Executable file
|
|
@ -0,0 +1,36 @@
|
|||
discard """
|
||||
file: "tmultim2.nim"
|
||||
output: "collide: unit, thing collide: unit, thing collide: thing, unit"
|
||||
"""
|
||||
# Test multi methods
|
||||
|
||||
type
|
||||
TThing = object
|
||||
TUnit = object of TThing
|
||||
x: int
|
||||
TParticle = object of TThing
|
||||
a, b: int
|
||||
|
||||
method collide(a, b: TThing) {.inline.} =
|
||||
quit "to override!"
|
||||
|
||||
method collide(a: TThing, b: TUnit) {.inline.} =
|
||||
write stdout, "collide: thing, unit "
|
||||
|
||||
method collide(a: TUnit, b: TThing) {.inline.} =
|
||||
write stdout, "collide: unit, thing "
|
||||
|
||||
proc test(a, b: TThing) {.inline.} =
|
||||
collide(a, b)
|
||||
|
||||
var
|
||||
a: TThing
|
||||
b, c: TUnit
|
||||
collide(b, c) # ambiguous (unit, thing) or (thing, unit)? -> prefer unit, thing!
|
||||
test(b, c)
|
||||
collide(a, b)
|
||||
#OUT collide: unit, thing collide: unit, thing collide: thing, unit
|
||||
|
||||
|
||||
|
||||
|
||||
20
tests/run/tmultim3.nim
Executable file
20
tests/run/tmultim3.nim
Executable file
|
|
@ -0,0 +1,20 @@
|
|||
discard """
|
||||
file: "tmultim3.nim"
|
||||
output: "Hi derived!"
|
||||
"""
|
||||
import mmultim3
|
||||
|
||||
type
|
||||
TBObj* = object of TObj
|
||||
|
||||
|
||||
method test123(a : ref TBObj) =
|
||||
echo("Hi derived!")
|
||||
|
||||
var a : ref TBObj
|
||||
new(a)
|
||||
myObj = a
|
||||
testMyObj()
|
||||
|
||||
|
||||
|
||||
45
tests/run/tmultim4.nim
Executable file
45
tests/run/tmultim4.nim
Executable file
|
|
@ -0,0 +1,45 @@
|
|||
discard """
|
||||
file: "tmultim4.nim"
|
||||
output: "hello"
|
||||
"""
|
||||
type
|
||||
Test = object of TObject
|
||||
|
||||
method doMethod(a: ref TObject) =
|
||||
quit "override"
|
||||
|
||||
method doMethod(a: ref Test) =
|
||||
echo "hello"
|
||||
|
||||
proc doProc(a: ref Test) =
|
||||
echo "hello"
|
||||
|
||||
proc newTest(): ref Test =
|
||||
new(result)
|
||||
|
||||
var s:ref Test = newTest()
|
||||
|
||||
|
||||
#doesn't work
|
||||
for z in 1..4:
|
||||
s.doMethod()
|
||||
break
|
||||
|
||||
#works
|
||||
#for z in 1..4:
|
||||
# s.doProc()
|
||||
# break
|
||||
|
||||
#works
|
||||
#while true:
|
||||
# s.doMethod()
|
||||
# break
|
||||
|
||||
#works
|
||||
#while true:
|
||||
# s.doProc()
|
||||
# break
|
||||
|
||||
|
||||
|
||||
|
||||
23
tests/run/tnamedenumfields.nim
Executable file
23
tests/run/tnamedenumfields.nim
Executable file
|
|
@ -0,0 +1,23 @@
|
|||
discard """
|
||||
file: "tnamedenumfields.nim"
|
||||
output: "my value A0my value Bconc1valueCabc3abc"
|
||||
"""
|
||||
|
||||
const
|
||||
strValB = "my value B"
|
||||
|
||||
type
|
||||
TMyEnum = enum
|
||||
valueA = (0, "my value A"),
|
||||
valueB = strValB & "conc",
|
||||
valueC,
|
||||
valueD = (3, "abc"),
|
||||
valueE = 4
|
||||
|
||||
# trick the optimizer with a variable:
|
||||
var x = valueD
|
||||
echo valueA, ord(valueA), valueB, ord(valueB), valueC, valueD, ord(valueD), x
|
||||
|
||||
|
||||
|
||||
|
||||
24
tests/run/tnestif.nim
Executable file
24
tests/run/tnestif.nim
Executable file
|
|
@ -0,0 +1,24 @@
|
|||
discard """
|
||||
file: "tnestif.nim"
|
||||
output: "i == 2"
|
||||
"""
|
||||
# test nested ifs
|
||||
|
||||
var
|
||||
x, y: int
|
||||
x = 2
|
||||
if x == 0:
|
||||
write(stdout, "i == 0")
|
||||
if y == 0:
|
||||
write(stdout, x)
|
||||
else:
|
||||
write(stdout, y)
|
||||
elif x == 1:
|
||||
write(stdout, "i == 1")
|
||||
elif x == 2:
|
||||
write(stdout, "i == 2")
|
||||
else:
|
||||
write(stdout, "looks like Python")
|
||||
#OUT i == 2
|
||||
|
||||
|
||||
16
tests/run/tnestprc.nim
Executable file
16
tests/run/tnestprc.nim
Executable file
|
|
@ -0,0 +1,16 @@
|
|||
discard """
|
||||
file: "tnestprc.nim"
|
||||
output: "10"
|
||||
"""
|
||||
# Test nested procs without closures
|
||||
|
||||
proc Add3(x: int): int =
|
||||
proc add(x, y: int): int {.noconv.} =
|
||||
result = x + y
|
||||
|
||||
result = add(x, 3)
|
||||
|
||||
echo Add3(7) #OUT 10
|
||||
|
||||
|
||||
|
||||
11
tests/run/tnewderef.nim
Executable file
11
tests/run/tnewderef.nim
Executable file
|
|
@ -0,0 +1,11 @@
|
|||
discard """
|
||||
output: 3
|
||||
|
||||
"""
|
||||
|
||||
var x: ref int
|
||||
new(x)
|
||||
x[] = 3
|
||||
|
||||
echo x[]
|
||||
|
||||
70
tests/run/tnodeadlocks.nim
Executable file
70
tests/run/tnodeadlocks.nim
Executable file
|
|
@ -0,0 +1,70 @@
|
|||
discard """
|
||||
outputsub: "101"
|
||||
cmd: "nimrod cc --hints:on --threads:on $# $#"
|
||||
"""
|
||||
|
||||
import os, locks
|
||||
|
||||
const
|
||||
noDeadlocks = defined(preventDeadlocks)
|
||||
|
||||
var
|
||||
thr: array [0..5, TThread[tuple[a, b: int]]]
|
||||
L, M, N: TLock
|
||||
|
||||
proc doNothing() = nil
|
||||
|
||||
proc threadFunc(interval: tuple[a, b: int]) {.thread.} =
|
||||
doNothing()
|
||||
for i in interval.a..interval.b:
|
||||
when nodeadlocks:
|
||||
case i mod 6
|
||||
of 0:
|
||||
Acquire(L) # lock stdout
|
||||
Acquire(M)
|
||||
Acquire(N)
|
||||
of 1:
|
||||
Acquire(L)
|
||||
Acquire(N) # lock stdout
|
||||
Acquire(M)
|
||||
of 2:
|
||||
Acquire(M)
|
||||
Acquire(L)
|
||||
Acquire(N)
|
||||
of 3:
|
||||
Acquire(M)
|
||||
Acquire(N)
|
||||
Acquire(L)
|
||||
of 4:
|
||||
Acquire(N)
|
||||
Acquire(M)
|
||||
Acquire(L)
|
||||
of 5:
|
||||
Acquire(N)
|
||||
Acquire(L)
|
||||
Acquire(M)
|
||||
else: assert false
|
||||
else:
|
||||
Acquire(L) # lock stdout
|
||||
Acquire(M)
|
||||
|
||||
echo i
|
||||
os.sleep(10)
|
||||
when nodeadlocks:
|
||||
echo "deadlocks prevented: ", deadlocksPrevented
|
||||
when nodeadlocks:
|
||||
Release(N)
|
||||
Release(M)
|
||||
Release(L)
|
||||
|
||||
InitLock(L)
|
||||
InitLock(M)
|
||||
InitLock(N)
|
||||
|
||||
proc main =
|
||||
for i in 0..high(thr):
|
||||
createThread(thr[i], threadFunc, (i*100, i*100+50))
|
||||
joinThreads(thr)
|
||||
|
||||
main()
|
||||
|
||||
26
tests/run/tofopr.nim
Executable file
26
tests/run/tofopr.nim
Executable file
|
|
@ -0,0 +1,26 @@
|
|||
discard """
|
||||
file: "tofopr.nim"
|
||||
output: "falsetrue"
|
||||
"""
|
||||
# Test is operator
|
||||
|
||||
type
|
||||
TMyType = object
|
||||
len: int
|
||||
data: string
|
||||
|
||||
TOtherType = object of TMyType
|
||||
|
||||
proc p(x: TMyType): bool =
|
||||
return x of TOtherType
|
||||
|
||||
var
|
||||
m: TMyType
|
||||
n: TOtherType
|
||||
|
||||
write(stdout, p(m))
|
||||
write(stdout, p(n))
|
||||
|
||||
#OUT falsetrue
|
||||
|
||||
|
||||
88
tests/run/toop1.nim
Executable file
88
tests/run/toop1.nim
Executable file
|
|
@ -0,0 +1,88 @@
|
|||
discard """
|
||||
file: "toop1.nim"
|
||||
output: "34[]o 5"
|
||||
"""
|
||||
# Test the stuff in the tutorial
|
||||
import macros
|
||||
|
||||
type
|
||||
TFigure = object of TObject # abstract base class:
|
||||
draw: proc (my: var TFigure) # concrete classes implement this proc
|
||||
|
||||
proc init(f: var TFigure) =
|
||||
f.draw = nil
|
||||
|
||||
type
|
||||
TCircle = object of TFigure
|
||||
radius: int
|
||||
|
||||
proc drawCircle(my: var TCircle) = stdout.writeln("o " & $my.radius)
|
||||
|
||||
proc init(my: var TCircle) =
|
||||
init(TFigure(my)) # call base constructor
|
||||
my.radius = 5
|
||||
my.draw = cast[proc (my: var TFigure)](drawCircle)
|
||||
|
||||
type
|
||||
TRectangle = object of TFigure
|
||||
width, height: int
|
||||
|
||||
proc drawRectangle(my: var TRectangle) = stdout.write("[]")
|
||||
|
||||
proc init(my: var TRectangle) =
|
||||
init(TFigure(my)) # call base constructor
|
||||
my.width = 5
|
||||
my.height = 10
|
||||
my.draw = cast[proc (my: var TFigure)](drawRectangle)
|
||||
|
||||
macro `!` (n: expr): stmt =
|
||||
result = newNimNode(nnkCall, n)
|
||||
var dot = newNimNode(nnkDotExpr, n)
|
||||
dot.add(n[1]) # obj
|
||||
if n[2].kind == nnkCall:
|
||||
# transforms ``obj!method(arg1, arg2, ...)`` to
|
||||
# ``(obj.method)(obj, arg1, arg2, ...)``
|
||||
dot.add(n[2][0]) # method
|
||||
result.add(dot)
|
||||
result.add(n[1]) # obj
|
||||
for i in 1..n[2].len-1:
|
||||
result.add(n[2][i])
|
||||
else:
|
||||
# transforms ``obj!method`` to
|
||||
# ``(obj.method)(obj)``
|
||||
dot.add(n[2]) # method
|
||||
result.add(dot)
|
||||
result.add(n[1]) # obj
|
||||
|
||||
type
|
||||
TSocket* = object of TObject
|
||||
FHost: int # cannot be accessed from the outside of the module
|
||||
# the `F` prefix is a convention to avoid clashes since
|
||||
# the accessors are named `host`
|
||||
|
||||
proc `host=`*(s: var TSocket, value: int) {.inline.} =
|
||||
## setter of hostAddr
|
||||
s.FHost = value
|
||||
|
||||
proc host*(s: TSocket): int {.inline.} =
|
||||
## getter of hostAddr
|
||||
return s.FHost
|
||||
|
||||
var
|
||||
s: TSocket
|
||||
s.host = 34 # same as `host=`(s, 34)
|
||||
stdout.write(s.host)
|
||||
|
||||
# now use these classes:
|
||||
var
|
||||
r: TRectangle
|
||||
c: TCircle
|
||||
init(r)
|
||||
init(c)
|
||||
r!draw
|
||||
c!draw()
|
||||
|
||||
#OUT 34[]o 5
|
||||
|
||||
|
||||
|
||||
17
tests/run/topenarrayrepr.nim
Executable file
17
tests/run/topenarrayrepr.nim
Executable file
|
|
@ -0,0 +1,17 @@
|
|||
discard """
|
||||
file: "topenarrayrepr.nim"
|
||||
output: "5 - [1]"
|
||||
"""
|
||||
type
|
||||
TProc = proc (n: int, m: openarray[int64])
|
||||
|
||||
proc Foo(x: int, P: TProc) =
|
||||
P(x, [ 1'i64 ])
|
||||
|
||||
proc Bar(n: int, m: openarray[int64]) =
|
||||
echo($n & " - " & repr(m))
|
||||
|
||||
Foo(5, Bar) #OUT 5 - [1]
|
||||
|
||||
|
||||
|
||||
18
tests/run/topenlen.nim
Executable file
18
tests/run/topenlen.nim
Executable file
|
|
@ -0,0 +1,18 @@
|
|||
discard """
|
||||
file: "topenlen.nim"
|
||||
output: "7"
|
||||
"""
|
||||
# Tests a special bug
|
||||
|
||||
proc choose(b: openArray[string]): string = return b[0]
|
||||
|
||||
proc p(a, b: openarray[string]): int =
|
||||
result = a.len + b.len - 1
|
||||
for j in 0 .. a.len: inc(result)
|
||||
discard choose(a)
|
||||
discard choose(b)
|
||||
|
||||
discard choose(["sh", "-c", $p([""], ["a"])])
|
||||
echo($p(["", "ha", "abc"], ["xyz"])) #OUT 7
|
||||
|
||||
|
||||
18
tests/run/toprprec.nim
Executable file
18
tests/run/toprprec.nim
Executable file
|
|
@ -0,0 +1,18 @@
|
|||
discard """
|
||||
file: "toprprec.nim"
|
||||
output: "done"
|
||||
"""
|
||||
# Test operator precedence:
|
||||
|
||||
assert 3+5*5-2 == 28- -26-28
|
||||
|
||||
proc `^-` (x, y: int): int =
|
||||
# now right-associative!
|
||||
result = x - y
|
||||
|
||||
assert 34 ^- 6 ^- 2 == 30
|
||||
assert 34 - 6 - 2 == 26
|
||||
echo "done"
|
||||
|
||||
|
||||
|
||||
21
tests/run/toverflw.nim
Executable file
21
tests/run/toverflw.nim
Executable file
|
|
@ -0,0 +1,21 @@
|
|||
discard """
|
||||
file: "toverflw.nim"
|
||||
output: "the computation overflowed"
|
||||
"""
|
||||
# Tests nimrod's ability to detect overflows
|
||||
|
||||
{.push overflowChecks: on.}
|
||||
|
||||
var
|
||||
a, b: int
|
||||
a = high(int)
|
||||
b = -2
|
||||
try:
|
||||
writeln(stdout, b - a)
|
||||
except EOverflow:
|
||||
writeln(stdout, "the computation overflowed")
|
||||
|
||||
{.pop.} # overflow check
|
||||
#OUT the computation overflowed
|
||||
|
||||
|
||||
10
tests/run/toverflw2.nim
Executable file
10
tests/run/toverflw2.nim
Executable file
|
|
@ -0,0 +1,10 @@
|
|||
discard """
|
||||
file: "toverflw2.nim"
|
||||
outputsub: "Error: unhandled exception: over- or underflow [EOverflow]"
|
||||
exitcode: "1"
|
||||
"""
|
||||
var a : int32 = 2147483647
|
||||
var b : int32 = 2147483647
|
||||
var c = a + b
|
||||
|
||||
|
||||
27
tests/run/toverl2.nim
Executable file
27
tests/run/toverl2.nim
Executable file
|
|
@ -0,0 +1,27 @@
|
|||
discard """
|
||||
file: "toverl2.nim"
|
||||
output: "true012"
|
||||
"""
|
||||
# Test new overloading resolution rules
|
||||
|
||||
import strutils
|
||||
|
||||
proc toverl2(x: int): string = return $x
|
||||
proc toverl2(x: bool): string = return $x
|
||||
|
||||
iterator toverl2(x: int): int =
|
||||
var res = 0
|
||||
while res < x:
|
||||
yield res
|
||||
inc(res)
|
||||
|
||||
var
|
||||
pp: proc (x: bool): string = toverl2
|
||||
stdout.write(pp(true))
|
||||
for x in toverl2(3):
|
||||
stdout.write(toverl2(x))
|
||||
stdout.write("\n")
|
||||
#OUT true012
|
||||
|
||||
|
||||
|
||||
16
tests/run/toverlop.nim
Executable file
16
tests/run/toverlop.nim
Executable file
|
|
@ -0,0 +1,16 @@
|
|||
discard """
|
||||
file: "toverlop.nim"
|
||||
output: "3"
|
||||
"""
|
||||
# Test operator overloading
|
||||
|
||||
proc `%` (a, b: int): int =
|
||||
return a mod b
|
||||
|
||||
var x, y: int
|
||||
x = 15
|
||||
y = 6
|
||||
write(stdout, x % y)
|
||||
#OUT 3
|
||||
|
||||
|
||||
13
tests/run/toverwr.nim
Executable file
13
tests/run/toverwr.nim
Executable file
|
|
@ -0,0 +1,13 @@
|
|||
discard """
|
||||
file: "toverwr.nim"
|
||||
output: "hello"
|
||||
"""
|
||||
# Test the overloading resolution in connection with a qualifier
|
||||
|
||||
proc write(t: TFile, s: string) =
|
||||
nil # a nop
|
||||
|
||||
system.write(stdout, "hello")
|
||||
#OUT hello
|
||||
|
||||
|
||||
23
tests/run/tovfint.nim
Executable file
23
tests/run/tovfint.nim
Executable file
|
|
@ -0,0 +1,23 @@
|
|||
discard """
|
||||
file: "tovfint.nim"
|
||||
output: "works!"
|
||||
"""
|
||||
# this tests the new overflow literals
|
||||
|
||||
var
|
||||
i: int
|
||||
i = int(0xffffffff)
|
||||
when defined(cpu64):
|
||||
if i == 4294967295:
|
||||
write(stdout, "works!\n")
|
||||
else:
|
||||
write(stdout, "broken!\n")
|
||||
else:
|
||||
if i == -1:
|
||||
write(stdout, "works!\n")
|
||||
else:
|
||||
write(stdout, "broken!\n")
|
||||
|
||||
#OUT works!
|
||||
|
||||
|
||||
1769
tests/run/tpegs.nim
Executable file
1769
tests/run/tpegs.nim
Executable file
File diff suppressed because it is too large
Load diff
35
tests/run/tpos.nim
Executable file
35
tests/run/tpos.nim
Executable file
|
|
@ -0,0 +1,35 @@
|
|||
discard """
|
||||
file: "tpos.nim"
|
||||
output: "6"
|
||||
"""
|
||||
# test this particular function
|
||||
|
||||
proc mypos(sub, s: string, start: int = 0): int =
|
||||
var
|
||||
i, j, M, N: int
|
||||
M = sub.len
|
||||
N = s.len
|
||||
i = start
|
||||
j = 0
|
||||
if i >= N:
|
||||
result = -1
|
||||
else:
|
||||
while True:
|
||||
if s[i] == sub[j]:
|
||||
Inc(i)
|
||||
Inc(j)
|
||||
else:
|
||||
i = i - j + 1
|
||||
j = 0
|
||||
if (j >= M) or (i >= N): break
|
||||
if j >= M:
|
||||
result = i - M
|
||||
else:
|
||||
result = -1
|
||||
|
||||
var sub = "hello"
|
||||
var s = "world hello"
|
||||
write(stdout, mypos(sub, s))
|
||||
#OUT 6
|
||||
|
||||
|
||||
11
tests/run/tprecedence.nim
Normal file
11
tests/run/tprecedence.nim
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
discard """
|
||||
output: "true"
|
||||
"""
|
||||
|
||||
# Test the new predence rules
|
||||
|
||||
proc `\+` (x, y: int): int = result = x + y
|
||||
proc `\*` (x, y: int): int = result = x * y
|
||||
|
||||
echo 5 \+ 1 \* 9 == 14
|
||||
|
||||
16
tests/run/tprintf.nim
Executable file
16
tests/run/tprintf.nim
Executable file
|
|
@ -0,0 +1,16 @@
|
|||
discard """
|
||||
file: "tprintf.nim"
|
||||
output: "Andreas Rumpf"
|
||||
"""
|
||||
# Test a printf proc
|
||||
|
||||
proc printf(file: TFile, args: openarray[string]) =
|
||||
var i = 0
|
||||
while i < args.len:
|
||||
write(file, args[i])
|
||||
inc(i)
|
||||
|
||||
printf(stdout, ["Andreas ", "Rumpf\n"])
|
||||
#OUT Andreas Rumpf
|
||||
|
||||
|
||||
32
tests/run/tprocvar.nim
Executable file
32
tests/run/tprocvar.nim
Executable file
|
|
@ -0,0 +1,32 @@
|
|||
discard """
|
||||
file: "tprocvar.nim"
|
||||
output: "papbpcpdpe7"
|
||||
"""
|
||||
# test variables of type proc
|
||||
|
||||
proc pa() {.cdecl.} = write(stdout, "pa")
|
||||
proc pb() {.cdecl.} = write(stdout, "pb")
|
||||
proc pc() {.cdecl.} = write(stdout, "pc")
|
||||
proc pd() {.cdecl.} = write(stdout, "pd")
|
||||
proc pe() {.cdecl.} = write(stdout, "pe")
|
||||
|
||||
const
|
||||
algos = [pa, pb, pc, pd, pe]
|
||||
|
||||
var
|
||||
x: proc (a, b: int): int {.cdecl.}
|
||||
|
||||
proc ha(c, d: int): int {.cdecl.} =
|
||||
echo(c + d)
|
||||
result = c + d
|
||||
|
||||
for a in items(algos):
|
||||
a()
|
||||
|
||||
x = ha
|
||||
discard x(3, 4)
|
||||
|
||||
#OUT papbpcpdpe7
|
||||
|
||||
|
||||
|
||||
25
tests/run/tquotewords.nim
Executable file
25
tests/run/tquotewords.nim
Executable file
|
|
@ -0,0 +1,25 @@
|
|||
discard """
|
||||
file: "tquotewords.nim"
|
||||
output: "thisanexample"
|
||||
"""
|
||||
# Test an idea I recently had:
|
||||
|
||||
import macros
|
||||
|
||||
macro quoteWords(n: expr): expr =
|
||||
result = newNimNode(nnkBracket, n)
|
||||
for i in 1..n.len-1:
|
||||
expectKind(n[i], nnkIdent)
|
||||
result.add(toStrLit(n[i]))
|
||||
|
||||
const
|
||||
myWordList = quoteWords(this, an, example)
|
||||
|
||||
var s = ""
|
||||
for w in items(myWordList):
|
||||
s.add(w)
|
||||
|
||||
echo s #OUT thisanexample
|
||||
|
||||
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue