ikarus/scheme/tests/string-to-number.ss

350 lines
12 KiB
Scheme
Raw Normal View History

;;; assume reader which loads this file can only read signed integers.
(library (tests string-to-number)
2008-10-18 13:03:17 -04:00
(export run-tests)
(import (ikarus) (tests framework))
2008-10-18 13:03:17 -04:00
(define (run-tests)
(test-string-to-number)
(generated-tests))
2008-10-18 13:03:17 -04:00
(define (test string expected)
(define (equal-results? x y)
(define (== x y)
(cond
[(nan? x) (nan? y)]
[(zero? x) (and (= x y) (= (atan 0.0 x) (atan 0.0 y)))]
[else
(and (or (and (exact? x) (exact? y))
(and (inexact? x) (inexact? y)))
(= x y))]))
(cond
[(and (number? x) (number? y))
(and (== (real-part x) (real-part y))
(== (imag-part x) (imag-part y)))]
[else (equal? x y)]))
(printf "testing ~a -> ~s\n" string expected)
(let ([result (string->number string)])
(if expected
(unless (number? result)
(error 'test "did not parse as number" string))
(when result
(error test "incorrectly parse as non-#f" string)))
(unless (equal-results? result expected)
(error 'test "failed/expected/got" string expected result))
(when expected
(let ([s1 (format "~s" result)])
(unless (string=? s1 string)
(test s1 expected))))))
(define inf+ (fl/ (inexact 1) (inexact 0)))
(define inf- (fl/ (inexact -1) (inexact 0)))
(define (test-string-to-number)
(test "10" 10)
(test "1" 1)
(test "-17" -17)
(test "12" 12)
(test "+12" +12)
(test "-12" -12)
(test "+13476238746782364786237846872346782364876238477" 13476238746782364786237846872346782364876238477)
(test "+inf.0" inf+)
(test "-inf.0" inf-)
(test "+i" (make-rectangular 0 +1))
(test "-i" (make-rectangular 0 -1))
(test "+15i" (make-rectangular 0 +15))
(test "-15i" (make-rectangular 0 -15))
(test "12/7" (/ 12 7))
(test "-12/7" (/ -12 7))
(test "+12/7" (/ 12 7))
(test "12/7i" (make-rectangular 0 (/ 12 7)))
(test "-12/7i" (make-rectangular 0 (/ -12 7)))
(test "+12/7i" (make-rectangular 0 (/ 12 7)))
(test "12/7+7i" (make-rectangular (/ 12 7) (/ 7 1)))
(test "12/7+7/5i" (make-rectangular (/ 12 7) (/ 7 5)))
(test "12/7-7/5i" (make-rectangular (/ 12 7) (/ -7 5)))
(test "12." (inexact 12))
(test "#e12." 12)
(test "12.5" (inexact (/ 125 10)))
(test "#e12.5123" (/ 125123 10000))
(test "#i125123/10000" (inexact (/ 125123 10000)))
(test "+inf.0i" (make-rectangular 0 inf+))
(test "-inf.0i" (make-rectangular 0 inf-))
(test "1/2" (/ 1 2))
(test "-1/2" (/ 1 -2))
(test "#x24" 36)
(test "#x-24" -36)
(test "#b+00000110110" 54)
(test "#b-00000110110/10" -27)
(test "#e10" 10)
(test "#e1" 1)
(test "#e-17" -17)
(test "#e#x24" 36)
(test "#e#x-24" -36)
(test "#e#b+00000110110" 54)
(test "#e#b-00000110110/10" -27)
(test "#x#e24" 36)
(test "#x#e-24" -36)
(test "#b#e+00000110110" 54)
(test "#b#e-00000110110/10" -27)
(test "#e1e1000" (expt 10 1000))
(test "#e-1e1000" (- (expt 10 1000)))
(test "#e1e-1000" (expt 10 -1000))
(test "#e-1e-1000" (- (expt 10 -1000)))
(test "#i1e100" (exact->inexact (expt 10 100)))
(test "#i1e1000" (exact->inexact (expt 10 1000)))
(test "#i-1e1000" (exact->inexact (- (expt 10 1000))))
(test "1e100" (exact->inexact (expt 10 100)))
(test "1.0e100" (exact->inexact (expt 10 100)))
(test "1.e100" (exact->inexact (expt 10 100)))
(test "0.1e100" (exact->inexact (expt 10 99)))
(test ".1e100" (exact->inexact (expt 10 99)))
(test "+1e100" (exact->inexact (expt 10 100)))
(test "+1.0e100" (exact->inexact (expt 10 100)))
(test "+1.e100" (exact->inexact (expt 10 100)))
(test "+0.1e100" (exact->inexact (expt 10 99)))
(test "+.1e100" (exact->inexact (expt 10 99)))
(test "-1e100" (exact->inexact (- (expt 10 100))))
(test "-1.0e100" (exact->inexact (- (expt 10 100))))
(test "-1.e100" (exact->inexact (- (expt 10 100))))
(test "-0.1e100" (exact->inexact (- (expt 10 99))))
(test "-.1e100" (exact->inexact (- (expt 10 99))))
(test "i" #f)
(test "/" #f)
(test "12/0" #f)
(test "+12/0" #f)
(test "-12/0" #f)
(test "12/0000" #f)
(test "+12/0000" #f)
(test "-12/0000" #f)
(test "12+" #f)
(test "+12+" #f)
(test "-12+" #f)
(test "12+" #f)
(test "+12+" #f)
(test "-12+" #f)
(test "8+6.0i" (make-rectangular 8 6.0))
(test "8.0+6i" (make-rectangular 8.0 6))
(test "+8+6.0i" (make-rectangular 8 6.0))
(test "+8.0+6i" (make-rectangular 8.0 6))
(test "-8+6.0i" (make-rectangular -8 6.0))
(test "-8.0+6i" (make-rectangular -8.0 6))
(test "8-6.0i" (make-rectangular 8 -6.0))
(test "8.0-6i" (make-rectangular 8.0 -6))
(test "+8-6.0i" (make-rectangular 8 -6.0))
(test "+8.0-6i" (make-rectangular 8.0 -6))
(test "-8-6.0i" (make-rectangular -8 -6.0))
(test "-8.0-6i" (make-rectangular -8.0 -6))
(test "0i" 0)
(test "+0i" 0)
(test "-0i" 0)
(test "1i" (make-rectangular 0 1))
(test "+1i" (make-rectangular 0 1))
(test "-1i" (make-rectangular 0 -1))
(test "8+nan.0i" (make-rectangular 8 +nan.0))
(test "8.0+nan.0i" (make-rectangular 8.0 +nan.0))
(test "+8+nan.0i" (make-rectangular 8 +nan.0))
(test "+8.0+nan.0i" (make-rectangular 8.0 +nan.0))
(test "-8+nan.0i" (make-rectangular -8 +nan.0))
(test "-8.0+nan.0i" (make-rectangular -8.0 +nan.0))
(test "8-nan.0i" (make-rectangular 8 -nan.0))
(test "8.0-nan.0i" (make-rectangular 8.0 -nan.0))
(test "+8-nan.0i" (make-rectangular 8 -nan.0))
(test "+8.0-nan.0i" (make-rectangular 8.0 -nan.0))
(test "-8-nan.0i" (make-rectangular -8 -nan.0))
(test "-8.0-nan.0i" (make-rectangular -8.0 -nan.0))
(test "+nan.0+6.0i" (make-rectangular +nan.0 6.0))
(test "+nan.0+6i" (make-rectangular +nan.0 6))
(test "+nan.0+6.0i" (make-rectangular +nan.0 6.0))
(test "+nan.0+6i" (make-rectangular +nan.0 6))
(test "-nan.0+6.0i" (make-rectangular -nan.0 6.0))
(test "-nan.0+6i" (make-rectangular -nan.0 6))
(test "+nan.0-6.0i" (make-rectangular +nan.0 -6.0))
(test "+nan.0-6i" (make-rectangular +nan.0 -6))
(test "+nan.0-6.0i" (make-rectangular +nan.0 -6.0))
(test "+nan.0-6i" (make-rectangular +nan.0 -6))
(test "-nan.0-6.0i" (make-rectangular -nan.0 -6.0))
(test "-nan.0-6i" (make-rectangular -nan.0 -6))
(test "+nan.0+nan.0i" (make-rectangular +nan.0 +nan.0))
(test "+nan.0-nan.0i" (make-rectangular +nan.0 -nan.0))
(test "-nan.0+nan.0i" (make-rectangular -nan.0 +nan.0))
(test "-nan.0-nan.0i" (make-rectangular -nan.0 -nan.0))
(test "+nan.0+i" (make-rectangular +nan.0 +1))
(test "+nan.0-i" (make-rectangular +nan.0 -1))
(test "-nan.0+i" (make-rectangular -nan.0 +1))
(test "-nan.0-i" (make-rectangular -nan.0 -1))
(test "8+inf.0i" (make-rectangular 8 +inf.0))
(test "8.0+inf.0i" (make-rectangular 8.0 +inf.0))
(test "+8+inf.0i" (make-rectangular 8 +inf.0))
(test "+8.0+inf.0i" (make-rectangular 8.0 +inf.0))
(test "-8+inf.0i" (make-rectangular -8 +inf.0))
(test "-8.0+inf.0i" (make-rectangular -8.0 +inf.0))
(test "8-inf.0i" (make-rectangular 8 -inf.0))
(test "8.0-inf.0i" (make-rectangular 8.0 -inf.0))
(test "+8-inf.0i" (make-rectangular 8 -inf.0))
(test "+8.0-inf.0i" (make-rectangular 8.0 -inf.0))
(test "-8-inf.0i" (make-rectangular -8 -inf.0))
(test "-8.0-inf.0i" (make-rectangular -8.0 -inf.0))
(test "+inf.0+6.0i" (make-rectangular +inf.0 6.0))
(test "+inf.0+6i" (make-rectangular +inf.0 6))
(test "+inf.0+6.0i" (make-rectangular +inf.0 6.0))
(test "+inf.0+6i" (make-rectangular +inf.0 6))
(test "-inf.0+6.0i" (make-rectangular -inf.0 6.0))
(test "-inf.0+6i" (make-rectangular -inf.0 6))
(test "+inf.0-6.0i" (make-rectangular +inf.0 -6.0))
(test "+inf.0-6i" (make-rectangular +inf.0 -6))
(test "+inf.0-6.0i" (make-rectangular +inf.0 -6.0))
(test "+inf.0-6i" (make-rectangular +inf.0 -6))
(test "-inf.0-6.0i" (make-rectangular -inf.0 -6.0))
(test "-inf.0-6i" (make-rectangular -inf.0 -6))
(test "+inf.0+inf.0i" (make-rectangular +inf.0 +inf.0))
(test "+inf.0-inf.0i" (make-rectangular +inf.0 -inf.0))
(test "-inf.0+inf.0i" (make-rectangular -inf.0 +inf.0))
(test "-inf.0-inf.0i" (make-rectangular -inf.0 -inf.0))
(test "+inf.0+i" (make-rectangular +inf.0 +1))
(test "+inf.0-i" (make-rectangular +inf.0 -1))
(test "-inf.0+i" (make-rectangular -inf.0 +1))
(test "-inf.0-i" (make-rectangular -inf.0 -1))
(test "8+6e20i" (make-rectangular +8 +6e20))
(test "8-6e20i" (make-rectangular +8 -6e20))
(test "8e20+6i" (make-rectangular +8e20 +6))
(test "8e20-6i" (make-rectangular +8e20 -6))
(test "+8+6e20i" (make-rectangular +8 +6e20))
(test "+8-6e20i" (make-rectangular +8 -6e20))
(test "+8e20+6i" (make-rectangular +8e20 +6))
(test "+8e20-6i" (make-rectangular +8e20 -6))
(test "-8+6e20i" (make-rectangular -8 +6e20))
(test "-8-6e20i" (make-rectangular -8 -6e20))
(test "-8e20+6i" (make-rectangular -8e20 +6))
(test "-8e20-6i" (make-rectangular -8e20 -6))
(test "8e10+6e20i" (make-rectangular +8e10 +6e20))
(test "8e10-6e20i" (make-rectangular +8e10 -6e20))
(test "+8e10+6e20i" (make-rectangular +8e10 +6e20))
(test "+8e10-6e20i" (make-rectangular +8e10 -6e20))
(test "-8e10+6e20i" (make-rectangular -8e10 +6e20))
(test "-8e10-6e20i" (make-rectangular -8e10 -6e20))
(test "-0e-10" -0.0)
(test "-0e-0" -0.0)
(test "#d-0e-10-0e-0i" (make-rectangular -0.0 -0.0))
(test "-0.i" (make-rectangular 0.0 -0.0))
2008-07-16 02:13:59 -04:00
(test "#d#e-0.0f-0-.0s-0i" 0)
(test "+.234e4i" (make-rectangular 0 0.234e4))
(test "+.234e-5i" (make-rectangular 0 0.234e-5))
(test "+.234i" (make-rectangular 0 0.234))
)
(define (generated-tests)
(define (gen ls1 ls2 comp1 comp2)
(apply append
(map (lambda (x1)
(map (lambda (x2)
(cons (comp1 (car x1) (car x2))
(comp2 (cdr x1) (cdr x2))))
ls2))
ls1)))
(define (gensa ls1 ls2 comp)
(gen ls1 ls2 string-append comp))
(define ureal
'(["0" . 0]
["1" . 1]
["1." . 1.0]
["1.0" . 1.0]
[".5" . 0.5]
["0.5" . 0.5]
2009-08-02 05:23:07 -04:00
;["1e1" . 10.0]
["1e0" . 1.0]
;["1e+1" . 10.0]
["1e+0" . 1.0]
["1e-1" . 0.1]
2009-08-02 05:23:07 -04:00
;["1.e1" . 10.0]
["1.e0" . 1.0]
;["1.e+1" . 10.0]
["1.e+0" . 1.0]
["1.e-1" . 0.1]
2009-08-02 05:23:07 -04:00
;["1.0e1" . 10.0]
["1.0e0" . 1.0]
;["1.0e+1" . 10.0]
["1.0e+0" . 1.0]
["1.0e-1" . 0.1]
2009-08-02 05:23:07 -04:00
;[".5e1" . 5.0]
[".5e0" . 0.5]
;[".5e+1" . 5.0]
[".5e+0" . 0.5]
[".5e-1" . 0.05]
))
(define naninf
'(["nan.0" . +nan.0]
["inf.0" . +inf.0]))
(define sign
'(["+" . +1]
["-" . -1]))
;;; <real> = <sign> <ureal>
;;; | + <naninf>
;;; | - <naninf>
(define sreal
(append
(gensa sign ureal *)
(gensa sign naninf *)))
(define real
(append ureal sreal))
;;;<complex> = <real>
;;; | <real> @ <real>
;;; | <real> <creal>
;;; | <creal>
;;; <creal> = <seal> i
;;; | +i
;;; | -i
(define creal
(append
(gensa sreal '(["i" . #f]) (lambda (x f) (make-rectangular 0 x)))
`(["+i" . ,(make-rectangular 0 1)]
["-i" . ,(make-rectangular 0 -1)])))
(define complex
(append
real creal
(gensa real creal +)
2009-08-02 05:23:07 -04:00
(gen real real (lambda (x y) (string-append x "@" y)) make-polar)
))
(printf "TESTING ~s tests\n" (length complex))
(for-each
(lambda (x)
(test (car x) (cdr x)))
complex)
)
)