407949e5027000db64fc6810bc994eaa16c5f876
[clnl] / src / test / clnl / main.lisp
1 (in-package #:clnl-test)
2
3 (defparameter *tests* nil)
4
5 (defun run-and-print-test (test)
6  (let
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)
11   result))
12
13 (defun run-tests (tests)
14  (let
15   ((final-result t))
16   (loop
17    :for test :in tests
18    :for result := (run-and-print-test test)
19    :do (setf final-result (and final-result result)))
20   final-result))
21
22 (defun run-all-tests ()
23  (run-tests (reverse *tests*)))
24
25 (defun run-tests-matching (match)
26  (run-tests
27   (remove-if-not (lambda (test-name) (cl-ppcre:scan (format nil "^~A$" match) test-name)) *tests* :key #'car)))
28
29 (defun find-test (name)
30  (or
31   (find name *tests* :test #'string= :key #'car)
32   (error "Couldn't find test with name: ~A" name)))
33
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))))
37
38 (defun clnl-commands (commands) (if (listp commands) (car commands) commands))
39 (defun scala-commands (commands) (if (listp commands) (cadr commands) commands))
40
41 (defmacro defsimpletest (name test-fn debug-fn scala-prog scala-input)
42  `(progn
43    ;(when (find-test ,name) (error "Test with name ~S already exists, abort, abort" ,name))
44    (push
45     (list ,name ,test-fn ,debug-fn ,scala-prog ,scala-input)
46     *tests*)))
47
48 (defun checksum= (expected got)
49  (if (stringp expected)
50   (string= got expected)
51   (find got expected :test #'string=)))
52
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)
56  `(defsimpletest
57    (format nil "Simple Command - ~A" ,name)
58    (lambda ()
59     (clnl:boot "resources/clnl/empty.nlogo" t)
60     (clnl:run-commands ,commands)
61     (checksum= ,checksum (checksum-world)))
62    (lambda ()
63     (clnl:boot "resources/clnl/empty.nlogo" t)
64     (clnl:run-commands ,commands)
65     (format nil "~A~A"
66      (clnl-nvm:export-world)
67      (checksum-world)))
68    "bin/runcmd.scala"
69    (format nil "~A~%" ,commands)))
70
71 (defmacro defsimplereportertest (name reporter value checksum)
72  `(defsimpletest
73    (format nil "Simple Reporter - ~A" ,name)
74    (lambda ()
75     (clnl:boot "resources/clnl/empty.nlogo" t)
76     (and
77      (string= (funcall (intern "DUMP-OBJECT" :clnl-nvm) (clnl:run-reporter ,reporter)) ,value)
78      (checksum= ,checksum (checksum-world))))
79    (lambda ()
80     (clnl:boot "resources/clnl/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)
84      (checksum-world)))
85    "bin/runcmd.scala"
86    (format nil "@#$#@#$#@~A" ,reporter)))
87
88 (defmacro defreportertestwithsetup (name setup reporter value checksum)
89  `(defsimpletest
90    (format nil "Reporter With Setup - ~A" ,name)
91    (lambda ()
92     (clnl:boot "resources/clnl/empty.nlogo" t)
93     (clnl:run-commands ,setup)
94     (and
95      (string= (funcall (intern "DUMP-OBJECT" :clnl-nvm) (clnl:run-reporter ,reporter)) ,value)
96      (checksum= ,checksum (checksum-world))))
97    (lambda ()
98     (clnl:boot "resources/clnl/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)
103      (checksum-world)))
104    "bin/runcmd.scala"
105    (format nil "~A@#$#@#$#@~A" ,setup ,reporter)))
106
107 (defun model-code->nlogo (code)
108  (format nil
109   "~A
110 @#$#@#$#@
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~%
112 @#$#@#$#@
113 "
114   code))
115
116 (defmacro defmodeltest (name model commands reporter value checksum)
117  `(defsimpletest
118    ,name
119    (lambda ()
120     (let
121      ((model (with-input-from-string (str ,(model-code->nlogo model)) (clnl-model:read-from-nlogo str))))
122      (and
123       (let
124        ((callback nil))
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))
128        (and
129         (or (not ,reporter) (string= (funcall (intern "DUMP-OBJECT" :clnl-nvm) (funcall callback ,reporter)) ,value))
130         (checksum= ,checksum (checksum-world))))
131       (let*
132        ((pkg (make-package (gensym)))
133         (clnl:*model-package* pkg)
134         (prev-package *package*))
135        (eval
136         (cons
137          'progn
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))
143        (and
144         (or
145          (not ,reporter)
146          (string=
147           (funcall (intern "DUMP-OBJECT" :clnl-nvm) (funcall (intern "NETLOGO-CALLBACK" pkg) ,reporter))
148           ,value))
149         (checksum= ,checksum (checksum-world)))))))
150    (lambda ()
151     (let
152      ((callback nil))
153      (declaim (sb-ext:muffle-conditions cl:warning))
154      (eval
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))
159      (format nil "~A~A~A"
160       (if ,reporter (format nil "~A~%" (funcall (intern "DUMP-OBJECT" :clnl-nvm) (funcall callback ,reporter))) "")
161       (clnl-nvm:export-world)
162       (checksum-world))))
163    "bin/runcmd.scala"
164    (format nil "~A@#$#@#$#@~A@#$#@#$#@~A" ,commands (or ,reporter "") ,model)))
165
166 (defmacro defmodelcommandtest (name model commands checksum)
167  `(defmodeltest (format nil "Model Command - ~A" ,name) ,model ,commands nil nil ,checksum))
168
169 (defmacro defmodelreportertest (name model commands reporter value checksum)
170  `(defmodeltest (format nil "Model Reporter - ~A" ,name) ,model ,commands ,reporter ,value ,checksum))
171
172 (defun wait-for-forever ()
173  (loop
174   :while
175   (find-if
176    (lambda (name) (cl-ppcre:scan "Forever button:" name))
177    (mapcar #'sb-thread:thread-name (sb-thread:list-all-threads)))
178   :do (sleep .1)))
179
180 (defmacro defmodelfiletest (name file commands checksum &optional wait-for-forever)
181  `(defsimpletest
182    ,(format nil "File Model - ~A" name)
183    (lambda ()
184     (let
185      ((model (with-open-file (str ,file) (clnl-model:read-from-nlogo str))))
186      (and
187       (let
188        ((callback nil))
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)))
194       (let*
195        ((pkg (make-package (gensym)))
196         (clnl:*model-package* pkg)
197         (prev-package *package*))
198        (eval
199         (cons
200          'progn
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))))))
209    (lambda ()
210     (let
211      ((callback nil))
212      (declaim (sb-ext:muffle-conditions cl:warning))
213      (eval
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))
219      (format nil "~A~A"
220       (clnl-nvm:export-world)
221       (checksum-world))))
222    "bin/runcmd.scala"
223    (format nil "~A@#$#@#$#@@#$#@#$#@@#$#@#$#@~A" ,(scala-commands commands) ,file)))
224
225 (defmacro defviewtest (name commands checksum)
226  `(defsimpletest
227    (format nil "Simple View - ~A" ,name)
228    (lambda ()
229     (clnl:boot "resources/clnl/empty55.nlogo")
230     (clnl:run-commands ,commands)
231     (let
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))))
236    (lambda ()
237     (clnl:boot "resources/clnl/empty55.nlogo")
238     (clnl:run-commands ,commands)
239     (save-view-to-ppm)
240     (format nil "~A" (checksum-view)))
241    ""
242    (format nil "~A~%" ,commands)))
243
244 (defun checksum-world ()
245  (format nil "~{~2,'0X~}"
246   (map 'list #'identity
247    (ironclad:digest-sequence
248     :sha1
249     (map '(vector (unsigned-byte 8)) #'char-code (clnl-nvm:export-world))))))
250
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)))))))
255
256 (defun save-view-to-ppm ()
257  (let
258   ((height 143) (width 143)) ; hardcoded in interface, hardcoded here, cry for me
259   (with-open-file (str "cl.ppm"
260                    :direction :output
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)
267    (let
268     ((image-data (clnl-interface:export-view)))
269     (dotimes (i width)
270      (dotimes (j height)
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)))))))
274
275 (defun run ()
276  (loop
277   :for str := (progn (format t "> ") (force-output) (read-line))
278   :while str
279   :do
280   (handler-case
281    (progn
282     (asdf:load-system :clnl-test)
283     (run-tests-matching str))
284    (error (e) (format t "Ok, something went wrong: ~A~%" e)))))