| 1 |
|
| 2 |
(library (tests parse-flonums) |
| 3 |
(export run-tests) |
| 4 |
(import (ikarus) (tests framework)) |
| 5 |
|
| 6 |
(define file (src-file "tests/rn100")) |
| 7 |
|
| 8 |
(define (read-all) |
| 9 |
(with-input-from-file file |
| 10 |
(lambda () |
| 11 |
(let f ([ac '()]) |
| 12 |
(let ([x (read)]) |
| 13 |
(if (eof-object? x) |
| 14 |
(reverse ac) |
| 15 |
(f (cons x ac)))))))) |
| 16 |
|
| 17 |
(define (read-flonum) |
| 18 |
(define (decimal x) |
| 19 |
(cond |
| 20 |
[(assv x '([#\0 . 0] [#\1 . 1] [#\2 . 2] [#\3 . 3] [#\4 . 4] |
| 21 |
[#\5 . 5] [#\6 . 6] [#\7 . 7] [#\8 . 8] [#\9 . 9])) |
| 22 |
=> cdr] |
| 23 |
[else #f])) |
| 24 |
(define (st) |
| 25 |
(let ([x (read-char)]) |
| 26 |
(cond |
| 27 |
[(eof-object? x) x] |
| 28 |
[(char-whitespace? x) (st)] |
| 29 |
[(char=? x #\-) (- (sign))] |
| 30 |
[(decimal x) => num] |
| 31 |
[else (error 'st "invalid char" x)]))) |
| 32 |
(define (sign) |
| 33 |
(let ([x (read-char)]) |
| 34 |
(cond |
| 35 |
[(eof-object? x) (error 'sign "eof")] |
| 36 |
[(decimal x) => num] |
| 37 |
[else (error 'sign "invalid char" x)]))) |
| 38 |
(define (num n) |
| 39 |
(let ([x (read-char)]) |
| 40 |
(cond |
| 41 |
[(eof-object? x) (error 'num "eof")] |
| 42 |
[(decimal x) => (lambda (m) (num (+ (* n 10) m)))] |
| 43 |
[(char=? x #\.) (+ n (frac 0 1))] |
| 44 |
[else (error 'num "invalid char" x)]))) |
| 45 |
(define (frac num den) |
| 46 |
(let ([x (read-char)]) |
| 47 |
(cond |
| 48 |
[(or (eof-object? x) (char-whitespace? x)) |
| 49 |
(/ num den)] |
| 50 |
[(decimal x) => (lambda (m) |
| 51 |
(frac (+ (* num 10) m) |
| 52 |
(* den 10)))] |
| 53 |
[else (error 'frac "invalid char" x)]))) |
| 54 |
(st)) |
| 55 |
|
| 56 |
(define smallest-flonum |
| 57 |
(bytevector-ieee-double-ref |
| 58 |
#vu8(1 0 0 0 0 0 0 0) |
| 59 |
0 |
| 60 |
'little)) |
| 61 |
|
| 62 |
(define (gen-epsilon x) |
| 63 |
(let ([x (flabs x)]) |
| 64 |
(let f ([eps smallest-flonum]) |
| 65 |
(if (fl=? x (fl- x eps)) |
| 66 |
(f (fl* eps 2.0)) |
| 67 |
eps)))) |
| 68 |
|
| 69 |
(define (inexact-close-enough? in ex) |
| 70 |
;;; take the inexact number, and generate two |
| 71 |
;;; additional numbers: in+epsilon, in-epsilon |
| 72 |
;;; turn them into exacts: e1=exact(in+epsilon), e2=exact(in-epsilon) |
| 73 |
;;; ensure that at least e1 < ex < e2 |
| 74 |
(let ([eps (gen-epsilon in)]) |
| 75 |
(< (exact (fl- in eps)) ex (exact (fl+ in eps))))) |
| 76 |
|
| 77 |
(define (read-exact-all) |
| 78 |
(with-input-from-file file |
| 79 |
(lambda () |
| 80 |
(let f ([ac '()]) |
| 81 |
(let ([x (read-flonum)]) |
| 82 |
(if (eof-object? x) |
| 83 |
(reverse ac) |
| 84 |
(f (cons x ac)))))))) |
| 85 |
|
| 86 |
(define (run-tests) |
| 87 |
(define who 'test-parse-flonums) |
| 88 |
(define failed #f) |
| 89 |
(define idx 0) |
| 90 |
(let ([ls1 (read-all)] |
| 91 |
[ls2 (read-exact-all)]) |
| 92 |
(assert (= (length ls1) (length ls2))) |
| 93 |
(for-each |
| 94 |
(lambda (x1 x2) |
| 95 |
(set! idx (+ idx 1)) |
| 96 |
(unless (inexact-close-enough? x1 x2) |
| 97 |
(set! failed #t) |
| 98 |
(printf "test failed in line ~s on read=~s and parsed=~s\n" |
| 99 |
idx x1 x2))) |
| 100 |
ls1 ls2)) |
| 101 |
(when failed (error who "failed")) |
| 102 |
)) |
| 103 |
|
| 104 |
|