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

14
tests/iter/tcountup.nim Normal file
View 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

44
tests/iter/titer.nim Normal file
View file

@ -0,0 +1,44 @@
# Test the new iterators
iterator xrange(fromm, to: int, step = 1): int =
var a = fromm
while a <= to:
yield a
inc(a, step)
iterator interval[T](a, b: T): T =
var x = a
while x <= b:
yield x
inc(x)
#
#iterator lines(filename: string): (line: string) =
# var
# f: tTextfile
# shouldClose = open(f, filename)
# if shouldClose:
# setSpace(line, 256)
# while readTextLine(f, line):
# yield line
# finally:
# if shouldClose: close(f)
#
for i in xrange(0, 5):
for k in xrange(1, 7):
write(stdout, "test")
for j in interval(45, 45):
write(stdout, "test2!")
write(stdout, "test3?")
for x in items(["hi", "what's", "your", "name"]):
echo(x)
const
stringArray = ["hi", "what's", "your", "name"]
for i in 0..len(stringArray)-1:
echo(stringArray[i])

51
tests/iter/titer2.nim Normal file
View file

@ -0,0 +1,51 @@
discard """
output: '''true'''
cmd: "nimrod cc --gc:none --hints:on $# $#"
"""
import hashes
type
TSlotEnum = enum seEmpty, seFilled, seDeleted
TKeyValuePair[A, B] = tuple[slot: TSlotEnum, key: A, val: B]
TKeyValuePairSeq[A, B] = seq[TKeyValuePair[A, B]]
TTable* {.final.}[A, B] = object
data: TKeyValuePairSeq[A, B]
counter: int
iterator mycountup(a, b: int): int =
var res = a
while res <= b:
yield res
inc(res)
when true:
iterator pairs*[A, B](t: TTable[A, B]): tuple[key: A, val: B] =
## iterates over any (key, value) pair in the table `t`.
for h in mycountup(0, high(t.data)):
var k = t.data[h].key
if t.data[h].slot == seFilled: yield (k, t.data[h].val)
else:
iterator pairs*(t: TTable[int, string]): tuple[key: int, val: string] =
## iterates over any (key, value) pair in the table `t`.
for h in mycountup(0, high(t.data)):
var k = t.data[h].key
if t.data[h].slot == seFilled: yield (k, t.data[h].val)
proc initTable*[A, B](initialSize=64): TTable[A, B] =
## creates a new hash table that is empty. `initialSize` needs to be
## a power of two.
result.counter = 0
newSeq(result.data, initialSize)
block Test1:
# generic cache does not instantiate the same iterator[types] twice. This
# means we have only one instantiation of 'h'. However, this is the same for
# a non-generic iterator!
var t = initTable[int, string]()
for k, v in t.pairs: nil
for k, v in t.pairs: nil
echo "true"

22
tests/iter/titer3.nim Normal file
View 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

10
tests/iter/titer4.nim Normal file
View file

@ -0,0 +1,10 @@
discard """
file: "titer4.nim"
line: 7
errormsg: "iterator within for loop context expected"
"""
# implicit items/pairs, but not if we have 3 for loop vars:
for x, y, z in {'a'..'z'}: #ERROR_MSG iterator within for loop context expected
nil

16
tests/iter/titer5.nim Normal file
View 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/iter/titer6.nim Normal file
View 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

57
tests/iter/titer7.nim Normal file
View file

@ -0,0 +1,57 @@
discard """
output: '''--- evens
2
4
6
8
--- squares
1
4
9
16
25
36
49
64
81
--- squares of evens, only
4
16
36
64'''
"""
iterator `/`[T](sequence: seq[T],
filter: proc(e:T):bool {.closure.}) : T =
for element in sequence:
if (filter(element)):
yield element
iterator `>>`[I,O](sequence: seq[I],
map: proc(e:I):O {.closure.}) : O =
for element in sequence:
yield map(element)
iterator `/>>`[I,O](sequence: seq[I],
filtermap:tuple[
f:proc(e:I):bool {.closure.},
m:proc(e:I):O {.closure.}]) : O =
for element in sequence:
if (filtermap.f(element)):
yield filtermap.m(element)
proc isEven(x:int): bool {.closure.} = result =
(x and 1) == 0
proc square(x:int): int {.closure.} = result =
x * x
let list = @[1,2,3,4,5,6,7,8,9]
echo ("--- evens")
for item in list / isEven : echo(item)
echo ("--- squares")
for item in list >> square : echo(item)
echo ("--- squares of evens, only")
# next line doesn't compile. Generic types are not inferred
for item in list />> (isEven, square) : echo(item)

