1
1
mirror of https://github.com/kanaka/mal.git synced 2024-11-10 12:47:45 +03:00
mal/tests/step4_if_fn_do.mal
2016-02-01 15:10:12 -06:00

454 lines
5.3 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) "yes" "no")
;=>"no"
(if (>= (count (list 1 2 3)) 3) "yes" "no")
;=>"yes"
;; Testing if form
(if true 7 8)
;=>7
(if false 7 8)
;=>8
(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 "" 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
(= "abc" "")
;=>false
(= "" "abc")
;=>false
(= "abc" "def")
;=>false
(= (list) (list))
;=>true
(= (list 1 2) (list 1 2))
;=>true
(= (list 1) (list))
;=>false
(= (list) (list 1))
;=>false
(= 0 (list))
;=>false
(= (list) 0)
;=>false
(= (list) "")
;=>false
(= "" (list))
;=>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 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 true)
;=>false
(not "a")
;=>false
(not 0)
;=>false
;; Testing do form
(do (prn "prn output1"))
; "prn output1"
;=>nil
(do (prn "prn output2") 7)
; "prn output2"
;=>7
(do (prn "prn output1") (prn "prn output2") (+ 1 2))
; "prn output1"
; "prn output2"
;=>3
(do (def! a 6) 7 (+ a 8))
;=>14
a
;=>6
;; 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 string quoting
""
;=>""
"abc"
;=>"abc"
"abc def"
;=>"abc def"
"\""
;=>"\""
"abc\ndef\nghi"
;=>"abc\ndef\nghi"
"abc\\def\\ghi"
;=>"abc\\def\\ghi"
;; 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 [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))
;=>"()"
(pr-str [])
;=>"[]"
;; 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 [1 2 "abc" "\""] "def")
;=>"[1 2 abc \"]def"
(str (list))
;=>"()"
(str [])
;=>"[]"
;; 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 Functionality --------
;; Testing keywords
(= :abc :abc)
;=>true
(= :abc :def)
;=>false
(= :abc ":abc")
;=>false
;; Testing vector truthiness
(if [] 7 8)
;=>7
;; 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