summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/cairotest.nim14
-rw-r--r--tests/cgitest.nim15
-rw-r--r--tests/stckovfl.nim8
-rw-r--r--tests/sunset.tmpl67
-rw-r--r--tests/tambsym2.nim18
-rw-r--r--tests/tambsym3.nim8
-rw-r--r--tests/tester.nim161
-rw-r--r--tests/tints.nim41
-rw-r--r--tests/tisopr.nim20
-rw-r--r--tests/tlibs.nim19
-rw-r--r--tests/topenlen.nim12
-rw-r--r--tests/tos.nim12
-rw-r--r--tests/trectype.nim2
-rw-r--r--tests/tsets.nim8
-rw-r--r--tests/tstreams.nim7
-rw-r--r--tests/ttempl.nim27
-rw-r--r--tests/tvarnums.nim8
-rw-r--r--tests/typalias.nim15
-rw-r--r--tests/x11test.nim71
19 files changed, 526 insertions, 7 deletions
diff --git a/tests/cairotest.nim b/tests/cairotest.nim
new file mode 100644
index 000000000..2c28e1abf
--- /dev/null
+++ b/tests/cairotest.nim
@@ -0,0 +1,14 @@
+import cairo
+
+var surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 240, 80)
+var cr = cairo_create(surface)
+
+cairo_select_font_face(cr, "serif", CAIRO_FONT_SLANT_NORMAL, 
+                                    CAIRO_FONT_WEIGHT_BOLD)
+cairo_set_font_size(cr, 32.0)
+cairo_set_source_rgb(cr, 0.0, 0.0, 1.0)
+cairo_move_to(cr, 10.0, 50.0)
+cairo_show_text(cr, "Hello, world")
+cairo_destroy(cr)
+discard cairo_surface_write_to_png(surface, "hello.png")
+cairo_surface_destroy(surface)
diff --git a/tests/cgitest.nim b/tests/cgitest.nim
new file mode 100644
index 000000000..ef115c80b
--- /dev/null
+++ b/tests/cgitest.nim
@@ -0,0 +1,15 @@
+# Test the new CGI module
+import strtabs, cgi
+
+
+#setTestData("name", "the andreas", "password", "rumpf\t\ttab")
+
+var myData = readData()
+validateData(myData, "name", "password")
+writeContentType()
+
+write(stdout, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">\n")
+write(stdout, "<html><head><title>Test</title></head><body>\n")
+writeln(stdout, "name: " & myData["name"])
+writeln(stdout, "password: " & myData["password"])
+writeln(stdout, "</body></html>")
diff --git a/tests/stckovfl.nim b/tests/stckovfl.nim
new file mode 100644
index 000000000..799fe0001
--- /dev/null
+++ b/tests/stckovfl.nim
@@ -0,0 +1,8 @@
+# To test stack overflow message
+
+proc over(a: int): int = 
+  if a == high(int): return
+  result = over(a+1)+5
+  
+Echo($over(0))
+
diff --git a/tests/sunset.tmpl b/tests/sunset.tmpl
new file mode 100644
index 000000000..08fa50f56
--- /dev/null
+++ b/tests/sunset.tmpl
@@ -0,0 +1,67 @@
+#proc sunsetTemplate*(current, ticker, content: string,
+#                     tabs: openarray[array[0..1, string]]): string = 
+#  result = ""
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
+
+<head>
+  <title>Nimrod Programming System</title>
+  <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
+  <link rel="stylesheet" type="text/css" href="style/style.css" />
+</head>
+
+<body>
+  <div id="main">
+    <div id="links">
+      <!-- **** INSERT LINKS HERE **** -->
+    </div>
+    <div id="logo"><h1>Nimrod Programming System</h1></div>
+    <div id="content">
+      <div id="menu">
+        <ul>
+  #for item in items(tabs):
+    #var name = item[0]
+    #var t = item[1]
+    #if t == current:
+          <li><a id="selected" href="${t}.html" title = "Nimrod - $name">$name</a></li>
+    #else:
+          <li><a               href="${t}.html" title = "Nimrod - $name">$name</a></li>
+    #end if
+  #end for
+        </ul>
+      </div>
+      <div id="column1">
+        <div class="sidebaritem">
+          <div class="sbihead">
+            <h1>latest news</h1>
+          </div>
+          <div class="sbicontent">
+            $ticker
+          </div>
+        </div>
+        <div class="sidebaritem">
+          <div class="sbihead">
+            <h1>additional links</h1>
+          </div>
+          <div class="sbilinks">
+            <!-- **** INSERT ADDITIONAL LINKS HERE **** -->
+            <ul>
+              <li><a class="reference" href="http://llvm.org">LLVM</a></li>
+              <li><a class="reference" href="http://gcc.gnu.org">GCC</a></li>
+            </ul>
+          </div>
+        </div>
+      </div>
+      <div id="column2">
+      $content
+      </div>
+    </div>
+    <div id="footer">
+      copyright &copy; 2008 Andreas Rumpf | Last update: ${getDateStr()}
+      | <a class="reference" href="http://validator.w3.org/check?uri=referer">XHTML 1.1</a>
+      | <a class="reference" href="http://jigsaw.w3.org/css-validator/check/referer">CSS</a>
+      | <a class="reference" href="http://www.dcarter.co.uk">design by dcarter</a>
+    </div>
+  </div>
+</body>
+</html>
diff --git a/tests/tambsym2.nim b/tests/tambsym2.nim
new file mode 100644
index 000000000..9178182aa
--- /dev/null
+++ b/tests/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/tambsym3.nim b/tests/tambsym3.nim
new file mode 100644
index 000000000..5d0a5f6b2
--- /dev/null
+++ b/tests/tambsym3.nim
@@ -0,0 +1,8 @@
+# Test ambigious symbols

