r7rs-tests.scm 66 KB

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