diff options
Diffstat (limited to 'tests/misc')
50 files changed, 598 insertions, 505 deletions
diff --git a/tests/misc/minit.nim b/tests/misc/minit.nim index 75fcebb77..513f46af5 100644 --- a/tests/misc/minit.nim +++ b/tests/misc/minit.nim @@ -1,2 +1,2 @@ -# Test the new initialization for modules -write(stdout, "Hello from module! ") +# Test the new initialization for modules +write(stdout, "Hello from module! ") diff --git a/tests/misc/mvarious.nim b/tests/misc/mvarious.nim index c0a8add73..d1587faec 100644 --- a/tests/misc/mvarious.nim +++ b/tests/misc/mvarious.nim @@ -1,6 +1,6 @@ -# Test a submodule - -#type -# TStringArr = array [0.. *] of string - -proc exportme* = discard +# Test a submodule + +#type +# TStringArr = array [0.. *] of string + +proc exportme* = discard diff --git a/tests/misc/t99bott.nim b/tests/misc/t99bott.nim index b3b30d296..62ccfbe16 100644 --- a/tests/misc/t99bott.nim +++ b/tests/misc/t99bott.nim @@ -25,7 +25,7 @@ proc GetBottleNumber(n: int): string = for bn in countdown(99, 1): const cur = GetBottleNumber(bn) echo(cur, " on the wall, ", cur, ".") - echo("Take one down and pass it around, ", GetBottleNumber(bn-1), + echo("Take one down and pass it around, ", GetBottleNumber(bn-1), " on the wall.\n") echo "No more bottles of beer on the wall, no more bottles of beer." diff --git a/tests/misc/tack.nim b/tests/misc/tack.nim index 680ff567e..a0afab9e8 100644 --- a/tests/misc/tack.nim +++ b/tests/misc/tack.nim @@ -2,20 +2,20 @@ discard """ file: "tack.nim" output: "125" """ -# 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 +# 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/misc/tatomic.nim b/tests/misc/tatomic.nim index 1fa0cff8d..f3c56ffe3 100644 --- a/tests/misc/tatomic.nim +++ b/tests/misc/tatomic.nim @@ -3,9 +3,9 @@ discard """ line: 7 errormsg: "identifier expected, but found 'keyword atomic'" """ -var +var atomic: int - + echo atomic diff --git a/tests/misc/tcmdline.nim b/tests/misc/tcmdline.nim index f4ee20d31..cb8cb402c 100644 --- a/tests/misc/tcmdline.nim +++ b/tests/misc/tcmdline.nim @@ -7,8 +7,8 @@ var i: int params = paramCount() i = 0 -writeln(stdout, "This exe: " & getAppFilename()) -writeln(stdout, "Number of parameters: " & $params) +writeLine(stdout, "This exe: " & getAppFilename()) +writeLine(stdout, "Number of parameters: " & $params) while i <= params: - writeln(stdout, paramStr(i)) + writeLine(stdout, paramStr(i)) i = i + 1 diff --git a/tests/misc/tcolonisproc.nim b/tests/misc/tcolonisproc.nim index af4077284..665e9e604 100644 --- a/tests/misc/tcolonisproc.nim +++ b/tests/misc/tcolonisproc.nim @@ -7,7 +7,7 @@ when false: p(1, 3): echo 1 echo 3 - + p(1, 1, proc() = echo 1 echo 2) diff --git a/tests/misc/tdllvar.nim b/tests/misc/tdllvar.nim index 5a31dfbbb..1c1238e8d 100644 --- a/tests/misc/tdllvar.nim +++ b/tests/misc/tdllvar.nim @@ -1,6 +1,6 @@ import os -proc getDllName: string = +proc getDllName: string = result = "mylib.dll" if fileExists(result): return result = "mylib2.dll" diff --git a/tests/misc/temit.nim b/tests/misc/temit.nim index e2a9eaff1..c83235659 100644 --- a/tests/misc/temit.nim +++ b/tests/misc/temit.nim @@ -2,14 +2,14 @@ discard """ file: "temit.nim" output: "509" """ -# Test the new ``emit`` pragma: +# Test the new ``emit`` pragma: {.emit: """ static int cvariable = 420; """.} -proc embedsC() = +proc embedsC() = var nimVar = 89 {.emit: """printf("%d\n", cvariable + (int)`nimVar`);""".} diff --git a/tests/misc/tendian.nim b/tests/misc/tendian.nim index 256e2653c..91044f4d5 100644 --- a/tests/misc/tendian.nim +++ b/tests/misc/tendian.nim @@ -1,3 +1,3 @@ # test the new endian magic -writeln(stdout, repr(system.cpuEndian)) +writeLine(stdout, repr(system.cpuEndian)) diff --git a/tests/misc/tevents.nim b/tests/misc/tevents.nim index fb94b1f79..5f2169f29 100644 --- a/tests/misc/tevents.nim +++ b/tests/misc/tevents.nim @@ -13,11 +13,11 @@ type proc handleprintevent*(e: TEventArgs) = write(stdout, "HandlePrintEvent: Output -> Handled print event\n") - + proc handleprintevent2*(e: TEventArgs) = var args: TPrintEventArgs = TPrintEventArgs(e) write(stdout, "HandlePrintEvent2: Output -> printing for " & args.user) - + var ee = initEventEmitter() var eventargs: TPrintEventArgs diff --git a/tests/misc/tfib.nim b/tests/misc/tfib.nim index 09a4d5038..34fe0dcf9 100644 --- a/tests/misc/tfib.nim +++ b/tests/misc/tfib.nim @@ -1,8 +1,8 @@ -iterator fibonacci(): int = +iterator fibonacci(): int = var a = 0 var b = 1 - while true: + while true: yield a var c = b b = a diff --git a/tests/misc/tgetstartmilsecs.nim b/tests/misc/tgetstartmilsecs.nim index 5a3368e0f..bf508dd54 100644 --- a/tests/misc/tgetstartmilsecs.nim +++ b/tests/misc/tgetstartmilsecs.nim @@ -1,4 +1,4 @@ -# +# import times, os var start = epochTime() diff --git a/tests/misc/thallo.nim b/tests/misc/thallo.nim index cbeb45b97..17e955f03 100644 --- a/tests/misc/thallo.nim +++ b/tests/misc/thallo.nim @@ -22,7 +22,7 @@ macro macrotest(n: expr): stmt {.immediate.} = result = newNimNode(nnkStmtList, n) for i in 2..n.len-1: result.add(newCall("write", n[1], n[i])) - result.add(newCall("writeln", n[1], newStrLitNode(""))) + result.add(newCall("writeLine", n[1], newStrLitNode(""))) macro debug(n: expr): stmt {.immediate.} = let n = callsite() @@ -30,7 +30,7 @@ macro debug(n: expr): stmt {.immediate.} = for i in 1..n.len-1: result.add(newCall("write", newIdentNode("stdout"), toStrLit(n[i]))) result.add(newCall("write", newIdentNode("stdout"), newStrLitNode(": "))) - result.add(newCall("writeln", newIdentNode("stdout"), n[i])) + result.add(newCall("writeLine", newIdentNode("stdout"), n[i])) macrotest(stdout, "finally", 4, 5, "variable", "argument lists") macrotest(stdout) @@ -38,7 +38,7 @@ macrotest(stdout) #GC_disable() echo("This was compiled by Nim version " & system.NimVersion) -writeln(stdout, "Hello", " World", "!") +writeLine(stdout, "Hello", " World", "!") echo(["a", "b", "c", "d"].len) for x in items(["What's", "your", "name", "?", ]): diff --git a/tests/misc/theaproots.nim b/tests/misc/theaproots.nim index aec140f42..77d0207b0 100644 --- a/tests/misc/theaproots.nim +++ b/tests/misc/theaproots.nim @@ -1,7 +1,7 @@ -type +type Bar = object x: int - + Foo = object rheap: ref Bar rmaybe: ref Bar @@ -31,7 +31,7 @@ proc test(maybeFoo: var Foo, maybeFoo.list[3] = bb maybeFoo.listarr[3] = bb acc(maybeFoo) = bb - + var localFoo: Foo localFoo.rstack = bb localFoo.list[3] = bb diff --git a/tests/misc/thintoff.nim b/tests/misc/thintoff.nim index 807ff44f3..95318ce9b 100644 --- a/tests/misc/thintoff.nim +++ b/tests/misc/thintoff.nim @@ -6,7 +6,7 @@ discard """ {.hint[XDeclaredButNotUsed]: off.} var x: int - + echo x #OUT 0 diff --git a/tests/misc/tinit.nim b/tests/misc/tinit.nim index 5c75567ec..02607909b 100644 --- a/tests/misc/tinit.nim +++ b/tests/misc/tinit.nim @@ -2,11 +2,11 @@ discard """ file: "tinit.nim" output: "Hello from module! Hello from main module!" """ -# Test the new init section in modules - -import minit - -write(stdout, "Hello from main module!\n") -#OUT Hello from module! Hello from main module! +# Test the new init section in modules + +import minit + +write(stdout, "Hello from main module!\n") +#OUT Hello from module! Hello from main module! diff --git a/tests/misc/tinout.nim b/tests/misc/tinout.nim index 4e5908428..0b2a54d9f 100644 --- a/tests/misc/tinout.nim +++ b/tests/misc/tinout.nim @@ -3,14 +3,14 @@ discard """ line: 12 errormsg: "type mismatch: got (int literal(3))" """ -# Test in out checking for parameters - -proc abc(x: var int) = - x = 0 - -proc b() = - abc(3) #ERROR - -b() +# Test in out checking for parameters + +proc abc(x: var int) = + x = 0 + +proc b() = + abc(3) #ERROR + +b() diff --git a/tests/misc/tinvalidnewseq.nim b/tests/misc/tinvalidnewseq.nim index 957a25560..89083d8b2 100644 --- a/tests/misc/tinvalidnewseq.nim +++ b/tests/misc/tinvalidnewseq.nim @@ -14,12 +14,12 @@ proc parseURL(url: string): TURL = var m: array[0..6, string] #Array with the matches newSeq(m, 7) #ERROR discard regexprs.match(url, re(pattern), m) - - result = (protocol: m[1], subdomain: m[2], domain: m[3] & m[4], + + result = (protocol: m[1], subdomain: m[2], domain: m[3] & m[4], port: m[5], path: m[6].split('/')) - + var r: TUrl - + r = parseUrl(r"http://google.com/search?var=bleahdhsad") echo(r.domain) diff --git a/tests/misc/tissue710.nim b/tests/misc/tissue710.nim index ecfdf653e..3b6d3e5f3 100644 --- a/tests/misc/tissue710.nim +++ b/tests/misc/tissue710.nim @@ -1,7 +1,7 @@ discard """ file: "tissue710.nim" line: 8 - errorMsg: "undeclared identifier: '||'" + errorMsg: "attempting to call undeclared routine: '||'" """ var sum = 0 for x in 3..1000: diff --git a/tests/misc/tlastmod.nim b/tests/misc/tlastmod.nim index 92ac922f7..538b5e656 100644 --- a/tests/misc/tlastmod.nim +++ b/tests/misc/tlastmod.nim @@ -1,18 +1,18 @@ -# test the new LastModificationTime() proc - -import - os, times, strutils - -proc main() = - var - a, b: TTime - a = getLastModificationTime(paramStr(1)) - b = getLastModificationTime(paramStr(2)) - writeln(stdout, $a) - writeln(stdout, $b) - if a < b: - write(stdout, "$2 is newer than $1\n" % [paramStr(1), paramStr(2)]) - else: - write(stdout, "$1 is newer than $2\n" % [paramStr(1), paramStr(2)]) - -main() +# test the new LastModificationTime() proc + +import + os, times, strutils + +proc main() = + var + a, b: TTime + a = getLastModificationTime(paramStr(1)) + b = getLastModificationTime(paramStr(2)) + writeLine(stdout, $a) + writeLine(stdout, $b) + if a < b: + write(stdout, "$2 is newer than $1\n" % [paramStr(1), paramStr(2)]) + else: + write(stdout, "$1 is newer than $2\n" % [paramStr(1), paramStr(2)]) + +main() diff --git a/tests/misc/tlocals.nim b/tests/misc/tlocals.nim index af8a54946..3e240d3c8 100644 --- a/tests/misc/tlocals.nim +++ b/tests/misc/tlocals.nim @@ -2,10 +2,10 @@ discard """ output: "(x: string here, a: 1)" """ -proc simple[T](a: T) = +proc simple[T](a: T) = var x = "string here" echo locals() - + simple(1) diff --git a/tests/misc/tloops.nim b/tests/misc/tloops.nim index 1aada0298..b160500af 100644 --- a/tests/misc/tloops.nim +++ b/tests/misc/tloops.nim @@ -76,7 +76,7 @@ proc main[T]() = b = (1, 2, 3) myType = b echo myType - + var myType2: MyType2 var c: MyType2 c = (1.0, 2.0) diff --git a/tests/misc/tmandelbrot.nim b/tests/misc/tmandelbrot.nim index 4228b0416..e9b7a3e5a 100644 --- a/tests/misc/tmandelbrot.nim +++ b/tests/misc/tmandelbrot.nim @@ -24,10 +24,10 @@ var size = parseInt (paramStr (1)) var bit = 128 var byteAcc = 0 -stdout.writeln ("P4") +stdout.writeLine ("P4") stdout.write ($size) stdout.write (" ") -stdout.writeln ($size) +stdout.writeLine ($size) var fsize = float (size) for y in 0 .. size-1: diff --git a/tests/misc/tmissingnilcheck.nim b/tests/misc/tmissingnilcheck.nim index c2f23ae87..461fb18f4 100644 --- a/tests/misc/tmissingnilcheck.nim +++ b/tests/misc/tmissingnilcheck.nim @@ -14,7 +14,7 @@ proc newConnection = proc cb {.closure.} = discard - + first.callback = cb newConnection() diff --git a/tests/misc/tnew.nim b/tests/misc/tnew.nim index 6527541a2..88e8bd02c 100644 --- a/tests/misc/tnew.nim +++ b/tests/misc/tnew.nim @@ -1,49 +1,49 @@ -# Test the implementation of the new operator -# and the code generation for gc walkers -# (and the garbage collector): - -type - PNode = ref TNode - TNode = object - data: int - str: string - le, ri: PNode - - TStressTest = ref array [0..45, array [1..45, TNode]] - -proc finalizer(n: PNode) = - write(stdout, n.data) - write(stdout, " is now freed\n") - -proc newNode(data: int, le, ri: PNode): PNode = - new(result, finalizer) - result.le = le - result.ri = ri - result.data = data - -# now loop and build a tree -proc main() = - var - i = 0 - p: TStressTest - while i < 1000: - var n: PNode - - n = newNode(i, nil, newNode(i + 10000, nil, nil)) - inc(i) - new(p) - - write(stdout, "Simple tree node allocation worked!\n") - i = 0 - while i < 1000: - var m = newNode(i + 20000, nil, nil) - var k = newNode(i + 30000, nil, nil) - m.le = m - m.ri = k - k.le = m - k.ri = k - inc(i) - - write(stdout, "Simple cycle allocation worked!\n") - -main() +# Test the implementation of the new operator +# and the code generation for gc walkers +# (and the garbage collector): + +type + PNode = ref TNode + TNode = object + data: int + str: string + le, ri: PNode + + TStressTest = ref array [0..45, array [1..45, TNode]] + +proc finalizer(n: PNode) = + write(stdout, n.data) + write(stdout, " is now freed\n") + +proc newNode(data: int, le, ri: PNode): PNode = + new(result, finalizer) + result.le = le + result.ri = ri + result.data = data + +# now loop and build a tree +proc main() = + var + i = 0 + p: TStressTest + while i < 1000: + var n: PNode + + n = newNode(i, nil, newNode(i + 10000, nil, nil)) + inc(i) + new(p) + + write(stdout, "Simple tree node allocation worked!\n") + i = 0 + while i < 1000: + var m = newNode(i + 20000, nil, nil) + var k = newNode(i + 30000, nil, nil) + m.le = m + m.ri = k + k.le = m + k.ri = k + inc(i) + + write(stdout, "Simple cycle allocation worked!\n") + +main() diff --git a/tests/misc/tnewderef.nim b/tests/misc/tnewderef.nim index 89dc4c8d1..3394dbddf 100644 --- a/tests/misc/tnewderef.nim +++ b/tests/misc/tnewderef.nim @@ -7,5 +7,5 @@ var x: ref int new(x) x[] = 3 -echo x[] +echo x[] diff --git a/tests/misc/tnewsets.nim b/tests/misc/tnewsets.nim index 415fe8f7e..f239d4aa2 100644 --- a/tests/misc/tnewsets.nim +++ b/tests/misc/tnewsets.nim @@ -1,6 +1,6 @@ -# new test for sets: - -const elem = ' ' - -var s: set[char] = {elem} -assert(elem in s and 'a' not_in s and 'c' not_in s ) +# new test for sets: + +const elem = ' ' + +var s: set[char] = {elem} +assert(elem in s and 'a' not_in s and 'c' not_in s ) diff --git a/tests/misc/tnewuns.nim b/tests/misc/tnewuns.nim index 5181e467c..d6bae4fb1 100644 --- a/tests/misc/tnewuns.nim +++ b/tests/misc/tnewuns.nim @@ -1,12 +1,12 @@ -# test the new unsigned operations: - -import - strutils - -var - x, y: int - -x = 1 -y = high(int) - -writeln(stdout, $ ( x +% y ) ) +# test the new unsigned operations: + +import + strutils + +var + x, y: int + +x = 1 +y = high(int) + +writeLine(stdout, $ ( x +% y ) ) diff --git a/tests/misc/tnoinst.nim b/tests/misc/tnoinst.nim index 4c8d9d1aa..25ebe8dfc 100644 --- a/tests/misc/tnoinst.nim +++ b/tests/misc/tnoinst.nim @@ -11,7 +11,7 @@ proc wrap[T]() = var x: proc (x, y: T): int x = notConcrete - + wrap[int]() diff --git a/tests/misc/tnoop.nim b/tests/misc/tnoop.nim index 10c2eb2ec..1e3fbe6cf 100644 --- a/tests/misc/tnoop.nim +++ b/tests/misc/tnoop.nim @@ -1,7 +1,7 @@ discard """ file: "tnoop.nim" line: 11 - errormsg: "undeclared identifier: 'a'" + errormsg: "attempting to call undeclared routine: 'a'" """ diff --git a/tests/misc/tnot.nim b/tests/misc/tnot.nim index 6193e21e1..8c75c6bc0 100644 --- a/tests/misc/tnot.nim +++ b/tests/misc/tnot.nim @@ -1,14 +1,14 @@ discard """ - file: "tnot.nim" - line: 14 + tfile: "tnot.nim" + tline: 14 errormsg: "type mismatch" """ # BUG: following compiles, but should not: -proc nodeOfDegree(x: int): bool = +proc nodeOfDegree(x: int): bool = result = false -proc main = +proc main = for j in 0..2: for i in 0..10: if not nodeOfDegree(1) >= 0: #ERROR_MSG type mismatch @@ -17,6 +17,3 @@ proc main = echo "No" main() - - - diff --git a/tests/misc/tparedef.nim b/tests/misc/tparedef.nim index dedebf6b7..83c2651ff 100644 --- a/tests/misc/tparedef.nim +++ b/tests/misc/tparedef.nim @@ -1,4 +1,4 @@ -# This test is now superfluous: - -proc a(a: int) = - return +# This test is now superfluous: + +proc a(a: int) = + return diff --git a/tests/misc/tpos.nim b/tests/misc/tpos.nim index 5560ef050..bedb62e62 100644 --- a/tests/misc/tpos.nim +++ b/tests/misc/tpos.nim @@ -2,34 +2,34 @@ discard """ file: "tpos.nim" output: "6" """ -# 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 = "hello" -var s = "world hello" -write(stdout, mypos(sub, s)) -#OUT 6 +# 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 = "hello" +var s = "world hello" +write(stdout, mypos(sub, s)) +#OUT 6 diff --git a/tests/misc/tquicksort.nim b/tests/misc/tquicksort.nim index 6706a185e..0867a3769 100644 --- a/tests/misc/tquicksort.nim +++ b/tests/misc/tquicksort.nim @@ -9,17 +9,17 @@ proc QuickSort(list: seq[int]): seq[int] = left.add(list[i]) elif list[i] > pivot: right.add(list[i]) - result = QuickSort(left) & - pivot & + result = QuickSort(left) & + pivot & QuickSort(right) - + proc echoSeq(a: seq[int]) = for i in low(a)..high(a): echo(a[i]) var list: seq[int] - + list = QuickSort(@[89,23,15,23,56,123,356,12,7,1,6,2,9,4,3]) echoSeq(list) diff --git a/tests/misc/tradix.nim b/tests/misc/tradix.nim index 311aa9ccd..36a4f39f6 100644 --- a/tests/misc/tradix.nim +++ b/tests/misc/tradix.nim @@ -1,7 +1,7 @@ # implements and tests an efficient radix tree -## another method to store an efficient array of pointers: -## We use a radix tree with node compression. +## another method to store an efficient array of pointers: +## We use a radix tree with node compression. ## There are two node kinds: const BitsPerUnit = 8*sizeof(int) @@ -15,7 +15,7 @@ type len: int8 keys: array [0..31, int8] vals: array [0..31, PRadixNode] - + TRadixNodeFull = object of TRadixNode b: array [0..255, PRadixNode] TRadixNodeLeafBits = object of TRadixNode @@ -27,43 +27,43 @@ type var root: PRadixNode -proc searchInner(r: PRadixNode, a: int): PRadixNode = +proc searchInner(r: PRadixNode, a: int): PRadixNode = case r.kind of rnLinear: var x = cast[ptr TRadixNodeLinear](r) - for i in 0..ze(x.len)-1: + for i in 0..ze(x.len)-1: if ze(x.keys[i]) == a: return x.vals[i] - of rnFull: + of rnFull: var x = cast[ptr TRadixNodeFull](r) return x.b[a] else: assert(false) -proc testBit(w, i: int): bool {.inline.} = +proc testBit(w, i: int): bool {.inline.} = result = (w and (1 shl (i %% BitsPerUnit))) != 0 -proc setBit(w: var int, i: int) {.inline.} = +proc setBit(w: var int, i: int) {.inline.} = w = w or (1 shl (i %% BitsPerUnit)) -proc resetBit(w: var int, i: int) {.inline.} = +proc resetBit(w: var int, i: int) {.inline.} = w = w and not (1 shl (i %% BitsPerUnit)) -proc testOrSetBit(w: var int, i: int): bool {.inline.} = +proc testOrSetBit(w: var int, i: int): bool {.inline.} = var x = (1 shl (i %% BitsPerUnit)) if (w and x) != 0: return true w = w or x -proc searchLeaf(r: PRadixNode, a: int): bool = +proc searchLeaf(r: PRadixNode, a: int): bool = case r.kind of rnLeafBits: var x = cast[ptr TRadixNodeLeafBits](r) return testBit(x.b[a /% BitsPerUnit], a) of rnLeafLinear: var x = cast[ptr TRadixNodeLeafLinear](r) - for i in 0..ze(x.len)-1: + for i in 0..ze(x.len)-1: if ze(x.keys[i]) == a: return true else: assert(false) -proc exclLeaf(r: PRadixNode, a: int) = +proc exclLeaf(r: PRadixNode, a: int) = case r.kind of rnLeafBits: var x = cast[ptr TRadixNodeLeafBits](r) @@ -71,8 +71,8 @@ proc exclLeaf(r: PRadixNode, a: int) = of rnLeafLinear: var x = cast[ptr TRadixNodeLeafLinear](r) var L = ze(x.len) - for i in 0..L-1: - if ze(x.keys[i]) == a: + for i in 0..L-1: + if ze(x.keys[i]) == a: x.keys[i] = x.keys[L-1] dec(x.len) return @@ -98,7 +98,7 @@ proc excl*(r: PRadixNode, a: ByteAddress): bool = if x == nil: return false exclLeaf(x, a and 0xff) -proc addLeaf(r: var PRadixNode, a: int): bool = +proc addLeaf(r: var PRadixNode, a: int): bool = if r == nil: # a linear node: var x = cast[ptr TRadixNodeLinear](alloc(sizeof(TRadixNodeLinear))) @@ -107,23 +107,23 @@ proc addLeaf(r: var PRadixNode, a: int): bool = x.keys[0] = toU8(a) r = x return false # not already in set - case r.kind - of rnLeafBits: + case r.kind + of rnLeafBits: var x = cast[ptr TRadixNodeLeafBits](r) return testOrSetBit(x.b[a /% BitsPerUnit], a) - of rnLeafLinear: + of rnLeafLinear: var x = cast[ptr TRadixNodeLeafLinear](r) var L = ze(x.len) - for i in 0..L-1: + for i in 0..L-1: if ze(x.keys[i]) == a: return true if L <= high(x.keys): x.keys[L] = toU8(a) inc(x.len) - else: + else: # transform into a full node: var y = cast[ptr TRadixNodeLeafBits](alloc0(sizeof(TRadixNodeLeafBits))) y.kind = rnLeafBits - for i in 0..ze(x.len)-1: + for i in 0..ze(x.len)-1: var u = ze(x.keys[i]) setBit(y.b[u /% BitsPerUnit], u) setBit(y.b[a /% BitsPerUnit], a) @@ -131,8 +131,8 @@ proc addLeaf(r: var PRadixNode, a: int): bool = r = y else: assert(false) -proc addInner(r: var PRadixNode, a: int, d: int): bool = - if d == 0: +proc addInner(r: var PRadixNode, a: int, d: int): bool = + if d == 0: return addLeaf(r, a and 0xff) var k = a shr d and 0xff if r == nil: @@ -147,14 +147,14 @@ proc addInner(r: var PRadixNode, a: int, d: int): bool = of rnLinear: var x = cast[ptr TRadixNodeLinear](r) var L = ze(x.len) - for i in 0..L-1: + for i in 0..L-1: if ze(x.keys[i]) == k: # already exists return addInner(x.vals[i], a, d-8) if L <= high(x.keys): x.keys[L] = toU8(k) inc(x.len) return addInner(x.vals[L], a, d-8) - else: + else: # transform into a full node: var y = cast[ptr TRadixNodeFull](alloc0(sizeof(TRadixNodeFull))) y.kind = rnFull @@ -162,55 +162,55 @@ proc addInner(r: var PRadixNode, a: int, d: int): bool = dealloc(r) r = y return addInner(y.b[k], a, d-8) - of rnFull: + of rnFull: var x = cast[ptr TRadixNodeFull](r) return addInner(x.b[k], a, d-8) else: assert(false) -proc incl*(r: var PRadixNode, a: ByteAddress) {.inline.} = +proc incl*(r: var PRadixNode, a: ByteAddress) {.inline.} = discard addInner(r, a, 24) - -proc testOrIncl*(r: var PRadixNode, a: ByteAddress): bool {.inline.} = + +proc testOrIncl*(r: var PRadixNode, a: ByteAddress): bool {.inline.} = return addInner(r, a, 24) - -iterator innerElements(r: PRadixNode): tuple[prefix: int, n: PRadixNode] = + +iterator innerElements(r: PRadixNode): tuple[prefix: int, n: PRadixNode] = if r != nil: - case r.kind - of rnFull: + case r.kind + of rnFull: var r = cast[ptr TRadixNodeFull](r) for i in 0..high(r.b): - if r.b[i] != nil: + if r.b[i] != nil: yield (i, r.b[i]) - of rnLinear: + of rnLinear: var r = cast[ptr TRadixNodeLinear](r) - for i in 0..ze(r.len)-1: + for i in 0..ze(r.len)-1: yield (ze(r.keys[i]), r.vals[i]) else: assert(false) -iterator leafElements(r: PRadixNode): int = +iterator leafElements(r: PRadixNode): int = if r != nil: case r.kind - of rnLeafBits: + of rnLeafBits: var r = cast[ptr TRadixNodeLeafBits](r) # iterate over any bit: - for i in 0..high(r.b): + for i in 0..high(r.b): if r.b[i] != 0: # test all bits for zero - for j in 0..BitsPerUnit-1: - if testBit(r.b[i], j): + for j in 0..BitsPerUnit-1: + if testBit(r.b[i], j): yield i*BitsPerUnit+j - of rnLeafLinear: + of rnLeafLinear: var r = cast[ptr TRadixNodeLeafLinear](r) - for i in 0..ze(r.len)-1: + for i in 0..ze(r.len)-1: yield ze(r.keys[i]) else: assert(false) - -iterator elements*(r: PRadixNode): ByteAddress {.inline.} = - for p1, n1 in innerElements(r): + +iterator elements*(r: PRadixNode): ByteAddress {.inline.} = + for p1, n1 in innerElements(r): for p2, n2 in innerElements(n1): for p3, n3 in innerElements(n2): - for p4 in leafElements(n3): + for p4 in leafElements(n3): yield p1 shl 24 or p2 shl 16 or p3 shl 8 or p4 - + proc main() = const numbers = [128, 1, 2, 3, 4, 255, 17, -8, 45, 19_000] @@ -224,31 +224,31 @@ main() when false: - proc traverse(r: PRadixNode, prefix: int, d: int) = + proc traverse(r: PRadixNode, prefix: int, d: int) = if r == nil: return - case r.kind - of rnLeafBits: + case r.kind + of rnLeafBits: assert(d == 0) var x = cast[ptr TRadixNodeLeafBits](r) # iterate over any bit: - for i in 0..high(x.b): + for i in 0..high(x.b): if x.b[i] != 0: # test all bits for zero - for j in 0..BitsPerUnit-1: - if testBit(x.b[i], j): + for j in 0..BitsPerUnit-1: + if testBit(x.b[i], j): visit(prefix or i*BitsPerUnit+j) - of rnLeafLinear: + of rnLeafLinear: assert(d == 0) var x = cast[ptr TRadixNodeLeafLinear](r) - for i in 0..ze(x.len)-1: + for i in 0..ze(x.len)-1: visit(prefix or ze(x.keys[i])) - of rnFull: + of rnFull: var x = cast[ptr TRadixNodeFull](r) for i in 0..high(r.b): - if r.b[i] != nil: + if r.b[i] != nil: traverse(r.b[i], prefix or (i shl d), d-8) - of rnLinear: + of rnLinear: var x = cast[ptr TRadixNodeLinear](r) - for i in 0..ze(x.len)-1: + for i in 0..ze(x.len)-1: traverse(x.vals[i], prefix or (ze(x.keys[i]) shl d), d-8) type @@ -261,59 +261,59 @@ when false: i.r = r i.x = 0 i.p = 0 - - proc nextr(i: var TRadixIter): PRadixNode = + + proc nextr(i: var TRadixIter): PRadixNode = if i.r == nil: return nil - case i.r.kind - of rnFull: + case i.r.kind + of rnFull: var r = cast[ptr TRadixNodeFull](i.r) while i.x <= high(r.b): - if r.b[i.x] != nil: + if r.b[i.x] != nil: i.p = i.x return r.b[i.x] inc(i.x) - of rnLinear: + of rnLinear: var r = cast[ptr TRadixNodeLinear](i.r) - if i.x < ze(r.len): + if i.x < ze(r.len): i.p = ze(r.keys[i.x]) result = r.vals[i.x] inc(i.x) else: assert(false) - proc nexti(i: var TRadixIter): int = + proc nexti(i: var TRadixIter): int = result = -1 - case i.r.kind - of rnLeafBits: + case i.r.kind + of rnLeafBits: var r = cast[ptr TRadixNodeLeafBits](i.r) - # iterate over any bit: - for i in 0..high(r.b): + # iterate over any bit: + for i in 0..high(r.b): if x.b[i] != 0: # test all bits for zero - for j in 0..BitsPerUnit-1: - if testBit(x.b[i], j): + for j in 0..BitsPerUnit-1: + if testBit(x.b[i], j): visit(prefix or i*BitsPerUnit+j) - of rnLeafLinear: + of rnLeafLinear: var r = cast[ptr TRadixNodeLeafLinear](i.r) - if i.x < ze(r.len): + if i.x < ze(r.len): result = ze(r.keys[i.x]) inc(i.x) - iterator elements(r: PRadixNode): ByteAddress {.inline.} = + iterator elements(r: PRadixNode): ByteAddress {.inline.} = var a, b, c, d: TRadixIter init(a, r) - while true: + while true: var x = nextr(a) - if x != nil: + if x != nil: init(b, x) - while true: + while true: var y = nextr(b) - if y != nil: + if y != nil: init(c, y) while true: var z = nextr(c) - if z != nil: + if z != nil: init(d, z) while true: var q = nexti(d) - if q != -1: + if q != -1: yield a.p shl 24 or b.p shl 16 or c.p shl 8 or q diff --git a/tests/misc/trawstr.nim b/tests/misc/trawstr.nim index ab2aae159..55e508acc 100644 --- a/tests/misc/trawstr.nim +++ b/tests/misc/trawstr.nim @@ -3,10 +3,10 @@ discard """ line: 10 errormsg: "closing \" expected" """ -# Test the new raw strings: - -const - xxx = r"This is a raw string!" - yyy = "This not\" #ERROR +# Test the new raw strings: + +const + xxx = r"This is a raw string!" + yyy = "This not\" #ERROR diff --git a/tests/misc/tromans.nim b/tests/misc/tromans.nim index fa6a63595..132c73ddd 100644 --- a/tests/misc/tromans.nim +++ b/tests/misc/tromans.nim @@ -32,7 +32,7 @@ proc RomanToDecimal(romanVal: string): int = of 'C', 'c': val = 100 of 'D', 'd': val = 500 of 'M', 'm': val = 1000 - else: raiseInvalidValue("Incorrect character in roman numeral! (" & + else: raiseInvalidValue("Incorrect character in roman numeral! (" & $romanVal[i] & ")") if val >= prevVal: inc(result, val) @@ -49,7 +49,7 @@ proc DecimalToRoman(decValParam: int): string = ("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)] + ("V", 5), ("IV", 4), ("I", 1)] if decValParam < 1 or decValParam > 3999: raiseInvalidValue("number not representable") result = "" @@ -64,7 +64,7 @@ for i in 1..100: 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/misc/tsimplesort.nim b/tests/misc/tsimplesort.nim index c282b3445..9c6ad1207 100644 --- a/tests/misc/tsimplesort.nim +++ b/tests/misc/tsimplesort.nim @@ -1,7 +1,7 @@ discard """ output: '''true''' """ - + import hashes, math {.pragma: myShallow.} @@ -112,7 +112,7 @@ proc initTable*[A, B](initialSize=64): TTable[A, B] = result.counter = 0 newSeq(result.data, initialSize) -proc toTable*[A, B](pairs: openarray[tuple[key: A, +proc toTable*[A, B](pairs: openarray[tuple[key: A, val: B]]): TTable[A, B] = ## creates a new hash table that contains the given `pairs`. result = initTable[A, B](nextPowerOfTwo(pairs.len+10)) @@ -214,7 +214,7 @@ proc `$`*[A](t: TCountTable[A]): string = ## The `$` operator for count tables. dollarImpl() -proc inc*[A](t: var TCountTable[A], key: A, val = 1) = +proc inc*[A](t: var TCountTable[A], key: A, val = 1) = ## increments `t[key]` by `val`. var index = RawGet(t, key) if index >= 0: diff --git a/tests/misc/tsimtych.nim b/tests/misc/tsimtych.nim index dd969958c..27a922f6a 100644 --- a/tests/misc/tsimtych.nim +++ b/tests/misc/tsimtych.nim @@ -3,10 +3,10 @@ discard """ line: 10 errormsg: "type mismatch: got (bool) but expected \'string\'" """ -# Test 2 -# Simple type checking - -var a: string -a = false #ERROR +# Test 2 +# Simple type checking + +var a: string +a = false #ERROR diff --git a/tests/misc/tsizeof.nim b/tests/misc/tsizeof.nim index f7b70dd4d..4afd48472 100644 --- a/tests/misc/tsizeof.nim +++ b/tests/misc/tsizeof.nim @@ -1,10 +1,10 @@ -# Test the sizeof proc - -type - TMyRecord {.final.} = object - x, y: int - b: bool - r: float - s: string - -write(stdout, sizeof(TMyRecord)) +# Test the sizeof proc + +type + TMyRecord {.final.} = object + x, y: int + b: bool + r: float + s: string + +write(stdout, sizeof(TMyRecord)) diff --git a/tests/misc/tsortdev.nim b/tests/misc/tsortdev.nim index d7d42d22c..f360d9646 100644 --- a/tests/misc/tsortdev.nim +++ b/tests/misc/tsortdev.nim @@ -37,17 +37,17 @@ proc cmpPlatforms(a, b: string): int = else: return system.cmp(a, b) -proc sorted[T](a: openArray[T]): bool = +proc sorted[T](a: openArray[T]): bool = result = true for i in 0 .. < a.high: - if cmpPlatforms(a[i], a[i+1]) > 0: + if cmpPlatforms(a[i], a[i+1]) > 0: echo "Out of order: ", a[i], " ", a[i+1] result = false proc main() = - var testData = @["netbsd-x86_64", "windows-x86", "linux-x86_64", "linux-x86", + var testData = @["netbsd-x86_64", "windows-x86", "linux-x86_64", "linux-x86", "linux-ppc64", "macosx-x86-1058", "macosx-x86-1068"] - + sort(testData, cmpPlatforms) doAssert sorted(testData) diff --git a/tests/misc/tstrace.nim b/tests/misc/tstrace.nim index 3032a34a3..23590d958 100644 --- a/tests/misc/tstrace.nim +++ b/tests/misc/tstrace.nim @@ -1,16 +1,16 @@ -# Test the new stacktraces (great for debugging!) - -{.push stack_trace: on.} - -proc recTest(i: int) = - # enter - if i < 10: - recTest(i+1) - else: # should printStackTrace() - var p: ptr int = nil - p[] = 12 - # leave - -{.pop.} - -recTest(0) +# Test the new stacktraces (great for debugging!) + +{.push stack_trace: on.} + +proc recTest(i: int) = + # enter + if i < 10: + recTest(i+1) + else: # should printStackTrace() + var p: ptr int = nil + p[] = 12 + # leave + +{.pop.} + +recTest(0) diff --git a/tests/misc/tstrange.nim b/tests/misc/tstrange.nim index 8742011bb..fee0f44e4 100644 --- a/tests/misc/tstrange.nim +++ b/tests/misc/tstrange.nim @@ -4,23 +4,23 @@ discard """ 1 2''' """ -# 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 - +# 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 + # bug #1442 let h=3 for x in 0.. <h.int: diff --git a/tests/misc/tstrdesc.nim b/tests/misc/tstrdesc.nim index 1c2e85b4b..d23160315 100644 --- a/tests/misc/tstrdesc.nim +++ b/tests/misc/tstrdesc.nim @@ -1,14 +1,14 @@ -var - x: array [0..2, int] - -x = [0, 1, 2] - -type - TStringDesc {.final.} = object - len, space: int # len and space without counting the terminating zero - data: array [0..0, char] # for the '\0' character - -var - emptyString {.exportc: "emptyString".}: TStringDesc - - +var + x: array [0..2, int] + +x = [0, 1, 2] + +type + TStringDesc {.final.} = object + len, space: int # len and space without counting the terminating zero + data: array [0..0, char] # for the '\0' character + +var + emptyString {.exportc: "emptyString".}: TStringDesc + + diff --git a/tests/misc/tunsigned64mod.nim b/tests/misc/tunsigned64mod.nim index 9ae0d535a..3007405a2 100644 --- a/tests/misc/tunsigned64mod.nim +++ b/tests/misc/tunsigned64mod.nim @@ -10,3 +10,17 @@ let t1 = v1 mod 2 # works let t2 = 7'u64 mod 2'u64 # works let t3 = v2 mod 2'u64 # Error: invalid type: 'range 0..1(uint64) let t4 = (v2 mod 2'u64).uint64 # works + +# bug #2550 + +var x: uint # doesn't work +echo x mod 2 == 0 + +var y: uint64 # doesn't work +echo y mod 2 == 0 + +var z: uint32 # works +echo z mod 2 == 0 + +var a: int # works +echo a mod 2 == 0 diff --git a/tests/misc/tunsignedinc.nim b/tests/misc/tunsignedinc.nim new file mode 100644 index 000000000..95622156f --- /dev/null +++ b/tests/misc/tunsignedinc.nim @@ -0,0 +1,14 @@ +discard """ + output: '''253''' +""" + +# bug #2427 + +import unsigned + +var x = 0'u8 +dec x # OverflowError +x -= 1 # OverflowError +x = x - 1 # No error + +echo x diff --git a/tests/misc/tunsignedmisc.nim b/tests/misc/tunsignedmisc.nim new file mode 100644 index 000000000..e6a497a3d --- /dev/null +++ b/tests/misc/tunsignedmisc.nim @@ -0,0 +1,68 @@ +import unsigned + +discard """ + errormsg: "number 0x123'u8 out of valid range" +""" + +# Bug #1179 + +# Unsigneds + +# 8 bit +let ref1 = 128'u8 shr 7 +let hex1 = 0x80'u8 shr 7 +let oct1 = 0c200'u8 shr 7 +let dig1 = 0b10000000'u8 shr 7 + +doAssert(ref1 == 1) +doAssert(ref1 == hex1) +doAssert(ref1 == oct1) +doAssert(ref1 == dig1) + +# 16 bit +let ref2 = 32768'u16 shr 15 +let hex2 = 0x8000'u16 shr 15 +let oct2 = 0c100000'u16 shr 15 +let dig2 = 0b1000000000000000'u16 shr 15 + +doAssert(ref2 == 1) +doAssert(ref2 == hex2) +doAssert(ref2 == oct2) +doAssert(ref2 == dig2) + +# 32 bit +let ref3 = 2147483648'u32 shr 31 +let hex3 = 0x80000000'u32 shr 31 +let oct3 = 0c20000000000'u32 shr 31 +let dig3 = 0b10000000000000000000000000000000'u32 shr 31 + +doAssert(ref3 == 1) +doAssert(ref3 == hex3) +doAssert(ref3 == oct3) +doAssert(ref3 == dig3) + +# Below doesn't work for lexer stage errors... +# doAssert(compiles(0xFF'u8) == true) +# doAssert(compiles(0xFFF'u16) == true) +# doAssert(compiles(0x7FFF'i16) == true) + +# doAssert(compiles(0x123'u8) == false) +# doAssert(compiles(0x123'i8) == false) +# doAssert(compiles(0x123123'u16) == false) +# doAssert(compiles(0x123123'i16) == false) + +# Should compile # +let boundOkHex1 = 0xFF'u8 +let boundOkHex2 = 0xFFFF'u16 +let boundOkHex3 = 0x7FFF'i16 + +let boundOkHex4 = 0x80'i8 +let boundOkHex5 = 0xFF'i8 +let boundOkHex6 = 0xFFFF'i16 +let boundOkHex7 = 0x7FFF'i16 + +# Should _not_ compile # +let boundBreakingHex1 = 0x123'u8 +let boundBreakingHex2 = 0x123'i8 +let boundBreakingHex3 = 0x123123'u16 +let boundBreakingHex4 = 0x123123'i16 diff --git a/tests/misc/tvarious1.nim b/tests/misc/tvarious1.nim index 1f2da2ae5..1d5ad876a 100644 --- a/tests/misc/tvarious1.nim +++ b/tests/misc/tvarious1.nim @@ -9,7 +9,7 @@ Whopie echo len([1_000_000]) #OUT 1 -type +type TArray = array[0..3, int] TVector = distinct array[0..3, int] proc `[]`(v: TVector; idx: int): int = TArray(v)[idx] @@ -22,7 +22,7 @@ import queues type TWidget = object - names: TQueue[string] + names: Queue[string] var w = TWidget(names: initQueue[string]()) @@ -43,6 +43,6 @@ echo value # bug #1334 -var ys = @[4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2] -#var x = int(ys.high / 2) #echo ys[x] # Works +var ys = @[4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2] +#var x = int(ys.high / 2) #echo ys[x] # Works echo ys[int(ys.high / 2)] # Doesn't work diff --git a/tests/misc/tvarnums.nim b/tests/misc/tvarnums.nim index b880cf006..04a1ef53b 100644 --- a/tests/misc/tvarnums.nim +++ b/tests/misc/tvarnums.nim @@ -2,141 +2,141 @@ discard """ file: "tvarnums.nim" output: "Success!" """ -# 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! +# 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: + writeLine(stdout, $i) + writeLine(stdout, toBinary(i)) + writeLine(stdout, toBinary(x)) + +proc t32(i: int32) = + var + b: TBuffer + toVarNum(i, b) + var x = toNum(b) + if x != i: + writeLine(stdout, toBinary(i)) + writeLine(stdout, toBinary(x)) + +proc tm(i: int32) = + var + b: TBuffer + toVarNum64(i, b) + var x = toNum(b) + if x != i: + writeLine(stdout, toBinary(i)) + writeLine(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)) + +writeLine(stdout, "Success!") #OUT Success! |