diff options
author | Araq <rumpf_a@web.de> | 2014-01-13 02:10:03 +0100 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2014-01-13 02:10:03 +0100 |
commit | 20b5f31c03fb556ec0aa2428a40adbac004d8987 (patch) | |
tree | 58086941e7d6bb8f480ca1173a95722ada9435b2 /tests/seq | |
parent | 51ee524109cf7e3e86c676bc1676063a01bfd979 (diff) | |
download | Nim-20b5f31c03fb556ec0aa2428a40adbac004d8987.tar.gz |
new tester; all tests categorized
Diffstat (limited to 'tests/seq')
-rw-r--r-- | tests/seq/tseq2.nim | 11 | ||||
-rw-r--r-- | tests/seq/tseqcon.nim | 51 | ||||
-rw-r--r-- | tests/seq/tseqcon2.nim | 9 | ||||
-rw-r--r-- | tests/seq/tseqtuple.nim | 28 | ||||
-rw-r--r-- | tests/seq/tsequtils.nim | 55 | ||||
-rw-r--r-- | tests/seq/ttoseq.nim | 18 |
6 files changed, 172 insertions, 0 deletions
diff --git a/tests/seq/tseq2.nim b/tests/seq/tseq2.nim new file mode 100644 index 000000000..e1271964c --- /dev/null +++ b/tests/seq/tseq2.nim @@ -0,0 +1,11 @@ +proc `*` *(a, b: seq[int]): seq[int] = + # allocate a new sequence: + newSeq(result, len(a)) + # multiply two int sequences: + for i in 0..len(a)-1: result[i] = a[i] * b[i] + +when isMainModule: + # test the new ``*`` operator for sequences: + assert(@[1, 2, 3] * @[1, 2, 3] == @[1, 4, 9]) + + diff --git a/tests/seq/tseqcon.nim b/tests/seq/tseqcon.nim new file mode 100644 index 000000000..6e0a5b56d --- /dev/null +++ b/tests/seq/tseqcon.nim @@ -0,0 +1,51 @@ +discard """ + file: "tseqcon.nim" + output: "Hithere, what\'s your name?Hathere, what\'s your name?" +""" +# Test the add proc for sequences and strings + +const + nestedFixed = true + +type + TRec {.final.} = object + x, y: int + s: string + seq: seq[string] + TRecSeq = seq[TRec] + +proc test() = + var s, b: seq[string] + s = @[] + add(s, "Hi") + add(s, "there, ") + add(s, "what's your name?") + + b = s # deep copying here! + b[0][1] = 'a' + + for i in 0 .. len(s)-1: + write(stdout, s[i]) + for i in 0 .. len(b)-1: + write(stdout, b[i]) + + +when nestedFixed: + proc nested() = + var + s: seq[seq[string]] + for i in 0..10_000: # test if the garbage collector + # now works with sequences + s = @[ + @["A", "B", "C", "D"], + @["E", "F", "G", "H"], + @["I", "J", "K", "L"], + @["M", "N", "O", "P"]] + +test() +when nestedFixed: + nested() + +#OUT Hithere, what's your name?Hathere, what's your name? + + diff --git a/tests/seq/tseqcon2.nim b/tests/seq/tseqcon2.nim new file mode 100644 index 000000000..4f2763ffe --- /dev/null +++ b/tests/seq/tseqcon2.nim @@ -0,0 +1,9 @@ +import os + +proc rec_dir(dir: string): seq[string] = + result = @[] + for kind, path in walk_dir(dir): + if kind == pcDir: + add(result, rec_dir(path)) + else: + add(result, path) diff --git a/tests/seq/tseqtuple.nim b/tests/seq/tseqtuple.nim new file mode 100644 index 000000000..7ef92f7f1 --- /dev/null +++ b/tests/seq/tseqtuple.nim @@ -0,0 +1,28 @@ +discard """ + file: "tseqtuple.nim" + output: "fA13msg1falsefB14msg2truefC15msg3false" +""" + +type + TMsg = tuple[ + file: string, + line: int, + msg: string, + err: bool] + +var s: seq[TMsg] = @[] + +s.add(("fA", 13, "msg1", false)) +s.add(("fB", 14, "msg2", true)) +s.add(("fC", 15, "msg3", false)) + +for file, line, msg, err in items(s): + stdout.write(file) + stdout.write($line) + stdout.write(msg) + stdout.write($err) + +#OUT fA13msg1falsefB14msg2truefC15msg3false + + + diff --git a/tests/seq/tsequtils.nim b/tests/seq/tsequtils.nim new file mode 100644 index 000000000..7bc15ef9c --- /dev/null +++ b/tests/seq/tsequtils.nim @@ -0,0 +1,55 @@ +discard """ +file: "tsequtils.nim" +output: '''Zip: [{"Field0": 1, "Field1": 2}, {"Field0": 3, "Field1": 4}, {"Field0": 5, "Field1": 6}] +Filter Iterator: 3 +Filter Iterator: 5 +Filter Iterator: 7 +Filter: [3, 5, 7] +FilterIt: [1, 3, 7] +Concat: [1, 3, 5, 7, 2, 4, 6] +Distnct: [1, 2, 3, 4, 5, 7]''' + +""" + +import sequtils, marshal + +proc testFindWhere(item : int) : bool = + if item != 1: return true + +var seq1: seq[int] = @[] + +seq1.add(1) +seq1.add(3) +seq1.add(5) +seq1.add(7) + +var seq2: seq[int] = @[2, 4, 6] +var final = zip(seq1, seq2) + +echo "Zip: ", $$(final) + +#Test findWhere as a iterator + +for itms in filter(seq1, testFindWhere): + echo "Filter Iterator: ", $$(itms) + + +#Test findWhere as a proc + +var fullseq: seq[int] = filter(seq1, testFindWhere) + +echo "Filter: ", $$(fullseq) + +#Test findIt as a template + +var finditval: seq[int] = filterIt(seq1, it!=5) + +echo "FilterIt: ", $$(finditval) + +var concatseq = concat(seq1,seq2) +echo "Concat: ", $$(concatseq) + +var seq3 = @[1,2,3,4,5,5,5,7] +var discntseq = distnct(seq3) +echo "Distnct: ", $$(discntseq) + diff --git a/tests/seq/ttoseq.nim b/tests/seq/ttoseq.nim new file mode 100644 index 000000000..34cc4824b --- /dev/null +++ b/tests/seq/ttoseq.nim @@ -0,0 +1,18 @@ +discard """ + output: "2345623456" +""" + +import sequtils + +for x in toSeq(countup(2, 6)): + stdout.write(x) +for x in items(toSeq(countup(2, 6))): + stdout.write(x) + +import strutils + +var y: type("a b c".split) +y = "xzy" + + + |