start of tests refactoring; sqlite3 new wrapper fixes

This commit is contained in:
rumpf_a@web.de 2010-02-21 19:42:36 +01:00
commit d913fdb280
128 changed files with 206 additions and 238 deletions

View file

@ -0,0 +1,13 @@
# test another strange bug ... (I hate this compiler; it is much too buggy!)
proc putEnv(key, val: string) =
# XXX: we have to leak memory here, as we cannot
# free it before the program ends (says Borland's
# documentation)
var
env: ptr array[0..500000, char]
env = cast[ptr array[0..500000, char]](alloc(len(key) + len(val) + 2))
for i in 0..len(key)-1: env[i] = key[i]
env[len(key)] = '='
for i in 0..len(val)-1:
env[len(key)+1+i] = val[i]

View file

@ -0,0 +1,31 @@
# Test the assignment operator for complex types which need RTTI
type
TRec = object
x, y: int
s: string
seq: seq[string]
arr: seq[seq[array[0..3, string]]]
TRecSeq = seq[TRec]
proc test() =
var
a, b: TRec
a.x = 1
a.y = 2
a.s = "Hallo!"
a.seq = @["abc", "def", "ghi", "jkl"]
a.arr = @[]
setLen(a.arr, 4)
a.arr[0] = @[]
a.arr[1] = @[]
b = a # perform a deep copy here!
b.seq = @["xyz", "huch", "was", "soll"]
writeln(stdout, len(a.seq))
writeln(stdout, a.seq[3])
writeln(stdout, len(b.seq))
writeln(stdout, b.seq[3])
writeln(stdout, b.y)
test()

View file

@ -0,0 +1,14 @@
# Test the command line
import
os, strutils
var
i: int
params = paramCount()
i = 0
writeln(stdout, "This exe: " & getApplicationFilename())
writeln(stdout, "Number of parameters: " & $params)
while i <= params:
writeln(stdout, paramStr(i))
i = i + 1

View file

