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