1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-11 13:55:55 +03:00
mal/tests/step4_if_fn_do.mal
Nicolas Boulenguez 8734e2eb01 Test mutual recursion in let* without vectors, which are deferrable.
Thanks to bjh21 for spotting this inconsistency.
2019-05-28 16:36:21 +02:00

495 lines
5.9 KiB
Plaintext

;; -----------------------------------------------------
;; Testing list functions
(list)
;=>()
(list? (list))
;=>true
(empty? (list))
;=>true
(empty? (list 1))
;=>false
(list 1 2 3)
;=>(1 2 3)
(count (list 1 2 3))
;=>3
(count (list))
;=>0
(count nil)
;=>0
(if (> (count (list 1 2 3)) 3) 89 78)
;=>78
(if (>= (count (list 1 2 3)) 3) 89 78)
;=>89
;; Testing if form
(if true 7 8)
;=>7
(if false 7 8)
;=>8
(if false 7 false)
;=>false
(if true (+ 1 7) (+ 1 8))
;=>8
(if false (+ 1 7) (+ 1 8))
;=>9
(if nil 7 8)
;=>8
(if 0 7 8)
;=>7
(if (list) 7 8)
;=>7
(if (list 1 2 3) 7 8)
;=>7
(= (list) nil)
;=>false
;; Testing 1-way if form
(if false (+ 1 7))
;=>nil
(if nil 8 7)
;=>7
(if true (+ 1 7))
;=>8
;; Testing basic conditionals
(= 2 1)
;=>false
(= 1 1)
;=>true
(= 1 2)
;=>false
(= 1 (+ 1 1))
;=>false
(= 2 (+ 1 1))
;=>true
(= nil 1)
;=>false
(= nil nil)
;=>true
(> 2 1)
;=>true
(> 1 1)
;=>false
(> 1 2)
;=>false
(>= 2 1)
;=>true
(>= 1 1)
;=>true
(>= 1 2)
;=>false
(< 2 1)
;=>false
(< 1 1)
;=>false
(< 1 2)
;=>true
(<= 2 1)
;=>false
(<= 1 1)
;=>true
(<= 1 2)
;=>true
;; Testing equality
(= 1 1)
;=>true
(= 0 0)
;=>true
(= 1 0)
;=>false
(= true true)
;=>true
(= false false)
;=>true
(= nil nil)
;=>true
(= (list) (list))
;=>true
(= (list 1 2) (list 1 2))
;=>true
(= (list 1) (list))
;=>false
(= (list) (list 1))
;=>false
(= 0 (list))
;=>false
(= (list) 0)
;=>false
;; Testing builtin and user defined functions
(+ 1 2)
;=>3
( (fn* (a b) (+ b a)) 3 4)
;=>7
( (fn* () 4) )
;=>4
( (fn* (f x) (f x)) (fn* (a) (+ 1 a)) 7)
;=>8
;; Testing closures
( ( (fn* (a) (fn* (b) (+ a b))) 5) 7)
;=>12
(def! gen-plus5 (fn* () (fn* (b) (+ 5 b))))
(def! plus5 (gen-plus5))
(plus5 7)
;=>12
(def! gen-plusX (fn* (x) (fn* (b) (+ x b))))
(def! plus7 (gen-plusX 7))
(plus7 8)
;=>15
;; Testing do form
(do (prn 101))
;/101
;=>nil
(do (prn 102) 7)
;/102
;=>7
(do (prn 101) (prn 102) (+ 1 2))
;/101
;/102
;=>3
(do (def! a 6) 7 (+ a 8))
;=>14
a
;=>6
;; Testing special form case-sensitivity
(def! DO (fn* (a) 7))
(DO 3)
;=>7
;; Testing recursive sumdown function
(def! sumdown (fn* (N) (if (> N 0) (+ N (sumdown (- N 1))) 0)))
(sumdown 1)
;=>1
(sumdown 2)
;=>3
(sumdown 6)
;=>21
;; Testing recursive fibonacci function
(def! fib (fn* (N) (if (= N 0) 1 (if (= N 1) 1 (+ (fib (- N 1)) (fib (- N 2)))))))
(fib 1)
;=>1
(fib 2)
;=>2
(fib 4)
;=>5
;;; Too slow for bash, erlang, make and miniMAL
;;;(fib 10)
;;;;=>89
;; Testing recursive function in environment.
(let* (cst (fn* (n) (if (= n 0) nil (cst (- n 1))))) (cst 1))
;=>nil
(let* (f (fn* (n) (if (= n 0) 0 (g (- n 1)))) g (fn* (n) (f n))) (f 2))
;=>0
;>>> deferrable=True
;;
;; -------- Deferrable Functionality --------
;; Testing if on strings
(if "" 7 8)
;=>7
;; Testing string equality
(= "" "")
;=>true
(= "abc" "abc")
;=>true
(= "abc" "")
;=>false
(= "" "abc")
;=>false
(= "abc" "def")
;=>false
(= "abc" "ABC")
;=>false
(= (list) "")
;=>false
(= "" (list))
;=>false
;; Testing variable length arguments
( (fn* (& more) (count more)) 1 2 3)
;=>3
( (fn* (& more) (list? more)) 1 2 3)
;=>true
( (fn* (& more) (count more)) 1)
;=>1
( (fn* (& more) (count more)) )
;=>0
( (fn* (& more) (list? more)) )
;=>true
( (fn* (a & more) (count more)) 1 2 3)
;=>2
( (fn* (a & more) (count more)) 1)
;=>0
( (fn* (a & more) (list? more)) 1)
;=>true
;; Testing language defined not function
(not false)
;=>true
(not nil)
;=>true
(not true)
;=>false
(not "a")
;=>false
(not 0)
;=>false
;; -----------------------------------------------------
;; Testing string quoting
""
;=>""
"abc"
;=>"abc"
"abc def"
;=>"abc def"
"\""
;=>"\""
"abc\ndef\nghi"
;=>"abc\ndef\nghi"
"abc\\def\\ghi"
;=>"abc\\def\\ghi"
"\\n"
;=>"\\n"
;; Testing pr-str
(pr-str)
;=>""
(pr-str "")
;=>"\"\""
(pr-str "abc")
;=>"\"abc\""
(pr-str "abc def" "ghi jkl")
;=>"\"abc def\" \"ghi jkl\""
(pr-str "\"")
;=>"\"\\\"\""
(pr-str (list 1 2 "abc" "\"") "def")
;=>"(1 2 \"abc\" \"\\\"\") \"def\""
(pr-str "abc\ndef\nghi")
;=>"\"abc\\ndef\\nghi\""
(pr-str "abc\\def\\ghi")
;=>"\"abc\\\\def\\\\ghi\""
(pr-str (list))
;=>"()"
;; Testing str
(str)
;=>""
(str "")
;=>""
(str "abc")
;=>"abc"
(str "\"")
;=>"\""
(str 1 "abc" 3)
;=>"1abc3"
(str "abc def" "ghi jkl")
;=>"abc defghi jkl"
(str "abc\ndef\nghi")
;=>"abc\ndef\nghi"
(str "abc\\def\\ghi")
;=>"abc\\def\\ghi"
(str (list 1 2 "abc" "\"") "def")
;=>"(1 2 abc \")def"
(str (list))
;=>"()"
;; Testing prn
(prn)
;/
;=>nil
(prn "")
;/""
;=>nil
(prn "abc")
;/"abc"
;=>nil
(prn "abc def" "ghi jkl")
;/"abc def" "ghi jkl"
(prn "\"")
;/"\\""
;=>nil
(prn "abc\ndef\nghi")
;/"abc\\ndef\\nghi"
;=>nil
(prn "abc\\def\\ghi")
;/"abc\\\\def\\\\ghi"
nil
(prn (list 1 2 "abc" "\"") "def")
;/\(1 2 "abc" "\\""\) "def"
;=>nil
;; Testing println
(println)
;/
;=>nil
(println "")
;/
;=>nil
(println "abc")
;/abc
;=>nil
(println "abc def" "ghi jkl")
;/abc def ghi jkl
(println "\"")
;/"
;=>nil
(println "abc\ndef\nghi")
;/abc
;/def
;/ghi
;=>nil
(println "abc\\def\\ghi")
;/abc\\def\\ghi
;=>nil
(println (list 1 2 "abc" "\"") "def")
;/\(1 2 abc "\) def
;=>nil
;>>> optional=True
;;
;; -------- Optional Functionality --------
;; Testing keywords
(= :abc :abc)
;=>true
(= :abc :def)
;=>false
(= :abc ":abc")
;=>false
;; Testing vector truthiness
(if [] 7 8)
;=>7
;; Testing vector printing
(pr-str [1 2 "abc" "\""] "def")
;=>"[1 2 \"abc\" \"\\\"\"] \"def\""
(pr-str [])
;=>"[]"
(str [1 2 "abc" "\""] "def")
;=>"[1 2 abc \"]def"
(str [])
;=>"[]"
;; Testing vector functions
(count [1 2 3])
;=>3
(empty? [1 2 3])
;=>false
(empty? [])
;=>true
(list? [4 5 6])
;=>false
;; Testing vector equality
(= [] (list))
;=>true
(= [7 8] [7 8])
;=>true
(= (list 1 2) [1 2])
;=>true
(= (list 1) [])
;=>false
(= [] [1])
;=>false
(= 0 [])
;=>false
(= [] 0)
;=>false
(= [] "")
;=>false
(= "" [])
;=>false
;; Testing vector parameter lists
( (fn* [] 4) )
;=>4
( (fn* [f x] (f x)) (fn* [a] (+ 1 a)) 7)
;=>8
;; Nested vector/list equality
(= [(list)] (list []))
;=>true
(= [1 2 (list 3 4 [5 6])] (list 1 2 [3 4 (list 5 6)]))
;=>true