1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
|
#| -*-Scheme-*-
Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
2017, 2018, 2019 Massachusetts Institute of Technology
This file is part of MIT/GNU Scheme.
MIT/GNU Scheme is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.
MIT/GNU Scheme is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with MIT/GNU Scheme; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
USA.
|#
;;;; Tests of the floating-point environment
;;; Many tests fail if there are accrued exceptions when you run them.
;;; This is pretty silly, but you can work around it provisionally by
;;; evaluating (FLO:CLEAR-EXCEPTIONS! (FLO:SUPPORTED-EXCEPTIONS))
;;; before running the tests.
(declare (usual-integrations))
(define-test 'FLO:DEFAULT-ROUNDING-MODE
(lambda ()
(assert-eqv (flo:default-rounding-mode) 'TO-NEAREST)))
(define-test 'FLO:ROUNDING-MODES
(lambda ()
(assert-eqv
(lset-difference eq?
(flo:rounding-modes)
'(TO-NEAREST TOWARD-ZERO DOWNWARD UPWARD))
'())))
(define-test 'FLO:ROUNDING-MODE
(lambda ()
(assert-memv (flo:rounding-mode) (flo:rounding-modes))))
(define (for-each-rounding-mode receiver)
(for-each receiver '(TO-NEAREST TOWARD-ZERO DOWNWARD UPWARD)))
(for-each-rounding-mode
(lambda (mode)
(define-test (symbol 'FLO:SET-ROUNDING-MODE ': mode)
(lambda ()
(let ((mode* (flo:rounding-mode)))
(flo:preserving-environment
(lambda ()
(flo:set-rounding-mode! mode)
(assert-eqv (flo:rounding-mode) mode)))
(assert-eqv (flo:rounding-mode) mode*))))))
(for-each-rounding-mode
(lambda (mode)
(define-test (symbol 'FLO:WITH-ROUNDING-MODE ': mode)
(lambda ()
(let ((mode* (flo:rounding-mode)))
(flo:with-rounding-mode mode
(lambda ()
(assert-eqv (flo:rounding-mode) mode)))
(assert-eqv (flo:rounding-mode) mode*))))))
(for-each-rounding-mode
(lambda (mode)
(define inputs '(-2.0 -1.5 -1.0 -0.5 -0.0 0.0 0.5 1.0 1.5 2.0))
(define (define-rounding-test name operator outputs)
(define-test (symbol 'ROUNDING-MODE-INDEPENDENT ': mode '/ name)
(lambda ()
(do ((inputs inputs (cdr inputs))
(outputs outputs (cdr outputs))
(i 0 (+ i 1)))
((not (and (pair? inputs) (pair? outputs))))
(let ((input (car inputs)) (output (car outputs)))
(run-sub-test
(lambda ()
(assert-eqv
(flo:with-rounding-mode mode (lambda () (operator input)))
output))))))))
(define-rounding-test 'CEILING ceiling
'(-2.0 -1.0 -1.0 -0.0 -0.0 0.0 1.0 1.0 2.0 2.0))
(define-rounding-test 'FLOOR floor
'(-2.0 -2.0 -1.0 -1.0 -0.0 0.0 0.0 1.0 1.0 2.0))
(define-rounding-test 'ROUND round
'(-2.0 -2.0 -1.0 -0.0 -0.0 0.0 0.0 1.0 2.0 2.0))
(define-rounding-test 'TRUNCATE truncate
'(-2.0 -1.0 -1.0 -0.0 -0.0 0.0 0.0 1.0 1.0 2.0))))
;++ Add tests for rounding-mode-dependent operations...
(define floating-point-exception-descriptors '())
(define (no-op x) x) ;Do not integrate!
(define (define-fpe-descriptor name trappable? exception condition-type)
(let ((descriptor (list name exception condition-type trappable? '())))
(cond ((assq name floating-point-exception-descriptors)
=> (lambda (descriptor*)
(set-cdr! descriptor* (cdr descriptor))))
(else
(set! floating-point-exception-descriptors
(cons descriptor floating-point-exception-descriptors))
unspecific))))
(define (define-fpe-elicitor name elicitor-name procedure)
(cond ((assq name floating-point-exception-descriptors)
=> (lambda (descriptor)
(let ((elicitors (list-ref descriptor 4)))
(cond ((assq elicitor-name elicitors)
=> (lambda (pair) (set-cdr! pair procedure)))
(else
(set-car! (list-tail descriptor 4)
(cons (cons elicitor-name procedure)
elicitors)))))))
(else
(error:bad-range-argument name 'DEFINE-FPE-ELICITOR))))
(define (for-each-exception receiver)
(for-each (lambda (descriptor)
(apply receiver descriptor))
floating-point-exception-descriptors))
(define (for-each-trappable-exception receiver)
(for-each-exception
(lambda (name exception condition-type trappable? elicitors)
(if (and trappable? (flo:have-trap-enable/disable?))
(receiver name exception condition-type elicitors)))))
(define (for-each-exception-elicitor receiver)
(for-each-exception
(lambda (name exception condition-type trappable? elicitors)
(for-each (lambda (name.elicitor)
(receiver name exception condition-type trappable?
(car name.elicitor)
(cdr name.elicitor)))
elicitors))))
(define (for-each-trappable-exception-elicitor receiver)
(for-each-trappable-exception
(lambda (name exception condition-type elicitors)
(for-each (lambda (name.elicitor)
(receiver name exception condition-type
(car name.elicitor)
(cdr name.elicitor)))
elicitors))))
(define-fpe-descriptor 'DIVIDE-BY-ZERO #t flo:exception:divide-by-zero
condition-type:floating-point-divide-by-zero)
(define-fpe-elicitor 'DIVIDE-BY-ZERO 'RAISE
(lambda () (flo:raise-exceptions! (flo:exception:divide-by-zero))))
(define-fpe-elicitor 'DIVIDE-BY-ZERO 'POSITIVE-ONE-OVER-ZERO
(lambda () (flo:/ (no-op +1.) (no-op 0.))))
(define-fpe-elicitor 'DIVIDE-BY-ZERO 'NEGATIVE-ONE-OVER-ZERO
(lambda () (flo:/ (no-op -1.) (no-op 0.))))
(define-fpe-elicitor 'DIVIDE-BY-ZERO 'LOG-ZERO
(lambda () (flo:log (no-op 0.))))
(define-fpe-descriptor 'INEXACT-RESULT #f flo:exception:inexact-result
condition-type:inexact-floating-point-result)
(define-fpe-elicitor 'INEXACT-RESULT 'RAISE
(lambda () (flo:raise-exceptions! (flo:exception:inexact-result))))
(define-fpe-elicitor 'INEXACT-RESULT 'ONE-PLUS-EPSILON-OVER-TWO
(lambda ()
(flo:+ (no-op 1.) (flo:* (no-op .5) microcode-id/floating-epsilon))))
(define-fpe-descriptor 'INVALID-OPERATION #t flo:exception:invalid-operation
condition-type:invalid-floating-point-operation)
(define-fpe-elicitor 'INVALID-OPERATION 'RAISE
(lambda () (flo:raise-exceptions! (flo:exception:invalid-operation))))
(define-fpe-elicitor 'INVALID-OPERATION 'ZERO-OVER-ZERO
(lambda () (flo:/ (no-op 0.) (no-op 0.))))
(define-fpe-descriptor 'OVERFLOW #t flo:exception:overflow
condition-type:floating-point-overflow)
(define-fpe-elicitor 'OVERFLOW 'RAISE
(lambda () (flo:raise-exceptions! (flo:exception:overflow))))
;++ The maximum and minimum exponents should not be hard-coded here.
(define-fpe-elicitor 'OVERFLOW 'MOST-POSITIVE-NUMBER-TIMES-TWO
(let ((flo:shift (make-primitive-procedure 'FLONUM-DENORMALIZE 2)))
(lambda ()
(flo:* (no-op 2.) (flo:shift (no-op 1.) 1023)))))
(define-fpe-descriptor 'UNDERFLOW #t flo:exception:underflow
condition-type:floating-point-underflow)
(define-fpe-elicitor 'UNDERFLOW 'RAISE
(lambda () (flo:raise-exceptions! (flo:exception:underflow))))
(define-fpe-elicitor 'UNDERFLOW 'LEAST-POSITIVE-NUMBER-OVER-TWO
(let ((flo:shift (make-primitive-procedure 'FLONUM-DENORMALIZE 2)))
(lambda ()
;; For some reason, on every architecture I tested (x87, amd64,
;; sparc), if underflow is not trapped then when the result is
;; exact it is not raised either. Multiplying by .50000001, as
;; suggested by Bill Kahan, forces the result to be inexact and
;; thereby forces the machine to raise the underflow exception.
;; (Note that if underflow is trapped, then the machine traps
;; whether or not the result is exact. Go figure.)
(flo:* (no-op .5000001) (flo:shift (no-op 1.) -1022)))))
(define (for-each-trappable-exception receiver)
(for-each-exception
(lambda (name exception condition-type trappable? elicitors)
(if (and trappable? (flo:have-trap-enable/disable?))
(receiver name exception condition-type elicitors)))))
(for-each-exception
(lambda (name exception condition-type trappable? elicitors)
condition-type trappable? elicitors ;ignore
(define-test (symbol 'FLO:EXCEPTIONS->NAMES ': name)
(lambda ()
(assert-equal (flo:exceptions->names (exception)) (list name))))
(define-test (symbol 'FLO:NAMES->EXCEPTIONS ': name)
(lambda ()
(assert-equal (flo:names->exceptions (list name)) (exception))))))
(define-test 'FLO:EXCEPTIONS->NAMES
(lambda ()
(let ((descriptors floating-point-exception-descriptors))
(assert-equal
(lset-difference
eq?
(flo:exceptions->names
(reduce fix:or 0 (map (lambda (f) (f)) (map cadr descriptors))))
(map car descriptors))
'()))))
(define-test 'FLO:NAMES->EXCEPTIONS
(lambda ()
(let ((descriptors floating-point-exception-descriptors))
(assert-eqv
(flo:names->exceptions (map car descriptors))
(reduce fix:or 0 (map (lambda (f) (f)) (map cadr descriptors)))))))
(define-test 'FLO:SUPPORTED-EXCEPTIONS
(lambda ()
(flo:supported-exceptions)))
(define-test 'FLO:SUPPORTED-EXCEPTION-NAMES
(lambda ()
(assert-eqv
(lset-difference eq?
(flo:exceptions->names (flo:supported-exceptions))
(map car floating-point-exception-descriptors))
'())))
(define-test 'FLO:TRAPPED-EXCEPTIONS
(lambda ()
(flo:trapped-exceptions)))
(define (define-set-trapped-exceptions-test name to-trap)
(if (flo:have-trap-enable/disable?)
(define-test (symbol 'FLO:SET-TRAPPED-EXCEPTIONS! ': name)
(lambda ()
(let ((exceptions (to-trap))
(trapped (flo:trapped-exceptions)))
(flo:preserving-environment
(lambda ()
(assert-eqv (flo:set-trapped-exceptions! exceptions) trapped)
(assert-eqv (flo:trapped-exceptions) exceptions))))))))
(define (define-with-trapped-exceptions-test name to-trap)
(if (flo:have-trap-enable/disable?)
(define-test (symbol 'FLO:WITH-TRAPPED-EXCEPTIONS ': name)
(lambda ()
(let ((exceptions (to-trap)))
(flo:with-trapped-exceptions exceptions
(lambda ()
(assert-eqv (flo:trapped-exceptions) exceptions))))))))
(define-set-trapped-exceptions-test 'ALL (lambda () 0))
(define-set-trapped-exceptions-test 'NONE flo:trappable-exceptions)
(define-with-trapped-exceptions-test 'ALL (lambda () 0))
(define-with-trapped-exceptions-test 'NONE flo:trappable-exceptions)
(for-each-trappable-exception
(lambda (name exception condition-type elicitors)
condition-type elicitors ;ignore
(define-test (symbol 'FLO:WITH-TRAPPED-EXCEPTIONS ': name)
(lambda ()
(flo:with-trapped-exceptions (exception)
(lambda ()
(assert-eqv (flo:trapped-exceptions) (exception))))))))
(for-each-trappable-exception
(lambda (name exception condition-type elicitors)
condition-type elicitors ;ignore
(define-test (symbol 'FLO:TRAP-EXCEPTIONS! ': name)
(lambda ()
(flo:with-trapped-exceptions 0
(lambda ()
(assert-eqv (flo:trap-exceptions! (exception)) 0)
(assert-eqv (flo:trapped-exceptions) (exception))))))))
(for-each-trappable-exception
(lambda (name exception condition-type elicitors)
condition-type elicitors ;ignore
(define-test (symbol 'FLO:UNTRAP-EXCEPTIONS! ': name)
(lambda ()
(flo:with-trapped-exceptions (flo:trappable-exceptions)
(lambda ()
(assert-eqv (flo:untrap-exceptions! (exception))
(flo:trappable-exceptions))
(assert-eqv (flo:trapped-exceptions)
(fix:andc (flo:trappable-exceptions) (exception)))))))))
(for-each-trappable-exception
(lambda (name exception condition-type elicitors)
condition-type elicitors ;ignore
(define-test (symbol 'FLO:SET-TRAPPED-EXCEPTIONS! ': name ': 'ENABLE)
(lambda ()
(flo:with-trapped-exceptions 0
(lambda ()
(assert-eqv (flo:set-trapped-exceptions! (exception)) 0)
(assert-eqv (flo:trapped-exceptions) (exception))))))))
(for-each-trappable-exception
(lambda (name exception condition-type elicitors)
condition-type elicitors ;ignore
(define-test (symbol 'FLO:SET-TRAPPED-EXCEPTIONS! ': name ': 'DISABLE)
(lambda ()
(let ((exceptions (fix:andc (flo:trappable-exceptions) (exception))))
(flo:with-trapped-exceptions (flo:trappable-exceptions)
(lambda ()
(assert-eqv (flo:set-trapped-exceptions! exceptions)
(flo:trappable-exceptions))
(assert-eqv (flo:trapped-exceptions) exceptions))))))))
(for-each-trappable-exception-elicitor
(lambda (name exception condition-type elicitor-name elicitor)
(define-test (symbol 'ELICIT ': name ': elicitor-name)
(lambda ()
(assert-error (lambda ()
(flo:with-trapped-exceptions (exception) elicitor))
(list condition-type))))))
(for-each-trappable-exception-elicitor
(lambda (name exception condition-type elicitor-name elicitor)
exception ;ignore
(define-test (symbol 'ELICIT-DEFERRED ': name ': elicitor-name)
(lambda ()
(assert-error
(lambda ()
(flo:with-trapped-exceptions (flo:trappable-exceptions)
(lambda ()
(flo:deferring-exception-traps
(lambda ()
(let ((flag #f))
(dynamic-wind (lambda () unspecific)
(lambda () (elicitor) (set! flag #t))
(lambda () (assert-true flag)))))))))
(list condition-type))))))
(for-each-exception-elicitor
(lambda (name exception condition-type trappable? elicitor-name elicitor)
exception condition-type trappable? ;ignore
(define-test (symbol 'ELICIT-IGNORED ': name ': elicitor-name)
(lambda ()
(flo:ignoring-exception-traps elicitor)))))
(for-each-exception-elicitor
(lambda (name exception condition-type trappable? elicitor-name elicitor)
condition-type trappable? ;ignore
(define-test (symbol 'ELICIT-AND-TEST ': name ': elicitor-name)
(lambda ()
(assert-eqv (flo:ignoring-exception-traps
(lambda ()
(elicitor)
(flo:test-exceptions (exception))))
(exception))))))
(for-each-exception-elicitor
(lambda (name exception condition-type trappable? elicitor-name elicitor)
condition-type trappable? ;ignore
(define-test (symbol 'ELICIT-CLEAR-TEST ': name ': elicitor-name)
(lambda ()
(assert-eqv (flo:ignoring-exception-traps
(lambda ()
(elicitor)
(flo:clear-exceptions! (exception))
(flo:test-exceptions (exception))))
0)))))
(define-test 'FLO:ENVIRONMENT
(lambda ()
(flo:environment)))
(define-test 'FLO:SET-ENVIRONMENT
(lambda ()
(flo:preserving-environment
(lambda ()
(flo:set-environment! (flo:environment))))))
(define-test 'FLO:DEFAULT-ENVIRONMENT
(lambda ()
(flo:default-environment)))
(define-test 'FLO:SET-DEFAULT-ENVIRONMENT
(lambda ()
(flo:preserving-environment
(lambda ()
(flo:set-environment! (flo:default-environment))))))
(define-test 'FLO:WITH-DEFAULT-ENVIRONMENT
(lambda ()
(flo:with-default-environment (lambda () 0))))
(define (define-default-environment-test name procedure)
(define-test (symbol 'FLO:DEFAULT-ENVIRONMENT ': name)
(lambda ()
(flo:preserving-environment
(lambda ()
;; Futz with the floating-point environment first,
;; guaranteeing that it is not the default one.
(flo:set-rounding-mode!
(if (eq? 'UPWARD (flo:default-rounding-mode))
'TO-NEAREST
'UPWARD))
(flo:set-trapped-exceptions!
(if (= (flo:trappable-exceptions) (flo:default-trapped-exceptions))
(fix:andc (flo:default-trapped-exceptions)
(flo:trappable-exceptions))
(flo:trappable-exceptions)))
(flo:with-default-environment procedure))))))
(define-default-environment-test 'ROUNDING-MODE
(lambda ()
(assert-eqv (flo:rounding-mode) (flo:default-rounding-mode))))
(if (flo:have-trap-enable/disable?)
(define-default-environment-test 'TRAPPED-EXCEPTIONS
(lambda ()
(assert-eqv (flo:trapped-exceptions)
(flo:default-trapped-exceptions)))))
|