random.test 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. ;;;; random.test --- tests guile's uniform arrays -*- scheme -*-
  2. ;;;;
  3. ;;;; Copyright 2013 Free Software Foundation, Inc.
  4. ;;;;
  5. ;;;; This library is free software; you can redistribute it and/or
  6. ;;;; modify it under the terms of the GNU Lesser General Public
  7. ;;;; License as published by the Free Software Foundation; either
  8. ;;;; version 3 of the License, or (at your option) any later version.
  9. ;;;;
  10. ;;;; This library is distributed in the hope that it will be useful,
  11. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;;;; Lesser General Public License for more details.
  14. ;;;;
  15. ;;;; You should have received a copy of the GNU Lesser General Public
  16. ;;;; License along with this library; if not, write to the Free Software
  17. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. (define-module (test-suite test-random)
  19. #:use-module ((system base compile) #:select (compile))
  20. #:use-module (test-suite lib)
  21. #:use-module (srfi srfi-4)
  22. #:use-module (srfi srfi-4 gnu)
  23. #:use-module ((ice-9 control) #:select (let/ec)))
  24. ; see strings.test, arrays.test.
  25. (define exception:wrong-type-arg
  26. (cons #t "Wrong type"))
  27. ;;;
  28. ;;; random:normal-vector!
  29. ;;;
  30. (with-test-prefix "random:normal-vector!"
  31. ;; FIXME need proper function test.
  32. (pass-if "non uniform"
  33. (let ((a (make-vector 4 0))
  34. (b (make-vector 4 0))
  35. (c (make-shared-array (make-vector 8 0)
  36. (lambda (i) (list (+ 1 (* 2 i)))) 4)))
  37. (begin
  38. (random:normal-vector! b (random-state-from-platform))
  39. (random:normal-vector! c (random-state-from-platform))
  40. (and (not (equal? a b)) (not (equal? a c))))))
  41. (pass-if "uniform (f64)"
  42. (let ((a (make-f64vector 4 0))
  43. (b (make-f64vector 4 0))
  44. (c (make-shared-array (make-f64vector 8 0)
  45. (lambda (i) (list (+ 1 (* 2 i)))) 4)))
  46. (begin
  47. (random:normal-vector! b (random-state-from-platform))
  48. (random:normal-vector! c (random-state-from-platform))
  49. (and (not (equal? a b)) (not (equal? a c))))))
  50. (pass-if "empty argument"
  51. (random:normal-vector! (vector) (random-state-from-platform))
  52. (random:normal-vector! (f64vector) (random-state-from-platform))
  53. #t))
  54. ;;;
  55. ;;; random:hollow-sphere!
  56. ;;;
  57. (with-test-prefix "random:hollow-sphere!"
  58. (define (sqr a)
  59. (* a a))
  60. (define (norm a)
  61. (sqrt (+ (sqr (array-ref a 0)) (sqr (array-ref a 1)) (sqr (array-ref a 2)))))
  62. (define double-eps 1e-15)
  63. (pass-if "non uniform"
  64. (let ((a (transpose-array (make-array 0. 3 10) 1 0)))
  65. (let/ec exit
  66. (array-slice-for-each 1
  67. (lambda (a)
  68. (random:hollow-sphere! a)
  69. (if (> (magnitude (- 1 (norm a))) double-eps) (exit #f)))
  70. a)
  71. #t)))
  72. (pass-if "uniform (f64)"
  73. (let ((a (transpose-array (make-array 0. 3 10) 1 0)))
  74. (let/ec exit
  75. (array-slice-for-each 1
  76. (lambda (a)
  77. (random:hollow-sphere! a)
  78. (if (> (magnitude (- 1 (norm a))) double-eps) (exit #f)))
  79. a)
  80. #t)))
  81. (pass-if "empty argument"
  82. (random:hollow-sphere! (vector))
  83. (random:hollow-sphere! (f64vector))
  84. #t))