r4rs.test 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  1. ;;;; r4rs.test --- tests for R4RS compliance -*- scheme -*-
  2. ;;;; Copyright (C) 1991, 1992, 1993, 1994, 1995, 1999, 2002 Free Software Foundation, Inc.
  3. ;;;;
  4. ;;;; This program is free software; you can redistribute it and/or modify
  5. ;;;; it under the terms of the GNU General Public License as published by
  6. ;;;; the Free Software Foundation; either version 2, or (at your option)
  7. ;;;; any later version.
  8. ;;;;
  9. ;;;; This program is distributed in the hope that it will be useful,
  10. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;;;; GNU General Public License for more details.
  13. ;;;;
  14. ;;;; You should have received a copy of the GNU General Public License
  15. ;;;; along with this software; see the file COPYING. If not, write to
  16. ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  17. ;;;; Boston, MA 02111-1307 USA
  18. ;;;;
  19. ;;;; As a special exception, the Free Software Foundation gives permission
  20. ;;;; for additional uses of the text contained in its release of GUILE.
  21. ;;;;
  22. ;;;; The exception is that, if you link the GUILE library with other files
  23. ;;;; to produce an executable, this does not by itself cause the
  24. ;;;; resulting executable to be covered by the GNU General Public License.
  25. ;;;; Your use of that executable is in no way restricted on account of
  26. ;;;; linking the GUILE library code into it.
  27. ;;;;
  28. ;;;; This exception does not however invalidate any other reasons why
  29. ;;;; the executable file might be covered by the GNU General Public License.
  30. ;;;;
  31. ;;;; This exception applies only to the code released by the
  32. ;;;; Free Software Foundation under the name GUILE. If you copy
  33. ;;;; code from other Free Software Foundation releases into a copy of
  34. ;;;; GUILE, as the General Public License permits, the exception does
  35. ;;;; not apply to the code that you add in this way. To avoid misleading
  36. ;;;; anyone as to the status of such modified files, you must delete
  37. ;;;; this exception notice from them.
  38. ;;;;
  39. ;;;; If you write modifications of your own for GUILE, it is your choice
  40. ;;;; whether to permit this exception to apply to your modifications.
  41. ;;;; If you do not wish that, delete this exception notice.
  42. ;;;; ============= NOTE =============
  43. ;;;; This file is a quick-and-dirty adaptation of Aubrey's test suite
  44. ;;;; to Guile's testing framework. As such, it's not as clean as one
  45. ;;;; might hope. (In particular, it uses with-test-prefix oddly.)
  46. ;;;;
  47. ;;;; If you're looking for an example of a test suite to imitate, you
  48. ;;;; might do better by looking at ports.test, which uses the
  49. ;;;; (test-suite lib) functions much more idiomatically.
  50. ;;;; "test.scm" Test correctness of scheme implementations.
  51. ;;;; Author: Aubrey Jaffer
  52. ;;;; Modified: Mikael Djurfeldt
  53. ;;;; Removed tests which Guile deliberately
  54. ;;;; won't pass. Made the the tests (test-cont), (test-sc4), and
  55. ;;;; (test-delay) start to run automatically.
  56. ;;;; Modified: Jim Blandy
  57. ;;;; adapted to new Guile test suite framework
  58. ;;; This includes examples from
  59. ;;; William Clinger and Jonathan Rees, editors.
  60. ;;; Revised^4 Report on the Algorithmic Language Scheme
  61. ;;; and the IEEE specification.
  62. ;;; The input tests read this file expecting it to be named
  63. ;;; "test.scm", so you'll have to run it from the ice-9 source
  64. ;;; directory, or copy this file elsewhere
  65. ;;; Files `tmp1', `tmp2' and `tmp3' will be created in the course of running
  66. ;;; these tests. You may need to delete them in order to run
  67. ;;; "test.scm" more than once.
  68. ;;; There are three optional tests:
  69. ;;; (TEST-CONT) tests multiple returns from call-with-current-continuation
  70. ;;;
  71. ;;; (TEST-SC4) tests procedures required by R4RS but not by IEEE
  72. ;;;
  73. ;;; (TEST-DELAY) tests DELAY and FORCE, which are not required by
  74. ;;; either standard.
  75. ;;; If you are testing a R3RS version which does not have `list?' do:
  76. ;;; (define list? #f)
  77. ;;; send corrections or additions to jaffer@ai.mit.edu or
  78. ;;; Aubrey Jaffer, 84 Pleasant St., Wakefield MA 01880, USA
  79. (define cur-section '())(define errs '())
  80. (define SECTION (lambda args
  81. (set! cur-section args) #t))
  82. (define record-error (lambda (e) (set! errs (cons (list cur-section e) errs))))
  83. (define (report-errs) #f)
  84. (define test
  85. (lambda (expect fun . args)
  86. (let ((res (if (procedure? fun) (apply fun args) (car args))))
  87. (with-test-prefix cur-section
  88. (pass-if (call-with-output-string (lambda (port)
  89. (write (cons fun args) port)))
  90. (equal? expect res))))))
  91. ;; test that all symbol characters are supported.
  92. (SECTION 2 1)
  93. '(+ - ... !.. $.+ %.- &.! *.: /:. :+. <-. =. >. ?. ~. _. ^.)
  94. (SECTION 3 4)
  95. (define disjoint-type-functions
  96. (list boolean? char? null? number? pair? procedure? string? symbol? vector?))
  97. (define type-examples
  98. (list
  99. #t #f #\a '() 9739 '(test) (lambda () #f) car "test" "" 'test
  100. '#() '#(a b c)))
  101. (define type-matrix
  102. (map (lambda (x)
  103. (let ((t (map (lambda (f) (f x)) disjoint-type-functions)))
  104. t))
  105. type-examples))
  106. (for-each (lambda (object row)
  107. (let ((count (apply + (map (lambda (elt) (if elt 1 0))
  108. row))))
  109. (pass-if (call-with-output-string
  110. (lambda (port)
  111. (display "object recognized by only one predicate: "
  112. port)
  113. (display object port)))
  114. (= count 1))))
  115. type-examples
  116. type-matrix)
  117. (SECTION 4 1 2)
  118. (test '(quote a) 'quote (quote 'a))
  119. (test '(quote a) 'quote ''a)
  120. (SECTION 4 1 3)
  121. (test 12 (if #f + *) 3 4)
  122. (SECTION 4 1 4)
  123. (test 8 (lambda (x) (+ x x)) 4)
  124. (define reverse-subtract
  125. (lambda (x y) (- y x)))
  126. (test 3 reverse-subtract 7 10)
  127. (define add4
  128. (let ((x 4))
  129. (lambda (y) (+ x y))))
  130. (test 10 add4 6)
  131. (test '(3 4 5 6) (lambda x x) 3 4 5 6)
  132. (test '(5 6) (lambda (x y . z) z) 3 4 5 6)
  133. (SECTION 4 1 5)
  134. (test 'yes 'if (if (> 3 2) 'yes 'no))
  135. (test 'no 'if (if (> 2 3) 'yes 'no))
  136. (test '1 'if (if (> 3 2) (- 3 2) (+ 3 2)))
  137. (SECTION 4 1 6)
  138. (define x 2)
  139. (test 3 'define (+ x 1))
  140. (set! x 4)
  141. (test 5 'set! (+ x 1))
  142. (SECTION 4 2 1)
  143. (test 'greater 'cond (cond ((> 3 2) 'greater)
  144. ((< 3 2) 'less)))
  145. (test 'equal 'cond (cond ((> 3 3) 'greater)
  146. ((< 3 3) 'less)
  147. (else 'equal)))
  148. (test 2 'cond (cond ((assv 'b '((a 1) (b 2))) => cadr)
  149. (else #f)))
  150. (test 'composite 'case (case (* 2 3)
  151. ((2 3 5 7) 'prime)
  152. ((1 4 6 8 9) 'composite)))
  153. (test 'consonant 'case (case (car '(c d))
  154. ((a e i o u) 'vowel)
  155. ((w y) 'semivowel)
  156. (else 'consonant)))
  157. (test #t 'and (and (= 2 2) (> 2 1)))
  158. (test #f 'and (and (= 2 2) (< 2 1)))
  159. (test '(f g) 'and (and 1 2 'c '(f g)))
  160. (test #t 'and (and))
  161. (test #t 'or (or (= 2 2) (> 2 1)))
  162. (test #t 'or (or (= 2 2) (< 2 1)))
  163. (test #f 'or (or #f #f #f))
  164. (test #f 'or (or))
  165. (test '(b c) 'or (or (memq 'b '(a b c)) (+ 3 0)))
  166. (SECTION 4 2 2)
  167. (test 6 'let (let ((x 2) (y 3)) (* x y)))
  168. (test 35 'let (let ((x 2) (y 3)) (let ((x 7) (z (+ x y))) (* z x))))
  169. (test 70 'let* (let ((x 2) (y 3)) (let* ((x 7) (z (+ x y))) (* z x))))
  170. (test #t 'letrec (letrec ((even?
  171. (lambda (n) (if (zero? n) #t (odd? (- n 1)))))
  172. (odd?
  173. (lambda (n) (if (zero? n) #f (even? (- n 1))))))
  174. (even? 88)))
  175. (define x 34)
  176. (test 5 'let (let ((x 3)) (define x 5) x))
  177. (test 34 'let x)
  178. (test 6 'let (let () (define x 6) x))
  179. (test 34 'let x)
  180. (test 7 'let* (let* ((x 3)) (define x 7) x))
  181. (test 34 'let* x)
  182. (test 8 'let* (let* () (define x 8) x))
  183. (test 34 'let* x)
  184. (test 9 'letrec (letrec () (define x 9) x))
  185. (test 34 'letrec x)
  186. (test 10 'letrec (letrec ((x 3)) (define x 10) x))
  187. (test 34 'letrec x)
  188. (SECTION 4 2 3)
  189. (define x 0)
  190. (test 6 'begin (begin (set! x 5) (+ x 1)))
  191. (SECTION 4 2 4)
  192. (test '#(0 1 2 3 4) 'do (do ((vec (make-vector 5))
  193. (i 0 (+ i 1)))
  194. ((= i 5) vec)
  195. (vector-set! vec i i)))
  196. (test 25 'do (let ((x '(1 3 5 7 9)))
  197. (do ((x x (cdr x))
  198. (sum 0 (+ sum (car x))))
  199. ((null? x) sum))))
  200. (test 1 'let (let foo () 1))
  201. (test '((6 1 3) (-5 -2)) 'let
  202. (let loop ((numbers '(3 -2 1 6 -5))
  203. (nonneg '())
  204. (neg '()))
  205. (cond ((null? numbers) (list nonneg neg))
  206. ((negative? (car numbers))
  207. (loop (cdr numbers)
  208. nonneg
  209. (cons (car numbers) neg)))
  210. (else
  211. (loop (cdr numbers)
  212. (cons (car numbers) nonneg)
  213. neg)))))
  214. (SECTION 4 2 6)
  215. (test '(list 3 4) 'quasiquote `(list ,(+ 1 2) 4))
  216. (test '(list a (quote a)) 'quasiquote (let ((name 'a)) `(list ,name ',name)))
  217. (test '(a 3 4 5 6 b) 'quasiquote `(a ,(+ 1 2) ,@(map abs '(4 -5 6)) b))
  218. (test '((foo 7) . cons)
  219. 'quasiquote
  220. `((foo ,(- 10 3)) ,@(cdr '(c)) . ,(car '(cons))))
  221. ;;; sqt is defined here because not all implementations are required to
  222. ;;; support it.
  223. (define (sqt x)
  224. (do ((i 0 (+ i 1)))
  225. ((> (* i i) x) (- i 1))))
  226. (test '#(10 5 2 4 3 8) 'quasiquote `#(10 5 ,(sqt 4) ,@(map sqt '(16 9)) 8))
  227. (test 5 'quasiquote `,(+ 2 3))
  228. (test '(a `(b ,(+ 1 2) ,(foo 4 d) e) f)
  229. 'quasiquote `(a `(b ,(+ 1 2) ,(foo ,(+ 1 3) d) e) f))
  230. (test '(a `(b ,x ,'y d) e) 'quasiquote
  231. (let ((name1 'x) (name2 'y)) `(a `(b ,,name1 ,',name2 d) e)))
  232. (test '(list 3 4) 'quasiquote (quasiquote (list (unquote (+ 1 2)) 4)))
  233. (test '`(list ,(+ 1 2) 4) 'quasiquote '(quasiquote (list (unquote (+ 1 2)) 4)))
  234. (SECTION 5 2 1)
  235. (define add3 (lambda (x) (+ x 3)))
  236. (test 6 'define (add3 3))
  237. (define first car)
  238. (test 1 'define (first '(1 2)))
  239. (SECTION 5 2 2)
  240. (test 45 'define
  241. (let ((x 5))
  242. (define foo (lambda (y) (bar x y)))
  243. (define bar (lambda (a b) (+ (* a b) a)))
  244. (foo (+ x 3))))
  245. (define x 34)
  246. (define (foo) (define x 5) x)
  247. (test 5 foo)
  248. (test 34 'define x)
  249. (define foo (lambda () (define x 5) x))
  250. (test 5 foo)
  251. (test 34 'define x)
  252. (define (foo x) ((lambda () (define x 5) x)) x)
  253. (test 88 foo 88)
  254. (test 4 foo 4)
  255. (test 34 'define x)
  256. (SECTION 6 1)
  257. (test #f not #t)
  258. (test #f not 3)
  259. (test #f not (list 3))
  260. (test #t not #f)
  261. (test #f not '())
  262. (test #f not (list))
  263. (test #f not 'nil)
  264. (test #t boolean? #f)
  265. (test #f boolean? 0)
  266. (test #f boolean? '())
  267. (SECTION 6 2)
  268. (test #t eqv? 'a 'a)
  269. (test #f eqv? 'a 'b)
  270. (test #t eqv? 2 2)
  271. (test #t eqv? '() '())
  272. (test #t eqv? '10000 '10000)
  273. (test #f eqv? (cons 1 2)(cons 1 2))
  274. (test #f eqv? (lambda () 1) (lambda () 2))
  275. (test #f eqv? #f 'nil)
  276. (let ((p (lambda (x) x)))
  277. (test #t eqv? p p))
  278. (define gen-counter
  279. (lambda ()
  280. (let ((n 0))
  281. (lambda () (set! n (+ n 1)) n))))
  282. (let ((g (gen-counter))) (test #t eqv? g g))
  283. (test #f eqv? (gen-counter) (gen-counter))
  284. (letrec ((f (lambda () (if (eqv? f g) 'f 'both)))
  285. (g (lambda () (if (eqv? f g) 'g 'both))))
  286. (test #f eqv? f g))
  287. (test #t eq? 'a 'a)
  288. (test #f eq? (list 'a) (list 'a))
  289. (test #t eq? '() '())
  290. (test #t eq? car car)
  291. (let ((x '(a))) (test #t eq? x x))
  292. (let ((x '#())) (test #t eq? x x))
  293. (let ((x (lambda (x) x))) (test #t eq? x x))
  294. (test #t equal? 'a 'a)
  295. (test #t equal? '(a) '(a))
  296. (test #t equal? '(a (b) c) '(a (b) c))
  297. (test #t equal? "abc" "abc")
  298. (test #t equal? 2 2)
  299. (test #t equal? (make-vector 5 'a) (make-vector 5 'a))
  300. (SECTION 6 3)
  301. (test '(a b c d e) 'dot '(a . (b . (c . (d . (e . ()))))))
  302. (define x (list 'a 'b 'c))
  303. (define y x)
  304. (and list? (test #t list? y))
  305. (set-cdr! x 4)
  306. (test '(a . 4) 'set-cdr! x)
  307. (test #t eqv? x y)
  308. (test '(a b c . d) 'dot '(a . (b . (c . d))))
  309. (and list? (test #f list? y))
  310. (and list? (let ((x (list 'a))) (set-cdr! x x) (test #f 'list? (list? x))))
  311. (test #t pair? '(a . b))
  312. (test #t pair? '(a . 1))
  313. (test #t pair? '(a b c))
  314. (test #f pair? '())
  315. (test #f pair? '#(a b))
  316. (test '(a) cons 'a '())
  317. (test '((a) b c d) cons '(a) '(b c d))
  318. (test '("a" b c) cons "a" '(b c))
  319. (test '(a . 3) cons 'a 3)
  320. (test '((a b) . c) cons '(a b) 'c)
  321. (test 'a car '(a b c))
  322. (test '(a) car '((a) b c d))
  323. (test 1 car '(1 . 2))
  324. (test '(b c d) cdr '((a) b c d))
  325. (test 2 cdr '(1 . 2))
  326. (test '(a 7 c) list 'a (+ 3 4) 'c)
  327. (test '() list)
  328. (test 3 length '(a b c))
  329. (test 3 length '(a (b) (c d e)))
  330. (test 0 length '())
  331. (test '(x y) append '(x) '(y))
  332. (test '(a b c d) append '(a) '(b c d))
  333. (test '(a (b) (c)) append '(a (b)) '((c)))
  334. (test '() append)
  335. (test '(a b c . d) append '(a b) '(c . d))
  336. (test 'a append '() 'a)
  337. (test '(c b a) reverse '(a b c))
  338. (test '((e (f)) d (b c) a) reverse '(a (b c) d (e (f))))
  339. (test 'c list-ref '(a b c d) 2)
  340. (test '(a b c) memq 'a '(a b c))
  341. (test '(b c) memq 'b '(a b c))
  342. (test '#f memq 'a '(b c d))
  343. (test '#f memq (list 'a) '(b (a) c))
  344. (test '((a) c) member (list 'a) '(b (a) c))
  345. (test '(101 102) memv 101 '(100 101 102))
  346. (define e '((a 1) (b 2) (c 3)))
  347. (test '(a 1) assq 'a e)
  348. (test '(b 2) assq 'b e)
  349. (test #f assq 'd e)
  350. (test #f assq (list 'a) '(((a)) ((b)) ((c))))
  351. (test '((a)) assoc (list 'a) '(((a)) ((b)) ((c))))
  352. (test '(5 7) assv 5 '((2 3) (5 7) (11 13)))
  353. (SECTION 6 4)
  354. (test #t symbol? 'foo)
  355. (test #t symbol? (car '(a b)))
  356. (test #f symbol? "bar")
  357. (test #t symbol? 'nil)
  358. (test #f symbol? '())
  359. (test #f symbol? #f)
  360. ;;; But first, what case are symbols in? Determine the standard case:
  361. (define char-standard-case char-upcase)
  362. (if (string=? (symbol->string 'A) "a")
  363. (set! char-standard-case char-downcase))
  364. ;;; Not for Guile
  365. ;(test #t 'standard-case
  366. ; (string=? (symbol->string 'a) (symbol->string 'A)))
  367. ;(test #t 'standard-case
  368. ; (or (string=? (symbol->string 'a) "A")
  369. ; (string=? (symbol->string 'A) "a")))
  370. (define (str-copy s)
  371. (let ((v (make-string (string-length s))))
  372. (do ((i (- (string-length v) 1) (- i 1)))
  373. ((< i 0) v)
  374. (string-set! v i (string-ref s i)))))
  375. (define (string-standard-case s)
  376. (set! s (str-copy s))
  377. (do ((i 0 (+ 1 i))
  378. (sl (string-length s)))
  379. ((>= i sl) s)
  380. (string-set! s i (char-standard-case (string-ref s i)))))
  381. ;;; Not for Guile
  382. ;(test (string-standard-case "flying-fish") symbol->string 'flying-fish)
  383. ;(test (string-standard-case "martin") symbol->string 'Martin)
  384. (test "Malvina" symbol->string (string->symbol "Malvina"))
  385. ;;; Not for Guile
  386. ;(test #t 'standard-case (eq? 'a 'A))
  387. (define x (string #\a #\b))
  388. (define y (string->symbol x))
  389. (string-set! x 0 #\c)
  390. (test "cb" 'string-set! x)
  391. (test "ab" symbol->string y)
  392. (test y string->symbol "ab")
  393. ;;; Not for Guile
  394. ;(test #t eq? 'mISSISSIppi 'mississippi)
  395. ;(test #f 'string->symbol (eq? 'bitBlt (string->symbol "bitBlt")))
  396. (test 'JollyWog string->symbol (symbol->string 'JollyWog))
  397. (SECTION 6 5 5)
  398. (test #t number? 3)
  399. (test #t complex? 3)
  400. (test #t real? 3)
  401. (test #t rational? 3)
  402. (test #t integer? 3)
  403. (test #t exact? 3)
  404. (test #f inexact? 3)
  405. (test #t = 22 22 22)
  406. (test #t = 22 22)
  407. (test #f = 34 34 35)
  408. (test #f = 34 35)
  409. (test #t > 3 -6246)
  410. (test #f > 9 9 -2424)
  411. (test #t >= 3 -4 -6246)
  412. (test #t >= 9 9)
  413. (test #f >= 8 9)
  414. (test #t < -1 2 3 4 5 6 7 8)
  415. (test #f < -1 2 3 4 4 5 6 7)
  416. (test #t <= -1 2 3 4 5 6 7 8)
  417. (test #t <= -1 2 3 4 4 5 6 7)
  418. (test #f < 1 3 2)
  419. (test #f >= 1 3 2)
  420. (test #t zero? 0)
  421. (test #f zero? 1)
  422. (test #f zero? -1)
  423. (test #f zero? -100)
  424. (test #t positive? 4)
  425. (test #f positive? -4)
  426. (test #f positive? 0)
  427. (test #f negative? 4)
  428. (test #t negative? -4)
  429. (test #f negative? 0)
  430. (test #t odd? 3)
  431. (test #f odd? 2)
  432. (test #f odd? -4)
  433. (test #t odd? -1)
  434. (test #f even? 3)
  435. (test #t even? 2)
  436. (test #t even? -4)
  437. (test #f even? -1)
  438. (test 38 max 34 5 7 38 6)
  439. (test -24 min 3 5 5 330 4 -24)
  440. (test 7 + 3 4)
  441. (test '3 + 3)
  442. (test 0 +)
  443. (test 4 * 4)
  444. (test 1 *)
  445. (test -1 - 3 4)
  446. (test -3 - 3)
  447. (test 7 abs -7)
  448. (test 7 abs 7)
  449. (test 0 abs 0)
  450. (test 5 quotient 35 7)
  451. (test -5 quotient -35 7)
  452. (test -5 quotient 35 -7)
  453. (test 5 quotient -35 -7)
  454. (test 1 modulo 13 4)
  455. (test 1 remainder 13 4)
  456. (test 3 modulo -13 4)
  457. (test -1 remainder -13 4)
  458. (test -3 modulo 13 -4)
  459. (test 1 remainder 13 -4)
  460. (test -1 modulo -13 -4)
  461. (test -1 remainder -13 -4)
  462. (define (divtest n1 n2)
  463. (= n1 (+ (* n2 (quotient n1 n2))
  464. (remainder n1 n2))))
  465. (test #t divtest 238 9)
  466. (test #t divtest -238 9)
  467. (test #t divtest 238 -9)
  468. (test #t divtest -238 -9)
  469. (test 4 gcd 0 4)
  470. (test 4 gcd -4 0)
  471. (test 4 gcd 32 -36)
  472. (test 0 gcd)
  473. (test 288 lcm 32 -36)
  474. (test 1 lcm)
  475. ;;;;From: fred@sce.carleton.ca (Fred J Kaudel)
  476. ;;; Modified by jaffer.
  477. (define (test-inexact)
  478. (define f3.9 (string->number "3.9"))
  479. (define f4.0 (string->number "4.0"))
  480. (define f-3.25 (string->number "-3.25"))
  481. (define f.25 (string->number ".25"))
  482. (define f4.5 (string->number "4.5"))
  483. (define f3.5 (string->number "3.5"))
  484. (define f0.0 (string->number "0.0"))
  485. (define f0.8 (string->number "0.8"))
  486. (define f1.0 (string->number "1.0"))
  487. (define wto write-test-obj)
  488. (define dto display-test-obj)
  489. (define lto load-test-obj)
  490. (SECTION 6 5 5)
  491. (test #t inexact? f3.9)
  492. (test #t 'inexact? (inexact? (max f3.9 4)))
  493. (test f4.0 'max (max f3.9 4))
  494. (test f4.0 'exact->inexact (exact->inexact 4))
  495. (test (- f4.0) round (- f4.5))
  496. (test (- f4.0) round (- f3.5))
  497. (test (- f4.0) round (- f3.9))
  498. (test f0.0 round f0.0)
  499. (test f0.0 round f.25)
  500. (test f1.0 round f0.8)
  501. (test f4.0 round f3.5)
  502. (test f4.0 round f4.5)
  503. (set! write-test-obj (list f.25 f-3.25));.25 inexact errors less likely.
  504. (set! display-test-obj (list f.25 f-3.25));.3 often has such errors (~10^-13)
  505. (set! load-test-obj (list 'define 'foo (list 'quote write-test-obj)))
  506. (test #t call-with-output-file
  507. "tmp3"
  508. (lambda (test-file)
  509. (write-char #\; test-file)
  510. (display write-test-obj test-file)
  511. (newline test-file)
  512. (write load-test-obj test-file)
  513. (output-port? test-file)))
  514. (check-test-file "tmp3")
  515. (set! write-test-obj wto)
  516. (set! display-test-obj dto)
  517. (set! load-test-obj lto)
  518. (let ((x (string->number "4195835.0"))
  519. (y (string->number "3145727.0")))
  520. (test #t 'pentium-fdiv-bug (> f1.0 (- x (* (/ x y) y)))))
  521. (report-errs))
  522. (define (test-bignum)
  523. (define tb
  524. (lambda (n1 n2)
  525. (= n1 (+ (* n2 (quotient n1 n2))
  526. (remainder n1 n2)))))
  527. (SECTION 6 5 5)
  528. (test 0 modulo -2177452800 86400)
  529. (test 0 modulo 2177452800 -86400)
  530. (test 0 modulo 2177452800 86400)
  531. (test 0 modulo -2177452800 -86400)
  532. (test #t 'remainder (tb 281474976710655 65535))
  533. (test #t 'remainder (tb 281474976710654 65535))
  534. (SECTION 6 5 6)
  535. (test 281474976710655 string->number "281474976710655")
  536. (test "281474976710655" number->string 281474976710655)
  537. (report-errs))
  538. (SECTION 6 5 6)
  539. (test "0" number->string 0)
  540. (test "100" number->string 100)
  541. (test "100" number->string 256 16)
  542. (test 100 string->number "100")
  543. (test 256 string->number "100" 16)
  544. (test #f string->number "")
  545. (test #f string->number ".")
  546. (test #f string->number "d")
  547. (test #f string->number "D")
  548. (test #f string->number "i")
  549. (test #f string->number "I")
  550. (test #f string->number "3i")
  551. (test #f string->number "3I")
  552. (test #f string->number "33i")
  553. (test #f string->number "33I")
  554. (test #f string->number "3.3i")
  555. (test #f string->number "3.3I")
  556. (test #f string->number "-")
  557. (test #f string->number "+")
  558. (SECTION 6 6)
  559. (test #t eqv? '#\ #\Space)
  560. (test #t eqv? #\space '#\Space)
  561. (test #t char? #\a)
  562. (test #t char? #\()
  563. (test #t char? #\ )
  564. (test #t char? '#\newline)
  565. (test #f char=? #\A #\B)
  566. (test #f char=? #\a #\b)
  567. (test #f char=? #\9 #\0)
  568. (test #t char=? #\A #\A)
  569. (test #t char<? #\A #\B)
  570. (test #t char<? #\a #\b)
  571. (test #f char<? #\9 #\0)
  572. (test #f char<? #\A #\A)
  573. (test #f char>? #\A #\B)
  574. (test #f char>? #\a #\b)
  575. (test #t char>? #\9 #\0)
  576. (test #f char>? #\A #\A)
  577. (test #t char<=? #\A #\B)
  578. (test #t char<=? #\a #\b)
  579. (test #f char<=? #\9 #\0)
  580. (test #t char<=? #\A #\A)
  581. (test #f char>=? #\A #\B)
  582. (test #f char>=? #\a #\b)
  583. (test #t char>=? #\9 #\0)
  584. (test #t char>=? #\A #\A)
  585. (test #f char-ci=? #\A #\B)
  586. (test #f char-ci=? #\a #\B)
  587. (test #f char-ci=? #\A #\b)
  588. (test #f char-ci=? #\a #\b)
  589. (test #f char-ci=? #\9 #\0)
  590. (test #t char-ci=? #\A #\A)
  591. (test #t char-ci=? #\A #\a)
  592. (test #t char-ci<? #\A #\B)
  593. (test #t char-ci<? #\a #\B)
  594. (test #t char-ci<? #\A #\b)
  595. (test #t char-ci<? #\a #\b)
  596. (test #f char-ci<? #\9 #\0)
  597. (test #f char-ci<? #\A #\A)
  598. (test #f char-ci<? #\A #\a)
  599. (test #f char-ci>? #\A #\B)
  600. (test #f char-ci>? #\a #\B)
  601. (test #f char-ci>? #\A #\b)
  602. (test #f char-ci>? #\a #\b)
  603. (test #t char-ci>? #\9 #\0)
  604. (test #f char-ci>? #\A #\A)
  605. (test #f char-ci>? #\A #\a)
  606. (test #t char-ci<=? #\A #\B)
  607. (test #t char-ci<=? #\a #\B)
  608. (test #t char-ci<=? #\A #\b)
  609. (test #t char-ci<=? #\a #\b)
  610. (test #f char-ci<=? #\9 #\0)
  611. (test #t char-ci<=? #\A #\A)
  612. (test #t char-ci<=? #\A #\a)
  613. (test #f char-ci>=? #\A #\B)
  614. (test #f char-ci>=? #\a #\B)
  615. (test #f char-ci>=? #\A #\b)
  616. (test #f char-ci>=? #\a #\b)
  617. (test #t char-ci>=? #\9 #\0)
  618. (test #t char-ci>=? #\A #\A)
  619. (test #t char-ci>=? #\A #\a)
  620. (test #t char-alphabetic? #\a)
  621. (test #t char-alphabetic? #\A)
  622. (test #t char-alphabetic? #\z)
  623. (test #t char-alphabetic? #\Z)
  624. (test #f char-alphabetic? #\0)
  625. (test #f char-alphabetic? #\9)
  626. (test #f char-alphabetic? #\space)
  627. (test #f char-alphabetic? #\;)
  628. (test #f char-numeric? #\a)
  629. (test #f char-numeric? #\A)
  630. (test #f char-numeric? #\z)
  631. (test #f char-numeric? #\Z)
  632. (test #t char-numeric? #\0)
  633. (test #t char-numeric? #\9)
  634. (test #f char-numeric? #\space)
  635. (test #f char-numeric? #\;)
  636. (test #f char-whitespace? #\a)
  637. (test #f char-whitespace? #\A)
  638. (test #f char-whitespace? #\z)
  639. (test #f char-whitespace? #\Z)
  640. (test #f char-whitespace? #\0)
  641. (test #f char-whitespace? #\9)
  642. (test #t char-whitespace? #\space)
  643. (test #f char-whitespace? #\;)
  644. (test #f char-upper-case? #\0)
  645. (test #f char-upper-case? #\9)
  646. (test #f char-upper-case? #\space)
  647. (test #f char-upper-case? #\;)
  648. (test #f char-lower-case? #\0)
  649. (test #f char-lower-case? #\9)
  650. (test #f char-lower-case? #\space)
  651. (test #f char-lower-case? #\;)
  652. (test #\. integer->char (char->integer #\.))
  653. (test #\A integer->char (char->integer #\A))
  654. (test #\a integer->char (char->integer #\a))
  655. (test #\A char-upcase #\A)
  656. (test #\A char-upcase #\a)
  657. (test #\a char-downcase #\A)
  658. (test #\a char-downcase #\a)
  659. (SECTION 6 7)
  660. (test #t string? "The word \"recursion\\\" has many meanings.")
  661. (test #t string? "")
  662. (define f (make-string 3 #\*))
  663. (test "?**" 'string-set! (begin (string-set! f 0 #\?) f))
  664. (test "abc" string #\a #\b #\c)
  665. (test "" string)
  666. (test 3 string-length "abc")
  667. (test #\a string-ref "abc" 0)
  668. (test #\c string-ref "abc" 2)
  669. (test 0 string-length "")
  670. (test "" substring "ab" 0 0)
  671. (test "" substring "ab" 1 1)
  672. (test "" substring "ab" 2 2)
  673. (test "a" substring "ab" 0 1)
  674. (test "b" substring "ab" 1 2)
  675. (test "ab" substring "ab" 0 2)
  676. (test "foobar" string-append "foo" "bar")
  677. (test "foo" string-append "foo")
  678. (test "foo" string-append "foo" "")
  679. (test "foo" string-append "" "foo")
  680. (test "" string-append)
  681. (test "" make-string 0)
  682. (test #t string=? "" "")
  683. (test #f string<? "" "")
  684. (test #f string>? "" "")
  685. (test #t string<=? "" "")
  686. (test #t string>=? "" "")
  687. (test #t string-ci=? "" "")
  688. (test #f string-ci<? "" "")
  689. (test #f string-ci>? "" "")
  690. (test #t string-ci<=? "" "")
  691. (test #t string-ci>=? "" "")
  692. (test #f string=? "A" "B")
  693. (test #f string=? "a" "b")
  694. (test #f string=? "9" "0")
  695. (test #t string=? "A" "A")
  696. (test #t string<? "A" "B")
  697. (test #t string<? "a" "b")
  698. (test #f string<? "9" "0")
  699. (test #f string<? "A" "A")
  700. (test #f string>? "A" "B")
  701. (test #f string>? "a" "b")
  702. (test #t string>? "9" "0")
  703. (test #f string>? "A" "A")
  704. (test #t string<=? "A" "B")
  705. (test #t string<=? "a" "b")
  706. (test #f string<=? "9" "0")
  707. (test #t string<=? "A" "A")
  708. (test #f string>=? "A" "B")
  709. (test #f string>=? "a" "b")
  710. (test #t string>=? "9" "0")
  711. (test #t string>=? "A" "A")
  712. (test #f string-ci=? "A" "B")
  713. (test #f string-ci=? "a" "B")
  714. (test #f string-ci=? "A" "b")
  715. (test #f string-ci=? "a" "b")
  716. (test #f string-ci=? "9" "0")
  717. (test #t string-ci=? "A" "A")
  718. (test #t string-ci=? "A" "a")
  719. (test #t string-ci<? "A" "B")
  720. (test #t string-ci<? "a" "B")
  721. (test #t string-ci<? "A" "b")
  722. (test #t string-ci<? "a" "b")
  723. (test #f string-ci<? "9" "0")
  724. (test #f string-ci<? "A" "A")
  725. (test #f string-ci<? "A" "a")
  726. (test #f string-ci>? "A" "B")
  727. (test #f string-ci>? "a" "B")
  728. (test #f string-ci>? "A" "b")
  729. (test #f string-ci>? "a" "b")
  730. (test #t string-ci>? "9" "0")
  731. (test #f string-ci>? "A" "A")
  732. (test #f string-ci>? "A" "a")
  733. (test #t string-ci<=? "A" "B")
  734. (test #t string-ci<=? "a" "B")
  735. (test #t string-ci<=? "A" "b")
  736. (test #t string-ci<=? "a" "b")
  737. (test #f string-ci<=? "9" "0")
  738. (test #t string-ci<=? "A" "A")
  739. (test #t string-ci<=? "A" "a")
  740. (test #f string-ci>=? "A" "B")
  741. (test #f string-ci>=? "a" "B")
  742. (test #f string-ci>=? "A" "b")
  743. (test #f string-ci>=? "a" "b")
  744. (test #t string-ci>=? "9" "0")
  745. (test #t string-ci>=? "A" "A")
  746. (test #t string-ci>=? "A" "a")
  747. (SECTION 6 8)
  748. (test #t vector? '#(0 (2 2 2 2) "Anna"))
  749. (test #t vector? '#())
  750. (test '#(a b c) vector 'a 'b 'c)
  751. (test '#() vector)
  752. (test 3 vector-length '#(0 (2 2 2 2) "Anna"))
  753. (test 0 vector-length '#())
  754. (test 8 vector-ref '#(1 1 2 3 5 8 13 21) 5)
  755. (test '#(0 ("Sue" "Sue") "Anna") 'vector-set
  756. (let ((vec (vector 0 '(2 2 2 2) "Anna")))
  757. (vector-set! vec 1 '("Sue" "Sue"))
  758. vec))
  759. (test '#(hi hi) make-vector 2 'hi)
  760. (test '#() make-vector 0)
  761. (test '#() make-vector 0 'a)
  762. (SECTION 6 9)
  763. (test #t procedure? car)
  764. (test #f procedure? 'car)
  765. (test #t procedure? (lambda (x) (* x x)))
  766. (test #f procedure? '(lambda (x) (* x x)))
  767. (test #t call-with-current-continuation procedure?)
  768. (test 7 apply + (list 3 4))
  769. (test 7 apply (lambda (a b) (+ a b)) (list 3 4))
  770. (test 17 apply + 10 (list 3 4))
  771. (test '() apply list '())
  772. (define compose (lambda (f g) (lambda args (f (apply g args)))))
  773. (test 30 (compose sqt *) 12 75)
  774. (test '(b e h) map cadr '((a b) (d e) (g h)))
  775. (test '(5 7 9) map + '(1 2 3) '(4 5 6))
  776. (test '#(0 1 4 9 16) 'for-each
  777. (let ((v (make-vector 5)))
  778. (for-each (lambda (i) (vector-set! v i (* i i)))
  779. '(0 1 2 3 4))
  780. v))
  781. (test -3 call-with-current-continuation
  782. (lambda (exit)
  783. (for-each (lambda (x) (if (negative? x) (exit x)))
  784. '(54 0 37 -3 245 19))
  785. #t))
  786. (define list-length
  787. (lambda (obj)
  788. (call-with-current-continuation
  789. (lambda (return)
  790. (letrec ((r (lambda (obj) (cond ((null? obj) 0)
  791. ((pair? obj) (+ (r (cdr obj)) 1))
  792. (else (return #f))))))
  793. (r obj))))))
  794. (test 4 list-length '(1 2 3 4))
  795. (test #f list-length '(a b . c))
  796. (test '() map cadr '())
  797. ;;; This tests full conformance of call-with-current-continuation. It
  798. ;;; is a separate test because some schemes do not support call/cc
  799. ;;; other than escape procedures. I am indebted to
  800. ;;; raja@copper.ucs.indiana.edu (Raja Sooriamurthi) for fixing this
  801. ;;; code. The function leaf-eq? compares the leaves of 2 arbitrary
  802. ;;; trees constructed of conses.
  803. (define (next-leaf-generator obj eot)
  804. (letrec ((return #f)
  805. (cont (lambda (x)
  806. (recur obj)
  807. (set! cont (lambda (x) (return eot)))
  808. (cont #f)))
  809. (recur (lambda (obj)
  810. (if (pair? obj)
  811. (for-each recur obj)
  812. (call-with-current-continuation
  813. (lambda (c)
  814. (set! cont c)
  815. (return obj)))))))
  816. (lambda () (call-with-current-continuation
  817. (lambda (ret) (set! return ret) (cont #f))))))
  818. (define (leaf-eq? x y)
  819. (let* ((eot (list 'eot))
  820. (xf (next-leaf-generator x eot))
  821. (yf (next-leaf-generator y eot)))
  822. (letrec ((loop (lambda (x y)
  823. (cond ((not (eq? x y)) #f)
  824. ((eq? eot x) #t)
  825. (else (loop (xf) (yf)))))))
  826. (loop (xf) (yf)))))
  827. (define (test-cont)
  828. (SECTION 6 9)
  829. (test #t leaf-eq? '(a (b (c))) '((a) b c))
  830. (test #f leaf-eq? '(a (b (c))) '((a) b c d))
  831. (report-errs))
  832. ;;; Test Optional R4RS DELAY syntax and FORCE procedure
  833. (define (test-delay)
  834. (SECTION 6 9)
  835. (test 3 'delay (force (delay (+ 1 2))))
  836. (test '(3 3) 'delay (let ((p (delay (+ 1 2))))
  837. (list (force p) (force p))))
  838. (test 2 'delay (letrec ((a-stream
  839. (letrec ((next (lambda (n)
  840. (cons n (delay (next (+ n 1)))))))
  841. (next 0)))
  842. (head car)
  843. (tail (lambda (stream) (force (cdr stream)))))
  844. (head (tail (tail a-stream)))))
  845. (letrec ((count 0)
  846. (p (delay (begin (set! count (+ count 1))
  847. (if (> count x)
  848. count
  849. (force p)))))
  850. (x 5))
  851. (test 6 force p)
  852. (set! x 10)
  853. (test 6 force p))
  854. (test 3 'force
  855. (letrec ((p (delay (if c 3 (begin (set! c #t) (+ (force p) 1)))))
  856. (c #f))
  857. (force p)))
  858. (report-errs))
  859. (SECTION 6 10 1)
  860. (test #t input-port? (current-input-port))
  861. (test #t output-port? (current-output-port))
  862. (test #t call-with-input-file (test-file-name "r4rs.test") input-port?)
  863. (define this-file (open-input-file (test-file-name "r4rs.test")))
  864. (test #t input-port? this-file)
  865. (SECTION 6 10 2)
  866. (test #\; peek-char this-file)
  867. (test #\; read-char this-file)
  868. (test '(define cur-section '()) read this-file)
  869. (test #\( peek-char this-file)
  870. (test '(define errs '()) read this-file)
  871. (close-input-port this-file)
  872. (close-input-port this-file)
  873. (define (check-test-file name)
  874. (define test-file (open-input-file name))
  875. (test #t 'input-port?
  876. (call-with-input-file
  877. name
  878. (lambda (test-file)
  879. (test load-test-obj read test-file)
  880. (test #t eof-object? (peek-char test-file))
  881. (test #t eof-object? (read-char test-file))
  882. (input-port? test-file))))
  883. (test #\; read-char test-file)
  884. (test display-test-obj read test-file)
  885. (test load-test-obj read test-file)
  886. (close-input-port test-file))
  887. (SECTION 6 10 3)
  888. (define write-test-obj
  889. '(#t #f #\a () 9739 -3 . #((test) "te \" \" st" "" test #() b c)))
  890. (define display-test-obj
  891. '(#t #f a () 9739 -3 . #((test) te " " st test #() b c)))
  892. (define load-test-obj
  893. (list 'define 'foo (list 'quote write-test-obj)))
  894. (test #t call-with-output-file
  895. "tmp1"
  896. (lambda (test-file)
  897. (write-char #\; test-file)
  898. (display write-test-obj test-file)
  899. (newline test-file)
  900. (write load-test-obj test-file)
  901. (output-port? test-file)))
  902. (check-test-file "tmp1")
  903. (define test-file (open-output-file "tmp2"))
  904. (write-char #\; test-file)
  905. (display write-test-obj test-file)
  906. (newline test-file)
  907. (write load-test-obj test-file)
  908. (test #t output-port? test-file)
  909. (close-output-port test-file)
  910. (check-test-file "tmp2")
  911. (define (test-sc4)
  912. (SECTION 6 7)
  913. (test '(#\P #\space #\l) string->list "P l")
  914. (test '() string->list "")
  915. (test "1\\\"" list->string '(#\1 #\\ #\"))
  916. (test "" list->string '())
  917. (SECTION 6 8)
  918. (test '(dah dah didah) vector->list '#(dah dah didah))
  919. (test '() vector->list '#())
  920. (test '#(dididit dah) list->vector '(dididit dah))
  921. (test '#() list->vector '())
  922. (SECTION 6 10 4)
  923. (load (data-file-name "tmp1"))
  924. (test write-test-obj 'load foo)
  925. (report-errs))
  926. (report-errs)
  927. (if (and (string->number "0.0") (inexact? (string->number "0.0")))
  928. (test-inexact))
  929. (let ((n (string->number "281474976710655")))
  930. (if (and n (exact? n))
  931. (test-bignum)))
  932. (test-cont)
  933. (test-sc4)
  934. (test-delay)
  935. "last item in file"