fixed some tests

This commit is contained in:
Araq 2011-11-19 12:23:03 +01:00
commit d0772feb08
15 changed files with 135 additions and 128 deletions

View file

@ -110,7 +110,7 @@ proc executeCgi(server: var TServer, client: TSocket, path, query: string,
if L.startsWith("content-length:"): if L.startsWith("content-length:"):
var i = len("content-length:") var i = len("content-length:")
while L[i] in Whitespace: inc(i) while L[i] in Whitespace: inc(i)
contentLength = parseInt(copy(L, i)) contentLength = parseInt(substr(L, i))
if contentLength < 0: if contentLength < 0:
badRequest(client) badRequest(client)
@ -176,7 +176,7 @@ proc acceptRequest(server: var TServer, client: TSocket) =
# extract path # extract path
if q >= 0: if q >= 0:
# strip "?..." from path, this may be found in both POST and GET # strip "?..." from path, this may be found in both POST and GET
path = data[1].copy(0, q-1) path = data[1].substr(0, q-1)
else: else:
path = data[1] path = data[1]
# path starts with "/", by adding "." in front of it we serve files from cwd # path starts with "/", by adding "." in front of it we serve files from cwd
@ -187,7 +187,7 @@ proc acceptRequest(server: var TServer, client: TSocket) =
if cmpIgnoreCase(data[0], "GET") == 0: if cmpIgnoreCase(data[0], "GET") == 0:
if q >= 0: if q >= 0:
cgi = true cgi = true
query = data[1].copy(q+1) query = data[1].substr(q+1)
elif cmpIgnoreCase(data[0], "POST") == 0: elif cmpIgnoreCase(data[0], "POST") == 0:
cgi = true cgi = true
meth = reqPost meth = reqPost

View file

@ -7,56 +7,56 @@
# distribution, for details about the copyright. # distribution, for details about the copyright.
# #
## This module contains simple high-level procedures for dealing with the ## This module contains simple high-level procedures for dealing with the
## web. Use cases: ## web. Use cases:
## ##
## * requesting URLs ## * requesting URLs
## * sending and retrieving emails ## * sending and retrieving emails
## * sending and retrieving files from an FTP server ## * sending and retrieving files from an FTP server
## ##
## Currently only requesting URLs is implemented. The implementation depends ## Currently only requesting URLs is implemented. The implementation depends
## on the libcurl library! ## on the libcurl library!
## ##
## **Deprecated since version 0.8.8:** Use the ``httpclient`` module instead. ## **Deprecated since version 0.8.8:** Use the ``httpclient`` module instead.
## ##
{.deprecated.} {.deprecated.}
import libcurl, streams import libcurl, streams
proc curlwrapperWrite(p: pointer, size, nmemb: int, proc curlwrapperWrite(p: pointer, size, nmemb: int,
data: pointer): int {.cdecl.} = data: pointer): int {.cdecl.} =
var stream = cast[PStream](data) var stream = cast[PStream](data)
stream.writeData(stream, p, size*nmemb) stream.writeData(p, size*nmemb)
return size*nmemb return size*nmemb
proc URLretrieveStream*(url: string): PStream = proc URLretrieveStream*(url: string): PStream =
## retrieves the given `url` and returns a stream which one can read from to ## retrieves the given `url` and returns a stream which one can read from to
## obtain the contents. Returns nil if an error occurs. ## obtain the contents. Returns nil if an error occurs.
result = newStringStream() result = newStringStream()
var hCurl = easy_init() var hCurl = easy_init()
if hCurl == nil: return nil if hCurl == nil: return nil
if easy_setopt(hCurl, OPT_URL, url) != E_OK: return nil if easy_setopt(hCurl, OPT_URL, url) != E_OK: return nil
if easy_setopt(hCurl, OPT_WRITEFUNCTION, if easy_setopt(hCurl, OPT_WRITEFUNCTION,
curlwrapperWrite) != E_OK: return nil curlwrapperWrite) != E_OK: return nil
if easy_setopt(hCurl, OPT_WRITEDATA, result) != E_OK: return nil if easy_setopt(hCurl, OPT_WRITEDATA, result) != E_OK: return nil
if easy_perform(hCurl) != E_OK: return nil if easy_perform(hCurl) != E_OK: return nil
easy_cleanup(hCurl) easy_cleanup(hCurl)
proc URLretrieveString*(url: string): TaintedString = proc URLretrieveString*(url: string): TaintedString =
## retrieves the given `url` and returns the contents. Returns nil if an ## retrieves the given `url` and returns the contents. Returns nil if an
## error occurs. ## error occurs.
var stream = newStringStream() var stream = newStringStream()
var hCurl = easy_init() var hCurl = easy_init()
if hCurl == nil: return if hCurl == nil: return
if easy_setopt(hCurl, OPT_URL, url) != E_OK: return if easy_setopt(hCurl, OPT_URL, url) != E_OK: return
if easy_setopt(hCurl, OPT_WRITEFUNCTION, if easy_setopt(hCurl, OPT_WRITEFUNCTION,
curlwrapperWrite) != E_OK: return curlwrapperWrite) != E_OK: return
if easy_setopt(hCurl, OPT_WRITEDATA, stream) != E_OK: return if easy_setopt(hCurl, OPT_WRITEDATA, stream) != E_OK: return
if easy_perform(hCurl) != E_OK: return if easy_perform(hCurl) != E_OK: return
easy_cleanup(hCurl) easy_cleanup(hCurl)
result = stream.data.TaintedString result = stream.data.TaintedString
when isMainModule: when isMainModule:
echo URLretrieveString("http://nimrod.ethexor.com/") echo URLretrieveString("http://nimrod.ethexor.com/")

View file

@ -140,11 +140,11 @@ proc executeCgi(client: TSocket, path, query: string, meth: TRequestMethod) =
dealloc(buf) dealloc(buf)
OSError() OSError()
var inp = process.inputStream var inp = process.inputStream
inp.writeData(inp, buf, contentLength) inp.writeData(buf, contentLength)
dealloc(buf) dealloc(buf)
var outp = process.outputStream var outp = process.outputStream
while running(process) or not outp.atEnd(outp): while running(process) or not atEnd(outp):
var line = outp.readLine() var line = outp.readLine()
send(client, line.string) send(client, line.string)
send(client, wwwNL) send(client, wwwNL)

View file

@ -7,7 +7,7 @@ var
i: int i: int
params = paramCount() params = paramCount()
i = 0 i = 0
writeln(stdout, "This exe: " & getApplicationFilename()) writeln(stdout, "This exe: " & getAppFilename())
writeln(stdout, "Number of parameters: " & $params) writeln(stdout, "Number of parameters: " & $params)
while i <= params: while i <= params:
writeln(stdout, paramStr(i)) writeln(stdout, paramStr(i))

View file

@ -1,5 +1,5 @@
import os import os
# TODO: Rename this module to ``utils``
type type
TStatusEnum* = enum TStatusEnum* = enum
sUnknown = -1, sBuildFailure, sBuildInProgress, sBuildSuccess, sUnknown = -1, sBuildFailure, sBuildInProgress, sBuildSuccess,
@ -51,5 +51,5 @@ proc `$`*(status: TStatusEnum): string =
return "unknown" return "unknown"
proc makeCommitPath*(platform, hash: string): string = proc makeCommitPath*(platform, hash: string): string =
return platform / "nimrod_" & hash.copy(0, 11) # 11 Chars. return platform / "nimrod_" & hash.substr(0, 11) # 11 Chars.

View file

@ -1,6 +0,0 @@
var
a {.deprecated.}: array[0..11, int]
a[8] = 1

View file

@ -1,7 +1,7 @@
# #
import times, os import times, os
var start = getStartMilsecs() var start = epochTime()
os.sleep(1000) os.sleep(1000)
echo getStartMilsecs() - start #OUT 1000 echo epochTime() - start #OUT 1000

View file

@ -52,7 +52,7 @@ var testseq: seq[string] = @[
echo(repr(testseq)) echo(repr(testseq))
var dummy = "hello" var dummy = "hello"
echo(copy(dummy, 2, 3)) echo(substr(dummy, 2, 3))
echo($meC) echo($meC)

View file

@ -1,7 +1,7 @@
# Test wether the bindings at least compile... # Test wether the bindings at least compile...
import import
unicode, cgi, terminal, libcurl, web, unicode, cgi, terminal, libcurl,
parsexml, parseopt, parsecfg, parsexml, parseopt, parsecfg,
osproc, complex, osproc, complex,
sdl, smpeg, sdl_gfx, sdl_net, sdl_mixer, sdl_ttf, sdl, smpeg, sdl_gfx, sdl_net, sdl_mixer, sdl_ttf,

View file

@ -3,19 +3,21 @@
import import
times times
{.warning: "This is only a test warning!".} #{.warning: "This is only a test warning!".}
const
case2 = true
case3 = true
{.define: case2.}
{.define: case3.}
when defined(case1): when defined(case1):
{.hint: "Case 1".} {.hint: "Case 1".}
when defined(case3): when case3:
{.hint: "Case 1.3".} {.hint: "Case 1.3".}
elif defined(case2): elif case2:
{.hint: "Case 2".} {.hint: "Case 2".}
when defined(case3): when case3:
{.hint: "Case 2.3".} {.hint: "Case 2.3".}
elif defined(case3): elif case3:
{.hint: "Case 3".} {.hint: "Case 3".}
else: else:
{.hint: "unknown case".} {.hint: "unknown case".}

View file

@ -2,24 +2,24 @@ discard """
file: "tcopy.nim" file: "tcopy.nim"
output: "TEMP=C:\\Programs\\xyz\\bin" output: "TEMP=C:\\Programs\\xyz\\bin"
""" """
# tests the copy proc # tests the substr proc
import import
strutils strutils
proc main() = proc main() =
const const
example = r"TEMP=C:\Programs\xyz\bin" example = r"TEMP=C:\Programs\xyz\bin"
var var
a, b: string a, b: string
p: int p: int
p = find(example, "=") p = find(example, "=")
a = copy(example, 0, p-1) a = substr(example, 0, p-1)
b = copy(example, p+1) b = substr(example, p+1)
writeln(stdout, a & '=' & b) writeln(stdout, a & '=' & b)
#writeln(stdout, b) #writeln(stdout, b)
main() main()
#OUT TEMP=C:\Programs\xyz\bin #OUT TEMP=C:\Programs\xyz\bin

View file

@ -6,32 +6,32 @@ discard """
import strutils import strutils
iterator tokenize2(s: string, seps: set[char] = Whitespace): tuple[ iterator tokenize2(s: string, seps: set[char] = Whitespace): tuple[
token: string, isSep: bool] = token: string, isSep: bool] =
var i = 0 var i = 0
while i < s.len: while i < s.len:
var j = i var j = i
if s[j] in seps: if s[j] in seps:
while j < s.len and s[j] in seps: inc(j) while j < s.len and s[j] in seps: inc(j)
if j > i: if j > i:
yield (copy(s, i, j-1), true) yield (substr(s, i, j-1), true)
else: else:
while j < s.len and s[j] notin seps: inc(j) while j < s.len and s[j] notin seps: inc(j)
if j > i: if j > i:
yield (copy(s, i, j-1), false) yield (substr(s, i, j-1), false)
i = j i = j
for word, isSep in tokenize2("ta da", whiteSpace): for word, isSep in tokenize2("ta da", whiteSpace):
var titer2TestVar = 0 var titer2TestVar = 0
stdout.write(titer2TestVar) stdout.write(titer2TestVar)
proc wordWrap2(s: string, maxLineWidth = 80, proc wordWrap2(s: string, maxLineWidth = 80,
splitLongWords = true, splitLongWords = true,
seps: set[char] = whitespace, seps: set[char] = whitespace,
newLine = "\n"): string = newLine = "\n"): string =
result = "" result = ""
for word, isSep in tokenize2(s, seps): for word, isSep in tokenize2(s, seps):
var w = 0 var w = 0

View file

@ -749,7 +749,7 @@ proc rawMatch*(s: string, p: TPeg, start: int, c: var TCaptures): int {.
var (a, b) = c.matches[p.index] var (a, b) = c.matches[p.index]
var n: TPeg var n: TPeg
n.kind = succ(pkTerminal, ord(p.kind)-ord(pkBackRef)) n.kind = succ(pkTerminal, ord(p.kind)-ord(pkBackRef))
n.term = s.copy(a, b) n.term = s.substr(a, b)
result = rawMatch(s, n, start, c) result = rawMatch(s, n, start, c)
of pkStartAnchor: of pkStartAnchor:
if c.origStart == start: result = 0 if c.origStart == start: result = 0
@ -767,7 +767,7 @@ proc match*(s: string, pattern: TPeg, matches: var openarray[string],
result = rawMatch(s, pattern, start, c) == len(s) -start result = rawMatch(s, pattern, start, c) == len(s) -start
if result: if result:
for i in 0..c.ml-1: for i in 0..c.ml-1:
matches[i] = copy(s, c.matches[i][0], c.matches[i][1]) matches[i] = substr(s, c.matches[i][0], c.matches[i][1])
proc match*(s: string, pattern: TPeg, proc match*(s: string, pattern: TPeg,
start = 0): bool {.rtl, extern: "npegs$1".} = start = 0): bool {.rtl, extern: "npegs$1".} =
@ -787,7 +787,7 @@ proc matchLen*(s: string, pattern: TPeg, matches: var openarray[string],
result = rawMatch(s, pattern, start, c) result = rawMatch(s, pattern, start, c)
if result >= 0: if result >= 0:
for i in 0..c.ml-1: for i in 0..c.ml-1:
matches[i] = copy(s, c.matches[i][0], c.matches[i][1]) matches[i] = substr(s, c.matches[i][0], c.matches[i][1])
proc matchLen*(s: string, pattern: TPeg, proc matchLen*(s: string, pattern: TPeg,
start = 0): int {.rtl, extern: "npegs$1".} = start = 0): int {.rtl, extern: "npegs$1".} =
@ -916,7 +916,7 @@ proc replacef*(s: string, sub: TPeg, by: string): string {.
else: else:
addf(result, by, caps) addf(result, by, caps)
inc(i, x) inc(i, x)
add(result, copy(s, i)) add(result, substr(s, i))
proc replace*(s: string, sub: TPeg, by = ""): string {. proc replace*(s: string, sub: TPeg, by = ""): string {.
rtl, extern: "npegs$1".} = rtl, extern: "npegs$1".} =
@ -933,7 +933,7 @@ proc replace*(s: string, sub: TPeg, by = ""): string {.
else: else:
addf(result, by, caps) addf(result, by, caps)
inc(i, x) inc(i, x)
add(result, copy(s, i)) add(result, substr(s, i))
proc parallelReplace*(s: string, subs: openArray[ proc parallelReplace*(s: string, subs: openArray[
tuple[pattern: TPeg, repl: string]]): string {. tuple[pattern: TPeg, repl: string]]): string {.
@ -954,7 +954,7 @@ proc parallelReplace*(s: string, subs: openArray[
add(result, s[i]) add(result, s[i])
inc(i) inc(i)
# copy the rest: # copy the rest:
add(result, copy(s, i)) add(result, substr(s, i))
proc transformFile*(infile, outfile: string, proc transformFile*(infile, outfile: string,
subs: openArray[tuple[pattern: TPeg, repl: string]]) {. subs: openArray[tuple[pattern: TPeg, repl: string]]) {.
@ -1003,7 +1003,7 @@ iterator split*(s: string, sep: TPeg): string =
x = matchLen(s, sep, last) x = matchLen(s, sep, last)
if x > 0: break if x > 0: break
if first < last: if first < last:
yield copy(s, first, last-1) yield substr(s, first, last-1)
proc split*(s: string, sep: TPeg): seq[string] {. proc split*(s: string, sep: TPeg): seq[string] {.
rtl, extern: "npegs$1".} = rtl, extern: "npegs$1".} =
@ -1701,7 +1701,7 @@ when isMainModule:
assert rawMatch(s, expr.rule, 0, c) == len(s) assert rawMatch(s, expr.rule, 0, c) == len(s)
var a = "" var a = ""
for i in 0..c.ml-1: for i in 0..c.ml-1:
a.add(copy(s, c.matches[i][0], c.matches[i][1])) a.add(substr(s, c.matches[i][0], c.matches[i][1]))
assert a == "abcdef" assert a == "abcdef"
#echo expr.rule #echo expr.rule

View file

@ -106,17 +106,17 @@ proc TimeConstruction(depth: int) =
iNumIters = NumIters(depth) iNumIters = NumIters(depth)
echo("Creating " & $iNumIters & " trees of depth " & $depth) echo("Creating " & $iNumIters & " trees of depth " & $depth)
t = getStartMilsecs() t = epochTime()
for i in 0..iNumIters-1: for i in 0..iNumIters-1:
new(tempTree) new(tempTree)
Populate(depth, tempTree) Populate(depth, tempTree)
tempTree = nil tempTree = nil
echo("\tTop down construction took " & $(getStartMilsecs() - t) & "msecs") echo("\tTop down construction took " & $(epochTime() - t) & "msecs")
t = getStartMilsecs() t = epochTime()
for i in 0..iNumIters-1: for i in 0..iNumIters-1:
tempTree = MakeTree(depth) tempTree = MakeTree(depth)
tempTree = nil tempTree = nil
echo("\tBottom up construction took " & $(getStartMilsecs() - t) & "msecs") echo("\tBottom up construction took " & $(epochTime() - t) & "msecs")
type type
tMyArray = seq[float] tMyArray = seq[float]
@ -130,7 +130,7 @@ proc main() =
echo("Garbage Collector Test") echo("Garbage Collector Test")
echo(" Stretching memory with a binary tree of depth " & $kStretchTreeDepth) echo(" Stretching memory with a binary tree of depth " & $kStretchTreeDepth)
PrintDiagnostics() PrintDiagnostics()
t = getStartMilsecs() t = epochTime()
# Stretch the memory space quickly # Stretch the memory space quickly
tempTree = MakeTree(kStretchTreeDepth) tempTree = MakeTree(kStretchTreeDepth)
@ -160,7 +160,7 @@ proc main() =
# fake reference to LongLivedTree # fake reference to LongLivedTree
# and array to keep them from being optimized away # and array to keep them from being optimized away
var elapsed = getStartMilsecs() - t var elapsed = epochTime() - t
PrintDiagnostics() PrintDiagnostics()
echo("Completed in " & $elapsed & "ms. Success!") echo("Completed in " & $elapsed & "ms. Success!")

11
tests/reject/tdeprecated.nim Executable file
View file

@ -0,0 +1,11 @@
discard """
line: 9
errmsg: "'a' is deprecated [Deprecated]"
"""
var
a {.deprecated.}: array[0..11, int]
a[8] = 1