new tester; all tests categorized

This commit is contained in:
Araq 2014-01-13 02:10:03 +01:00
commit 20b5f31c03
481 changed files with 794 additions and 2506 deletions

View file

@ -0,0 +1,20 @@
type
TAnimal=object {.inheritable.}
PAnimal=ref TAnimal
TDog=object of TAnimal
PDog=ref TDog
TCat=object of TAnimal
PCat=ref TCat
TAnimalArray=array[0..2,PAnimal]
proc newDog():PDog = new(result)
proc newCat():PCat = new(result)
proc test(a:openarray[PAnimal])=
echo("dummy")
#test(newDog(),newCat()) #does not work
var myarray:TAnimalArray=[newDog(),newCat(),newDog()] #does not work
#var myarray2:TAnimalArray=[newDog(),newDog(),newDog()] #does not work either

View file

@ -0,0 +1,8 @@
discard """
line: 6
errormsg: "type mismatch: got (int literal(1)) but expected 'bool'"
"""
if 1:
echo "wtf?"

View file

@ -0,0 +1,7 @@
import db_sqlite
var db: TDbConn
Exec(db, sql"create table blabla()")

View file

@ -0,0 +1,11 @@
discard """
file: "tnocontains.nim"
line: 10
errormsg: "type mismatch: got (string, string)"
"""
# shouldn't compile since it doesn't do what you think it does without
# importing strutils:
let x = "abcdef".contains("abc")
echo x

View file

@ -0,0 +1,11 @@
type
PNode = ref TNode
TNode = tuple # comment
self: PNode # comment
a, b: int # comment
var node: PNode
new(node)
node.self = node

View file

@ -0,0 +1,10 @@
type Node = tuple[left: ref Node]
proc traverse(root: ref Node) =
if root.left != nil: traverse(root.left)
type A = tuple[B: ptr A]
proc C(D: ptr A) = C(D.B)

View file

@ -0,0 +1,26 @@
discard """
errormsg: "internal error: cannot generate C type for: PA"
"""
# Test recursive type descriptions
# (mainly for the C code generator)
type
PA = ref TA
TA = array [0..2, PA]
PRec = ref TRec
TRec {.final.} = object
a, b: TA
P1 = ref T1
PB = ref TB
TB = array [0..3, P1]
T1 = array [0..6, PB]
var
x: PA
new(x)
#ERROR_MSG internal error: cannot generate C type for: PA

23
tests/typerel/trefs.nim Normal file
View file

@ -0,0 +1,23 @@
discard """
file: "trefs.nim"
line: 20
errormsg: "type mismatch"
"""
# test for ref types (including refs to procs)
type
TProc = proc (a, b: int): int {.stdcall.}
proc foo(c, d: int): int {.stdcall.} =
return 0
proc wrongfoo(c, e: int): int {.inline.} =
return 0
var p: TProc
p = foo
write(stdout, "success!")
p = wrongfoo #ERROR_MSG type mismatch

View file

@ -0,0 +1,33 @@
discard """
msg: "instantiated for string\ninstantiated for int\ninstantiated for bool"
output: "int\nseq[string]\nA\nB\n100\ntrue"
"""
import typetraits
proc plus(a, b): auto = a + b
proc makePair(a, b): auto = (first: a, second: b)
proc `+`(a, b: string): seq[string] = @[a, b]
var i = plus(10, 20)
var s = plus("A", "B")
var p = makePair("key", 100)
static: assert p[0].type is string
echo i.type.name
echo s.type.name
proc inst(a): auto =
static: echo "instantiated for ", a.type.name
result = a
echo inst("A")
echo inst("B")
echo inst(100)
echo inst(true)
# XXX: [string, tyGenericParam] is cached instead of [string, string]
# echo inst[string, string]("C")

View file

@ -0,0 +1,28 @@
type
TFoo = object
data: array[0..100, int]
TSecond = distinct TFoo
proc `[]` (self: var TFoo, x: int): var int =
return self.data[x]
proc `[]=` (self: var TFoo, x, y: int) =
# only `[]` returning a 'var T' seems to not work for now :-/
self.data[x] = y
proc second(self: var TFoo): var TSecond =
return TSecond(self)
proc `[]`(self: var TSecond, x: int): var int =
return TFoo(self).data[2*x]
var f: TFoo
for i in 0..f.data.high: f[i] = 2 * i
echo f.second[1]
#echo `second[]`(f,1)
# this is the only way I could use it, but not what I expected

16
tests/typerel/ttuple1.nim Normal file
View file

@ -0,0 +1,16 @@
const romanNumbers = [
("M", 1000), ("D", 500), ("C", 100),
("L", 50), ("X", 10), ("V", 5), ("I", 1) ]
var c = 0
for key, val in items(romanNumbers):
inc(c)
stdout.write(key & "=" & $val)
if c < romanNumbers.len: stdout.write(", ") else: echo""
#echo""
proc PrintBiTuple(t: tuple[k: string, v: int]): int =
stdout.write(t.k & "=" & $t.v & ", ")
return 0

View file

@ -0,0 +1,6 @@
discard """
errormsg: "internal error: invalid kind for last(tyEmpty)"
"""
var q = false
discard (if q: {} else: {})

View file

@ -0,0 +1,55 @@
discard """
file: "ttypenoval.nim"
line: 38
errormsg: "type mismatch: got (typedesc[int]) but expected 'int'"
"""
# 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] < heap.heap[p]:
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 = int
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):
echo($heap.heap[i])
var
heap: PBinHeap[int]
newBinHeap(heap, 256)
add(heap, 1, 100)
print(heap)

View file

@ -0,0 +1,11 @@
discard """
errormsg: "value expected, but got a type"
line: 7
disabled: true
"""
proc crashAndBurn() =
var stuff = seq[tuple[title, body: string]]
crashAndBurn()

33
tests/typerel/tvoid.nim Normal file
View file

@ -0,0 +1,33 @@
discard """
output: "he, no return type;abc a string"
"""
proc ReturnT[T](x: T): T =
when T is void:
echo "he, no return type;"
else:
result = x & " a string"
proc nothing(x, y: void): void =
echo "ha"
proc callProc[T](p: proc (x: T) {.nimcall.}, x: T) =
when T is void:
p()
else:
p(x)
proc intProc(x: int) =
echo x
proc emptyProc() =
echo "empty"
callProc[int](intProc, 12)
callProc[void](emptyProc)
ReturnT[void]()
echo ReturnT[string]("abc")
nothing()

View file

@ -0,0 +1,15 @@
type
TMyObj = TYourObj
TYourObj = object of TObject
x, y: int
proc init: TYourObj =
result.x = 0
result.y = -1
proc f(x: var TYourObj) =
nil
var m: TMyObj = init()
f(m)

View file

@ -0,0 +1,8 @@
discard """
file: "typredef.nim"
line: 7
errormsg: "illegal recursion in type \'Uint8\'"
"""
type
Uint8 = Uint8 #ERROR_MSG illegal recursion in type 'Uint8'