;;;; fp-utils-test -*- Scheme -*- ;;;; Kon Lovett, Sep '18 (import test) (test-begin "FP Utils") ;;; (import fp-inlines fp-utils) ;; (import (chicken base)) (import (chicken flonum)) (test-group "FP Inlines" (test-assert (fpzero? 0.0)) (test-assert (not (fpzero? 1.0))) (test-assert (not (fpzero? maximum-flonum))) (test-assert (not (fpzero? minimum-flonum))) (test-assert (not (fppositive? 0.0))) (test-assert (not (fppositive? (fpneg minimum-flonum)))) (test-assert (fppositive? maximum-flonum)) (test-assert (fpnatural? 0.0)) (test-assert (not (fpnatural? (fpneg minimum-flonum)))) (test-assert (fpnatural? maximum-flonum)) (test-assert (not (fpnegative? 0.0))) (test-assert (fpnegative? (fpneg minimum-flonum))) (test-assert (not (fpnegative? maximum-flonum))) (test-assert (not (fpeven? 7.0))) (test-assert (fpeven? 6.0)) (test-assert (not (fpodd? 6.0))) (test-assert (fpodd? 7.0)) (test 4.0 (fpadd1 3.0)) (test 2.0 (fpsub1 3.0)) (test 9.0 (fpsqr 3.0)) (test 27.0 (fpcub 3.0)) (test 0.477121254719662 (fplog10 3.0)) ) ;; (test-group "FP precision" (test 10.0 (fpprecision-factor 1)) (test 10000000000.0 (fpprecision-factor 10)) (test 15.8489319246111 (fpprecision-factor 1.2)) (test 0.1 (fpprecision-epsilon 1)) (test 0.0000000001 (fpprecision-epsilon 10)) (test 0.0630957344480193 (fpprecision-epsilon 1.2)) ) ;; (test-group "FP Approximate =" ;sweet, sweet repeating expansion (let ((x (fp+ 0.15 0.15)) (y (fp+ 0.1 0.2))) (test-assert ".15+.15 <> .1+.2" (not (fp= x y))) (test-assert ".15+.15 ~= .1+.2" (fp~= x y)) ) ;own epsilon (let ((eps5 (fpprecision-epsilon 5))) (test-assert (fp~= 0.123456 0.123457 eps5)) (test-assert (fp~<= 0.123456 0.123457 eps5)) (test-assert (fp~>= 0.123456 0.123457 eps5)) (test-assert (fp~<= 0.123456 0.12346 eps5)) (test-assert (fp~>= 0.123456 0.12344 eps5)) ) ) ;; (define-constant 4eps (fp/ 9.0 1e05)) (test-group "FP Utils" (test 1.0 (fpmodulo 5.0 2.0)) (test 0.0 (fpmodulo 0.0 1.0)) (test 2.0 (fpquotient 5.0 2.0)) (test 1.0 (fpremainder 5.0 2.0)) (test-assert (flonum? (fprandom))) (test-assert (flonum? (fprandom 2456))) (parameterize ((current-test-epsilon 4eps)) (test 5.6568 (fpdistance 1.0 1.0 5.0 5.0)) ) (receive (mx mn) (fpmax-and-min 1.0 -1.0 -16.0 13.0 2.0 16.0 7.0 -8.0) (test "fpmax-and-min max" 16.0 mx) (test "fpmax-and-min min" -16.0 mn) ) ) #; (begin (import (chicken flonum) fp-inlines fp-utils) (do ((i 0 (add1 i)) ) ((>= i 100000) ) (let ((n (fprandom 0.05 0.002))) (print n) (assert (<= 0.002 n)) (assert (<= n 0.05)) ) ) ) ;;; (test-end "FP Utils") (test-exit)