From 02c3a20936f3f4bf1044e6b1dd7e0385eacf3199 Mon Sep 17 00:00:00 2001 From: VaclavT Date: Mon, 19 Apr 2021 21:14:22 +0200 Subject: [PATCH] work in progress on unit testing functions --- Readme.md | 2 +- debug.lsp | 102 +++++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 86 insertions(+), 18 deletions(-) diff --git a/Readme.md b/Readme.md index 3dfecbf..c27df5d 100644 --- a/Readme.md +++ b/Readme.md @@ -36,7 +36,7 @@ - env functions - get-env, set-env; set-env cannot be implemented in stdlib.lsp, because popen is in fact subshell - format (sprintf) -- setq +- add include-stdlib function for other libs in stdlib dir (during startup stdlib.lsp is loaded only) - syntax highlighting do VS Code - add hash datatype diff --git a/debug.lsp b/debug.lsp index d641707..3270147 100644 --- a/debug.lsp +++ b/debug.lsp @@ -1,18 +1,18 @@ -(print (string-split "split me by space" "\\s+")) +;; (print (string-split "split me by space" "\\s+")) -(print (string-rtrim "abc ")) -(print (string-ltrim " abc")) -(print (string-trim " abc ")) +;; (print (string-rtrim "abc ")) +;; (print (string-ltrim " abc")) +;; (print (string-trim " abc ")) -(defun string-upcase (str) - (string-case str "upper")) +;; (defun string-upcase (str) +;; (string-case str "upper")) -(defun string-downcase (str) - (string-case str "lower")) +;; (defun string-downcase (str) +;; (string-case str "lower")) -(print (string-upcase "abcABCD")) -(print (string-downcase "abcABCD")) +;; (print (string-upcase "abcABCD")) +;; (print (string-downcase "abcABCD")) ;; (print (sprintf "%.2f" (list 1.25))) @@ -44,14 +44,82 @@ ;; (define fdx_list (parse-csv (read-file "tests/csv_data.csv"))) ;; (print fdx_list) -(print (and (print "xx") (= 1 0) (print "yy"))) +;; (print (and (print "xx") (= 1 0) (print "yy"))) -(defun aa () (do - (print "prvni") - (+ 1 xx) - (print "druhy") +;; (defun aa () (do +;; (print "prvni") +;; (+ 1 xx) +;; (print "druhy") +;; )) + +;; (aa) + +; (sleep 1.5) + +(define multiply-by (lambda (n) (lambda (y) (* y n)))) +(define doubler (multiply-by 2)) +(define tripler (multiply-by 3)) +(doubler 4) +(tripler 4) + + +;https://github.com/anthay/Lisp90/blob/master/lisp90.cpp +;http://howtowriteaprogram.blogspot.com/2010/11/lisp-interpreter-in-90-lines-of-c.html + +; https://stackoverflow.com/questions/526082/in-scheme-whats-the-point-of-set +(define x 3) +(defun foo() (do (define x 4) x)) +(defun bar() (do (set! x 4) x)) + +(foo) ; returns 4 +x ; still 3 +(bar) ; returns 4 +x ; is now 4 + + +(defun mini-uuid (uuid_len) + (do + (define a_list '("_" "-" "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z" "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z")) + (reduce (lambda (acc y) (+ acc y)) "" (map (lambda (e) (index a_list (random 0 (- (len a_list) 1)))) (range 0 uuid_len))) )) -(aa) +(benchmark "uuid 1000x :" (define i 0) (while (< i 1000) (do (mini-uuid 5) (define i (+ i 1))))) -; (sleep 1.5) \ No newline at end of file + +(defun ut::assert-equal (value form) (do + (define fvalue (eval form)) + (if (= value fvalue) + 1 + nil + ) + )) + +(define ut::assert-true (test) (ut::assert-equal 1 test)) +(assert-false (test)() (asertt-equal nil test) +(defun ut::assert-true test) () + + +(defun ut::assert-false test) +(define ut::tests_list '()) +(defun ut::define-test (name exp_list) ( + define test_list (push ut::tests_list (list name exp_list)))) + +(run-tests name1 name2 ...) + + + +(defun define-test (name exp_list) (set! test_list (push test_list (list name exp_list)))) +(defun run-tests () (do (for t test_list (if (eval (index t 1)) (print (+ "OK -> " (index t 0))) (print (+ "ERR -> " (index t 0))))))) +;(run-test name) + +(define test_list '()) + +(define-test "je to dvojka" '(assert-equal 2 (+ 1 1))) +(define-test "je to trojka" '(assert-equal 3 (+ 1 1))) + +(run-tests) + + + + +(define-test "je to dvojka" '(assert-equal 2 (+ 1 1))) \ No newline at end of file