r7rs.test 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576
  1. ;;; R7RS compatibility libraries -*- scheme -*-
  2. ;;; Copyright (C) 2019 Free Software Foundation, Inc.
  3. ;;;
  4. ;;; This library is free software: you can redistribute it and/or modify
  5. ;;; it under the terms of the GNU Lesser General Public License as
  6. ;;; published by the Free Software Foundation, either version 3 of the
  7. ;;; License, or (at your option) any later version.
  8. ;;;
  9. ;;; This library is distributed in the hope that it will be useful, but
  10. ;;; 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 program. If not, see
  16. ;;; <http://www.gnu.org/licenses/>.
  17. ;; Adapted from Chibi Scheme, which carries this in COPYING:
  18. ;; Copyright (c) 2009-2018 Alex Shinn
  19. ;; All rights reserved.
  20. ;;
  21. ;; Redistribution and use in source and binary forms, with or without
  22. ;; modification, are permitted provided that the following conditions
  23. ;; are met:
  24. ;; 1. Redistributions of source code must retain the above copyright
  25. ;; notice, this list of conditions and the following disclaimer.
  26. ;; 2. Redistributions in binary form must reproduce the above copyright
  27. ;; notice, this list of conditions and the following disclaimer in the
  28. ;; documentation and/or other materials provided with the distribution.
  29. ;; 3. The name of the author may not be used to endorse or promote products
  30. ;; derived from this software without specific prior written permission.
  31. ;;
  32. ;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  33. ;; IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34. ;; OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  35. ;; IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  36. ;; INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. ;; NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  38. ;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  39. ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  40. ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  41. ;; THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. (define-module (test-suite r7rs)
  43. #:pure
  44. #:use-module ((guile) #:select (install-r7rs!
  45. define-syntax-rule quote read-disable
  46. import))
  47. #:use-module (test-suite lib))
  48. ;; R7RS test suite. Covers all procedures and syntax in the small
  49. ;; language except `delete-file'. Currently assumes full-unicode
  50. ;; support, the full numeric tower and all standard libraries
  51. ;; provided.
  52. (install-r7rs!)
  53. (define-syntax-rule (undo-install-r7rs!)
  54. (begin
  55. (read-disable 'r7rs-symbols)
  56. (read-disable 'r6rs-hex-escapes)
  57. (read-disable 'hungry-eol-escapes)))
  58. (import (scheme base) (scheme char) (scheme lazy)
  59. (scheme inexact) (scheme complex) (scheme time)
  60. (scheme file) (scheme read) (scheme write)
  61. (scheme eval) (scheme process-context) (scheme case-lambda)
  62. (only (scheme r5rs) null-environment interaction-environment))
  63. ;;; Guile shims for Chibi R7RS test suite library.
  64. (define-syntax-rule (test-begin . _) #f)
  65. (define-syntax-rule (test-end . _) #f)
  66. (define (%test-equal? expr expected)
  67. (if (and (number? expr) (number? expected)
  68. (inexact? expr) (inexact? expected))
  69. (if (and (real? expr) (real? expected))
  70. (<= (- expected 1.0e-5) expr (+ expected 1.0e-5))
  71. (and (%test-equal? (real-part expr) (real-part expected))
  72. (%test-equal? (imag-part expr) (imag-part expected))))
  73. (equal? expr expected)))
  74. (define-syntax-rule (test expected expr)
  75. (pass-if (%test-equal? expr expected)))
  76. ;; This form is used for those R7RS tests that do not yet pass in Guile.
  77. (define-syntax-rule (failing-test url expected expr)
  78. (expect-fail url (%test-equal? expr expected)))
  79. (define-syntax-rule (failing-test-with-exception url expected expr)
  80. (expect-fail url (guard (exn (else #f))
  81. (%test-equal? expr expected))))
  82. (define-syntax-rule (test-values expected expr)
  83. (pass-if-equal (call-with-values (lambda () expected) list)
  84. (call-with-values (lambda () expr) list)))
  85. (define-syntax-rule (test-error expr)
  86. (pass-if (guard (exn (else #t))
  87. expr
  88. #f)))
  89. (define-syntax-rule (test-assert str expr)
  90. (pass-if str expr))
  91. ;;; Chibi R7RS tests continue here.
  92. (test-begin "R7RS")
  93. (test-begin "4.1 Primitive expression types")
  94. (let ()
  95. (define x 28)
  96. (test 28 x))
  97. (test 'a (quote a))
  98. ;; (test #(a b c) (quote #(a b c)))
  99. (test '(+ 1 2) (quote (+ 1 2)))
  100. (test 'a 'a)
  101. ;; (test #(a b c) '#(a b c))
  102. (test '() '())
  103. (test '(+ 1 2) '(+ 1 2))
  104. (test '(quote a) '(quote a))
  105. (test '(quote a) ''a)
  106. (test "abc" '"abc")
  107. (test "abc" "abc")
  108. (test 145932 '145932)
  109. (test 145932 145932)
  110. (test #t '#t)
  111. (test #t #t)
  112. (test 7 (+ 3 4))
  113. (test 12 ((if #f + *) 3 4))
  114. (test 8 ((lambda (x) (+ x x)) 4))
  115. (define reverse-subtract
  116. (lambda (x y) (- y x)))
  117. (test 3 (reverse-subtract 7 10))
  118. (define add4
  119. (let ((x 4))
  120. (lambda (y) (+ x y))))
  121. (test 10 (add4 6))
  122. (test '(3 4 5 6) ((lambda x x) 3 4 5 6))
  123. (test '(5 6) ((lambda (x y . z) z)
  124. 3 4 5 6))
  125. (test 'yes (if (> 3 2) 'yes 'no))
  126. (test 'no (if (> 2 3) 'yes 'no))
  127. (test 1 (if (> 3 2)
  128. (- 3 2)
  129. (+ 3 2)))
  130. (let ()
  131. (define x 2)
  132. (test 3 (+ x 1)))
  133. (test-end)
  134. (test-begin "4.2 Derived expression types")
  135. (test 'greater
  136. (cond ((> 3 2) 'greater)
  137. ((< 3 2) 'less)))
  138. (test 'equal
  139. (cond ((> 3 3) 'greater)
  140. ((< 3 3) 'less)
  141. (else 'equal)))
  142. (test 2
  143. (cond ((assv 'b '((a 1) (b 2))) => cadr)
  144. (else #f)))
  145. (test 'composite
  146. (case (* 2 3)
  147. ((2 3 5 7) 'prime)
  148. ((1 4 6 8 9) 'composite)))
  149. (test 'c
  150. (case (car '(c d))
  151. ((a e i o u) 'vowel)
  152. ((w y) 'semivowel)
  153. (else => (lambda (x) x))))
  154. (test '((other . z) (semivowel . y) (other . x)
  155. (semivowel . w) (vowel . u))
  156. (map (lambda (x)
  157. (case x
  158. ((a e i o u) => (lambda (w) (cons 'vowel w)))
  159. ((w y) (cons 'semivowel x))
  160. (else => (lambda (w) (cons 'other w)))))
  161. '(z y x w u)))
  162. (test #t (and (= 2 2) (> 2 1)))
  163. (test #f (and (= 2 2) (< 2 1)))
  164. (test '(f g) (and 1 2 'c '(f g)))
  165. (test #t (and))
  166. (test #t (or (= 2 2) (> 2 1)))
  167. (test #t (or (= 2 2) (< 2 1)))
  168. (test #f (or #f #f #f))
  169. (test '(b c) (or (memq 'b '(a b c))
  170. (/ 3 0)))
  171. (test 6 (let ((x 2) (y 3))
  172. (* x y)))
  173. (test 35 (let ((x 2) (y 3))
  174. (let ((x 7)
  175. (z (+ x y)))
  176. (* z x))))
  177. (test 70 (let ((x 2) (y 3))
  178. (let* ((x 7)
  179. (z (+ x y)))
  180. (* z x))))
  181. (test #t
  182. (letrec ((even?
  183. (lambda (n)
  184. (if (zero? n)
  185. #t
  186. (odd? (- n 1)))))
  187. (odd?
  188. (lambda (n)
  189. (if (zero? n)
  190. #f
  191. (even? (- n 1))))))
  192. (even? 88)))
  193. (test 5
  194. (letrec* ((p
  195. (lambda (x)
  196. (+ 1 (q (- x 1)))))
  197. (q
  198. (lambda (y)
  199. (if (zero? y)
  200. 0
  201. (+ 1 (p (- y 1))))))
  202. (x (p 5))
  203. (y x))
  204. y))
  205. ;; By Jussi Piitulainen <jpiitula@ling.helsinki.fi>
  206. ;; and John Cowan <cowan@mercury.ccil.org>:
  207. ;; http://lists.scheme-reports.org/pipermail/scheme-reports/2013-December/003876.html
  208. (define (means ton)
  209. (letrec*
  210. ((mean
  211. (lambda (f g)
  212. (f (/ (sum g ton) n))))
  213. (sum
  214. (lambda (g ton)
  215. (if (null? ton)
  216. (+)
  217. (if (number? ton)
  218. (g ton)
  219. (+ (sum g (car ton))
  220. (sum g (cdr ton)))))))
  221. (n (sum (lambda (x) 1) ton)))
  222. (values (mean values values)
  223. (mean exp log)
  224. (mean / /))))
  225. (let*-values (((a b c) (means '(8 5 99 1 22))))
  226. (test 27 a)
  227. (test 9.728 b)
  228. (test 1800/497 c))
  229. (let*-values (((root rem) (exact-integer-sqrt 32)))
  230. (test 35 (* root rem)))
  231. (test '(1073741824 0)
  232. (let*-values (((root rem) (exact-integer-sqrt (expt 2 60))))
  233. (list root rem)))
  234. (test '(1518500249 3000631951)
  235. (let*-values (((root rem) (exact-integer-sqrt (expt 2 61))))
  236. (list root rem)))
  237. (test '(815238614083298888 443242361398135744)
  238. (let*-values (((root rem) (exact-integer-sqrt (expt 2 119))))
  239. (list root rem)))
  240. (test '(1152921504606846976 0)
  241. (let*-values (((root rem) (exact-integer-sqrt (expt 2 120))))
  242. (list root rem)))
  243. (test '(1630477228166597776 1772969445592542976)
  244. (let*-values (((root rem) (exact-integer-sqrt (expt 2 121))))
  245. (list root rem)))
  246. (test '(31622776601683793319 62545769258890964239)
  247. (let*-values (((root rem) (exact-integer-sqrt (expt 10 39))))
  248. (list root rem)))
  249. (let*-values (((root rem) (exact-integer-sqrt (expt 2 140))))
  250. (test 0 rem)
  251. (test (expt 2 140) (square root)))
  252. (test '(x y x y) (let ((a 'a) (b 'b) (x 'x) (y 'y))
  253. (let*-values (((a b) (values x y))
  254. ((x y) (values a b)))
  255. (list a b x y))))
  256. (test 'ok (let-values () 'ok))
  257. (test 1 (let ((x 1))
  258. (let*-values ()
  259. (define x 2)
  260. #f)
  261. x))
  262. (let ()
  263. (define x 0)
  264. (set! x 5)
  265. (test 6 (+ x 1)))
  266. (test '#(0 1 2 3 4) (do ((vec (make-vector 5))
  267. (i 0 (+ i 1)))
  268. ((= i 5) vec)
  269. (vector-set! vec i i)))
  270. (test 25 (let ((x '(1 3 5 7 9)))
  271. (do ((x x (cdr x))
  272. (sum 0 (+ sum (car x))))
  273. ((null? x) sum))))
  274. (test '((6 1 3) (-5 -2))
  275. (let loop ((numbers '(3 -2 1 6 -5))
  276. (nonneg '())
  277. (neg '()))
  278. (cond ((null? numbers) (list nonneg neg))
  279. ((>= (car numbers) 0)
  280. (loop (cdr numbers)
  281. (cons (car numbers) nonneg)
  282. neg))
  283. ((< (car numbers) 0)
  284. (loop (cdr numbers)
  285. nonneg
  286. (cons (car numbers) neg))))))
  287. (test 3 (force (delay (+ 1 2))))
  288. (test '(3 3)
  289. (let ((p (delay (+ 1 2))))
  290. (list (force p) (force p))))
  291. (define integers
  292. (letrec ((next
  293. (lambda (n)
  294. (delay (cons n (next (+ n 1)))))))
  295. (next 0)))
  296. (define head
  297. (lambda (stream) (car (force stream))))
  298. (define tail
  299. (lambda (stream) (cdr (force stream))))
  300. (test 2 (head (tail (tail integers))))
  301. (define (stream-filter p? s)
  302. (delay-force
  303. (if (null? (force s))
  304. (delay '())
  305. (let ((h (car (force s)))
  306. (t (cdr (force s))))
  307. (if (p? h)
  308. (delay (cons h (stream-filter p? t)))
  309. (stream-filter p? t))))))
  310. (test 5 (head (tail (tail (stream-filter odd? integers)))))
  311. (let ()
  312. (define x 5)
  313. (define count 0)
  314. (define p
  315. (delay (begin (set! count (+ count 1))
  316. (if (> count x)
  317. count
  318. (force p)))))
  319. (test 6 (force p))
  320. (test 6 (begin (set! x 10) (force p))))
  321. (test #t (promise? (delay (+ 2 2))))
  322. (test #t (promise? (make-promise (+ 2 2))))
  323. (test #t
  324. (let ((x (delay (+ 2 2))))
  325. (force x)
  326. (promise? x)))
  327. (test #t
  328. (let ((x (make-promise (+ 2 2))))
  329. (force x)
  330. (promise? x)))
  331. (define radix
  332. (make-parameter
  333. 10
  334. (lambda (x)
  335. (if (and (integer? x) (<= 2 x 16))
  336. x
  337. (error "invalid radix")))))
  338. (define (f n) (number->string n (radix)))
  339. (test "12" (f 12))
  340. (test "1100" (parameterize ((radix 2))
  341. (f 12)))
  342. (test "12" (f 12))
  343. (test '(list 3 4) `(list ,(+ 1 2) 4))
  344. (let ((name 'a)) (test '(list a (quote a)) `(list ,name ',name)))
  345. (test '(a 3 4 5 6 b) `(a ,(+ 1 2) ,@(map abs '(4 -5 6)) b))
  346. (test '#(10 5 4 16 9 8)
  347. `#(10 5 ,(square 2) ,@(map square '(4 3)) 8))
  348. (test '(a `(b ,(+ 1 2) ,(foo 4 d) e) f)
  349. `(a `(b ,(+ 1 2) ,(foo ,(+ 1 3) d) e) f) )
  350. (let ((name1 'x)
  351. (name2 'y))
  352. (test '(a `(b ,x ,'y d) e) `(a `(b ,,name1 ,',name2 d) e)))
  353. (test '(list 3 4) (quasiquote (list (unquote (+ 1 2)) 4)) )
  354. (test `(list ,(+ 1 2) 4) (quasiquote (list (unquote (+ 1 2)) 4)))
  355. (define plus
  356. (case-lambda
  357. (() 0)
  358. ((x) x)
  359. ((x y) (+ x y))
  360. ((x y z) (+ (+ x y) z))
  361. (args (apply + args))))
  362. (test 0 (plus))
  363. (test 1 (plus 1))
  364. (test 3 (plus 1 2))
  365. (test 6 (plus 1 2 3))
  366. (test 10 (plus 1 2 3 4))
  367. (define mult
  368. (case-lambda
  369. (() 1)
  370. ((x) x)
  371. ((x y) (* x y))
  372. ((x y . z) (apply mult (* x y) z))))
  373. (test 1 (mult))
  374. (test 1 (mult 1))
  375. (test 2 (mult 1 2))
  376. (test 6 (mult 1 2 3))
  377. (test 24 (mult 1 2 3 4))
  378. (test-end)
  379. (test-begin "4.3 Macros")
  380. (test 'now (let-syntax
  381. ((when (syntax-rules ()
  382. ((when test stmt1 stmt2 ...)
  383. (if test
  384. (begin stmt1
  385. stmt2 ...))))))
  386. (let ((if #t))
  387. (when if (set! if 'now))
  388. if)))
  389. (test 'outer (let ((x 'outer))
  390. (let-syntax ((m (syntax-rules () ((m) x))))
  391. (let ((x 'inner))
  392. (m)))))
  393. (test 7 (letrec-syntax
  394. ((my-or (syntax-rules ()
  395. ((my-or) #f)
  396. ((my-or e) e)
  397. ((my-or e1 e2 ...)
  398. (let ((temp e1))
  399. (if temp
  400. temp
  401. (my-or e2 ...)))))))
  402. (let ((x #f)
  403. (y 7)
  404. (temp 8)
  405. (let odd?)
  406. (if even?))
  407. (my-or x
  408. (let temp)
  409. (if y)
  410. y))))
  411. (define-syntax be-like-begin1
  412. (syntax-rules ()
  413. ((be-like-begin1 name)
  414. (define-syntax name
  415. (syntax-rules ()
  416. ((name expr (... ...))
  417. (begin expr (... ...))))))))
  418. (be-like-begin1 sequence1)
  419. (test 3 (sequence1 0 1 2 3))
  420. (define-syntax be-like-begin2
  421. (syntax-rules ()
  422. ((be-like-begin2 name)
  423. (define-syntax name
  424. (... (syntax-rules ()
  425. ((name expr ...)
  426. (begin expr ...))))))))
  427. (be-like-begin2 sequence2)
  428. (test 4 (sequence2 1 2 3 4))
  429. (define-syntax be-like-begin3
  430. (syntax-rules ()
  431. ((be-like-begin3 name)
  432. (define-syntax name
  433. (syntax-rules dots ()
  434. ((name expr dots)
  435. (begin expr dots)))))))
  436. (be-like-begin3 sequence3)
  437. (test 5 (sequence3 2 3 4 5))
  438. ;; ellipsis escape
  439. (define-syntax elli-esc-1
  440. (syntax-rules ()
  441. ((_)
  442. '(... ...))
  443. ((_ x)
  444. '(... (x ...)))
  445. ((_ x y)
  446. '(... (... x y)))))
  447. (test '... (elli-esc-1))
  448. (test '(100 ...) (elli-esc-1 100))
  449. (test '(... 100 200) (elli-esc-1 100 200))
  450. ;; Syntax pattern with ellipsis in middle of proper list.
  451. (define-syntax part-2
  452. (syntax-rules ()
  453. ((_ a b (m n) ... x y)
  454. (vector (list a b) (list m ...) (list n ...) (list x y)))
  455. ((_ . rest) 'error)))
  456. (test '#((10 43) (31 41 51) (32 42 52) (63 77))
  457. (part-2 10 (+ 21 22) (31 32) (41 42) (51 52) (+ 61 2) 77))
  458. ;; Syntax pattern with ellipsis in middle of improper list.
  459. (define-syntax part-2x
  460. (syntax-rules ()
  461. ((_ (a b (m n) ... x y . rest))
  462. (vector (list a b) (list m ...) (list n ...) (list x y)
  463. (cons "rest:" 'rest)))
  464. ((_ . rest) 'error)))
  465. (test '#((10 43) (31 41 51) (32 42 52) (63 77) ("rest:"))
  466. (part-2x (10 (+ 21 22) (31 32) (41 42) (51 52) (+ 61 2) 77)))
  467. (test '#((10 43) (31 41 51) (32 42 52) (63 77) ("rest:" . "tail"))
  468. (part-2x (10 (+ 21 22) (31 32) (41 42) (51 52) (+ 61 2) 77 . "tail")))
  469. ;; underscore
  470. (define-syntax underscore
  471. (syntax-rules ()
  472. ((foo _) '_)))
  473. (test '_ (underscore foo))
  474. (let ()
  475. (define-syntax underscore2
  476. (syntax-rules ()
  477. ((underscore2 (a _) ...) 42)))
  478. (test 42 (underscore2 (1 2))))
  479. ;; (define-syntax count-to-2
  480. ;; (syntax-rules ()
  481. ;; ((_) 0)
  482. ;; ((_ _) 1)
  483. ;; ((_ _ _) 2)
  484. ;; ((_ . _) 'many)))
  485. ;; (test '(2 0 many)
  486. ;; (list (count-to-2 a b) (count-to-2) (count-to-2 a b c d)))
  487. ;; (define-syntax count-to-2_
  488. ;; (syntax-rules (_)
  489. ;; ((_) 0)
  490. ;; ((_ _) 1)
  491. ;; ((_ _ _) 2)
  492. ;; ((x . y) 'fail)))
  493. ;; (test '(2 0 fail fail)
  494. ;; (list (count-to-2_ _ _) (count-to-2_)
  495. ;; (count-to-2_ a b) (count-to-2_ a b c d)))
  496. (define-syntax jabberwocky
  497. (syntax-rules ()
  498. ((_ hatter)
  499. (begin
  500. (define march-hare 42)
  501. (define-syntax hatter
  502. (syntax-rules ()
  503. ((_) march-hare)))))))
  504. (jabberwocky mad-hatter)
  505. (test 42 (mad-hatter))
  506. (test 'ok (let ((=> #f)) (cond (#t => 'ok))))
  507. (let ()
  508. (define x 1)
  509. (let-syntax ()
  510. (define x 2)
  511. #f)
  512. (test 1 x))
  513. (let ()
  514. (define-syntax foo
  515. (syntax-rules ()
  516. ((foo bar y)
  517. (define-syntax bar
  518. (syntax-rules ()
  519. ((bar x) 'y))))))
  520. (foo bar x)
  521. (test 'x (bar 1)))
  522. (begin
  523. (define-syntax ffoo
  524. (syntax-rules ()
  525. ((ffoo ff)
  526. (begin
  527. (define (ff x)
  528. (gg x))
  529. (define (gg x)
  530. (* x x))))))
  531. (ffoo ff)
  532. (test 100 (ff 10)))
  533. (let-syntax ((vector-lit
  534. (syntax-rules ()
  535. ((vector-lit)
  536. '#(b)))))
  537. (test '#(b) (vector-lit)))
  538. (let ()
  539. ;; forward hygienic refs
  540. (define-syntax foo399
  541. (syntax-rules () ((foo399) (bar399))))
  542. (define (quux399)
  543. (foo399))
  544. (define (bar399)
  545. 42)
  546. (test 42 (quux399)))
  547. (let-syntax
  548. ((m (syntax-rules ()
  549. ((m x) (let-syntax
  550. ((n (syntax-rules (k)
  551. ((n x) 'bound-identifier=?)
  552. ((n y) 'free-identifier=?))))
  553. (n z))))))
  554. (test 'bound-identifier=? (m k)))
  555. ;; literal has priority to ellipsis (R7RS 4.3.2)
  556. ;; (let ()
  557. ;; (define-syntax elli-lit-1
  558. ;; (syntax-rules ... (...)
  559. ;; ((_ x)
  560. ;; '(x ...))))
  561. ;; (test '(100 ...) (elli-lit-1 100)))
  562. ;; bad ellipsis
  563. #|
  564. (test 'error
  565. (guard (exn (else 'error))
  566. (eval
  567. '(define-syntax bad-elli-1
  568. (syntax-rules ()
  569. ((_ ... x)
  570. '(... x))))
  571. (interaction-environment))))
  572. (test 'error
  573. (guard (exn (else 'error))
  574. (eval
  575. '(define-syntax bad-elli-2
  576. (syntax-rules ()
  577. ((_ (... x))
  578. '(... x))))
  579. (interaction-environment))))
  580. |#
  581. (test-end)
  582. (test-begin "5 Program structure")
  583. (define add3
  584. (lambda (x) (+ x 3)))
  585. (test 6 (add3 3))
  586. (define first car)
  587. (test 1 (first '(1 2)))
  588. (test 45 (let ((x 5))
  589. (define foo (lambda (y) (bar x y)))
  590. (define bar (lambda (a b) (+ (* a b) a)))
  591. (foo (+ x 3))))
  592. (test 'ok
  593. (let ()
  594. (define-values () (values))
  595. 'ok))
  596. (test 1
  597. (let ()
  598. (define-values (x) (values 1))
  599. x))
  600. (test 3
  601. (let ()
  602. (define-values x (values 1 2))
  603. (apply + x)))
  604. (test 3
  605. (let ()
  606. (define-values (x y) (values 1 2))
  607. (+ x y)))
  608. (test 6
  609. (let ()
  610. (define-values (x y z) (values 1 2 3))
  611. (+ x y z)))
  612. (test 10
  613. (let ()
  614. (define-values (x y . z) (values 1 2 3 4))
  615. (+ x y (car z) (cadr z))))
  616. (test '(2 1) (let ((x 1) (y 2))
  617. (define-syntax swap!
  618. (syntax-rules ()
  619. ((swap! a b)
  620. (let ((tmp a))
  621. (set! a b)
  622. (set! b tmp)))))
  623. (swap! x y)
  624. (list x y)))
  625. ;; Records
  626. (define-record-type <pare>
  627. (kons x y)
  628. pare?
  629. (x kar set-kar!)
  630. (y kdr))
  631. (test #t (pare? (kons 1 2)))
  632. (test #f (pare? (cons 1 2)))
  633. (test 1 (kar (kons 1 2)))
  634. (test 2 (kdr (kons 1 2)))
  635. (test 3 (let ((k (kons 1 2)))
  636. (set-kar! k 3)
  637. (kar k)))
  638. (test-end)
  639. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  640. ;; 6 Standard Procedures
  641. (test-begin "6.1 Equivalence Predicates")
  642. (test #t (eqv? 'a 'a))
  643. (test #f (eqv? 'a 'b))
  644. (test #t (eqv? 2 2))
  645. (test #t (eqv? '() '()))
  646. (test #t (eqv? 100000000 100000000))
  647. (test #f (eqv? (cons 1 2) (cons 1 2)))
  648. (test #f (eqv? (lambda () 1)
  649. (lambda () 2)))
  650. (test #f (eqv? #f 'nil))
  651. (define gen-counter
  652. (lambda ()
  653. (let ((n 0))
  654. (lambda () (set! n (+ n 1)) n))))
  655. (test #t
  656. (let ((g (gen-counter)))
  657. (eqv? g g)))
  658. (test #f (eqv? (gen-counter) (gen-counter)))
  659. (define gen-loser
  660. (lambda ()
  661. (let ((n 0))
  662. (lambda () (set! n (+ n 1)) 27))))
  663. (test #t (let ((g (gen-loser)))
  664. (eqv? g g)))
  665. (test #f
  666. (letrec ((f (lambda () (if (eqv? f g) 'f 'both)))
  667. (g (lambda () (if (eqv? f g) 'g 'both))))
  668. (eqv? f g)))
  669. (test #t
  670. (let ((x '(a)))
  671. (eqv? x x)))
  672. (test #t (eq? 'a 'a))
  673. (test #f (eq? (list 'a) (list 'a)))
  674. (test #t (eq? '() '()))
  675. (test #t
  676. (let ((x '(a)))
  677. (eq? x x)))
  678. (test #t
  679. (let ((x '#()))
  680. (eq? x x)))
  681. (test #t
  682. (let ((p (lambda (x) x)))
  683. (eq? p p)))
  684. (test #t (equal? 'a 'a))
  685. (test #t (equal? '(a) '(a)))
  686. (test #t (equal? '(a (b) c)
  687. '(a (b) c)))
  688. (test #t (equal? "abc" "abc"))
  689. (test #t (equal? 2 2))
  690. (test #t (equal? (make-vector 5 'a)
  691. (make-vector 5 'a)))
  692. (test-end)
  693. (test-begin "6.2 Numbers")
  694. (test #t (complex? 3+4i))
  695. (test #t (complex? 3))
  696. (test #t (real? 3))
  697. (test #t (real? -2.5+0i))
  698. (test #f (real? -2.5+0.0i))
  699. (test #t (real? #e1e10))
  700. (test #t (real? +inf.0))
  701. (test #f (rational? -inf.0))
  702. (test #t (rational? 6/10))
  703. (test #t (rational? 6/3))
  704. (test #t (integer? 3+0i))
  705. (test #t (integer? 3.0))
  706. (test #t (integer? 8/4))
  707. (test #f (exact? 3.0))
  708. (test #t (exact? #e3.0))
  709. (test #t (inexact? 3.))
  710. (test #t (exact-integer? 32))
  711. (test #f (exact-integer? 32.0))
  712. (test #f (exact-integer? 32/5))
  713. (test #t (finite? 3))
  714. (test #f (finite? +inf.0))
  715. (test #f (finite? 3.0+inf.0i))
  716. (test #f (infinite? 3))
  717. (test #t (infinite? +inf.0))
  718. (test #f (infinite? +nan.0))
  719. (test #t (infinite? 3.0+inf.0i))
  720. (test #t (nan? +nan.0))
  721. (test #f (nan? 32))
  722. (test #t (nan? +nan.0+5.0i))
  723. (test #f (nan? 1+2i))
  724. (test #t (= 1 1.0 1.0+0.0i))
  725. (test #f (= 1.0 1.0+1.0i))
  726. (test #t (< 1 2 3))
  727. (test #f (< 1 1 2))
  728. (test #t (> 3.0 2.0 1.0))
  729. (test #f (> -3.0 2.0 1.0))
  730. (test #t (<= 1 1 2))
  731. (test #f (<= 1 2 1))
  732. (test #t (>= 2 1 1))
  733. (test #f (>= 1 2 1))
  734. (test '(#t #f) (list (<= 1 1 2) (<= 2 1 3)))
  735. ;; From R7RS 6.2.6 Numerical operations:
  736. ;;
  737. ;; These predicates are required to be transitive.
  738. ;;
  739. ;; _Note:_ The traditional implementations of these predicates in
  740. ;; Lisp-like languages, which involve converting all arguments to inexact
  741. ;; numbers if any argument is inexact, are not transitive.
  742. ;; Example from Alan Bawden
  743. (let ((a (- (expt 2 1000) 1))
  744. (b (inexact (expt 2 1000))) ; assuming > single-float-epsilon
  745. (c (+ (expt 2 1000) 1)))
  746. (test #t (if (and (= a b) (= b c))
  747. (= a c)
  748. #t)))
  749. ;; From CLtL 12.3. Comparisons on Numbers:
  750. ;;
  751. ;; Let _a_ be the result of (/ 10.0 single-float-epsilon), and let
  752. ;; _j_ be the result of (floor a). ..., all of (<= a j), (< j (+ j
  753. ;; 1)), and (<= (+ j 1) a) would be true; transitivity would then
  754. ;; imply that (< a a) ought to be true ...
  755. ;; Transliteration from Jussi Piitulainen
  756. (define single-float-epsilon
  757. (do ((eps 1.0 (* eps 2.0)))
  758. ((= eps (+ eps 1.0)) eps)))
  759. (let* ((a (/ 10.0 single-float-epsilon))
  760. (j (exact a)))
  761. (test #t (if (and (<= a j) (< j (+ j 1)))
  762. (not (<= (+ j 1) a))
  763. #t)))
  764. (test #t (zero? 0))
  765. (test #t (zero? 0.0))
  766. (test #t (zero? 0.0+0.0i))
  767. (test #f (zero? 1))
  768. (test #f (zero? -1))
  769. (test #f (positive? 0))
  770. (test #f (positive? 0.0))
  771. (test #t (positive? 1))
  772. (test #t (positive? 1.0))
  773. (test #f (positive? -1))
  774. (test #f (positive? -1.0))
  775. (test #t (positive? +inf.0))
  776. (test #f (positive? -inf.0))
  777. (test #f (negative? 0))
  778. (test #f (negative? 0.0))
  779. (test #f (negative? 1))
  780. (test #f (negative? 1.0))
  781. (test #t (negative? -1))
  782. (test #t (negative? -1.0))
  783. (test #f (negative? +inf.0))
  784. (test #t (negative? -inf.0))
  785. (test #f (odd? 0))
  786. (test #t (odd? 1))
  787. (test #t (odd? -1))
  788. (test #f (odd? 102))
  789. (test #t (even? 0))
  790. (test #f (even? 1))
  791. (test #t (even? -2))
  792. (test #t (even? 102))
  793. (test 3 (max 3))
  794. (test 4 (max 3 4))
  795. (test 4.0 (max 3.9 4))
  796. (test 5.0 (max 5 3.9 4))
  797. (test +inf.0 (max 100 +inf.0))
  798. (test 3 (min 3))
  799. (test 3 (min 3 4))
  800. (test 3.0 (min 3 3.1))
  801. (test -inf.0 (min -inf.0 -100))
  802. (test 7 (+ 3 4))
  803. (test 3 (+ 3))
  804. (test 0 (+))
  805. (test 4 (* 4))
  806. (test 1 (*))
  807. (test -1 (- 3 4))
  808. (test -6 (- 3 4 5))
  809. (test -3 (- 3))
  810. (test 3/20 (/ 3 4 5))
  811. (test 1/3 (/ 3))
  812. (test 7 (abs -7))
  813. (test 7 (abs 7))
  814. (test-values (values 2 1) (floor/ 5 2))
  815. (test-values (values -3 1) (floor/ -5 2))
  816. (test-values (values -3 -1) (floor/ 5 -2))
  817. (test-values (values 2 -1) (floor/ -5 -2))
  818. (test-values (values 2 1) (truncate/ 5 2))
  819. (test-values (values -2 -1) (truncate/ -5 2))
  820. (test-values (values -2 1) (truncate/ 5 -2))
  821. (test-values (values 2 -1) (truncate/ -5 -2))
  822. (test-values (values 2.0 -1.0) (truncate/ -5.0 -2))
  823. (test 1 (modulo 13 4))
  824. (test 1 (remainder 13 4))
  825. (test 3 (modulo -13 4))
  826. (test -1 (remainder -13 4))
  827. (test -3 (modulo 13 -4))
  828. (test 1 (remainder 13 -4))
  829. (test -1 (modulo -13 -4))
  830. (test -1 (remainder -13 -4))
  831. (test -1.0 (remainder -13 -4.0))
  832. (test 4 (gcd 32 -36))
  833. (test 0 (gcd))
  834. (test 288 (lcm 32 -36))
  835. (test 288.0 (lcm 32.0 -36))
  836. (test 1 (lcm))
  837. (test 3 (numerator (/ 6 4)))
  838. (test 2 (denominator (/ 6 4)))
  839. (test 2.0 (denominator (inexact (/ 6 4))))
  840. (test 11.0 (numerator 5.5))
  841. (test 2.0 (denominator 5.5))
  842. (test 5.0 (numerator 5.0))
  843. (test 1.0 (denominator 5.0))
  844. (test -5.0 (floor -4.3))
  845. (test -4.0 (ceiling -4.3))
  846. (test -4.0 (truncate -4.3))
  847. (test -4.0 (round -4.3))
  848. (test 3.0 (floor 3.5))
  849. (test 4.0 (ceiling 3.5))
  850. (test 3.0 (truncate 3.5))
  851. (test 4.0 (round 3.5))
  852. (test 4 (round 7/2))
  853. (test 7 (round 7))
  854. (test 1/3 (rationalize (exact .3) 1/10))
  855. (test #i1/3 (rationalize .3 1/10))
  856. (test 1.0 (inexact (exp 0))) ;; may return exact number
  857. (test 20.0855369231877 (exp 3))
  858. (test 0.0 (inexact (log 1))) ;; may return exact number
  859. (test 1.0 (log (exp 1)))
  860. (test 42.0 (log (exp 42)))
  861. (test 2.0 (log 100 10))
  862. (test 12.0 (log 4096 2))
  863. (test 0.0 (inexact (sin 0))) ;; may return exact number
  864. (test 1.0 (sin 1.5707963267949))
  865. (test 1.0 (inexact (cos 0))) ;; may return exact number
  866. (test -1.0 (cos 3.14159265358979))
  867. (test 0.0 (inexact (tan 0))) ;; may return exact number
  868. (test 1.5574077246549 (tan 1))
  869. (test 0.0 (inexact (asin 0))) ;; may return exact number
  870. (test 1.5707963267949 (asin 1))
  871. (test 0.0 (inexact (acos 1))) ;; may return exact number
  872. (test 3.14159265358979 (acos -1))
  873. ;; (test 0.0-0.0i (asin 0+0.0i))
  874. ;; (test 1.5707963267948966+0.0i (acos 0+0.0i))
  875. (test 0.0 (atan 0.0 1.0))
  876. (test -0.0 (atan -0.0 1.0))
  877. (test 0.785398163397448 (atan 1.0 1.0))
  878. (test 1.5707963267949 (atan 1.0 0.0))
  879. (test 2.35619449019234 (atan 1.0 -1.0))
  880. (test 3.14159265358979 (atan 0.0 -1.0))
  881. (test -3.14159265358979 (atan -0.0 -1.0)) ;
  882. (test -2.35619449019234 (atan -1.0 -1.0))
  883. (test -1.5707963267949 (atan -1.0 0.0))
  884. (test -0.785398163397448 (atan -1.0 1.0))
  885. ;; (test undefined (atan 0.0 0.0))
  886. (test 1764 (square 42))
  887. (test 4 (square 2))
  888. (test 3.0 (inexact (sqrt 9)))
  889. (test 1.4142135623731 (sqrt 2))
  890. (test 0.0+1.0i (inexact (sqrt -1)))
  891. (test '(2 0) (call-with-values (lambda () (exact-integer-sqrt 4)) list))
  892. (test '(2 1) (call-with-values (lambda () (exact-integer-sqrt 5)) list))
  893. (test 27 (expt 3 3))
  894. (test 1 (expt 0 0))
  895. (test 0 (expt 0 1))
  896. (test 1.0 (expt 0.0 0))
  897. (test 0.0 (expt 0 1.0))
  898. (test 1+2i (make-rectangular 1 2))
  899. (test 0.54030230586814+0.841470984807897i (make-polar 1 1))
  900. (cond-expand
  901. (exact-complex
  902. (test 1 (real-part 1+2i))
  903. (test 2 (imag-part 1+2i)))
  904. (else #f))
  905. (test 2.23606797749979 (magnitude 1+2i))
  906. (test 1.10714871779409 (angle 1+2i))
  907. (test 1.0 (inexact 1))
  908. (test #t (inexact? (inexact 1)))
  909. (test 1 (exact 1.0))
  910. (test #t (exact? (exact 1.0)))
  911. (test 100 (string->number "100"))
  912. (test 256 (string->number "100" 16))
  913. (test 100.0 (string->number "1e2"))
  914. (test-end)
  915. (test-begin "6.3 Booleans")
  916. (test #t #t)
  917. (test #f #f)
  918. (test #f '#f)
  919. (test #f (not #t))
  920. (test #f (not 3))
  921. (test #f (not (list 3)))
  922. (test #t (not #f))
  923. (test #f (not '()))
  924. (test #f (not (list)))
  925. (test #f (not 'nil))
  926. (test #t (boolean? #f))
  927. (test #f (boolean? 0))
  928. (test #f (boolean? '()))
  929. (test #t (boolean=? #t #t))
  930. (test #t (boolean=? #f #f))
  931. (test #f (boolean=? #t #f))
  932. (test #t (boolean=? #f #f #f))
  933. (test #f (boolean=? #t #t #f))
  934. (test-end)
  935. (test-begin "6.4 Lists")
  936. (let* ((x (list 'a 'b 'c))
  937. (y x))
  938. (test '(a b c) (values y))
  939. (test #t (list? y))
  940. (set-cdr! x 4)
  941. (test '(a . 4) (values x))
  942. (test #t (eqv? x y))
  943. (test #f (list? y))
  944. (set-cdr! x x)
  945. (test #f (list? x)))
  946. (test #t (pair? '(a . b)))
  947. (test #t (pair? '(a b c)))
  948. (test #f (pair? '()))
  949. (test #f (pair? '#(a b)))
  950. (test '(a) (cons 'a '()))
  951. (test '((a) b c d) (cons '(a) '(b c d)))
  952. (test '("a" b c) (cons "a" '(b c)))
  953. (test '(a . 3) (cons 'a 3))
  954. (test '((a b) . c) (cons '(a b) 'c))
  955. (test 'a (car '(a b c)))
  956. (test '(a) (car '((a) b c d)))
  957. (test 1 (car '(1 . 2)))
  958. (test '(b c d) (cdr '((a) b c d)))
  959. (test 2 (cdr '(1 . 2)))
  960. (define (g) '(constant-list))
  961. (test #t (list? '(a b c)))
  962. (test #t (list? '()))
  963. (test #f (list? '(a . b)))
  964. (test #f (let ((x (list 'a))) (set-cdr! x x) (list? x)))
  965. (test '(3 3) (make-list 2 3))
  966. (test '(a 7 c) (list 'a (+ 3 4) 'c))
  967. (test '() (list))
  968. (test 3 (length '(a b c)))
  969. (test 3 (length '(a (b) (c d e))))
  970. (test 0 (length '()))
  971. (test '(x y) (append '(x) '(y)))
  972. (test '(a b c d) (append '(a) '(b c d)))
  973. (test '(a (b) (c)) (append '(a (b)) '((c))))
  974. (test '(a b c . d) (append '(a b) '(c . d)))
  975. (test 'a (append '() 'a))
  976. (test '(c b a) (reverse '(a b c)))
  977. (test '((e (f)) d (b c) a) (reverse '(a (b c) d (e (f)))))
  978. (test '(d e) (list-tail '(a b c d e) 3))
  979. (test 'c (list-ref '(a b c d) 2))
  980. (test 'c (list-ref '(a b c d)
  981. (exact (round 1.8))))
  982. (test '(0 ("Sue" "Sue") "Anna")
  983. (let ((lst (list 0 '(2 2 2 2) "Anna")))
  984. (list-set! lst 1 '("Sue" "Sue"))
  985. lst))
  986. (test '(a b c) (memq 'a '(a b c)))
  987. (test '(b c) (memq 'b '(a b c)))
  988. (test #f (memq 'a '(b c d)))
  989. (test #f (memq (list 'a) '(b (a) c)))
  990. (test '((a) c) (member (list 'a) '(b (a) c)))
  991. (test '("b" "c") (member "B" '("a" "b" "c") string-ci=?))
  992. (test '(101 102) (memv 101 '(100 101 102)))
  993. (let ()
  994. (define e '((a 1) (b 2) (c 3)))
  995. (test '(a 1) (assq 'a e))
  996. (test '(b 2) (assq 'b e))
  997. (test #f (assq 'd e)))
  998. (test #f (assq (list 'a) '(((a)) ((b)) ((c)))))
  999. (test '((a)) (assoc (list 'a) '(((a)) ((b)) ((c)))))
  1000. (test '(2 4) (assoc 2.0 '((1 1) (2 4) (3 9)) =))
  1001. (test '(5 7) (assv 5 '((2 3) (5 7) (11 13))))
  1002. (test '(1 2 3) (list-copy '(1 2 3)))
  1003. (test "foo" (list-copy "foo"))
  1004. (test '() (list-copy '()))
  1005. (test '(3 . 4) (list-copy '(3 . 4)))
  1006. (test '(6 7 8 . 9) (list-copy '(6 7 8 . 9)))
  1007. (let* ((l1 '((a b) (c d) e))
  1008. (l2 (list-copy l1)))
  1009. (test l2 '((a b) (c d) e))
  1010. (test #t (eq? (car l1) (car l2)))
  1011. (test #t (eq? (cadr l1) (cadr l2)))
  1012. (test #f (eq? (cdr l1) (cdr l2)))
  1013. (test #f (eq? (cddr l1) (cddr l2))))
  1014. (test-end)
  1015. (test-begin "6.5 Symbols")
  1016. (test #t (symbol? 'foo))
  1017. (test #t (symbol? (car '(a b))))
  1018. (test #f (symbol? "bar"))
  1019. (test #t (symbol? 'nil))
  1020. (test #f (symbol? '()))
  1021. (test #f (symbol? #f))
  1022. (test #t (symbol=? 'a 'a))
  1023. (test #f (symbol=? 'a 'A))
  1024. (test #t (symbol=? 'a 'a 'a))
  1025. (test #f (symbol=? 'a 'a 'A))
  1026. (test "flying-fish"
  1027. (symbol->string 'flying-fish))
  1028. (test "Martin" (symbol->string 'Martin))
  1029. (test "Malvina" (symbol->string (string->symbol "Malvina")))
  1030. (test 'mISSISSIppi (string->symbol "mISSISSIppi"))
  1031. (test #t (eq? 'bitBlt (string->symbol "bitBlt")))
  1032. (test #t (eq? 'LollyPop (string->symbol (symbol->string 'LollyPop))))
  1033. (test #t (string=? "K. Harper, M.D."
  1034. (symbol->string (string->symbol "K. Harper, M.D."))))
  1035. (test-end)
  1036. (test-begin "6.6 Characters")
  1037. (test #t (char? #\a))
  1038. (test #f (char? "a"))
  1039. (test #f (char? 'a))
  1040. (test #f (char? 0))
  1041. (test #t (char=? #\a #\a #\a))
  1042. (test #f (char=? #\a #\A))
  1043. (test #t (char<? #\a #\b #\c))
  1044. (test #f (char<? #\a #\a))
  1045. (test #f (char<? #\b #\a))
  1046. (test #f (char>? #\a #\b))
  1047. (test #f (char>? #\a #\a))
  1048. (test #t (char>? #\c #\b #\a))
  1049. (test #t (char<=? #\a #\b #\b))
  1050. (test #t (char<=? #\a #\a))
  1051. (test #f (char<=? #\b #\a))
  1052. (test #f (char>=? #\a #\b))
  1053. (test #t (char>=? #\a #\a))
  1054. (test #t (char>=? #\b #\b #\a))
  1055. (test #t (char-ci=? #\a #\a))
  1056. (test #t (char-ci=? #\a #\A #\a))
  1057. (test #f (char-ci=? #\a #\b))
  1058. (test #t (char-ci<? #\a #\B #\c))
  1059. (test #f (char-ci<? #\A #\a))
  1060. (test #f (char-ci<? #\b #\A))
  1061. (test #f (char-ci>? #\A #\b))
  1062. (test #f (char-ci>? #\a #\A))
  1063. (test #t (char-ci>? #\c #\B #\a))
  1064. (test #t (char-ci<=? #\a #\B #\b))
  1065. (test #t (char-ci<=? #\A #\a))
  1066. (test #f (char-ci<=? #\b #\A))
  1067. (test #f (char-ci>=? #\A #\b))
  1068. (test #t (char-ci>=? #\a #\A))
  1069. (test #t (char-ci>=? #\b #\B #\a))
  1070. (test #t (char-alphabetic? #\a))
  1071. (test #f (char-alphabetic? #\space))
  1072. (test #t (char-numeric? #\0))
  1073. (test #f (char-numeric? #\.))
  1074. (test #f (char-numeric? #\a))
  1075. (test #t (char-whitespace? #\space))
  1076. (test #t (char-whitespace? #\tab))
  1077. (test #t (char-whitespace? #\newline))
  1078. (test #f (char-whitespace? #\_))
  1079. (test #f (char-whitespace? #\a))
  1080. (test #t (char-upper-case? #\A))
  1081. (test #f (char-upper-case? #\a))
  1082. (test #f (char-upper-case? #\3))
  1083. (test #t (char-lower-case? #\a))
  1084. (test #f (char-lower-case? #\A))
  1085. (test #f (char-lower-case? #\3))
  1086. (test #t (char-alphabetic? #\Λ))
  1087. (test #f (char-alphabetic? #\x0E50))
  1088. (test #t (char-upper-case? #\Λ))
  1089. (test #f (char-upper-case? #\λ))
  1090. (test #f (char-lower-case? #\Λ))
  1091. (test #t (char-lower-case? #\λ))
  1092. (test #f (char-numeric? #\Λ))
  1093. (test #t (char-numeric? #\x0E50))
  1094. (test #t (char-whitespace? #\x1680))
  1095. (test 0 (digit-value #\0))
  1096. (test 3 (digit-value #\3))
  1097. (test 9 (digit-value #\9))
  1098. (test 4 (digit-value #\x0664))
  1099. (test 0 (digit-value #\x0AE6))
  1100. (test #f (digit-value #\.))
  1101. (test #f (digit-value #\-))
  1102. (test 97 (char->integer #\a))
  1103. (test #\a (integer->char 97))
  1104. (test #\A (char-upcase #\a))
  1105. (test #\A (char-upcase #\A))
  1106. (test #\a (char-downcase #\a))
  1107. (test #\a (char-downcase #\A))
  1108. (test #\a (char-foldcase #\a))
  1109. (test #\a (char-foldcase #\A))
  1110. (test #\Λ (char-upcase #\λ))
  1111. (test #\Λ (char-upcase #\Λ))
  1112. (test #\λ (char-downcase #\λ))
  1113. (test #\λ (char-downcase #\Λ))
  1114. (test #\λ (char-foldcase #\λ))
  1115. (test #\λ (char-foldcase #\Λ))
  1116. (test-end)
  1117. (test-begin "6.7 Strings")
  1118. (test #t (string? ""))
  1119. (test #t (string? " "))
  1120. (test #f (string? 'a))
  1121. (test #f (string? #\a))
  1122. (test 3 (string-length (make-string 3)))
  1123. (test "---" (make-string 3 #\-))
  1124. (test "" (string))
  1125. (test "---" (string #\- #\- #\-))
  1126. (test "kitten" (string #\k #\i #\t #\t #\e #\n))
  1127. (test 0 (string-length ""))
  1128. (test 1 (string-length "a"))
  1129. (test 3 (string-length "abc"))
  1130. (test #\a (string-ref "abc" 0))
  1131. (test #\b (string-ref "abc" 1))
  1132. (test #\c (string-ref "abc" 2))
  1133. (test "a-c" (let ((str (string #\a #\b #\c))) (string-set! str 1 #\-) str))
  1134. (test (string #\a #\x1F700 #\c)
  1135. (let ((s (string #\a #\b #\c)))
  1136. (string-set! s 1 #\x1F700)
  1137. s))
  1138. (test #t (string=? "" ""))
  1139. (test #t (string=? "abc" "abc" "abc"))
  1140. (test #f (string=? "" "abc"))
  1141. (test #f (string=? "abc" "aBc"))
  1142. (test #f (string<? "" ""))
  1143. (test #f (string<? "abc" "abc"))
  1144. (test #t (string<? "abc" "abcd" "acd"))
  1145. (test #f (string<? "abcd" "abc"))
  1146. (test #t (string<? "abc" "bbc"))
  1147. (test #f (string>? "" ""))
  1148. (test #f (string>? "abc" "abc"))
  1149. (test #f (string>? "abc" "abcd"))
  1150. (test #t (string>? "acd" "abcd" "abc"))
  1151. (test #f (string>? "abc" "bbc"))
  1152. (test #t (string<=? "" ""))
  1153. (test #t (string<=? "abc" "abc"))
  1154. (test #t (string<=? "abc" "abcd" "abcd"))
  1155. (test #f (string<=? "abcd" "abc"))
  1156. (test #t (string<=? "abc" "bbc"))
  1157. (test #t (string>=? "" ""))
  1158. (test #t (string>=? "abc" "abc"))
  1159. (test #f (string>=? "abc" "abcd"))
  1160. (test #t (string>=? "abcd" "abcd" "abc"))
  1161. (test #f (string>=? "abc" "bbc"))
  1162. (test #t (string-ci=? "" ""))
  1163. (test #t (string-ci=? "abc" "abc"))
  1164. (test #f (string-ci=? "" "abc"))
  1165. (test #t (string-ci=? "abc" "aBc"))
  1166. (test #f (string-ci=? "abc" "aBcD"))
  1167. (test #f (string-ci<? "abc" "aBc"))
  1168. (test #t (string-ci<? "abc" "aBcD"))
  1169. (test #f (string-ci<? "ABCd" "aBc"))
  1170. (test #f (string-ci>? "abc" "aBc"))
  1171. (test #f (string-ci>? "abc" "aBcD"))
  1172. (test #t (string-ci>? "ABCd" "aBc"))
  1173. (test #t (string-ci<=? "abc" "aBc"))
  1174. (test #t (string-ci<=? "abc" "aBcD"))
  1175. (test #f (string-ci<=? "ABCd" "aBc"))
  1176. (test #t (string-ci>=? "abc" "aBc"))
  1177. (test #f (string-ci>=? "abc" "aBcD"))
  1178. (test #t (string-ci>=? "ABCd" "aBc"))
  1179. ;; Fails in Ikarus and Larceny
  1180. (cond-expand
  1181. ((or ikarus larceny) #f)
  1182. (else
  1183. (test #t (string-ci=? "ΑΒΓ" "αβγ" "αβγ"))))
  1184. (test #f (string-ci<? "ΑΒΓ" "αβγ"))
  1185. (test #f (string-ci>? "ΑΒΓ" "αβγ"))
  1186. (test #t (string-ci<=? "ΑΒΓ" "αβγ"))
  1187. (test #t (string-ci>=? "ΑΒΓ" "αβγ"))
  1188. ;; latin
  1189. (test "ABC" (string-upcase "abc"))
  1190. (test "ABC" (string-upcase "ABC"))
  1191. (test "abc" (string-downcase "abc"))
  1192. (test "abc" (string-downcase "ABC"))
  1193. (test "abc" (string-foldcase "abc"))
  1194. (test "abc" (string-foldcase "ABC"))
  1195. ;; cyrillic
  1196. (test "ΑΒΓ" (string-upcase "αβγ"))
  1197. (test "ΑΒΓ" (string-upcase "ΑΒΓ"))
  1198. (test "αβγ" (string-downcase "αβγ"))
  1199. (test "αβγ" (string-downcase "ΑΒΓ"))
  1200. (test "αβγ" (string-foldcase "αβγ"))
  1201. (test "αβγ" (string-foldcase "ΑΒΓ"))
  1202. ;; special cases
  1203. (test "SSA" (string-upcase "ßa"))
  1204. (test "ßa" (string-downcase "ßa"))
  1205. (test "ssa" (string-downcase "SSA"))
  1206. (test "maß" (string-downcase "Maß"))
  1207. (test "mass" (string-foldcase "Maß"))
  1208. (test "İ" (string-upcase "İ"))
  1209. (test "i\x0307;" (string-downcase "İ"))
  1210. (test "i\x0307;" (string-foldcase "İ"))
  1211. (test "J̌" (string-upcase "ǰ"))
  1212. (test "ſ" (string-downcase "ſ"))
  1213. (test "s" (string-foldcase "ſ"))
  1214. ;; context-sensitive (final sigma)
  1215. (test "ΓΛΏΣΣΑ" (string-upcase "γλώσσα"))
  1216. (test "γλώσσα" (string-downcase "ΓΛΏΣΣΑ"))
  1217. (test "γλώσσα" (string-foldcase "ΓΛΏΣΣΑ"))
  1218. (test "ΜΈΛΟΣ" (string-upcase "μέλος"))
  1219. (test #t (and (member (string-downcase "ΜΈΛΟΣ") '("μέλος" "μέλοσ")) #t))
  1220. (failing-test "https://bugs.gnu.org/38235"
  1221. "μέλοσ" (string-foldcase "ΜΈΛΟΣ"))
  1222. (test #t (and (member (string-downcase "ΜΈΛΟΣ ΕΝΌΣ")
  1223. '("μέλος ενός" "μέλοσ ενόσ"))
  1224. #t))
  1225. (test "" (substring "" 0 0))
  1226. (test "" (substring "a" 0 0))
  1227. (test "" (substring "abc" 1 1))
  1228. (test "ab" (substring "abc" 0 2))
  1229. (test "bc" (substring "abc" 1 3))
  1230. (test "" (string-append ""))
  1231. (test "" (string-append "" ""))
  1232. (test "abc" (string-append "" "abc"))
  1233. (test "abc" (string-append "abc" ""))
  1234. (test "abcde" (string-append "abc" "de"))
  1235. (test "abcdef" (string-append "abc" "de" "f"))
  1236. (test '() (string->list ""))
  1237. (test '(#\a) (string->list "a"))
  1238. (test '(#\a #\b #\c) (string->list "abc"))
  1239. (test '(#\a #\b #\c) (string->list "abc" 0))
  1240. (test '(#\b #\c) (string->list "abc" 1))
  1241. (test '(#\b #\c) (string->list "abc" 1 3))
  1242. (test "" (list->string '()))
  1243. (test "abc" (list->string '(#\a #\b #\c)))
  1244. (test "" (string-copy ""))
  1245. (test "" (string-copy "" 0))
  1246. (test "" (string-copy "" 0 0))
  1247. (test "abc" (string-copy "abc"))
  1248. (test "abc" (string-copy "abc" 0))
  1249. (test "bc" (string-copy "abc" 1))
  1250. (test "b" (string-copy "abc" 1 2))
  1251. (test "bc" (string-copy "abc" 1 3))
  1252. (test "-----"
  1253. (let ((str (make-string 5 #\x))) (string-fill! str #\-) str))
  1254. (test "xx---"
  1255. (let ((str (make-string 5 #\x))) (string-fill! str #\- 2) str))
  1256. (test "xx-xx"
  1257. (let ((str (make-string 5 #\x))) (string-fill! str #\- 2 3) str))
  1258. (test "a12de"
  1259. (let ((str (string-copy "abcde"))) (string-copy! str 1 "12345" 0 2) str))
  1260. (test "-----"
  1261. (let ((str (make-string 5 #\x))) (string-copy! str 0 "-----") str))
  1262. (test "---xx"
  1263. (let ((str (make-string 5 #\x))) (string-copy! str 0 "-----" 2) str))
  1264. (test "xx---"
  1265. (let ((str (make-string 5 #\x))) (string-copy! str 2 "-----" 0 3) str))
  1266. (test "xx-xx"
  1267. (let ((str (make-string 5 #\x))) (string-copy! str 2 "-----" 2 3) str))
  1268. ;; same source and dest
  1269. (test "aabde"
  1270. (let ((str (string-copy "abcde"))) (string-copy! str 1 str 0 2) str))
  1271. (test "abcab"
  1272. (let ((str (string-copy "abcde"))) (string-copy! str 3 str 0 2) str))
  1273. (test-end)
  1274. (test-begin "6.8 Vectors")
  1275. ;; (test #t (vector? #()))
  1276. ;; (test #t (vector? #(1 2 3)))
  1277. (test #t (vector? '#(1 2 3)))
  1278. (test 0 (vector-length (make-vector 0)))
  1279. (test 1000 (vector-length (make-vector 1000)))
  1280. ;; (test #(0 (2 2 2 2) "Anna") '#(0 (2 2 2 2) "Anna"))
  1281. (test '#(a b c) (vector 'a 'b 'c))
  1282. (test 8 (vector-ref '#(1 1 2 3 5 8 13 21) 5))
  1283. (test 13 (vector-ref '#(1 1 2 3 5 8 13 21)
  1284. (let ((i (round (* 2 (acos -1)))))
  1285. (if (inexact? i)
  1286. (exact i)
  1287. i))))
  1288. (test '#(0 ("Sue" "Sue") "Anna") (let ((vec (vector 0 '(2 2 2 2) "Anna")))
  1289. (vector-set! vec 1 '("Sue" "Sue"))
  1290. vec))
  1291. (test '(dah dah didah) (vector->list '#(dah dah didah)))
  1292. (test '(dah didah) (vector->list '#(dah dah didah) 1))
  1293. (test '(dah) (vector->list '#(dah dah didah) 1 2))
  1294. (test '#(dididit dah) (list->vector '(dididit dah)))
  1295. (test '#() (string->vector ""))
  1296. (test '#(#\A #\B #\C) (string->vector "ABC"))
  1297. (test '#(#\B #\C) (string->vector "ABC" 1))
  1298. (test '#(#\B) (string->vector "ABC" 1 2))
  1299. (test "" (vector->string '#()))
  1300. (test "123" (vector->string '#(#\1 #\2 #\3)))
  1301. (test "23" (vector->string '#(#\1 #\2 #\3) 1))
  1302. (test "2" (vector->string '#(#\1 #\2 #\3) 1 2))
  1303. (test '#() (vector-copy '#()))
  1304. (test '#(a b c) (vector-copy '#(a b c)))
  1305. (test '#(b c) (vector-copy '#(a b c) 1))
  1306. (test '#(b) (vector-copy '#(a b c) 1 2))
  1307. (test '#() (vector-append '#()))
  1308. (test '#() (vector-append '#() '#()))
  1309. (test '#(a b c) (vector-append '#() '#(a b c)))
  1310. (test '#(a b c) (vector-append '#(a b c) '#()))
  1311. (test '#(a b c d e) (vector-append '#(a b c) '#(d e)))
  1312. (test '#(a b c d e f) (vector-append '#(a b c) '#(d e) '#(f)))
  1313. (test '#(1 2 smash smash 5)
  1314. (let ((vec (vector 1 2 3 4 5))) (vector-fill! vec 'smash 2 4) vec))
  1315. (test '#(x x x x x)
  1316. (let ((vec (vector 1 2 3 4 5))) (vector-fill! vec 'x) vec))
  1317. (test '#(1 2 x x x)
  1318. (let ((vec (vector 1 2 3 4 5))) (vector-fill! vec 'x 2) vec))
  1319. (test '#(1 2 x 4 5)
  1320. (let ((vec (vector 1 2 3 4 5))) (vector-fill! vec 'x 2 3) vec))
  1321. (test '#(1 a b 4 5)
  1322. (let ((vec (vector 1 2 3 4 5))) (vector-copy! vec 1 '#(a b c d e) 0 2) vec))
  1323. (test '#(a b c d e)
  1324. (let ((vec (vector 1 2 3 4 5))) (vector-copy! vec 0 '#(a b c d e)) vec))
  1325. (test '#(c d e 4 5)
  1326. (let ((vec (vector 1 2 3 4 5))) (vector-copy! vec 0 '#(a b c d e) 2) vec))
  1327. (test '#(1 2 a b c)
  1328. (let ((vec (vector 1 2 3 4 5))) (vector-copy! vec 2 '#(a b c d e) 0 3) vec))
  1329. (test '#(1 2 c 4 5)
  1330. (let ((vec (vector 1 2 3 4 5))) (vector-copy! vec 2 '#(a b c d e) 2 3) vec))
  1331. ;; same source and dest
  1332. (test '#(1 1 2 4 5)
  1333. (let ((vec (vector 1 2 3 4 5))) (vector-copy! vec 1 vec 0 2) vec))
  1334. (test '#(1 2 3 1 2)
  1335. (let ((vec (vector 1 2 3 4 5))) (vector-copy! vec 3 vec 0 2) vec))
  1336. (test-end)
  1337. (test-begin "6.9 Bytevectors")
  1338. (test #t (bytevector? #u8()))
  1339. (test #t (bytevector? #u8(0 1 2)))
  1340. (test #f (bytevector? '#()))
  1341. (test #f (bytevector? '#(0 1 2)))
  1342. (test #f (bytevector? '()))
  1343. (test #t (bytevector? (make-bytevector 0)))
  1344. (test 0 (bytevector-length (make-bytevector 0)))
  1345. (test 1024 (bytevector-length (make-bytevector 1024)))
  1346. (test 1024 (bytevector-length (make-bytevector 1024 255)))
  1347. (test 3 (bytevector-length (bytevector 0 1 2)))
  1348. (test 0 (bytevector-u8-ref (bytevector 0 1 2) 0))
  1349. (test 1 (bytevector-u8-ref (bytevector 0 1 2) 1))
  1350. (test 2 (bytevector-u8-ref (bytevector 0 1 2) 2))
  1351. (test #u8(0 255 2)
  1352. (let ((bv (bytevector 0 1 2))) (bytevector-u8-set! bv 1 255) bv))
  1353. (test #u8() (bytevector-copy #u8()))
  1354. (test #u8(0 1 2) (bytevector-copy #u8(0 1 2)))
  1355. (test #u8(1 2) (bytevector-copy #u8(0 1 2) 1))
  1356. (test #u8(1) (bytevector-copy #u8(0 1 2) 1 2))
  1357. (test #u8(1 6 7 4 5)
  1358. (let ((bv (bytevector 1 2 3 4 5)))
  1359. (bytevector-copy! bv 1 #u8(6 7 8 9 10) 0 2)
  1360. bv))
  1361. (test #u8(6 7 8 9 10)
  1362. (let ((bv (bytevector 1 2 3 4 5)))
  1363. (bytevector-copy! bv 0 #u8(6 7 8 9 10))
  1364. bv))
  1365. (test #u8(8 9 10 4 5)
  1366. (let ((bv (bytevector 1 2 3 4 5)))
  1367. (bytevector-copy! bv 0 #u8(6 7 8 9 10) 2)
  1368. bv))
  1369. (test #u8(1 2 6 7 8)
  1370. (let ((bv (bytevector 1 2 3 4 5)))
  1371. (bytevector-copy! bv 2 #u8(6 7 8 9 10) 0 3)
  1372. bv))
  1373. (test #u8(1 2 8 4 5)
  1374. (let ((bv (bytevector 1 2 3 4 5)))
  1375. (bytevector-copy! bv 2 #u8(6 7 8 9 10) 2 3)
  1376. bv))
  1377. ;; same source and dest
  1378. (test #u8(1 1 2 4 5)
  1379. (let ((bv (bytevector 1 2 3 4 5)))
  1380. (bytevector-copy! bv 1 bv 0 2)
  1381. bv))
  1382. (test #u8(1 2 3 1 2)
  1383. (let ((bv (bytevector 1 2 3 4 5)))
  1384. (bytevector-copy! bv 3 bv 0 2)
  1385. bv))
  1386. (test #u8() (bytevector-append #u8()))
  1387. (test #u8() (bytevector-append #u8() #u8()))
  1388. (test #u8(0 1 2) (bytevector-append #u8() #u8(0 1 2)))
  1389. (test #u8(0 1 2) (bytevector-append #u8(0 1 2) #u8()))
  1390. (test #u8(0 1 2 3 4) (bytevector-append #u8(0 1 2) #u8(3 4)))
  1391. (test #u8(0 1 2 3 4 5) (bytevector-append #u8(0 1 2) #u8(3 4) #u8(5)))
  1392. (test "ABC" (utf8->string #u8(#x41 #x42 #x43)))
  1393. (test "ABC" (utf8->string #u8(0 #x41 #x42 #x43) 1))
  1394. (test "ABC" (utf8->string #u8(0 #x41 #x42 #x43 0) 1 4))
  1395. (test "λ" (utf8->string #u8(0 #xCE #xBB 0) 1 3))
  1396. (test #u8(#x41 #x42 #x43) (string->utf8 "ABC"))
  1397. (test #u8(#x42 #x43) (string->utf8 "ABC" 1))
  1398. (test #u8(#x42) (string->utf8 "ABC" 1 2))
  1399. (test #u8(#xCE #xBB) (string->utf8 "λ"))
  1400. (test-end)
  1401. (test-begin "6.10 Control Features")
  1402. (test #t (procedure? car))
  1403. (test #f (procedure? 'car))
  1404. (test #t (procedure? (lambda (x) (* x x))))
  1405. (test #f (procedure? '(lambda (x) (* x x))))
  1406. (test #t (call-with-current-continuation procedure?))
  1407. (test 7 (apply + (list 3 4)))
  1408. (test 7 (apply + 3 4 (list)))
  1409. (cond-expand
  1410. (sagittarius ;raises the error at compile time
  1411. #t)
  1412. (else
  1413. (test-error (apply +)))) ;; not enough args
  1414. (test-error (apply + 3)) ;; final arg not a list
  1415. (test-error (apply + 3 4)) ;; final arg not a list
  1416. (test-error (apply + '(2 3 . 4))) ;; final arg is improper
  1417. (define compose
  1418. (lambda (f g)
  1419. (lambda args
  1420. (f (apply g args)))))
  1421. (test '(30 0)
  1422. (call-with-values (lambda () ((compose exact-integer-sqrt *) 12 75))
  1423. list))
  1424. (test '(b e h) (map cadr '((a b) (d e) (g h))))
  1425. (test '(1 4 27 256 3125) (map (lambda (n) (expt n n)) '(1 2 3 4 5)))
  1426. (test '(5 7 9) (map + '(1 2 3) '(4 5 6 7)))
  1427. (test #t
  1428. (let ((res (let ((count 0))
  1429. (map (lambda (ignored)
  1430. (set! count (+ count 1))
  1431. count)
  1432. '(a b)))))
  1433. (or (equal? res '(1 2))
  1434. (equal? res '(2 1)))))
  1435. (test '(10 200 3000 40 500 6000)
  1436. (let ((ls1 (list 10 100 1000))
  1437. (ls2 (list 1 2 3 4 5 6)))
  1438. (set-cdr! (cddr ls1) ls1)
  1439. (map * ls1 ls2)))
  1440. (test "abdegh" (string-map char-foldcase "AbdEgH"))
  1441. (test "IBM" (string-map
  1442. (lambda (c)
  1443. (integer->char (+ 1 (char->integer c))))
  1444. "HAL"))
  1445. (test "StUdLyCaPs"
  1446. (string-map
  1447. (lambda (c k) (if (eqv? k #\u) (char-upcase c) (char-downcase c)))
  1448. "studlycaps xxx"
  1449. "ululululul"))
  1450. (test '#(b e h) (vector-map cadr '#((a b) (d e) (g h))))
  1451. (test '#(1 4 27 256 3125)
  1452. (vector-map (lambda (n) (expt n n))
  1453. '#(1 2 3 4 5)))
  1454. (test '#(5 7 9) (vector-map + '#(1 2 3) '#(4 5 6 7)))
  1455. (test #t
  1456. (let ((res (let ((count 0))
  1457. (vector-map
  1458. (lambda (ignored)
  1459. (set! count (+ count 1))
  1460. count)
  1461. '#(a b)))))
  1462. (or (equal? res '#(1 2))
  1463. (equal? res '#(2 1)))))
  1464. (test '#(0 1 4 9 16)
  1465. (let ((v (make-vector 5)))
  1466. (for-each (lambda (i)
  1467. (vector-set! v i (* i i)))
  1468. '(0 1 2 3 4))
  1469. v))
  1470. (test 9750
  1471. (let ((ls1 (list 10 100 1000))
  1472. (ls2 (list 1 2 3 4 5 6))
  1473. (count 0))
  1474. (set-cdr! (cddr ls1) ls1)
  1475. (for-each (lambda (x y) (set! count (+ count (* x y)))) ls2 ls1)
  1476. count))
  1477. (test '(101 100 99 98 97)
  1478. (let ((v '()))
  1479. (string-for-each
  1480. (lambda (c) (set! v (cons (char->integer c) v)))
  1481. "abcde")
  1482. v))
  1483. (test '(0 1 4 9 16) (let ((v (make-list 5)))
  1484. (vector-for-each
  1485. (lambda (i) (list-set! v i (* i i)))
  1486. '#(0 1 2 3 4))
  1487. v))
  1488. (test -3 (call-with-current-continuation
  1489. (lambda (exit)
  1490. (for-each (lambda (x)
  1491. (if (negative? x)
  1492. (exit x)))
  1493. '(54 0 37 -3 245 19))
  1494. #t)))
  1495. (define list-length
  1496. (lambda (obj)
  1497. (call-with-current-continuation
  1498. (lambda (return)
  1499. (letrec ((r
  1500. (lambda (obj)
  1501. (cond ((null? obj) 0)
  1502. ((pair? obj)
  1503. (+ (r (cdr obj)) 1))
  1504. (else (return #f))))))
  1505. (r obj))))))
  1506. (test 4 (list-length '(1 2 3 4)))
  1507. (test #f (list-length '(a b . c)))
  1508. (test 5
  1509. (call-with-values (lambda () (values 4 5))
  1510. (lambda (a b) b)))
  1511. (test -1 (call-with-values * -))
  1512. (test '(connect talk1 disconnect
  1513. connect talk2 disconnect)
  1514. (let ((path '())
  1515. (c #f))
  1516. (let ((add (lambda (s)
  1517. (set! path (cons s path)))))
  1518. (dynamic-wind
  1519. (lambda () (add 'connect))
  1520. (lambda ()
  1521. (add (call-with-current-continuation
  1522. (lambda (c0)
  1523. (set! c c0)
  1524. 'talk1))))
  1525. (lambda () (add 'disconnect)))
  1526. (if (< (length path) 4)
  1527. (c 'talk2)
  1528. (reverse path)))))
  1529. (test-end)
  1530. (test-begin "6.11 Exceptions")
  1531. (test 65
  1532. (with-exception-handler
  1533. (lambda (con) 42)
  1534. (lambda ()
  1535. (+ (raise-continuable "should be a number")
  1536. 23))))
  1537. (test #t
  1538. (error-object? (guard (exn (else exn)) (error "BOOM!" 1 2 3))))
  1539. (test "BOOM!"
  1540. (error-object-message (guard (exn (else exn)) (error "BOOM!" 1 2 3))))
  1541. (test '(1 2 3)
  1542. (error-object-irritants (guard (exn (else exn)) (error "BOOM!" 1 2 3))))
  1543. (test #f
  1544. (file-error? (guard (exn (else exn)) (error "BOOM!"))))
  1545. (failing-test
  1546. "https://bugs.gnu.org/38237"
  1547. #t
  1548. (file-error? (guard (exn (else exn)) (open-input-file " no such file "))))
  1549. (test #f
  1550. (read-error? (guard (exn (else exn)) (error "BOOM!"))))
  1551. (test #t
  1552. (read-error? (guard (exn (else exn)) (read (open-input-string ")")))))
  1553. (define something-went-wrong #f)
  1554. (define (test-exception-handler-1 v)
  1555. (call-with-current-continuation
  1556. (lambda (k)
  1557. (with-exception-handler
  1558. (lambda (x)
  1559. (set! something-went-wrong (list "condition: " x))
  1560. (k 'exception))
  1561. (lambda ()
  1562. (+ 1 (if (> v 0) (+ v 100) (raise 'an-error))))))))
  1563. (test 106 (test-exception-handler-1 5))
  1564. (test #f something-went-wrong)
  1565. (test 'exception (test-exception-handler-1 -1))
  1566. (test '("condition: " an-error) something-went-wrong)
  1567. (set! something-went-wrong #f)
  1568. (define (test-exception-handler-2 v)
  1569. (guard (ex (else 'caught-another-exception))
  1570. (with-exception-handler
  1571. (lambda (x)
  1572. (set! something-went-wrong #t)
  1573. (list "exception:" x))
  1574. (lambda ()
  1575. (+ 1 (if (> v 0) (+ v 100) (raise 'an-error)))))))
  1576. (test 106 (test-exception-handler-2 5))
  1577. (test #f something-went-wrong)
  1578. (test 'caught-another-exception (test-exception-handler-2 -1))
  1579. (test #t something-went-wrong)
  1580. ;; Based on an example from R6RS-lib section 7.1 Exceptions.
  1581. ;; R7RS section 6.11 Exceptions has a simplified version.
  1582. (let* ((out (open-output-string))
  1583. (value (with-exception-handler
  1584. (lambda (con)
  1585. (cond
  1586. ((not (list? con))
  1587. (raise con))
  1588. ((list? con)
  1589. (display (car con) out))
  1590. (else
  1591. (display "a warning has been issued" out)))
  1592. 42)
  1593. (lambda ()
  1594. (+ (raise-continuable
  1595. (list "should be a number"))
  1596. 23)))))
  1597. (test "should be a number" (get-output-string out))
  1598. (test 65 value))
  1599. ;; From SRFI-34 "Examples" section - #3
  1600. (define (test-exception-handler-3 v out)
  1601. (guard (condition
  1602. (else
  1603. (display "condition: " out)
  1604. (write condition out)
  1605. (display #\! out)
  1606. 'exception))
  1607. (+ 1 (if (= v 0) (raise 'an-error) (/ 10 v)))))
  1608. (let* ((out (open-output-string))
  1609. (value (test-exception-handler-3 0 out)))
  1610. (test 'exception value)
  1611. (test "condition: an-error!" (get-output-string out)))
  1612. (define (test-exception-handler-4 v out)
  1613. (call-with-current-continuation
  1614. (lambda (k)
  1615. (with-exception-handler
  1616. (lambda (x)
  1617. (display "reraised " out)
  1618. (write x out) (display #\! out)
  1619. (k 'zero))
  1620. (lambda ()
  1621. (guard (condition
  1622. ((positive? condition)
  1623. 'positive)
  1624. ((negative? condition)
  1625. 'negative))
  1626. (raise v)))))))
  1627. ;; From SRFI-34 "Examples" section - #5
  1628. (let* ((out (open-output-string))
  1629. (value (test-exception-handler-4 1 out)))
  1630. (test "" (get-output-string out))
  1631. (test 'positive value))
  1632. ;; From SRFI-34 "Examples" section - #6
  1633. (let* ((out (open-output-string))
  1634. (value (test-exception-handler-4 -1 out)))
  1635. (test "" (get-output-string out))
  1636. (test 'negative value))
  1637. ;; From SRFI-34 "Examples" section - #7
  1638. (let* ((out (open-output-string))
  1639. (value (test-exception-handler-4 0 out)))
  1640. (test "reraised 0!" (get-output-string out))
  1641. (test 'zero value))
  1642. ;; From SRFI-34 "Examples" section - #8
  1643. (test 42
  1644. (guard (condition
  1645. ((assq 'a condition) => cdr)
  1646. ((assq 'b condition)))
  1647. (raise (list (cons 'a 42)))))
  1648. ;; From SRFI-34 "Examples" section - #9
  1649. (test '(b . 23)
  1650. (guard (condition
  1651. ((assq 'a condition) => cdr)
  1652. ((assq 'b condition)))
  1653. (raise (list (cons 'b 23)))))
  1654. (test 'caught-d
  1655. (guard (condition
  1656. ((assq 'c condition) 'caught-c)
  1657. ((assq 'd condition) 'caught-d))
  1658. (list
  1659. (sqrt 8)
  1660. (guard (condition
  1661. ((assq 'a condition) => cdr)
  1662. ((assq 'b condition)))
  1663. (raise (list (cons 'd 24)))))))
  1664. (test-end)
  1665. (test-begin "6.12 Environments and evaluation")
  1666. ;; (test 21 (eval '(* 7 3) (scheme-report-environment 5)))
  1667. (test 20
  1668. (let ((f (eval '(lambda (f x) (f x x)) (null-environment 5))))
  1669. (f + 10)))
  1670. (test 1024 (eval '(expt 2 10) (environment '(scheme base))))
  1671. ;; (sin 0) may return exact number
  1672. (test 0.0 (inexact (eval '(sin 0) (environment '(scheme inexact)))))
  1673. ;; ditto
  1674. (test 1024.0 (eval '(+ (expt 2 10) (inexact (sin 0)))
  1675. (environment '(scheme base) '(scheme inexact))))
  1676. (test-end)
  1677. (test-begin "6.13 Input and output")
  1678. (test #t (port? (current-input-port)))
  1679. (test #t (input-port? (current-input-port)))
  1680. (test #t (output-port? (current-output-port)))
  1681. (test #t (output-port? (current-error-port)))
  1682. (test #t (input-port? (open-input-string "abc")))
  1683. (test #t (output-port? (open-output-string)))
  1684. (test #t (textual-port? (open-input-string "abc")))
  1685. (test #t (textual-port? (open-output-string)))
  1686. (test #t (binary-port? (open-input-bytevector #u8(0 1 2))))
  1687. (test #t (binary-port? (open-output-bytevector)))
  1688. (test #t (input-port-open? (open-input-string "abc")))
  1689. (test #t (output-port-open? (open-output-string)))
  1690. (test #f
  1691. (let ((in (open-input-string "abc")))
  1692. (close-input-port in)
  1693. (input-port-open? in)))
  1694. (test #f
  1695. (let ((out (open-output-string)))
  1696. (close-output-port out)
  1697. (output-port-open? out)))
  1698. (test #f
  1699. (let ((out (open-output-string)))
  1700. (close-port out)
  1701. (output-port-open? out)))
  1702. (test 'error
  1703. (let ((in (open-input-string "abc")))
  1704. (close-input-port in)
  1705. (guard (exn (else 'error)) (read-char in))))
  1706. (test 'error
  1707. (let ((out (open-output-string)))
  1708. (close-output-port out)
  1709. (guard (exn (else 'error)) (write-char #\c out))))
  1710. (test #t (eof-object? (eof-object)))
  1711. (test #t (eof-object? (read (open-input-string ""))))
  1712. (test #t (char-ready? (open-input-string "42")))
  1713. (test 42 (read (open-input-string " 42 ")))
  1714. (test #t (eof-object? (read-char (open-input-string ""))))
  1715. (test #\a (read-char (open-input-string "abc")))
  1716. (test #t (eof-object? (read-line (open-input-string ""))))
  1717. (test "abc" (read-line (open-input-string "abc")))
  1718. (test "abc" (read-line (open-input-string "abc\ndef\n")))
  1719. (test #t (eof-object? (read-string 3 (open-input-string ""))))
  1720. (test "abc" (read-string 3 (open-input-string "abcd")))
  1721. (test "abc" (read-string 3 (open-input-string "abc\ndef\n")))
  1722. (let ((in (open-input-string (string #\x10F700 #\x10F701 #\x10F702))))
  1723. (let* ((c0 (peek-char in))
  1724. (c1 (read-char in))
  1725. (c2 (read-char in))
  1726. (c3 (read-char in)))
  1727. (test #\x10F700 c0)
  1728. (test #\x10F700 c1)
  1729. (test #\x10F701 c2)
  1730. (test #\x10F702 c3)))
  1731. (test (string #\x10F700)
  1732. (let ((out (open-output-string)))
  1733. (write-char #\x10F700 out)
  1734. (get-output-string out)))
  1735. (test "abc"
  1736. (let ((out (open-output-string)))
  1737. (write 'abc out)
  1738. (get-output-string out)))
  1739. (test "abc def"
  1740. (let ((out (open-output-string)))
  1741. (display "abc def" out)
  1742. (get-output-string out)))
  1743. (test "abc"
  1744. (let ((out (open-output-string)))
  1745. (display #\a out)
  1746. (display "b" out)
  1747. (display #\c out)
  1748. (get-output-string out)))
  1749. (test #t
  1750. (let* ((out (open-output-string))
  1751. (r (begin (newline out) (get-output-string out))))
  1752. (or (equal? r "\n") (equal? r "\r\n"))))
  1753. (test "abc def"
  1754. (let ((out (open-output-string)))
  1755. (write-string "abc def" out)
  1756. (get-output-string out)))
  1757. (test "def"
  1758. (let ((out (open-output-string)))
  1759. (write-string "abc def" out 4)
  1760. (get-output-string out)))
  1761. (test "c d"
  1762. (let ((out (open-output-string)))
  1763. (write-string "abc def" out 2 5)
  1764. (get-output-string out)))
  1765. (test ""
  1766. (let ((out (open-output-string)))
  1767. (flush-output-port out)
  1768. (get-output-string out)))
  1769. (test #t (eof-object? (read-u8 (open-input-bytevector #u8()))))
  1770. (test 1 (read-u8 (open-input-bytevector #u8(1 2 3))))
  1771. (test #t (eof-object? (read-bytevector 3 (open-input-bytevector #u8()))))
  1772. (test #t (u8-ready? (open-input-bytevector #u8(1))))
  1773. (test #u8(1) (read-bytevector 3 (open-input-bytevector #u8(1))))
  1774. (test #u8(1 2) (read-bytevector 3 (open-input-bytevector #u8(1 2))))
  1775. (test #u8(1 2 3) (read-bytevector 3 (open-input-bytevector #u8(1 2 3))))
  1776. (test #u8(1 2 3) (read-bytevector 3 (open-input-bytevector #u8(1 2 3 4))))
  1777. (test #t
  1778. (let ((bv (bytevector 1 2 3 4 5)))
  1779. (eof-object? (read-bytevector! bv (open-input-bytevector #u8())))))
  1780. (test #u8(6 7 8 9 10)
  1781. (let ((bv (bytevector 1 2 3 4 5)))
  1782. (read-bytevector! bv (open-input-bytevector #u8(6 7 8 9 10)) 0 5)
  1783. bv))
  1784. (test #u8(6 7 8 4 5)
  1785. (let ((bv (bytevector 1 2 3 4 5)))
  1786. (read-bytevector! bv (open-input-bytevector #u8(6 7 8 9 10)) 0 3)
  1787. bv))
  1788. (test #u8(1 2 3 6 5)
  1789. (let ((bv (bytevector 1 2 3 4 5)))
  1790. (read-bytevector! bv (open-input-bytevector #u8(6 7 8 9 10)) 3 4)
  1791. bv))
  1792. (test #u8(1 2 3)
  1793. (let ((out (open-output-bytevector)))
  1794. (write-u8 1 out)
  1795. (write-u8 2 out)
  1796. (write-u8 3 out)
  1797. (get-output-bytevector out)))
  1798. (test #u8(1 2 3 4 5)
  1799. (let ((out (open-output-bytevector)))
  1800. (write-bytevector #u8(1 2 3 4 5) out)
  1801. (get-output-bytevector out)))
  1802. (test #u8(3 4 5)
  1803. (let ((out (open-output-bytevector)))
  1804. (write-bytevector #u8(1 2 3 4 5) out 2)
  1805. (get-output-bytevector out)))
  1806. (test #u8(3 4)
  1807. (let ((out (open-output-bytevector)))
  1808. (write-bytevector #u8(1 2 3 4 5) out 2 4)
  1809. (get-output-bytevector out)))
  1810. (test #u8()
  1811. (let ((out (open-output-bytevector)))
  1812. (flush-output-port out)
  1813. (get-output-bytevector out)))
  1814. (test #t
  1815. (and (member
  1816. (let ((out (open-output-string))
  1817. (x (list 1)))
  1818. (set-cdr! x x)
  1819. (write-shared x out)
  1820. (get-output-string out))
  1821. ;; labels not guaranteed to be 0 indexed, spacing may differ
  1822. '("#0=(1 . #0#)" "#1=(1 . #1#)"))
  1823. #t))
  1824. (test "((1 2 3) (1 2 3))"
  1825. (let ((out (open-output-string))
  1826. (x (list 1 2 3)))
  1827. (write (list x x) out)
  1828. (get-output-string out)))
  1829. (test "((1 2 3) (1 2 3))"
  1830. (let ((out (open-output-string))
  1831. (x (list 1 2 3)))
  1832. (write-simple (list x x) out)
  1833. (get-output-string out)))
  1834. (test #t
  1835. (and (member (let ((out (open-output-string))
  1836. (x (list 1 2 3)))
  1837. (write-shared (list x x) out)
  1838. (get-output-string out))
  1839. '("(#0=(1 2 3) #0#)" "(#1=(1 2 3) #1#)"))
  1840. #t))
  1841. (test-begin "Read syntax")
  1842. ;; check reading boolean followed by eof
  1843. (test #t (read (open-input-string "#t")))
  1844. (test #t (read (open-input-string "#true")))
  1845. (test #f (read (open-input-string "#f")))
  1846. (test #f (read (open-input-string "#false")))
  1847. (define (read2 port)
  1848. (let* ((o1 (read port)) (o2 (read port)))
  1849. (cons o1 o2)))
  1850. ;; check reading boolean followed by delimiter
  1851. (test '(#t . (5)) (read2 (open-input-string "#t(5)")))
  1852. (test '(#t . 6) (read2 (open-input-string "#true 6 ")))
  1853. (test '(#f . 7) (read2 (open-input-string "#f 7")))
  1854. (test '(#f . "8") (read2 (open-input-string "#false\"8\"")))
  1855. (test '() (read (open-input-string "()")))
  1856. (test '(1 2) (read (open-input-string "(1 2)")))
  1857. (test '(1 . 2) (read (open-input-string "(1 . 2)")))
  1858. (test '(1 2) (read (open-input-string "(1 . (2))")))
  1859. (test '(1 2 3 4 5) (read (open-input-string "(1 . (2 3 4 . (5)))")))
  1860. (failing-test-with-exception
  1861. "https://bugs.gnu.org/38236"
  1862. '1 (cadr (read (open-input-string "#0=(1 . #0#)"))))
  1863. (failing-test-with-exception
  1864. "https://bugs.gnu.org/38236"
  1865. '(1 2 3) (cadr (read (open-input-string "(#0=(1 2 3) #0#)"))))
  1866. (test '(quote (1 2)) (read (open-input-string "'(1 2)")))
  1867. (test '(quote (1 (unquote 2))) (read (open-input-string "'(1 ,2)")))
  1868. (test '(quote (1 (unquote-splicing 2))) (read (open-input-string "'(1 ,@2)")))
  1869. (test '(quasiquote (1 (unquote 2))) (read (open-input-string "`(1 ,2)")))
  1870. (test '#() (read (open-input-string "#()")))
  1871. (test '#(a b) (read (open-input-string "#(a b)")))
  1872. (test #u8() (read (open-input-string "#u8()")))
  1873. (test #u8(0 1) (read (open-input-string "#u8(0 1)")))
  1874. (test 'abc (read (open-input-string "abc")))
  1875. (test 'abc (read (open-input-string "abc def")))
  1876. (test 'ABC (read (open-input-string "ABC")))
  1877. (test 'Hello (read (open-input-string "|H\\x65;llo|")))
  1878. (test 'abc (read (open-input-string "#!fold-case ABC")))
  1879. (test 'ABC (read (open-input-string "#!fold-case #!no-fold-case ABC")))
  1880. (test 'def (read (open-input-string "#; abc def")))
  1881. (test 'def (read (open-input-string "; abc \ndef")))
  1882. (test 'def (read (open-input-string "#| abc |# def")))
  1883. (test 'ghi (read (open-input-string "#| abc #| def |# |# ghi")))
  1884. (test 'ghi (read (open-input-string "#; ; abc\n def ghi")))
  1885. (test '(abs -16) (read (open-input-string "(#;sqrt abs -16)")))
  1886. (test '(a d) (read (open-input-string "(a #; #;b c d)")))
  1887. (test '(a e) (read (open-input-string "(a #;(b #;c d) e)")))
  1888. (test '(a . c) (read (open-input-string "(a . #;b c)")))
  1889. (test '(a . b) (read (open-input-string "(a . b #;c)")))
  1890. (define (test-read-error str)
  1891. (test-assert str
  1892. (guard (exn (else #t))
  1893. (read (open-input-string str))
  1894. #f)))
  1895. ;; These should all use test-read-error instead.
  1896. (failing-test "https://bugs.gnu.org/38238" #f "(#;a . b)")
  1897. (failing-test "https://bugs.gnu.org/38238" #f "(a . #;b)")
  1898. (failing-test "https://bugs.gnu.org/38238" #f "(a #;. b)")
  1899. (failing-test "https://bugs.gnu.org/38238" #f "(#;x #;y . z)")
  1900. (failing-test "https://bugs.gnu.org/38238" #f "(#; #;x #;y . z)")
  1901. (failing-test "https://bugs.gnu.org/38238" #f "(#; #;x . z)")
  1902. (test #\a (read (open-input-string "#\\a")))
  1903. (test #\space (read (open-input-string "#\\space")))
  1904. (test 0 (char->integer (read (open-input-string "#\\null"))))
  1905. (test 7 (char->integer (read (open-input-string "#\\alarm"))))
  1906. (test 8 (char->integer (read (open-input-string "#\\backspace"))))
  1907. (test 9 (char->integer (read (open-input-string "#\\tab"))))
  1908. (test 10 (char->integer (read (open-input-string "#\\newline"))))
  1909. (test 13 (char->integer (read (open-input-string "#\\return"))))
  1910. (test #x7F (char->integer (read (open-input-string "#\\delete"))))
  1911. (test #x1B (char->integer (read (open-input-string "#\\escape"))))
  1912. (test #x03BB (char->integer (read (open-input-string "#\\λ"))))
  1913. (test #x03BB (char->integer (read (open-input-string "#\\x03BB"))))
  1914. (test "abc" (read (open-input-string "\"abc\"")))
  1915. (test "abc" (read (open-input-string "\"abc\" \"def\"")))
  1916. (test "ABC" (read (open-input-string "\"ABC\"")))
  1917. (test "Hello" (read (open-input-string "\"H\\x65;llo\"")))
  1918. (test 7 (char->integer (string-ref (read (open-input-string "\"\\a\"")) 0)))
  1919. (test 8 (char->integer (string-ref (read (open-input-string "\"\\b\"")) 0)))
  1920. (test 9 (char->integer (string-ref (read (open-input-string "\"\\t\"")) 0)))
  1921. (test 10 (char->integer (string-ref (read (open-input-string "\"\\n\"")) 0)))
  1922. (test 13 (char->integer (string-ref (read (open-input-string "\"\\r\"")) 0)))
  1923. (test #x22 (char->integer (string-ref (read (open-input-string "\"\\\"\"")) 0)))
  1924. (test #x7C (char->integer (string-ref (read (open-input-string "\"\\|\"")) 0)))
  1925. (test "line 1\nline 2\n" (read (open-input-string "\"line 1\nline 2\n\"")))
  1926. (test "line 1continued\n" (read (open-input-string "\"line 1\\\ncontinued\n\"")))
  1927. (failing-test-with-exception
  1928. "https://bugs.gnu.org/38239"
  1929. "line 1continued\n" (read (open-input-string "\"line 1\\ \ncontinued\n\"")))
  1930. (test "line 1continued\n" (read (open-input-string "\"line 1\\\n continued\n\"")))
  1931. (failing-test-with-exception
  1932. "https://bugs.gnu.org/38239"
  1933. "line 1continued\n" (read (open-input-string "\"line 1\\ \t \n \t continued\n\"")))
  1934. (failing-test-with-exception
  1935. "https://bugs.gnu.org/38239"
  1936. "line 1\n\nline 3\n" (read (open-input-string "\"line 1\\ \t \n \t \n\nline 3\n\"")))
  1937. (test #x03BB (char->integer (string-ref (read (open-input-string "\"\\x03BB;\"")) 0)))
  1938. (define-syntax test-write-syntax
  1939. (syntax-rules ()
  1940. ((test-write-syntax expect-str obj-expr)
  1941. (let ((out (open-output-string)))
  1942. (write obj-expr out)
  1943. (test expect-str (get-output-string out))))))
  1944. ;; (test-write-syntax "|.|" '|.|)
  1945. ;; (test-write-syntax "|a b|" '|a b|)
  1946. ;; (test-write-syntax "|,a|" '|,a|)
  1947. ;; (test-write-syntax "|\"|" '|\"|)
  1948. ;; (test-write-syntax "a" '|a|)
  1949. ;; ;; (test-write-syntax "a.b" '|a.b|)
  1950. ;; (test-write-syntax "|2|" '|2|)
  1951. ;; (test-write-syntax "|+3|" '|+3|)
  1952. ;; (test-write-syntax "|-.4|" '|-.4|)
  1953. ;; (test-write-syntax "|+i|" '|+i|)
  1954. ;; (test-write-syntax "|-i|" '|-i|)
  1955. ;; (test-write-syntax "|+inf.0|" '|+inf.0|)
  1956. ;; (test-write-syntax "|-inf.0|" '|-inf.0|)
  1957. ;; (test-write-syntax "|+nan.0|" '|+nan.0|)
  1958. ;; (test-write-syntax "|+NaN.0|" '|+NaN.0|)
  1959. ;; (test-write-syntax "|+NaN.0abc|" '|+NaN.0abc|)
  1960. (test-end)
  1961. (test-begin "Numeric syntax")
  1962. ;; Numeric syntax adapted from Peter Bex's tests.
  1963. ;;
  1964. ;; These are updated to R7RS, using string ports instead of
  1965. ;; string->number, and "error" tests removed because implementations
  1966. ;; are free to provide their own numeric extensions. Currently all
  1967. ;; tests are run by default - need to cond-expand and test for
  1968. ;; infinities and -0.0.
  1969. (define-syntax test-numeric-syntax
  1970. (syntax-rules ()
  1971. ((test-numeric-syntax str expect strs ...)
  1972. (let* ((z (read (open-input-string str)))
  1973. (out (open-output-string))
  1974. (z-str (begin (write z out) (get-output-string out))))
  1975. (test expect (values z))
  1976. (test #t (and (member z-str '(str strs ...)) #t))))))
  1977. ;; Each test is of the form:
  1978. ;;
  1979. ;; (test-numeric-syntax input-str expected-value expected-write-values ...)
  1980. ;;
  1981. ;; where the input should be eqv? to the expected-value, and the
  1982. ;; written output the same as any of the expected-write-values. The
  1983. ;; form
  1984. ;;
  1985. ;; (test-numeric-syntax input-str expected-value)
  1986. ;;
  1987. ;; is a shorthand for
  1988. ;;
  1989. ;; (test-numeric-syntax input-str expected-value (input-str))
  1990. ;; Simple
  1991. (test-numeric-syntax "1" 1)
  1992. (test-numeric-syntax "+1" 1 "1")
  1993. (test-numeric-syntax "-1" -1)
  1994. (test-numeric-syntax "#i1" 1.0 "1.0" "1.")
  1995. (test-numeric-syntax "#I1" 1.0 "1.0" "1.")
  1996. (test-numeric-syntax "#i-1" -1.0 "-1.0" "-1.")
  1997. ;; Decimal
  1998. (test-numeric-syntax "1.0" 1.0 "1.0" "1.")
  1999. (test-numeric-syntax "1." 1.0 "1.0" "1.")
  2000. (test-numeric-syntax ".1" 0.1 "0.1" "100.0e-3")
  2001. (test-numeric-syntax "-.1" -0.1 "-0.1" "-100.0e-3")
  2002. ;; Some Schemes don't allow negative zero. This is okay with the standard
  2003. (test-numeric-syntax "-.0" -0.0 "-0." "-0.0" "0.0" "0." ".0")
  2004. (test-numeric-syntax "-0." -0.0 "-.0" "-0.0" "0.0" "0." ".0")
  2005. (test-numeric-syntax "#i1.0" 1.0 "1.0" "1.")
  2006. (test-numeric-syntax "#e1.0" 1 "1")
  2007. (test-numeric-syntax "#e-.0" 0 "0")
  2008. (test-numeric-syntax "#e-0." 0 "0")
  2009. ;; Decimal notation with suffix
  2010. (test-numeric-syntax "1e2" 100.0 "100.0" "100.")
  2011. (test-numeric-syntax "1E2" 100.0 "100.0" "100.")
  2012. (test-numeric-syntax "1s2" 100.0 "100.0" "100.")
  2013. (test-numeric-syntax "1S2" 100.0 "100.0" "100.")
  2014. (test-numeric-syntax "1f2" 100.0 "100.0" "100.")
  2015. (test-numeric-syntax "1F2" 100.0 "100.0" "100.")
  2016. (test-numeric-syntax "1d2" 100.0 "100.0" "100.")
  2017. (test-numeric-syntax "1D2" 100.0 "100.0" "100.")
  2018. (test-numeric-syntax "1l2" 100.0 "100.0" "100.")
  2019. (test-numeric-syntax "1L2" 100.0 "100.0" "100.")
  2020. ;; NaN, Inf
  2021. ;; (test-numeric-syntax "+nan.0" +nan.0 "+nan.0" "+NaN.0")
  2022. ;; (test-numeric-syntax "+NAN.0" +nan.0 "+nan.0" "+NaN.0")
  2023. (test-numeric-syntax "+inf.0" +inf.0 "+inf.0" "+Inf.0")
  2024. (test-numeric-syntax "+InF.0" +inf.0 "+inf.0" "+Inf.0")
  2025. (test-numeric-syntax "-inf.0" -inf.0 "-inf.0" "-Inf.0")
  2026. (test-numeric-syntax "-iNF.0" -inf.0 "-inf.0" "-Inf.0")
  2027. ;; (test-numeric-syntax "#i+nan.0" +nan.0 "+nan.0" "+NaN.0")
  2028. (test-numeric-syntax "#i+inf.0" +inf.0 "+inf.0" "+Inf.0")
  2029. (test-numeric-syntax "#i-inf.0" -inf.0 "-inf.0" "-Inf.0")
  2030. ;; Exact ratios
  2031. (test-numeric-syntax "1/2" (/ 1 2))
  2032. (test-numeric-syntax "#e1/2" (/ 1 2) "1/2")
  2033. (test-numeric-syntax "10/2" 5 "5")
  2034. (test-numeric-syntax "-1/2" (- (/ 1 2)))
  2035. (test-numeric-syntax "0/10" 0 "0")
  2036. (test-numeric-syntax "#e0/10" 0 "0")
  2037. (test-numeric-syntax "#i3/2" (/ 3.0 2.0) "1.5")
  2038. ;; Exact complex
  2039. (cond-expand
  2040. (exact-complex
  2041. (test-numeric-syntax "1+2i" (make-rectangular 1 2))
  2042. (test-numeric-syntax "1+2I" (make-rectangular 1 2) "1+2i")
  2043. (test-numeric-syntax "1-2i" (make-rectangular 1 -2))
  2044. (test-numeric-syntax "-1+2i" (make-rectangular -1 2))
  2045. (test-numeric-syntax "-1-2i" (make-rectangular -1 -2))
  2046. (test-numeric-syntax "+i" (make-rectangular 0 1) "+i" "+1i" "0+i" "0+1i")
  2047. (test-numeric-syntax "0+i" (make-rectangular 0 1) "+i" "+1i" "0+i" "0+1i")
  2048. (test-numeric-syntax "0+1i" (make-rectangular 0 1) "+i" "+1i" "0+i" "0+1i")
  2049. (test-numeric-syntax "-i" (make-rectangular 0 -1) "-i" "-1i" "0-i" "0-1i")
  2050. (test-numeric-syntax "0-i" (make-rectangular 0 -1) "-i" "-1i" "0-i" "0-1i")
  2051. (test-numeric-syntax "0-1i" (make-rectangular 0 -1) "-i" "-1i" "0-i" "0-1i")
  2052. (test-numeric-syntax "+2i" (make-rectangular 0 2) "2i" "+2i" "0+2i")
  2053. (test-numeric-syntax "-2i" (make-rectangular 0 -2) "-2i" "0-2i"))
  2054. (else #t))
  2055. ;; Decimal-notation complex numbers (rectangular notation)
  2056. (test-numeric-syntax "1.0+2i" (make-rectangular 1.0 2) "1.0+2.0i" "1.0+2i" "1.+2i" "1.+2.i")
  2057. (test-numeric-syntax "1+2.0i" (make-rectangular 1 2.0) "1.0+2.0i" "1+2.0i" "1.+2.i" "1+2.i")
  2058. (test-numeric-syntax "1e2+1.0i" (make-rectangular 100.0 1.0) "100.0+1.0i" "100.+1.i")
  2059. (test-numeric-syntax "1s2+1.0i" (make-rectangular 100.0 1.0) "100.0+1.0i" "100.+1.i")
  2060. (test-numeric-syntax "1.0+1e2i" (make-rectangular 1.0 100.0) "1.0+100.0i" "1.+100.i")
  2061. (test-numeric-syntax "1.0+1s2i" (make-rectangular 1.0 100.0) "1.0+100.0i" "1.+100.i")
  2062. ;; Fractional complex numbers (rectangular notation)
  2063. (cond-expand
  2064. (exact-complex
  2065. (test-numeric-syntax "1/2+3/4i" (make-rectangular (/ 1 2) (/ 3 4))))
  2066. (else #f))
  2067. ;; Mixed fractional/decimal notation complex numbers (rectangular notation)
  2068. (test-numeric-syntax "0.5+3/4i" (make-rectangular 0.5 (/ 3 4))
  2069. "0.5+0.75i" ".5+.75i" "0.5+3/4i" ".5+3/4i" "500.0e-3+750.0e-3i")
  2070. ;; Complex NaN, Inf (rectangular notation)
  2071. ;;(test-numeric-syntax "+nan.0+nan.0i" (make-rectangular the-nan the-nan) "+NaN.0+NaN.0i")
  2072. (test-numeric-syntax "+inf.0+inf.0i" (make-rectangular +inf.0 +inf.0) "+Inf.0+Inf.0i")
  2073. (test-numeric-syntax "-inf.0+inf.0i" (make-rectangular -inf.0 +inf.0) "-Inf.0+Inf.0i")
  2074. (test-numeric-syntax "-inf.0-inf.0i" (make-rectangular -inf.0 -inf.0) "-Inf.0-Inf.0i")
  2075. (test-numeric-syntax "+inf.0-inf.0i" (make-rectangular +inf.0 -inf.0) "+Inf.0-Inf.0i")
  2076. ;; Complex numbers (polar notation)
  2077. ;; Need to account for imprecision in write output.
  2078. ;;(test-numeric-syntax "1@2" -0.416146836547142+0.909297426825682i "-0.416146836547142+0.909297426825682i")
  2079. ;; Base prefixes
  2080. (test-numeric-syntax "#x11" 17 "17")
  2081. (test-numeric-syntax "#X11" 17 "17")
  2082. (test-numeric-syntax "#d11" 11 "11")
  2083. (test-numeric-syntax "#D11" 11 "11")
  2084. (test-numeric-syntax "#o11" 9 "9")
  2085. (test-numeric-syntax "#O11" 9 "9")
  2086. (test-numeric-syntax "#b11" 3 "3")
  2087. (test-numeric-syntax "#B11" 3 "3")
  2088. (test-numeric-syntax "#o7" 7 "7")
  2089. (test-numeric-syntax "#xa" 10 "10")
  2090. (test-numeric-syntax "#xA" 10 "10")
  2091. (test-numeric-syntax "#xf" 15 "15")
  2092. (test-numeric-syntax "#x-10" -16 "-16")
  2093. (test-numeric-syntax "#d-10" -10 "-10")
  2094. (test-numeric-syntax "#o-10" -8 "-8")
  2095. (test-numeric-syntax "#b-10" -2 "-2")
  2096. ;; Combination of prefixes
  2097. (test-numeric-syntax "#e#x10" 16 "16")
  2098. (test-numeric-syntax "#i#x10" 16.0 "16.0" "16.")
  2099. ;; (Attempted) decimal notation with base prefixes
  2100. (test-numeric-syntax "#d1." 1.0 "1.0" "1.")
  2101. (test-numeric-syntax "#d.1" 0.1 "0.1" ".1" "100.0e-3")
  2102. (test-numeric-syntax "#x1e2" 482 "482")
  2103. (test-numeric-syntax "#d1e2" 100.0 "100.0" "100.")
  2104. ;; Fractions with prefixes
  2105. (test-numeric-syntax "#x10/2" 8 "8")
  2106. (test-numeric-syntax "#x11/2" (/ 17 2) "17/2")
  2107. (test-numeric-syntax "#d11/2" (/ 11 2) "11/2")
  2108. (test-numeric-syntax "#o11/2" (/ 9 2) "9/2")
  2109. (test-numeric-syntax "#b11/10" (/ 3 2) "3/2")
  2110. ;; Complex numbers with prefixes
  2111. (test-numeric-syntax "#d1.0+1.0i" (make-rectangular 1.0 1.0) "1.0+1.0i" "1.+1.i")
  2112. (test-numeric-syntax "#i1.0+1.0i" (make-rectangular 1.0 1.0) "1.0+1.0i" "1.+1.i")
  2113. (cond-expand
  2114. (exact-complex
  2115. (test-numeric-syntax "#x10+11i" (make-rectangular 16 17) "16+17i")
  2116. (test-numeric-syntax "#d10+11i" (make-rectangular 10 11) "10+11i")
  2117. (test-numeric-syntax "#o10+11i" (make-rectangular 8 9) "8+9i")
  2118. (test-numeric-syntax "#b10+11i" (make-rectangular 2 3) "2+3i")
  2119. (test-numeric-syntax "#e1.0+1.0i" (make-rectangular 1 1) "1+1i" "1+i"))
  2120. (else #f))
  2121. ;; (define-syntax test-precision
  2122. ;; (syntax-rules ()
  2123. ;; ((test-round-trip str alt ...)
  2124. ;; (let* ((n (string->number str))
  2125. ;; (str2 (number->string n))
  2126. ;; (accepted (list str alt ...))
  2127. ;; (ls (member str2 accepted)))
  2128. ;; (test-assert (string-append "(member? " str2 " "
  2129. ;; (let ((out (open-output-string)))
  2130. ;; (write accepted out)
  2131. ;; (get-output-string out))
  2132. ;; ")")
  2133. ;; (pair? ls))
  2134. ;; (when (pair? ls)
  2135. ;; (test-assert (string-append "(eqv?: " str " " str2 ")")
  2136. ;; (eqv? n (string->number (car ls)))))))))
  2137. ;; (test-precision "-1.7976931348623157e+308" "-inf.0")
  2138. ;; (test-precision "4.940656458412465e-324" "4.94065645841247e-324" "5.0e-324" "0.0")
  2139. ;; (test-precision "9.881312916824931e-324" "9.88131291682493e-324" "1.0e-323" "0.0")
  2140. ;; (test-precision "1.48219693752374e-323" "1.5e-323" "0.0")
  2141. ;; (test-precision "1.976262583364986e-323" "1.97626258336499e-323" "2.0e-323" "0.0")
  2142. ;; (test-precision "2.470328229206233e-323" "2.47032822920623e-323" "2.5e-323" "0.0")
  2143. ;; (test-precision "2.420921664622108e-322" "2.42092166462211e-322" "2.4e-322" "0.0")
  2144. ;; (test-precision "2.420921664622108e-320" "2.42092166462211e-320" "2.421e-320" "0.0")
  2145. ;; (test-precision "1.4489974452386991" "1.4489975")
  2146. ;; (test-precision "0.14285714285714282" "0.14285714285714288" "0.14285715")
  2147. ;; (test-precision "1.7976931348623157e+308" "+inf.0")
  2148. (test-end)
  2149. (test-end)
  2150. (test-begin "6.14 System interface")
  2151. ;; 6.14 System interface
  2152. ;; (test "/usr/local/bin:/usr/bin:/bin" (get-environment-variable "PATH"))
  2153. (test #t (string? (get-environment-variable "PATH")))
  2154. ;; (test '(("USER" . "root") ("HOME" . "/")) (get-environment-variables))
  2155. (let ((env (get-environment-variables)))
  2156. (define (env-pair? x)
  2157. (and (pair? x) (string? (car x)) (string? (cdr x))))
  2158. (define (all? pred ls)
  2159. (or (null? ls) (and (pred (car ls)) (all? pred (cdr ls)))))
  2160. (test #t (list? env))
  2161. (test #t (all? env-pair? env)))
  2162. (test #t (list? (command-line)))
  2163. (test #t (real? (current-second)))
  2164. (test #t (inexact? (current-second)))
  2165. (test #t (exact? (current-jiffy)))
  2166. (test #t (exact? (jiffies-per-second)))
  2167. (test #t (list? (features)))
  2168. (test #t (and (memq 'r7rs (features)) #t))
  2169. (test #t (file-exists? "."))
  2170. (test #f (file-exists? " no such file "))
  2171. (failing-test
  2172. "https://bugs.gnu.org/38237"
  2173. #t (file-error?
  2174. (guard (exn (else exn))
  2175. (delete-file " no such file "))))
  2176. (test-end)
  2177. (test-end)
  2178. (undo-install-r7rs!)