srfi-1.test 71 KB

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