r4rs.test 29 KB

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