@ -0,0 +1,39 @@
import strutils
type
TColor = distinct int32
proc rgb(r, g, b: range[0..255]): TColor =
result = TColor(r or g shl 8 or b shl 16)
proc `$`(c: TColor): string =
result = "#" & toHex(int32(c), 6)
echo rgb(34, 55, 255)
when false:
type
TColor = distinct int32
TColorComponent = distinct int8
proc red(a: TColor): TColorComponent =
result = TColorComponent(int32(a) and 0xff'i32)
proc green(a: TColor): TColorComponent =
result = TColorComponent(int32(a) shr 8'i32 and 0xff'i32)
proc blue(a: TColor): TColorComponent =
result = TColorComponent(int32(a) shr 16'i32 and 0xff'i32)
proc rgb(r, g, b: range[0..255]): TColor =
result = TColor(r or g shl 8 or b shl 8)
proc `+!` (a, b: TColorComponent): TColorComponent =
## saturated arithmetic:
result = TColorComponent(min(ze(int8(a)) + ze(int8(b)), 255))
proc `+` (a, b: TColor): TColor =
## saturated arithmetic for colors makes sense, I think:
return rgb(red(a) +! red(b), green(a) +! green(b), blue(a) +! blue(b))
rgb(34, 55, 255)

View file

@ -0,0 +1,28 @@
import strutils
const
HelpText = """
+-----------------------------------------------------------------+
| Maintenance program for Nimrod |
| Version $1|
| (c) 2009 Andreas Rumpf |
+-----------------------------------------------------------------+
Compiled at: $2, $3
Usage:
koch [options] command [options for command]
Options:
--force, -f, -B, -b forces rebuild
--help, -h shows this help and quits
Possible Commands:
boot [options] bootstraps with given command line options
clean cleans Nimrod project; removes generated files
web generates the website
csource [options] builds the C sources for installation
zip builds the installation ZIP package
inno builds the Inno Setup installer
""" % [NimrodVersion & repeatChar(44-len(NimrodVersion)),
CompileDate, CompileTime]
echo helpText

View file

@ -0,0 +1,38 @@
import
Cairo
converter FloatConversion64(x: int): float64 = return toFloat(x)
converter FloatConversion32(x: int): float32 = return toFloat(x)
converter FloatConversionPlain(x: int): float = return toFloat(x)
const width = 500
const height = 500
const outFile = "CairoTest.png"
var surface = Cairo_ImageSurfaceCreate(CAIRO_FORMAT_RGB24, width, height)
var ç = Cairo_Create(surface)
ç.Cairo_SetSourceRGB(1, 1, 1)
ç.Cairo_Paint()
ç.Cairo_SetLineWidth(10)
ç.Cairo_SetLineCap(CAIRO_LINE_CAP_ROUND)
const count = 12
var winc = width / count
var hinc = width / count
for i in 1 .. count-1:
var amount = i / count
ç.Cairo_SetSourceRGB(0, 1 - amount, amount)
ç.Cairo_MoveTo(i * winc, hinc)
ç.Cairo_LineTo(width - i * winc, height - hinc)
ç.Cairo_Stroke()
ç.Cairo_SetSourceRGB(1 - amount, 0, amount)
ç.Cairo_MoveTo(winc, i * hinc)
ç.Cairo_LineTo(width - winc, height - i * hinc)
ç.Cairo_Stroke()
echo(surface.Cairo_SurfaceWriteToPNG(outFile))
surface.Cairo_SurfaceDestroy()

View file

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

View file

@ -0,0 +1,17 @@
# Test the dialogs module
import dialogs, gtk2
gtk_nimrod_init()
var x = ChooseFilesToOpen(nil)
for a in items(x):
writeln(stdout, a)
info(nil, "start with an info box")
warning(nil, "now a warning ...")
error(nil, "... and an error!")
writeln(stdout, ChooseFileToOpen(nil))
writeln(stdout, ChooseFileToSave(nil))
writeln(stdout, ChooseDir(nil))

View file

@ -0,0 +1,16 @@
import os
proc getDllName: string =
result = "mylib.dll"
if ExistsFile(result): return
result = "mylib2.dll"
if ExistsFile(result): return
quit("could not load dynamic library")
proc myImport(s: cstring) {.cdecl, importc, dynlib: getDllName().}
proc myImport2(s: int) {.cdecl, importc, dynlib: getDllName().}
myImport("test2")
myImport2(12)

View file

@ -0,0 +1,35 @@
# Dump the contents of a PNimrodNode
import macros
proc dumpit(n: PNimrodNode): string {.compileTime.} =
if n == nil: return "nil"
result = $n.kind
add(result, "(")
case n.kind
of nnkEmpty: nil # same as nil node in this representation
of nnkNilLit: add(result, "nil")
of nnkCharLit..nnkInt64Lit: add(result, $n.intVal)
of nnkFloatLit..nnkFloat64Lit: add(result, $n.floatVal)
of nnkStrLit..nnkTripleStrLit: add(result, $n.strVal)
of nnkIdent: add(result, $n.ident)
of nnkSym, nnkNone: assert false
else:
add(result, dumpit(n[0]))
for j in 1..n.len-1:
add(result, ", ")
add(result, dumpit(n[j]))
add(result, ")")
macro dumpAST(n: stmt): stmt =
# dump AST as a side-effect and return the inner node
echo dumpit(n)
result = n[1]
dumpAST:
proc add(x, y: int): int =
return x + y
proc sub(x, y: int): int = return x - y

View file

@ -0,0 +1,3 @@
# Simplest Nimrod program
echo "Hallo, World!"

View file

@ -0,0 +1,3 @@
# test the new endian magic
writeln(stdout, repr(system.cpuEndian))

View file

@ -0,0 +1,8 @@
# Test enums
type
E = enum a, b, c, x, y, z
var
en: E
en = a

View file

@ -0,0 +1,9 @@
# Test 13: forward types
type
PSym = ref TSym
TSym = object
next: PSym
var s: PSym

View file

@ -0,0 +1,22 @@
# Test for a hard to fix internal error
# occured in the SDL library
{.push dynlib: "SDL.dll", callconv: cdecl.}
type
PSDL_semaphore = ptr TSDL_semaphore
TSDL_semaphore {.final.} = object
sem: Pointer #PSem_t;
when not defined(USE_NAMED_SEMAPHORES):
sem_data: int
when defined(BROKEN_SEMGETVALUE):
# This is a little hack for MacOS X -
# It's not thread-safe, but it's better than nothing
sem_value: cint
type
PSDL_Sem = ptr TSDL_Sem
TSDL_Sem = TSDL_Semaphore
proc SDL_CreateSemaphore(initial_value: Int32): PSDL_Sem {.
importc: "SDL_CreateSemaphore".}

View file

@ -0,0 +1,61 @@
import
gtk2, glib2, atk, gdk2, gdk2pixbuf, libglade2, pango,
pangoutils
proc hello(widget: PGtkWidget, data: pointer) {.cdecl.} =
write(stdout, "Hello World\n")
proc delete_event(widget: PGtkWidget, event: PGdkEvent,
data: pointer): bool {.cdecl.} =
# If you return FALSE in the "delete_event" signal handler,
# GTK will emit the "destroy" signal. Returning TRUE means
# you don't want the window to be destroyed.
# This is useful for popping up 'are you sure you want to quit?'
# type dialogs.
write(stdout, "delete event occurred\n")
# Change TRUE to FALSE and the main window will be destroyed with
# a "delete_event".
return false
# Another callback
proc destroy(widget: PGtkWidget, data: pointer) {.cdecl.} =
gtk_main_quit()
proc main() =
# GtkWidget is the storage type for widgets
var
window: PGtkWindow
button: PGtkButton
gtk_nimrod_init()
window = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL))
discard g_signal_connect(window, "delete_event",
Gcallback(delete_event), nil)
discard g_signal_connect(window, "destroy", Gcallback(destroy), nil)
# Sets the border width of the window.
gtk_container_set_border_width(window, 10)
# Creates a new button with the label "Hello World".
button = GTK_BUTTON(gtk_button_new_with_label("Hello World"))
discard g_signal_connect(button, "clicked", Gcallback(hello), nil)
# This will cause the window to be destroyed by calling
# gtk_widget_destroy(window) when "clicked". Again, the destroy
# signal could come from here, or the window manager.
discard g_signal_connect_swapped(button, "clicked",
Gcallback(gtk_widget_destroy), window)
# This packs the button into the window (a gtk container).
gtk_container_add(window, button)
# The final step is to display this newly created widget.
gtk_widget_show(button)
# and the window
gtk_widget_show(window)
gtk_main()
main()

