diff options
author | rumpf_a@web.de <> | 2010-02-21 19:42:36 +0100 |
---|---|---|
committer | rumpf_a@web.de <> | 2010-02-21 19:42:36 +0100 |
commit | d913fdb2800d83680e413cd8a5f07b7f85deac6e (patch) | |
tree | 09a284861adf96520059f211ba8bae1a76294a9c /tests/accept/run | |
parent | 6bc16904edd3738ab97573b9eeb3a6a7cce9574c (diff) | |
download | Nim-d913fdb2800d83680e413cd8a5f07b7f85deac6e.tar.gz |
start of tests refactoring; sqlite3 new wrapper fixes
Diffstat (limited to 'tests/accept/run')
60 files changed, 1409 insertions, 0 deletions
diff --git a/tests/accept/run/tack.nim b/tests/accept/run/tack.nim new file mode 100644 index 000000000..59535e547 --- /dev/null +++ b/tests/accept/run/tack.nim @@ -0,0 +1,15 @@ +# 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 diff --git a/tests/accept/run/tambsym2.nim b/tests/accept/run/tambsym2.nim new file mode 100644 index 000000000..9178182aa --- /dev/null +++ b/tests/accept/run/tambsym2.nim @@ -0,0 +1,18 @@ +# 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 diff --git a/tests/accept/run/tambsys.nim b/tests/accept/run/tambsys.nim new file mode 100644 index 000000000..bb2622824 --- /dev/null +++ b/tests/accept/run/tambsys.nim @@ -0,0 +1,7 @@ +# Test ambiguous symbols + +import mambsys1, mambsys2 + +var + v: mambsys1.TExport +mambsys2.foo(3) #OUT diff --git a/tests/accept/run/tarray.nim b/tests/accept/run/tarray.nim new file mode 100644 index 000000000..252cbd991 --- /dev/null +++ b/tests/accept/run/tarray.nim @@ -0,0 +1,27 @@ +# 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 diff --git a/tests/accept/run/tarray2.nim b/tests/accept/run/tarray2.nim new file mode 100644 index 000000000..eb0b75692 --- /dev/null +++ b/tests/accept/run/tarray2.nim @@ -0,0 +1,18 @@ +# 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] diff --git a/tests/accept/run/tassert.nim b/tests/accept/run/tassert.nim new file mode 100644 index 000000000..9fd18e9bd --- /dev/null +++ b/tests/accept/run/tassert.nim @@ -0,0 +1,16 @@ +# 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 diff --git a/tests/accept/run/tbind1.nim b/tests/accept/run/tbind1.nim new file mode 100644 index 000000000..e7eed3e4f --- /dev/null +++ b/tests/accept/run/tbind1.nim @@ -0,0 +1,14 @@ +# 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(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 + diff --git a/tests/accept/run/tbind3.nim b/tests/accept/run/tbind3.nim new file mode 100644 index 000000000..f7fb4865b --- /dev/null +++ b/tests/accept/run/tbind3.nim @@ -0,0 +1,5 @@ +# Module B +import mbind3 + +echo genId() #OUT 1 + diff --git a/tests/accept/run/tbintre2.nim b/tests/accept/run/tbintre2.nim new file mode 100644 index 000000000..dedc87705 --- /dev/null +++ b/tests/accept/run/tbintre2.nim @@ -0,0 +1,25 @@ +# Same test, but check module boundaries + +import tbintree + +var + root: PBinaryTree[string] + x = newNode("hallo") +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 halloworld99110223 + diff --git a/tests/accept/run/tbintree.nim b/tests/accept/run/tbintree.nim new file mode 100644 index 000000000..89126eaa5 --- /dev/null +++ b/tests/accept/run/tbintree.nim @@ -0,0 +1,101 @@ +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("hallo") + 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 halloworld99110223 diff --git a/tests/accept/run/tcasestm.nim b/tests/accept/run/tcasestm.nim new file mode 100644 index 000000000..277b0bab1 --- /dev/null +++ b/tests/accept/run/tcasestm.nim @@ -0,0 +1,32 @@ +# 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 + diff --git a/tests/accept/run/tclosure.nim b/tests/accept/run/tclosure.nim new file mode 100644 index 000000000..761e9a8f3 --- /dev/null +++ b/tests/accept/run/tclosure.nim @@ -0,0 +1,26 @@ +# 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 + + diff --git a/tests/accept/run/tcnstseq.nim b/tests/accept/run/tcnstseq.nim new file mode 100644 index 000000000..4f389bb3b --- /dev/null +++ b/tests/accept/run/tcnstseq.nim @@ -0,0 +1,11 @@ +# 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 + diff --git a/tests/accept/run/tconstr2.nim b/tests/accept/run/tconstr2.nim new file mode 100644 index 000000000..7687a416c --- /dev/null +++ b/tests/accept/run/tconstr2.nim @@ -0,0 +1,20 @@ +# 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 + diff --git a/tests/accept/run/tcopy.nim b/tests/accept/run/tcopy.nim new file mode 100644 index 000000000..6cb2ec14c --- /dev/null +++ b/tests/accept/run/tcopy.nim @@ -0,0 +1,19 @@ +# tests the copy proc + +import + strutils + +proc main() = + const + example = r"TEMP=C:\Programs\xyz\bin" + var + a, b: string + p: int + p = find(example, "=") + a = copy(example, 0, p-1) + b = copy(example, p+1) + writeln(stdout, a & '=' & b) + #writeln(stdout, b) + +main() +#OUT TEMP=C:\Programs\xyz\bin diff --git a/tests/accept/run/tcurrncy.nim b/tests/accept/run/tcurrncy.nim new file mode 100644 index 000000000..fa08d620b --- /dev/null +++ b/tests/accept/run/tcurrncy.nim @@ -0,0 +1,32 @@ +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 + diff --git a/tests/accept/run/tfinally.nim b/tests/accept/run/tfinally.nim new file mode 100644 index 000000000..df6397125 --- /dev/null +++ b/tests/accept/run/tfinally.nim @@ -0,0 +1,10 @@ +# Test return in try statement: + +proc main: int = + try: + return 1 + finally: + echo "came here" + +discard main() #OUT came here + diff --git a/tests/accept/run/tfloat1.nim b/tests/accept/run/tfloat1.nim new file mode 100644 index 000000000..89911dd61 --- /dev/null +++ b/tests/accept/run/tfloat1.nim @@ -0,0 +1,8 @@ +# 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] diff --git a/tests/accept/run/tfloat2.nim b/tests/accept/run/tfloat2.nim new file mode 100644 index 000000000..92421d446 --- /dev/null +++ b/tests/accept/run/tfloat2.nim @@ -0,0 +1,8 @@ +# 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] diff --git a/tests/accept/run/tformat.nim b/tests/accept/run/tformat.nim new file mode 100644 index 000000000..aba35504b --- /dev/null +++ b/tests/accept/run/tformat.nim @@ -0,0 +1,6 @@ +# 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? diff --git a/tests/accept/run/thintoff.nim b/tests/accept/run/thintoff.nim new file mode 100644 index 000000000..7aff283d6 --- /dev/null +++ b/tests/accept/run/thintoff.nim @@ -0,0 +1,6 @@ + +{.hint[XDeclaredButNotUsed]: off.} +var + x: int + +echo x #OUT 0 diff --git a/tests/accept/run/tinit.nim b/tests/accept/run/tinit.nim new file mode 100644 index 000000000..85475ce94 --- /dev/null +++ b/tests/accept/run/tinit.nim @@ -0,0 +1,6 @@ +# Test the new init section in modules + +import minit + +write(stdout, "Hallo from main module!\n") +#OUT Hallo from module! Hallo from main module! diff --git a/tests/accept/run/tints.nim b/tests/accept/run/tints.nim new file mode 100644 index 000000000..f2b52c134 --- /dev/null +++ b/tests/accept/run/tints.nim @@ -0,0 +1,41 @@ +# 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 + diff --git a/tests/accept/run/tisopr.nim b/tests/accept/run/tisopr.nim new file mode 100644 index 000000000..d52859b09 --- /dev/null +++ b/tests/accept/run/tisopr.nim @@ -0,0 +1,20 @@ +# Test is operator + +type + TMyType = object + len: int + data: string + + TOtherType = object of TMyType + +proc p(x: TMyType): bool = + return x is TOtherType + +var + m: TMyType + n: TOtherType + +write(stdout, p(m)) +write(stdout, p(n)) + +#OUT falsetrue diff --git a/tests/accept/run/titer2.nim b/tests/accept/run/titer2.nim new file mode 100644 index 000000000..b9cdb53fe --- /dev/null +++ b/tests/accept/run/titer2.nim @@ -0,0 +1,10 @@ +# 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 + diff --git a/tests/accept/run/titer3.nim b/tests/accept/run/titer3.nim new file mode 100644 index 000000000..d0e121445 --- /dev/null +++ b/tests/accept/run/titer3.nim @@ -0,0 +1,17 @@ + +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 \ No newline at end of file diff --git a/tests/accept/run/titer5.nim b/tests/accept/run/titer5.nim new file mode 100644 index 000000000..1ac37ba66 --- /dev/null +++ b/tests/accept/run/titer5.nim @@ -0,0 +1,10 @@ +# Test method call syntax for iterators: +import strutils + +const lines = """abc xyz""" + +for x in lines.split(): + stdout.write(x) + +#OUT abcxyz + diff --git a/tests/accept/run/tlowhigh.nim b/tests/accept/run/tlowhigh.nim new file mode 100644 index 000000000..79f5c5b95 --- /dev/null +++ b/tests/accept/run/tlowhigh.nim @@ -0,0 +1,18 @@ +# 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 diff --git a/tests/accept/run/tmatrix.nim b/tests/accept/run/tmatrix.nim new file mode 100644 index 000000000..a162d0f10 --- /dev/null +++ b/tests/accept/run/tmatrix.nim @@ -0,0 +1,60 @@ +# 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, a, b, c, d: int): TMatrix = + result = createMatrix(b-a+1, d-c+1) + for x in a..b: + for y in c..d: + result[x-a, y-c] = m[x, y] + +proc `[$ .. $, $ .. $]=`*(m: var TMatrix, a, b, c, d: int, val: float) = + for x in a..b: + for y in c..d: + m[x, y] = val + +proc `[$ .. $, $ .. $]=`*(m: var TMatrix, a, b, c, d: int, val: TMatrix) = + assert val.width == b-a+1 + assert val.height == d-c+1 + for x in a..b: + for y in c..d: + m[x, y] = val[x-a, y-c] + +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 diff --git a/tests/accept/run/tmultim1.nim b/tests/accept/run/tmultim1.nim new file mode 100644 index 000000000..5d807e4c9 --- /dev/null +++ b/tests/accept/run/tmultim1.nim @@ -0,0 +1,23 @@ +# 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 diff --git a/tests/accept/run/tmultim2.nim b/tests/accept/run/tmultim2.nim new file mode 100644 index 000000000..bf3b5fd6e --- /dev/null +++ b/tests/accept/run/tmultim2.nim @@ -0,0 +1,30 @@ +# 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 + + diff --git a/tests/accept/run/tnestif.nim b/tests/accept/run/tnestif.nim new file mode 100644 index 000000000..558fe8d07 --- /dev/null +++ b/tests/accept/run/tnestif.nim @@ -0,0 +1,18 @@ +# 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 diff --git a/tests/accept/run/tnestprc.nim b/tests/accept/run/tnestprc.nim new file mode 100644 index 000000000..b7326e032 --- /dev/null +++ b/tests/accept/run/tnestprc.nim @@ -0,0 +1,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 + diff --git a/tests/accept/run/toop1.nim b/tests/accept/run/toop1.nim new file mode 100644 index 000000000..8bae002e7 --- /dev/null +++ b/tests/accept/run/toop1.nim @@ -0,0 +1,82 @@ +# 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 = 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 = 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 + diff --git a/tests/accept/run/topenarrayrepr.nim b/tests/accept/run/topenarrayrepr.nim new file mode 100644 index 000000000..7e976540f --- /dev/null +++ b/tests/accept/run/topenarrayrepr.nim @@ -0,0 +1,11 @@ +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] + diff --git a/tests/accept/run/topenlen.nim b/tests/accept/run/topenlen.nim new file mode 100644 index 000000000..b9d7fbc2d --- /dev/null +++ b/tests/accept/run/topenlen.nim @@ -0,0 +1,12 @@ +# 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 diff --git a/tests/accept/run/toverflw.nim b/tests/accept/run/toverflw.nim new file mode 100644 index 000000000..c8f194e68 --- /dev/null +++ b/tests/accept/run/toverflw.nim @@ -0,0 +1,15 @@ +# Tests emc'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 diff --git a/tests/accept/run/toverl2.nim b/tests/accept/run/toverl2.nim new file mode 100644 index 000000000..2d1225c6f --- /dev/null +++ b/tests/accept/run/toverl2.nim @@ -0,0 +1,21 @@ +# 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 + diff --git a/tests/accept/run/toverlop.nim b/tests/accept/run/toverlop.nim new file mode 100644 index 000000000..f11275644 --- /dev/null +++ b/tests/accept/run/toverlop.nim @@ -0,0 +1,10 @@ +# 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 diff --git a/tests/accept/run/toverwr.nim b/tests/accept/run/toverwr.nim new file mode 100644 index 000000000..f2b42df15 --- /dev/null +++ b/tests/accept/run/toverwr.nim @@ -0,0 +1,7 @@ +# Test the overloading resolution in connection with a qualifier + +proc write(t: TFile, s: string) = + nil # a nop + +system.write(stdout, "hallo") +#OUT hallo diff --git a/tests/accept/run/tovfint.nim b/tests/accept/run/tovfint.nim new file mode 100644 index 000000000..91eda8d0b --- /dev/null +++ b/tests/accept/run/tovfint.nim @@ -0,0 +1,17 @@ +# 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! diff --git a/tests/accept/run/tpos.nim b/tests/accept/run/tpos.nim new file mode 100644 index 000000000..114d39c05 --- /dev/null +++ b/tests/accept/run/tpos.nim @@ -0,0 +1,29 @@ +# 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 = "hallo" +var s = "world hallo" +write(stdout, mypos(sub, s)) +#OUT 6 diff --git a/tests/accept/run/tprintf.nim b/tests/accept/run/tprintf.nim new file mode 100644 index 000000000..14687a937 --- /dev/null +++ b/tests/accept/run/tprintf.nim @@ -0,0 +1,10 @@ +# 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 diff --git a/tests/accept/run/tprocvar.nim b/tests/accept/run/tprocvar.nim new file mode 100644 index 000000000..f51543dfa --- /dev/null +++ b/tests/accept/run/tprocvar.nim @@ -0,0 +1,26 @@ +# 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 + diff --git a/tests/accept/run/tquotewords.nim b/tests/accept/run/tquotewords.nim new file mode 100644 index 000000000..462293b40 --- /dev/null +++ b/tests/accept/run/tquotewords.nim @@ -0,0 +1,19 @@ +# 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 + diff --git a/tests/accept/run/tregex.nim b/tests/accept/run/tregex.nim new file mode 100644 index 000000000..d9d22d603 --- /dev/null +++ b/tests/accept/run/tregex.nim @@ -0,0 +1,19 @@ +# Test the new regular expression module +# which is based on the PCRE library + +import + regexprs + +if "keyA = valueA" =~ r"\s*(\w+)\s*\=\s*(\w+)": + write(stdout, "key: ", matches[1]) +elif "# comment!" =~ r"\s*(\#.*)": + echo("comment: ", matches[1]) +else: + echo("Bug!") + +if "Username".match("[A-Za-z]+"): + echo("Yes!") +else: + echo("Bug!") + +#OUT key: keyAYes! diff --git a/tests/accept/run/treguse.nim b/tests/accept/run/treguse.nim new file mode 100644 index 000000000..dc805fc70 --- /dev/null +++ b/tests/accept/run/treguse.nim @@ -0,0 +1,21 @@ +# Test the register usage of the virtual machine and +# the blocks in var statements + +proc main(a, b: int) = + var x = 0 + write(stdout, x) + if x == 0: + var y = 55 + write(stdout, y) + write(stdout, "this should be the case") + var input = "<no input>" + if input == "Andreas": + write(stdout, "wow") + else: + write(stdout, "hugh") + else: + var z = 66 + write(stdout, z) # "bug!") + +main(45, 1000) +#OUT 055this should be the casehugh diff --git a/tests/accept/run/tromans.nim b/tests/accept/run/tromans.nim new file mode 100644 index 000000000..12deca1ea --- /dev/null +++ b/tests/accept/run/tromans.nim @@ -0,0 +1,65 @@ +import + strutils + +## Convert an integer to a Roman numeral +# See http://en.wikipedia.org/wiki/Roman_numerals for reference + +proc raiseInvalidValue(msg: string) {.noreturn.} = + # Yes, we really need a shorthand for this code... + var e: ref EInvalidValue + new(e) + e.msg = msg + raise e + +# I should use a class, perhaps. +# --> No. Why introduce additional state into such a simple and nice +# interface? State is evil. :D + +proc RomanToDecimal(romanVal: string): int = + result = 0 + var prevVal = 0 + for i in countdown(romanVal.len - 1, 0): + var val = 0 + case romanVal[i] + of 'I', 'i': val = 1 + of 'V', 'v': val = 5 + of 'X', 'x': val = 10 + of 'L', 'l': val = 50 + of 'C', 'c': val = 100 + of 'D', 'd': val = 500 + of 'M', 'm': val = 1000 + else: raiseInvalidValue("Incorrect character in roman numeral! (" & + $romanVal[i] & ")") + if val >= prevVal: + inc(result, val) + else: + dec(result, val) + prevVal = val + +proc DecimalToRoman(decValParam: int): string = + # Apparently numbers cannot be above 4000 + # Well, they can be (using overbar or parenthesis notation) + # but I see little interest (beside coding challenge) in coding them as + # we rarely use huge Roman numeral. + const romanComposites = [ + ("M", 1000), ("CM", 900), + ("D", 500), ("CD", 400), ("C", 100), + ("XC", 90), ("L", 50), ("XL", 40), ("X", 10), ("IX", 9), + ("V", 5), ("IV", 4), ("I", 1)] + if decValParam < 1 or decValParam > 3999: + raiseInvalidValue("number not representable") + result = "" + var decVal = decValParam + for key, val in items(romanComposites): + while decVal >= val: + dec(decVal, val) + result.add(key) + +for i in 1..100: + if RomanToDecimal(DecimalToRoman(i)) != i: quit "BUG" + +for i in items([1238, 1777, 3830, 2401, 379, 33, 940, 3973]): + if RomanToDecimal(DecimalToRoman(i)) != i: quit "BUG" + +echo "success" #OUT success + diff --git a/tests/accept/run/tseqcon.nim b/tests/accept/run/tseqcon.nim new file mode 100644 index 000000000..935da86b5 --- /dev/null +++ b/tests/accept/run/tseqcon.nim @@ -0,0 +1,45 @@ +# 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/accept/run/tsets.nim b/tests/accept/run/tsets.nim new file mode 100644 index 000000000..08ab3e54b --- /dev/null +++ b/tests/accept/run/tsets.nim @@ -0,0 +1,58 @@ +# Test the handling of sets + +import + strutils + +proc testSets(s: var set[char]) = + s = {'A', 'B', 'C', 'E'..'G'} + {'Z'} + s + +# test sets if the first element is different from 0: +type + TAZ = range['a'..'z'] + TAZset = set[TAZ] + + TTokType* = enum + tkInvalid, tkEof, + tkSymbol, + tkAddr, tkAnd, tkAs, tkAsm, tkBlock, tkBreak, tkCase, tkCast, tkConst, + tkContinue, tkConverter, tkDiscard, tkDiv, tkElif, tkElse, tkEnd, tkEnum, + tkExcept, tkException, tkFinally, tkFor, tkFrom, tkGeneric, tkIf, tkImplies, + tkImport, tkIn, tkInclude, tkIs, tkIsnot, tkIterator, tkLambda, tkMacro, + tkMethod, tkMod, tkNil, tkNot, tkNotin, tkObject, tkOf, tkOr, tkOut, tkProc, + tkPtr, tkRaise, tkRecord, tkRef, tkReturn, tkShl, tkShr, tkTemplate, tkTry, + tkType, tkVar, tkWhen, tkWhere, tkWhile, tkWith, tkWithout, tkXor, tkYield, + tkIntLit, tkInt8Lit, tkInt16Lit, tkInt32Lit, tkInt64Lit, tkFloatLit, + tkFloat32Lit, tkFloat64Lit, tkStrLit, tkRStrLit, tkTripleStrLit, tkCharLit, + tkRCharLit, tkParLe, tkParRi, tkBracketLe, tkBracketRi, tkCurlyLe, + tkCurlyRi, tkBracketDotLe, tkBracketDotRi, + tkCurlyDotLe, tkCurlyDotRi, + tkParDotLe, tkParDotRi, + tkComma, tkSemiColon, tkColon, tkEquals, tkDot, tkDotDot, tkHat, tkOpr, + tkComment, tkAccent, tkInd, tkSad, tkDed, + tkSpaces, tkInfixOpr, tkPrefixOpr, tkPostfixOpr + TTokTypeRange = range[tkSymbol..tkDed] + TTokTypes* = set[TTokTypeRange] + +const + toktypes: TTokTypes = {TTokTypeRange(tkSymbol)..pred(tkIntLit), + tkStrLit..tkTripleStrLit} + +var + s: set[char] + a: TAZset +s = {'0'..'9'} +testSets(s) +if 'F' in s: write(stdout, "Ha ein F ist in s!\n") +else: write(stdout, "BUG: F ist nicht in s!\n") +a = {} #{'a'..'z'} +for x in low(TAZ) .. high(TAZ): + incl(a, x) + if x in a: nil + else: write(stdout, "BUG: something not in a!\n") + +for x in low(TTokTypeRange) .. high(TTokTypeRange): + if x in tokTypes: + nil + #writeln(stdout, "the token '$1' is in the set" % repr(x)) + +#OUT Ha ein F ist in s! diff --git a/tests/accept/run/tsidee2.nim b/tests/accept/run/tsidee2.nim new file mode 100644 index 000000000..2eaec01d7 --- /dev/null +++ b/tests/accept/run/tsidee2.nim @@ -0,0 +1,11 @@ + +var + global: int + +proc dontcare(x: int): int = return x + +proc SideEffectLyer(x, y: int): int {.noSideEffect.} = + return x + y + dontcare(x) + +echo SideEffectLyer(1, 3) #OUT 5 + diff --git a/tests/accept/run/tsidee3.nim b/tests/accept/run/tsidee3.nim new file mode 100644 index 000000000..be94192e7 --- /dev/null +++ b/tests/accept/run/tsidee3.nim @@ -0,0 +1,11 @@ + +var + global: int + +proc dontcare(x: int): int {.noSideEffect.} = return x + +proc noSideEffect(x, y: int, p: proc (a: int): int {.noSideEffect.}): int {.noSideEffect.} = + return x + y + dontcare(x) + +echo noSideEffect(1, 3, dontcare) #OUT 5 + diff --git a/tests/accept/run/tsimmeth.nim b/tests/accept/run/tsimmeth.nim new file mode 100644 index 000000000..3f5f810e6 --- /dev/null +++ b/tests/accept/run/tsimmeth.nim @@ -0,0 +1,8 @@ +# Test method simulation + +import strutils + +var x = "hallo world!".toLower.toUpper +x.echo() +#OUT HALLO WORLD! + diff --git a/tests/accept/run/tsplit.nim b/tests/accept/run/tsplit.nim new file mode 100644 index 000000000..711696b9e --- /dev/null +++ b/tests/accept/run/tsplit.nim @@ -0,0 +1,14 @@ +import strutils + +var s = "" +for w in split("|abc|xy|z", {'|'}): + s.add("#") + s.add(w) + +if s == "#abc#xy#z": + echo "true" +else: + echo "false" + +#OUT true + diff --git a/tests/accept/run/tstrange.nim b/tests/accept/run/tstrange.nim new file mode 100644 index 000000000..13aab2302 --- /dev/null +++ b/tests/accept/run/tstrange.nim @@ -0,0 +1,17 @@ +# test for extremely strange bug + +proc ack(x: int, y: int): int = + if x != 0: + if y != 5: + return y + return x + return x+y + +proc gen[T](a: T) = + write(stdout, a) + + +gen("hallo") +write(stdout, ack(5, 4)) +#OUT hallo4 + diff --git a/tests/accept/run/tstrlits.nim b/tests/accept/run/tstrlits.nim new file mode 100644 index 000000000..48ae08212 --- /dev/null +++ b/tests/accept/run/tstrlits.nim @@ -0,0 +1,14 @@ +# Test the new different string literals + +const + tripleEmpty = """"long string"""""""" # "long string """"" + + rawQuote = r"a""" + + raw = r"abc""def" + +stdout.write(rawQuote) +stdout.write(tripleEmpty) +stdout.write(raw) +#OUT a""long string"""""abc"def + diff --git a/tests/accept/run/tstrutil.nim b/tests/accept/run/tstrutil.nim new file mode 100644 index 000000000..0468dfa0c --- /dev/null +++ b/tests/accept/run/tstrutil.nim @@ -0,0 +1,24 @@ +# test the new strutils module + +import + strutils + +proc testStrip() = + write(stdout, strip(" ha ")) + +proc main() = + testStrip() + for p in split("/home/a1:xyz:/usr/bin", {':'}): + write(stdout, p) + + +assert(editDistance("prefix__hallo_suffix", "prefix__hallo_suffix") == 0) +assert(editDistance("prefix__hallo_suffix", "prefix__hallo_suffi1") == 1) +assert(editDistance("prefix__hallo_suffix", "prefix__HALLO_suffix") == 5) +assert(editDistance("prefix__hallo_suffix", "prefix__ha_suffix") == 3) +assert(editDistance("prefix__hallo_suffix", "prefix") == 14) +assert(editDistance("prefix__hallo_suffix", "suffix") == 14) +assert(editDistance("prefix__hallo_suffix", "prefix__hao_suffix") == 2) + +main() +#OUT ha/home/a1xyz/usr/bin diff --git a/tests/accept/run/tvardecl.nim b/tests/accept/run/tvardecl.nim new file mode 100644 index 000000000..496601e3a --- /dev/null +++ b/tests/accept/run/tvardecl.nim @@ -0,0 +1,9 @@ +# Test the new variable declaration syntax + +var + x = 0 + s = "Hallo" + a, b: int = 4 + +write(stdout, a) +write(stdout, b) #OUT 44 diff --git a/tests/accept/run/tvarnums.nim b/tests/accept/run/tvarnums.nim new file mode 100644 index 000000000..1b683ad94 --- /dev/null +++ b/tests/accept/run/tvarnums.nim @@ -0,0 +1,136 @@ +# Test variable length binary integers + +import + strutils + +type + TBuffer = array [0..10, int8] + +proc toVarNum(x: int32, b: var TBuffer) = + # encoding: first bit indicates end of number (0 if at end) + # second bit of the first byte denotes the sign (1 --> negative) + var a = x + if x != low(x): + # low(int) is a special case, + # because abs() does not work here! + # we leave x as it is and use the check >% instead of > + # for low(int) this is needed and positive numbers are not affected + # anyway + a = abs(x) + # first 6 bits: + b[0] = toU8(ord(a >% 63'i32) shl 7 or (ord(x < 0'i32) shl 6) or (int(a) and 63)) + a = a shr 6'i32 # skip first 6 bits + var i = 1 + while a != 0'i32: + b[i] = toU8(ord(a >% 127'i32) shl 7 or (int(a) and 127)) + inc(i) + a = a shr 7'i32 + +proc toVarNum64(x: int64, b: var TBuffer) = + # encoding: first bit indicates end of number (0 if at end) + # second bit of the first byte denotes the sign (1 --> negative) + var a = x + if x != low(x): + # low(int) is a special case, + # because abs() does not work here! + # we leave x as it is and use the check >% instead of > + # for low(int) this is needed and positive numbers are not affected + # anyway + a = abs(x) + # first 6 bits: + b[0] = toU8(ord(a >% 63'i64) shl 7 or (ord(x < 0'i64) shl 6) or int(a and 63)) + a = a shr 6 # skip first 6 bits + var i = 1 + while a != 0'i64: + b[i] = toU8(ord(a >% 127'i64) shl 7 or int(a and 127)) + inc(i) + a = a shr 7 + +proc toNum64(b: TBuffer): int64 = + # treat first byte different: + result = ze64(b[0]) and 63 + var + i = 0 + Shift = 6'i64 + while (ze(b[i]) and 128) != 0: + inc(i) + result = result or ((ze64(b[i]) and 127) shl Shift) + inc(Shift, 7) + if (ze(b[0]) and 64) != 0: # sign bit set? + result = not result +% 1 + # this is the same as ``- result`` + # but gives no overflow error for low(int) + +proc toNum(b: TBuffer): int32 = + # treat first byte different: + result = ze(b[0]) and 63 + var + i = 0 + Shift = 6'i32 + while (ze(b[i]) and 128) != 0: + inc(i) + result = result or ((int32(ze(b[i])) and 127'i32) shl Shift) + Shift = shift + 7'i32 + if (ze(b[0]) and (1 shl 6)) != 0: # sign bit set? + result = (not result) +% 1'i32 + # this is the same as ``- result`` + # but gives no overflow error for low(int) + +proc toBinary(x: int64): string = + result = newString(64) + for i in 0..63: + result[63-i] = chr((int(x shr i) and 1) + ord('0')) + +proc t64(i: int64) = + var + b: TBuffer + toVarNum64(i, b) + var x = toNum64(b) + if x != i: + writeln(stdout, $i) + writeln(stdout, toBinary(i)) + writeln(stdout, toBinary(x)) + +proc t32(i: int32) = + var + b: TBuffer + toVarNum(i, b) + var x = toNum(b) + if x != i: + writeln(stdout, toBinary(i)) + writeln(stdout, toBinary(x)) + +proc tm(i: int32) = + var + b: TBuffer + toVarNum64(i, b) + var x = toNum(b) + if x != i: + writeln(stdout, toBinary(i)) + writeln(stdout, toBinary(x)) + +t32(0) +t32(1) +t32(-1) +t32(-100_000) +t32(100_000) +t32(low(int32)) +t32(high(int32)) + +t64(low(int64)) +t64(high(int64)) +t64(0) +t64(-1) +t64(1) +t64(1000_000) +t64(-1000_000) + +tm(0) +tm(1) +tm(-1) +tm(-100_000) +tm(100_000) +tm(low(int32)) +tm(high(int32)) + +writeln(stdout, "Success!") #OUT Success! diff --git a/tests/accept/run/tvartup.nim b/tests/accept/run/tvartup.nim new file mode 100644 index 000000000..05b00b207 --- /dev/null +++ b/tests/accept/run/tvartup.nim @@ -0,0 +1,11 @@ +# Test the new tuple unpacking + +proc divmod(a, b: int): tuple[di, mo: int] = + return (a div b, a mod b) + +var (x, y) = divmod(15, 6) +stdout.write(x) +stdout.write(" ") +stdout.write(y) + +#OUT 2 3 |