From 481884fb892f949478dad8d801ced704baea986c Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Mon, 18 Nov 2019 18:36:57 +0100 Subject: Automatic testing of examples --- closuretest.lisp | 1 - data.lisp | 9 --- f.lisp | 7 --- fibo.lisp | 156 ---------------------------------------------- fiboY.lisp | 24 ------- match.lisp | 15 ----- stdlib.lisp | 3 - symbols.lisp | 3 - test.sh | 9 +++ tests/closuretest.lisp | 7 +++ tests/closuretest.out | 2 + tests/data.lisp | 9 +++ tests/data.out | 5 ++ tests/disabled/match.lisp | 15 +++++ tests/f.lisp | 7 +++ tests/f.out | 5 ++ tests/fibo.lisp | 156 ++++++++++++++++++++++++++++++++++++++++++++++ tests/fibo.out | 25 ++++++++ tests/fiboY.lisp | 24 +++++++ tests/fiboY.out | 25 ++++++++ tests/filetest.lisp | 3 + tests/filetest.out | 4 ++ tests/let-fail.lisp | 8 +++ tests/let-fail.out | 1 + tests/stdlib.lisp | 28 +++++++++ tests/stdlib.out | 0 tests/symbols.lisp | 3 + tests/symbols.out | 3 + 28 files changed, 339 insertions(+), 218 deletions(-) delete mode 100644 closuretest.lisp delete mode 100644 data.lisp delete mode 100644 f.lisp delete mode 100644 fibo.lisp delete mode 100644 fiboY.lisp delete mode 100644 match.lisp delete mode 100644 stdlib.lisp delete mode 100644 symbols.lisp create mode 100755 test.sh create mode 100644 tests/closuretest.lisp create mode 100644 tests/closuretest.out create mode 100644 tests/data.lisp create mode 100644 tests/data.out create mode 100644 tests/disabled/match.lisp create mode 100644 tests/f.lisp create mode 100644 tests/f.out create mode 100644 tests/fibo.lisp create mode 100644 tests/fibo.out create mode 100644 tests/fiboY.lisp create mode 100644 tests/fiboY.out create mode 100644 tests/filetest.lisp create mode 100644 tests/filetest.out create mode 100644 tests/let-fail.lisp create mode 100644 tests/let-fail.out create mode 100644 tests/stdlib.lisp create mode 100644 tests/stdlib.out create mode 100644 tests/symbols.lisp create mode 100644 tests/symbols.out diff --git a/closuretest.lisp b/closuretest.lisp deleted file mode 100644 index 5d96c7f..0000000 --- a/closuretest.lisp +++ /dev/null @@ -1 +0,0 @@ -(print (((lambda (x) (lambda (y) (+ x y))) 1) 2)) diff --git a/data.lisp b/data.lisp deleted file mode 100644 index 1e77aae..0000000 --- a/data.lisp +++ /dev/null @@ -1,9 +0,0 @@ -#include "stdlib.lisp" - -(define li (list 1 2 3)) - -(print (car li)) -(print (cadr li)) -(print (caddr li)) -(print (list 1 2 3 4 5 6)) -(print '(1 2 3 4 5 6)) diff --git a/f.lisp b/f.lisp deleted file mode 100644 index 5644138..0000000 --- a/f.lisp +++ /dev/null @@ -1,7 +0,0 @@ -(print 42) -(print "kaas") -(if 42 (print "ja") (print "nee")) -(define f (lambda (a) (print a))) -(f "iets") -(define f (lambda (a) (print a a))) -(f "iets") diff --git a/fibo.lisp b/fibo.lisp deleted file mode 100644 index 3beae63..0000000 --- a/fibo.lisp +++ /dev/null @@ -1,156 +0,0 @@ -(define fibo1 (n) - (if (<= n 0) 0 - (if (<= n 2) 1 - (+ (fibo1 (- n 1)) (fibo1 (- n 2)))))) - -; == main lambda == -; Params: 1 -; B0: -; t0 <- param 0 -; t1 <- callf "<=" [t0 0] -; br t1 B1 B2 -; B1: -; t2 <- assign 0 -; jmp B5 -; B2: -; t3 <- param 0 -; t4 <- callf "<=" [t3 2] -; br t4 B3 B4 -; B3: -; t2 <- assign 1 -; jmp B5 -; B4: -; t5 <- param 0 -; t6 <- callf "-" [t5 1] -; t7 <- callf "fibo1" [t6] ; patched up (statically known address, afterwards) -; t8 <- param 0 -; t9 <- callf "-" [t8 2] -; t10 <- callf "fibo1" [t9] ; patched up -; t2 <- callf "+" [t7 t10] -; jmp B5 -; B5: -; return t2 - - -(define fibo2help (a b n) - (if (<= n 0) b - (fibo2help b (+ a b) (- n 1)))) - -(define fibo2 (n) - (if (<= n 0) 0 - (if (<= n 2) 1 - (fibo2help 1 1 (- n 2))))) - - -(define fibo3 (n) - ((lambda (helper) - (if (<= n 0) 0 - (if (<= n 2) 1 - (helper helper 1 1 (- n 2))))) - (lambda (recur a b n) - (if (<= n 0) b - (recur recur b (+ a b) (- n 1)))))) - -; == L1 == -; Params: 1 -; Closure slots: 1 -; B0: -; t0 <- closure 0 -; t1 <- callf "<=" [t0 0] -; br t1 B1 B2 -; B1: -; t2 <- assign 0 -; jmp B5 -; B2: -; t3 <- closure 0 -; t4 <- callf "<=" [t3 2] -; br t4 B3 B4 -; B3: -; t2 <- assign 1 -; jmp B5 -; B4: -; t5 <- param 0 -; t6 <- param 0 -; t7 <- closure 0 -; t8 <- callf "-" [t7 2] -; t2 <- callc t5 [t6 1 1 t8] -; jmp B5 -; B5: -; return t2 -; -; == L2 == -; Params: 4 -; B0: -; t0 <- param 3 -; t1 <- callf "<=" [t0 0] -; br t1 B1 B2 -; B1: -; t2 <- param 2 -; jmp B3 -; B2: -; t3 <- param 0 -; t4 <- param 0 -; t5 <- param 2 -; t6 <- param 1 -; t7 <- param 2 -; t8 <- callf "+" [t6 t7] -; t9 <- param 3 -; t10 <- callf "-" [t9 1] -; t2 <- callc t3 [t4 t5 t8 t10] -; jmp B3 -; B3: -; return t2 -; -; == main lambda == -; Params: 1 -; B0: -; t0 <- param 0 -; t1 <- alloc-closure L1 [t0] -; t2 <- callc t1 [C(L2)] -; free-closure t1 -; return t2 - - -(define for (start end f) - (if (<= start end) - (do (f start) (for (+ start 1) end f)) - '())) - -; == main lambda == -; Params: 3 -; Data table: -; 0: () -; B0: -; t0 <- param 0 -; t1 <- param 1 -; t2 <- callf "<=" [t0 t1] -; br t2 B1 B2 -; B1: -; t3 <- param 2 -; t4 <- param 0 -; _ <- callc t3 [t4] -; t5 <- param 0 -; t6 <- callf "+" [t5 1] -; t7 <- param 1 -; t8 <- param 2 -; t9 <- callf "for" [t6 t7 t8] ; patched up -; jmp B3 -; B2: -; t9 <- data 0 -; jmp B3 -; B3: -; return t9 - -(for 1 25 (lambda (n) (print (fibo3 n)))) - -; == L1 == -; Params: 1 -; B0: -; t0 <- param 0 -; t1 <- callf "fibo3" [t0] -; t2 <- callf "print" [t1] -; return t2 -; -; == global code == -; B0: -; _ <- callf "for" [1 25 C(L1)] diff --git a/fiboY.lisp b/fiboY.lisp deleted file mode 100644 index d12a5ab..0000000 --- a/fiboY.lisp +++ /dev/null @@ -1,24 +0,0 @@ -(define cadr (l) (car (cdr l))) -(define caddr (l) (car (cdr (cdr l)))) - -(define YY (recur) (lambda (f) (lambda (a) (f ((recur recur) f) a)))) -(define Y (YY YY)) - -(define forX (recur low_high_func) - (if (<= (car low_high_func) (cadr low_high_func)) - (do - ((caddr low_high_func) (car low_high_func)) - (recur (list (+ (car low_high_func) 1) (cadr low_high_func) (caddr low_high_func)))) - '())) - -(define for (Y forX)) - -(define fibohelperX (recur n_a_b) - (if (<= (car n_a_b) 0) (cadr n_a_b) - (recur (list (- (car n_a_b) 1) (caddr n_a_b) (+ (cadr n_a_b) (caddr n_a_b)))))) - -(define fibohelper (Y fibohelperX)) - -(define fibo (n) (fibohelper (list n 0 1))) - -(for (list 1 25 (lambda (n) (print (fibo n))))) diff --git a/match.lisp b/match.lisp deleted file mode 100644 index 84503e0..0000000 --- a/match.lisp +++ /dev/null @@ -1,15 +0,0 @@ -(define f (x) - (match x - ((1 2 3) "1-2-3") - ((1) "just 1") - ((1 ...) "1 something") - ((n ...) "number something") - ('v "something quoted") - "dunno")) - -(print (f '(1 2 3))) -(print (f '(1))) -(print (f '(1 2 3 4 5))) -(print (f '(2 3 4 5))) -(print (f ''"kaas")) -(print (f "kaas")) diff --git a/stdlib.lisp b/stdlib.lisp deleted file mode 100644 index b691a0a..0000000 --- a/stdlib.lisp +++ /dev/null @@ -1,3 +0,0 @@ -(define cadr (x) (car (cdr x))) -(define caddr (x) (car (cdr (cdr x)))) -(define cadddr (x) (car (cdr (cdr (cdr x))))) diff --git a/symbols.lisp b/symbols.lisp deleted file mode 100644 index cba1302..0000000 --- a/symbols.lisp +++ /dev/null @@ -1,3 +0,0 @@ -(print '(1 2 3)) -(print (list 1 2 3)) -(print (= 'hoi 'hoi)) diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..6b1971f --- /dev/null +++ b/test.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +set -euo pipefail +stack build +prog="$(stack path --local-install-root)/bin/lisp" +for f in tests/*.lisp; do + echo "$f" + diff -u <("$prog" "$f") "$(sed 's/\.lisp$/.out/' <<<"$f")" +done +echo "All OK" diff --git a/tests/closuretest.lisp b/tests/closuretest.lisp new file mode 100644 index 0000000..d743481 --- /dev/null +++ b/tests/closuretest.lisp @@ -0,0 +1,7 @@ +(print (((lambda (x) (lambda (y) (+ x y))) 1) 2)) + +((lambda (f) + ((lambda (g1 g2) + (print (g1 10) (g1 20))) + (f 42))) + (lambda (x) (lambda (y) (+ x y)))) diff --git a/tests/closuretest.out b/tests/closuretest.out new file mode 100644 index 0000000..542797d --- /dev/null +++ b/tests/closuretest.out @@ -0,0 +1,2 @@ +3 +52, 62 diff --git a/tests/data.lisp b/tests/data.lisp new file mode 100644 index 0000000..1e77aae --- /dev/null +++ b/tests/data.lisp @@ -0,0 +1,9 @@ +#include "stdlib.lisp" + +(define li (list 1 2 3)) + +(print (car li)) +(print (cadr li)) +(print (caddr li)) +(print (list 1 2 3 4 5 6)) +(print '(1 2 3 4 5 6)) diff --git a/tests/data.out b/tests/data.out new file mode 100644 index 0000000..40c5dd4 --- /dev/null +++ b/tests/data.out @@ -0,0 +1,5 @@ +1 +2 +3 +[1,2,3,4,5,6] +[1,2,3,4,5,6] diff --git a/tests/disabled/match.lisp b/tests/disabled/match.lisp new file mode 100644 index 0000000..84503e0 --- /dev/null +++ b/tests/disabled/match.lisp @@ -0,0 +1,15 @@ +(define f (x) + (match x + ((1 2 3) "1-2-3") + ((1) "just 1") + ((1 ...) "1 something") + ((n ...) "number something") + ('v "something quoted") + "dunno")) + +(print (f '(1 2 3))) +(print (f '(1))) +(print (f '(1 2 3 4 5))) +(print (f '(2 3 4 5))) +(print (f ''"kaas")) +(print (f "kaas")) diff --git a/tests/f.lisp b/tests/f.lisp new file mode 100644 index 0000000..5644138 --- /dev/null +++ b/tests/f.lisp @@ -0,0 +1,7 @@ +(print 42) +(print "kaas") +(if 42 (print "ja") (print "nee")) +(define f (lambda (a) (print a))) +(f "iets") +(define f (lambda (a) (print a a))) +(f "iets") diff --git a/tests/f.out b/tests/f.out new file mode 100644 index 0000000..297955d --- /dev/null +++ b/tests/f.out @@ -0,0 +1,5 @@ +42 +kaas +ja +iets +iets, iets diff --git a/tests/fibo.lisp b/tests/fibo.lisp new file mode 100644 index 0000000..3beae63 --- /dev/null +++ b/tests/fibo.lisp @@ -0,0 +1,156 @@ +(define fibo1 (n) + (if (<= n 0) 0 + (if (<= n 2) 1 + (+ (fibo1 (- n 1)) (fibo1 (- n 2)))))) + +; == main lambda == +; Params: 1 +; B0: +; t0 <- param 0 +; t1 <- callf "<=" [t0 0] +; br t1 B1 B2 +; B1: +; t2 <- assign 0 +; jmp B5 +; B2: +; t3 <- param 0 +; t4 <- callf "<=" [t3 2] +; br t4 B3 B4 +; B3: +; t2 <- assign 1 +; jmp B5 +; B4: +; t5 <- param 0 +; t6 <- callf "-" [t5 1] +; t7 <- callf "fibo1" [t6] ; patched up (statically known address, afterwards) +; t8 <- param 0 +; t9 <- callf "-" [t8 2] +; t10 <- callf "fibo1" [t9] ; patched up +; t2 <- callf "+" [t7 t10] +; jmp B5 +; B5: +; return t2 + + +(define fibo2help (a b n) + (if (<= n 0) b + (fibo2help b (+ a b) (- n 1)))) + +(define fibo2 (n) + (if (<= n 0) 0 + (if (<= n 2) 1 + (fibo2help 1 1 (- n 2))))) + + +(define fibo3 (n) + ((lambda (helper) + (if (<= n 0) 0 + (if (<= n 2) 1 + (helper helper 1 1 (- n 2))))) + (lambda (recur a b n) + (if (<= n 0) b + (recur recur b (+ a b) (- n 1)))))) + +; == L1 == +; Params: 1 +; Closure slots: 1 +; B0: +; t0 <- closure 0 +; t1 <- callf "<=" [t0 0] +; br t1 B1 B2 +; B1: +; t2 <- assign 0 +; jmp B5 +; B2: +; t3 <- closure 0 +; t4 <- callf "<=" [t3 2] +; br t4 B3 B4 +; B3: +; t2 <- assign 1 +; jmp B5 +; B4: +; t5 <- param 0 +; t6 <- param 0 +; t7 <- closure 0 +; t8 <- callf "-" [t7 2] +; t2 <- callc t5 [t6 1 1 t8] +; jmp B5 +; B5: +; return t2 +; +; == L2 == +; Params: 4 +; B0: +; t0 <- param 3 +; t1 <- callf "<=" [t0 0] +; br t1 B1 B2 +; B1: +; t2 <- param 2 +; jmp B3 +; B2: +; t3 <- param 0 +; t4 <- param 0 +; t5 <- param 2 +; t6 <- param 1 +; t7 <- param 2 +; t8 <- callf "+" [t6 t7] +; t9 <- param 3 +; t10 <- callf "-" [t9 1] +; t2 <- callc t3 [t4 t5 t8 t10] +; jmp B3 +; B3: +; return t2 +; +; == main lambda == +; Params: 1 +; B0: +; t0 <- param 0 +; t1 <- alloc-closure L1 [t0] +; t2 <- callc t1 [C(L2)] +; free-closure t1 +; return t2 + + +(define for (start end f) + (if (<= start end) + (do (f start) (for (+ start 1) end f)) + '())) + +; == main lambda == +; Params: 3 +; Data table: +; 0: () +; B0: +; t0 <- param 0 +; t1 <- param 1 +; t2 <- callf "<=" [t0 t1] +; br t2 B1 B2 +; B1: +; t3 <- param 2 +; t4 <- param 0 +; _ <- callc t3 [t4] +; t5 <- param 0 +; t6 <- callf "+" [t5 1] +; t7 <- param 1 +; t8 <- param 2 +; t9 <- callf "for" [t6 t7 t8] ; patched up +; jmp B3 +; B2: +; t9 <- data 0 +; jmp B3 +; B3: +; return t9 + +(for 1 25 (lambda (n) (print (fibo3 n)))) + +; == L1 == +; Params: 1 +; B0: +; t0 <- param 0 +; t1 <- callf "fibo3" [t0] +; t2 <- callf "print" [t1] +; return t2 +; +; == global code == +; B0: +; _ <- callf "for" [1 25 C(L1)] diff --git a/tests/fibo.out b/tests/fibo.out new file mode 100644 index 0000000..14950e3 --- /dev/null +++ b/tests/fibo.out @@ -0,0 +1,25 @@ +1 +1 +2 +3 +5 +8 +13 +21 +34 +55 +89 +144 +233 +377 +610 +987 +1597 +2584 +4181 +6765 +10946 +17711 +28657 +46368 +75025 diff --git a/tests/fiboY.lisp b/tests/fiboY.lisp new file mode 100644 index 0000000..d12a5ab --- /dev/null +++ b/tests/fiboY.lisp @@ -0,0 +1,24 @@ +(define cadr (l) (car (cdr l))) +(define caddr (l) (car (cdr (cdr l)))) + +(define YY (recur) (lambda (f) (lambda (a) (f ((recur recur) f) a)))) +(define Y (YY YY)) + +(define forX (recur low_high_func) + (if (<= (car low_high_func) (cadr low_high_func)) + (do + ((caddr low_high_func) (car low_high_func)) + (recur (list (+ (car low_high_func) 1) (cadr low_high_func) (caddr low_high_func)))) + '())) + +(define for (Y forX)) + +(define fibohelperX (recur n_a_b) + (if (<= (car n_a_b) 0) (cadr n_a_b) + (recur (list (- (car n_a_b) 1) (caddr n_a_b) (+ (cadr n_a_b) (caddr n_a_b)))))) + +(define fibohelper (Y fibohelperX)) + +(define fibo (n) (fibohelper (list n 0 1))) + +(for (list 1 25 (lambda (n) (print (fibo n))))) diff --git a/tests/fiboY.out b/tests/fiboY.out new file mode 100644 index 0000000..14950e3 --- /dev/null +++ b/tests/fiboY.out @@ -0,0 +1,25 @@ +1 +1 +2 +3 +5 +8 +13 +21 +34 +55 +89 +144 +233 +377 +610 +987 +1597 +2584 +4181 +6765 +10946 +17711 +28657 +46368 +75025 diff --git a/tests/filetest.lisp b/tests/filetest.lisp new file mode 100644 index 0000000..b37d2fe --- /dev/null +++ b/tests/filetest.lisp @@ -0,0 +1,3 @@ +#include "stdlib.lisp" + +(print (read-file "tests/filetest.lisp")) diff --git a/tests/filetest.out b/tests/filetest.out new file mode 100644 index 0000000..7d444f6 --- /dev/null +++ b/tests/filetest.out @@ -0,0 +1,4 @@ +#include "stdlib.lisp" + +(print (read-file "tests/filetest.lisp")) + diff --git a/tests/let-fail.lisp b/tests/let-fail.lisp new file mode 100644 index 0000000..005cfc9 --- /dev/null +++ b/tests/let-fail.lisp @@ -0,0 +1,8 @@ +(print + ( + (lambda (fid) + (let ((x (lambda (arg) fid))) + (x 42))) + 123 + ) +) diff --git a/tests/let-fail.out b/tests/let-fail.out new file mode 100644 index 0000000..190a180 --- /dev/null +++ b/tests/let-fail.out @@ -0,0 +1 @@ +123 diff --git a/tests/stdlib.lisp b/tests/stdlib.lisp new file mode 100644 index 0000000..380fa2a --- /dev/null +++ b/tests/stdlib.lisp @@ -0,0 +1,28 @@ +(define cadr (x) (car (cdr x))) +(define caddr (x) (car (cdr (cdr x)))) +(define cadddr (x) (car (cdr (cdr (cdr x))))) + +(define YY (recur) (lambda (f) (lambda (a) (f ((recur recur) f) a)))) +(define Y (YY YY)) + +(define for (start end f) + (if (<= start end) + (do (f start) (for (+ start 1) end f)) + '())) + +(define with-open-file (path mode f) + (let ((fid (sys-open-file mode path))) + (let ((value (f fid))) + (do + (sys-close-file fid) + value)))) + +(define read-file (path) + (with-open-file path 0 (lambda (fid) + ; (print (sys-get-char fid)) + ; (print (let ((x (lambda (arg) fid))) (x 42))) + (let ((helper (Y (lambda (recur s) + (let ((ch (sys-get-char fid))) + (if (null? ch) s (recur (+ s ch)))))))) + (helper "")) + ))) diff --git a/tests/stdlib.out b/tests/stdlib.out new file mode 100644 index 0000000..e69de29 diff --git a/tests/symbols.lisp b/tests/symbols.lisp new file mode 100644 index 0000000..cba1302 --- /dev/null +++ b/tests/symbols.lisp @@ -0,0 +1,3 @@ +(print '(1 2 3)) +(print (list 1 2 3)) +(print (= 'hoi 'hoi)) diff --git a/tests/symbols.out b/tests/symbols.out new file mode 100644 index 0000000..16f9c5e --- /dev/null +++ b/tests/symbols.out @@ -0,0 +1,3 @@ +[1,2,3] +[1,2,3] +1 -- cgit v1.2.3-70-g09d2