mlisp/tests/test.lsp

194 lines
5.9 KiB
Common Lisp

; it is expected to be called from parent directory ie: ml -f tests/test.lisp
(print "Test starts")
(print "result of (and (> 2 1) (> 2 1)): " (and (> 2 1) (> 2 1)))
(print "result of (or (> 2 1) (> 2 1)): " (or (> 2 1) (> 2 1)))
(print "result of (and (> 2 1) (> 1 2)): " (and (> 2 1) (> 1 2)))
(print "result of (or (> 2 1) (> 1 2)): " (or (> 2 1) (> 1 2)))
(print "result of (and (> 1 1) (> 1 2)): " (and (> 1 1) (> 1 2)))
(print "result of (or (> 1 1) (> 2 1)): " (or (> 1 1) (> 1 2)))
(print "(member '(1 2 3) 1:" (member '(1 2 3) 1))
(print "(member '(1 2 3) 3:" (member '(1 2 3) 3))
(print "(member '(1 2 3) 30:" (member '(1 2 3) 30))
(print "(make-list 3) :" (make-list 3))
(print "(make-list-of 3) :" (make-list-of 3 999))
(defun fact (n)
(if (<= n 1)
1
(* n (fact (- n 1)))
))
(print (fact 5))
; for this stack must be 16MB, otherwise 1000 is ok
(defun scitej (n)
(if (<= n 1)
1
(+ n (scitej (- n 1)))
))
;; (print (scitej 4000))
(print (scitej 1000))
(define json_list (parse-json "{\"k1\":\"v1\", \"k2\":42, \"k3\":[\"a\",123,true,false,null]}"))
(print json_list)
(for x json_list (print x))
(print "sorted: " (quick-sort '(10 9 8 7 6 5 4 3 2 1)))
(print "sorted: " (quick-sort '(1 2 3 4 5 6 7 8 9 10)))
(print "sorted by: " (quick-sort-by '(10 9 8 7 6 5 4 3 2 1) (lambda (a b) (> a b)) ))
(print "sorted by: " (quick-sort-by '(1 2 3 4 5 6 7 8 9 10) (lambda (a b) (> a b)) ))
(print "sorted by desc: " (quick-sort-by '(1 2 3 4 5 6 7 8 9 10) (lambda (a b) (< a b)) ))
(define csv_str (read-file "tests/data.csv"))
(define csv_list (parse-csv csv_str))
(for x csv_list
(print x))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; to create test file with csv
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; (define web_page (read-url "https://query1.finance.yahoo.com/v7/finance/download/FDX?period1=1581272585&period2=1612894985&interval=1d&events=history&includeAdjustedClose=true"))
; (write-file "tests/csv_data.csv" (last web_page))
(define fdx_list (parse-csv (read-file "tests/csv_data.csv")))
; (print fdx_list)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; to create test file with json
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; (define curl "curl -H \"Accept: application/json, text/plain, */*\" -H \"Origin: https://www.nasdaq.com/\" -H \"User-Agent: Mozilla/5.0 (Windows NT 10.0)\" https://api.nasdaq.com/api/calendar/dividends/?date=2021-02-01 2>/dev/null")
; (define curl_out (system-cmd curl))
; (write-file "tests/json_data.json" (last curl_out))
; (define json_list (parse-json (last curl_out)))
(define json_list (parse-json (read-file "tests/json_data.json")))
(define data (first json_list))
(define calendar (index data 1))
(define calendar_data (index (index calendar 0) 1))
(define header (index (index calendar_data 0) 1))
(define rows (index (index calendar_data 1) 1))
(define csv_list '())
(for e rows
; (("announcement_Date" "01/07/2021") ("companyName" "Itau Unibanco Banco Holding SA") ("dividend_Ex_Date" "02/01/2021") ("dividend_Rate" 0.003) ("indicated_Annual_Dividend" 0.033) ("payment_Date" "03/11/2021") ("record_Date" "02/02/2021") ("symbol" "ITUB"))
(define symbol (second (first (filter (lambda (x) (= (first x) "symbol")) e))))
(define divrate (second (first (filter (lambda (x) (= (first x) "dividend_Rate")) e))))
(define adate (second (first (filter (lambda (x) (= (first x) "announcement_Date")) e))))
(define name (second (first (filter (lambda (x) (= (first x) "companyName")) e))))
(define edate (second (first (filter (lambda (x) (= (first x) "dividend_Ex_Date")) e))))
(define pdate (second (first (filter (lambda (x) (= (first x) "payment_Date")) e))))
(define rdate (second (first (filter (lambda (x) (= (first x) "record_Date")) e))))
(define csv_list (push csv_list (list symbol edate pdate divrate)))
1)
(print (make-csv csv_list))
(print (ls-dir "/tmp"))
(write-file "/tmp/file" "write-file test\n")
(print (is-file? "/tmp/file"))
(print (is-file? "/tmp/file_not_exists"))
(print (is-dir? "/tmp"))
(print (get-universal-time))
(print (date-to-str (get-universal-time) "%d.%m.%Y"))
(print (str-to-date "01.01.1970" "%d.%m.%Y"))
(print (date-add (str-to-date "01.01.1970" "%d.%m.%Y") 1 "day"))
(print (string-regex? "test.lsp" "^.*\.l(i)?sp$"))
(if (> 2 1)
(print "2 > 1"))
(if (> 2 10)
(print "2 > 10"))
(define csv_list '())
(for f (ls-dir "tests/divi")
(if (string-regex? f "^divi.*\.csv$")
(do
(define filename (+ "tests/divi/" f))
; (print filename)
(define csv_str (read-file filename))
(define csv_file_list (parse-csv csv_str))
(define csv_list (+ csv_list csv_file_list))
)
))
;; (for x csv_list (print x))
(print "(len csv_list) : " (len csv_list))
(define my_tickers '("WFC" "AIG"))
; (print (filter (lambda (x) (= (first x) "CLX")) csv_list))
(print (filter (lambda (x) (member my_tickers (first x))) csv_list))
(define l '(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))
(print "list:" l)
(print "uniq-ed list:" (uniq l))
(define l '(1 2 (3 3 (4)) 5 6))
(print "list:" l)
(print "flatten-ed list:" (flatten l))
(print (sprintf "%.2f" (list 1.25)))
(print (sprintf "%.2f" '(1.23456)))
(print (sprintf "%d" '(10000000)))
(define q 1.23)
(print (sprintf "%.2f" (list q)))
(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))) " "
))
(print (get-env "HOME"))
(print (string-rtrim "abc "))
(print (string-ltrim " abc"))
(print (string-trim " abc "))
(print "Test ends")