(include "/usr/local/var/mlisp/ut.lsp")

;;  prepare some code to be used in tests
(defun fact (n)
   (if (<= n 1)
      1
      (* n (fact (- n 1)))
    ))

; for scitej 4000 stack must be 16MB, otherwise 1000 is ok
(defun scitej (n)
   (if (<= n 1)
      1
      (+ n (scitej (- n 1)))
    ))

(write-file "/tmp/f.txt" "line 1\nline 2\nline3")
(define counter (lambda (ln) (do (set! ii (+ ii 1)) )))
(define ii 0)


(define json_list (parse-json "{\"k1\":\"v1\", \"k2\":42, \"k3\":[\"a\",123,true,false,null]}"))


(ut::define-test "result of (and (> 2 1) (> 2 1))" '(ut::assert-true (and (> 2 1) (> 2 1))))
(ut::define-test "result of (or  (> 2 1) (> 2 1))" '(ut::assert-true (or  (> 2 1) (> 2 1))))
(ut::define-test "retult of (and (> 2 1) (> 1 2))" '(ut::assert-false (and (> 2 1) (> 1 2))))
(ut::define-test "result of (or (> 2 1) (> 1 2))" '(ut::assert-true (or (> 2 1) (> 1 2))))
(ut::define-test "result of (and (> 1 1) (> 1 2))" '(ut::assert-false (and (> 1 1) (> 1 2))))
(ut::define-test "result of (or (> 1 1) (> 2 1))" '(ut::assert-true (or (> 1 1) (> 2 1))))

(ut::define-test "result of (not 1)" '(ut::assert-false (not 1)))
(ut::define-test "result of (not nil)" '(ut::assert-true (not nil)))


(ut::define-test "result of (member '(1 2 3) 1" '(ut::assert-true (member '(1 2 3) 1)))
(ut::define-test "result of (member '(1 2 3) 3" '(ut::assert-true (member '(1 2 3) 3)))
(ut::define-test "result of (member '(1 2 3) 4" '(ut::assert-false (member '(1 2 3) 4)))

(ut::define-test "result of (make-list 3)" '(ut::assert-equal '(nil nil nil) (make-list 3)))
(ut::define-test "result of (make-list-of 3 999)" '(ut::assert-equal '(999 999 999) (make-list-of 3 999)))


