;;;; slib-charplot-test.scm ;;;; Kon Lovett, Mar '20 (import test) (test-begin "Charplot (SLIB)") ;;; (import slib-charplot) (import (chicken base)) (import (chicken type)) (import (srfi 63)) ;; #; ;Pass - Argument #1 to procedure `plot' has an invalid type:... (plot "FAILS" "" "") ;NOTE must be proper list 4 `list->array', otherwise cons ok (define (make-point n) (let ((i (/ n 6))) `(,i ,(sin i)))) #; ;test dep (define (unfold/count f n) (import (srfi 1)) (unfold-right zero? f sub1 n) ) #; ;test dep (define (make-points n) (unfold/count make-point n)) (define (make-points n) (if (zero? n) '() (cons (make-point n) (make-points (sub1 n)))) ) (print) (print "*** Plot w/ dimensions 4x5+2 (minimum safe @ minimum left-margin)") (print) ;Fail - infinite loop forall H, W : H < 4, W < (5 + LM) (time (parameterize ((plot-left-margin #f) ;5 = (string-length "Sin(x)") - 1 ; max (map string-length (hlabel vlabel)) (plot-dimensions `(4 ,(+ 5 (plot-left-margin)))) ) (plot (list->array 2 '#() (make-points 40)) "x" "Sin(x)") ) ) (print) (time (let ((hlabel "x") (vlabel "Sin(x)")) (parameterize ((plot-left-margin (+ 2 (max (string-length hlabel) (string-length vlabel)))) (plot-dimensions `(24 ,(+ 54 (plot-left-margin)))) ) (plot (list->array 2 '#() (make-points 40)) hlabel vlabel) ) ) ) ;;;"Tests" (examples) from http://people.csail.mit.edu/jaffer/slib_5.html#SEC125 ;; (print) (print "*** Plot w/ dimensions 20x55") (print) ;ex 1 (time (parameterize ((plot-dimensions '(20 55))) (plot (make-points 40) "x" "Sin(x)") ) ) ;checkout the, undocumented, histogram? switch (print) (print "*** Histogram w/ dimensions 20x55 (NOTE undocumented by SLIB site)") (print) (time (parameterize ((plot-dimensions '(20 55))) (plot (make-points 40) "x" "Sin(x)" #t) ) ) ;default labels (print) (print "*** Plot w/ default labels") (print) (time (parameterize ((plot-left-margin #f) (plot-dimensions '(20 55)) ) (plot (make-points 40)) ) ) ;; ;mathh (define-constant PI 3.1415926535897932384626433832795028841972) ; pi (define pi PI) ; pi (print) (print "*** Plot w/ dimensions (#f)") (print) ;test framing ;ex 2 (time (parameterize ((plot-dimensions #f)) (plot sin 0 (* 2 pi)) ) ) (print) (print "*** Plot w/ default range") (print) (time (parameterize ((plot-left-margin #f) (plot-dimensions '(20 55)) ) (plot sin) ) ) ;; ;srfi-27 (define-type random-f64-function (-> float)) ;fortran "real" (define-type random-real-function random-f64-function) ;; Knuth's "The Art of Computer Programming", Vol. II, 2nd ed., ;; Algorithm P of Section 3.4.1.C. ; ;silly experiment (: *make-random-normals (number number random-real-function --> random-real-function)) ; (define (*make-random-normals mu sigma randoms) (let ((next #f)) (lambda () (if next (let ((result next)) (set! next #f) (+ mu (* sigma result))) (let loop () (let ((v1 (- (* 2 (inexact->exact (randoms))) 1)) (v2 (- (* 2 (inexact->exact (randoms))) 1)) ) (let ((s (+ (* v1 v1) (* v2 v2)))) (if (<= 1 s) (loop) (let ((scale (inexact->exact (sqrt (/ (* -2 (inexact->exact (log s))) s)))) ) (set! next (* scale v2)) (+ mu (* sigma scale v1)) ) ) ) ) ) ) ) ) ) (define random-normal (let () (import (only (chicken random) pseudo-random-real)) (*make-random-normals 0 1 pseudo-random-real))) (define (make-normals n bias) (do ((n n (sub1 n)) (ls '() (cons (* bias (random-normal)) ls))) ((<= n 0) ls)) ) (print) (print "*** Histograph (Normals) w/ dimensions 20x55 & left-margin 6") (print) ;test framing ;ex 3 (time (parameterize ((plot-left-margin 6) (plot-dimensions '(20 55)) ) (histograph (make-normals 99 .02) "Normals") ) ) (print) (print "*** Histograph data labeling") (print) (define (plotdata-length x) (cond ((list? x) (values (length x) 0)) ((vector? x) (values (vector-length x) 0)) (else (case (array-rank x) ((1) (values (car (array-dimensions x)) 0)) ((2) (apply values (array-dimensions x))) ) ) ) ) (define (plotdata-label x) (import (only (chicken format) format)) (define format-label (cut format #f "[#~A]" <>)) (let-values (((xlen ylen) (plotdata-length x))) (values (format-label xlen) (if (zero? ylen) "" (format-label ylen)))) ) (time (parameterize ((plot-labels plotdata-label) (plot-left-margin 6) (plot-dimensions '(20 55))) (histograph (make-normals 9 .02)) ) ) ;; (test-end "Charplot (SLIB)") (test-exit)