test.scm 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. ;;;; `test.scm' Test correctness of scheme implementations.
  2. ;;; ;; Copyright (C) 1991, 1992, 1993, 1994, 1995, 2000, 2003 Free Software Foundation, Inc.
  3. ;;;; "r4rstest.scm" Test correctness of scheme implementations.
  4. ;;; Author: Aubrey Jaffer
  5. ;;; Modified for Kawa testing framework by Per Bothner 1996-2003.
  6. ;;; This includes examples from
  7. ;;; William Clinger and Jonathan Rees, editors.
  8. ;;; Revised^4 Report on the Algorithmic Language Scheme
  9. ;;; and the IEEE specification.
  10. ;;; The input tests read this file expecting it to be named "test.scm".
  11. ;;; Files `tmp1', `tmp2' and `tmp3' will be created in the course of running
  12. ;;; these tests. You may need to delete them in order to run
  13. ;;; "test.scm" more than once.
  14. ;;; There are three optional tests:
  15. ;;; (TEST-CONT) tests multiple returns from call-with-current-continuation
  16. ;;;
  17. ;;; (TEST-SC4) tests procedures required by R4RS but not by IEEE
  18. ;;;
  19. ;;; (TEST-DELAY) tests DELAY and FORCE, which are not required by
  20. ;;; either standard.
  21. ;;; If you are testing a R3RS version which does not have `list?' do:
  22. ;;; (define list? #f)
  23. ;;; send corrections or additions to jaffer@ai.mit.edu or
  24. ;;; Aubrey Jaffer, 84 Pleasant St., Wakefield MA 01880, USA
  25. (TEST-INIT "scm-test" 617)(define errs '())
  26. ;(define test
  27. ; (lambda (expect fun . args)
  28. ; (write (cons fun args))
  29. ; (display " ==> ")
  30. ; ((lambda (res)
  31. ; (write res)
  32. ; (newline)
  33. ; (cond ((not (equal? expect res))
  34. ; (record-error (list res expect (cons fun args)))
  35. ; (display " BUT EXPECTED ")
  36. ; (write expect)
  37. ; (newline)
  38. ; #f)
  39. ; (else #t)))
  40. ; (if (procedure? fun) (apply fun args) (car args)))))
  41. (define (report-errs) #t)
  42. ;(define (report-errs)
  43. ; (newline)
  44. ; (if (null? errs) (display "Passed all tests")
  45. ; (begin
  46. ; (display "errors were:")
  47. ; (newline)
  48. ; (display "(SECTION (got expected (call)))")
  49. ; (newline)
  50. ; (for-each (lambda (l) (write l) (newline))
  51. ; errs)))
  52. ; (newline))
  53. (define *out-port* (or *log-file* (current-output-port)))
  54. (SECTION 2 1);; test that all symbol characters are supported.
  55. '(+ - ... !.. $.+ %.- &.! *.: /:. :+. <-. =. >. ?. ~. _. ^.)
  56. (SECTION 3 4)
  57. (define disjoint-type-functions
  58. (list boolean? char? null? number? pair? procedure? string? symbol? vector?))
  59. (define type-examples
  60. (list
  61. #t #f #\a '() 9739 '(test) record-error "test" "" 'test '#() '#(a b c) ))
  62. (define i 1)
  63. (for-each (lambda (x) (display (make-string i #\ ) *out-port*)
  64. (set! i (+ 3 i))
  65. (write x *out-port*)
  66. (newline *out-port*))
  67. disjoint-type-functions)
  68. (define type-matrix
  69. (map (lambda (x)
  70. (let ((t (map (lambda (f) (f x)) disjoint-type-functions)))
  71. (write t *out-port*)
  72. (write x *out-port*)
  73. (newline *out-port*)
  74. t))
  75. type-examples))
  76. #|
  77. (set! i 0)
  78. (define j 0)
  79. (for-each (lambda (x y)
  80. (set! j (+ 1 j))
  81. (set! i 0)
  82. (for-each (lambda (f)
  83. (set! i (+ 1 i))
  84. (cond ((and (= i j))
  85. (cond ((not (f x)) (test #t f x))))
  86. ((f x) (test #f f x)))
  87. (cond ((and (= i j))
  88. (cond ((not (f y)) (test #t f y))))
  89. ((f y) (test #f f y))))
  90. disjoint-type-functions))
  91. (list #t #\a '() 9739 '(test) record-error "test" 'car '#(a b c))
  92. (list #f #\newline '() -3252 '(t . t) car "" 'nil '#()))
  93. |#
  94. (SECTION "4 1 2")
  95. (test '(quote a) 'quote (quote 'a))
  96. (test '(quote a) 'quote ''a)
  97. (SECTION "4 1 3")
  98. (test 12 (if #f + *) 3 4)
  99. (SECTION 4 1 4)
  100. (test 8 (lambda (x) (+ x x)) 4)
  101. (define reverse-subtract
  102. (lambda (x y) (- y x)))
  103. (test 3 reverse-subtract 7 10)
  104. (define add4
  105. (let ((x 4))
  106. (lambda (y) (+ x y))))
  107. (test 10 add4 6)
  108. (test '(3 4 5 6) (lambda x x) 3 4 5 6)
  109. (test '(5 6) (lambda (x y . z) z) 3 4 5 6)
  110. (SECTION 4 1 5)
  111. (test 'yes 'if (if (> 3 2) 'yes 'no))
  112. (test 'no 'if (if (> 2 3) 'yes 'no))
  113. (test '1 'if (if (> 3 2) (- 3 2) (+ 3 2)))
  114. (SECTION 4 1 6)
  115. (define x 2)
  116. (test 3 'define (+ x 1))
  117. (set! x 4)
  118. (test 5 'set! (+ x 1))
  119. (SECTION 4 2 1)
  120. ;; Moved to bad-voidexp.scm
  121. ;;(test 'greater 'cond (cond ((> 3 2) 'greater)
  122. ;; ((< 3 2) 'less)))
  123. (test 'equal 'cond (cond ((> 3 3) 'greater)
  124. ((< 3 3) 'less)
  125. (else 'equal)))
  126. (test 2 'cond (cond ((assv 'b '((a 1) (b 2))) => cadr)
  127. (else #f)))
  128. (test #t 'cond (cond (#t) (3 4)))
  129. ;; Moved to bad-voidexp.scm
  130. ;;(test 'composite 'case (case (* 2 3)
  131. ;; ((2 3 5 7) 'prime)
  132. ;; ((1 4 6 8 9) 'composite)))
  133. (test 'consonant 'case (case (car '(c d))
  134. ((a e i o u) 'vowel)
  135. ((w y) 'semivowel)
  136. (else 'consonant)))
  137. (test #t 'and (and (= 2 2) (> 2 1)))
  138. (test #f 'and (and (= 2 2) (< 2 1)))
  139. (test '(f g) 'and (and 1 2 'c '(f g)))
  140. (test #t 'and (and))
  141. (test #t 'or (or (= 2 2) (> 2 1)))
  142. (test #t 'or (or (= 2 2) (< 2 1)))
  143. (test #f 'or (or #f #f #f))
  144. (test #f 'or (or))
  145. (test '(b c) 'or (or (memq 'b '(a b c)) (+ 3 0)))
  146. (SECTION 4 2 2)
  147. (test 6 'let (let ((x 2) (y 3)) (* x y)))
  148. (test 35 'let (let ((x 2) (y 3)) (let ((x 7) (z (+ x y))) (* z x))))
  149. (test 70 'let* (let ((x 2) (y 3)) (let* ((x 7) (z (+ x y))) (* z x))))
  150. (test #t 'letrec (letrec ((even?
  151. (lambda (n) (if (zero? n) #t (odd? (- n 1)))))
  152. (odd?
  153. (lambda (n) (if (zero? n) #f (even? (- n 1))))))
  154. (even? 88)))
  155. (define x 34)
  156. (test 5 'let (let ((x 3)) (define x 5) x))
  157. (test 34 'let x)
  158. (test 6 'let (let () (define x 6) x))
  159. (test 34 'let x)
  160. (test 7 'let* (let* ((x 3)) (define x 7) x))
  161. (test 34 'let* x)
  162. (test 8 'let* (let* () (define x 8) x))
  163. (test 34 'let* x)
  164. (test 9 'letrec (letrec () (define x 9) x))
  165. (test 34 'letrec x)
  166. (test 10 'letrec (letrec ((x 3)) (define x 10) x))
  167. (test 34 'letrec x)
  168. (SECTION 4 2 3)
  169. (define x 0)
  170. (test 6 'begin (begin (set! x (begin (begin 5)))
  171. (begin ((begin +) (begin x) (begin (begin 1))))))
  172. (SECTION 4 2 4)
  173. (test '#(0 1 2 3 4) 'do (do ((vec (make-vector 5))
  174. (i 0 (+ i 1)))
  175. ((= i 5) vec)
  176. (vector-set! vec i i)))
  177. (test 25 'do (let ((x '(1 3 5 7 9)))
  178. (do ((x x (cdr x))
  179. (sum 0 (+ sum (car x))))
  180. ((null? x) sum))))
  181. (test 1 'let (let foo () 1))
  182. (test '((6 1 3) (-5 -2)) 'let
  183. (let loop ((numbers '(3 -2 1 6 -5))
  184. (nonneg '())
  185. (neg '()))
  186. (cond ((null? numbers) (list nonneg neg))
  187. ((negative? (car numbers))
  188. (loop (cdr numbers)
  189. nonneg
  190. (cons (car numbers) neg)))
  191. (else
  192. (loop (cdr numbers)
  193. (cons (car numbers) nonneg)
  194. neg)))))
  195. (SECTION 4 2 6)
  196. (test '(list 3 4) 'quasiquote `(list ,(+ 1 2) 4))
  197. (test '(list a (quote a)) 'quasiquote (let ((name 'a)) `(list ,name ',name)))
  198. (test '(a 3 4 5 6 b) 'quasiquote `(a ,(+ 1 2) ,@(map abs '(4 -5 6)) b))
  199. (test '((foo 7) . cons)
  200. 'quasiquote
  201. `((foo ,(- 10 3)) ,@(cdr '(c)) . ,(car '(cons))))
  202. ;;; sqt is defined here because not all implementations are required to
  203. ;;; support it.
  204. (define (sqt x)
  205. (do ((i 0 (+ i 1)))
  206. ((> (* i i) x) (- i 1))))
  207. (test '#(10 5 2 4 3 8) 'quasiquote `#(10 5 ,(sqt 4) ,@(map sqt '(16 9)) 8))
  208. (test 5 'quasiquote `,(+ 2 3))
  209. (test '(a `(b ,(+ 1 2) ,(foo 4 d) e) f)
  210. 'quasiquote `(a `(b ,(+ 1 2) ,(foo ,(+ 1 3) d) e) f))
  211. (test '(a `(b ,x ,'y d) e) 'quasiquote
  212. (let ((name1 'x) (name2 'y)) `(a `(b ,,name1 ,',name2 d) e)))
  213. (test '(list 3 4) 'quasiquote (quasiquote (list (unquote (+ 1 2)) 4)))
  214. (test '`(list ,(+ 1 2) 4) 'quasiquote '(quasiquote (list (unquote (+ 1 2)) 4)))
  215. (SECTION 5 2 1)
  216. ;; some tests moved to define3.scm because they only work if --no-inline
  217. (begin)
  218. (begin (begin))
  219. (begin (begin (begin (begin))))
  220. (SECTION 5 2 2)
  221. (test 45 'define
  222. (let ((x 5))
  223. (define foo (lambda (y) (bar x y)))
  224. (define bar (lambda (a b) (+ (* a b) a)))
  225. (foo (+ x 3))))
  226. (define x 34)
  227. (define (foo) (define x 5) x)
  228. (test 5 foo)
  229. (test 34 'define x)
  230. (define foo (lambda () (define x 5) x))
  231. (test 5 foo)
  232. (test 34 'define x)
  233. (define (foo x) ((lambda () (define x 5) x)) x)
  234. (test 88 foo 88)
  235. (test 4 foo 4)
  236. (test 34 'define x)
  237. (test 99 'internal-define (letrec ((foo (lambda (arg)
  238. (or arg (and (procedure? foo)
  239. (foo 99))))))
  240. (define bar (foo #f))
  241. (foo #f)))
  242. (test 77 'internal-define (letrec ((foo 77)
  243. (bar #f)
  244. (retfoo (lambda () foo)))
  245. (define baz (retfoo))
  246. (retfoo)))
  247. (SECTION 6 1)
  248. (test #f not #t)
  249. (test #f not 3)
  250. (test #f not (list 3))
  251. (test #t not #f)
  252. (test #f not '())
  253. (test #f not (list))
  254. (test #f not 'nil)
  255. (test #t boolean? #f)
  256. (test #f boolean? 0)
  257. (test #f boolean? '())
  258. (SECTION 6 2)
  259. (test #t eqv? 'a 'a)
  260. (test #f eqv? 'a 'b)
  261. (test #t eqv? 2 2)
  262. (test #t eqv? '() '())
  263. (test #t eqv? '10000 '10000)
  264. (test #f eqv? (cons 1 2)(cons 1 2))
  265. (test #f eqv? (lambda () 1) (lambda () 2))
  266. (test #f eqv? #f 'nil)
  267. (let ((p (lambda (x) x)))
  268. (test #t eqv? p p))
  269. (define gen-counter
  270. (lambda ()
  271. (let ((n 0))
  272. (lambda () (set! n (+ n 1)) n))))
  273. (let ((g (gen-counter))) (test #t eqv? g g))
  274. (test #f eqv? (gen-counter) (gen-counter))
  275. (letrec ((f (lambda () (if (eqv? f g) 'f 'both)))
  276. (g (lambda () (if (eqv? f g) 'g 'both))))
  277. (test #f eqv? f g))
  278. (test #t eq? 'a 'a)
  279. (test #f eq? (list 'a) (list 'a))
  280. (test #t eq? '() '())
  281. (test #t eq? car car)
  282. (let ((x '(a))) (test #t eq? x x))
  283. (let ((x '#())) (test #t eq? x x))
  284. (let ((x (lambda (x) x))) (test #t eq? x x))
  285. (define (eq?-eqv?-agreement obj1 obj2)
  286. (eq? (eq? obj1 obj2) (eqv? obj1 obj2)))
  287. (define-syntax test-eq?-eqv?-agreement
  288. (syntax-rules ()
  289. ((_ obj1 obj2)
  290. (test #t eq?-eqv?-agreement obj1 obj2))))
  291. (test-eq?-eqv?-agreement '#f '#f)
  292. (test-eq?-eqv?-agreement '#t '#t)
  293. (test-eq?-eqv?-agreement '#t '#f)
  294. (test-eq?-eqv?-agreement '(a) '(a))
  295. (test-eq?-eqv?-agreement '(a) '(b))
  296. (test-eq?-eqv?-agreement car car)
  297. (test-eq?-eqv?-agreement car cdr)
  298. (test-eq?-eqv?-agreement (list 'a) (list 'a))
  299. (test-eq?-eqv?-agreement (list 'a) (list 'b))
  300. (test-eq?-eqv?-agreement '#(a) '#(a))
  301. (test-eq?-eqv?-agreement '#(a) '#(b))
  302. (test-eq?-eqv?-agreement "abc" "abc")
  303. (test-eq?-eqv?-agreement "abc" "abz")
  304. (test #t equal? 'a 'a)
  305. (test #t equal? '(a) '(a))
  306. (test #t equal? '(a (b) c) '(a (b) c))
  307. (test #t equal? "abc" "abc")
  308. (test #t equal? 2 2)
  309. (test #t equal? (make-vector 5 'a) (make-vector 5 'a))
  310. (SECTION 6 3)
  311. (test '(a b c d e) 'dot '(a . (b . (c . (d . (e . ()))))))
  312. (define x (list 'a 'b 'c))
  313. (define y x)
  314. (and list? (test #t list? y))
  315. (set-cdr! x 4)
  316. (test '(a . 4) 'set-cdr! x)
  317. (test #t eqv? x y)
  318. (test '(a b c . d) 'dot '(a . (b . (c . d))))
  319. (and list? (test #f list? y))
  320. (and list? (let ((x (list 'a))) (set-cdr! x x) (test #f 'list? (list? x))))
  321. (test #t pair? '(a . b))
  322. (test #t pair? '(a . 1))
  323. (test #t pair? '(a b c))
  324. (test #f pair? '())
  325. (test #f pair? '#(a b))
  326. (test '(a) cons 'a '())
  327. (test '((a) b c d) cons '(a) '(b c d))
  328. (test '("a" b c) cons "a" '(b c))
  329. (test '(a . 3) cons 'a 3)
  330. (test '((a b) . c) cons '(a b) 'c)
  331. (test 'a car '(a b c))
  332. (test '(a) car '((a) b c d))
  333. (test 1 car '(1 . 2))
  334. (test '(b c d) cdr '((a) b c d))
  335. (test 2 cdr '(1 . 2))
  336. (test '(a 7 c) list 'a (+ 3 4) 'c)
  337. (test '() list)
  338. (test 3 length '(a b c))
  339. (test 3 length '(a (b) (c d e)))
  340. (test 0 length '())
  341. (test '(x y) append '(x) '(y))
  342. (test '(a b c d) append '(a) '(b c d))
  343. (test '(a (b) (c)) append '(a (b)) '((c)))
  344. (test '() append)
  345. (test '(a b c . d) append '(a b) '(c . d))
  346. (test 'a append '() 'a)
  347. (test '(c b a) reverse '(a b c))
  348. (test '((e (f)) d (b c) a) reverse '(a (b c) d (e (f))))
  349. (test 'c list-ref '(a b c d) 2)
  350. (test '(a b c) memq 'a '(a b c))
  351. (test '(b c) memq 'b '(a b c))
  352. (test '#f memq 'a '(b c d))
  353. (test '#f memq (list 'a) '(b (a) c))
  354. (test '((a) c) member (list 'a) '(b (a) c))
  355. (test '(101 102) memv 101 '(100 101 102))
  356. (define e '((a 1) (b 2) (c 3)))
  357. (test '(a 1) assq 'a e)
  358. (test '(b 2) assq 'b e)
  359. (test #f assq 'd e)
  360. (test #f assq (list 'a) '(((a)) ((b)) ((c))))
  361. (test '((a)) assoc (list 'a) '(((a)) ((b)) ((c))))
  362. (test '(5 7) assv 5 '((2 3) (5 7) (11 13)))
  363. (SECTION 6 4)
  364. (test #t symbol? 'foo)
  365. (test #t symbol? (car '(a b)))
  366. (test #f symbol? "bar")
  367. (test #t symbol? 'nil)
  368. (test #f symbol? '())
  369. (test #f symbol? #f)
  370. ;;; But first, what case are symbols in? Determine the standard case:
  371. (define char-standard-case char-upcase)
  372. (if (string=? (symbol->string 'A) "a")
  373. (set! char-standard-case char-downcase))
  374. (test #t 'standard-case
  375. (string=? (symbol->string 'a) (symbol->string 'A)))
  376. (test #t 'standard-case
  377. (or (string=? (symbol->string 'a) "A")
  378. (string=? (symbol->string 'A) "a")))
  379. (define (str-copy s)
  380. (let ((v (make-string (string-length s))))
  381. (do ((i (- (string-length v) 1) (- i 1)))
  382. ((< i 0) v)
  383. (string-set! v i (string-ref s i)))))
  384. (define (string-standard-case s)
  385. (set! s (str-copy s))
  386. (do ((i 0 (+ 1 i))
  387. (sl (string-length s)))
  388. ((>= i sl) s)
  389. (string-set! s i (char-standard-case (string-ref s i)))))
  390. (test (string-standard-case "flying-fish") symbol->string 'flying-fish)
  391. (test (string-standard-case "martin") symbol->string 'Martin)
  392. (test "Malvina" symbol->string (string->symbol "Malvina"))
  393. (test #t 'standard-case (eq? 'a 'A))
  394. (define x (string #\a #\b))
  395. (define y (string->symbol x))
  396. (string-set! x 0 #\c)
  397. (test "cb" 'string-set! x)
  398. (test "ab" symbol->string y)
  399. (test y string->symbol "ab")
  400. (test #t eq? 'mISSISSIppi 'mississippi)
  401. (test #f 'string->symbol (eq? 'bitBlt (string->symbol "bitBlt")))
  402. (test 'JollyWog string->symbol (symbol->string 'JollyWog))
  403. (SECTION 6 5 5)
  404. (test #t number? 3)
  405. (test #t complex? 3)
  406. (test #t real? 3)
  407. (test #t rational? 3)
  408. (test #t integer? 3)
  409. (test #t exact? 3)
  410. (test #f inexact? 3)
  411. (test #t = 22 22 22)
  412. (test #t = 22 22)
  413. (test #f = 34 34 35)
  414. (test #f = 34 35)
  415. (test #t > 3 -6246)
  416. (test #f > 9 9 -2424)
  417. (test #t >= 3 -4 -6246)
  418. (test #t >= 9 9)
  419. (test #f >= 8 9)
  420. (test #t < -1 2 3 4 5 6 7 8)
  421. (test #f < -1 2 3 4 4 5 6 7)
  422. (test #t <= -1 2 3 4 5 6 7 8)
  423. (test #t <= -1 2 3 4 4 5 6 7)
  424. (test #f < 1 3 2)
  425. (test #f >= 1 3 2)
  426. (test #t zero? 0)
  427. (test #f zero? 1)
  428. (test #f zero? -1)
  429. (test #f zero? -100)
  430. (test #t positive? 4)
  431. (test #f positive? -4)
  432. (test #f positive? 0)
  433. (test #f negative? 4)
  434. (test #t negative? -4)
  435. (test #f negative? 0)
  436. (test #t odd? 3)
  437. (test #f odd? 2)
  438. (test #f odd? -4)
  439. (test #t odd? -1)
  440. (test #f even? 3)
  441. (test #t even? 2)
  442. (test #t even? -4)
  443. (test #f even? -1)
  444. (test 38 max 34 5 7 38 6)
  445. (test -24 min 3 5 5 330 4 -24)
  446. (test 7 + 3 4)
  447. (test '3 + 3)
  448. (test 0 +)
  449. (test 4 * 4)
  450. (test 1 *)
  451. (test -1 - 3 4)
  452. (test -3 - 3)
  453. (test 7 abs -7)
  454. (test 7 abs 7)
  455. (test 0 abs 0)
  456. (test 5 quotient 35 7)
  457. (test -5 quotient -35 7)
  458. (test -5 quotient 35 -7)
  459. (test 5 quotient -35 -7)
  460. (test 1 modulo 13 4)
  461. (test 1 remainder 13 4)
  462. (test 3 modulo -13 4)
  463. (test -1 remainder -13 4)
  464. (test -3 modulo 13 -4)
  465. (test 1 remainder 13 -4)
  466. (test -1 modulo -13 -4)
  467. (test -1 remainder -13 -4)
  468. (test 0 modulo 0 86400)
  469. (test 0 modulo 0 -86400)
  470. (define (divtest n1 n2)
  471. (= n1 (+ (* n2 (quotient n1 n2))
  472. (remainder n1 n2))))
  473. (test #t divtest 238 9)
  474. (test #t divtest -238 9)
  475. (test #t divtest 238 -9)
  476. (test #t divtest -238 -9)
  477. (test 4 gcd 0 4)
  478. (test 4 gcd -4 0)
  479. (test 4 gcd 32 -36)
  480. (test 0 gcd)
  481. (test 288 lcm 32 -36)
  482. (test 1 lcm)
  483. (SECTION 6 5 5)
  484. ;;; Implementations which don't allow division by 0 can have fragile
  485. ;;; string->number.
  486. (define (test-string->number str)
  487. (define ans (string->number str))
  488. (cond ((not ans) #t) ((number? ans) #t) (else ans)))
  489. (for-each (lambda (str) (test #t test-string->number str))
  490. '("+#.#" "-#.#" "#.#" "1/0" "-1/0" "0/0"
  491. "+1/0i" "-1/0i" "0/0i" "0/0-0/0i" "1/0-1/0i" "-1/0+1/0i"))
  492. ;;;;From: fred@sce.carleton.ca (Fred J Kaudel)
  493. ;;; Modified by jaffer.
  494. (define (test-inexact)
  495. (define f3.9 (string->number "3.9"))
  496. (define f4.0 (string->number "4.0"))
  497. (define f-3.25 (string->number "-3.25"))
  498. (define f.25 (string->number ".25"))
  499. (define f4.5 (string->number "4.5"))
  500. (define f3.5 (string->number "3.5"))
  501. (define f0.0 (string->number "0.0"))
  502. (define f0.8 (string->number "0.8"))
  503. (define f1.0 (string->number "1.0"))
  504. (define wto write-test-obj)
  505. (define dto display-test-obj)
  506. (define lto load-test-obj)
  507. (SECTION 6 5 5 "(inexact numbers)")
  508. (test #t inexact? f3.9)
  509. (test #t 'inexact? (inexact? (max f3.9 4)))
  510. (test f4.0 'max (max f3.9 4))
  511. (test f4.0 'exact->inexact (exact->inexact 4))
  512. (test (- f4.0) round (- f4.5))
  513. (test (- f4.0) round (- f3.5))
  514. (test (- f4.0) round (- f3.9))
  515. (test f0.0 round f0.0)
  516. (test f0.0 round f.25)
  517. (test f1.0 round f0.8)
  518. (test f4.0 round f3.5)
  519. (test f4.0 round f4.5)
  520. (test 1 expt 0 0)
  521. (test 0 expt 0 1)
  522. (test (atan 1) atan 1 1)
  523. (set! write-test-obj (list f.25 f-3.25));.25 inexact errors less likely.
  524. (set! display-test-obj (list f.25 f-3.25));.3 often has such errors (~10^-13)
  525. (set! load-test-obj (list 'define 'foo (list 'quote write-test-obj)))
  526. (test #t call-with-output-file
  527. "tmp3"
  528. (lambda (test-file)
  529. (write-char #\; test-file)
  530. (display write-test-obj test-file)
  531. (newline test-file)
  532. (write load-test-obj test-file)
  533. (output-port? test-file)))
  534. (check-test-file "tmp3")
  535. (set! write-test-obj wto)
  536. (set! display-test-obj dto)
  537. (set! load-test-obj lto)
  538. (let ((x (string->number "4195835.0"))
  539. (y (string->number "3145727.0")))
  540. (test #t 'pentium-fdiv-bug (> f1.0 (- x (* (/ x y) y)))))
  541. (report-errs))
  542. (define (test-inexact-printing)
  543. (let ((f0.0 (string->number "0.0"))
  544. (f0.5 (string->number "0.5"))
  545. (f1.0 (string->number "1.0"))
  546. (f2.0 (string->number "2.0")))
  547. (define log2
  548. (let ((l2 (log 2)))
  549. (lambda (x) (/ (log x) l2))))
  550. (define (slow-frexp x)
  551. (if (zero? x)
  552. (list f0.0 0)
  553. (let* ((l2 (log2 x))
  554. (e (floor (log2 x)))
  555. (e (if (= l2 e)
  556. (inexact->exact e)
  557. (+ (inexact->exact e) 1)))
  558. (f (/ x (expt 2 e))))
  559. (list f e))))
  560. (define float-precision
  561. (let ((mantissa-bits
  562. (do ((i 0 (+ i 1))
  563. (eps f1.0 (* f0.5 eps)))
  564. ((= f1.0 (+ f1.0 eps))
  565. i)))
  566. (minval
  567. (do ((x f1.0 (* f0.5 x)))
  568. ((zero? (* f0.5 x)) x))))
  569. (lambda (x)
  570. (apply (lambda (f e)
  571. (let ((eps
  572. (cond ((= f1.0 f) (expt f2.0 (+ 1 (- e mantissa-bits))))
  573. ((zero? f) minval)
  574. (else (expt f2.0 (- e mantissa-bits))))))
  575. (if (zero? eps) ;Happens if gradual underflow.
  576. minval
  577. eps)))
  578. (slow-frexp x)))))
  579. (define (float-print-test x)
  580. (define (testit number)
  581. (eqv? number (string->number (number->string number))))
  582. (let ((eps (float-precision x))
  583. (all-ok? #t))
  584. (do ((j -100 (+ j 1)))
  585. ((or (not all-ok?) (> j 100)) all-ok?)
  586. (let* ((xx (+ x (* j eps)))
  587. (ok? (testit xx)))
  588. (cond ((not ok?)
  589. (display "Number readback failure for ")
  590. (display `(+ ,x (* ,j ,eps)))
  591. (newline)
  592. (display xx)
  593. (newline)
  594. (set! all-ok? #f))
  595. ;; (else (display xx) (newline))
  596. )))))
  597. (define (mult-float-print-test x)
  598. (let ((res #t))
  599. (for-each
  600. (lambda (mult)
  601. (or (float-print-test (* mult x)) (set! res #f)))
  602. (map string->number
  603. '("1.0" "10.0" "100.0" "1.0e20" "1.0e50" "1.0e100"
  604. "0.1" "0.01" "0.001" "1.0e-20" "1.0e-50" "1.0e-100")))
  605. res))
  606. (SECTION 6 5 6)
  607. (test #t 'float-print-test (float-print-test f0.0))
  608. (test #t 'mult-float-print-test (mult-float-print-test f1.0))
  609. (test #t 'mult-float-print-test (mult-float-print-test
  610. (string->number "3.0")))
  611. (test #t 'mult-float-print-test (mult-float-print-test
  612. (string->number "7.0")))
  613. (test #t 'mult-float-print-test (mult-float-print-test
  614. (string->number "3.1415926535897931")))
  615. (test #t 'mult-float-print-test (mult-float-print-test
  616. (string->number "2.7182818284590451")))))
  617. (define (test-bignum)
  618. (define tb
  619. (lambda (n1 n2)
  620. (= n1 (+ (* n2 (quotient n1 n2))
  621. (remainder n1 n2)))))
  622. (SECTION "6 5 5 (bignums)")
  623. (test 0 modulo 33333333333333333333 3)
  624. (test 0 modulo 33333333333333333333 -3)
  625. (test 0 remainder 33333333333333333333 3)
  626. (test 0 remainder 33333333333333333333 -3)
  627. (test 2 modulo 33333333333333333332 3)
  628. (test -1 modulo 33333333333333333332 -3)
  629. (test 2 remainder 33333333333333333332 3)
  630. (test 2 remainder 33333333333333333332 -3)
  631. (test 1 modulo -33333333333333333332 3)
  632. (test -2 modulo -33333333333333333332 -3)
  633. (test -2 remainder -33333333333333333332 3)
  634. (test -2 remainder -33333333333333333332 -3)
  635. (test 3 modulo 3 33333333333333333333)
  636. (test 33333333333333333330 modulo -3 33333333333333333333)
  637. (test 3 remainder 3 33333333333333333333)
  638. (test -3 remainder -3 33333333333333333333)
  639. (test -33333333333333333330 modulo 3 -33333333333333333333)
  640. (test -3 modulo -3 -33333333333333333333)
  641. (test 3 remainder 3 -33333333333333333333)
  642. (test -3 remainder -3 -33333333333333333333)
  643. (test 0 modulo -2177452800 86400)
  644. (test 0 modulo 2177452800 -86400)
  645. (test 0 modulo 2177452800 86400)
  646. (test 0 modulo -2177452800 -86400)
  647. (test 0 modulo -2177452800 86400)
  648. (test 0 modulo 2177452800 -86400)
  649. (test 0 modulo 2177452800 86400)
  650. (test 0 modulo -2177452800 -86400)
  651. (test 0 modulo 0 -2177452800)
  652. (test #t 'remainder (tb 281474976710655 65535))
  653. (test #t 'remainder (tb 281474976710654 65535))
  654. (test #t 'remainder (tb 281474976710655325431 65535))
  655. (test #t 'remainder (tb 281474976710655325430 65535))
  656. (SECTION 6 5 8)
  657. (test 281474976710655 string->number "281474976710655")
  658. (test "281474976710655" number->string 281474976710655)
  659. (test 281474976710655325431 string->number "281474976710655325431")
  660. (test "281474976710655325431" number->string 281474976710655325431)
  661. (report-errs))
  662. (define (test-numeric-predicates)
  663. (let* ((big-ex (expt 2 90))
  664. (big-inex (exact->inexact big-ex)))
  665. (SECTION 6 5 5 "(bignum-inexact comparisons)")
  666. (test #f = (+ big-ex 1) big-inex (- big-ex 1))
  667. (test #f = big-inex (+ big-ex 1) (- big-ex 1))
  668. (test #t < (- (inexact->exact big-inex) 1)
  669. big-inex
  670. (+ (inexact->exact big-inex) 1))))
  671. (SECTION 6 5 9)
  672. (test "0" number->string 0)
  673. (test "100" number->string 100)
  674. (test "100" number->string 256 16)
  675. (test 100 string->number "100")
  676. (test 256 string->number "100" 16)
  677. (test #f string->number "")
  678. (test #f string->number ".")
  679. (test #f string->number "d")
  680. (test #f string->number "D")
  681. (test #f string->number "i")
  682. (test #f string->number "I")
  683. ;; The next 6 are not valid according to R5RS.
  684. (test 3i string->number "3i")
  685. (test 3i string->number "3I")
  686. (test 33i string->number "33i")
  687. (test 33i string->number "33I")
  688. (test 3.3i string->number "3.3i")
  689. (test 3.3i string->number "3.3I")
  690. (test #f string->number "-")
  691. (test #f string->number "+")
  692. (test #t 'string->number (or (not (string->number "80000000" 16))
  693. (positive? (string->number "80000000" 16))))
  694. (test #t 'string->number (or (not (string->number "-80000000" 16))
  695. (negative? (string->number "-80000000" 16))))
  696. (SECTION 6 6)
  697. (test #t eqv? '#\ #\Space)
  698. (test #t eqv? #\space '#\Space)
  699. (test #t char? #\a)
  700. (test #t char? #\()
  701. (test #t char? #\ )
  702. (test #t char? '#\newline)
  703. (test #f char=? #\A #\B)
  704. (test #f char=? #\a #\b)
  705. (test #f char=? #\9 #\0)
  706. (test #t char=? #\A #\A)
  707. (test #t char<? #\A #\B)
  708. (test #t char<? #\a #\b)
  709. (test #f char<? #\9 #\0)
  710. (test #f char<? #\A #\A)
  711. (test #f char>? #\A #\B)
  712. (test #f char>? #\a #\b)
  713. (test #t char>? #\9 #\0)
  714. (test #f char>? #\A #\A)
  715. (test #t char<=? #\A #\B)
  716. (test #t char<=? #\a #\b)
  717. (test #f char<=? #\9 #\0)
  718. (test #t char<=? #\A #\A)
  719. (test #f char>=? #\A #\B)
  720. (test #f char>=? #\a #\b)
  721. (test #t char>=? #\9 #\0)
  722. (test #t char>=? #\A #\A)
  723. (test #f char-ci=? #\A #\B)
  724. (test #f char-ci=? #\a #\B)
  725. (test #f char-ci=? #\A #\b)
  726. (test #f char-ci=? #\a #\b)
  727. (test #f char-ci=? #\9 #\0)
  728. (test #t char-ci=? #\A #\A)
  729. (test #t char-ci=? #\A #\a)
  730. (test #t char-ci<? #\A #\B)
  731. (test #t char-ci<? #\a #\B)
  732. (test #t char-ci<? #\A #\b)
  733. (test #t char-ci<? #\a #\b)
  734. (test #f char-ci<? #\9 #\0)
  735. (test #f char-ci<? #\A #\A)
  736. (test #f char-ci<? #\A #\a)
  737. (test #f char-ci>? #\A #\B)
  738. (test #f char-ci>? #\a #\B)
  739. (test #f char-ci>? #\A #\b)
  740. (test #f char-ci>? #\a #\b)
  741. (test #t char-ci>? #\9 #\0)
  742. (test #f char-ci>? #\A #\A)
  743. (test #f char-ci>? #\A #\a)
  744. (test #t char-ci<=? #\A #\B)
  745. (test #t char-ci<=? #\a #\B)
  746. (test #t char-ci<=? #\A #\b)
  747. (test #t char-ci<=? #\a #\b)
  748. (test #f char-ci<=? #\9 #\0)
  749. (test #t char-ci<=? #\A #\A)
  750. (test #t char-ci<=? #\A #\a)
  751. (test #f char-ci>=? #\A #\B)
  752. (test #f char-ci>=? #\a #\B)
  753. (test #f char-ci>=? #\A #\b)
  754. (test #f char-ci>=? #\a #\b)
  755. (test #t char-ci>=? #\9 #\0)
  756. (test #t char-ci>=? #\A #\A)
  757. (test #t char-ci>=? #\A #\a)
  758. (test #t char-alphabetic? #\a)
  759. (test #t char-alphabetic? #\A)
  760. (test #t char-alphabetic? #\z)
  761. (test #t char-alphabetic? #\Z)
  762. (test #f char-alphabetic? #\0)
  763. (test #f char-alphabetic? #\9)
  764. (test #f char-alphabetic? #\space)
  765. (test #f char-alphabetic? #\;)
  766. (test #f char-numeric? #\a)
  767. (test #f char-numeric? #\A)
  768. (test #f char-numeric? #\z)
  769. (test #f char-numeric? #\Z)
  770. (test #t char-numeric? #\0)
  771. (test #t char-numeric? #\9)
  772. (test #f char-numeric? #\space)
  773. (test #f char-numeric? #\;)
  774. (test #f char-whitespace? #\a)
  775. (test #f char-whitespace? #\A)
  776. (test #f char-whitespace? #\z)
  777. (test #f char-whitespace? #\Z)
  778. (test #f char-whitespace? #\0)
  779. (test #f char-whitespace? #\9)
  780. (test #t char-whitespace? #\space)
  781. (test #f char-whitespace? #\;)
  782. (test #f char-upper-case? #\0)
  783. (test #f char-upper-case? #\9)
  784. (test #f char-upper-case? #\space)
  785. (test #f char-upper-case? #\;)
  786. (test #f char-lower-case? #\0)
  787. (test #f char-lower-case? #\9)
  788. (test #f char-lower-case? #\space)
  789. (test #f char-lower-case? #\;)
  790. (test #\. integer->char (char->integer #\.))
  791. (test #\A integer->char (char->integer #\A))
  792. (test #\a integer->char (char->integer #\a))
  793. (test #\A char-upcase #\A)
  794. (test #\A char-upcase #\a)
  795. (test #\a char-downcase #\A)
  796. (test #\a char-downcase #\a)
  797. (SECTION 6 7)
  798. (test #t string? "The word \"recursion\\\" has many meanings.")
  799. (test #t string? "")
  800. (define f (make-string 3 #\*))
  801. (test "?**" 'string-set! (begin (string-set! f 0 #\?) f))
  802. (test "abc" string #\a #\b #\c)
  803. (test "" string)
  804. (test 3 string-length "abc")
  805. (test #\a string-ref "abc" 0)
  806. (test #\c string-ref "abc" 2)
  807. (test 0 string-length "")
  808. (test "" substring "ab" 0 0)
  809. (test "" substring "ab" 1 1)
  810. (test "" substring "ab" 2 2)
  811. (test "a" substring "ab" 0 1)
  812. (test "b" substring "ab" 1 2)
  813. (test "ab" substring "ab" 0 2)
  814. (test "foobar" string-append "foo" "bar")
  815. (test "foo" string-append "foo")
  816. (test "foo" string-append "foo" "")
  817. (test "foo" string-append "" "foo")
  818. (test "" string-append)
  819. (test "" make-string 0)
  820. (test #t string=? "" "")
  821. (test #f string<? "" "")
  822. (test #f string>? "" "")
  823. (test #t string<=? "" "")
  824. (test #t string>=? "" "")
  825. (test #t string-ci=? "" "")
  826. (test #f string-ci<? "" "")
  827. (test #f string-ci>? "" "")
  828. (test #t string-ci<=? "" "")
  829. (test #t string-ci>=? "" "")
  830. (test #f string=? "A" "B")
  831. (test #f string=? "a" "b")
  832. (test #f string=? "9" "0")
  833. (test #t string=? "A" "A")
  834. (test #t string<? "A" "B")
  835. (test #t string<? "a" "b")
  836. (test #f string<? "9" "0")
  837. (test #f string<? "A" "A")
  838. (test #f string>? "A" "B")
  839. (test #f string>? "a" "b")
  840. (test #t string>? "9" "0")
  841. (test #f string>? "A" "A")
  842. (test #t string<=? "A" "B")
  843. (test #t string<=? "a" "b")
  844. (test #f string<=? "9" "0")
  845. (test #t string<=? "A" "A")
  846. (test #f string>=? "A" "B")
  847. (test #f string>=? "a" "b")
  848. (test #t string>=? "9" "0")
  849. (test #t string>=? "A" "A")
  850. (test #f string-ci=? "A" "B")
  851. (test #f string-ci=? "a" "B")
  852. (test #f string-ci=? "A" "b")
  853. (test #f string-ci=? "a" "b")
  854. (test #f string-ci=? "9" "0")
  855. (test #t string-ci=? "A" "A")
  856. (test #t string-ci=? "A" "a")
  857. (test #t string-ci<? "A" "B")
  858. (test #t string-ci<? "a" "B")
  859. (test #t string-ci<? "A" "b")
  860. (test #t string-ci<? "a" "b")
  861. (test #f string-ci<? "9" "0")
  862. (test #f string-ci<? "A" "A")
  863. (test #f string-ci<? "A" "a")
  864. (test #f string-ci>? "A" "B")
  865. (test #f string-ci>? "a" "B")
  866. (test #f string-ci>? "A" "b")
  867. (test #f string-ci>? "a" "b")
  868. (test #t string-ci>? "9" "0")
  869. (test #f string-ci>? "A" "A")
  870. (test #f string-ci>? "A" "a")
  871. (test #t string-ci<=? "A" "B")
  872. (test #t string-ci<=? "a" "B")
  873. (test #t string-ci<=? "A" "b")
  874. (test #t string-ci<=? "a" "b")
  875. (test #f string-ci<=? "9" "0")
  876. (test #t string-ci<=? "A" "A")
  877. (test #t string-ci<=? "A" "a")
  878. (test #f string-ci>=? "A" "B")
  879. (test #f string-ci>=? "a" "B")
  880. (test #f string-ci>=? "A" "b")
  881. (test #f string-ci>=? "a" "b")
  882. (test #t string-ci>=? "9" "0")
  883. (test #t string-ci>=? "A" "A")
  884. (test #t string-ci>=? "A" "a")
  885. (SECTION 6 8)
  886. (test #t vector? '#(0 (2 2 2 2) "Anna"))
  887. (test #t vector? '#())
  888. (test '#(a b c) vector 'a 'b 'c)
  889. (test '#() vector)
  890. (test 3 vector-length '#(0 (2 2 2 2) "Anna"))
  891. (test 0 vector-length '#())
  892. (test 8 vector-ref '#(1 1 2 3 5 8 13 21) 5)
  893. (test '#(0 ("Sue" "Sue") "Anna") 'vector-set
  894. (let ((vec (vector 0 '(2 2 2 2) "Anna")))
  895. (vector-set! vec 1 '("Sue" "Sue"))
  896. vec))
  897. (test '#(hi hi) make-vector 2 'hi)
  898. (test '#() make-vector 0)
  899. (test '#() make-vector 0 'a)
  900. (SECTION 6 9)
  901. (test #t procedure? car)
  902. (test #f procedure? 'car)
  903. (test #t procedure? (lambda (x) (* x x)))
  904. (test #f procedure? '(lambda (x) (* x x)))
  905. (test #t call-with-current-continuation procedure?)
  906. (test 7 apply + (list 3 4))
  907. (test 7 apply (lambda (a b) (+ a b)) (list 3 4))
  908. (test 17 apply + 10 (list 3 4))
  909. (test '() apply list '())
  910. (define compose (lambda (f g) (lambda args (f (apply g args)))))
  911. (test 30 (compose sqt *) 12 75)
  912. (test '(b e h) map cadr '((a b) (d e) (g h)))
  913. (test '(5 7 9) map + '(1 2 3) '(4 5 6))
  914. (test '(1 2 3) map + '(1 2 3))
  915. (test '(1 2 3) map * '(1 2 3))
  916. (test '(-1 -2 -3) map - '(1 2 3))
  917. (test '#(0 1 4 9 16) 'for-each
  918. (let ((v (make-vector 5)))
  919. (for-each (lambda (i) (vector-set! v i (* i i)))
  920. '(0 1 2 3 4))
  921. v))
  922. (test -3 call-with-current-continuation
  923. (lambda (exit)
  924. (for-each (lambda (x) (if (negative? x) (exit x)))
  925. '(54 0 37 -3 245 19))
  926. #t))
  927. (define list-length
  928. (lambda (obj)
  929. (call-with-current-continuation
  930. (lambda (return)
  931. (letrec ((r (lambda (obj) (cond ((null? obj) 0)
  932. ((pair? obj) (+ (r (cdr obj)) 1))
  933. (else (return #f))))))
  934. (r obj))))))
  935. (test 4 list-length '(1 2 3 4))
  936. (test #f list-length '(a b . c))
  937. (test '() map cadr '())
  938. ;;; This tests full conformance of call-with-current-continuation. It
  939. ;;; is a separate test because some schemes do not support call/cc
  940. ;;; other than escape procedures. I am indebted to
  941. ;;; raja@copper.ucs.indiana.edu (Raja Sooriamurthi) for fixing this
  942. ;;; code. The function leaf-eq? compares the leaves of 2 arbitrary
  943. ;;; trees constructed of conses.
  944. (define (next-leaf-generator obj eot)
  945. (letrec ((return #f)
  946. (cont (lambda (x)
  947. (recur obj)
  948. (set! cont (lambda (x) (return eot)))
  949. (cont #f)))
  950. (recur (lambda (obj)
  951. (if (pair? obj)
  952. (for-each recur obj)
  953. (call-with-current-continuation
  954. (lambda (c)
  955. (set! cont c)
  956. (return obj)))))))
  957. (lambda () (call-with-current-continuation
  958. (lambda (ret) (set! return ret) (cont #f))))))
  959. (define (leaf-eq? x y)
  960. (let* ((eot (list 'eot))
  961. (xf (next-leaf-generator x eot))
  962. (yf (next-leaf-generator y eot)))
  963. (letrec ((loop (lambda (x y)
  964. (cond ((not (eq? x y)) #f)
  965. ((eq? eot x) #t)
  966. (else (loop (xf) (yf)))))))
  967. (loop (xf) (yf)))))
  968. (define (test-cont)
  969. (SECTION "6 9 (continuations)")
  970. (test #t leaf-eq? '(a (b (c))) '((a) b c))
  971. (test #f leaf-eq? '(a (b (c))) '((a) b c d))
  972. (report-errs))
  973. ;;; Test Optional R4RS DELAY syntax and FORCE procedure
  974. (define (test-delay)
  975. (SECTION "6 9 (DELAY and FORCE)")
  976. (test 3 'delay (force (delay (+ 1 2))))
  977. (test '(3 3) 'delay (let ((p (delay (+ 1 2))))
  978. (list (force p) (force p))))
  979. (test 2 'delay (letrec ((a-stream
  980. (letrec ((next (lambda (n)
  981. (cons n (delay (next (+ n 1)))))))
  982. (next 0)))
  983. (head car)
  984. (tail (lambda (stream) (force (cdr stream)))))
  985. (head (tail (tail a-stream)))))
  986. (letrec ((count 0)
  987. (p (delay (begin (set! count (+ count 1))
  988. (if (> count x)
  989. count
  990. (force p)))))
  991. (x 5))
  992. (test 6 force p)
  993. (set! x 10)
  994. (test 6 force p))
  995. (test 3 'force
  996. (letrec ((p (delay (if c 3 (begin (set! c #t) (+ (force p) 1)))))
  997. (c #f))
  998. (force p)))
  999. (report-errs))
  1000. (SECTION 6 10 1)
  1001. (test #t input-port? (current-input-port))
  1002. (test #t output-port? (current-output-port))
  1003. (test #t call-with-input-file this-file-name input-port?)
  1004. (define this-file (open-input-file this-file-name))
  1005. (test #t input-port? this-file)
  1006. (SECTION 6 10 2)
  1007. (test #\; peek-char this-file)
  1008. (test #\; read-char this-file)
  1009. (test '(TEST-INIT "scm-test" 617) read this-file)
  1010. (test #\( peek-char this-file)
  1011. (test '(define errs '()) read this-file)
  1012. (close-input-port this-file)
  1013. (close-input-port this-file)
  1014. (define (check-test-file name)
  1015. (define test-file (open-input-file name))
  1016. (test #t 'input-port?
  1017. (call-with-input-file
  1018. name
  1019. (lambda (test-file)
  1020. (test load-test-obj read test-file)
  1021. (test #t eof-object? (peek-char test-file))
  1022. (test #t eof-object? (read-char test-file))
  1023. (input-port? test-file))))
  1024. (test #\; read-char test-file)
  1025. (test display-test-obj read test-file)
  1026. (test load-test-obj read test-file)
  1027. (close-input-port test-file))
  1028. (SECTION 6 10 3)
  1029. (define write-test-obj
  1030. '(#t #f #\a () 9739 -3 . #((test) "te \" \" st" "" test #() b c)))
  1031. (define display-test-obj
  1032. '(#t #f a () 9739 -3 . #((test) te " " st test #() b c)))
  1033. (define load-test-obj
  1034. (list 'define 'foo (list 'quote write-test-obj)))
  1035. (test #t call-with-output-file
  1036. "tmp1"
  1037. (lambda (test-file)
  1038. (write-char #\; test-file)
  1039. (display write-test-obj test-file)
  1040. (newline test-file)
  1041. (write load-test-obj test-file)
  1042. (output-port? test-file)))
  1043. (check-test-file "tmp1")
  1044. #|
  1045. (define foo (lambda () 9))
  1046. (test 9 'define (foo))
  1047. (define foo foo)
  1048. (test 9 'define (foo))
  1049. (define foo (let ((foo foo)) (lambda () (+ 1 (foo)))))
  1050. (test 10 'define (foo))
  1051. |#
  1052. (define test-file (open-output-file "tmp2"))
  1053. (write-char #\; test-file)
  1054. (display write-test-obj test-file)
  1055. (newline test-file)
  1056. (write load-test-obj test-file)
  1057. (test #t output-port? test-file)
  1058. (close-output-port test-file)
  1059. (check-test-file "tmp2")
  1060. (define (test-sc4)
  1061. (SECTION "[scheme 4 functions]")
  1062. (SECTION 6 7)
  1063. (test '(#\P #\space #\l) string->list "P l")
  1064. (test '() string->list "")
  1065. (test "1\\\"" list->string '(#\1 #\\ #\"))
  1066. (test "" list->string '())
  1067. (SECTION 6 8)
  1068. (test '(dah dah didah) vector->list '#(dah dah didah))
  1069. (test '() vector->list '#())
  1070. (test '#(dididit dah) list->vector '(dididit dah))
  1071. (test '#() list->vector '())
  1072. (SECTION 6 10 4)
  1073. (define-variable foo "FOO")
  1074. (load "tmp1")
  1075. (test write-test-obj 'load foo)
  1076. (report-errs))
  1077. (report-errs)
  1078. (let ((have-inexacts?
  1079. (and (string->number "0.0") (inexact? (string->number "0.0"))))
  1080. (have-bignums?
  1081. (let ((n (string->number "281474976710655325431")))
  1082. (and n (exact? n)))))
  1083. (cond (have-inexacts?
  1084. (test-inexact)
  1085. (test-inexact-printing)))
  1086. (if have-bignums? (test-bignum))
  1087. (if (and have-inexacts? have-bignums?)
  1088. (test-numeric-predicates)))
  1089. ;(newline)
  1090. (test-sc4)
  1091. (test-delay)
  1092. ;(display "To fully test continuations:")
  1093. ;(newline)
  1094. ;(display "(test-cont)")
  1095. ;'(newline)
  1096. (SECTION "last item in file")