make tests green

This commit is contained in:
Araq 2014-08-31 15:15:26 +02:00
commit 30823c1ce3
44 changed files with 415 additions and 403 deletions

View file

@ -6,13 +6,13 @@ import
proc main() =
var
a, b: TTime
a = getLastModificationTime(ParamStr(1))
b = getLastModificationTime(ParamStr(2))
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)])
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)])
write(stdout, "$1 is newer than $2\n" % [paramStr(1), paramStr(2)])
main()

View file

@ -31,7 +31,7 @@ proc TestLoops() =
break
break
while True:
while true:
break

View file

@ -5,16 +5,16 @@ discard """
"""
# BUG: following compiles, but should not:
proc nodeOfDegree(x: Int): bool =
proc nodeOfDegree(x: int): bool =
result = false
proc main =
for j in 0..2:
for i in 0..10:
if not nodeOfDegree(1) >= 0: #ERROR_MSG type mismatch
Echo "Yes"
echo "Yes"
else:
Echo "No"
echo "No"
main()

View file

@ -14,10 +14,10 @@ proc mypos(sub, s: string, start: int = 0): int =
if i >= N:
result = -1
else:
while True:
while true:
if s[i] == sub[j]:
Inc(i)
Inc(j)
inc(i)
inc(j)
else:
i = i - j + 1
j = 0

View file

@ -24,7 +24,7 @@ else:
var
s: string
write(stdout, "compiled at " & system.compileDate &
" " & compileTime & "\n")
write(stdout, "compiled at " & system.CompileDate &
" " & CompileTime & "\n")
echo getDateStr()
echo getClockStr()

View file

@ -4,7 +4,7 @@
## We use a radix tree with node compression.
## There are two node kinds:
const bitsPerUnit = 8*sizeof(int)
const BitsPerUnit = 8*sizeof(int)
type
TRadixNodeKind = enum rnLinear, rnFull, rnLeafBits, rnLeafLinear
@ -42,13 +42,13 @@ proc testBit(w, i: int): bool {.inline.} =
result = (w and (1 shl (i %% BitsPerUnit))) != 0
proc setBit(w: var int, i: int) {.inline.} =
w = w or (1 shl (i %% bitsPerUnit))
w = w or (1 shl (i %% BitsPerUnit))
proc resetBit(w: var int, i: int) {.inline.} =
w = w and not (1 shl (i %% bitsPerUnit))
w = w and not (1 shl (i %% BitsPerUnit))
proc testOrSetBit(w: var int, i: int): bool {.inline.} =
var x = (1 shl (i %% bitsPerUnit))
var x = (1 shl (i %% BitsPerUnit))
if (w and x) != 0: return true
w = w or x
@ -78,7 +78,7 @@ proc exclLeaf(r: PRadixNode, a: int) =
return
else: assert(false)
proc contains*(r: PRadixNode, a: TAddress): bool =
proc contains*(r: PRadixNode, a: ByteAddress): bool =
if r == nil: return false
var x = searchInner(r, a shr 24 and 0xff)
if x == nil: return false
@ -88,7 +88,7 @@ proc contains*(r: PRadixNode, a: TAddress): bool =
if x == nil: return false
return searchLeaf(x, a and 0xff)
proc excl*(r: PRadixNode, a: TAddress): bool =
proc excl*(r: PRadixNode, a: ByteAddress): bool =
if r == nil: return false
var x = searchInner(r, a shr 24 and 0xff)
if x == nil: return false
@ -167,10 +167,10 @@ proc addInner(r: var PRadixNode, a: int, d: int): bool =
return addInner(x.b[k], a, d-8)
else: assert(false)
proc incl*(r: var PRadixNode, a: TAddress) {.inline.} =
proc incl*(r: var PRadixNode, a: ByteAddress) {.inline.} =
discard addInner(r, a, 24)
proc testOrIncl*(r: var PRadixNode, a: TAddress): 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] =
@ -204,7 +204,7 @@ iterator leafElements(r: PRadixNode): int =
yield ze(r.keys[i])
else: assert(false)
iterator elements*(r: PRadixNode): TAddress {.inline.} =
iterator elements*(r: PRadixNode): ByteAddress {.inline.} =
for p1, n1 in innerElements(r):
for p2, n2 in innerElements(n1):
for p3, n3 in innerElements(n2):
@ -297,7 +297,7 @@ when false:
result = ze(r.keys[i.x])
inc(i.x)
iterator elements(r: PRadixNode): TAddress {.inline.} =
iterator elements(r: PRadixNode): ByteAddress {.inline.} =
var
a, b, c, d: TRadixIter
init(a, r)

View file

@ -2,11 +2,11 @@
# Macintosh, Unix or Windows text format.
var
inp: TFile
inp: File
line: string
if open(inp, "readme.txt"):
while not EndOfFile(inp):
while not endOfFile(inp):
line = readLine(inp)
echo("#" & line & "#")
close(inp)

View file

