1 (in-package #:clnl-test)
3 (defparameter *tests* nil)
5 (defun run-and-print-test (test)
7 ((green (format nil "~c[1;32m" #\Esc))
8 (red (format nil "~c[1;31m" #\Esc))
9 (result (funcall (cadr test))))
10 (format t "~A- ~S ~A~c[0m~%" (if result green red) (car test) (if result "passed" "failed") #\Esc)
13 (defun run-tests (tests)
18 :for result := (run-and-print-test test)
19 :do (setf final-result (and final-result result)))
22 (defun run-all-tests ()
23 (run-tests (reverse *tests*)))
25 (defun run-tests-matching (match)
27 (remove-if-not (lambda (test-name) (cl-ppcre:scan (format nil "^~A$" match) test-name)) *tests* :key #'car)))
29 (defun find-test (name)
31 (find name *tests* :test #'string= :key #'car)
32 (error "Couldn't find test with name: ~A" name)))
34 (defun test-debug (name) (format t "----~%~A~%" (funcall (third (find-test name)))))
35 (defun test-scala-prog (name) (format t "----~%~A~%" (fourth (find-test name))))
36 (defun test-scala-input (name) (format t "----~%~A~%" (fifth (find-test name))))
38 (defmacro defsimpletest (name test-fn debug-fn scala-prog scala-input)
40 ;(when (find-test ,name) (error "Test with name ~S already exists, abort, abort" ,name))
42 (list ,name ,test-fn ,debug-fn ,scala-prog ,scala-input)
45 (defun checksum= (expected got)
46 (if (stringp expected)
47 (string= got expected)
48 (find got expected :test #'string=)))
50 ; To be used only with the simplest of tests, just a list of commands and a checksum of the
51 ; world after they've been run.
52 (defmacro defsimplecommandtest (name commands checksum)
54 (format nil "Simple Command - ~A" ,name)
56 (clnl:boot "resources/empty.nlogo")
57 (clnl:run-commands ,commands)
58 (checksum= ,checksum (checksum-world)))
60 (clnl:boot "resources/empty.nlogo")
61 (clnl:run-commands ,commands)
63 (clnl-nvm:export-world)
66 (format nil "~A~%" ,commands)))
68 (defmacro defsimplereportertest (name reporter value checksum)
70 (format nil "Simple Reporter - ~A" ,name)
72 (clnl:boot "resources/empty.nlogo")
74 (string= (funcall (intern "DUMP-OBJECT" :clnl-nvm) (clnl:run-reporter ,reporter)) ,value)
75 (checksum= ,checksum (checksum-world))))
77 (clnl:boot "resources/empty.nlogo")
78 (format nil "~A~%~A~A"
79 (funcall (intern "DUMP-OBJECT" :clnl-nvm) (clnl:run-reporter ,reporter))
80 (clnl-nvm:export-world)
83 (format nil "~%@#$#@#$#@~A~%" ,reporter)))
85 (defmacro defreportertestwithsetup (name setup reporter value checksum)
87 (format nil "Reporter With Setup - ~A" ,name)
89 (clnl:boot "resources/empty.nlogo")
90 (clnl:run-commands ,setup)
92 (string= (funcall (intern "DUMP-OBJECT" :clnl-nvm) (clnl:run-reporter ,reporter)) ,value)
93 (checksum= ,checksum (checksum-world))))
95 (clnl:boot "resources/empty.nlogo")
96 (clnl:run-commands ,setup)
97 (format nil "~A~%~A~A"
98 (funcall (intern "DUMP-OBJECT" :clnl-nvm) (clnl:run-reporter ,reporter))
99 (clnl-nvm:export-world)
102 (format nil "~A~%@#$#@#$#@~A" ,setup ,reporter)))
104 (defmacro defviewtest (name commands checksum)
106 (format nil "Simple View - ~A" ,name)
108 (clnl:boot "resources/empty55.nlogo")
109 (clnl:run-commands ,commands)
111 ((viewsum (checksum-view)))
112 (when (not (checksum= ,checksum viewsum))
113 (format t "~c[1;35m-- For ~A, got ~A but expected ~A~c[0m~%" #\Esc ,name viewsum ,checksum #\Esc))
114 (checksum= ,checksum (checksum-view))))
116 (clnl:boot "resources/empty55.nlogo")
117 (clnl:run-commands ,commands)
119 (format nil "~A" (checksum-view)))
121 (format nil "~A~%" ,commands)))
123 (defun checksum-world ()
124 (format nil "~{~2,'0X~}"
125 (map 'list #'identity
126 (ironclad:digest-sequence
128 (map '(vector (unsigned-byte 8)) #'char-code (clnl-nvm:export-world))))))
130 (defun checksum-view ()
131 (format nil "~{~2,'0X~}"
132 (map 'list #'identity
133 (ironclad:digest-sequence :sha1 (coerce (clnl-interface:export-view) '(vector (unsigned-byte 8)))))))
135 (defun save-view-to-ppm ()
137 ((height 143) (width 143)) ; hardcoded in interface, hardcoded here, cry for me
138 (with-open-file (str "cl.ppm"
140 :if-exists :supersede
141 :if-does-not-exist :create
142 :element-type '(unsigned-byte 8))
143 (write-sequence (map 'vector #'char-code (format nil "P6~%")) str)
144 (write-sequence (map 'vector #'char-code (format nil "143 143~%")) str)
145 (write-sequence (map 'vector #'char-code (format nil "255~%")) str)
147 ((image-data (clnl-interface:export-view)))
150 (write-byte (aref image-data (+ 0 (* 4 (+ (* (- (1- height) i) width) j)))) str)
151 (write-byte (aref image-data (+ 1 (* 4 (+ (* (- (1- height) i) width) j)))) str)
152 (write-byte (aref image-data (+ 2 (* 4 (+ (* (- (1- height) i) width) j)))) str)))))))
156 :for str := (progn (format t "> ") (force-output) (read-line))
158 :do (progn (asdf:load-system :clnl-test) (run-tests-matching str))))