Added unit tests for about half the bootstrap functions

This commit is contained in:
Simon Brooke 2019-08-16 19:16:44 +01:00
parent 3f4e873e74
commit 10b2ad5322
4 changed files with 153 additions and 46 deletions

View file

@ -3,7 +3,7 @@
[clojure.test :refer :all]
[beowulf.cons-cell :refer [make-beowulf-list make-cons-cell NIL T F]]
[beowulf.eval :refer :all]
[beowulf.read :refer [parse simplify generate]]))
[beowulf.read :refer [gsp]]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
@ -19,7 +19,7 @@
actual (primitive-atom T)]
(is (= actual expected) "T is an atom (symbol)"))
(let [expected T
actual (primitive-atom (generate (simplify (parse "HELLO"))))]
actual (primitive-atom (gsp "HELLO"))]
(is (= actual expected) "HELLO is an atom (symbol)"))
(let [expected T
actual (primitive-atom 7)]
@ -30,14 +30,14 @@
actual (primitive-atom (make-cons-cell 'A 'B))]
(is (= actual expected) "A dotted pair is explicitly not an atom."))
(let [expected F
actual (primitive-atom (generate (simplify (parse "(A B C D)"))))]
actual (primitive-atom (gsp "(A B C D)"))]
(is (= actual expected) "A list is explicitly not an atom")))
(testing "primitive-atom?"
(let [expected T
actual (primitive-atom? T)]
(is (= actual expected) "T is an atom (symbol)"))
(let [expected T
actual (primitive-atom? (generate (simplify (parse "HELLO"))))]
actual (primitive-atom? (gsp "HELLO"))]
(is (= actual expected) "HELLO is an atom (symbol)"))
(let [expected T
actual (primitive-atom? 7)]
@ -48,7 +48,7 @@
actual (primitive-atom? (make-cons-cell 'A 'B))]
(is (= actual expected) "A dotted pair is explicitly not an atom."))
(let [expected NIL
actual (primitive-atom? (generate (simplify (parse "(A B C D)"))))]
actual (primitive-atom? (gsp "(A B C D)"))]
(is (= actual expected) "A list is explicitly not an atom"))
))
@ -59,7 +59,7 @@
actual (car (make-cons-cell 'A 'B))]
(is (= actual expected) "A is car of (A . B)"))
(let [expected 'A
actual (car (generate (simplify (parse "(A B C D)"))))]
actual (car (gsp "(A B C D)"))]
(is (= actual expected) "A is car of (A B C D)"))
(is (thrown-with-msg?
Exception
@ -76,7 +76,7 @@
actual (cdr (make-cons-cell 'A 'B))]
(is (= actual expected) "B is cdr of (A . B)"))
(let [expected 'B
actual (cdr (generate (simplify (parse "(A B C D)"))))]
actual (cdr (gsp "(A B C D)"))]
(is (instance? beowulf.cons_cell.ConsCell actual)
"cdr of (A B C D) is a cons cell")
(is (= (car actual) expected) "the car of that cons-cell is B"))
@ -90,22 +90,23 @@
#"Cannot take CDR of `.*"
(cdr 7))
"Can't take the cdr of a number"))
(let [s (generate
(simplify
(parse
"((((1 . 2) 3)(4 5) 6)(7 (8 9) (10 11 12) 13) 14 (15 16) 17)")))]
(let [s (gsp "((((1 . 2) 3)(4 5) 6)(7 (8 9) (10 11 12) 13) 14 (15 16) 17)")]
;; structure for testing access functions
(testing "cadr"
(let [expected 'B
actual (cadr (generate (simplify (parse "(A B C D)"))))]
actual (cadr (gsp "(A B C D)"))]
(is (= actual expected))))
(testing "caddr"
(let [expected 'C
actual (caddr (generate (simplify (parse "(A B C D)"))))]
(is (= actual expected))))
actual (caddr (gsp "(A B C D)"))]
(is (= actual expected)))
(let [expected 14
actual (caddr s)]
(is (= actual expected)))
)
(testing "cadddr"
(let [expected 'D
actual (cadddr (generate (simplify (parse "(A B C D)"))))]
actual (cadddr (gsp "(A B C D)"))]
(is (= actual expected))))
(testing "caaaar"
(let [expected "1"
@ -114,3 +115,74 @@
))
(deftest equality-tests
(testing "eq"
(let [expected 'T
actual (eq 'FRED 'FRED)]
(is (= actual expected) "identical symbols"))
(let [expected 'F
actual (eq 'FRED 'ELFREDA)]
(is (= actual expected) "different symbols"))
(let [expected 'F
l (gsp "(NOT AN ATOM)")
actual (eq l l)]
(is (= actual expected) "identical lists (eq is not defined for lists)")))
(testing "equal"
(let [expected 'T
actual (equal 'FRED 'FRED)]
(is (= actual expected) "identical symbols"))
(let [expected 'F
actual (equal 'FRED 'ELFREDA)]
(is (= actual expected) "different symbols"))
(let [expected 'T
l (gsp "(NOT AN ATOM)")
actual (equal l l)]
(is (= actual expected) "same list, same content"))
(let [expected 'T
l (gsp "(NOT AN ATOM)")
m (gsp "(NOT AN ATOM)")
actual (equal l m)]
(is (= actual expected) "different lists, same content"))
(let [expected 'F
l (gsp "(NOT AN ATOM)")
m (gsp "(NOT REALLY AN ATOM)")
actual (equal l m)]
(is (= actual expected) "different lists, different content"))))
(deftest substitution-tests
(testing "subst"
(let [expected "((A X . A) . C)"
;; differs from example in book only because of how the function
;; `beowulf.cons-cell/to-string` formats lists.
actual (print-str
(subst
(gsp "(X . A)")
(gsp "B")
(gsp "((A . B) . C)")))]
(is (= actual expected)))))
(deftest append-tests
(testing "append"
(let [expected "(A B C D E)"
actual (print-str
(append
(gsp "(A B)")
(gsp "(C D E)")))]
(is (= actual expected)))))
(deftest member-tests
(testing "member"
(let [expected 'T
actual (member (gsp "ALBERT") (gsp "(ALBERT BELINDA CHARLIE DORIS ELFREDA FRED)"))]
(= actual expected))
(let [expected 'T
actual (member (gsp "BELINDA") (gsp "(ALBERT BELINDA CHARLIE DORIS ELFREDA FRED)"))]
(= actual expected))
(let [expected 'T
actual (member (gsp "ELFREDA") (gsp "(ALBERT BELINDA CHARLIE DORIS ELFREDA FRED)"))]
(= actual expected))
(let [expected 'F
actual (member (gsp "BERTRAM") (gsp "(ALBERT BELINDA CHARLIE DORIS ELFREDA FRED)"))]
(= actual expected))))