+

+import mambsym1, times

+

+var

+  v = mDec #ERROR_MSG ambigious identifier

+

+writeln(stdout, ord(v))

diff --git a/tests/tester.nim b/tests/tester.nim
new file mode 100644
index 000000000..fc75754d5
--- /dev/null
+++ b/tests/tester.nim
@@ -0,0 +1,161 @@
+#
+#
+#            Nimrod Tester
+#        (c) Copyright 2008 Andreas Rumpf
+#
+#    See the file "copying.txt", included in this
+#    distribution, for details about the copyright.
+#
+
+## This program verifies Nimrod against the testcases.
+## The testcases may contain the directives '#ERROR' or '#ERROR_IN'.
+## '#ERROR' is used to indicate that the compiler should report
+## an error in the marked line (the line that contains the '#ERROR'
+## directive.)
+## The format for '#ERROR_IN' is:
+##      #ERROR_IN filename linenumber
+## One can omit the extension of the filename ('.nim' is then assumed).
+## Tests which contain none of the two directives should compile. Thus they
+## are executed after successful compilation and their output is verified
+## against the results specified with the '#OUT' directive.
+## (Tests which require user interaction are not possible.)
+## Tests can have an #ERROR_MSG directive specifiying the error message
+## Nimrod shall produce.
+
+import
+  strutils, regexprs, os, osproc, streams
+
+const
+  cmdTemplate = r"nimrod cc --hints:on $options $filename"
+
+type
+  TSpec = object of TObject ## specification object
+    line: int    ## line number where compiler should throw an error
+    file: string ## file where compiler should throw an error
+    err: bool    ## true if the specification says there should be an error
+    outp: string ## output that should be produced
+    puremsg: string ## pure message of compiler
+
+proc myExec(cmd: string): string =
+  #echo("Executing: " & cmd)
+  result = osproc.executeProcess(cmd)
+  #echo("Received: " & result)
+
+proc parseTest(filename: string): TSpec =
+  var i = 0 # the line counter
+  var matches: array [0..2, string]
+  result.outp = ""
+  result.puremsg = ""
+  result.file = filename
+  for s in lines(filename):
+    inc(i)
+    if find(s, r"\#OUT\s*(.*)", matches):
+      result.outp = matches[1]
+      break
+    if find(s, r"\#ERROR_IN\s*(\S*)\s*(\d+)", matches):
+      result.file = matches[1]
+      result.line = parseInt(matches[2])
+      result.err = true
+      break
+    if find(s, r"\#ERROR_MSG\s*(.*)", matches):
+      result.line = i
+      result.outp = matches[1]
+      result.err = True
+      break
+    if find(s, r"\#ERROR$", matches):
+      result.line = i
+      result.err = true
+      break
+
+proc callCompiler(filename, options: string): TSpec =
+  var c = parseCmdLine(cmdTemplate % ["filename", filename, "options", options])
+  var a: seq[string] = @[] # slicing is not yet implemented :-(
+  for i in 1 .. c.len-1: add(a, c[i])
+  var p = startProcess(command=c[0], args=a,
+                       options={poStdErrToStdOut, poUseShell})
+  var outp = p.outputStream
+  while running(p) or not outp.atEnd(outp):
+    var s = outp.readLine()
+    var matches: array [0..3, string]
+    result.outp = ""
+    result.puremsg = ""
+    result.file = ""
+    result.err = true
+    if match(s, r"(.*)\((\d+), \d+\) Error\: (.*)", matches):
+      result.file = matches[1]
+      result.line = parseInt(matches[2])
+      result.outp = matches[0]
+      result.puremsg = matches[3]
+      break
+    elif match(s, r"Error\: (.*)", matches):
+      result.puremsg = matches[1]
+      result.outp = matches[0]
+      result.line = 1
+      break
+    elif match(s, r"Hint\: operation successful", matches):
+      result.outp = matches[0]
+      result.err = false
+      break
+
+proc cmpResults(filename: string, spec, comp: TSpec): bool =
+  # short filename for messages (better readability):
+  var shortfile = os.extractFilename(filename)
+
+  if comp.err and comp.outp == "":
+    # the compiler did not say "[Error]" nor "operation sucessful"
+    Echo("[Tester] $1 -- FAILED; COMPILER BROKEN" % shortfile)
+  elif spec.err != comp.err:
+    Echo(("[Tester] $1 -- FAILED\n" &
+         "Compiler says: $2\n" &
+         "But specification says: $3") %
+         [shortfile, comp.outp, spec.outp])
+  elif spec.err:
+    if extractFilename(comp.file) != extractFilename(spec.file):
+      Echo(("[Tester] $1 -- FAILED: file names do not match:\n" &
+           "Compiler: $2\nSpec: $3") % [shortfile, comp.file, spec.file])
+    elif strip(spec.outp) notin strip(comp.puremsg):
+      Echo(("[Tester] $1 -- FAILED: error messages do not match:\n" &
+           "Compiler: $2\nSpec: $3") % [shortfile, comp.pureMsg, spec.outp])
+    elif comp.line != spec.line:
+      Echo(("[Tester] $1 -- FAILED: line numbers do not match:\n" &
+           "Compiler: $2\nSpec: $3") % [shortfile, $comp.line, $spec.line])
+    else:
+      Echo("[Tester] $1 -- OK" % shortfile)
+      result = true
+  else:
+    # we have to run the executable and check its output:
+    var exeFile = changeFileExt(filename, ExeExt)
+    if ExistsFile(exeFile):
+      if len(spec.outp) == 0:
+        # we have no output to validate against, but compilation succeeded,
+        # so it's okay:
+        Echo("[Tester] $1 -- OK" % shortfile)
+        result = true
+      else:
+        var buf = myExec(exeFile)
+        result = strip(buf) == strip(spec.outp)
+        if result:
+          Echo("[Tester] $1 -- compiled program OK" % shortfile)
+        else:
+          Echo("[Tester] $1 -- compiled program FAILED" % shortfile)
+    else:
+      Echo("[Tester] $1 -- FAILED; executable not found" % shortfile)
+
+proc main(options: string) =
+  # runs the complete testsuite
+  var total = 0
+  var passed = 0
+  for filename in os.walkFiles("tests/t*.nim"):
+    if extractFilename(filename) == "tester.nim": continue
+    var spec = parseTest(filename)
+    var comp = callCompiler(filename, options)
+    if cmpResults(filename, spec, comp): inc(passed)
+    inc(total)
+  Echo("[Tester] $1/$2 tests passed\n" % [$passed, $total])
+
+var
+  options = ""
+for i in 1.. paramCount():
+  add(options, " ")
+  add(options, paramStr(i))
+main(options)
diff --git a/tests/tints.nim b/tests/tints.nim
new file mode 100644
index 000000000..f2b52c134
--- /dev/null
+++ b/tests/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/tisopr.nim b/tests/tisopr.nim
new file mode 100644
index 000000000..d52859b09
--- /dev/null
+++ b/tests/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/tlibs.nim b/tests/tlibs.nim
new file mode 100644
index 000000000..317430744
--- /dev/null
+++ b/tests/tlibs.nim
@@ -0,0 +1,19 @@
+# Test wether the bindings at least compile...
+
+import
+  osproc, zipfiles,
+  sdl, smpeg, sdl_gfx, sdl_net, sdl_mixer, sdl_ttf,
+  sdl_image, sdl_mixer_nosmpeg
+  cursorfont, xatom, xf86vmode, xkb, xrandr, xshm, xvlib, keysym, xcms, xi,
+  xkblib, xrender, xutil, x, xf86dga, xinerama, xlib, xresource, xv,
+  gtk2, glib2, pango, gdk2,
+  cairowin32, cairoxlib,
+  odbcsql,
+  gl, glut, glu, glx, glext, wingl,
+  zlib
+
+import "lib/base/lua/lua"
+import "lib/base/lua/lualib"
+import "lib/base/lua/lauxlib"
+
+writeln(stdout, "test compilation of binding modules")
diff --git a/tests/topenlen.nim b/tests/topenlen.nim
new file mode 100644
index 000000000..b9d7fbc2d
--- /dev/null
+++ b/tests/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/tos.nim b/tests/tos.nim
new file mode 100644
index 000000000..9ab4295f8
--- /dev/null
+++ b/tests/tos.nim
@@ -0,0 +1,12 @@
+# test some things of the os module
+
+import os
+
+proc walkDirTree(root: string) = 
+  for k, f in walkDir(root):
+    case k 
+    of pcFile, pcLinkToFile: echo(f)
+    of pcDirectory: walkDirTree(f)
+    of pcLinkToDirectory: nil
+
+walkDirTree(".")
diff --git a/tests/trectype.nim b/tests/trectype.nim
index 8e68767b5..3161b0255 100644
--- a/tests/trectype.nim
+++ b/tests/trectype.nim
@@ -13,3 +13,5 @@ type
   PB = ref TB

   TB = array [0..3, P1]

   T1 = array [0..6, PB]

