put test programs into their own file; altered brain-test and core.clj accordingly

This commit is contained in:
Richard Harrington 2013-08-09 22:01:56 -04:00
parent 7618378083
commit 1cf9f7e42e
3 changed files with 60 additions and 45 deletions

View File

@ -1,16 +1,15 @@
(ns robotwar.core
(:use [clojure.pprint])
(:require [robotwar.assembler :as assembler]
[robotwar.brain :as brain]
[robotwar.robot :as robot]
(:require [robotwar.test-programs :as test-programs]
[robotwar.world :as world]
[robotwar.brain-test :as brain-test]
[robotwar.register :as register]))
; this is a hacky place for messing with stuff. currently imports
; all the test data from brain-test.
; this is a hacky place for messing with stuff.
(def world
(world/init-world 256 256 [test-programs/multi-use-program]))
(def worlds (iterate world/tick-world world))
(def world (nth brain-test/worlds 0))
(def robots (:robots world))
(def robot (robots 0))
(def registers (:registers robot))
@ -19,8 +18,6 @@
(defn rv [reg-name] (get-in registers [reg-name :val]))
; ppt uses some of those variables to
; pretty-print a robot-state with line numbers for the obj-code instructions,
; and only the registers you want. Very convenient.
@ -48,8 +45,4 @@
(sort (zipmap (range) (get-in
brain
[:obj-code :instrs]))))
:registers (sort (into {} (for [[reg-name reg-map]
(select-keys
registers
reg-keys)]
{reg-name (:val reg-map)})))})))))
:register (sort (select-keys registers reg-keys))})))))

View File

@ -2,75 +2,68 @@
(:use [clojure.test]
[robotwar.brain])
(:require [robotwar.world :as world]
[robotwar.register :as register]))
[robotwar.register :as register]
[robotwar.test-programs :as test-programs]))
(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 INDEX/DATA pair of registers
" 300 TO A
1 TO INDEX
DATA " ])
(def initial-multi-use-world
(world/init-world 256 256 [test-programs/multi-use-program]))
(def initial-index-data-world
(world/init-world 256 256 [test-programs/index-data-program]))
(def initial-world (world/init-world 256 256 src-codes))
(def worlds (iterate world/tick-world initial-world))
(def multi-use-worlds (iterate world/tick-world initial-multi-use-world))
(def index-data-worlds (iterate world/tick-world initial-index-data-world))
(deftest branching-test
(testing "comparison statement should cause jump in instr-ptr"
(is (= (get-in (world/get-world 4 0 worlds) [:robots 0 :brain :instr-ptr])
(is (= (get-in (world/get-world 4 0 multi-use-worlds)
[:robots 0 :brain :instr-ptr])
5))))
(deftest arithmetic-test
(testing "addition"
(is (= (get-in (world/get-world 7 0 worlds) [:robots 0 :brain :acc])
(is (= (get-in (world/get-world 7 0 multi-use-worlds)
[:robots 0 :brain :acc])
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]}
(get-in (world/get-world 5 0 worlds) [:robots 0 :brain])]
(get-in (world/get-world 5 0 multi-use-worlds)
[:robots 0 :brain])]
(= [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]}
(get-in (world/get-world 9 0 worlds) [:robots 0 :brain])]
(get-in (world/get-world 9 0 multi-use-worlds)
[:robots 0 :brain])]
(= [instr-ptr call-stack]
[6 []])))))
(deftest push-test
(testing "pushing number to register"
(is (= (get-in (world/get-world 8 0 worlds) [:robots 0 :brain :registers "A" :val])
(is (= (get-in (world/get-world 8 0 multi-use-worlds)
[:robots 0 :brain :registers "A" :val])
1))))
(deftest index-data-pair-test
(testing "registers whose index numbers are push to INDEX can
be referenced by accessing DATA"
(is (= (get-in (world/get-world 5 1 worlds) [:robots 1 :brain :registers "A" :val])
(is (= (get-in (world/get-world 5 0 index-data-worlds)
[:robots 0 :brain :registers "A" :val])
300))))
; remaining tests will use a different method:
; last test will use a different method:
; just push and pull from one sample world and one sample robot
(def sample-world (world/get-world 0 0 worlds))
(def sample-robot ((:robots sample-world) 0))
(def initial-multi-use-robot ((:robots initial-multi-use-world) 0))
(deftest random-test
(testing "push to random register and pull a series of numbers all different
from random register"
(let [random-register (get-in sample-robot [:brain :registers "RANDOM"])
new-world (register/write-register random-register sample-world 1000)
(let [random-register (get-in initial-multi-use-robot [:brain :registers "RANDOM"])
new-world (register/write-register random-register initial-multi-use-world 1000)
random-nums (repeatedly 5 (partial register/read-register random-register new-world))]
(is (= (get-in new-world [:robots 0 :brain :registers "RANDOM" :val])
1000))

View File

@ -0,0 +1,29 @@
(ns robotwar.test-programs)
(def 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 ")
(def index-data-program
; to test the INDEX/DATA pair of registers
" 300 TO A
1 TO INDEX
DATA TO B")
(def random-program
; to test the RANDOM register
" 1000 TO RANDOM
RANDOM TO A
RANDOM TO A
RANDOM TO A
RANDOM TO A
RANDOM TO A ")