test.scm 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. ; Functions for testers.
  2. ; (c) Daniel Llorens - 2012-2013
  3. ; This library is free software; you can redistribute it and/or modify it under
  4. ; the terms of the GNU General Public License as published by the Free
  5. ; Software Foundation; either version 3 of the License, or (at your option) any
  6. ; later version.
  7. (define-module (ploy test))
  8. (import (ploy assert) (srfi srfi-1))
  9. (re-export assert assert-fail)
  10. (define (relative-error a b)
  11. (cond
  12. ((= a 0) (magnitude b))
  13. ((= b 0) (magnitude a))
  14. (else (/ (magnitude (- a b)) (+ (magnitude a) (magnitude b)) 1/2))))
  15. (define (absolute-error a b)
  16. (magnitude (- a b)))
  17. (define (T . args)
  18. (assert (cond ((every array? args) (apply array-equal? args))
  19. ((every number? args) (apply = args))
  20. (else (apply equal? args)))
  21. "T failed"))
  22. (define (T-msg msg . args)
  23. (assert (cond ((every array? args) (apply array-equal? args))
  24. ((every number? args) (apply = args))
  25. (else (apply equal? args)))
  26. msg))
  27. (export relative-error absolute-error T T-msg)
  28. (define* (compare-arrays a b #:key (relative-to 1))
  29. (and (equal? (array-dimensions a) (array-dimensions b))
  30. (let* ((aerr (let ((err 0))
  31. (array-for-each
  32. (lambda (a b) (set! err (max err (absolute-error a b))))
  33. a b)
  34. err)))
  35. (if (= relative-to 1.)
  36. aerr
  37. (values (/ aerr relative-to) aerr)))))
  38. (define (T-eps eps . args)
  39. (let ((e (cond ((every array? args)
  40. (fold (lambda (a c) (max c (compare-arrays (car args) a)))
  41. 0. (cdr args)))
  42. ((every number? args)
  43. (fold (lambda (a c) (max c (absolute-error (car args) a)))
  44. 0 (cdr args)))
  45. (else (error "bad arguments")))))
  46. (assert (>= eps e) "failed T-eps with eps, error" e eps)
  47. e))
  48. (define (T-eps-msg msg eps . args)
  49. (catch #t (lambda () (apply T-eps eps args))
  50. (lambda x (apply throw 'precision-error msg x))))
  51. (export compare-arrays T-eps T-eps-msg)
  52. (define-syntax repeat
  53. (syntax-rules ()
  54. ((_ n e0 ...) (do ((i 0 (+ i 1))) ((= i n)) e0 ...))))
  55. (export repeat)