+#ERROR_MSG internal error: cannot generate C type for: PA
+
diff --git a/tests/tsets.nim b/tests/tsets.nim
index 2a01292f3..08ab3e54b 100644
--- a/tests/tsets.nim
+++ b/tests/tsets.nim
@@ -34,7 +34,8 @@ type
   TTokTypes* = set[TTokTypeRange]

 

 const

-  toktypes: TTokTypes = {tkSymbol..pred(tkIntLit), tkStrLit..tkTripleStrLit}

+  toktypes: TTokTypes = {TTokTypeRange(tkSymbol)..pred(tkIntLit), 
+                         tkStrLit..tkTripleStrLit}

 

 var

   s: set[char]

@@ -46,11 +47,12 @@ 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: write(stdout, 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:

-    writeln(stdout, "the token '$1' is in the set" % repr(x))

+    nil
+    #writeln(stdout, "the token '$1' is in the set" % repr(x))

 

 #OUT Ha ein F ist in s!

diff --git a/tests/tstreams.nim b/tests/tstreams.nim
new file mode 100644
index 000000000..68ca8eeeb
--- /dev/null
+++ b/tests/tstreams.nim
@@ -0,0 +1,7 @@
+import streams
+
+var outp = newFileStream(stdout)
+var inp = newFileStream(stdin)
+write(outp, "Hallo! What is your name?")
+var line = readLine(inp)
+write(outp, "nice name: " & line)
diff --git a/tests/ttempl.nim b/tests/ttempl.nim
new file mode 100644
index 000000000..dcf096671
--- /dev/null
+++ b/tests/ttempl.nim
@@ -0,0 +1,27 @@
+# Test the new template file mechanism
+
+import
+  os, times
+
+include "sunset.tmpl"
+
+const
+  tabs = [["home", "index"],
+          ["news", "news"],
+          ["documentation", "documentation"],
+          ["download", "download"],
+          ["FAQ", "question"],
+          ["links", "links"]]
+
+
+var i = 0
+for item in items(tabs): 
+  var content = $i
+  var file: TFile
+  if openFile(file, changeFileExt(item[1], "html"), fmWrite): 
+    write(file, sunsetTemplate(current=item[1], ticker="", content=content,
+                               tabs=tabs))
+    closeFile(file)
+  else:
+    write(stdout, "cannot open file for writing")
+  inc(i)
diff --git a/tests/tvarnums.nim b/tests/tvarnums.nim
index 5079e0e16..1b683ad94 100644
--- a/tests/tvarnums.nim
+++ b/tests/tvarnums.nim
@@ -66,13 +66,13 @@ proc toNum(b: TBuffer): int32 =
   result = ze(b[0]) and 63

   var

     i = 0

-    Shift = 6

+    Shift = 6'i32

   while (ze(b[i]) and 128) != 0:

     inc(i)

-    result = result or int32((ze(b[i]) and 127) shl Shift)

-    inc(Shift, 7)

+    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 int(result) +% 1

+    result = (not result) +% 1'i32

     # this is the same as ``- result``

     # but gives no overflow error for low(int)

 

diff --git a/tests/typalias.nim b/tests/typalias.nim
new file mode 100644
index 000000000..ba9f38ed9
--- /dev/null
+++ b/tests/typalias.nim
@@ -0,0 +1,15 @@
+
+type
+  TMyObj = TYourObj
+  TYourObj = object of TObject
+    x, y: int
+  
+proc init: TYourObj =
+  result.x = 0
+  result.y = -1
+  
+proc f(x: var TYourObj) =
+  nil
+  
+var m: TMyObj = init()
+f(m)
diff --git a/tests/x11test.nim b/tests/x11test.nim
new file mode 100644
index 000000000..2769b6c74
--- /dev/null
+++ b/tests/x11test.nim
@@ -0,0 +1,71 @@
+import xlib, xutil, x, keysym
+
+const
+  WINDOW_WIDTH = 400
+  WINDOW_HEIGHT = 300
+  
+var
+  width, height: int
+  display: PDisplay
+  screen: cint
+  depth: int
+  win: TWindow
+  sizeHints: TXSizeHints
+
+proc create_window = 
+  width = WINDOW_WIDTH
+  height = WINDOW_HEIGHT
+
+  display = XOpenDisplay(nil)
+  if display == nil:
+    echo("Verbindung zum X-Server fehlgeschlagen")
+    quit(1)
+
+  screen = XDefaultScreen(display)
+  depth = XDefaultDepth(display, screen)
+  var rootwin = XRootWindow(display, screen)
+  win = XCreateSimpleWindow(display, rootwin, 100, 10,
+                            width, height, 5,
+                            XBlackPixel(display, screen),
+                            XWhitePixel(display, screen))
+  size_hints.flags = PSize or PMinSize or PMaxSize
+  size_hints.min_width =  width
+  size_hints.max_width =  width
+  size_hints.min_height = height
+  size_hints.max_height = height
+  discard XSetStandardProperties(display, win, "Simple Window", "window",
+                         0, nil, 0, addr(size_hints))
+  discard XSelectInput(display, win, ButtonPressMask or KeyPressMask or 
+                                     PointerMotionMask)
+  discard XMapWindow(display, win)
+
+proc close_window =
+  discard XDestroyWindow(display, win)
+  discard XCloseDisplay(display)
+    
+var
+  xev: TXEvent
+
+proc process_event =
+  var key: TKeySym
+  case int(xev.theType)
+  of KeyPress:
+    key = XLookupKeysym(cast[ptr TXKeyEvent](addr(xev)), 0)
+    if key != 0:
+      echo("keyboard event")
+  of ButtonPressMask, PointerMotionMask:
+    Echo("Mouse event")
+  else: nil
+
+proc eventloop =
+  discard XFlush(display)
+  var num_events = int(XPending(display))
+  while num_events != 0:
+     dec(num_events)
+     discard XNextEvent(display, addr(xev))
+     process_event()
+
+create_window()
+while true:
+  eventloop()
+close_window()