@ -4,11 +4,7 @@ discard """
import hashes, math
when defined(shallowADT):
{.pragma: myShallow, shallow.}
else:
{.pragma: myShallow.}
{.pragma: myShallow.}
type
TSlotEnum = enum seEmpty, seFilled, seDeleted
@ -63,7 +59,7 @@ template rawInsertImpl() =
data[h].val = val
data[h].slot = seFilled
proc RawGet[A, B](t: TTable[A, B], key: A): int =
proc rawGet[A, B](t: TTable[A, B], key: A): int =
rawGetImpl()
proc `[]`*[A, B](t: TTable[A, B], key: A): B =
@ -71,31 +67,31 @@ proc `[]`*[A, B](t: TTable[A, B], key: A): B =
## default empty value for the type `B` is returned
## and no exception is raised. One can check with ``hasKey`` whether the key
## exists.
var index = RawGet(t, key)
var index = rawGet(t, key)
if index >= 0: result = t.data[index].val
proc hasKey*[A, B](t: TTable[A, B], key: A): bool =
## returns true iff `key` is in the table `t`.
result = rawGet(t, key) >= 0
proc RawInsert[A, B](t: var TTable[A, B], data: var TKeyValuePairSeq[A, B],
proc rawInsert[A, B](t: var TTable[A, B], data: var TKeyValuePairSeq[A, B],
key: A, val: B) =
rawInsertImpl()
proc Enlarge[A, B](t: var TTable[A, B]) =
proc enlarge[A, B](t: var TTable[A, B]) =
var n: TKeyValuePairSeq[A, B]
newSeq(n, len(t.data) * growthFactor)
for i in countup(0, high(t.data)):
if t.data[i].slot == seFilled: RawInsert(t, n, t.data[i].key, t.data[i].val)
if t.data[i].slot == seFilled: rawInsert(t, n, t.data[i].key, t.data[i].val)
swap(t.data, n)
template PutImpl() =
var index = RawGet(t, key)
template putImpl() =
var index = rawGet(t, key)
if index >= 0:
t.data[index].val = val
else:
if mustRehash(len(t.data), t.counter): Enlarge(t)
RawInsert(t, t.data, key, val)
if mustRehash(len(t.data), t.counter): enlarge(t)
rawInsert(t, t.data, key, val)
inc(t.counter)
proc `[]=`*[A, B](t: var TTable[A, B], key: A, val: B) =
@ -104,7 +100,7 @@ proc `[]=`*[A, B](t: var TTable[A, B], key: A, val: B) =
proc del*[A, B](t: var TTable[A, B], key: A) =
## deletes `key` from hash table `t`.
var index = RawGet(t, key)
var index = rawGet(t, key)
if index >= 0:
t.data[index].slot = seDeleted
dec(t.counter)
@ -183,24 +179,24 @@ proc hasKey*[A](t: TCountTable[A], key: A): bool =
## returns true iff `key` is in the table `t`.
result = rawGet(t, key) >= 0
proc RawInsert[A](t: TCountTable[A], data: var seq[tuple[key: A, val: int]],
proc rawInsert[A](t: TCountTable[A], data: var seq[tuple[key: A, val: int]],
key: A, val: int) =
var h: THash = hash(key) and high(data)
while data[h].val != 0: h = nextTry(h, high(data))
data[h].key = key
data[h].val = val
proc Enlarge[A](t: var TCountTable[A]) =
proc enlarge[A](t: var TCountTable[A]) =
var n: seq[tuple[key: A, val: int]]
newSeq(n, len(t.data) * growthFactor)
for i in countup(0, high(t.data)):
if t.data[i].val != 0: RawInsert(t, n, t.data[i].key, t.data[i].val)
if t.data[i].val != 0: rawInsert(t, n, t.data[i].key, t.data[i].val)
swap(t.data, n)
proc `[]=`*[A](t: var TCountTable[A], key: A, val: int) =
## puts a (key, value)-pair into `t`. `val` has to be positive.
assert val > 0
PutImpl()
putImpl()
proc initCountTable*[A](initialSize=64): TCountTable[A] =
## creates a new count table that is empty. `initialSize` needs to be
@ -224,11 +220,11 @@ proc inc*[A](t: var TCountTable[A], key: A, val = 1) =
if index >= 0:
inc(t.data[index].val, val)
else:
if mustRehash(len(t.data), t.counter): Enlarge(t)
RawInsert(t, t.data, key, val)
if mustRehash(len(t.data), t.counter): enlarge(t)
rawInsert(t, t.data, key, val)
inc(t.counter)
proc Smallest*[A](t: TCountTable[A]): tuple[key: A, val: int] =
proc smallest*[A](t: TCountTable[A]): tuple[key: A, val: int] =
## returns the largest (key,val)-pair. Efficiency: O(n)
assert t.len > 0
var minIdx = 0
@ -237,7 +233,7 @@ proc Smallest*[A](t: TCountTable[A]): tuple[key: A, val: int] =
result.key = t.data[minIdx].key
result.val = t.data[minIdx].val
proc Largest*[A](t: TCountTable[A]): tuple[key: A, val: int] =
proc largest*[A](t: TCountTable[A]): tuple[key: A, val: int] =
## returns the (key,val)-pair with the largest `val`. Efficiency: O(n)
assert t.len > 0
var maxIdx = 0

View file

@ -74,7 +74,7 @@ proc toNum(b: TBuffer): int32 =
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
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``