diff options
Diffstat (limited to 'test/src/floatfns-tests.el')
| -rw-r--r-- | test/src/floatfns-tests.el | 168 |
1 files changed, 162 insertions, 6 deletions
diff --git a/test/src/floatfns-tests.el b/test/src/floatfns-tests.el index aa4e55e4897..aa709e3c2f5 100644 --- a/test/src/floatfns-tests.el +++ b/test/src/floatfns-tests.el | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | ;;; floatfns-tests.el --- tests for floating point operations | 1 | ;;; floatfns-tests.el --- tests for floating point operations -*- lexical-binding: t -*- |
| 2 | 2 | ||
| 3 | ;; Copyright 2017 Free Software Foundation, Inc. | 3 | ;; Copyright 2017-2022 Free Software Foundation, Inc. |
| 4 | 4 | ||
| 5 | ;; This file is part of GNU Emacs. | 5 | ;; This file is part of GNU Emacs. |
| 6 | 6 | ||
| @@ -17,13 +17,77 @@ | |||
| 17 | ;; You should have received a copy of the GNU General Public License | 17 | ;; You should have received a copy of the GNU General Public License |
| 18 | ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. | 18 | ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. |
| 19 | 19 | ||
| 20 | ;;; Code: | ||
| 21 | |||
| 20 | (require 'ert) | 22 | (require 'ert) |
| 21 | 23 | ||
| 24 | (ert-deftest floatfns-tests-cos () | ||
| 25 | (should (= (cos 0) 1.0)) | ||
| 26 | (should (= (cos float-pi) -1.0))) | ||
| 27 | |||
| 28 | (ert-deftest floatfns-tests-sin () | ||
| 29 | (should (= (sin 0) 0.0))) | ||
| 30 | |||
| 31 | (ert-deftest floatfns-tests-tan () | ||
| 32 | (should (= (tan 0) 0.0))) | ||
| 33 | |||
| 34 | (ert-deftest floatfns-tests-isnan () | ||
| 35 | (should (isnan 0.0e+NaN)) | ||
| 36 | (should (isnan -0.0e+NaN)) | ||
| 37 | (should-error (isnan "foo") :type 'wrong-type-argument)) | ||
| 38 | |||
| 39 | (ert-deftest floatfns-tests-exp () | ||
| 40 | (should (= (exp 0) 1.0))) | ||
| 41 | |||
| 42 | (ert-deftest floatfns-tests-expt () | ||
| 43 | (should (= (expt 2 8) 256))) | ||
| 44 | |||
| 45 | (ert-deftest floatfns-tests-log () | ||
| 46 | (should (= (log 1000 10) 3.0))) | ||
| 47 | |||
| 48 | (ert-deftest floatfns-tests-sqrt () | ||
| 49 | (should (= (sqrt 25) 5))) | ||
| 50 | |||
| 51 | (ert-deftest floatfns-tests-abs () | ||
| 52 | (should (= (abs 10) 10)) | ||
| 53 | (should (= (abs -10) 10))) | ||
| 54 | |||
| 55 | (ert-deftest floatfns-tests-logb () | ||
| 56 | (should (= (logb 10000) 13))) | ||
| 57 | |||
| 58 | (ert-deftest floatfns-tests-ceiling () | ||
| 59 | (should (= (ceiling 0.5) 1))) | ||
| 60 | |||
| 61 | (ert-deftest floatfns-tests-floor () | ||
| 62 | (should (= (floor 1.5) 1))) | ||
| 63 | |||
| 64 | (ert-deftest floatfns-tests-round () | ||
| 65 | (should (= (round 1.49999999999) 1)) | ||
| 66 | (should (= (round 1.50000000000) 2)) | ||
| 67 | (should (= (round 1.50000000001) 2))) | ||
| 68 | |||
| 69 | (ert-deftest floatfns-tests-truncate () | ||
| 70 | (should (= (truncate float-pi) 3))) | ||
| 71 | |||
| 72 | (ert-deftest floatfns-tests-fceiling () | ||
| 73 | (should (= (fceiling 0.5) 1.0))) | ||
| 74 | |||
| 75 | (ert-deftest floatfns-tests-ffloor () | ||
| 76 | (should (= (ffloor 1.5) 1.0))) | ||
| 77 | |||
| 78 | (ert-deftest floatfns-tests-fround () | ||
| 79 | (should (= (fround 1.49999999999) 1.0)) | ||
| 80 | (should (= (fround 1.50000000000) 2.0)) | ||
| 81 | (should (= (fround 1.50000000001) 2.0))) | ||
| 82 | |||
| 83 | (ert-deftest floatfns-tests-ftruncate () | ||
| 84 | (should (= (ftruncate float-pi) 3.0))) | ||
| 85 | |||
| 22 | (ert-deftest divide-extreme-sign () | 86 | (ert-deftest divide-extreme-sign () |
| 23 | (should-error (ceiling most-negative-fixnum -1.0)) | 87 | (should (= (ceiling most-negative-fixnum -1.0) (- most-negative-fixnum))) |
| 24 | (should-error (floor most-negative-fixnum -1.0)) | 88 | (should (= (floor most-negative-fixnum -1.0) (- most-negative-fixnum))) |
| 25 | (should-error (round most-negative-fixnum -1.0)) | 89 | (should (= (round most-negative-fixnum -1.0) (- most-negative-fixnum))) |
| 26 | (should-error (truncate most-negative-fixnum -1.0))) | 90 | (should (= (truncate most-negative-fixnum -1.0) (- most-negative-fixnum)))) |
| 27 | 91 | ||
| 28 | (ert-deftest logb-extreme-fixnum () | 92 | (ert-deftest logb-extreme-fixnum () |
| 29 | (should (= (logb most-negative-fixnum) (1+ (logb most-positive-fixnum))))) | 93 | (should (= (logb most-negative-fixnum) (1+ (logb most-positive-fixnum))))) |
| @@ -34,4 +98,96 @@ | |||
| 34 | (should-error (ftruncate 0) :type 'wrong-type-argument) | 98 | (should-error (ftruncate 0) :type 'wrong-type-argument) |
| 35 | (should-error (fround 0) :type 'wrong-type-argument)) | 99 | (should-error (fround 0) :type 'wrong-type-argument)) |
| 36 | 100 | ||
| 101 | (ert-deftest bignum-to-float () | ||
| 102 | ;; 122 because we want to go as big as possible to provoke a rounding error, | ||
| 103 | ;; but not too big: 2**122 < 10**37 < 2**123, and the C standard says | ||
| 104 | ;; 10**37 <= DBL_MAX so 2**122 cannot overflow as a double. | ||
| 105 | (let ((a (1- (ash 1 122)))) | ||
| 106 | (should (or (eql a (1- (floor (float a)))) | ||
| 107 | (eql a (floor (float a)))))) | ||
| 108 | (should (eql (float (+ most-positive-fixnum 1)) | ||
| 109 | (+ (float most-positive-fixnum) 1)))) | ||
| 110 | |||
| 111 | (ert-deftest bignum-abs () | ||
| 112 | (should (= most-positive-fixnum | ||
| 113 | (- (abs most-negative-fixnum) 1)))) | ||
| 114 | |||
| 115 | (ert-deftest bignum-expt () | ||
| 116 | (dolist (n (list most-positive-fixnum (1+ most-positive-fixnum) | ||
| 117 | most-negative-fixnum (1- most-negative-fixnum) | ||
| 118 | (* 5 most-negative-fixnum) | ||
| 119 | (* 5 (1+ most-positive-fixnum)) | ||
| 120 | -2 -1 0 1 2)) | ||
| 121 | (should (or (<= n 0) (= (expt 0 n) 0))) | ||
| 122 | (should (= (expt 1 n) 1)) | ||
| 123 | (should (or (< n 0) (= (expt -1 n) (if (zerop (logand n 1)) 1 -1)))) | ||
| 124 | (should (= (expt n 0) 1)) | ||
| 125 | (should (= (expt n 1) n)) | ||
| 126 | (should (= (expt n 2) (* n n))) | ||
| 127 | (should (= (expt n 3) (* n n n))))) | ||
| 128 | |||
| 129 | (ert-deftest bignum-logb () | ||
| 130 | (should (= (+ (logb most-positive-fixnum) 1) | ||
| 131 | (logb (+ most-positive-fixnum 1))))) | ||
| 132 | |||
| 133 | (ert-deftest bignum-mod () | ||
| 134 | (should (= 0 (mod (1+ most-positive-fixnum) 2.0)))) | ||
| 135 | |||
| 136 | (ert-deftest bignum-round () | ||
| 137 | (let ((ns (list (* most-positive-fixnum most-negative-fixnum) | ||
| 138 | (1- most-negative-fixnum) most-negative-fixnum | ||
| 139 | (1+ most-negative-fixnum) -2 1 1 2 | ||
| 140 | (1- most-positive-fixnum) most-positive-fixnum | ||
| 141 | (1+ most-positive-fixnum) | ||
| 142 | (* most-positive-fixnum most-positive-fixnum)))) | ||
| 143 | (dolist (n ns) | ||
| 144 | (should (= n (ceiling n))) | ||
| 145 | (should (= n (floor n))) | ||
| 146 | (should (= n (round n))) | ||
| 147 | (should (= n (truncate n))) | ||
| 148 | (let ((-n (- n)) | ||
| 149 | (f (float n)) | ||
| 150 | (-f (- (float n)))) | ||
| 151 | (should (= 1 (round n f) (round -n -f) (round f n) (round -f -n))) | ||
| 152 | (should (= -1 (round -n f) (round n -f) (round f -n) (round -f n)))) | ||
| 153 | (dolist (d ns) | ||
| 154 | (let ((q (/ n d)) | ||
| 155 | (r (% n d)) | ||
| 156 | (same-sign (eq (< n 0) (< d 0)))) | ||
| 157 | (should (= (ceiling n d) | ||
| 158 | (+ q (if (and same-sign (not (zerop r))) 1 0)))) | ||
| 159 | (should (= (floor n d) | ||
| 160 | (- q (if (and (not same-sign) (not (zerop r))) 1 0)))) | ||
| 161 | (should (= (truncate n d) q)) | ||
| 162 | (let ((cdelta (abs (- n (* d (ceiling n d))))) | ||
| 163 | (fdelta (abs (- n (* d (floor n d))))) | ||
| 164 | (rdelta (abs (- n (* d (round n d)))))) | ||
| 165 | (should (<= rdelta cdelta)) | ||
| 166 | (should (<= rdelta fdelta)) | ||
| 167 | (should (if (zerop r) | ||
| 168 | (= 0 cdelta fdelta rdelta) | ||
| 169 | (or (/= cdelta fdelta) | ||
| 170 | (zerop (% (round n d) 2))))))))))) | ||
| 171 | |||
| 172 | (ert-deftest special-round () | ||
| 173 | (dolist (f '(ceiling floor round truncate)) | ||
| 174 | (let ((ns '(-1e+INF 1e+INF -1 -0.0 0.0 0 1 -1e+NaN 1e+NaN))) | ||
| 175 | (dolist (n ns) | ||
| 176 | (if (not (<= (abs n) 1)) | ||
| 177 | (should-error (funcall f n)) | ||
| 178 | (should (= n (funcall f n))) | ||
| 179 | (dolist (d '(-1e+INF 1e+INF)) | ||
| 180 | (should (eq 0 (funcall f n d))))) | ||
| 181 | (dolist (d ns) | ||
| 182 | (when (or (zerop d) (= (abs n) 1e+INF) (not (= n n)) (not (= d d))) | ||
| 183 | (should-error (funcall f n d)))))))) | ||
| 184 | |||
| 185 | (ert-deftest big-round () | ||
| 186 | (should (= (floor 54043195528445955 3) | ||
| 187 | (floor 54043195528445955 3.0))) | ||
| 188 | (should (= (floor 1.7976931348623157e+308 5e-324) | ||
| 189 | (ash (1- (ash 1 53)) 2045)))) | ||
| 190 | |||
| 37 | (provide 'floatfns-tests) | 191 | (provide 'floatfns-tests) |
| 192 | |||
| 193 | ;;; floatfns-tests.el ends here | ||