First pass at adding opengl interface
[clnl] / src / test / 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 for test in tests
17         for result = (run-and-print-test test)
18         do (setf final-result (and final-result result)))
19   final-result))
20
21 (defun run-all-tests ()
22  (format t "~%~c[1;33mHere we goooooooo~c[0m~%" #\Esc #\Esc)
23  (run-tests (reverse *tests*)))
24   
25 (defun run-tests-matching (match)
26  (run-tests (remove-if-not (lambda (test-name) (cl-ppcre:scan (format nil "^~A$" match) test-name)) *tests* :key #'car)))
27
28 (defun find-test (name)
29  (or
30   (find name *tests* :test #'string= :key #'car)
31   (error "Couldn't find test with name: ~A" name)))
32
33 (defun test-debug (name) (format t "----~%~A~%" (funcall (third (find-test name)))))
34 (defun test-scala-prog (name) (format t "----~%~A~%" (fourth (find-test name))))
35 (defun test-scala-input (name) (format t "----~%~A~%" (fifth (find-test name))))
36
37 (defmacro defsimpletest (name test-fn debug-fn scala-prog scala-input)
38  `(progn
39    ;(when (find-test ,name) (error "Test with name ~S already exists, abort, abort" ,name))
40    (push
41     (list ,name ,test-fn ,debug-fn ,scala-prog ,scala-input)
42     *tests*)))
43
44 (defun checksum= (expected got)
45  (if (stringp expected)
46      (string= got expected)
47      (find got expected :test #'string=)))
48
49 ; To be used only with the simplest of tests, just a list of commands and a checksum of the
50 ; world after they've been run.
51 (defmacro defsimplecommandtest (name commands checksum)
52  `(defsimpletest
53    (format nil "Simple Command - ~A" ,name)
54    (lambda ()
55     (clnl:boot)
56     (clnl:run-commands ,commands)
57     (checksum= ,checksum (checksum-world)))
58    (lambda ()
59     (clnl:boot)
60     (clnl:run-commands ,commands)
61     (format nil "~A~A"
62      (clnl-nvm:export-world)
63      (checksum-world)))
64    "bin/runcmd.scala"
65    (format nil "~A~%" ,commands)))
66
67 (defmacro defsimplereportertest (name reporter value checksum)
68  `(defsimpletest
69    (format nil "Simple Reporter - ~A" ,name)
70    (lambda ()
71     (clnl:boot)
72     (and
73      (string= (clnl-nvm:dump-object (clnl:run-reporter ,reporter)) ,value)
74      (checksum= ,checksum (checksum-world))))
75    (lambda ()
76     (clnl:boot)
77     (format nil "~A~%~A~A"
78      (clnl-nvm:dump-object (clnl:run-reporter ,reporter))
79      (clnl-nvm:export-world)
80      (checksum-world)))
81    "bin/runreporter.scala"
82    (format nil "~A~%" ,reporter)))
83
84 (defmacro defviewtest (name commands checksum)
85  `(defsimpletest
86    (format nil "Simple View - ~A" ,name)
87    (lambda ()
88     (clnl:boot)
89     (clnl:run-commands ,commands)
90     (let
91      ((viewsum (checksum-view)))
92      (when (not (checksum= ,checksum viewsum))
93       (format t "~c[1;35m-- For ~A, got ~A but expected ~A~c[0m~%" #\Esc ,name viewsum ,checksum #\Esc))
94      (checksum= ,checksum (checksum-view))))
95    (lambda ()
96     (clnl:boot)
97     (clnl:run-commands ,commands)
98     (save-view-to-ppm)
99     (format nil "~A" (checksum-view)))
100    ""
101    (format nil "~A~%" ,commands)))
102
103 (defun checksum-world ()
104  (format nil "~{~2,'0X~}"
105   (map 'list #'identity
106    (ironclad:digest-sequence
107     :sha1
108     (map '(vector (unsigned-byte 8)) #'char-code (clnl-nvm:export-world))))))
109
110 (defun checksum-view ()
111  (format nil "~{~2,'0X~}"
112   (map 'list #'identity
113    (ironclad:digest-sequence :sha1 (coerce (clnl-interface:export-view) '(vector (unsigned-byte 8)))))))
114
115 (defun save-view-to-ppm ()
116  (let
117   ((height 143) (width 143)) ; hardcoded in interface, hardcoded here, cry for me
118   (with-open-file (str "cl.ppm" :direction :output :if-exists :supersede :if-does-not-exist :create :element-type '(unsigned-byte 8))
119    (write-sequence (map 'vector #'char-code (format nil "P6~%")) str)
120    (write-sequence (map 'vector #'char-code (format nil "143 143~%")) str)
121    (write-sequence (map 'vector #'char-code (format nil "255~%")) str)
122    (let
123     ((image-data (clnl-interface:export-view)))
124     (dotimes (i width)
125      (dotimes (j height)
126       (write-byte (aref image-data (+ 0 (* 4 (+ (* (- (1- height) i) width) j)))) str)
127       (write-byte (aref image-data (+ 1 (* 4 (+ (* (- (1- height) i) width) j)))) str)
128       (write-byte (aref image-data (+ 2 (* 4 (+ (* (- (1- height) i) width) j)))) str)))))))
129
130 (defun run ()
131  (loop for str = (progn (format t "> ") (force-output) (read-line))
132        while str
133        do (progn (asdf:load-system :clnl-test) (run-tests-matching str))))