View file

@ -0,0 +1,83 @@
# Hallo
import
os, strutils, macros
type
TMyEnum = enum
meA, meB, meC, meD
when isMainModule:
{.hint: "this is the main file".}
proc fac[T](x: T): T =
# test recursive generic procs
if x <= 1: return 1
else: return x.`*`(fac(x-1))
macro macrotest(n: expr): stmt =
expectKind(n, nnkCall)
expectMinLen(n, 2)
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("")))
macro debug(n: expr): stmt =
result = newNimNode(nnkStmtList, n)
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]))
macrotest(stdout, "finally", 4, 5, "variable", "argument lists")
macrotest(stdout)
#GC_disable()
echo("This was compiled by Nimrod version " & system.nimrodVersion)
writeln(stdout, "Hallo", " World", "!")
echo(["a", "b", "c", "d"].len)
for x in items(["What's", "your", "name", "?", ]):
echo(x)
var `name` = readLine(stdin)
{.breakpoint.}
echo("Hi " & thallo.name & "!\n")
debug(name)
var testseq: seq[string] = @[
"a", "b", "c", "d", "e"
]
echo(repr(testseq))
var dummy = "hallo"
echo(copy(dummy, 2, 3))
echo($meC)
# test tuples:
for x, y in items([(1, 2), (3, 4), (6, 1), (5, 2)]):
echo x
echo y
proc simpleConst(): int = return 34
# test constant evaluation:
const
constEval3 = simpleConst()
constEval = "abc".contains('b')
constEval2 = fac(7)
echo(constEval3)
echo(constEval)
echo(constEval2)
echo(1.`+`(2))
for i in 2..6:
for j in countdown(i+4, 2):
echo(fac(i * j))
when isMainModule:
{.hint: "this is the main file".}

View file

@ -0,0 +1,22 @@
type
TIdObj* = object of TObject
id*: int # unique id; use this for comparisons and not the pointers
PIdObj* = ref TIdObj
PIdent* = ref TIdent
TIdent*{.acyclic.} = object
s*: string
proc myNewString(L: int): string {.inline.} =
result = newString(L)
if result.len == L: echo("Length correct")
else: echo("bug")
for i in 0..L-1:
if result[i] == '\0':
echo("Correct")
else:
echo("Wrong")
var s = myNewString(8)

View file

@ -0,0 +1,24 @@
const romanNumbers1 =
[
("M", 1000), ("D", 500), ("C", 100),
("L", 50), ("X", 10), ("V", 5), ("I", 1) ]
const romanNumbers2 =
[
("M", 1000), ("D", 500), ("C", 100),
("L", 50), ("X", 10), ("V", 5), ("I", 1)
]
const romanNumbers3 =
[
("M", 1000), ("D", 500), ("C", 100),
("L", 50), ("X", 10), ("V", 5), ("I", 1)
]
const romanNumbers4 = [
("M", 1000), ("D", 500), ("C", 100),
("L", 50), ("X", 10), ("V", 5), ("I", 1)
]

View file

@ -0,0 +1,7 @@
# test the file-IO
proc main() =
for line in lines("thallo.nim"):
writeln(stdout, line)
main()

Some files were not shown because too many files have changed in this diff Show more