added tests

This commit is contained in:
Andreas Rumpf
2010-05-28 23:33:29 +02:00
parent 6c20509121
commit b34b1dbc6c
7 changed files with 172 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
type
TDict[TK, TV] = object
k: TK
v: TV
PDict[TK, TV] = ref TDict[TK, TV]
proc destroyDict[TK, TV](a : PDict[TK, TV]) =
return
proc newDict[TK, TV](a: TK, b: TV): PDict[TK, TV] =
new(result, destroyDict)
discard newDict("a", "b")

View File

@@ -0,0 +1,7 @@
#
import times, os
var start = getStartMilsecs()
os.sleep(1000)
echo getStartMilsecs() - start #OUT 1000

View File

@@ -0,0 +1,32 @@
# 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)

View File

@@ -0,0 +1,30 @@
# 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)

View File

@@ -0,0 +1,17 @@
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")
while True:
try:
genErrors("errssor!")
except ESomething:
echo("Error happened")
except:
raise

View File

@@ -0,0 +1,24 @@
#BUG
type
TAnyKind = enum
nkInt,
nkFloat,
nkString
TAny = object
case kind: TAnyKind
of nkInt: intVal: int
of nkFloat: floatVal: float
of nkString: strVal: string
var s: TAny
s.kind = nkString
s.strVal = "test"
var nr: TAny
nr.kind = nkint
nr.intVal = 78
# s = nr # works
nr = s # fails!

View File

@@ -0,0 +1,46 @@
#BUG
type
TAnyKind = enum
nkInt,
nkFloat,
nkString
PAny = ref TAny
TAny = object
case kind: TAnyKind
of nkInt: intVal: int
of nkFloat: floatVal: float
of nkString: strVal: string
TStack* = object
list*: seq[TAny]
proc newStack(): TStack =
result.list = @[]
proc push(Stack: var TStack, item: TAny) =
var nSeq: seq[TAny] = @[item]
for i in items(Stack.list):
nSeq.add(i)
Stack.list = nSeq
proc pop(Stack: var TStack): TAny =
result = Stack.list[0]
Stack.list.delete(0)
var stack = newStack()
var s: TAny
s.kind = nkString
s.strVal = "test"
stack.push(s)
var nr: TAny
nr.kind = nkint
nr.intVal = 78
stack.push(nr)
var t = stack.pop()