(ut::define-test "result of (fact 5)" '(ut::assert-equal 120 (fact 5)))
(ut::define-test "result of (scitej 1000)" '(ut::assert-equal 500500 (scitej 1000)))

(ut::define-test "result of (len json_list)" '(ut::assert-equal 3 (len json_list)))

(ut::define-test "result of (quick-sort (10 9 8 7 6 5 4 3 2 1))" '(ut::assert-equal '(1 2 3 4 5 6 7 8 9 10) (quick-sort '(10 9 8 7 6 5 4 3 2 1))))
(ut::define-test "result of (quick-sort (10 9 8 7 6 5 4 3 2 1))" '(ut::assert-equal '(1 2 3 4 5 6 7 8 9 10) (quick-sort '(1 2 3 4 5 6 7 8 9 10))))
(ut::define-test "result of (quick-sort-reverse (1 2 3 4 5 6 7 8 9 10))" '(ut::assert-equal '(10 9 8 7 6 5 4 3 2 1) (quick-sort-reverse '(1 2 3 4 5 6 7 8 9 10))))
(ut::define-test "result of (quick-sort-by (10 9 8 7 6 5 4 3 2 1) (lambda (a b) (> a b)))" '(ut::assert-equal '(1 2 3 4 5 6 7 8 9 10) (quick-sort-by '(10 9 8 7 6 5 4 3 2 1) (lambda (a b) (> a b)))))
(ut::define-test "result of (quick-sort-by (1 2 3 4 5 6 7 8 9 10) (lambda (a b) (> a b)))" '(ut::assert-equal '(1 2 3 4 5 6 7 8 9 10) (quick-sort-by '(1 2 3 4 5 6 7 8 9 10) (lambda (a b) (> a b)))))
(ut::define-test "result of (quick-sort-by (1 2 3 4 5 6 7 8 9 10) (lambda (a b) (< a b)))" '(ut::assert-equal '(10 9 8 7 6 5 4 3 2 1) (quick-sort-by '(1 2 3 4 5 6 7 8 9 10) (lambda (a b) (< a b)))))

(ut::define-test "result of (len (parse-csv (read-file \"tests/data.csv\")))" '(ut::assert-equal 2 (len (parse-csv (read-file "tests/data.csv")))))

(ut::define-test "result of (string-rtrim \"abc     \")" '(ut::assert-equal "abc" (string-rtrim "abc     ")))
(ut::define-test "result of (string-ltrim \"     abc\")" '(ut::assert-equal "abc" (string-ltrim "     abc")))
(ut::define-test "result of (string-trim \"  abc  \")" '(ut::assert-equal "abc" (string-trim "  abc   ")))
(ut::define-test "result of (string-regex? \"test.lsp\" \"^.*\.l(i)?sp$\")" '(ut::assert-equal 1 (string-regex? "test.lsp" "^.*\.l(i)?sp$")))

(ut::define-test "result of (string-split \"split me by space\" \"\\s+\")" '(ut::assert-equal '("split" "me" "by" "space") (string-split "split me by space" "\\s+")))
(ut::define-test "result of (string-upcase \"abcABCD\")" '(ut::assert-equal "ABCABCD" (string-upcase "abcABCD")))
(ut::define-test "result of (string-downcase \"abcABCD\")" '(ut::assert-equal "abcabcd" (string-downcase "abcABCD")))
(ut::define-test "result of (string-len \"abcdef\")" '(ut::assert-equal 6 (string-len "abcdef")))

(ut::define-test "result of (string-substr \"ABCDEF\")" '(ut::assert-equal "ABCDEF" (string-substr "ABCDEF")))
(ut::define-test "result of (string-substr \"ABCDEF\" 1)" '(ut::assert-equal "BCDEF" (string-substr "ABCDEF" 1)))
(ut::define-test "result of (string-substr \"ABCDEF\" 2 3)" '(ut::assert-equal "CDE" (string-substr "ABCDEF" 2 3)))
(ut::define-test "result of (string-substr \"ABCDEF\"4 42)" '(ut::assert-equal "EF" (string-substr "ABCDEF" 4 42)))
(ut::define-test "result of (string-substr \"ABCDEF\" -2 2)" '(ut::assert-equal "EF" (string-substr "ABCDEF" -2 2)))

(ut::define-test "result of (string-find \" long long int;\" \"long\")" '(ut::assert-equal 1 (string-find " long long int;" "long")))
(ut::define-test "result of (string-find \" long long int;\" \"long\" 2)" '(ut::assert-equal 6 (string-find " long long int;" "long" 2)))
(ut::define-test "result of (string-find \" long long int;\" \" \")" '(ut::assert-equal 0 (string-find " long long int;" " ")))
(ut::define-test "result of (string-find \" long long int;\" \"o\")" '(ut::assert-equal 2 (string-find " long long int;" "o")))
(ut::define-test "result of (string-find \" long long int;\" \"float\")" '(ut::assert-nil (string-find " long long int;" "float")))

(ut::define-test "result of (itok 65)" '(ut::assert-equal "A" (itok 65)))
(ut::define-test "result of (itok 48)" '(ut::assert-equal "0" (itok 48)))
(ut::define-test "result of (ktoi \"A\")" '(ut::assert-equal 65 (ktoi "A")))
(ut::define-test "result of (ktoi \"0\")" '(ut::assert-equal 48 (ktoi "0")))

(ut::define-test "result of (write-file \"/tmp/file\" \"write-file test\")" '(ut::assert-equal 1 (write-file "/tmp/file" "write-file test\n")))
(ut::define-test "result of (is-file? \"/tmp/file\")" '(ut::assert-true (is-file? "/tmp/file")))
(ut::define-test "result of (is-file? \"/tmp/file_whichnotex_ists\")" '(ut::assert-false (is-file? "/tmp/file_whichnotex_ists")))
(ut::define-test "result of (is-dir? \"/tmp/file_whichnotex_ists\")" '(ut::assert-false (is-dir? "/tmp/file_whichnotex_ists")))
(ut::define-test "result of (is-dir? \"/tmp\"" '(ut::assert-true (is-dir? "/tmp")))

(ut::define-test "result of (str-to-date \"01.01.1970\" \"%d.%m.%Y\")" '(ut::assert-equal 0 (str-to-date "01.01.1970" "%d.%m.%Y")))
(ut::define-test "result of (date-add (str-to-date \"01.01.1970\" \"%d.%m.%Y\") 1 \"day\")" '(ut::assert-equal 86400 (date-add (str-to-date "01.01.1970" "%d.%m.%Y") 1 "day")))

(ut::define-test "result of (uniq '(1 2 2 2 3 4 5 4 4 1 2 2 2 3 4 5 4 4 61 2 2 2 3 4 5 4 4 66))" '(ut::assert-equal '(1 2 3 4 5 61 66) (uniq '(1 2 2 2 3 4 5 4 4 1 2 2 2 3 4 5 4 4 61 2 2 2 3 4 5 4 4 66))))
(ut::define-test "result of (flatten '(1 2 (3 3 (4)) 5 6))" '(ut::assert-equal '(1 2 3 3 4 5 6) (flatten (1 2 (3 3 (4)) 5 6))))

(ut::define-test "result of (sprintf \"%.2f\" '(1.25))" '(ut::assert-equal "1.25" (sprintf "%.2f" '(1.25))))
(ut::define-test "result of (sprintf \"%.2f\" '(1.23456))" '(ut::assert-equal "1.23" (sprintf "%.2f" '(1.23456))))
(ut::define-test "result of (sprintf \"%.d\" '(10000000))" '(ut::assert-equal "10000000" (sprintf "%.d" '(10000000))))

(ut::define-test "result of (get-env \"HOME\")" '(ut::assert-equal "/Users/vaclavt" (get-env "HOME")))

(ut::define-test "result of (!= nil nil)" '(ut::assert-false (!= nil nil)))


(ut::define-test "result of (start-of-day)" '(ut::assert-equal 1620864000 (start-of-day (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S"))))
(ut::define-test "result of (end-of-day)" '(ut::assert-equal 1620950399 (end-of-day (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S"))))
(ut::define-test "result of (start-of-month)" '(ut::assert-equal 1619827200 (start-of-month (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S"))))
(ut::define-test "result of (end-of-month)" '(ut::assert-equal 1622505599 (end-of-month (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S"))))
(ut::define-test "result of (start-of-next-month)" '(ut::assert-equal 1622505600 (start-of-next-month (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S"))))
(ut::define-test "result of (end-of-next-month)" '(ut::assert-equal 1625097599 (end-of-next-month (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S"))))
(ut::define-test "result of (start-of-prev-month)" '(ut::assert-equal 1617235200 (start-of-prev-month (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S"))))
(ut::define-test "result of (end-of-prev-month)" '(ut::assert-equal 1619827199 (end-of-prev-month (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S"))))
(ut::define-test "result of (start-of-year)" '(ut::assert-equal 1609459200 (start-of-year (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S"))))
(ut::define-test "result of (end-of-year)" '(ut::assert-equal 1640995199 (end-of-year (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S"))))

(ut::define-test "(read-file-lines \"/tmp/f.txt\" counter)" '(ut::assert-equal 3 (read-file-lines "/tmp/f.txt" counter)))

;(ut::define-test "result of " '(ut::assert-true )
(ut::run-tests)


;; (include "/usr/local/var/mlisp/terminal.lsp")

;; (print (term-red "red text"))
;; (print (term-green "green text"))
;; (print (term-yellow "yellow text"))
;; (print (term-blue "blue text"))
;; (print (term-magenta "magenta text"))
;; (print (term-cyan "cyan text"))
;; (print (term-white "white text"))
;; (print (term-bold "bold text"))
;; (print (term-underline "underline text"))
;; (print "normal text")

;; (print (+ (term-red (sprintf "%.2f" (list 1.11))) "  "          
;;           (term-green (sprintf "%.2f" (list 1.11))) " "
;;           (term-blue (sprintf "%.2f" (list 1.11))) " "
;;           (term-yellow (sprintf "%.2f" (list 1.11))) " "
;;     ))

