refactored exec-test and wrote new test for pushing and pulling from RANDOM register

This commit is contained in:
Richard Harrington 2013-07-31 15:33:09 -04:00
parent e15147c679
commit dacfaf34db

View File

@ -3,44 +3,72 @@
(robotwar [create :refer :all]
[exec :refer :all])))
(def src-code " START
0 TO A
TEST
IF A > 2 GOTO START
GOSUB INCREMENT
GOTO TEST
100 TO A
INCREMENT
A + 1 TO A
ENDSUB
200 TO A ")
(def src-codes [ ; program 0: multi-use program
" START
0 TO A
TEST
IF A > 2 GOTO START
GOSUB INCREMENT
GOTO TEST
100 TO A
INCREMENT
A + 1 TO A
ENDSUB
200 TO A "
; program 1: to test RANDOM register
" 1000 TO RANDOM
RANDOM
RANDOM
RANDOM
RANDOM
RANDOM
RANDOM
RANDOM
RANDOM
RANDOM
RANDOM " ])
(def initial-state (init-robot-state (assemble src-code) {}))
(def states (iterate tick-robot initial-state))
(def robot-history #(iterate tick-robot (init-robot-state (assemble %) {})))
(def robot-histories (map robot-history src-codes))
(def multi-use-history (nth robot-histories 0))
(def random-check-history (nth robot-histories 1))
(deftest branching-test
(testing "comparison statement should cause jump in instr-ptr"
(is (= (:instr-ptr (nth states 4))
(is (= (:instr-ptr (nth multi-use-history 4))
5))))
(deftest arithmetic-test
(testing "addition"
(is (= (:acc (nth states 7))
(is (= (:acc (nth multi-use-history 7))
1))))
(deftest gosub-test
(testing "gosub should move instr-ptr and add the return-ptr to the call stack"
(is (let [{:keys [instr-ptr call-stack]} (nth states 5)]
(is (let [{:keys [instr-ptr call-stack]} (nth multi-use-history 5)]
(= [instr-ptr call-stack]
[9 [6]])))))
(deftest endsub-test
(testing "endsub pops instr-ptr off call stack and goes there"
(is (let [{:keys [instr-ptr call-stack]} (nth states 9)]
(is (let [{:keys [instr-ptr call-stack]} (nth multi-use-history 9)]
(= [instr-ptr call-stack]
[6 []])))))
(deftest push-test
(testing "pushing number to register"
(is (= (get-in (nth states 8) [:registers "A"])
(is (= (get-in (nth multi-use-history 8) [:registers "A"])
1))))
(deftest random-test
(testing "push to random register and pull from it to receive a number
of unequal numbers less than the number that was pushed"
(is (let [random-pairs (map (fn [n]
(let [{{random "RANDOM"} :registers, acc :acc}
(nth random-check-history n)]
[random acc]))
(range 3 13))]
(and (every? #{1000} (map first random-pairs))
(every? #(< -1 % 1000) (map second random-pairs))
(apply not= (map second random-pairs)))))))