1 ; Copyright 2022 Frank Duncan (frank@consxy.com) under AGPL3. See distributed LICENSE.txt.
2 (in-package #:clnl-test)
4 (defparameter *tests* nil)
6 (defun run-and-print-test (test)
8 ((green (format nil "~c[1;32m" #\Esc))
9 (red (format nil "~c[1;31m" #\Esc))
10 (result (funcall (cadr test))))
11 (format t "~A- ~S ~A~c[0m~%" (if result green red) (car test) (if result "passed" "failed") #\Esc)
14 (defun run-tests (tests)
19 :for result := (run-and-print-test test)
20 :do (setf final-result (and final-result result)))
23 (defun run-all-tests ()
24 (run-tests (reverse *tests*)))
26 (defun run-tests-matching (match)
28 (remove-if-not (lambda (test-name) (cl-ppcre:scan (format nil "^~A$" match) test-name)) *tests* :key #'car)))
30 (defun find-test (name)
32 (find name *tests* :test #'string= :key #'car)
33 (error "Couldn't find test with name: ~A" name)))
35 (defun test-debug (name) (format t "----~%~A~%" (funcall (third (find-test name)))))
36 (defun test-scala-prog (name) (format t "----~%~A~%" (fourth (find-test name))))
37 (defun test-scala-input (name) (format t "----~%~A~%" (fifth (find-test name))))
39 (defun clnl-commands (commands) (if (listp commands) (car commands) commands))
40 (defun scala-commands (commands) (if (listp commands) (cadr commands) commands))
42 (defmacro defsimpletest (name test-fn debug-fn scala-prog scala-input)
44 ;(when (find-test ,name) (error "Test with name ~S already exists, abort, abort" ,name))
46 (list ,name ,test-fn ,debug-fn ,scala-prog ,scala-input)
49 (defun checksum= (expected got)
50 (if (stringp expected)
51 (string= got expected)
52 (find got expected :test #'string=)))
54 ; To be used only with the simplest of tests, just a list of commands and a checksum of the
55 ; world after they've been run.
56 (defmacro defsimplecommandtest (name commands checksum)
58 (format nil "Simple Command - ~A" ,name)
60 (clnl:boot "resources/clnl/empty.nlogo" t)
61 (clnl:run-commands ,commands)
62 (checksum= ,checksum (checksum-world)))
64 (clnl:boot "resources/clnl/empty.nlogo" t)
65 (clnl:run-commands ,commands)
67 (clnl-nvm:export-world)
70 (format nil "~A~%" ,commands)))
72 (defmacro defsimplereportertest (name reporter value checksum)
74 (format nil "Simple Reporter - ~A" ,name)
76 (clnl:boot "resources/clnl/empty.nlogo" t)
78 (string= (funcall (intern "DUMP-OBJECT" :clnl-nvm) (clnl:run-reporter ,reporter)) ,value)
79 (checksum= ,checksum (checksum-world))))
81 (clnl:boot "resources/clnl/empty.nlogo" t)
82 (format nil "~A~%~A~A"
83 (funcall (intern "DUMP-OBJECT" :clnl-nvm) (clnl:run-reporter ,reporter))
84 (clnl-nvm:export-world)
87 (format nil "@#$#@#$#@~A" ,reporter)))
89 (defmacro defreportertestwithsetup (name setup reporter value checksum)
91 (format nil "Reporter With Setup - ~A" ,name)
93 (clnl:boot "resources/clnl/empty.nlogo" t)
94 (clnl:run-commands ,setup)
96 (string= (funcall (intern "DUMP-OBJECT" :clnl-nvm) (clnl:run-reporter ,reporter)) ,value)
97 (checksum= ,checksum (checksum-world))))
99 (clnl:boot "resources/clnl/empty.nlogo" t)
100 (clnl:run-commands ,setup)
101 (format nil "~A~%~A~A"
102 (funcall (intern "DUMP-OBJECT" :clnl-nvm) (clnl:run-reporter ,reporter))
103 (clnl-nvm:export-world)
106 (format nil "~A@#$#@#$#@~A" ,setup ,reporter)))
108 (defun model-code->nlogo (code)
112 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~%
117 (defmacro defmodeltest (name model commands reporter value checksum)
122 ((model (with-input-from-string (str ,(model-code->nlogo model)) (clnl-model:read-from-nlogo str))))
126 (declaim (sb-ext:muffle-conditions cl:warning))
127 (eval (clnl:model->single-form-lisp model :netlogo-callback (lambda (f) (setf callback f))))
128 (when ,commands (funcall callback ,commands))
130 (or (not ,reporter) (string= (funcall (intern "DUMP-OBJECT" :clnl-nvm) (funcall callback ,reporter)) ,value))
131 (checksum= ,checksum (checksum-world))))
133 ((pkg (make-package (gensym)))
134 (clnl:*model-package* pkg)
135 (prev-package *package*))
139 (clnl:model->multi-form-lisp model (intern "BOOT-ME" pkg)
140 :netlogo-callback-fn (intern "NETLOGO-CALLBACK" pkg))))
141 (eval `(in-package ,(package-name prev-package)))
142 (funcall (symbol-function (intern "BOOT-ME" pkg)))
143 (when ,commands (funcall (symbol-function (intern "NETLOGO-CALLBACK" pkg)) ,commands))
148 (funcall (intern "DUMP-OBJECT" :clnl-nvm) (funcall (intern "NETLOGO-CALLBACK" pkg) ,reporter))
150 (checksum= ,checksum (checksum-world)))))))
154 (declaim (sb-ext:muffle-conditions cl:warning))
156 (clnl:model->single-form-lisp
157 (with-input-from-string (str ,(model-code->nlogo model)) (clnl-model:read-from-nlogo str))
158 :netlogo-callback (lambda (f) (setf callback f))))
159 (when ,commands (funcall callback ,commands))
161 (if ,reporter (format nil "~A~%" (funcall (intern "DUMP-OBJECT" :clnl-nvm) (funcall callback ,reporter))) "")
162 (clnl-nvm:export-world)
165 (format nil "~A@#$#@#$#@~A@#$#@#$#@~A" ,commands (or ,reporter "") ,model)))
167 (defmacro defmodelcommandtest (name model commands checksum)
168 `(defmodeltest (format nil "Model Command - ~A" ,name) ,model ,commands nil nil ,checksum))
170 (defmacro defmodelreportertest (name model commands reporter value checksum)
171 `(defmodeltest (format nil "Model Reporter - ~A" ,name) ,model ,commands ,reporter ,value ,checksum))
173 (defun wait-for-forever ()
177 (lambda (name) (cl-ppcre:scan "Forever button:" name))
178 (mapcar #'sb-thread:thread-name (sb-thread:list-all-threads)))
181 (defmacro defmodelfiletest (name file commands checksum &optional wait-for-forever)
183 ,(format nil "File Model - ~A" name)
186 ((model (with-open-file (str ,file) (clnl-model:read-from-nlogo str))))
190 (declaim (sb-ext:muffle-conditions cl:warning))
191 (eval (clnl:model->single-form-lisp model :netlogo-callback (lambda (f) (setf callback f))))
192 (when ,(clnl-commands commands) (funcall callback ,(clnl-commands commands)))
193 ,(when wait-for-forever `(wait-for-forever))
194 (checksum= ,checksum (checksum-world)))
196 ((pkg (make-package (gensym)))
197 (clnl:*model-package* pkg)
198 (prev-package *package*))
202 (clnl:model->multi-form-lisp model (intern "BOOT-ME" pkg)
203 :netlogo-callback-fn (intern "NETLOGO-CALLBACK" pkg))))
204 (eval `(in-package ,(package-name prev-package)))
205 (funcall (symbol-function (intern "BOOT-ME" pkg)))
206 (when ,(clnl-commands commands)
207 (funcall (symbol-function (intern "NETLOGO-CALLBACK" pkg)) ,(clnl-commands commands)))
208 ,(when wait-for-forever `(wait-for-forever))
209 (checksum= ,checksum (checksum-world))))))
213 (declaim (sb-ext:muffle-conditions cl:warning))
215 (clnl:model->single-form-lisp
216 (with-open-file (str ,file) (clnl-model:read-from-nlogo str))
217 :netlogo-callback (lambda (f) (setf callback f))))
218 (when ,(clnl-commands commands) (funcall callback ,(clnl-commands commands)))
219 ,(when wait-for-forever `(wait-for-forever))
221 (clnl-nvm:export-world)
224 (format nil "~A@#$#@#$#@@#$#@#$#@@#$#@#$#@~A" ,(scala-commands commands) ,file)))
226 (defmacro defviewtest (name commands checksum)
228 (format nil "Simple View - ~A" ,name)
230 (clnl:boot "resources/clnl/empty55.nlogo")
231 (clnl:run-commands ,commands)
233 ((viewsum (checksum-view)))
234 (when (not (checksum= ,checksum viewsum))
235 (format t "~c[1;35m-- For ~A, got ~A but expected ~A~c[0m~%" #\Esc ,name viewsum ,checksum #\Esc))
236 (checksum= ,checksum (checksum-view))))
238 (clnl:boot "resources/clnl/empty55.nlogo")
239 (clnl:run-commands ,commands)
241 (format nil "~A" (checksum-view)))
243 (format nil "~A~%" ,commands)))
245 (defun checksum-world ()
246 (format nil "~{~2,'0X~}"
247 (map 'list #'identity
248 (ironclad:digest-sequence
250 (map '(vector (unsigned-byte 8)) #'char-code (clnl-nvm:export-world))))))
252 (defun checksum-view ()
253 (format nil "~{~2,'0X~}"
254 (map 'list #'identity
255 (ironclad:digest-sequence :sha1 (coerce (clnl-interface:export-view) '(vector (unsigned-byte 8)))))))
257 (defun save-view-to-ppm ()
259 ((height 143) (width 143)) ; hardcoded in interface, hardcoded here, cry for me
260 (with-open-file (str "cl.ppm"
262 :if-exists :supersede
263 :if-does-not-exist :create
264 :element-type '(unsigned-byte 8))
265 (write-sequence (map 'vector #'char-code (format nil "P6~%")) str)
266 (write-sequence (map 'vector #'char-code (format nil "143 143~%")) str)
267 (write-sequence (map 'vector #'char-code (format nil "255~%")) str)
269 ((image-data (clnl-interface:export-view)))
272 (write-byte (aref image-data (+ 0 (* 4 (+ (* (- (1- height) i) width) j)))) str)
273 (write-byte (aref image-data (+ 1 (* 4 (+ (* (- (1- height) i) width) j)))) str)
274 (write-byte (aref image-data (+ 2 (* 4 (+ (* (- (1- height) i) width) j)))) str)))))))
278 :for str := (progn (format t "> ") (force-output) (read-line))
283 (asdf:load-system :clnl-test)
284 (run-tests-matching str))
285 (error (e) (format t "Ok, something went wrong: ~A~%" e)))))