srfi-1.test 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656
  1. ;;;; srfi-1.test --- Test suite for Guile's SRFI-1 functions. -*- scheme -*-
  2. ;;;;
  3. ;;;; Copyright 2003-2006, 2008-2011, 2014 Free Software Foundation, Inc.
  4. ;;;;
  5. ;;;; This library is free software; you can redistribute it and/or
  6. ;;;; modify it under the terms of the GNU Lesser General Public
  7. ;;;; License as published by the Free Software Foundation; either
  8. ;;;; version 3 of the License, or (at your option) any later version.
  9. ;;;;
  10. ;;;; This library is distributed in the hope that it will be useful,
  11. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;;;; Lesser General Public License for more details.
  14. ;;;;
  15. ;;;; You should have received a copy of the GNU Lesser General Public
  16. ;;;; License along with this library; if not, write to the Free Software
  17. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. (define-module (test-srfi-1)
  19. #:use-module (test-suite lib)
  20. #:use-module (srfi srfi-1))
  21. (define (ref-delete x lst . proc)
  22. "Reference implemenation of srfi-1 `delete'."
  23. (set! proc (if (null? proc) equal? (car proc)))
  24. (do ((ret '())
  25. (lst lst (cdr lst)))
  26. ((null? lst)
  27. (reverse! ret))
  28. (if (not (proc x (car lst)))
  29. (set! ret (cons (car lst) ret)))))
  30. (define (ref-delete-duplicates lst . proc)
  31. "Reference implemenation of srfi-1 `delete-duplicates'."
  32. (set! proc (if (null? proc) equal? (car proc)))
  33. (if (null? lst)
  34. '()
  35. (do ((keep '()))
  36. ((null? lst)
  37. (reverse! keep))
  38. (let ((elem (car lst)))
  39. (set! keep (cons elem keep))
  40. (set! lst (ref-delete elem lst proc))))))
  41. ;;
  42. ;; alist-copy
  43. ;;
  44. (with-test-prefix "alist-copy"
  45. ;; return a list which is the pairs making up alist A, the spine and cells
  46. (define (alist-pairs a)
  47. (let more ((a a)
  48. (result a))
  49. (if (pair? a)
  50. (more (cdr a) (cons a result))
  51. result)))
  52. ;; return a list of the elements common to lists X and Y, compared with eq?
  53. (define (common-elements x y)
  54. (if (null? x)
  55. '()
  56. (if (memq (car x) y)
  57. (cons (car x) (common-elements (cdr x) y))
  58. (common-elements (cdr x) y))))
  59. ;; validate an alist-copy of OLD to NEW
  60. ;; lists must be equal, and must comprise new pairs
  61. (define (valid-alist-copy? old new)
  62. (and (equal? old new)
  63. (null? (common-elements old new))))
  64. (pass-if-exception "too few args" exception:wrong-num-args
  65. (alist-copy))
  66. (pass-if-exception "too many args" exception:wrong-num-args
  67. (alist-copy '() '()))
  68. (let ((old '()))
  69. (pass-if old (valid-alist-copy? old (alist-copy old))))
  70. (let ((old '((1 . 2))))
  71. (pass-if old (valid-alist-copy? old (alist-copy old))))
  72. (let ((old '((1 . 2) (3 . 4))))
  73. (pass-if old (valid-alist-copy? old (alist-copy old))))
  74. (let ((old '((1 . 2) (3 . 4) (5 . 6))))
  75. (pass-if old (valid-alist-copy? old (alist-copy old)))))
  76. ;;
  77. ;; alist-delete
  78. ;;
  79. (with-test-prefix "alist-delete"
  80. (pass-if "equality call arg order"
  81. (let ((good #f))
  82. (alist-delete 'k '((ak . 123))
  83. (lambda (k ak)
  84. (if (and (eq? k 'k) (eq? ak 'ak))
  85. (set! good #t))))
  86. good))
  87. (pass-if "delete keys greater than 5"
  88. (equal? '((4 . x) (5 . y))
  89. (alist-delete 5 '((4 . x) (5 . y) (6 . z)) <)))
  90. (pass-if "empty"
  91. (equal? '() (alist-delete 'x '())))
  92. (pass-if "(y)"
  93. (equal? '() (alist-delete 'y '((y . 1)))))
  94. (pass-if "(n)"
  95. (equal? '((n . 1)) (alist-delete 'y '((n . 1)))))
  96. (pass-if "(y y)"
  97. (equal? '() (alist-delete 'y '((y . 1) (y . 2)))))
  98. (pass-if "(n y)"
  99. (equal? '((n . 1)) (alist-delete 'y '((n . 1) (y . 2)))))
  100. (pass-if "(y n)"
  101. (equal? '((n . 2)) (alist-delete 'y '((y . 1) (n . 2)))))
  102. (pass-if "(n n)"
  103. (equal? '((n . 1) (n . 2)) (alist-delete 'y '((n . 1) (n . 2)))))
  104. (pass-if "(y y y)"
  105. (equal? '() (alist-delete 'y '((y . 1) (y . 2) (y . 3)))))
  106. (pass-if "(n y y)"
  107. (equal? '((n . 1)) (alist-delete 'y '((n . 1) (y . 2) (y . 3)))))
  108. (pass-if "(y n y)"
  109. (equal? '((n . 2)) (alist-delete 'y '((y . 1) (n . 2) (y . 3)))))
  110. (pass-if "(n n y)"
  111. (equal? '((n . 1) (n . 2)) (alist-delete 'y '((n . 1) (n . 2) (y . 3)))))
  112. (pass-if "(y y n)"
  113. (equal? '( (n . 3)) (alist-delete 'y '((y . 1) (y . 2) (n . 3)))))
  114. (pass-if "(n y n)"
  115. (equal? '((n . 1) (n . 3)) (alist-delete 'y '((n . 1) (y . 2) (n . 3)))))
  116. (pass-if "(y n n)"
  117. (equal? '((n . 2) (n . 3)) (alist-delete 'y '((y . 1) (n . 2) (n . 3)))))
  118. (pass-if "(n n n)"
  119. (equal? '((n . 1) (n . 2) (n . 3)) (alist-delete 'y '((n . 1) (n . 2) (n . 3))))))
  120. ;;
  121. ;; append-map
  122. ;;
  123. (with-test-prefix "append-map"
  124. (with-test-prefix "one list"
  125. (pass-if "()"
  126. (equal? '() (append-map noop '(()))))
  127. (pass-if "(1)"
  128. (equal? '(1) (append-map noop '((1)))))
  129. (pass-if "(1 2)"
  130. (equal? '(1 2) (append-map noop '((1 2)))))
  131. (pass-if "() ()"
  132. (equal? '() (append-map noop '(() ()))))
  133. (pass-if "() (1)"
  134. (equal? '(1) (append-map noop '(() (1)))))
  135. (pass-if "() (1 2)"
  136. (equal? '(1 2) (append-map noop '(() (1 2)))))
  137. (pass-if "(1) (2)"
  138. (equal? '(1 2) (append-map noop '((1) (2)))))
  139. (pass-if "(1 2) ()"
  140. (equal? '(1 2) (append-map noop '(() (1 2))))))
  141. (with-test-prefix "two lists"
  142. (pass-if "() / 9"
  143. (equal? '() (append-map noop '(()) '(9))))
  144. (pass-if "(1) / 9"
  145. (equal? '(1) (append-map noop '((1)) '(9))))
  146. (pass-if "() () / 9 9"
  147. (equal? '() (append-map noop '(() ()) '(9 9))))
  148. (pass-if "(1) (2) / 9"
  149. (equal? '(1) (append-map noop '((1) (2)) '(9))))
  150. (pass-if "(1) (2) / 9 9"
  151. (equal? '(1 2) (append-map noop '((1) (2)) '(9 9))))))
  152. ;;
  153. ;; append-reverse
  154. ;;
  155. (with-test-prefix "append-reverse"
  156. ;; return a list which is the cars and cdrs of LST
  157. (define (list-contents lst)
  158. (if (null? lst)
  159. '()
  160. (cons* (car lst) (cdr lst) (list-contents (cdr lst)))))
  161. (define (valid-append-reverse revhead tail want)
  162. (let ((revhead-contents (list-contents revhead))
  163. (got (append-reverse revhead tail)))
  164. (and (equal? got want)
  165. ;; revhead unchanged
  166. (equal? revhead-contents (list-contents revhead)))))
  167. (pass-if-exception "too few args (0)" exception:wrong-num-args
  168. (append-reverse))
  169. (pass-if-exception "too few args (1)" exception:wrong-num-args
  170. (append-reverse '(x)))
  171. (pass-if-exception "too many args (3)" exception:wrong-num-args
  172. (append-reverse '() '() #f))
  173. (pass-if (valid-append-reverse '() '() '()))
  174. (pass-if (valid-append-reverse '() '(1 2 3) '(1 2 3)))
  175. (pass-if (valid-append-reverse '(1) '() '(1)))
  176. (pass-if (valid-append-reverse '(1) '(2) '(1 2)))
  177. (pass-if (valid-append-reverse '(1) '(2 3) '(1 2 3)))
  178. (pass-if (valid-append-reverse '(1 2) '() '(2 1)))
  179. (pass-if (valid-append-reverse '(1 2) '(3) '(2 1 3)))
  180. (pass-if (valid-append-reverse '(1 2) '(3 4) '(2 1 3 4)))
  181. (pass-if (valid-append-reverse '(1 2 3) '() '(3 2 1)))
  182. (pass-if (valid-append-reverse '(1 2 3) '(4) '(3 2 1 4)))
  183. (pass-if (valid-append-reverse '(1 2 3) '(4 5) '(3 2 1 4 5))))
  184. ;;
  185. ;; append-reverse!
  186. ;;
  187. (with-test-prefix "append-reverse!"
  188. (pass-if-exception "too few args (0)" exception:wrong-num-args
  189. (append-reverse!))
  190. (pass-if-exception "too few args (1)" exception:wrong-num-args
  191. (append-reverse! '(x)))
  192. (pass-if-exception "too many args (3)" exception:wrong-num-args
  193. (append-reverse! '() '() #f))
  194. (pass-if (equal? '() (append-reverse! '() '())))
  195. (pass-if (equal? '(1 2 3) (append-reverse! '() '(1 2 3))))
  196. (pass-if (equal? '(1) (append-reverse! '(1) '())))
  197. (pass-if (equal? '(1 2) (append-reverse! '(1) '(2))))
  198. (pass-if (equal? '(1 2 3) (append-reverse! '(1) '(2 3))))
  199. (pass-if (equal? '(2 1) (append-reverse! '(1 2) '())))
  200. (pass-if (equal? '(2 1 3) (append-reverse! '(1 2) '(3))))
  201. (pass-if (equal? '(2 1 3 4) (append-reverse! '(1 2) '(3 4))))
  202. (pass-if (equal? '(3 2 1) (append-reverse! '(1 2 3) '())))
  203. (pass-if (equal? '(3 2 1 4) (append-reverse! '(1 2 3) '(4))))
  204. (pass-if (equal? '(3 2 1 4 5) (append-reverse! '(1 2 3) '(4 5)))))
  205. ;;
  206. ;; assoc
  207. ;;
  208. (with-test-prefix "assoc"
  209. (pass-if "not found"
  210. (let ((alist '((a . 1)
  211. (b . 2)
  212. (c . 3))))
  213. (eqv? #f (assoc 'z alist))))
  214. (pass-if "found"
  215. (let ((alist '((a . 1)
  216. (b . 2)
  217. (c . 3))))
  218. (eqv? (second alist) (assoc 'b alist))))
  219. ;; this was wrong in guile 1.8.0 (a gremlin newly introduced in the 1.8
  220. ;; series, 1.6.x and earlier was ok)
  221. (pass-if "= arg order"
  222. (let ((alist '((b . 1)))
  223. (good #f))
  224. (assoc 'a alist (lambda (x y)
  225. (set! good (and (eq? x 'a)
  226. (eq? y 'b)))))
  227. good))
  228. ;; likewise this one bad in guile 1.8.0
  229. (pass-if "srfi-1 example <"
  230. (let ((alist '((1 . a)
  231. (5 . b)
  232. (6 . c))))
  233. (eq? (third alist) (assoc 5 alist <)))))
  234. ;;
  235. ;; break
  236. ;;
  237. (with-test-prefix "break"
  238. (define (test-break lst want-v1 want-v2)
  239. (call-with-values
  240. (lambda ()
  241. (break negative? lst))
  242. (lambda (got-v1 got-v2)
  243. (and (equal? got-v1 want-v1)
  244. (equal? got-v2 want-v2)))))
  245. (pass-if "empty"
  246. (test-break '() '() '()))
  247. (pass-if "y"
  248. (test-break '(1) '(1) '()))
  249. (pass-if "n"
  250. (test-break '(-1) '() '(-1)))
  251. (pass-if "yy"
  252. (test-break '(1 2) '(1 2) '()))
  253. (pass-if "ny"
  254. (test-break '(-1 1) '() '(-1 1)))
  255. (pass-if "yn"
  256. (test-break '(1 -1) '(1) '(-1)))
  257. (pass-if "nn"
  258. (test-break '(-1 -2) '() '(-1 -2)))
  259. (pass-if "yyy"
  260. (test-break '(1 2 3) '(1 2 3) '()))
  261. (pass-if "nyy"
  262. (test-break '(-1 1 2) '() '(-1 1 2)))
  263. (pass-if "yny"
  264. (test-break '(1 -1 2) '(1) '(-1 2)))
  265. (pass-if "nny"
  266. (test-break '(-1 -2 1) '() '(-1 -2 1)))
  267. (pass-if "yyn"
  268. (test-break '(1 2 -1) '(1 2) '(-1)))
  269. (pass-if "nyn"
  270. (test-break '(-1 1 -2) '() '(-1 1 -2)))
  271. (pass-if "ynn"
  272. (test-break '(1 -1 -2) '(1) '(-1 -2)))
  273. (pass-if "nnn"
  274. (test-break '(-1 -2 -3) '() '(-1 -2 -3))))
  275. ;;
  276. ;; break!
  277. ;;
  278. (with-test-prefix "break!"
  279. (define (test-break! lst want-v1 want-v2)
  280. (call-with-values
  281. (lambda ()
  282. (break! negative? lst))
  283. (lambda (got-v1 got-v2)
  284. (and (equal? got-v1 want-v1)
  285. (equal? got-v2 want-v2)))))
  286. (pass-if "empty"
  287. (test-break! '() '() '()))
  288. (pass-if "y"
  289. (test-break! (list 1) '(1) '()))
  290. (pass-if "n"
  291. (test-break! (list -1) '() '(-1)))
  292. (pass-if "yy"
  293. (test-break! (list 1 2) '(1 2) '()))
  294. (pass-if "ny"
  295. (test-break! (list -1 1) '() '(-1 1)))
  296. (pass-if "yn"
  297. (test-break! (list 1 -1) '(1) '(-1)))
  298. (pass-if "nn"
  299. (test-break! (list -1 -2) '() '(-1 -2)))
  300. (pass-if "yyy"
  301. (test-break! (list 1 2 3) '(1 2 3) '()))
  302. (pass-if "nyy"
  303. (test-break! (list -1 1 2) '() '(-1 1 2)))
  304. (pass-if "yny"
  305. (test-break! (list 1 -1 2) '(1) '(-1 2)))
  306. (pass-if "nny"
  307. (test-break! (list -1 -2 1) '() '(-1 -2 1)))
  308. (pass-if "yyn"
  309. (test-break! (list 1 2 -1) '(1 2) '(-1)))
  310. (pass-if "nyn"
  311. (test-break! (list -1 1 -2) '() '(-1 1 -2)))
  312. (pass-if "ynn"
  313. (test-break! (list 1 -1 -2) '(1) '(-1 -2)))
  314. (pass-if "nnn"
  315. (test-break! (list -1 -2 -3) '() '(-1 -2 -3))))
  316. ;;
  317. ;; car+cdr
  318. ;;
  319. (with-test-prefix "car+cdr"
  320. (pass-if "(1 . 2)"
  321. (call-with-values
  322. (lambda ()
  323. (car+cdr '(1 . 2)))
  324. (lambda (x y)
  325. (and (eqv? x 1)
  326. (eqv? y 2))))))
  327. ;;
  328. ;; concatenate and concatenate!
  329. ;;
  330. (let ()
  331. (define (common-tests concatenate-proc unmodified?)
  332. (define (try lstlst want)
  333. (let ((lstlst-copy (copy-tree lstlst))
  334. (got (concatenate-proc lstlst)))
  335. (if unmodified?
  336. (if (not (equal? lstlst lstlst-copy))
  337. (error "input lists modified")))
  338. (equal? got want)))
  339. (pass-if-exception "too few args" exception:wrong-num-args
  340. (concatenate-proc))
  341. (pass-if-exception "too many args" exception:wrong-num-args
  342. (concatenate-proc '() '()))
  343. (pass-if-exception "number" exception:wrong-type-arg
  344. (concatenate-proc 123))
  345. (pass-if-exception "vector" exception:wrong-type-arg
  346. (concatenate-proc #(1 2 3)))
  347. (pass-if "no lists"
  348. (try '() '()))
  349. (pass-if (try '((1)) '(1)))
  350. (pass-if (try '((1 2)) '(1 2)))
  351. (pass-if (try '(() (1)) '(1)))
  352. (pass-if (try '(() () (1)) '(1)))
  353. (pass-if (try '((1) (2)) '(1 2)))
  354. (pass-if (try '(() (1 2)) '(1 2)))
  355. (pass-if (try '((1) 2) '(1 . 2)))
  356. (pass-if (try '((1) (2) 3) '(1 2 . 3)))
  357. (pass-if (try '((1) (2) (3 . 4)) '(1 2 3 . 4)))
  358. )
  359. (with-test-prefix "concatenate"
  360. (common-tests concatenate #t))
  361. (with-test-prefix "concatenate!"
  362. (common-tests concatenate! #f)))
  363. ;;
  364. ;; count
  365. ;;
  366. (with-test-prefix "count"
  367. (pass-if-exception "no args" exception:wrong-num-args
  368. (count))
  369. (pass-if-exception "one arg" exception:wrong-num-args
  370. (count noop))
  371. (with-test-prefix "one list"
  372. (define (or1 x)
  373. x)
  374. (pass-if "empty list" (= 0 (count or1 '())))
  375. (pass-if-exception "pred arg count 0" exception:wrong-num-args
  376. (count (lambda () x) '(1 2 3)))
  377. (pass-if-exception "pred arg count 2" exception:wrong-num-args
  378. (count (lambda (x y) x) '(1 2 3)))
  379. (pass-if-exception "improper 1" exception:wrong-type-arg
  380. (count or1 1))
  381. (pass-if-exception "improper 2" exception:wrong-type-arg
  382. (count or1 '(1 . 2)))
  383. (pass-if-exception "improper 3" exception:wrong-type-arg
  384. (count or1 '(1 2 . 3)))
  385. (pass-if (= 0 (count or1 '(#f))))
  386. (pass-if (= 1 (count or1 '(#t))))
  387. (pass-if (= 0 (count or1 '(#f #f))))
  388. (pass-if (= 1 (count or1 '(#f #t))))
  389. (pass-if (= 1 (count or1 '(#t #f))))
  390. (pass-if (= 2 (count or1 '(#t #t))))
  391. (pass-if (= 0 (count or1 '(#f #f #f))))
  392. (pass-if (= 1 (count or1 '(#f #f #t))))
  393. (pass-if (= 1 (count or1 '(#t #f #f))))
  394. (pass-if (= 2 (count or1 '(#t #f #t))))
  395. (pass-if (= 3 (count or1 '(#t #t #t)))))
  396. (with-test-prefix "two lists"
  397. (define (or2 x y)
  398. (or x y))
  399. (pass-if "arg order"
  400. (= 1 (count (lambda (x y)
  401. (and (= 1 x)
  402. (= 2 y)))
  403. '(1) '(2))))
  404. (pass-if "empty lists" (= 0 (count or2 '() '())))
  405. (pass-if-exception "pred arg count 0" exception:wrong-num-args
  406. (count (lambda () #t) '(1 2 3) '(1 2 3)))
  407. (pass-if-exception "pred arg count 1" exception:wrong-num-args
  408. (count (lambda (x) x) '(1 2 3) '(1 2 3)))
  409. (pass-if-exception "pred arg count 3" exception:wrong-num-args
  410. (count (lambda (x y z) x) '(1 2 3) '(1 2 3)))
  411. (pass-if-exception "improper first 1" exception:wrong-type-arg
  412. (count or2 1 '(1 2 3)))
  413. (pass-if-exception "improper first 2" exception:wrong-type-arg
  414. (count or2 '(1 . 2) '(1 2 3)))
  415. (pass-if-exception "improper first 3" exception:wrong-type-arg
  416. (count or2 '(1 2 . 3) '(1 2 3)))
  417. (pass-if-exception "improper second 1" exception:wrong-type-arg
  418. (count or2 '(1 2 3) 1))
  419. (pass-if-exception "improper second 2" exception:wrong-type-arg
  420. (count or2 '(1 2 3) '(1 . 2)))
  421. (pass-if-exception "improper second 3" exception:wrong-type-arg
  422. (count or2 '(1 2 3) '(1 2 . 3)))
  423. (pass-if (= 0 (count or2 '(#f) '(#f))))
  424. (pass-if (= 1 (count or2 '(#t) '(#f))))
  425. (pass-if (= 1 (count or2 '(#f) '(#t))))
  426. (pass-if (= 0 (count or2 '(#f #f) '(#f #f))))
  427. (pass-if (= 1 (count or2 '(#t #f) '(#t #f))))
  428. (pass-if (= 2 (count or2 '(#t #t) '(#f #f))))
  429. (pass-if (= 2 (count or2 '(#t #f) '(#f #t))))
  430. (with-test-prefix "stop shortest"
  431. (pass-if (= 2 (count or2 '(#t #f #t) '(#f #t))))
  432. (pass-if (= 2 (count or2 '(#t #f #t #t) '(#f #t))))
  433. (pass-if (= 2 (count or2 '(#t #f) '(#f #t #t))))
  434. (pass-if (= 2 (count or2 '(#t #f) '(#f #t #t #t))))))
  435. (with-test-prefix "three lists"
  436. (define (or3 x y z)
  437. (or x y z))
  438. (pass-if "arg order"
  439. (= 1 (count (lambda (x y z)
  440. (and (= 1 x)
  441. (= 2 y)
  442. (= 3 z)))
  443. '(1) '(2) '(3))))
  444. (pass-if "empty lists" (= 0 (count or3 '() '() '())))
  445. ;; currently bad pred argument gives wrong-num-args when 3 or more
  446. ;; lists, as opposed to wrong-type-arg for 1 or 2 lists
  447. (pass-if-exception "pred arg count 0" exception:wrong-num-args
  448. (count (lambda () #t) '(1 2 3) '(1 2 3) '(1 2 3)))
  449. (pass-if-exception "pred arg count 2" exception:wrong-num-args
  450. (count (lambda (x y) x) '(1 2 3) '(1 2 3)'(1 2 3) ))
  451. (pass-if-exception "pred arg count 4" exception:wrong-num-args
  452. (count (lambda (w x y z) x) '(1 2 3) '(1 2 3) '(1 2 3)))
  453. (pass-if-exception "improper first 1" exception:wrong-type-arg
  454. (count or3 1 '(1 2 3) '(1 2 3)))
  455. (pass-if-exception "improper first 2" exception:wrong-type-arg
  456. (count or3 '(1 . 2) '(1 2 3) '(1 2 3)))
  457. (pass-if-exception "improper first 3" exception:wrong-type-arg
  458. (count or3 '(1 2 . 3) '(1 2 3) '(1 2 3)))
  459. (pass-if-exception "improper second 1" exception:wrong-type-arg
  460. (count or3 '(1 2 3) 1 '(1 2 3)))
  461. (pass-if-exception "improper second 2" exception:wrong-type-arg
  462. (count or3 '(1 2 3) '(1 . 2) '(1 2 3)))
  463. (pass-if-exception "improper second 3" exception:wrong-type-arg
  464. (count or3 '(1 2 3) '(1 2 . 3) '(1 2 3)))
  465. (pass-if-exception "improper third 1" exception:wrong-type-arg
  466. (count or3 '(1 2 3) '(1 2 3) 1))
  467. (pass-if-exception "improper third 2" exception:wrong-type-arg
  468. (count or3 '(1 2 3) '(1 2 3) '(1 . 2)))
  469. (pass-if-exception "improper third 3" exception:wrong-type-arg
  470. (count or3 '(1 2 3) '(1 2 3) '(1 2 . 3)))
  471. (pass-if (= 0 (count or3 '(#f) '(#f) '(#f))))
  472. (pass-if (= 1 (count or3 '(#t) '(#f) '(#f))))
  473. (pass-if (= 1 (count or3 '(#f) '(#t) '(#f))))
  474. (pass-if (= 1 (count or3 '(#f) '(#f) '(#t))))
  475. (pass-if (= 0 (count or3 '(#f #f) '(#f #f) '(#f #f))))
  476. (pass-if (= 1 (count or3 '(#t #f) '(#f #f) '(#f #f))))
  477. (pass-if (= 1 (count or3 '(#f #t) '(#f #f) '(#f #f))))
  478. (pass-if (= 1 (count or3 '(#f #f) '(#t #f) '(#f #f))))
  479. (pass-if (= 1 (count or3 '(#f #f) '(#f #t) '(#f #f))))
  480. (pass-if (= 1 (count or3 '(#f #f) '(#f #f) '(#t #f))))
  481. (pass-if (= 1 (count or3 '(#f #f) '(#f #f) '(#f #t))))
  482. (pass-if (= 2 (count or3 '(#t #t) '(#f #f) '(#f #f))))
  483. (pass-if (= 2 (count or3 '(#f #f) '(#t #t) '(#f #f))))
  484. (pass-if (= 2 (count or3 '(#f #f) '(#f #f) '(#t #t))))
  485. (pass-if (= 2 (count or3 '(#f #f) '(#t #f) '(#f #t))))
  486. (with-test-prefix "stop shortest"
  487. (pass-if (= 0 (count or3 '() '(#t #t #t) '(#t #t))))
  488. (pass-if (= 0 (count or3 '(#t #t #t) '() '(#t #t))))
  489. (pass-if (= 0 (count or3 '(#t #t #t) '(#t #t) '())))
  490. (pass-if (= 1 (count or3 '(#t) '(#t #t #t) '(#t #t))))
  491. (pass-if (= 1 (count or3 '(#t #t #t) '(#t) '(#t #t))))
  492. (pass-if (= 1 (count or3 '(#t #t #t) '(#t #t) '(#t)))))
  493. (pass-if "apply list unchanged"
  494. (let ((lst (list (list 1 2) (list 3 4) (list 5 6))))
  495. (and (equal? 2 (apply count or3 lst))
  496. ;; lst unmodified
  497. (equal? '((1 2) (3 4) (5 6)) lst))))))
  498. ;;
  499. ;; delete and delete!
  500. ;;
  501. (let ()
  502. ;; Call (PROC lst) for all lists of length up to 6, with all combinations
  503. ;; of elements to be retained or deleted. Elements to retain are numbers,
  504. ;; 0 upwards. Elements to be deleted are #f.
  505. (define (test-lists proc)
  506. (do ((n 0 (1+ n)))
  507. ((>= n 6))
  508. (do ((limit (ash 1 n))
  509. (i 0 (1+ i)))
  510. ((>= i limit))
  511. (let ((lst '()))
  512. (do ((bit 0 (1+ bit)))
  513. ((>= bit n))
  514. (set! lst (cons (if (logbit? bit i) bit #f) lst)))
  515. (proc lst)))))
  516. (define (common-tests delete-proc)
  517. (pass-if-exception "too few args" exception:wrong-num-args
  518. (delete-proc 0))
  519. (pass-if-exception "too many args" exception:wrong-num-args
  520. (delete-proc 0 '() equal? 99))
  521. (pass-if "empty"
  522. (eq? '() (delete-proc 0 '() equal?)))
  523. (pass-if "equal?"
  524. (equal? '((1) (3))
  525. (delete-proc '(2) '((1) (2) (3)) equal?)))
  526. (pass-if "eq?"
  527. (equal? '((1) (2) (3))
  528. (delete-proc '(2) '((1) (2) (3)) eq?)))
  529. (pass-if "called arg order"
  530. (equal? '(1 2 3)
  531. (delete-proc 3 '(1 2 3 4 5) <))))
  532. (with-test-prefix "delete"
  533. (common-tests delete)
  534. (test-lists
  535. (lambda (lst)
  536. (let ((lst-copy (list-copy lst)))
  537. (with-test-prefix lst-copy
  538. (pass-if "result"
  539. (equal? (delete #f lst equal?)
  540. (ref-delete #f lst equal?)))
  541. (pass-if "non-destructive"
  542. (equal? lst-copy lst)))))))
  543. (with-test-prefix "delete!"
  544. (common-tests delete!)
  545. (test-lists
  546. (lambda (lst)
  547. (pass-if lst
  548. (equal? (delete! #f lst)
  549. (ref-delete #f lst)))))))
  550. ;;
  551. ;; delete-duplicates and delete-duplicates!
  552. ;;
  553. (let ()
  554. ;; Call (PROC lst) for all lists of length 1 <= n <= 4, with all
  555. ;; combinations of numbers 1 to n in the elements
  556. (define (test-lists proc)
  557. (do ((n 1 (1+ n)))
  558. ((> n 4))
  559. (do ((limit (integer-expt n n))
  560. (i 0 (1+ i)))
  561. ((>= i limit))
  562. (let ((lst '()))
  563. (do ((j 0 (1+ j))
  564. (rem i (quotient rem n)))
  565. ((>= j n))
  566. (set! lst (cons (remainder rem n) lst)))
  567. (proc lst)))))
  568. (define (common-tests delete-duplicates-proc)
  569. (pass-if-exception "too few args" exception:wrong-num-args
  570. (delete-duplicates-proc))
  571. (pass-if-exception "too many args" exception:wrong-num-args
  572. (delete-duplicates-proc '() equal? 99))
  573. (pass-if "empty"
  574. (eq? '() (delete-duplicates-proc '())))
  575. (pass-if "equal? (the default)"
  576. (equal? '((2))
  577. (delete-duplicates-proc '((2) (2) (2)))))
  578. (pass-if "eq?"
  579. (equal? '((2) (2) (2))
  580. (delete-duplicates-proc '((2) (2) (2)) eq?)))
  581. (pass-if "called arg order"
  582. (let ((ok #t))
  583. (delete-duplicates-proc '(1 2 3 4 5)
  584. (lambda (x y)
  585. (if (> x y)
  586. (set! ok #f))
  587. #f))
  588. ok)))
  589. (with-test-prefix "delete-duplicates"
  590. (common-tests delete-duplicates)
  591. (test-lists
  592. (lambda (lst)
  593. (let ((lst-copy (list-copy lst)))
  594. (with-test-prefix lst-copy
  595. (pass-if "result"
  596. (equal? (delete-duplicates lst)
  597. (ref-delete-duplicates lst)))
  598. (pass-if "non-destructive"
  599. (equal? lst-copy lst)))))))
  600. (with-test-prefix "delete-duplicates!"
  601. (common-tests delete-duplicates!)
  602. (test-lists
  603. (lambda (lst)
  604. (pass-if lst
  605. (equal? (delete-duplicates! lst)
  606. (ref-delete-duplicates lst)))))))
  607. ;;
  608. ;; drop
  609. ;;
  610. (with-test-prefix "drop"
  611. (pass-if "'() 0"
  612. (null? (drop '() 0)))
  613. (pass-if "'(a) 0"
  614. (let ((lst '(a)))
  615. (eq? lst
  616. (drop lst 0))))
  617. (pass-if "'(a b) 0"
  618. (let ((lst '(a b)))
  619. (eq? lst
  620. (drop lst 0))))
  621. (pass-if "'(a) 1"
  622. (let ((lst '(a)))
  623. (eq? (cdr lst)
  624. (drop lst 1))))
  625. (pass-if "'(a b) 1"
  626. (let ((lst '(a b)))
  627. (eq? (cdr lst)
  628. (drop lst 1))))
  629. (pass-if "'(a b) 2"
  630. (let ((lst '(a b)))
  631. (eq? (cddr lst)
  632. (drop lst 2))))
  633. (pass-if "'(a b c) 1"
  634. (let ((lst '(a b c)))
  635. (eq? (cddr lst)
  636. (drop lst 2))))
  637. (pass-if "circular '(a) 0"
  638. (let ((lst (circular-list 'a)))
  639. (eq? lst
  640. (drop lst 0))))
  641. (pass-if "circular '(a) 1"
  642. (let ((lst (circular-list 'a)))
  643. (eq? lst
  644. (drop lst 1))))
  645. (pass-if "circular '(a) 2"
  646. (let ((lst (circular-list 'a)))
  647. (eq? lst
  648. (drop lst 1))))
  649. (pass-if "circular '(a b) 1"
  650. (let ((lst (circular-list 'a)))
  651. (eq? (cdr lst)
  652. (drop lst 0))))
  653. (pass-if "circular '(a b) 2"
  654. (let ((lst (circular-list 'a)))
  655. (eq? lst
  656. (drop lst 1))))
  657. (pass-if "circular '(a b) 5"
  658. (let ((lst (circular-list 'a)))
  659. (eq? (cdr lst)
  660. (drop lst 5))))
  661. (pass-if "'(a . b) 1"
  662. (eq? 'b
  663. (drop '(a . b) 1)))
  664. (pass-if "'(a b . c) 1"
  665. (equal? 'c
  666. (drop '(a b . c) 2))))
  667. ;;
  668. ;; drop-right
  669. ;;
  670. (with-test-prefix "drop-right"
  671. (pass-if-exception "() -1" exception:out-of-range
  672. (drop-right '() -1))
  673. (pass-if (equal? '() (drop-right '() 0)))
  674. (pass-if-exception "() 1" exception:wrong-type-arg
  675. (drop-right '() 1))
  676. (pass-if-exception "(1) -1" exception:out-of-range
  677. (drop-right '(1) -1))
  678. (pass-if (equal? '(1) (drop-right '(1) 0)))
  679. (pass-if (equal? '() (drop-right '(1) 1)))
  680. (pass-if-exception "(1) 2" exception:wrong-type-arg
  681. (drop-right '(1) 2))
  682. (pass-if-exception "(4 5) -1" exception:out-of-range
  683. (drop-right '(4 5) -1))
  684. (pass-if (equal? '(4 5) (drop-right '(4 5) 0)))
  685. (pass-if (equal? '(4) (drop-right '(4 5) 1)))
  686. (pass-if (equal? '() (drop-right '(4 5) 2)))
  687. (pass-if-exception "(4 5) 3" exception:wrong-type-arg
  688. (drop-right '(4 5) 3))
  689. (pass-if-exception "(4 5 6) -1" exception:out-of-range
  690. (drop-right '(4 5 6) -1))
  691. (pass-if (equal? '(4 5 6) (drop-right '(4 5 6) 0)))
  692. (pass-if (equal? '(4 5) (drop-right '(4 5 6) 1)))
  693. (pass-if (equal? '(4) (drop-right '(4 5 6) 2)))
  694. (pass-if (equal? '() (drop-right '(4 5 6) 3)))
  695. (pass-if-exception "(4 5 6) 4" exception:wrong-type-arg
  696. (drop-right '(4 5 6) 4))
  697. (pass-if "(a b . c) 0"
  698. (equal? (drop-right '(a b . c) 0) '(a b)))
  699. (pass-if "(a b . c) 1"
  700. (equal? (drop-right '(a b . c) 1) '(a))))
  701. ;;
  702. ;; drop-right!
  703. ;;
  704. (with-test-prefix "drop-right!"
  705. (pass-if-exception "() -1" exception:out-of-range
  706. (drop-right! '() -1))
  707. (pass-if (equal? '() (drop-right! '() 0)))
  708. (pass-if-exception "() 1" exception:wrong-type-arg
  709. (drop-right! '() 1))
  710. (pass-if-exception "(1) -1" exception:out-of-range
  711. (drop-right! (list 1) -1))
  712. (pass-if (equal? '(1) (drop-right! (list 1) 0)))
  713. (pass-if (equal? '() (drop-right! (list 1) 1)))
  714. (pass-if-exception "(1) 2" exception:wrong-type-arg
  715. (drop-right! (list 1) 2))
  716. (pass-if-exception "(4 5) -1" exception:out-of-range
  717. (drop-right! (list 4 5) -1))
  718. (pass-if (equal? '(4 5) (drop-right! (list 4 5) 0)))
  719. (pass-if (equal? '(4) (drop-right! (list 4 5) 1)))
  720. (pass-if (equal? '() (drop-right! (list 4 5) 2)))
  721. (pass-if-exception "(4 5) 3" exception:wrong-type-arg
  722. (drop-right! (list 4 5) 3))
  723. (pass-if-exception "(4 5 6) -1" exception:out-of-range
  724. (drop-right! (list 4 5 6) -1))
  725. (pass-if (equal? '(4 5 6) (drop-right! (list 4 5 6) 0)))
  726. (pass-if (equal? '(4 5) (drop-right! (list 4 5 6) 1)))
  727. (pass-if (equal? '(4) (drop-right! (list 4 5 6) 2)))
  728. (pass-if (equal? '() (drop-right! (list 4 5 6) 3)))
  729. (pass-if-exception "(4 5 6) 4" exception:wrong-type-arg
  730. (drop-right! (list 4 5 6) 4)))
  731. ;;
  732. ;; drop-while
  733. ;;
  734. (with-test-prefix "drop-while"
  735. (pass-if (equal? '() (drop-while odd? '())))
  736. (pass-if (equal? '() (drop-while odd? '(1))))
  737. (pass-if (equal? '() (drop-while odd? '(1 3))))
  738. (pass-if (equal? '() (drop-while odd? '(1 3 5))))
  739. (pass-if (equal? '(2) (drop-while odd? '(2))))
  740. (pass-if (equal? '(2) (drop-while odd? '(1 2))))
  741. (pass-if (equal? '(4) (drop-while odd? '(1 3 4))))
  742. (pass-if (equal? '(2 1) (drop-while odd? '(2 1))))
  743. (pass-if (equal? '(4 3) (drop-while odd? '(1 4 3))))
  744. (pass-if (equal? '(4 1 3) (drop-while odd? '(4 1 3)))))
  745. ;;
  746. ;; eighth
  747. ;;
  748. (with-test-prefix "eighth"
  749. (pass-if-exception "() -1" exception:wrong-type-arg
  750. (eighth '(a b c d e f g)))
  751. (pass-if (eq? 'h (eighth '(a b c d e f g h))))
  752. (pass-if (eq? 'h (eighth '(a b c d e f g h i)))))
  753. ;;
  754. ;; fifth
  755. ;;
  756. (with-test-prefix "fifth"
  757. (pass-if-exception "() -1" exception:wrong-type-arg
  758. (fifth '(a b c d)))
  759. (pass-if (eq? 'e (fifth '(a b c d e))))
  760. (pass-if (eq? 'e (fifth '(a b c d e f)))))
  761. ;;
  762. ;; filter-map
  763. ;;
  764. (with-test-prefix "filter-map"
  765. (with-test-prefix "one list"
  766. (pass-if-exception "'x" exception:wrong-type-arg
  767. (filter-map noop 'x))
  768. (pass-if-exception "'(1 . x)" exception:wrong-type-arg
  769. (filter-map noop '(1 . x)))
  770. (pass-if "(1)"
  771. (equal? '(1) (filter-map noop '(1))))
  772. (pass-if "(#f)"
  773. (equal? '() (filter-map noop '(#f))))
  774. (pass-if "(1 2)"
  775. (equal? '(1 2) (filter-map noop '(1 2))))
  776. (pass-if "(#f 2)"
  777. (equal? '(2) (filter-map noop '(#f 2))))
  778. (pass-if "(#f #f)"
  779. (equal? '() (filter-map noop '(#f #f))))
  780. (pass-if "(1 2 3)"
  781. (equal? '(1 2 3) (filter-map noop '(1 2 3))))
  782. (pass-if "(#f 2 3)"
  783. (equal? '(2 3) (filter-map noop '(#f 2 3))))
  784. (pass-if "(1 #f 3)"
  785. (equal? '(1 3) (filter-map noop '(1 #f 3))))
  786. (pass-if "(1 2 #f)"
  787. (equal? '(1 2) (filter-map noop '(1 2 #f)))))
  788. (with-test-prefix "two lists"
  789. (pass-if-exception "'x '(1 2 3)" exception:wrong-type-arg
  790. (filter-map noop 'x '(1 2 3)))
  791. (pass-if-exception "'(1 2 3) 'x" exception:wrong-type-arg
  792. (filter-map noop '(1 2 3) 'x))
  793. (pass-if-exception "'(1 . x) '(1 2 3)" exception:wrong-type-arg
  794. (filter-map noop '(1 . x) '(1 2 3)))
  795. (pass-if-exception "'(1 2 3) '(1 . x)" exception:wrong-type-arg
  796. (filter-map noop '(1 2 3) '(1 . x)))
  797. (pass-if "(1 2 3) (4 5 6)"
  798. (equal? '(5 7 9) (filter-map + '(1 2 3) '(4 5 6))))
  799. (pass-if "(#f 2 3) (4 5)"
  800. (equal? '(2) (filter-map noop '(#f 2 3) '(4 5))))
  801. (pass-if "(4 #f) (1 2 3)"
  802. (equal? '(4) (filter-map noop '(4 #f) '(1 2 3))))
  803. (pass-if "() (1 2 3)"
  804. (equal? '() (filter-map noop '() '(1 2 3))))
  805. (pass-if "(1 2 3) ()"
  806. (equal? '() (filter-map noop '(1 2 3) '()))))
  807. (with-test-prefix "three lists"
  808. (pass-if-exception "'x '(1 2 3) '(1 2 3)" exception:wrong-type-arg
  809. (filter-map noop 'x '(1 2 3) '(1 2 3)))
  810. (pass-if-exception "'(1 2 3) 'x '(1 2 3)" exception:wrong-type-arg
  811. (filter-map noop '(1 2 3) 'x '(1 2 3)))
  812. (pass-if-exception "'(1 2 3) '(1 2 3) 'x" exception:wrong-type-arg
  813. (filter-map noop '(1 2 3) '(1 2 3) 'x))
  814. (pass-if-exception "'(1 . x) '(1 2 3) '(1 2 3)" exception:wrong-type-arg
  815. (filter-map noop '(1 . x) '(1 2 3) '(1 2 3)))
  816. (pass-if-exception "'(1 2 3) '(1 . x) '(1 2 3)" exception:wrong-type-arg
  817. (filter-map noop '(1 2 3) '(1 . x) '(1 2 3)))
  818. (pass-if-exception "'(1 2 3) '(1 2 3) '(1 . x)" exception:wrong-type-arg
  819. (filter-map noop '(1 2 3) '(1 2 3) '(1 . x)))
  820. (pass-if "(1 2 3) (4 5 6) (7 8 9)"
  821. (equal? '(12 15 18) (filter-map + '(1 2 3) '(4 5 6) '(7 8 9))))
  822. (pass-if "(#f 2 3) (4 5) (7 8 9)"
  823. (equal? '(2) (filter-map noop '(#f 2 3) '(4 5) '(7 8 9))))
  824. (pass-if "(#f 2 3) (7 8 9) (4 5)"
  825. (equal? '(2) (filter-map noop '(#f 2 3) '(7 8 9) '(4 5))))
  826. (pass-if "(4 #f) (1 2 3) (7 8 9)"
  827. (equal? '(4) (filter-map noop '(4 #f) '(1 2 3) '(7 8 9))))
  828. (pass-if "apply list unchanged"
  829. (let ((lst (list (list 1 #f 2) (list 3 4 5) (list 6 7 8))))
  830. (and (equal? '(1 2) (apply filter-map noop lst))
  831. ;; lst unmodified
  832. (equal? lst '((1 #f 2) (3 4 5) (6 7 8))))))))
  833. ;;
  834. ;; find
  835. ;;
  836. (with-test-prefix "find"
  837. (pass-if (eqv? #f (find odd? '())))
  838. (pass-if (eqv? #f (find odd? '(0))))
  839. (pass-if (eqv? #f (find odd? '(0 2))))
  840. (pass-if (eqv? 1 (find odd? '(1))))
  841. (pass-if (eqv? 1 (find odd? '(0 1))))
  842. (pass-if (eqv? 1 (find odd? '(0 1 2))))
  843. (pass-if (eqv? 1 (find odd? '(2 0 1))))
  844. (pass-if (eqv? 1 (find (lambda (x) (= 1 x)) '(2 0 1)))))
  845. ;;
  846. ;; find-tail
  847. ;;
  848. (with-test-prefix "find-tail"
  849. (pass-if (let ((lst '()))
  850. (eq? #f (find-tail odd? lst))))
  851. (pass-if (let ((lst '(0)))
  852. (eq? #f (find-tail odd? lst))))
  853. (pass-if (let ((lst '(0 2)))
  854. (eq? #f (find-tail odd? lst))))
  855. (pass-if (let ((lst '(1)))
  856. (eq? lst (find-tail odd? lst))))
  857. (pass-if (let ((lst '(1 2)))
  858. (eq? lst (find-tail odd? lst))))
  859. (pass-if (let ((lst '(2 1)))
  860. (eq? (cdr lst) (find-tail odd? lst))))
  861. (pass-if (let ((lst '(2 1 0)))
  862. (eq? (cdr lst) (find-tail odd? lst))))
  863. (pass-if (let ((lst '(2 0 1)))
  864. (eq? (cddr lst) (find-tail odd? lst))))
  865. (pass-if (let ((lst '(2 0 1)))
  866. (eq? (cddr lst) (find-tail (lambda (x) (= 1 x)) lst)))))
  867. ;;
  868. ;; fold
  869. ;;
  870. (with-test-prefix "fold"
  871. (pass-if-exception "no args" exception:wrong-num-args
  872. (fold))
  873. (pass-if-exception "one arg" exception:wrong-num-args
  874. (fold 123))
  875. (pass-if-exception "two args" exception:wrong-num-args
  876. (fold 123 noop))
  877. (with-test-prefix "one list"
  878. (pass-if "arg order"
  879. (eq? #t (fold (lambda (x prev)
  880. (and (= 1 x)
  881. (= 2 prev)))
  882. 2 '(1))))
  883. (pass-if "empty list" (= 123 (fold + 123 '())))
  884. (pass-if-exception "proc arg count 0" exception:wrong-num-args
  885. (fold (lambda () x) 123 '(1 2 3)))
  886. (pass-if-exception "proc arg count 1" exception:wrong-num-args
  887. (fold (lambda (x) x) 123 '(1 2 3)))
  888. (pass-if-exception "proc arg count 3" exception:wrong-num-args
  889. (fold (lambda (x y z) x) 123 '(1 2 3)))
  890. (pass-if-exception "improper 1" exception:wrong-type-arg
  891. (fold + 123 1))
  892. (pass-if-exception "improper 2" exception:wrong-type-arg
  893. (fold + 123 '(1 . 2)))
  894. (pass-if-exception "improper 3" exception:wrong-type-arg
  895. (fold + 123 '(1 2 . 3)))
  896. (pass-if (= 3 (fold + 1 '(2))))
  897. (pass-if (= 6 (fold + 1 '(2 3))))
  898. (pass-if (= 10 (fold + 1 '(2 3 4)))))
  899. (with-test-prefix "two lists"
  900. (pass-if "arg order"
  901. (eq? #t (fold (lambda (x y prev)
  902. (and (= 1 x)
  903. (= 2 y)
  904. (= 3 prev)))
  905. 3 '(1) '(2))))
  906. (pass-if "empty lists" (= 1 (fold + 1 '() '())))
  907. ;; currently bad proc argument gives wrong-num-args when 2 or more
  908. ;; lists, as opposed to wrong-type-arg for 1 list
  909. (pass-if-exception "proc arg count 2" exception:wrong-num-args
  910. (fold (lambda (x prev) x) 1 '(1 2 3) '(1 2 3)))
  911. (pass-if-exception "proc arg count 4" exception:wrong-num-args
  912. (fold (lambda (x y z prev) x) 1 '(1 2 3) '(1 2 3)))
  913. (pass-if-exception "improper first 1" exception:wrong-type-arg
  914. (fold + 1 1 '(1 2 3)))
  915. (pass-if-exception "improper first 2" exception:wrong-type-arg
  916. (fold + 1 '(1 . 2) '(1 2 3)))
  917. (pass-if-exception "improper first 3" exception:wrong-type-arg
  918. (fold + 1 '(1 2 . 3) '(1 2 3)))
  919. (pass-if-exception "improper second 1" exception:wrong-type-arg
  920. (fold + 1 '(1 2 3) 1))
  921. (pass-if-exception "improper second 2" exception:wrong-type-arg
  922. (fold + 1 '(1 2 3) '(1 . 2)))
  923. (pass-if-exception "improper second 3" exception:wrong-type-arg
  924. (fold + 1 '(1 2 3) '(1 2 . 3)))
  925. (pass-if (= 6 (fold + 1 '(2) '(3))))
  926. (pass-if (= 15 (fold + 1 '(2 3) '(4 5))))
  927. (pass-if (= 28 (fold + 1 '(2 3 4) '(5 6 7))))
  928. (with-test-prefix "stop shortest"
  929. (pass-if (= 13 (fold + 1 '(1 2 3) '(4 5))))
  930. (pass-if (= 13 (fold + 1 '(4 5) '(1 2 3))))
  931. (pass-if (= 11 (fold + 1 '(3 4) '(1 2 9 9))))
  932. (pass-if (= 11 (fold + 1 '(1 2 9 9) '(3 4)))))
  933. (pass-if "apply list unchanged"
  934. (let ((lst (list (list 1 2) (list 3 4))))
  935. (and (equal? 11 (apply fold + 1 lst))
  936. ;; lst unmodified
  937. (equal? '((1 2) (3 4)) lst)))))
  938. (with-test-prefix "three lists"
  939. (pass-if "arg order"
  940. (eq? #t (fold (lambda (x y z prev)
  941. (and (= 1 x)
  942. (= 2 y)
  943. (= 3 z)
  944. (= 4 prev)))
  945. 4 '(1) '(2) '(3))))
  946. (pass-if "empty lists" (= 1 (fold + 1 '() '() '())))
  947. (pass-if-exception "proc arg count 3" exception:wrong-num-args
  948. (fold (lambda (x y prev) x) 1 '(1 2 3) '(1 2 3)'(1 2 3) ))
  949. (pass-if-exception "proc arg count 5" exception:wrong-num-args
  950. (fold (lambda (w x y z prev) x) 1 '(1 2 3) '(1 2 3) '(1 2 3)))
  951. (pass-if-exception "improper first 1" exception:wrong-type-arg
  952. (fold + 1 1 '(1 2 3) '(1 2 3)))
  953. (pass-if-exception "improper first 2" exception:wrong-type-arg
  954. (fold + 1 '(1 . 2) '(1 2 3) '(1 2 3)))
  955. (pass-if-exception "improper first 3" exception:wrong-type-arg
  956. (fold + 1 '(1 2 . 3) '(1 2 3) '(1 2 3)))
  957. (pass-if-exception "improper second 1" exception:wrong-type-arg
  958. (fold + 1 '(1 2 3) 1 '(1 2 3)))
  959. (pass-if-exception "improper second 2" exception:wrong-type-arg
  960. (fold + 1 '(1 2 3) '(1 . 2) '(1 2 3)))
  961. (pass-if-exception "improper second 3" exception:wrong-type-arg
  962. (fold + 1 '(1 2 3) '(1 2 . 3) '(1 2 3)))
  963. (pass-if-exception "improper third 1" exception:wrong-type-arg
  964. (fold + 1 '(1 2 3) '(1 2 3) 1))
  965. (pass-if-exception "improper third 2" exception:wrong-type-arg
  966. (fold + 1 '(1 2 3) '(1 2 3) '(1 . 2)))
  967. (pass-if-exception "improper third 3" exception:wrong-type-arg
  968. (fold + 1 '(1 2 3) '(1 2 3) '(1 2 . 3)))
  969. (pass-if (= 10 (fold + 1 '(2) '(3) '(4))))
  970. (pass-if (= 28 (fold + 1 '(2 5) '(3 6) '(4 7))))
  971. (pass-if (= 55 (fold + 1 '(2 5 8) '(3 6 9) '(4 7 10))))
  972. (with-test-prefix "stop shortest"
  973. (pass-if (= 28 (fold + 1 '(2 5 9) '(3 6) '(4 7))))
  974. (pass-if (= 28 (fold + 1 '(2 5) '(3 6 9) '(4 7))))
  975. (pass-if (= 28 (fold + 1 '(2 5) '(3 6) '(4 7 9)))))
  976. (pass-if "apply list unchanged"
  977. (let ((lst (list (list 1 2) (list 3 4) (list 5 6))))
  978. (and (equal? 22 (apply fold + 1 lst))
  979. ;; lst unmodified
  980. (equal? '((1 2) (3 4) (5 6)) lst))))))
  981. ;;
  982. ;; fold-right
  983. ;;
  984. (with-test-prefix "fold-right"
  985. (pass-if "one list"
  986. (equal? (iota 10)
  987. (fold-right cons '() (iota 10))))
  988. (pass-if "two lists"
  989. (equal? (zip (iota 10) (map integer->char (iota 10)))
  990. (fold-right (lambda (x y z)
  991. (cons (list x y) z))
  992. '()
  993. (iota 10)
  994. (map integer->char (iota 10)))))
  995. (pass-if "tail-recursive"
  996. (= 1e6 (fold-right (lambda (x y) (+ 1 y))
  997. 0
  998. (iota 1e6)))))
  999. ;;
  1000. ;; unfold
  1001. ;;
  1002. (with-test-prefix "unfold"
  1003. (pass-if "basic"
  1004. (equal? (iota 10)
  1005. (unfold (lambda (x) (>= x 10))
  1006. identity
  1007. 1+
  1008. 0)))
  1009. (pass-if "tail-gen"
  1010. (equal? (append (iota 10) '(tail 10))
  1011. (unfold (lambda (x) (>= x 10))
  1012. identity
  1013. 1+
  1014. 0
  1015. (lambda (seed) (list 'tail seed)))))
  1016. (pass-if "tail-recursive"
  1017. ;; Bug #30071.
  1018. (pair? (unfold (lambda (x) (>= x 1e6))
  1019. identity
  1020. 1+
  1021. 0))))
  1022. ;;
  1023. ;; length+
  1024. ;;
  1025. (with-test-prefix "length+"
  1026. (pass-if-exception "too few args" exception:wrong-num-args
  1027. (length+))
  1028. (pass-if-exception "too many args" exception:wrong-num-args
  1029. (length+ 123 456))
  1030. (pass-if-exception "not a pair" exception:wrong-type-arg
  1031. (length+ 'x))
  1032. (pass-if-exception "improper list" exception:wrong-type-arg
  1033. (length+ '(x y . z)))
  1034. (pass-if (= 0 (length+ '())))
  1035. (pass-if (= 1 (length+ '(x))))
  1036. (pass-if (= 2 (length+ '(x y))))
  1037. (pass-if (= 3 (length+ '(x y z))))
  1038. (pass-if (not (length+ (circular-list 1))))
  1039. (pass-if (not (length+ (circular-list 1 2))))
  1040. (pass-if (not (length+ (circular-list 1 2 3)))))
  1041. ;;
  1042. ;; last
  1043. ;;
  1044. (with-test-prefix "last"
  1045. (pass-if-exception "empty" exception:wrong-type-arg
  1046. (last '()))
  1047. (pass-if "one elem"
  1048. (eqv? 1 (last '(1))))
  1049. (pass-if "two elems"
  1050. (eqv? 2 (last '(1 2))))
  1051. (pass-if "three elems"
  1052. (eqv? 3 (last '(1 2 3))))
  1053. (pass-if "four elems"
  1054. (eqv? 4 (last '(1 2 3 4)))))
  1055. ;;
  1056. ;; list=
  1057. ;;
  1058. (with-test-prefix "list="
  1059. (pass-if "no lists"
  1060. (eq? #t (list= eqv?)))
  1061. (with-test-prefix "one list"
  1062. (pass-if "empty"
  1063. (eq? #t (list= eqv? '())))
  1064. (pass-if "one elem"
  1065. (eq? #t (list= eqv? '(1))))
  1066. (pass-if "two elems"
  1067. (eq? #t (list= eqv? '(2)))))
  1068. (with-test-prefix "two lists"
  1069. (pass-if "empty / empty"
  1070. (eq? #t (list= eqv? '() '())))
  1071. (pass-if "one / empty"
  1072. (eq? #f (list= eqv? '(1) '())))
  1073. (pass-if "empty / one"
  1074. (eq? #f (list= eqv? '() '(1))))
  1075. (pass-if "one / one same"
  1076. (eq? #t (list= eqv? '(1) '(1))))
  1077. (pass-if "one / one diff"
  1078. (eq? #f (list= eqv? '(1) '(2))))
  1079. (pass-if "called arg order"
  1080. (let ((good #t))
  1081. (list= (lambda (x y)
  1082. (set! good (and good (= (1+ x) y)))
  1083. #t)
  1084. '(1 3) '(2 4))
  1085. good)))
  1086. (with-test-prefix "three lists"
  1087. (pass-if "empty / empty / empty"
  1088. (eq? #t (list= eqv? '() '() '())))
  1089. (pass-if "one / empty / empty"
  1090. (eq? #f (list= eqv? '(1) '() '())))
  1091. (pass-if "one / one / empty"
  1092. (eq? #f (list= eqv? '(1) '(1) '())))
  1093. (pass-if "one / diff / empty"
  1094. (eq? #f (list= eqv? '(1) '(2) '())))
  1095. (pass-if "one / one / one"
  1096. (eq? #t (list= eqv? '(1) '(1) '(1))))
  1097. (pass-if "two / two / diff"
  1098. (eq? #f (list= eqv? '(1 2) '(1 2) '(1 99))))
  1099. (pass-if "two / two / two"
  1100. (eq? #t (list= eqv? '(1 2) '(1 2) '(1 2))))
  1101. (pass-if "called arg order"
  1102. (let ((good #t))
  1103. (list= (lambda (x y)
  1104. (set! good (and good (= (1+ x) y)))
  1105. #t)
  1106. '(1 4) '(2 5) '(3 6))
  1107. good))))
  1108. ;;
  1109. ;; list-copy
  1110. ;;
  1111. (with-test-prefix "list-copy"
  1112. (pass-if (equal? '() (list-copy '())))
  1113. (pass-if (equal? '(1 2) (list-copy '(1 2))))
  1114. (pass-if (equal? '(1 2 3) (list-copy '(1 2 3))))
  1115. (pass-if (equal? '(1 2 3 4) (list-copy '(1 2 3 4))))
  1116. (pass-if (equal? '(1 2 3 4 5) (list-copy '(1 2 3 4 5))))
  1117. ;; improper lists can be copied
  1118. (pass-if (equal? 1 (list-copy 1)))
  1119. (pass-if (equal? '(1 . 2) (list-copy '(1 . 2))))
  1120. (pass-if (equal? '(1 2 . 3) (list-copy '(1 2 . 3))))
  1121. (pass-if (equal? '(1 2 3 . 4) (list-copy '(1 2 3 . 4))))
  1122. (pass-if (equal? '(1 2 3 4 . 5) (list-copy '(1 2 3 4 . 5)))))
  1123. ;;
  1124. ;; list-index
  1125. ;;
  1126. (with-test-prefix "list-index"
  1127. (pass-if-exception "no args" exception:wrong-num-args
  1128. (list-index))
  1129. (pass-if-exception "one arg" exception:wrong-num-args
  1130. (list-index noop))
  1131. (with-test-prefix "one list"
  1132. (pass-if "empty list" (eq? #f (list-index symbol? '())))
  1133. (pass-if-exception "pred arg count 0" exception:wrong-num-args
  1134. (list-index (lambda () x) '(1 2 3)))
  1135. (pass-if-exception "pred arg count 2" exception:wrong-num-args
  1136. (list-index (lambda (x y) x) '(1 2 3)))
  1137. (pass-if-exception "improper 1" exception:wrong-type-arg
  1138. (list-index symbol? 1))
  1139. (pass-if-exception "improper 2" exception:wrong-type-arg
  1140. (list-index symbol? '(1 . 2)))
  1141. (pass-if-exception "improper 3" exception:wrong-type-arg
  1142. (list-index symbol? '(1 2 . 3)))
  1143. (pass-if (eqv? #f (list-index symbol? '(1))))
  1144. (pass-if (eqv? 0 (list-index symbol? '(x))))
  1145. (pass-if (eqv? #f (list-index symbol? '(1 2))))
  1146. (pass-if (eqv? 0 (list-index symbol? '(x 1))))
  1147. (pass-if (eqv? 1 (list-index symbol? '(1 x))))
  1148. (pass-if (eqv? #f (list-index symbol? '(1 2 3))))
  1149. (pass-if (eqv? 0 (list-index symbol? '(x 1 2))))
  1150. (pass-if (eqv? 1 (list-index symbol? '(1 x 2))))
  1151. (pass-if (eqv? 2 (list-index symbol? '(1 2 x)))))
  1152. (with-test-prefix "two lists"
  1153. (define (sym1 x y)
  1154. (symbol? x))
  1155. (define (sym2 x y)
  1156. (symbol? y))
  1157. (pass-if "arg order"
  1158. (eqv? 0 (list-index (lambda (x y)
  1159. (and (= 1 x)
  1160. (= 2 y)))
  1161. '(1) '(2))))
  1162. (pass-if "empty lists" (eqv? #f (list-index sym2 '() '())))
  1163. (pass-if-exception "pred arg count 0" exception:wrong-num-args
  1164. (list-index (lambda () #t) '(1 2 3) '(1 2 3)))
  1165. (pass-if-exception "pred arg count 1" exception:wrong-num-args
  1166. (list-index (lambda (x) x) '(1 2 3) '(1 2 3)))
  1167. (pass-if-exception "pred arg count 3" exception:wrong-num-args
  1168. (list-index (lambda (x y z) x) '(1 2 3) '(1 2 3)))
  1169. (pass-if-exception "improper first 1" exception:wrong-type-arg
  1170. (list-index sym2 1 '(1 2 3)))
  1171. (pass-if-exception "improper first 2" exception:wrong-type-arg
  1172. (list-index sym2 '(1 . 2) '(1 2 3)))
  1173. (pass-if-exception "improper first 3" exception:wrong-type-arg
  1174. (list-index sym2 '(1 2 . 3) '(1 2 3)))
  1175. (pass-if-exception "improper second 1" exception:wrong-type-arg
  1176. (list-index sym2 '(1 2 3) 1))
  1177. (pass-if-exception "improper second 2" exception:wrong-type-arg
  1178. (list-index sym2 '(1 2 3) '(1 . 2)))
  1179. (pass-if-exception "improper second 3" exception:wrong-type-arg
  1180. (list-index sym2 '(1 2 3) '(1 2 . 3)))
  1181. (pass-if (eqv? #f (list-index sym2 '(1) '(2))))
  1182. (pass-if (eqv? 0 (list-index sym2 '(1) '(x))))
  1183. (pass-if (eqv? #f (list-index sym2 '(1 2) '(3 4))))
  1184. (pass-if (eqv? 0 (list-index sym2 '(1 2) '(x 3))))
  1185. (pass-if (eqv? 1 (list-index sym2 '(1 2) '(3 x))))
  1186. (pass-if (eqv? #f (list-index sym2 '(1 2 3) '(3 4 5))))
  1187. (pass-if (eqv? 0 (list-index sym2 '(1 2 3) '(x 3 4))))
  1188. (pass-if (eqv? 1 (list-index sym2 '(1 2 3) '(3 x 4))))
  1189. (pass-if (eqv? 2 (list-index sym2 '(1 2 3) '(3 4 x))))
  1190. (with-test-prefix "stop shortest"
  1191. (pass-if (eqv? #f (list-index sym1 '(1 2 x) '(4 5))))
  1192. (pass-if (eqv? #f (list-index sym2 '(4 5) '(1 2 x))))
  1193. (pass-if (eqv? #f (list-index sym1 '(3 4) '(1 2 x y))))
  1194. (pass-if (eqv? #f (list-index sym2 '(1 2 x y) '(3 4))))))
  1195. (with-test-prefix "three lists"
  1196. (define (sym1 x y z)
  1197. (symbol? x))
  1198. (define (sym2 x y z)
  1199. (symbol? y))
  1200. (define (sym3 x y z)
  1201. (symbol? z))
  1202. (pass-if "arg order"
  1203. (eqv? 0 (list-index (lambda (x y z)
  1204. (and (= 1 x)
  1205. (= 2 y)
  1206. (= 3 z)))
  1207. '(1) '(2) '(3))))
  1208. (pass-if "empty lists" (eqv? #f (list-index sym3 '() '() '())))
  1209. ;; currently bad pred argument gives wrong-num-args when 3 or more
  1210. ;; lists, as opposed to wrong-type-arg for 1 or 2 lists
  1211. (pass-if-exception "pred arg count 0" exception:wrong-num-args
  1212. (list-index (lambda () #t) '(1 2 3) '(1 2 3) '(1 2 3)))
  1213. (pass-if-exception "pred arg count 2" exception:wrong-num-args
  1214. (list-index (lambda (x y) x) '(1 2 3) '(1 2 3)'(1 2 3) ))
  1215. (pass-if-exception "pred arg count 4" exception:wrong-num-args
  1216. (list-index (lambda (w x y z) x) '(1 2 3) '(1 2 3) '(1 2 3)))
  1217. (pass-if-exception "improper first 1" exception:wrong-type-arg
  1218. (list-index sym3 1 '(1 2 3) '(1 2 3)))
  1219. (pass-if-exception "improper first 2" exception:wrong-type-arg
  1220. (list-index sym3 '(1 . 2) '(1 2 3) '(1 2 3)))
  1221. (pass-if-exception "improper first 3" exception:wrong-type-arg
  1222. (list-index sym3 '(1 2 . 3) '(1 2 3) '(1 2 3)))
  1223. (pass-if-exception "improper second 1" exception:wrong-type-arg
  1224. (list-index sym3 '(1 2 3) 1 '(1 2 3)))
  1225. (pass-if-exception "improper second 2" exception:wrong-type-arg
  1226. (list-index sym3 '(1 2 3) '(1 . 2) '(1 2 3)))
  1227. (pass-if-exception "improper second 3" exception:wrong-type-arg
  1228. (list-index sym3 '(1 2 3) '(1 2 . 3) '(1 2 3)))
  1229. (pass-if-exception "improper third 1" exception:wrong-type-arg
  1230. (list-index sym3 '(1 2 3) '(1 2 3) 1))
  1231. (pass-if-exception "improper third 2" exception:wrong-type-arg
  1232. (list-index sym3 '(1 2 3) '(1 2 3) '(1 . 2)))
  1233. (pass-if-exception "improper third 3" exception:wrong-type-arg
  1234. (list-index sym3 '(1 2 3) '(1 2 3) '(1 2 . 3)))
  1235. (pass-if (eqv? #f (list-index sym3 '(#f) '(#f) '(#f))))
  1236. (pass-if (eqv? 0 (list-index sym3 '(#f) '(#f) '(x))))
  1237. (pass-if (eqv? #f (list-index sym3 '(#f #f) '(#f #f) '(#f #f))))
  1238. (pass-if (eqv? 0 (list-index sym3 '(#f #f) '(#f #f) '(x #f))))
  1239. (pass-if (eqv? 1 (list-index sym3 '(#f #f) '(#f #f) '(#f x))))
  1240. (pass-if (eqv? #f (list-index sym3 '(#f #f #f) '(#f #f #f) '(#f #f #f))))
  1241. (pass-if (eqv? 0 (list-index sym3 '(#f #f #f) '(#f #f #f) '(x #f #f))))
  1242. (pass-if (eqv? 1 (list-index sym3 '(#f #f #f) '(#f #f #f) '(#f x #f))))
  1243. (pass-if (eqv? 2 (list-index sym3 '(#f #f #f) '(#f #f #f) '(#f #f x))))
  1244. (with-test-prefix "stop shortest"
  1245. (pass-if (eqv? #f (list-index sym2 '() '(x x x) '(x x))))
  1246. (pass-if (eqv? #f (list-index sym1 '(x x x) '() '(x x))))
  1247. (pass-if (eqv? #f (list-index sym2 '(x x x) '(x x) '())))
  1248. (pass-if (eqv? #f (list-index sym2 '(#t) '(#t x x) '(#t x))))
  1249. (pass-if (eqv? #f (list-index sym1 '(#t x x) '(#t) '(#t x))))
  1250. (pass-if (eqv? #f (list-index sym1 '(#t x x) '(#t x) '(#t)))))
  1251. (pass-if "apply list unchanged"
  1252. (let ((lst (list (list 1 2) (list 3 4) (list 5 6))))
  1253. (and (equal? #f (apply list-index sym3 lst))
  1254. ;; lst unmodified
  1255. (equal? '((1 2) (3 4) (5 6)) lst))))))
  1256. ;;
  1257. ;; list-tabulate
  1258. ;;
  1259. (with-test-prefix "list-tabulate"
  1260. (pass-if-exception "-1" exception:wrong-type-arg
  1261. (list-tabulate -1 identity))
  1262. (pass-if "0"
  1263. (equal? '() (list-tabulate 0 identity)))
  1264. (pass-if "1"
  1265. (equal? '(0) (list-tabulate 1 identity)))
  1266. (pass-if "2"
  1267. (equal? '(0 1) (list-tabulate 2 identity)))
  1268. (pass-if "3"
  1269. (equal? '(0 1 2) (list-tabulate 3 identity)))
  1270. (pass-if "4"
  1271. (equal? '(0 1 2 3) (list-tabulate 4 identity)))
  1272. (pass-if "string ref proc"
  1273. (equal? '(#\a #\b #\c #\d) (list-tabulate 4
  1274. (lambda (i)
  1275. (string-ref "abcd" i))))))
  1276. ;;
  1277. ;; lset=
  1278. ;;
  1279. (with-test-prefix "lset="
  1280. ;; in guile 1.6.7 and earlier, lset= incorrectly demanded at least one
  1281. ;; list arg
  1282. (pass-if "no args"
  1283. (eq? #t (lset= eq?)))
  1284. (with-test-prefix "one arg"
  1285. (pass-if "()"
  1286. (eq? #t (lset= eqv? '())))
  1287. (pass-if "(1)"
  1288. (eq? #t (lset= eqv? '(1))))
  1289. (pass-if "(1 2)"
  1290. (eq? #t (lset= eqv? '(1 2)))))
  1291. (with-test-prefix "two args"
  1292. (pass-if "() ()"
  1293. (eq? #t (lset= eqv? '() '())))
  1294. (pass-if "(1) (1)"
  1295. (eq? #t (lset= eqv? '(1) '(1))))
  1296. (pass-if "(1) (2)"
  1297. (eq? #f (lset= eqv? '(1) '(2))))
  1298. (pass-if "(1) (1 2)"
  1299. (eq? #f (lset= eqv? '(1) '(1 2))))
  1300. (pass-if "(1 2) (2 1)"
  1301. (eq? #t (lset= eqv? '(1 2) '(2 1))))
  1302. (pass-if "called arg order"
  1303. (let ((good #t))
  1304. (lset= (lambda (x y)
  1305. (if (not (= x (1- y)))
  1306. (set! good #f))
  1307. #t)
  1308. '(1 1) '(2 2))
  1309. good)))
  1310. (with-test-prefix "three args"
  1311. (pass-if "() () ()"
  1312. (eq? #t (lset= eqv? '() '() '())))
  1313. (pass-if "(1) (1) (1)"
  1314. (eq? #t (lset= eqv? '(1) '(1) '(1))))
  1315. (pass-if "(1) (1) (2)"
  1316. (eq? #f (lset= eqv? '(1) '(1) '(2))))
  1317. (pass-if "(1) (1) (1 2)"
  1318. (eq? #f (lset= eqv? '(1) '(1) '(1 2))))
  1319. (pass-if "(1 2 3) (3 2 1) (1 3 2)"
  1320. (eq? #t (lset= eqv? '(1 2 3) '(3 2 1) '(1 3 2))))
  1321. (pass-if "called arg order"
  1322. (let ((good #t))
  1323. (lset= (lambda (x y)
  1324. (if (not (= x (1- y)))
  1325. (set! good #f))
  1326. #t)
  1327. '(1 1) '(2 2) '(3 3))
  1328. good))))
  1329. ;;
  1330. ;; lset-adjoin
  1331. ;;
  1332. (with-test-prefix "lset-adjoin"
  1333. ;; in guile 1.6.7 and earlier, lset-adjoin didn't actually use the given
  1334. ;; `=' procedure, all comparisons were just with `equal?
  1335. ;;
  1336. (with-test-prefix "case-insensitive ="
  1337. (pass-if "(\"x\") \"X\""
  1338. (equal? '("x") (lset-adjoin string-ci=? '("x") "X"))))
  1339. (pass-if "called arg order"
  1340. (let ((good #f))
  1341. (lset-adjoin (lambda (x y)
  1342. (set! good (and (= x 1) (= y 2)))
  1343. (= x y))
  1344. '(1) 2)
  1345. good))
  1346. (pass-if (equal? '() (lset-adjoin = '())))
  1347. (pass-if (equal? '(1) (lset-adjoin = '() 1)))
  1348. (pass-if (equal? '(1) (lset-adjoin = '() 1 1)))
  1349. (pass-if (equal? '(2 1) (lset-adjoin = '() 1 2)))
  1350. (pass-if (equal? '(3 1 2) (lset-adjoin = '(1 2) 1 2 3 2 1)))
  1351. (pass-if "apply list unchanged"
  1352. (let ((lst (list 1 2)))
  1353. (and (equal? '(2 1 3) (apply lset-adjoin = '(3) lst))
  1354. ;; lst unmodified
  1355. (equal? '(1 2) lst))))
  1356. (pass-if "(1 1) 1 1"
  1357. (equal? '(1 1) (lset-adjoin = '(1 1) 1 1)))
  1358. ;; duplicates among args are cast out
  1359. (pass-if "(2) 1 1"
  1360. (equal? '(1 2) (lset-adjoin = '(2) 1 1))))
  1361. ;;
  1362. ;; lset-difference
  1363. ;;
  1364. (with-test-prefix "lset-difference"
  1365. (pass-if "called arg order"
  1366. (let ((good #f))
  1367. (lset-difference (lambda (x y)
  1368. (set! good (and (= x 1) (= y 2)))
  1369. (= x y))
  1370. '(1) '(2))
  1371. good)))
  1372. ;;
  1373. ;; lset-difference!
  1374. ;;
  1375. (with-test-prefix "lset-difference!"
  1376. (pass-if-exception "proc - num" exception:wrong-type-arg
  1377. (lset-difference! 123 '(4)))
  1378. (pass-if-exception "proc - list" exception:wrong-type-arg
  1379. (lset-difference! (list 1 2 3) '(4)))
  1380. (pass-if "called arg order"
  1381. (let ((good #f))
  1382. (lset-difference! (lambda (x y)
  1383. (set! good (and (= x 1) (= y 2)))
  1384. (= x y))
  1385. (list 1) (list 2))
  1386. good))
  1387. (pass-if (equal? '() (lset-difference! = '())))
  1388. (pass-if (equal? '(1) (lset-difference! = (list 1))))
  1389. (pass-if (equal? '(1 2) (lset-difference! = (list 1 2))))
  1390. (pass-if (equal? '() (lset-difference! = (list ) '(3))))
  1391. (pass-if (equal? '() (lset-difference! = (list 3) '(3))))
  1392. (pass-if (equal? '(1) (lset-difference! = (list 1 3) '(3))))
  1393. (pass-if (equal? '(1) (lset-difference! = (list 3 1) '(3))))
  1394. (pass-if (equal? '(1) (lset-difference! = (list 1 3 3) '(3))))
  1395. (pass-if (equal? '(1) (lset-difference! = (list 3 1 3) '(3))))
  1396. (pass-if (equal? '(1) (lset-difference! = (list 3 3 1) '(3))))
  1397. (pass-if (equal? '(1) (lset-difference! = (list 1 2 3) '(2 3))))
  1398. (pass-if (equal? '(1) (lset-difference! = (list 1 2 3) '(3 2))))
  1399. (pass-if (equal? '(1) (lset-difference! = (list 1 2 3) '(3) '(2))))
  1400. (pass-if (equal? '(1) (lset-difference! = (list 1 2 3) '(2) '(3))))
  1401. (pass-if (equal? '(1) (lset-difference! = (list 1 2 3) '(2) '(2 3))))
  1402. (pass-if (equal? '(1) (lset-difference! = (list 1 2 3) '(2) '(3 2))))
  1403. (pass-if (equal? '(1 2) (lset-difference! = (list 1 2 3) '(3) '(3))))
  1404. (pass-if (equal? '(1 2) (lset-difference! = (list 1 3 2) '(3) '(3))))
  1405. (pass-if (equal? '(1 2) (lset-difference! = (list 3 1 2) '(3) '(3))))
  1406. (pass-if (equal? '(1 2 3) (lset-difference! = (list 1 2 3 4) '(4))))
  1407. (pass-if (equal? '(1 2 3) (lset-difference! = (list 1 2 4 3) '(4))))
  1408. (pass-if (equal? '(1 2 3) (lset-difference! = (list 1 4 2 3) '(4))))
  1409. (pass-if (equal? '(1 2 3) (lset-difference! = (list 4 1 2 3) '(4))))
  1410. (pass-if (equal? '(1 2) (lset-difference! = (list 1 2 3 4) '(4) '(3))))
  1411. (pass-if (equal? '(1 2) (lset-difference! = (list 1 3 2 4) '(4) '(3))))
  1412. (pass-if (equal? '(1 2) (lset-difference! = (list 3 1 2 4) '(4) '(3))))
  1413. (pass-if (equal? '(1 2) (lset-difference! = (list 1 3 4 2) '(4) '(3))))
  1414. (pass-if (equal? '(1 2) (lset-difference! = (list 3 1 4 2) '(4) '(3))))
  1415. (pass-if (equal? '(1 2) (lset-difference! = (list 3 4 1 2) '(4) '(3)))))
  1416. ;;
  1417. ;; lset-diff+intersection
  1418. ;;
  1419. (with-test-prefix "lset-diff+intersection"
  1420. (pass-if "called arg order"
  1421. (let ((good #f))
  1422. (lset-diff+intersection (lambda (x y)
  1423. (set! good (and (= x 1) (= y 2)))
  1424. (= x y))
  1425. '(1) '(2))
  1426. good)))
  1427. ;;
  1428. ;; lset-diff+intersection!
  1429. ;;
  1430. (with-test-prefix "lset-diff+intersection"
  1431. (pass-if "called arg order"
  1432. (let ((good #f))
  1433. (lset-diff+intersection (lambda (x y)
  1434. (set! good (and (= x 1) (= y 2)))
  1435. (= x y))
  1436. (list 1) (list 2))
  1437. good)))
  1438. ;;
  1439. ;; lset-intersection
  1440. ;;
  1441. (with-test-prefix "lset-intersection"
  1442. (pass-if "called arg order"
  1443. (let ((good #f))
  1444. (lset-intersection (lambda (x y)
  1445. (set! good (and (= x 1) (= y 2)))
  1446. (= x y))
  1447. '(1) '(2))
  1448. good)))
  1449. ;;
  1450. ;; lset-intersection!
  1451. ;;
  1452. (with-test-prefix "lset-intersection"
  1453. (pass-if "called arg order"
  1454. (let ((good #f))
  1455. (lset-intersection (lambda (x y)
  1456. (set! good (and (= x 1) (= y 2)))
  1457. (= x y))
  1458. (list 1) (list 2))
  1459. good)))
  1460. ;;
  1461. ;; lset-union
  1462. ;;
  1463. (with-test-prefix "lset-union"
  1464. (pass-if "no args"
  1465. (eq? '() (lset-union eq?)))
  1466. (pass-if "one arg"
  1467. (equal? '(1 2 3) (lset-union eq? '(1 2 3))))
  1468. (pass-if "'() '()"
  1469. (equal? '() (lset-union eq? '() '())))
  1470. (pass-if "'() '(1 2 3)"
  1471. (equal? '(1 2 3) (lset-union eq? '() '(1 2 3))))
  1472. (pass-if "'(1 2 3) '()"
  1473. (equal? '(1 2 3) (lset-union eq? '(1 2 3) '())))
  1474. (pass-if "'(1 2 3) '(4 3 5)"
  1475. (equal? '(5 4 1 2 3) (lset-union eq? '(1 2 3) '(4 3 5))))
  1476. (pass-if "'(1 2 3) '(4) '(3 5))"
  1477. (equal? '(5 4 1 2 3) (lset-union eq? '(1 2 3) '(4) '(3 5))))
  1478. ;; in guile 1.6.7 and earlier, `=' was called with the arguments the wrong
  1479. ;; way around
  1480. (pass-if "called arg order"
  1481. (let ((good #f))
  1482. (lset-union (lambda (x y)
  1483. (set! good (and (= x 1) (= y 2)))
  1484. (= x y))
  1485. '(1) '(2))
  1486. good)))
  1487. ;;
  1488. ;; member
  1489. ;;
  1490. (with-test-prefix "member"
  1491. (pass-if-exception "no args" exception:wrong-num-args
  1492. (member))
  1493. (pass-if-exception "one arg" exception:wrong-num-args
  1494. (member 1))
  1495. (pass-if "1 (1 2 3)"
  1496. (let ((lst '(1 2 3)))
  1497. (eq? lst (member 1 lst))))
  1498. (pass-if "2 (1 2 3)"
  1499. (let ((lst '(1 2 3)))
  1500. (eq? (cdr lst) (member 2 lst))))
  1501. (pass-if "3 (1 2 3)"
  1502. (let ((lst '(1 2 3)))
  1503. (eq? (cddr lst) (member 3 lst))))
  1504. (pass-if "4 (1 2 3)"
  1505. (let ((lst '(1 2 3)))
  1506. (eq? #f (member 4 lst))))
  1507. (pass-if "called arg order"
  1508. (let ((good #f))
  1509. (member 1 '(2) (lambda (x y)
  1510. (set! good (and (eqv? 1 x)
  1511. (eqv? 2 y)))))
  1512. good)))
  1513. ;;
  1514. ;; ninth
  1515. ;;
  1516. (with-test-prefix "ninth"
  1517. (pass-if-exception "() -1" exception:wrong-type-arg
  1518. (ninth '(a b c d e f g h)))
  1519. (pass-if (eq? 'i (ninth '(a b c d e f g h i))))
  1520. (pass-if (eq? 'i (ninth '(a b c d e f g h i j)))))
  1521. ;;
  1522. ;; not-pair?
  1523. ;;
  1524. (with-test-prefix "not-pair?"
  1525. (pass-if "inum"
  1526. (eq? #t (not-pair? 123)))
  1527. (pass-if "pair"
  1528. (eq? #f (not-pair? '(x . y))))
  1529. (pass-if "symbol"
  1530. (eq? #t (not-pair? 'x))))
  1531. ;;
  1532. ;; take
  1533. ;;
  1534. (with-test-prefix "take"
  1535. (pass-if "'() 0"
  1536. (null? (take '() 0)))
  1537. (pass-if "'(a) 0"
  1538. (null? (take '(a) 0)))
  1539. (pass-if "'(a b) 0"
  1540. (null? (take '() 0)))
  1541. (pass-if "'(a b c) 0"
  1542. (null? (take '() 0)))
  1543. (pass-if "'(a) 1"
  1544. (let* ((lst '(a))
  1545. (got (take lst 1)))
  1546. (and (equal? '(a) got)
  1547. (not (eq? lst got)))))
  1548. (pass-if "'(a b) 1"
  1549. (equal? '(a)
  1550. (take '(a b) 1)))
  1551. (pass-if "'(a b c) 1"
  1552. (equal? '(a)
  1553. (take '(a b c) 1)))
  1554. (pass-if "'(a b) 2"
  1555. (let* ((lst '(a b))
  1556. (got (take lst 2)))
  1557. (and (equal? '(a b) got)
  1558. (not (eq? lst got)))))
  1559. (pass-if "'(a b c) 2"
  1560. (equal? '(a b)
  1561. (take '(a b c) 2)))
  1562. (pass-if "circular '(a) 0"
  1563. (equal? '()
  1564. (take (circular-list 'a) 0)))
  1565. (pass-if "circular '(a) 1"
  1566. (equal? '(a)
  1567. (take (circular-list 'a) 1)))
  1568. (pass-if "circular '(a) 2"
  1569. (equal? '(a a)
  1570. (take (circular-list 'a) 2)))
  1571. (pass-if "circular '(a b) 5"
  1572. (equal? '(a b a b a)
  1573. (take (circular-list 'a 'b) 5)))
  1574. (pass-if "'(a . b) 1"
  1575. (equal? '(a)
  1576. (take '(a . b) 1)))
  1577. (pass-if "'(a b . c) 1"
  1578. (equal? '(a)
  1579. (take '(a b . c) 1)))
  1580. (pass-if "'(a b . c) 2"
  1581. (equal? '(a b)
  1582. (take '(a b . c) 2))))
  1583. ;;
  1584. ;; take-while
  1585. ;;
  1586. (with-test-prefix "take-while"
  1587. (pass-if (equal? '() (take-while odd? '())))
  1588. (pass-if (equal? '(1) (take-while odd? '(1))))
  1589. (pass-if (equal? '(1 3) (take-while odd? '(1 3))))
  1590. (pass-if (equal? '(1 3 5) (take-while odd? '(1 3 5))))
  1591. (pass-if (equal? '() (take-while odd? '(2))))
  1592. (pass-if (equal? '(1) (take-while odd? '(1 2))))
  1593. (pass-if (equal? '(1 3) (take-while odd? '(1 3 4))))
  1594. (pass-if (equal? '() (take-while odd? '(2 1))))
  1595. (pass-if (equal? '(1) (take-while odd? '(1 4 3))))
  1596. (pass-if (equal? '() (take-while odd? '(4 1 3)))))
  1597. ;;
  1598. ;; take-while!
  1599. ;;
  1600. (with-test-prefix "take-while!"
  1601. (pass-if (equal? '() (take-while! odd? '())))
  1602. (pass-if (equal? '(1) (take-while! odd? (list 1))))
  1603. (pass-if (equal? '(1 3) (take-while! odd? (list 1 3))))
  1604. (pass-if (equal? '(1 3 5) (take-while! odd? (list 1 3 5))))
  1605. (pass-if (equal? '() (take-while! odd? (list 2))))
  1606. (pass-if (equal? '(1) (take-while! odd? (list 1 2))))
  1607. (pass-if (equal? '(1 3) (take-while! odd? (list 1 3 4))))
  1608. (pass-if (equal? '() (take-while! odd? (list 2 1))))
  1609. (pass-if (equal? '(1) (take-while! odd? (list 1 4 3))))
  1610. (pass-if (equal? '() (take-while! odd? (list 4 1 3)))))
  1611. ;;
  1612. ;; partition
  1613. ;;
  1614. (define (test-partition pred list kept-good dropped-good)
  1615. (call-with-values (lambda ()
  1616. (partition pred list))
  1617. (lambda (kept dropped)
  1618. (and (equal? kept kept-good)
  1619. (equal? dropped dropped-good)))))
  1620. (with-test-prefix "partition"
  1621. (pass-if "with dropped tail"
  1622. (test-partition even? '(1 2 3 4 5 6 7)
  1623. '(2 4 6) '(1 3 5 7)))
  1624. (pass-if "with kept tail"
  1625. (test-partition even? '(1 2 3 4 5 6)
  1626. '(2 4 6) '(1 3 5)))
  1627. (pass-if "with everything dropped"
  1628. (test-partition even? '(1 3 5 7)
  1629. '() '(1 3 5 7)))
  1630. (pass-if "with everything kept"
  1631. (test-partition even? '(2 4 6)
  1632. '(2 4 6) '()))
  1633. (pass-if "with empty list"
  1634. (test-partition even? '()
  1635. '() '()))
  1636. (pass-if "with reasonably long list"
  1637. ;; the old implementation from SRFI-1 reference implementation
  1638. ;; would signal a stack-overflow for a list of only 500 elements!
  1639. (call-with-values (lambda ()
  1640. (partition even?
  1641. (make-list 10000 1)))
  1642. (lambda (even odd)
  1643. (and (= (length odd) 10000)
  1644. (= (length even) 0)))))
  1645. (pass-if-exception "with improper list"
  1646. exception:wrong-type-arg
  1647. (partition symbol? '(a b . c))))
  1648. ;;
  1649. ;; partition!
  1650. ;;
  1651. (define (test-partition! pred list kept-good dropped-good)
  1652. (call-with-values (lambda ()
  1653. (partition! pred list))
  1654. (lambda (kept dropped)
  1655. (and (equal? kept kept-good)
  1656. (equal? dropped dropped-good)))))
  1657. (with-test-prefix "partition!"
  1658. (pass-if "with dropped tail"
  1659. (test-partition! even? (list 1 2 3 4 5 6 7)
  1660. '(2 4 6) '(1 3 5 7)))
  1661. (pass-if "with kept tail"
  1662. (test-partition! even? (list 1 2 3 4 5 6)
  1663. '(2 4 6) '(1 3 5)))
  1664. (pass-if "with everything dropped"
  1665. (test-partition! even? (list 1 3 5 7)
  1666. '() '(1 3 5 7)))
  1667. (pass-if "with everything kept"
  1668. (test-partition! even? (list 2 4 6)
  1669. '(2 4 6) '()))
  1670. (pass-if "with empty list"
  1671. (test-partition! even? '()
  1672. '() '()))
  1673. (pass-if "with reasonably long list"
  1674. ;; the old implementation from SRFI-1 reference implementation
  1675. ;; would signal a stack-overflow for a list of only 500 elements!
  1676. (call-with-values (lambda ()
  1677. (partition! even?
  1678. (make-list 10000 1)))
  1679. (lambda (even odd)
  1680. (and (= (length odd) 10000)
  1681. (= (length even) 0)))))
  1682. (pass-if-exception "with improper list"
  1683. exception:wrong-type-arg
  1684. (partition! symbol? (cons* 'a 'b 'c))))
  1685. ;;
  1686. ;; reduce
  1687. ;;
  1688. (with-test-prefix "reduce"
  1689. (pass-if "empty"
  1690. (let* ((calls '())
  1691. (ret (reduce (lambda (x prev)
  1692. (set! calls (cons (list x prev) calls))
  1693. x)
  1694. 1 '())))
  1695. (and (equal? calls '())
  1696. (equal? ret 1))))
  1697. (pass-if "one elem"
  1698. (let* ((calls '())
  1699. (ret (reduce (lambda (x prev)
  1700. (set! calls (cons (list x prev) calls))
  1701. x)
  1702. 1 '(2))))
  1703. (and (equal? calls '())
  1704. (equal? ret 2))))
  1705. (pass-if "two elems"
  1706. (let* ((calls '())
  1707. (ret (reduce (lambda (x prev)
  1708. (set! calls (cons (list x prev) calls))
  1709. x)
  1710. 1 '(2 3))))
  1711. (and (equal? calls '((3 2)))
  1712. (equal? ret 3))))
  1713. (pass-if "three elems"
  1714. (let* ((calls '())
  1715. (ret (reduce (lambda (x prev)
  1716. (set! calls (cons (list x prev) calls))
  1717. x)
  1718. 1 '(2 3 4))))
  1719. (and (equal? calls '((4 3)
  1720. (3 2)))
  1721. (equal? ret 4))))
  1722. (pass-if "four elems"
  1723. (let* ((calls '())
  1724. (ret (reduce (lambda (x prev)
  1725. (set! calls (cons (list x prev) calls))
  1726. x)
  1727. 1 '(2 3 4 5))))
  1728. (and (equal? calls '((5 4)
  1729. (4 3)
  1730. (3 2)))
  1731. (equal? ret 5)))))
  1732. ;;
  1733. ;; reduce-right
  1734. ;;
  1735. (with-test-prefix "reduce-right"
  1736. (pass-if "empty"
  1737. (let* ((calls '())
  1738. (ret (reduce-right (lambda (x prev)
  1739. (set! calls (cons (list x prev) calls))
  1740. x)
  1741. 1 '())))
  1742. (and (equal? calls '())
  1743. (equal? ret 1))))
  1744. (pass-if "one elem"
  1745. (let* ((calls '())
  1746. (ret (reduce-right (lambda (x prev)
  1747. (set! calls (cons (list x prev) calls))
  1748. x)
  1749. 1 '(2))))
  1750. (and (equal? calls '())
  1751. (equal? ret 2))))
  1752. (pass-if "two elems"
  1753. (let* ((calls '())
  1754. (ret (reduce-right (lambda (x prev)
  1755. (set! calls (cons (list x prev) calls))
  1756. x)
  1757. 1 '(2 3))))
  1758. (and (equal? calls '((2 3)))
  1759. (equal? ret 2))))
  1760. (pass-if "three elems"
  1761. (let* ((calls '())
  1762. (ret (reduce-right (lambda (x prev)
  1763. (set! calls (cons (list x prev) calls))
  1764. x)
  1765. 1 '(2 3 4))))
  1766. (and (equal? calls '((2 3)
  1767. (3 4)))
  1768. (equal? ret 2))))
  1769. (pass-if "four elems"
  1770. (let* ((calls '())
  1771. (ret (reduce-right (lambda (x prev)
  1772. (set! calls (cons (list x prev) calls))
  1773. x)
  1774. 1 '(2 3 4 5))))
  1775. (and (equal? calls '((2 3)
  1776. (3 4)
  1777. (4 5)))
  1778. (equal? ret 2)))))
  1779. ;;
  1780. ;; remove
  1781. ;;
  1782. (with-test-prefix "remove"
  1783. (pass-if (equal? '() (remove odd? '())))
  1784. (pass-if (equal? '() (remove odd? '(1))))
  1785. (pass-if (equal? '(2) (remove odd? '(2))))
  1786. (pass-if (equal? '() (remove odd? '(1 3))))
  1787. (pass-if (equal? '(2) (remove odd? '(2 3))))
  1788. (pass-if (equal? '(2) (remove odd? '(1 2))))
  1789. (pass-if (equal? '(2 4) (remove odd? '(2 4))))
  1790. (pass-if (equal? '() (remove odd? '(1 3 5))))
  1791. (pass-if (equal? '(2) (remove odd? '(2 3 5))))
  1792. (pass-if (equal? '(2) (remove odd? '(1 2 5))))
  1793. (pass-if (equal? '(2 4) (remove odd? '(2 4 5))))
  1794. (pass-if (equal? '(6) (remove odd? '(1 3 6))))
  1795. (pass-if (equal? '(2 6) (remove odd? '(2 3 6))))
  1796. (pass-if (equal? '(2 6) (remove odd? '(1 2 6))))
  1797. (pass-if (equal? '(2 4 6) (remove odd? '(2 4 6)))))
  1798. ;;
  1799. ;; remove!
  1800. ;;
  1801. (with-test-prefix "remove!"
  1802. (pass-if (equal? '() (remove! odd? '())))
  1803. (pass-if (equal? '() (remove! odd? (list 1))))
  1804. (pass-if (equal? '(2) (remove! odd? (list 2))))
  1805. (pass-if (equal? '() (remove! odd? (list 1 3))))
  1806. (pass-if (equal? '(2) (remove! odd? (list 2 3))))
  1807. (pass-if (equal? '(2) (remove! odd? (list 1 2))))
  1808. (pass-if (equal? '(2 4) (remove! odd? (list 2 4))))
  1809. (pass-if (equal? '() (remove! odd? (list 1 3 5))))
  1810. (pass-if (equal? '(2) (remove! odd? (list 2 3 5))))
  1811. (pass-if (equal? '(2) (remove! odd? (list 1 2 5))))
  1812. (pass-if (equal? '(2 4) (remove! odd? (list 2 4 5))))
  1813. (pass-if (equal? '(6) (remove! odd? (list 1 3 6))))
  1814. (pass-if (equal? '(2 6) (remove! odd? (list 2 3 6))))
  1815. (pass-if (equal? '(2 6) (remove! odd? (list 1 2 6))))
  1816. (pass-if (equal? '(2 4 6) (remove! odd? (list 2 4 6)))))
  1817. ;;
  1818. ;; seventh
  1819. ;;
  1820. (with-test-prefix "seventh"
  1821. (pass-if-exception "() -1" exception:wrong-type-arg
  1822. (seventh '(a b c d e f)))
  1823. (pass-if (eq? 'g (seventh '(a b c d e f g))))
  1824. (pass-if (eq? 'g (seventh '(a b c d e f g h)))))
  1825. ;;
  1826. ;; sixth
  1827. ;;
  1828. (with-test-prefix "sixth"
  1829. (pass-if-exception "() -1" exception:wrong-type-arg
  1830. (sixth '(a b c d e)))
  1831. (pass-if (eq? 'f (sixth '(a b c d e f))))
  1832. (pass-if (eq? 'f (sixth '(a b c d e f g)))))
  1833. ;;
  1834. ;; split-at
  1835. ;;
  1836. (with-test-prefix "split-at"
  1837. (define (equal-values? lst thunk)
  1838. (call-with-values thunk
  1839. (lambda got
  1840. (equal? lst got))))
  1841. (pass-if-exception "() -1" exception:out-of-range
  1842. (split-at '() -1))
  1843. (pass-if (equal-values? '(() ())
  1844. (lambda () (split-at '() 0))))
  1845. (pass-if-exception "() 1" exception:wrong-type-arg
  1846. (split-at '() 1))
  1847. (pass-if-exception "(1) -1" exception:out-of-range
  1848. (split-at '(1) -1))
  1849. (pass-if (equal-values? '(() (1)) (lambda () (split-at '(1) 0))))
  1850. (pass-if (equal-values? '((1) ()) (lambda () (split-at '(1) 1))))
  1851. (pass-if-exception "(1) 2" exception:wrong-type-arg
  1852. (split-at '(1) 2))
  1853. (pass-if-exception "(4 5) -1" exception:out-of-range
  1854. (split-at '(4 5) -1))
  1855. (pass-if (equal-values? '(() (4 5)) (lambda () (split-at '(4 5) 0))))
  1856. (pass-if (equal-values? '((4) (5)) (lambda () (split-at '(4 5) 1))))
  1857. (pass-if (equal-values? '((4 5) ()) (lambda () (split-at '(4 5) 2))))
  1858. (pass-if-exception "(4 5) 3" exception:wrong-type-arg
  1859. (split-at '(4 5) 3))
  1860. (pass-if-exception "(4 5 6) -1" exception:out-of-range
  1861. (split-at '(4 5 6) -1))
  1862. (pass-if (equal-values? '(() (4 5 6)) (lambda () (split-at '(4 5 6) 0))))
  1863. (pass-if (equal-values? '((4) (5 6)) (lambda () (split-at '(4 5 6) 1))))
  1864. (pass-if (equal-values? '((4 5) (6)) (lambda () (split-at '(4 5 6) 2))))
  1865. (pass-if (equal-values? '((4 5 6) ()) (lambda () (split-at '(4 5 6) 3))))
  1866. (pass-if-exception "(4 5 6) 4" exception:wrong-type-arg
  1867. (split-at '(4 5 6) 4)))
  1868. ;;
  1869. ;; split-at!
  1870. ;;
  1871. (with-test-prefix "split-at!"
  1872. (define (equal-values? lst thunk)
  1873. (call-with-values thunk
  1874. (lambda got
  1875. (equal? lst got))))
  1876. (pass-if-exception "() -1" exception:out-of-range
  1877. (split-at! '() -1))
  1878. (pass-if (equal-values? '(() ())
  1879. (lambda () (split-at! '() 0))))
  1880. (pass-if-exception "() 1" exception:wrong-type-arg
  1881. (split-at! '() 1))
  1882. (pass-if-exception "(1) -1" exception:out-of-range
  1883. (split-at! (list 1) -1))
  1884. (pass-if (equal-values? '(() (1)) (lambda () (split-at! (list 1) 0))))
  1885. (pass-if (equal-values? '((1) ()) (lambda () (split-at! (list 1) 1))))
  1886. (pass-if-exception "(1) 2" exception:wrong-type-arg
  1887. (split-at! (list 1) 2))
  1888. (pass-if-exception "(4 5) -1" exception:out-of-range
  1889. (split-at! (list 4 5) -1))
  1890. (pass-if (equal-values? '(() (4 5)) (lambda () (split-at! (list 4 5) 0))))
  1891. (pass-if (equal-values? '((4) (5)) (lambda () (split-at! (list 4 5) 1))))
  1892. (pass-if (equal-values? '((4 5) ()) (lambda () (split-at! (list 4 5) 2))))
  1893. (pass-if-exception "(4 5) 3" exception:wrong-type-arg
  1894. (split-at! (list 4 5) 3))
  1895. (pass-if-exception "(4 5 6) -1" exception:out-of-range
  1896. (split-at! (list 4 5 6) -1))
  1897. (pass-if (equal-values? '(() (4 5 6)) (lambda () (split-at! (list 4 5 6) 0))))
  1898. (pass-if (equal-values? '((4) (5 6)) (lambda () (split-at! (list 4 5 6) 1))))
  1899. (pass-if (equal-values? '((4 5) (6)) (lambda () (split-at! (list 4 5 6) 2))))
  1900. (pass-if (equal-values? '((4 5 6) ()) (lambda () (split-at! (list 4 5 6) 3))))
  1901. (pass-if-exception "(4 5 6) 4" exception:wrong-type-arg
  1902. (split-at! (list 4 5 6) 4)))
  1903. ;;
  1904. ;; span
  1905. ;;
  1906. (with-test-prefix "span"
  1907. (define (test-span lst want-v1 want-v2)
  1908. (call-with-values
  1909. (lambda ()
  1910. (span positive? lst))
  1911. (lambda (got-v1 got-v2)
  1912. (and (equal? got-v1 want-v1)
  1913. (equal? got-v2 want-v2)))))
  1914. (pass-if "empty"
  1915. (test-span '() '() '()))
  1916. (pass-if "y"
  1917. (test-span '(1) '(1) '()))
  1918. (pass-if "n"
  1919. (test-span '(-1) '() '(-1)))
  1920. (pass-if "yy"
  1921. (test-span '(1 2) '(1 2) '()))
  1922. (pass-if "ny"
  1923. (test-span '(-1 1) '() '(-1 1)))
  1924. (pass-if "yn"
  1925. (test-span '(1 -1) '(1) '(-1)))
  1926. (pass-if "nn"
  1927. (test-span '(-1 -2) '() '(-1 -2)))
  1928. (pass-if "yyy"
  1929. (test-span '(1 2 3) '(1 2 3) '()))
  1930. (pass-if "nyy"
  1931. (test-span '(-1 1 2) '() '(-1 1 2)))
  1932. (pass-if "yny"
  1933. (test-span '(1 -1 2) '(1) '(-1 2)))
  1934. (pass-if "nny"
  1935. (test-span '(-1 -2 1) '() '(-1 -2 1)))
  1936. (pass-if "yyn"
  1937. (test-span '(1 2 -1) '(1 2) '(-1)))
  1938. (pass-if "nyn"
  1939. (test-span '(-1 1 -2) '() '(-1 1 -2)))
  1940. (pass-if "ynn"
  1941. (test-span '(1 -1 -2) '(1) '(-1 -2)))
  1942. (pass-if "nnn"
  1943. (test-span '(-1 -2 -3) '() '(-1 -2 -3))))
  1944. ;;
  1945. ;; span!
  1946. ;;
  1947. (with-test-prefix "span!"
  1948. (define (test-span! lst want-v1 want-v2)
  1949. (call-with-values
  1950. (lambda ()
  1951. (span! positive? lst))
  1952. (lambda (got-v1 got-v2)
  1953. (and (equal? got-v1 want-v1)
  1954. (equal? got-v2 want-v2)))))
  1955. (pass-if "empty"
  1956. (test-span! '() '() '()))
  1957. (pass-if "y"
  1958. (test-span! (list 1) '(1) '()))
  1959. (pass-if "n"
  1960. (test-span! (list -1) '() '(-1)))
  1961. (pass-if "yy"
  1962. (test-span! (list 1 2) '(1 2) '()))
  1963. (pass-if "ny"
  1964. (test-span! (list -1 1) '() '(-1 1)))
  1965. (pass-if "yn"
  1966. (test-span! (list 1 -1) '(1) '(-1)))
  1967. (pass-if "nn"
  1968. (test-span! (list -1 -2) '() '(-1 -2)))
  1969. (pass-if "yyy"
  1970. (test-span! (list 1 2 3) '(1 2 3) '()))
  1971. (pass-if "nyy"
  1972. (test-span! (list -1 1 2) '() '(-1 1 2)))
  1973. (pass-if "yny"
  1974. (test-span! (list 1 -1 2) '(1) '(-1 2)))
  1975. (pass-if "nny"
  1976. (test-span! (list -1 -2 1) '() '(-1 -2 1)))
  1977. (pass-if "yyn"
  1978. (test-span! (list 1 2 -1) '(1 2) '(-1)))
  1979. (pass-if "nyn"
  1980. (test-span! (list -1 1 -2) '() '(-1 1 -2)))
  1981. (pass-if "ynn"
  1982. (test-span! (list 1 -1 -2) '(1) '(-1 -2)))
  1983. (pass-if "nnn"
  1984. (test-span! (list -1 -2 -3) '() '(-1 -2 -3))))
  1985. ;;
  1986. ;; take!
  1987. ;;
  1988. (with-test-prefix "take!"
  1989. (pass-if-exception "() -1" exception:out-of-range
  1990. (take! '() -1))
  1991. (pass-if (equal? '() (take! '() 0)))
  1992. (pass-if-exception "() 1" exception:wrong-type-arg
  1993. (take! '() 1))
  1994. (pass-if-exception "(1) -1" exception:out-of-range
  1995. (take! '(1) -1))
  1996. (pass-if (equal? '() (take! '(1) 0)))
  1997. (pass-if (equal? '(1) (take! '(1) 1)))
  1998. (pass-if-exception "(1) 2" exception:wrong-type-arg
  1999. (take! '(1) 2))
  2000. (pass-if-exception "(4 5) -1" exception:out-of-range
  2001. (take! '(4 5) -1))
  2002. (pass-if (equal? '() (take! '(4 5) 0)))
  2003. (pass-if (equal? '(4) (take! '(4 5) 1)))
  2004. (pass-if (equal? '(4 5) (take! '(4 5) 2)))
  2005. (pass-if-exception "(4 5) 3" exception:wrong-type-arg
  2006. (take! '(4 5) 3))
  2007. (pass-if-exception "(4 5 6) -1" exception:out-of-range
  2008. (take! '(4 5 6) -1))
  2009. (pass-if (equal? '() (take! '(4 5 6) 0)))
  2010. (pass-if (equal? '(4) (take! '(4 5 6) 1)))
  2011. (pass-if (equal? '(4 5) (take! '(4 5 6) 2)))
  2012. (pass-if (equal? '(4 5 6) (take! '(4 5 6) 3)))
  2013. (pass-if-exception "(4 5 6) 4" exception:wrong-type-arg
  2014. (take! '(4 5 6) 4)))
  2015. ;;
  2016. ;; take-right
  2017. ;;
  2018. (with-test-prefix "take-right"
  2019. (pass-if-exception "() -1" exception:out-of-range
  2020. (take-right '() -1))
  2021. (pass-if (equal? '() (take-right '() 0)))
  2022. (pass-if-exception "() 1" exception:wrong-type-arg
  2023. (take-right '() 1))
  2024. (pass-if-exception "(1) -1" exception:out-of-range
  2025. (take-right '(1) -1))
  2026. (pass-if (equal? '() (take-right '(1) 0)))
  2027. (pass-if (equal? '(1) (take-right '(1) 1)))
  2028. (pass-if-exception "(1) 2" exception:wrong-type-arg
  2029. (take-right '(1) 2))
  2030. (pass-if-exception "(4 5) -1" exception:out-of-range
  2031. (take-right '(4 5) -1))
  2032. (pass-if (equal? '() (take-right '(4 5) 0)))
  2033. (pass-if (equal? '(5) (take-right '(4 5) 1)))
  2034. (pass-if (equal? '(4 5) (take-right '(4 5) 2)))
  2035. (pass-if-exception "(4 5) 3" exception:wrong-type-arg
  2036. (take-right '(4 5) 3))
  2037. (pass-if-exception "(4 5 6) -1" exception:out-of-range
  2038. (take-right '(4 5 6) -1))
  2039. (pass-if (equal? '() (take-right '(4 5 6) 0)))
  2040. (pass-if (equal? '(6) (take-right '(4 5 6) 1)))
  2041. (pass-if (equal? '(5 6) (take-right '(4 5 6) 2)))
  2042. (pass-if (equal? '(4 5 6) (take-right '(4 5 6) 3)))
  2043. (pass-if-exception "(4 5 6) 4" exception:wrong-type-arg
  2044. (take-right '(4 5 6) 4))
  2045. (pass-if "(a b . c) 0"
  2046. (equal? (take-right '(a b . c) 0) 'c))
  2047. (pass-if "(a b . c) 1"
  2048. (equal? (take-right '(a b . c) 1) '(b . c))))
  2049. ;;
  2050. ;; tenth
  2051. ;;
  2052. (with-test-prefix "tenth"
  2053. (pass-if-exception "() -1" exception:wrong-type-arg
  2054. (tenth '(a b c d e f g h i)))
  2055. (pass-if (eq? 'j (tenth '(a b c d e f g h i j))))
  2056. (pass-if (eq? 'j (tenth '(a b c d e f g h i j k)))))
  2057. ;;
  2058. ;; xcons
  2059. ;;
  2060. (with-test-prefix "xcons"
  2061. (pass-if (equal? '(y . x) (xcons 'x 'y))))