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 (defun clnl-commands (commands) (if (listp commands) (car commands) commands))
39 (defun scala-commands (commands) (if (listp commands) (cadr commands) commands))
41 (defmacro defsimpletest (name test-fn debug-fn scala-prog scala-input)
43 ;(when (find-test ,name) (error "Test with name ~S already exists, abort, abort" ,name))
45 (list ,name ,test-fn ,debug-fn ,scala-prog ,scala-input)
48 (defun checksum= (expected got)
49 (if (stringp expected)
50 (string= got expected)
51 (find got expected :test #'string=)))
53 ; To be used only with the simplest of tests, just a list of commands and a checksum of the
54 ; world after they've been run.
55 (defmacro defsimplecommandtest (name commands checksum)
57 (format nil "Simple Command - ~A" ,name)
59 (clnl:boot "resources/empty.nlogo" t)
60 (clnl:run-commands ,commands)
61 (checksum= ,checksum (checksum-world)))
63 (clnl:boot "resources/empty.nlogo" t)
64 (clnl:run-commands ,commands)
66 (clnl-nvm:export-world)
69 (format nil "~A~%" ,commands)))
71 (defmacro defsimplereportertest (name reporter value checksum)
73 (format nil "Simple Reporter - ~A" ,name)
75 (clnl:boot "resources/empty.nlogo" t)
77 (string= (funcall (intern "DUMP-OBJECT" :clnl-nvm) (clnl:run-reporter ,reporter)) ,value)
78 (checksum= ,checksum (checksum-world))))
80 (clnl:boot "resources/empty.nlogo" t)
81 (format nil "~A~%~A~A"
82 (funcall (intern "DUMP-OBJECT" :clnl-nvm) (clnl:run-reporter ,reporter))
83 (clnl-nvm:export-world)
86 (format nil "@#$#@#$#@~A" ,reporter)))
88 (defmacro defreportertestwithsetup (name setup reporter value checksum)
90 (format nil "Reporter With Setup - ~A" ,name)
92 (clnl:boot "resources/empty.nlogo" t)
93 (clnl:run-commands ,setup)
95 (string= (funcall (intern "DUMP-OBJECT" :clnl-nvm) (clnl:run-reporter ,reporter)) ,value)
96 (checksum= ,checksum (checksum-world))))
98 (clnl:boot "resources/empty.nlogo" t)
99 (clnl:run-commands ,setup)
100 (format nil "~A~%~A~A"
101 (funcall (intern "DUMP-OBJECT" :clnl-nvm) (clnl:run-reporter ,reporter))
102 (clnl-nvm:export-world)
105 (format nil "~A@#$#@#$#@~A" ,setup ,reporter)))
107 (defun model-code->nlogo (code)
111 GRAPHICS-WINDOW~%210~%10~%649~%470~%-1~%-1~%13.0~%1~%10~%1~%1~%1~%0~%1~%1~%1~%-1~%1~%-1~%1~%0~%0~%1~%ticks~%30.0~%
116 (defmacro defmodeltest (name model commands reporter value checksum)
121 ((model (with-input-from-string (str ,(model-code->nlogo model)) (clnl-model:read-from-nlogo str))))
125 (declaim (sb-ext:muffle-conditions cl:warning))
126 (eval (clnl:model->single-form-lisp model :netlogo-callback (lambda (f) (setf callback f))))
127 (when ,commands (funcall callback ,commands))
129 (or (not ,reporter) (string= (funcall (intern "DUMP-OBJECT" :clnl-nvm) (funcall callback ,reporter)) ,value))
130 (checksum= ,checksum (checksum-world))))
132 ((pkg (make-package (gensym)))
133 (clnl:*model-package* pkg)
134 (prev-package *package*))
138 (clnl:model->multi-form-lisp model (intern "BOOT-ME" pkg)
139 :netlogo-callback-fn (intern "NETLOGO-CALLBACK" pkg))))
140 (eval `(in-package ,(package-name prev-package)))
141 (funcall (symbol-function (intern "BOOT-ME" pkg)))
142 (when ,commands (funcall (symbol-function (intern "NETLOGO-CALLBACK" pkg)) ,commands))
147 (funcall (intern "DUMP-OBJECT" :clnl-nvm) (funcall (intern "NETLOGO-CALLBACK" pkg) ,reporter))
149 (checksum= ,checksum (checksum-world)))))))
153 (declaim (sb-ext:muffle-conditions cl:warning))
155 (clnl:model->single-form-lisp
156 (with-input-from-string (str ,(model-code->nlogo model)) (clnl-model:read-from-nlogo str))
157 :netlogo-callback (lambda (f) (setf callback f))))
158 (when ,commands (funcall callback ,commands))
160 (if ,reporter (format nil "~A~%" (funcall (intern "DUMP-OBJECT" :clnl-nvm) (funcall callback ,reporter))) "")
161 (clnl-nvm:export-world)
164 (format nil "~A@#$#@#$#@~A@#$#@#$#@~A" ,commands (or ,reporter "") ,model)))
166 (defmacro defmodelcommandtest (name model commands checksum)
167 `(defmodeltest (format nil "Model Command - ~A" ,name) ,model ,commands nil nil ,checksum))
169 (defmacro defmodelreportertest (name model commands reporter value checksum)
170 `(defmodeltest (format nil "Model Reporter - ~A" ,name) ,model ,commands ,reporter ,value ,checksum))
172 (defun wait-for-forever ()
176 (lambda (name) (cl-ppcre:scan "Forever button:" name))
177 (mapcar #'sb-thread:thread-name (sb-thread:list-all-threads)))
180 (defmacro defmodelfiletest (name file commands checksum &optional wait-for-forever)
182 ,(format nil "File Model - ~A" name)
185 ((model (with-open-file (str ,file) (clnl-model:read-from-nlogo str))))
189 (declaim (sb-ext:muffle-conditions cl:warning))
190 (eval (clnl:model->single-form-lisp model :netlogo-callback (lambda (f) (setf callback f))))
191 (when ,(clnl-commands commands) (funcall callback ,(clnl-commands commands)))
192 ,(when wait-for-forever `(wait-for-forever))
193 (checksum= ,checksum (checksum-world)))
195 ((pkg (make-package (gensym)))
196 (clnl:*model-package* pkg)
197 (prev-package *package*))
201 (clnl:model->multi-form-lisp model (intern "BOOT-ME" pkg)
202 :netlogo-callback-fn (intern "NETLOGO-CALLBACK" pkg))))
203 (eval `(in-package ,(package-name prev-package)))
204 (funcall (symbol-function (intern "BOOT-ME" pkg)))
205 (when ,(clnl-commands commands)
206 (funcall (symbol-function (intern "NETLOGO-CALLBACK" pkg)) ,(clnl-commands commands)))
207 ,(when wait-for-forever `(wait-for-forever))
208 (checksum= ,checksum (checksum-world))))))
212 (declaim (sb-ext:muffle-conditions cl:warning))
214 (clnl:model->single-form-lisp
215 (with-open-file (str ,file) (clnl-model:read-from-nlogo str))
216 :netlogo-callback (lambda (f) (setf callback f))))
217 (when ,(clnl-commands commands) (funcall callback ,(clnl-commands commands)))
218 ,(when wait-for-forever `(wait-for-forever))
220 (clnl-nvm:export-world)
223 (format nil "~A@#$#@#$#@@#$#@#$#@@#$#@#$#@~A" ,(scala-commands commands) ,file)))
225 (defmacro defviewtest (name commands checksum)
227 (format nil "Simple View - ~A" ,name)
229 (clnl:boot "resources/empty55.nlogo")
230 (clnl:run-commands ,commands)
232 ((viewsum (checksum-view)))
233 (when (not (checksum= ,checksum viewsum))
234 (format t "~c[1;35m-- For ~A, got ~A but expected ~A~c[0m~%" #\Esc ,name viewsum ,checksum #\Esc))
235 (checksum= ,checksum (checksum-view))))
237 (clnl:boot "resources/empty55.nlogo")
238 (clnl:run-commands ,commands)
240 (format nil "~A" (checksum-view)))
242 (format nil "~A~%" ,commands)))
244 (defun checksum-world ()
245 (format nil "~{~2,'0X~}"
246 (map 'list #'identity
247 (ironclad:digest-sequence
249 (map '(vector (unsigned-byte 8)) #'char-code (clnl-nvm:export-world))))))
251 (defun checksum-view ()
252 (format nil "~{~2,'0X~}"
253 (map 'list #'identity
254 (ironclad:digest-sequence :sha1 (coerce (clnl-interface:export-view) '(vector (unsigned-byte 8)))))))
256 (defun save-view-to-ppm ()
258 ((height 143) (width 143)) ; hardcoded in interface, hardcoded here, cry for me
259 (with-open-file (str "cl.ppm"
261 :if-exists :supersede
262 :if-does-not-exist :create
263 :element-type '(unsigned-byte 8))
264 (write-sequence (map 'vector #'char-code (format nil "P6~%")) str)
265 (write-sequence (map 'vector #'char-code (format nil "143 143~%")) str)
266 (write-sequence (map 'vector #'char-code (format nil "255~%")) str)
268 ((image-data (clnl-interface:export-view)))
271 (write-byte (aref image-data (+ 0 (* 4 (+ (* (- (1- height) i) width) j)))) str)
272 (write-byte (aref image-data (+ 1 (* 4 (+ (* (- (1- height) i) width) j)))) str)
273 (write-byte (aref image-data (+ 2 (* 4 (+ (* (- (1- height) i) width) j)))) str)))))))
277 :for str := (progn (format t "> ") (force-output) (read-line))
282 (asdf:load-system :clnl-test)
283 (run-tests-matching str))
284 (error (e) (format t "Ok, something went wrong: ~A~%" e)))))