| 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 |
|
| 19 |
(define-module (test-suite test-random) |
| 20 |
#:use-module ((system base compile) #:select (compile)) |
| 21 |
#:use-module (test-suite lib) |
| 22 |
#:use-module (srfi srfi-4) |
| 23 |
#:use-module (srfi srfi-4 gnu) |
| 24 |
#:use-module ((ice-9 control) #:select (let/ec))) |
| 25 |
|
| 26 |
; see strings.test, arrays.test. |
| 27 |
(define exception:wrong-type-arg |
| 28 |
(cons #t "Wrong type")) |
| 29 |
|
| 30 |
;;; |
| 31 |
;;; random:normal-vector! |
| 32 |
;;; |
| 33 |
|
| 34 |
(with-test-prefix "random:normal-vector!" |
| 35 |
|
| 36 |
;; FIXME need proper function test. |
| 37 |
|
| 38 |
(pass-if "non uniform" |
| 39 |
(let ((a (make-vector 4 0)) |
| 40 |
(b (make-vector 4 0)) |
| 41 |
(c (make-shared-array (make-vector 8 0) |
| 42 |
(lambda (i) (list (+ 1 (* 2 i)))) 4))) |
| 43 |
(begin |
| 44 |
(random:normal-vector! b (random-state-from-platform)) |
| 45 |
(random:normal-vector! c (random-state-from-platform)) |
| 46 |
(and (not (equal? a b)) (not (equal? a c)))))) |
| 47 |
|
| 48 |
(pass-if "uniform (f64)" |
| 49 |
(let ((a (make-f64vector 4 0)) |
| 50 |
(b (make-f64vector 4 0)) |
| 51 |
(c (make-shared-array (make-f64vector 8 0) |
| 52 |
(lambda (i) (list (+ 1 (* 2 i)))) 4))) |
| 53 |
(begin |
| 54 |
(random:normal-vector! b (random-state-from-platform)) |
| 55 |
(random:normal-vector! c (random-state-from-platform)) |
| 56 |
(and (not (equal? a b)) (not (equal? a c)))))) |
| 57 |
|
| 58 |
(pass-if "empty argument" |
| 59 |
(random:normal-vector! (vector) (random-state-from-platform)) |
| 60 |
(random:normal-vector! (f64vector) (random-state-from-platform)) |
| 61 |
#t)) |
| 62 |
|
| 63 |
;;; |
| 64 |
;;; random:hollow-sphere! |
| 65 |
;;; |
| 66 |
|
| 67 |
(with-test-prefix "random:hollow-sphere!" |
| 68 |
|
| 69 |
(define (sqr a) |
| 70 |
(* a a)) |
| 71 |
(define (norm a) |
| 72 |
(sqrt (+ (sqr (array-ref a 0)) (sqr (array-ref a 1)) (sqr (array-ref a 2))))) |
| 73 |
(define double-eps 1e-15) |
| 74 |
|
| 75 |
(pass-if "non uniform" |
| 76 |
(let ((a (transpose-array (make-array 0. 3 10) 1 0))) |
| 77 |
(let/ec exit |
| 78 |
(array-slice-for-each 1 |
| 79 |
(lambda (a) |
| 80 |
(random:hollow-sphere! a) |
| 81 |
(if (> (magnitude (- 1 (norm a))) double-eps) (exit #f))) |
| 82 |
a) |
| 83 |
#t))) |
| 84 |
|
| 85 |
(pass-if "uniform (f64)" |
| 86 |
(let ((a (transpose-array (make-array 0. 3 10) 1 0))) |
| 87 |
(let/ec exit |
| 88 |
(array-slice-for-each 1 |
| 89 |
(lambda (a) |
| 90 |
(random:hollow-sphere! a) |
| 91 |
(if (> (magnitude (- 1 (norm a))) double-eps) (exit #f))) |
| 92 |
a) |
| 93 |
#t))) |
| 94 |
|
| 95 |
(pass-if "empty argument" |
| 96 |
(random:hollow-sphere! (vector)) |
| 97 |
(random:hollow-sphere! (f64vector)) |
| 98 |
#t)) |