4805395dd92152f75b0027102dff029ea59be66a
[clnl] / src / test / strictmath / main.lisp
1 (in-package #:strictmath-test)
2
3 (defvar *tests* nil)
4
5 ; This really is just here to check against regressions
6 (defun run-all-tests ()
7  (let
8   ((results (mapcar #'funcall (reverse *tests*))))
9   (every #'identity results)))
10
11 (defun slurp-file (filename &key (element-type 'character) (sequence-type 'string))
12  (with-open-file (str filename :element-type element-type)
13   (let ((seq (make-sequence sequence-type (file-length str)))) (read-sequence seq str) seq)))
14
15 (defmacro deftest (name f)
16  `(push
17    (lambda ()
18     (let
19      ((success
20        (handler-case
21         (funcall ,f)
22         (error (e) (format t "Got unexpected error in tests: ~A" e)))))
23      (if success
24       (format t "~c[1;32m- ~A passed~c[0m~%" #\Esc ,name #\Esc)
25       (format t "~c[1;31m- ~A failed~c[0m~%" #\Esc ,name #\Esc))
26      success))
27    *tests*))
28
29 (deftest
30  "To radians"
31  (lambda ()
32   (every
33    #'identity
34    (mapcar
35     (lambda (pair)
36      (destructuring-bind (deg expected-rad) pair
37       (or
38        (= (strictmath:to-radians deg) expected-rad)
39        (format t "** Expected ~A but got ~A for ~A? **~%" expected-rad (strictmath:to-radians deg) deg))))
40     (with-open-file (str "resources/strictmath/testfiles/toRadiansData" :direction :input) (read str))))))
41
42 (deftest
43  "sin"
44  (lambda ()
45   (every
46    #'identity
47    (mapcar
48     (lambda (pair)
49      (destructuring-bind (deg expected-sin) pair
50       (or
51        (= (strictmath:sin (strictmath:to-radians deg)) expected-sin)
52        (format t "** Expected ~A but got ~A for ~A? **~%"
53         expected-sin
54         (strictmath:sin (strictmath:to-radians deg))
55         deg))))
56     (with-open-file (str "resources/strictmath/testfiles/sinData" :direction :input) (read str))))))
57
58 (deftest
59  "cos"
60  (lambda ()
61   (every
62    #'identity
63    (mapcar
64     (lambda (pair)
65      (destructuring-bind (deg expected-cos) pair
66       (or
67        (= (strictmath:cos (strictmath:to-radians deg)) expected-cos)
68        (format t "** Expected ~A but got ~A for ~A? **~%"
69         expected-cos
70         (strictmath:cos (strictmath:to-radians deg))
71         deg))))
72     (with-open-file (str "resources/strictmath/testfiles/cosData" :direction :input) (read str))))))