test.scm 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. ;; Copyright (C) 1991, 1992, 1993, 1994, 1995, 2006, 2023 Free Software Foundation, Inc.
  2. ;;
  3. ;;;; This library is free software; you can redistribute it and/or
  4. ;;;; modify it under the terms of the GNU Lesser General Public
  5. ;;;; License as published by the Free Software Foundation; either
  6. ;;;; version 3 of the License, or (at your option) any later version.
  7. ;;;;
  8. ;;;; This library is distributed in the hope that it will be useful,
  9. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;;;; Lesser General Public License for more details.
  12. ;;;;
  13. ;;;; You should have received a copy of the GNU Lesser General Public
  14. ;;;; License along with this library; if not, write to the Free Software
  15. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. ;;;; "test.scm" Test correctness of scheme implementations.
  17. ;;; Author: Aubrey Jaffer
  18. ;;; Modified: Mikael Djurfeldt (Removed tests which Guile deliberately
  19. ;;; won't pass. Made the tests (test-cont), (test-sc4), and
  20. ;;; (test-delay) start to run automatically.
  21. ;;; This includes examples from
  22. ;;; William Clinger and Jonathan Rees, editors.
  23. ;;; Revised^4 Report on the Algorithmic Language Scheme
  24. ;;; and the IEEE specification.
  25. ;;; The input tests read this file expecting it to be named
  26. ;;; "test.scm", so you'll have to run it from the ice-9 source
  27. ;;; directory, or copy this file elsewhere
  28. ;;; Files `tmp1', `tmp2' and `tmp3' will be created in the course of running
  29. ;;; these tests. You may need to delete them in order to run
  30. ;;; "test.scm" more than once.
  31. ;;; There are three optional tests:
  32. ;;; (TEST-CONT) tests multiple returns from call-with-current-continuation
  33. ;;;
  34. ;;; (TEST-SC4) tests procedures required by R4RS but not by IEEE
  35. ;;;
  36. ;;; (TEST-DELAY) tests DELAY and FORCE, which are not required by
  37. ;;; either standard.
  38. ;;; If you are testing a R3RS version which does not have `list?' do:
  39. ;;; (define list? #f)
  40. ;;; send corrections or additions to jaffer@ai.mit.edu or
  41. ;;; Aubrey Jaffer, 84 Pleasant St., Wakefield MA 01880, USA
  42. (define cur-section '())(define errs '())
  43. (define SECTION (lambda args
  44. (display "SECTION") (write args) (newline)
  45. (set! cur-section args) #t))
  46. (define record-error (lambda (e) (set! errs (cons (list cur-section e) errs))))
  47. (define test
  48. (lambda (expect fun . args)
  49. (write (cons fun args))
  50. (display " ==> ")
  51. ((lambda (res)
  52. (write res)
  53. (newline)
  54. (cond ((not (equal? expect res))
  55. (record-error (list res expect (cons fun args)))
  56. (display " BUT EXPECTED ")
  57. (write expect)
  58. (newline)
  59. #f)
  60. (else #t)))
  61. (if (procedure? fun) (apply fun args) (car args)))))
  62. (define (report-errs)
  63. (newline)
  64. (if (null? errs) (display "Passed all tests")
  65. (begin
  66. (display "errors were:")
  67. (newline)
  68. (display "(SECTION (got expected (call)))")
  69. (newline)
  70. (for-each (lambda (l) (write l) (newline))
  71. errs)))
  72. (newline))
  73. (SECTION 2 1);; test that all symbol characters are supported.
  74. '(+ - ... !.. $.+ %.- &.! *.: /:. :+. <-. =. >. ?. ~. _. ^.)
  75. (SECTION 3 4)
  76. (define disjoint-type-functions
  77. (list boolean? char? null? number? pair? procedure? string? symbol? vector?))
  78. (define type-examples
  79. (list
  80. #t #f #\a '() 9739 '(test) record-error "test" "" 'test '#() '#(a b c) ))
  81. (define i 1)
  82. (for-each (lambda (x) (display (make-string i #\ ))
  83. (set! i (+ 3 i))
  84. (write x)
  85. (newline))
  86. disjoint-type-functions)
  87. (define type-matrix
  88. (map (lambda (x)
  89. (let ((t (map (lambda (f) (f x)) disjoint-type-functions)))
  90. (write t)
  91. (write x)
  92. (newline)
  93. t))
  94. type-examples))
  95. (SECTION 4 1 2)
  96. (test '(quote a) 'quote (quote 'a))
  97. (test '(quote a) 'quote ''a)
  98. (SECTION 4 1 3)
  99. (test 12 (if #f + *) 3 4)
  100. (SECTION 4 1 4)
  101. (test 8 (lambda (x) (+ x x)) 4)
  102. (define reverse-subtract
  103. (lambda (x y) (- y x)))
  104. (test 3 reverse-subtract 7 10)
  105. (define add4
  106. (let ((x 4))
  107. (lambda (y) (+ x y))))
  108. (test 10 add4 6)
  109. (test '(3 4 5 6) (lambda x x) 3 4 5 6)
  110. (test '(5 6) (lambda (x y . z) z) 3 4 5 6)
  111. (SECTION 4 1 5)
  112. (test 'yes 'if (if (> 3 2) 'yes 'no))
  113. (test 'no 'if (if (> 2 3) 'yes 'no))
  114. (test '1 'if (if (> 3 2) (- 3 2) (+ 3 2)))
  115. (SECTION 4 1 6)
  116. (define x 2)
  117. (test 3 'define (+ x 1))
  118. (set! x 4)
  119. (test 5 'set! (+ x 1))
  120. (SECTION 4 2 1)
  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 'composite 'case (case (* 2 3)
  129. ((2 3 5 7) 'prime)
  130. ((1 4 6 8 9) 'composite)))
  131. (test 'consonant 'case (case (car '(c d))
  132. ((a e i o u) 'vowel)
  133. ((w y) 'semivowel)
  134. (else 'consonant)))
  135. (test #t 'and (and (= 2 2) (> 2 1)))
  136. (test #f 'and (and (= 2 2) (< 2 1)))
  137. (test '(f g) 'and (and 1 2 'c '(f g)))
  138. (test #t 'and (and))
  139. (test #t 'or (or (= 2 2) (> 2 1)))
  140. (test #t 'or (or (= 2 2) (< 2 1)))
  141. (test #f 'or (or #f #f #f))
  142. (test #f 'or (or))
  143. (test '(b c) 'or (or (memq 'b '(a b c)) (+ 3 0)))
  144. (SECTION 4 2 2)
  145. (test 6 'let (let ((x 2) (y 3)) (* x y)))
  146. (test 35 'let (let ((x 2) (y 3)) (let ((x 7) (z (+ x y))) (* z x))))
  147. (test 70 'let* (let ((x 2) (y 3)) (let* ((x 7) (z (+ x y))) (* z x))))
  148. (test #t 'letrec (letrec ((even?
  149. (lambda (n) (if (zero? n) #t (odd? (- n 1)))))
  150. (odd?
  151. (lambda (n) (if (zero? n) #f (even? (- n 1))))))
  152. (even? 88)))
  153. (define x 34)
  154. (test 5 'let (let ((x 3)) (define x 5) x))
  155. (test 34 'let x)
  156. (test 6 'let (let () (define x 6) x))
  157. (test 34 'let x)
  158. (test 7 'let* (let* ((x 3)) (define x 7) x))
  159. (test 34 'let* x)
  160. (test 8 'let* (let* () (define x 8) x))
  161. (test 34 'let* x)
  162. (test 9 'letrec (letrec () (define x 9) x))
  163. (test 34 'letrec x)
  164. (test 10 'letrec (letrec ((x 3)) (define x 10) x))
  165. (test 34 'letrec x)
  166. (SECTION 4 2 3)
  167. (define x 0)
  168. (test 6 'begin (begin (set! x 5) (+ x 1)))
  169. (SECTION 4 2 4)
  170. (test '#(0 1 2 3 4) 'do (do ((vec (make-vector 5))
  171. (i 0 (+ i 1)))
  172. ((= i 5) vec)
  173. (vector-set! vec i i)))
  174. (test 25 'do (let ((x '(1 3 5 7 9)))
  175. (do ((x x (cdr x))
  176. (sum 0 (+ sum (car x))))
  177. ((null? x) sum))))
  178. (test 1 'let (let foo () 1))
  179. (test '((6 1 3) (-5 -2)) 'let
  180. (let loop ((numbers '(3 -2 1 6 -5))
  181. (nonneg '())
  182. (neg '()))
  183. (cond ((null? numbers) (list nonneg neg))
  184. ((negative? (car numbers))
  185. (loop (cdr numbers)
  186. nonneg
  187. (cons (car numbers) neg)))
  188. (else
  189. (loop (cdr numbers)
  190. (cons (car numbers) nonneg)
  191. neg)))))
  192. (SECTION 4 2 6)
  193. (test '(list 3 4) 'quasiquote `(list ,(+ 1 2) 4))
  194. (test '(list a (quote a)) 'quasiquote (let ((name 'a)) `(list ,name ',name)))
  195. (test '(a 3 4 5 6 b) 'quasiquote `(a ,(+ 1 2) ,@(map abs '(4 -5 6)) b))
  196. (test '((foo 7) . cons)
  197. 'quasiquote
  198. `((foo ,(- 10 3)) ,@(cdr '(c)) . ,(car '(cons))))
  199. ;;; sqt is defined here because not all implementations are required to
  200. ;;; support it.
  201. (define (sqt x)
  202. (do ((i 0 (+ i 1)))
  203. ((> (* i i) x) (- i 1))))
  204. (test '#(10 5 2 4 3 8) 'quasiquote `#(10 5 ,(sqt 4) ,@(map sqt '(16 9)) 8))
  205. (test 5 'quasiquote `,(+ 2 3))
  206. (test '(a `(b ,(+ 1 2) ,(foo 4 d) e) f)
  207. 'quasiquote `(a `(b ,(+ 1 2) ,(foo ,(+ 1 3) d) e) f))
  208. (test '(a `(b ,x ,'y d) e) 'quasiquote
  209. (let ((name1 'x) (name2 'y)) `(a `(b ,,name1 ,',name2 d) e)))
  210. (test '(list 3 4) 'quasiquote (quasiquote (list (unquote (+ 1 2)) 4)))
  211. (test '`(list ,(+ 1 2) 4) 'quasiquote '(quasiquote (list (unquote (+ 1 2)) 4)))
  212. (SECTION 5 2 1)
  213. (define add3 (lambda (x) (+ x 3)))
  214. (test 6 'define (add3 3))
  215. (define first car)
  216. (test 1 'define (first '(1 2)))
  217. (SECTION 5 2 2)
  218. (test 45 'define
  219. (let ((x 5))
  220. (define foo (lambda (y) (bar x y)))
  221. (define bar (lambda (a b) (+ (* a b) a)))
  222. (foo (+ x 3))))
  223. (define x 34)
  224. (define (foo) (define x 5) x)
  225. (test 5 foo)
  226. (test 34 'define x)
  227. (define foo (lambda () (define x 5) x))
  228. (test 5 foo)
  229. (test 34 'define x)
  230. (define (foo x) ((lambda () (define x 5) x)) x)
  231. (test 88 foo 88)
  232. (test 4 foo 4)
  233. (test 34 'define x)
  234. (SECTION 6 1)
  235. (test #f not #t)
  236. (test #f not 3)
  237. (test #f not (list 3))
  238. (test #t not #f)
  239. (test #f not '())
  240. (test #f not (list))
  241. (test #f not 'nil)
  242. (test #t boolean? #f)
  243. (test #f boolean? 0)
  244. (test #f boolean? '())
  245. (SECTION 6 2)
  246. (test #t eqv? 'a 'a)
  247. (test #f eqv? 'a 'b)
  248. (test #t eqv? 2 2)
  249. (test #t eqv? '() '())
  250. (test #t eqv? '10000 '10000)
  251. (test #f eqv? (cons 1 2)(cons 1 2))
  252. (test #f eqv? (lambda () 1) (lambda () 2))
  253. (test #f eqv? #f 'nil)
  254. (let ((p (lambda (x) x)))
  255. (test #t eqv? p p))
  256. (define gen-counter
  257. (lambda ()
  258. (let ((n 0))
  259. (lambda () (set! n (+ n 1)) n))))
  260. (let ((g (gen-counter))) (test #t eqv? g g))
  261. (test #f eqv? (gen-counter) (gen-counter))
  262. (letrec ((f (lambda () (if (eqv? f g) 'f 'both)))
  263. (g (lambda () (if (eqv? f g) 'g 'both))))
  264. (test #f eqv? f g))
  265. (test #t eq? 'a 'a)
  266. (test #f eq? (list 'a) (list 'a))
  267. (test #t eq? '() '())
  268. (test #t eq? car car)
  269. (let ((x '(a))) (test #t eq? x x))
  270. (let ((x '#())) (test #t eq? x x))
  271. (let ((x (lambda (x) x))) (test #t eq? x x))
  272. (test #t equal? 'a 'a)
  273. (test #t equal? '(a) '(a))
  274. (test #t equal? '(a (b) c) '(a (b) c))
  275. (test #t equal? "abc" "abc")
  276. (test #t equal? 2 2)
  277. (test #t equal? (make-vector 5 'a) (make-vector 5 'a))
  278. (SECTION 6 3)
  279. (test '(a b c d e) 'dot '(a . (b . (c . (d . (e . ()))))))
  280. (define x (list 'a 'b 'c))
  281. (define y x)
  282. (and list? (test #t list? y))
  283. (set-cdr! x 4)
  284. (test '(a . 4) 'set-cdr! x)
  285. (test #t eqv? x y)
  286. (test '(a b c . d) 'dot '(a . (b . (c . d))))
  287. (and list? (test #f list? y))
  288. (and list? (let ((x (list 'a))) (set-cdr! x x) (test #f 'list? (list? x))))
  289. (test #t pair? '(a . b))
  290. (test #t pair? '(a . 1))
  291. (test #t pair? '(a b c))
  292. (test #f pair? '())
  293. (test #f pair? '#(a b))
  294. (test '(a) cons 'a '())
  295. (test '((a) b c d) cons '(a) '(b c d))
  296. (test '("a" b c) cons "a" '(b c))
  297. (test '(a . 3) cons 'a 3)
  298. (test '((a b) . c) cons '(a b) 'c)
  299. (test 'a car '(a b c))
  300. (test '(a) car '((a) b c d))
  301. (test 1 car '(1 . 2))
  302. (test '(b c d) cdr '((a) b c d))
  303. (test 2 cdr '(1 . 2))
  304. (test '(a 7 c) list 'a (+ 3 4) 'c)
  305. (test '() list)
  306. (test 3 length '(a b c))
  307. (test 3 length '(a (b) (c d e)))
  308. (test 0 length '())
  309. (test '(x y) append '(x) '(y))
  310. (test '(a b c d) append '(a) '(b c d))
  311. (test '(a (b) (c)) append '(a (b)) '((c)))
  312. (test '() append)
  313. (test '(a b c . d) append '(a b) '(c . d))
  314. (test 'a append '() 'a)
  315. (test '(c b a) reverse '(a b c))
  316. (test '((e (f)) d (b c) a) reverse '(a (b c) d (e (f))))
  317. (test 'c list-ref '(a b c d) 2)
  318. (test '(a b c) memq 'a '(a b c))
  319. (test '(b c) memq 'b '(a b c))
  320. (test '#f memq 'a '(b c d))
  321. (test '#f memq (list 'a) '(b (a) c))
  322. (test '((a) c) member (list 'a) '(b (a) c))
  323. (test '(101 102) memv 101 '(100 101 102))
  324. (define e '((a 1) (b 2) (c 3)))
  325. (test '(a 1) assq 'a e)
  326. (test '(b 2) assq 'b e)
  327. (test #f assq 'd e)
  328. (test #f assq (list 'a) '(((a)) ((b)) ((c))))
  329. (test '((a)) assoc (list 'a) '(((a)) ((b)) ((c))))
  330. (test '(5 7) assv 5 '((2 3) (5 7) (11 13)))
  331. (SECTION 6 4)
  332. (test #t symbol? 'foo)
  333. (test #t symbol? (car '(a b)))
  334. (test #f symbol? "bar")
  335. (test #t symbol? 'nil)
  336. (test #f symbol? '())
  337. (test #f symbol? #f)
  338. ;;; But first, what case are symbols in? Determine the standard case:
  339. (define char-standard-case char-upcase)
  340. (if (string=? (symbol->string 'A) "a")
  341. (set! char-standard-case char-downcase))
  342. ;;; Not for Guile
  343. ;(test #t 'standard-case
  344. ; (string=? (symbol->string 'a) (symbol->string 'A)))
  345. ;(test #t 'standard-case
  346. ; (or (string=? (symbol->string 'a) "A")
  347. ; (string=? (symbol->string 'A) "a")))
  348. (define (str-copy s)
  349. (let ((v (make-string (string-length s))))
  350. (do ((i (- (string-length v) 1) (- i 1)))
  351. ((< i 0) v)
  352. (string-set! v i (string-ref s i)))))
  353. (define (string-standard-case s)
  354. (set! s (str-copy s))
  355. (do ((i 0 (+ 1 i))
  356. (sl (string-length s)))
  357. ((>= i sl) s)
  358. (string-set! s i (char-standard-case (string-ref s i)))))
  359. ;;; Not for Guile
  360. ;(test (string-standard-case "flying-fish") symbol->string 'flying-fish)
  361. ;(test (string-standard-case "martin") symbol->string 'Martin)
  362. (test "Malvina" symbol->string (string->symbol "Malvina"))
  363. ;;; Not for Guile
  364. ;(test #t 'standard-case (eq? 'a 'A))
  365. (define x (string #\a #\b))
  366. (define y (string->symbol x))
  367. (string-set! x 0 #\c)
  368. (test "cb" 'string-set! x)
  369. (test "ab" symbol->string y)
  370. (test y string->symbol "ab")
  371. ;;; Not for Guile
  372. ;(test #t eq? 'mISSISSIppi 'mississippi)
  373. ;(test #f 'string->symbol (eq? 'bitBlt (string->symbol "bitBlt")))
  374. (test 'HeyJeffrey string->symbol (symbol->string 'HeyJeffrey))
  375. (SECTION 6 5 5)
  376. (test #t number? 3)
  377. (test #t complex? 3)
  378. (test #t real? 3)
  379. (test #t rational? 3)
  380. (test #t integer? 3)
  381. (test #t exact? 3)
  382. (test #f inexact? 3)
  383. (test #t = 22 22 22)
  384. (test #t = 22 22)
  385. (test #f = 34 34 35)
  386. (test #f = 34 35)
  387. (test #t > 3 -6246)
  388. (test #f > 9 9 -2424)
  389. (test #t >= 3 -4 -6246)
  390. (test #t >= 9 9)
  391. (test #f >= 8 9)
  392. (test #t < -1 2 3 4 5 6 7 8)
  393. (test #f < -1 2 3 4 4 5 6 7)
  394. (test #t <= -1 2 3 4 5 6 7 8)
  395. (test #t <= -1 2 3 4 4 5 6 7)
  396. (test #f < 1 3 2)
  397. (test #f >= 1 3 2)
  398. (test #t zero? 0)
  399. (test #f zero? 1)
  400. (test #f zero? -1)
  401. (test #f zero? -100)
  402. (test #t positive? 4)
  403. (test #f positive? -4)
  404. (test #f positive? 0)
  405. (test #f negative? 4)
  406. (test #t negative? -4)
  407. (test #f negative? 0)
  408. (test #t odd? 3)
  409. (test #f odd? 2)
  410. (test #f odd? -4)
  411. (test #t odd? -1)
  412. (test #f even? 3)
  413. (test #t even? 2)
  414. (test #t even? -4)
  415. (test #f even? -1)
  416. (test 38 max 34 5 7 38 6)
  417. (test -24 min 3 5 5 330 4 -24)
  418. (test 7 + 3 4)
  419. (test '3 + 3)
  420. (test 0 +)
  421. (test 4 * 4)
  422. (test 1 *)
  423. (test -1 - 3 4)
  424. (test -3 - 3)
  425. (test 7 abs -7)
  426. (test 7 abs 7)
  427. (test 0 abs 0)
  428. (test 5 quotient 35 7)
  429. (test -5 quotient -35 7)
  430. (test -5 quotient 35 -7)
  431. (test 5 quotient -35 -7)
  432. (test 1 modulo 13 4)
  433. (test 1 remainder 13 4)
  434. (test 3 modulo -13 4)
  435. (test -1 remainder -13 4)
  436. (test -3 modulo 13 -4)
  437. (test 1 remainder 13 -4)
  438. (test -1 modulo -13 -4)
  439. (test -1 remainder -13 -4)
  440. (define (divtest n1 n2)
  441. (= n1 (+ (* n2 (quotient n1 n2))
  442. (remainder n1 n2))))
  443. (test #t divtest 238 9)
  444. (test #t divtest -238 9)
  445. (test #t divtest 238 -9)
  446. (test #t divtest -238 -9)
  447. (test 4 gcd 0 4)
  448. (test 4 gcd -4 0)
  449. (test 4 gcd 32 -36)
  450. (test 0 gcd)
  451. (test 288 lcm 32 -36)
  452. (test 1 lcm)
  453. ;;;;From: fred@sce.carleton.ca (Fred J Kaudel)
  454. ;;; Modified by jaffer.
  455. (define (test-inexact)
  456. (define f3.9 (string->number "3.9"))
  457. (define f4.0 (string->number "4.0"))
  458. (define f-3.25 (string->number "-3.25"))
  459. (define f.25 (string->number ".25"))
  460. (define f4.5 (string->number "4.5"))
  461. (define f3.5 (string->number "3.5"))
  462. (define f0.0 (string->number "0.0"))
  463. (define f0.8 (string->number "0.8"))
  464. (define f1.0 (string->number "1.0"))
  465. (define wto write-test-obj)
  466. (define dto display-test-obj)
  467. (define lto load-test-obj)
  468. (newline)
  469. (display ";testing inexact numbers; ")
  470. (newline)
  471. (SECTION 6 5 5)
  472. (test #t inexact? f3.9)
  473. (test #t 'inexact? (inexact? (max f3.9 4)))
  474. (test f4.0 'max (max f3.9 4))
  475. (test f4.0 'exact->inexact (exact->inexact 4))
  476. (test (- f4.0) round (- f4.5))
  477. (test (- f4.0) round (- f3.5))
  478. (test (- f4.0) round (- f3.9))
  479. (test f0.0 round f0.0)
  480. (test f0.0 round f.25)
  481. (test f1.0 round f0.8)
  482. (test f4.0 round f3.5)
  483. (test f4.0 round f4.5)
  484. (set! write-test-obj (list f.25 f-3.25));.25 inexact errors less likely.
  485. (set! display-test-obj (list f.25 f-3.25));.3 often has such errors (~10^-13)
  486. (set! load-test-obj (list 'define 'foo (list 'quote write-test-obj)))
  487. (test #t call-with-output-file
  488. "tmp3"
  489. (lambda (test-file)
  490. (write-char #\; test-file)
  491. (display write-test-obj test-file)
  492. (newline test-file)
  493. (write load-test-obj test-file)
  494. (output-port? test-file)))
  495. (check-test-file "tmp3")
  496. (set! write-test-obj wto)
  497. (set! display-test-obj dto)
  498. (set! load-test-obj lto)
  499. (let ((x (string->number "4195835.0"))
  500. (y (string->number "3145727.0")))
  501. (test #t 'pentium-fdiv-bug (> f1.0 (- x (* (/ x y) y)))))
  502. (report-errs))
  503. (define (test-bignum)
  504. (define tb
  505. (lambda (n1 n2)
  506. (= n1 (+ (* n2 (quotient n1 n2))
  507. (remainder n1 n2)))))
  508. (newline)
  509. (display ";testing bignums; ")
  510. (newline)
  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. (newline)
  813. (display ";testing continuations; ")
  814. (newline)
  815. (SECTION 6 9)
  816. (test #t leaf-eq? '(a (b (c))) '((a) b c))
  817. (test #f leaf-eq? '(a (b (c))) '((a) b c d))
  818. (report-errs))
  819. ;;; Test Optional R4RS DELAY syntax and FORCE procedure
  820. (define (test-delay)
  821. (newline)
  822. (display ";testing DELAY and FORCE; ")
  823. (newline)
  824. (SECTION 6 9)
  825. (test 3 'delay (force (delay (+ 1 2))))
  826. (test '(3 3) 'delay (let ((p (delay (+ 1 2))))
  827. (list (force p) (force p))))
  828. (test 2 'delay (letrec ((a-stream
  829. (letrec ((next (lambda (n)
  830. (cons n (delay (next (+ n 1)))))))
  831. (next 0)))
  832. (head car)
  833. (tail (lambda (stream) (force (cdr stream)))))
  834. (head (tail (tail a-stream)))))
  835. (letrec ((count 0)
  836. (p (delay (begin (set! count (+ count 1))
  837. (if (> count x)
  838. count
  839. (force p)))))
  840. (x 5))
  841. (test 6 force p)
  842. (set! x 10)
  843. (test 6 force p))
  844. (test 3 'force
  845. (letrec ((p (delay (if c 3 (begin (set! c #t) (+ (force p) 1)))))
  846. (c #f))
  847. (force p)))
  848. (report-errs))
  849. (SECTION 6 10 1)
  850. (test #t input-port? (current-input-port))
  851. (test #t output-port? (current-output-port))
  852. (test #t call-with-input-file "test.scm" input-port?)
  853. (define this-file (open-input-file "test.scm"))
  854. (test #t input-port? this-file)
  855. (SECTION 6 10 2)
  856. (test #\; peek-char this-file)
  857. (test #\; read-char this-file)
  858. (test '(define cur-section '()) read this-file)
  859. (test #\( peek-char this-file)
  860. (test '(define errs '()) read this-file)
  861. (close-input-port this-file)
  862. (close-input-port this-file)
  863. (define (check-test-file name)
  864. (define test-file (open-input-file name))
  865. (test #t 'input-port?
  866. (call-with-input-file
  867. name
  868. (lambda (test-file)
  869. (test load-test-obj read test-file)
  870. (test #t eof-object? (peek-char test-file))
  871. (test #t eof-object? (read-char test-file))
  872. (input-port? test-file))))
  873. (test #\; read-char test-file)
  874. (test display-test-obj read test-file)
  875. (test load-test-obj read test-file)
  876. (close-input-port test-file))
  877. (SECTION 6 10 3)
  878. (define write-test-obj
  879. '(#t #f #\a () 9739 -3 . #((test) "te \" \" st" "" test #() b c)))
  880. (define display-test-obj
  881. '(#t #f a () 9739 -3 . #((test) te " " st test #() b c)))
  882. (define load-test-obj
  883. (list 'define 'foo (list 'quote write-test-obj)))
  884. (test #t call-with-output-file
  885. "tmp1"
  886. (lambda (test-file)
  887. (write-char #\; test-file)
  888. (display write-test-obj test-file)
  889. (newline test-file)
  890. (write load-test-obj test-file)
  891. (output-port? test-file)))
  892. (check-test-file "tmp1")
  893. (define test-file (open-output-file "tmp2"))
  894. (write-char #\; test-file)
  895. (display write-test-obj test-file)
  896. (newline test-file)
  897. (write load-test-obj test-file)
  898. (test #t output-port? test-file)
  899. (close-output-port test-file)
  900. (check-test-file "tmp2")
  901. (define (test-sc4)
  902. (newline)
  903. (display ";testing scheme 4 functions; ")
  904. (newline)
  905. (SECTION 6 7)
  906. (test '(#\P #\space #\l) string->list "P l")
  907. (test '() string->list "")
  908. (test "1\\\"" list->string '(#\1 #\\ #\"))
  909. (test "" list->string '())
  910. (SECTION 6 8)
  911. (test '(dah dah didah) vector->list '#(dah dah didah))
  912. (test '() vector->list '#())
  913. (test '#(dididit dah) list->vector '(dididit dah))
  914. (test '#() list->vector '())
  915. (SECTION 6 10 4)
  916. (load "tmp1")
  917. (test write-test-obj 'load foo)
  918. (report-errs))
  919. (report-errs)
  920. (if (and (string->number "0.0") (inexact? (string->number "0.0")))
  921. (test-inexact))
  922. (let ((n (string->number "281474976710655")))
  923. (if (and n (exact? n))
  924. (test-bignum)))
  925. (newline)
  926. (test-cont)
  927. (newline)
  928. (test-sc4)
  929. (newline)
  930. (test-delay)
  931. (newline)
  932. "last item in file"