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

121 lines
3.8 KiB
Scheme
Raw Normal View History

;;; assume reader which loads this file can only read signed integers.
(library (tests string-to-number)
(export test-string-to-number)
(import (ikarus) (tests framework))
(define (test string expected)
(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? 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)
)
)