117
tests/iter/titer8.nim Normal file
View file

@ -0,0 +1,117 @@
discard """
output: '''tada
1
2
3
ta da1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
0
1
2
a1: A
a2: A
a1: B
a2: B
a1: C
a2: C
a1: D'''
"""
# Test first class iterator:
import strutils
iterator tokenize2(s: string, seps: set[char] = Whitespace): tuple[
token: string, isSep: bool] {.closure.} =
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
iterator count3(): int {.closure.} =
yield 1
yield 2
yield 3
for word, isSep in tokenize2("ta da", whiteSpace):
if not isSep:
stdout.write(word)
echo ""
proc inProc() =
for c in count3():
echo c
for word, isSep in tokenize2("ta da", whiteSpace):
stdout.write(word)
for c in count3():
for d in count3():
echo c, " ", d
inProc()
iterator count0(): int {.closure.} =
# note: doesn't require anything in its closure (except 'state')
yield 0
iterator count2(): int {.closure.} =
# note: requires 'x' in its closure
var x = 1
yield x
inc x
yield x
# a first class iterator has the type 'proc {.closure.}', but maybe
# it shouldn't:
proc invoke(iter: iterator(): int {.closure.}) =
for x in iter(): echo x
invoke(count0)
invoke(count2)
# simple tasking:
type
TTask = iterator (ticker: int)
iterator a1(ticker: int) {.closure.} =
echo "a1: A"
yield
echo "a1: B"
yield
echo "a1: C"
yield
echo "a1: D"
iterator a2(ticker: int) {.closure.} =
echo "a2: A"
yield
echo "a2: B"
yield
echo "a2: C"
proc runTasks(t: varargs[TTask]) =
var ticker = 0
while true:
let x = t[ticker mod t.len]
if finished(x): break
x(ticker)
inc ticker
runTasks(a1, a2)

20
tests/iter/titer9.nim Normal file
View file

@ -0,0 +1,20 @@
discard """
output: '''5
14
0'''
"""
iterator count[T](x: T, skip: bool): int {.closure.} =
if skip: return x+10
else: yield x+1
if skip: return x+10
else: yield x+2
proc takeProc[T](x: iterator (x: T, skip: bool): int) =
echo x(4, false)
echo x(4, true)
echo x(4, false)
takeProc(count[int])

View file

@ -0,0 +1,13 @@
iterator xrange(fromm, to: int, step = 1): tuple[x, y: int] =
var a = fromm
while a <= to:
yield (a, a+1)
inc(a, step)
for a, b in xrange(3, 7):
echo a, " ", b
for tup in xrange(3, 7):
echo tup

21
tests/iter/titerovl.nim Normal file
View file

@ -0,0 +1,21 @@
discard """
output: '''9
1
2
3
'''
"""
# Test the new overloading rules for iterators:
# test that iterator 'p' is preferred:
proc p(): seq[int] = @[1, 2, 3]
iterator p(): int = yield 9
for x in p(): echo x
# test that 'q' works in this position:
proc q(): seq[int] = @[1, 2, 3]
for x in q(): echo x

View file

@ -0,0 +1,9 @@
discard """
output: '''2
3
4'''
"""
var t1 = @["1", "2", "3", "4"]
for t in t1[1..3]:
echo t

View 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

29
tests/iter/tmoditer.nim Normal file
View 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 ""

14
tests/iter/treciter.nim Normal file
View file

@ -0,0 +1,14 @@
discard """
file: "treciter.nim"
line: 9
errormsg: "recursive dependency: \'myrec\'"
"""
# Test that an error message occurs for a recursive iterator
iterator myrec(n: int): int =
for x in myrec(n-1): #ERROR_MSG recursive dependency: 'myrec'
yield x
for x in myrec(10): echo x

13
tests/iter/twrongiter.nim Normal file
View file

@ -0,0 +1,13 @@
discard """
line: 12
errormsg: "type mismatch"
"""
proc first(it: iterator(): int): seq[int] =
return @[]
iterator primes(): int =
yield 1
for i in first(primes):
break