srfi-1.test 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642
  1. ;;;; srfi-1.test --- Test suite for Guile's SRFI-1 functions. -*- scheme -*-
  2. ;;;;
  3. ;;;; Copyright 2003, 2004, 2005, 2006, 2008, 2009, 2010 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. ;;
  698. ;; drop-right!
  699. ;;
  700. (with-test-prefix "drop-right!"
  701. (pass-if-exception "() -1" exception:out-of-range
  702. (drop-right! '() -1))
  703. (pass-if (equal? '() (drop-right! '() 0)))
  704. (pass-if-exception "() 1" exception:wrong-type-arg
  705. (drop-right! '() 1))
  706. (pass-if-exception "(1) -1" exception:out-of-range
  707. (drop-right! (list 1) -1))
  708. (pass-if (equal? '(1) (drop-right! (list 1) 0)))
  709. (pass-if (equal? '() (drop-right! (list 1) 1)))
  710. (pass-if-exception "(1) 2" exception:wrong-type-arg
  711. (drop-right! (list 1) 2))
  712. (pass-if-exception "(4 5) -1" exception:out-of-range
  713. (drop-right! (list 4 5) -1))
  714. (pass-if (equal? '(4 5) (drop-right! (list 4 5) 0)))
  715. (pass-if (equal? '(4) (drop-right! (list 4 5) 1)))
  716. (pass-if (equal? '() (drop-right! (list 4 5) 2)))
  717. (pass-if-exception "(4 5) 3" exception:wrong-type-arg
  718. (drop-right! (list 4 5) 3))
  719. (pass-if-exception "(4 5 6) -1" exception:out-of-range
  720. (drop-right! (list 4 5 6) -1))
  721. (pass-if (equal? '(4 5 6) (drop-right! (list 4 5 6) 0)))
  722. (pass-if (equal? '(4 5) (drop-right! (list 4 5 6) 1)))
  723. (pass-if (equal? '(4) (drop-right! (list 4 5 6) 2)))
  724. (pass-if (equal? '() (drop-right! (list 4 5 6) 3)))
  725. (pass-if-exception "(4 5 6) 4" exception:wrong-type-arg
  726. (drop-right! (list 4 5 6) 4)))
  727. ;;
  728. ;; drop-while
  729. ;;
  730. (with-test-prefix "drop-while"
  731. (pass-if (equal? '() (drop-while odd? '())))
  732. (pass-if (equal? '() (drop-while odd? '(1))))
  733. (pass-if (equal? '() (drop-while odd? '(1 3))))
  734. (pass-if (equal? '() (drop-while odd? '(1 3 5))))
  735. (pass-if (equal? '(2) (drop-while odd? '(2))))
  736. (pass-if (equal? '(2) (drop-while odd? '(1 2))))
  737. (pass-if (equal? '(4) (drop-while odd? '(1 3 4))))
  738. (pass-if (equal? '(2 1) (drop-while odd? '(2 1))))
  739. (pass-if (equal? '(4 3) (drop-while odd? '(1 4 3))))
  740. (pass-if (equal? '(4 1 3) (drop-while odd? '(4 1 3)))))
  741. ;;
  742. ;; eighth
  743. ;;
  744. (with-test-prefix "eighth"
  745. (pass-if-exception "() -1" exception:wrong-type-arg
  746. (eighth '(a b c d e f g)))
  747. (pass-if (eq? 'h (eighth '(a b c d e f g h))))
  748. (pass-if (eq? 'h (eighth '(a b c d e f g h i)))))
  749. ;;
  750. ;; fifth
  751. ;;
  752. (with-test-prefix "fifth"
  753. (pass-if-exception "() -1" exception:wrong-type-arg
  754. (fifth '(a b c d)))
  755. (pass-if (eq? 'e (fifth '(a b c d e))))
  756. (pass-if (eq? 'e (fifth '(a b c d e f)))))
  757. ;;
  758. ;; filter-map
  759. ;;
  760. (with-test-prefix "filter-map"
  761. (with-test-prefix "one list"
  762. (pass-if-exception "'x" exception:wrong-type-arg
  763. (filter-map noop 'x))
  764. (pass-if-exception "'(1 . x)" exception:wrong-type-arg
  765. (filter-map noop '(1 . x)))
  766. (pass-if "(1)"
  767. (equal? '(1) (filter-map noop '(1))))
  768. (pass-if "(#f)"
  769. (equal? '() (filter-map noop '(#f))))
  770. (pass-if "(1 2)"
  771. (equal? '(1 2) (filter-map noop '(1 2))))
  772. (pass-if "(#f 2)"
  773. (equal? '(2) (filter-map noop '(#f 2))))
  774. (pass-if "(#f #f)"
  775. (equal? '() (filter-map noop '(#f #f))))
  776. (pass-if "(1 2 3)"
  777. (equal? '(1 2 3) (filter-map noop '(1 2 3))))
  778. (pass-if "(#f 2 3)"
  779. (equal? '(2 3) (filter-map noop '(#f 2 3))))
  780. (pass-if "(1 #f 3)"
  781. (equal? '(1 3) (filter-map noop '(1 #f 3))))
  782. (pass-if "(1 2 #f)"
  783. (equal? '(1 2) (filter-map noop '(1 2 #f)))))
  784. (with-test-prefix "two lists"
  785. (pass-if-exception "'x '(1 2 3)" exception:wrong-type-arg
  786. (filter-map noop 'x '(1 2 3)))
  787. (pass-if-exception "'(1 2 3) 'x" exception:wrong-type-arg
  788. (filter-map noop '(1 2 3) 'x))
  789. (pass-if-exception "'(1 . x) '(1 2 3)" exception:wrong-type-arg
  790. (filter-map noop '(1 . x) '(1 2 3)))
  791. (pass-if-exception "'(1 2 3) '(1 . x)" exception:wrong-type-arg
  792. (filter-map noop '(1 2 3) '(1 . x)))
  793. (pass-if "(1 2 3) (4 5 6)"
  794. (equal? '(5 7 9) (filter-map + '(1 2 3) '(4 5 6))))
  795. (pass-if "(#f 2 3) (4 5)"
  796. (equal? '(2) (filter-map noop '(#f 2 3) '(4 5))))
  797. (pass-if "(4 #f) (1 2 3)"
  798. (equal? '(4) (filter-map noop '(4 #f) '(1 2 3))))
  799. (pass-if "() (1 2 3)"
  800. (equal? '() (filter-map noop '() '(1 2 3))))
  801. (pass-if "(1 2 3) ()"
  802. (equal? '() (filter-map noop '(1 2 3) '()))))
  803. (with-test-prefix "three lists"
  804. (pass-if-exception "'x '(1 2 3) '(1 2 3)" exception:wrong-type-arg
  805. (filter-map noop 'x '(1 2 3) '(1 2 3)))
  806. (pass-if-exception "'(1 2 3) 'x '(1 2 3)" exception:wrong-type-arg
  807. (filter-map noop '(1 2 3) 'x '(1 2 3)))
  808. (pass-if-exception "'(1 2 3) '(1 2 3) 'x" exception:wrong-type-arg
  809. (filter-map noop '(1 2 3) '(1 2 3) 'x))
  810. (pass-if-exception "'(1 . x) '(1 2 3) '(1 2 3)" exception:wrong-type-arg
  811. (filter-map noop '(1 . x) '(1 2 3) '(1 2 3)))
  812. (pass-if-exception "'(1 2 3) '(1 . x) '(1 2 3)" exception:wrong-type-arg
  813. (filter-map noop '(1 2 3) '(1 . x) '(1 2 3)))
  814. (pass-if-exception "'(1 2 3) '(1 2 3) '(1 . x)" exception:wrong-type-arg
  815. (filter-map noop '(1 2 3) '(1 2 3) '(1 . x)))
  816. (pass-if "(1 2 3) (4 5 6) (7 8 9)"
  817. (equal? '(12 15 18) (filter-map + '(1 2 3) '(4 5 6) '(7 8 9))))
  818. (pass-if "(#f 2 3) (4 5) (7 8 9)"
  819. (equal? '(2) (filter-map noop '(#f 2 3) '(4 5) '(7 8 9))))
  820. (pass-if "(#f 2 3) (7 8 9) (4 5)"
  821. (equal? '(2) (filter-map noop '(#f 2 3) '(7 8 9) '(4 5))))
  822. (pass-if "(4 #f) (1 2 3) (7 8 9)"
  823. (equal? '(4) (filter-map noop '(4 #f) '(1 2 3) '(7 8 9))))
  824. (pass-if "apply list unchanged"
  825. (let ((lst (list (list 1 #f 2) (list 3 4 5) (list 6 7 8))))
  826. (and (equal? '(1 2) (apply filter-map noop lst))
  827. ;; lst unmodified
  828. (equal? lst '((1 #f 2) (3 4 5) (6 7 8))))))))
  829. ;;
  830. ;; find
  831. ;;
  832. (with-test-prefix "find"
  833. (pass-if (eqv? #f (find odd? '())))
  834. (pass-if (eqv? #f (find odd? '(0))))
  835. (pass-if (eqv? #f (find odd? '(0 2))))
  836. (pass-if (eqv? 1 (find odd? '(1))))
  837. (pass-if (eqv? 1 (find odd? '(0 1))))
  838. (pass-if (eqv? 1 (find odd? '(0 1 2))))
  839. (pass-if (eqv? 1 (find odd? '(2 0 1))))
  840. (pass-if (eqv? 1 (find (lambda (x) (= 1 x)) '(2 0 1)))))
  841. ;;
  842. ;; find-tail
  843. ;;
  844. (with-test-prefix "find-tail"
  845. (pass-if (let ((lst '()))
  846. (eq? #f (find-tail odd? lst))))
  847. (pass-if (let ((lst '(0)))
  848. (eq? #f (find-tail odd? lst))))
  849. (pass-if (let ((lst '(0 2)))
  850. (eq? #f (find-tail odd? lst))))
  851. (pass-if (let ((lst '(1)))
  852. (eq? lst (find-tail odd? lst))))
  853. (pass-if (let ((lst '(1 2)))
  854. (eq? lst (find-tail odd? lst))))
  855. (pass-if (let ((lst '(2 1)))
  856. (eq? (cdr lst) (find-tail odd? lst))))
  857. (pass-if (let ((lst '(2 1 0)))
  858. (eq? (cdr lst) (find-tail odd? lst))))
  859. (pass-if (let ((lst '(2 0 1)))
  860. (eq? (cddr lst) (find-tail odd? lst))))
  861. (pass-if (let ((lst '(2 0 1)))
  862. (eq? (cddr lst) (find-tail (lambda (x) (= 1 x)) lst)))))
  863. ;;
  864. ;; fold
  865. ;;
  866. (with-test-prefix "fold"
  867. (pass-if-exception "no args" exception:wrong-num-args
  868. (fold))
  869. (pass-if-exception "one arg" exception:wrong-num-args
  870. (fold 123))
  871. (pass-if-exception "two args" exception:wrong-num-args
  872. (fold 123 noop))
  873. (with-test-prefix "one list"
  874. (pass-if "arg order"
  875. (eq? #t (fold (lambda (x prev)
  876. (and (= 1 x)
  877. (= 2 prev)))
  878. 2 '(1))))
  879. (pass-if "empty list" (= 123 (fold + 123 '())))
  880. (pass-if-exception "proc arg count 0" exception:wrong-num-args
  881. (fold (lambda () x) 123 '(1 2 3)))
  882. (pass-if-exception "proc arg count 1" exception:wrong-num-args
  883. (fold (lambda (x) x) 123 '(1 2 3)))
  884. (pass-if-exception "proc arg count 3" exception:wrong-num-args
  885. (fold (lambda (x y z) x) 123 '(1 2 3)))
  886. (pass-if-exception "improper 1" exception:wrong-type-arg
  887. (fold + 123 1))
  888. (pass-if-exception "improper 2" exception:wrong-type-arg
  889. (fold + 123 '(1 . 2)))
  890. (pass-if-exception "improper 3" exception:wrong-type-arg
  891. (fold + 123 '(1 2 . 3)))
  892. (pass-if (= 3 (fold + 1 '(2))))
  893. (pass-if (= 6 (fold + 1 '(2 3))))
  894. (pass-if (= 10 (fold + 1 '(2 3 4)))))
  895. (with-test-prefix "two lists"
  896. (pass-if "arg order"
  897. (eq? #t (fold (lambda (x y prev)
  898. (and (= 1 x)
  899. (= 2 y)
  900. (= 3 prev)))
  901. 3 '(1) '(2))))
  902. (pass-if "empty lists" (= 1 (fold + 1 '() '())))
  903. ;; currently bad proc argument gives wrong-num-args when 2 or more
  904. ;; lists, as opposed to wrong-type-arg for 1 list
  905. (pass-if-exception "proc arg count 2" exception:wrong-num-args
  906. (fold (lambda (x prev) x) 1 '(1 2 3) '(1 2 3)))
  907. (pass-if-exception "proc arg count 4" exception:wrong-num-args
  908. (fold (lambda (x y z prev) x) 1 '(1 2 3) '(1 2 3)))
  909. (pass-if-exception "improper first 1" exception:wrong-type-arg
  910. (fold + 1 1 '(1 2 3)))
  911. (pass-if-exception "improper first 2" exception:wrong-type-arg
  912. (fold + 1 '(1 . 2) '(1 2 3)))
  913. (pass-if-exception "improper first 3" exception:wrong-type-arg
  914. (fold + 1 '(1 2 . 3) '(1 2 3)))
  915. (pass-if-exception "improper second 1" exception:wrong-type-arg
  916. (fold + 1 '(1 2 3) 1))
  917. (pass-if-exception "improper second 2" exception:wrong-type-arg
  918. (fold + 1 '(1 2 3) '(1 . 2)))
  919. (pass-if-exception "improper second 3" exception:wrong-type-arg
  920. (fold + 1 '(1 2 3) '(1 2 . 3)))
  921. (pass-if (= 6 (fold + 1 '(2) '(3))))
  922. (pass-if (= 15 (fold + 1 '(2 3) '(4 5))))
  923. (pass-if (= 28 (fold + 1 '(2 3 4) '(5 6 7))))
  924. (with-test-prefix "stop shortest"
  925. (pass-if (= 13 (fold + 1 '(1 2 3) '(4 5))))
  926. (pass-if (= 13 (fold + 1 '(4 5) '(1 2 3))))
  927. (pass-if (= 11 (fold + 1 '(3 4) '(1 2 9 9))))
  928. (pass-if (= 11 (fold + 1 '(1 2 9 9) '(3 4)))))
  929. (pass-if "apply list unchanged"
  930. (let ((lst (list (list 1 2) (list 3 4))))
  931. (and (equal? 11 (apply fold + 1 lst))
  932. ;; lst unmodified
  933. (equal? '((1 2) (3 4)) lst)))))
  934. (with-test-prefix "three lists"
  935. (pass-if "arg order"
  936. (eq? #t (fold (lambda (x y z prev)
  937. (and (= 1 x)
  938. (= 2 y)
  939. (= 3 z)
  940. (= 4 prev)))
  941. 4 '(1) '(2) '(3))))
  942. (pass-if "empty lists" (= 1 (fold + 1 '() '() '())))
  943. (pass-if-exception "proc arg count 3" exception:wrong-num-args
  944. (fold (lambda (x y prev) x) 1 '(1 2 3) '(1 2 3)'(1 2 3) ))
  945. (pass-if-exception "proc arg count 5" exception:wrong-num-args
  946. (fold (lambda (w x y z prev) x) 1 '(1 2 3) '(1 2 3) '(1 2 3)))
  947. (pass-if-exception "improper first 1" exception:wrong-type-arg
  948. (fold + 1 1 '(1 2 3) '(1 2 3)))
  949. (pass-if-exception "improper first 2" exception:wrong-type-arg
  950. (fold + 1 '(1 . 2) '(1 2 3) '(1 2 3)))
  951. (pass-if-exception "improper first 3" exception:wrong-type-arg
  952. (fold + 1 '(1 2 . 3) '(1 2 3) '(1 2 3)))
  953. (pass-if-exception "improper second 1" exception:wrong-type-arg
  954. (fold + 1 '(1 2 3) 1 '(1 2 3)))
  955. (pass-if-exception "improper second 2" exception:wrong-type-arg
  956. (fold + 1 '(1 2 3) '(1 . 2) '(1 2 3)))
  957. (pass-if-exception "improper second 3" exception:wrong-type-arg
  958. (fold + 1 '(1 2 3) '(1 2 . 3) '(1 2 3)))
  959. (pass-if-exception "improper third 1" exception:wrong-type-arg
  960. (fold + 1 '(1 2 3) '(1 2 3) 1))
  961. (pass-if-exception "improper third 2" exception:wrong-type-arg
  962. (fold + 1 '(1 2 3) '(1 2 3) '(1 . 2)))
  963. (pass-if-exception "improper third 3" exception:wrong-type-arg
  964. (fold + 1 '(1 2 3) '(1 2 3) '(1 2 . 3)))
  965. (pass-if (= 10 (fold + 1 '(2) '(3) '(4))))
  966. (pass-if (= 28 (fold + 1 '(2 5) '(3 6) '(4 7))))
  967. (pass-if (= 55 (fold + 1 '(2 5 8) '(3 6 9) '(4 7 10))))
  968. (with-test-prefix "stop shortest"
  969. (pass-if (= 28 (fold + 1 '(2 5 9) '(3 6) '(4 7))))
  970. (pass-if (= 28 (fold + 1 '(2 5) '(3 6 9) '(4 7))))
  971. (pass-if (= 28 (fold + 1 '(2 5) '(3 6) '(4 7 9)))))
  972. (pass-if "apply list unchanged"
  973. (let ((lst (list (list 1 2) (list 3 4) (list 5 6))))
  974. (and (equal? 22 (apply fold + 1 lst))
  975. ;; lst unmodified
  976. (equal? '((1 2) (3 4) (5 6)) lst))))))
  977. ;;
  978. ;; fold-right
  979. ;;
  980. (with-test-prefix "fold-right"
  981. (pass-if "one list"
  982. (equal? (iota 10)
  983. (fold-right cons '() (iota 10))))
  984. (pass-if "two lists"
  985. (equal? (zip (iota 10) (map integer->char (iota 10)))
  986. (fold-right (lambda (x y z)
  987. (cons (list x y) z))
  988. '()
  989. (iota 10)
  990. (map integer->char (iota 10)))))
  991. (pass-if "tail-recursive"
  992. (= 1e6 (fold-right (lambda (x y) (+ 1 y))
  993. 0
  994. (iota 1e6)))))
  995. ;;
  996. ;; unfold
  997. ;;
  998. (with-test-prefix "unfold"
  999. (pass-if "basic"
  1000. (equal? (iota 10)
  1001. (unfold (lambda (x) (>= x 10))
  1002. identity
  1003. 1+
  1004. 0)))
  1005. (pass-if "tail-gen"
  1006. (equal? (append (iota 10) '(tail 10))
  1007. (unfold (lambda (x) (>= x 10))
  1008. identity
  1009. 1+
  1010. 0
  1011. (lambda (seed) (list 'tail seed)))))
  1012. (pass-if "tail-recursive"
  1013. ;; Bug #30071.
  1014. (pair? (unfold (lambda (x) (>= x 1e6))
  1015. identity
  1016. 1+
  1017. 0))))
  1018. ;;
  1019. ;; length+
  1020. ;;
  1021. (with-test-prefix "length+"
  1022. (pass-if-exception "too few args" exception:wrong-num-args
  1023. (length+))
  1024. (pass-if-exception "too many args" exception:wrong-num-args
  1025. (length+ 123 456))
  1026. (pass-if (= 0 (length+ '())))
  1027. (pass-if (= 1 (length+ '(x))))
  1028. (pass-if (= 2 (length+ '(x y))))
  1029. (pass-if (= 3 (length+ '(x y z))))
  1030. (pass-if (not (length+ (circular-list 1))))
  1031. (pass-if (not (length+ (circular-list 1 2))))
  1032. (pass-if (not (length+ (circular-list 1 2 3)))))
  1033. ;;
  1034. ;; last
  1035. ;;
  1036. (with-test-prefix "last"
  1037. (pass-if-exception "empty" exception:wrong-type-arg
  1038. (last '()))
  1039. (pass-if "one elem"
  1040. (eqv? 1 (last '(1))))
  1041. (pass-if "two elems"
  1042. (eqv? 2 (last '(1 2))))
  1043. (pass-if "three elems"
  1044. (eqv? 3 (last '(1 2 3))))
  1045. (pass-if "four elems"
  1046. (eqv? 4 (last '(1 2 3 4)))))
  1047. ;;
  1048. ;; list=
  1049. ;;
  1050. (with-test-prefix "list="
  1051. (pass-if "no lists"
  1052. (eq? #t (list= eqv?)))
  1053. (with-test-prefix "one list"
  1054. (pass-if "empty"
  1055. (eq? #t (list= eqv? '())))
  1056. (pass-if "one elem"
  1057. (eq? #t (list= eqv? '(1))))
  1058. (pass-if "two elems"
  1059. (eq? #t (list= eqv? '(2)))))
  1060. (with-test-prefix "two lists"
  1061. (pass-if "empty / empty"
  1062. (eq? #t (list= eqv? '() '())))
  1063. (pass-if "one / empty"
  1064. (eq? #f (list= eqv? '(1) '())))
  1065. (pass-if "empty / one"
  1066. (eq? #f (list= eqv? '() '(1))))
  1067. (pass-if "one / one same"
  1068. (eq? #t (list= eqv? '(1) '(1))))
  1069. (pass-if "one / one diff"
  1070. (eq? #f (list= eqv? '(1) '(2))))
  1071. (pass-if "called arg order"
  1072. (let ((good #t))
  1073. (list= (lambda (x y)
  1074. (set! good (and good (= (1+ x) y)))
  1075. #t)
  1076. '(1 3) '(2 4))
  1077. good)))
  1078. (with-test-prefix "three lists"
  1079. (pass-if "empty / empty / empty"
  1080. (eq? #t (list= eqv? '() '() '())))
  1081. (pass-if "one / empty / empty"
  1082. (eq? #f (list= eqv? '(1) '() '())))
  1083. (pass-if "one / one / empty"
  1084. (eq? #f (list= eqv? '(1) '(1) '())))
  1085. (pass-if "one / diff / empty"
  1086. (eq? #f (list= eqv? '(1) '(2) '())))
  1087. (pass-if "one / one / one"
  1088. (eq? #t (list= eqv? '(1) '(1) '(1))))
  1089. (pass-if "two / two / diff"
  1090. (eq? #f (list= eqv? '(1 2) '(1 2) '(1 99))))
  1091. (pass-if "two / two / two"
  1092. (eq? #t (list= eqv? '(1 2) '(1 2) '(1 2))))
  1093. (pass-if "called arg order"
  1094. (let ((good #t))
  1095. (list= (lambda (x y)
  1096. (set! good (and good (= (1+ x) y)))
  1097. #t)
  1098. '(1 4) '(2 5) '(3 6))
  1099. good))))
  1100. ;;
  1101. ;; list-copy
  1102. ;;
  1103. (with-test-prefix "list-copy"
  1104. (pass-if (equal? '() (list-copy '())))
  1105. (pass-if (equal? '(1 2) (list-copy '(1 2))))
  1106. (pass-if (equal? '(1 2 3) (list-copy '(1 2 3))))
  1107. (pass-if (equal? '(1 2 3 4) (list-copy '(1 2 3 4))))
  1108. (pass-if (equal? '(1 2 3 4 5) (list-copy '(1 2 3 4 5))))
  1109. ;; improper lists can be copied
  1110. (pass-if (equal? 1 (list-copy 1)))
  1111. (pass-if (equal? '(1 . 2) (list-copy '(1 . 2))))
  1112. (pass-if (equal? '(1 2 . 3) (list-copy '(1 2 . 3))))
  1113. (pass-if (equal? '(1 2 3 . 4) (list-copy '(1 2 3 . 4))))
  1114. (pass-if (equal? '(1 2 3 4 . 5) (list-copy '(1 2 3 4 . 5)))))
  1115. ;;
  1116. ;; list-index
  1117. ;;
  1118. (with-test-prefix "list-index"
  1119. (pass-if-exception "no args" exception:wrong-num-args
  1120. (list-index))
  1121. (pass-if-exception "one arg" exception:wrong-num-args
  1122. (list-index noop))
  1123. (with-test-prefix "one list"
  1124. (pass-if "empty list" (eq? #f (list-index symbol? '())))
  1125. (pass-if-exception "pred arg count 0" exception:wrong-num-args
  1126. (list-index (lambda () x) '(1 2 3)))
  1127. (pass-if-exception "pred arg count 2" exception:wrong-num-args
  1128. (list-index (lambda (x y) x) '(1 2 3)))
  1129. (pass-if-exception "improper 1" exception:wrong-type-arg
  1130. (list-index symbol? 1))
  1131. (pass-if-exception "improper 2" exception:wrong-type-arg
  1132. (list-index symbol? '(1 . 2)))
  1133. (pass-if-exception "improper 3" exception:wrong-type-arg
  1134. (list-index symbol? '(1 2 . 3)))
  1135. (pass-if (eqv? #f (list-index symbol? '(1))))
  1136. (pass-if (eqv? 0 (list-index symbol? '(x))))
  1137. (pass-if (eqv? #f (list-index symbol? '(1 2))))
  1138. (pass-if (eqv? 0 (list-index symbol? '(x 1))))
  1139. (pass-if (eqv? 1 (list-index symbol? '(1 x))))
  1140. (pass-if (eqv? #f (list-index symbol? '(1 2 3))))
  1141. (pass-if (eqv? 0 (list-index symbol? '(x 1 2))))
  1142. (pass-if (eqv? 1 (list-index symbol? '(1 x 2))))
  1143. (pass-if (eqv? 2 (list-index symbol? '(1 2 x)))))
  1144. (with-test-prefix "two lists"
  1145. (define (sym1 x y)
  1146. (symbol? x))
  1147. (define (sym2 x y)
  1148. (symbol? y))
  1149. (pass-if "arg order"
  1150. (eqv? 0 (list-index (lambda (x y)
  1151. (and (= 1 x)
  1152. (= 2 y)))
  1153. '(1) '(2))))
  1154. (pass-if "empty lists" (eqv? #f (list-index sym2 '() '())))
  1155. (pass-if-exception "pred arg count 0" exception:wrong-num-args
  1156. (list-index (lambda () #t) '(1 2 3) '(1 2 3)))
  1157. (pass-if-exception "pred arg count 1" exception:wrong-num-args
  1158. (list-index (lambda (x) x) '(1 2 3) '(1 2 3)))
  1159. (pass-if-exception "pred arg count 3" exception:wrong-num-args
  1160. (list-index (lambda (x y z) x) '(1 2 3) '(1 2 3)))
  1161. (pass-if-exception "improper first 1" exception:wrong-type-arg
  1162. (list-index sym2 1 '(1 2 3)))
  1163. (pass-if-exception "improper first 2" exception:wrong-type-arg
  1164. (list-index sym2 '(1 . 2) '(1 2 3)))
  1165. (pass-if-exception "improper first 3" exception:wrong-type-arg
  1166. (list-index sym2 '(1 2 . 3) '(1 2 3)))
  1167. (pass-if-exception "improper second 1" exception:wrong-type-arg
  1168. (list-index sym2 '(1 2 3) 1))
  1169. (pass-if-exception "improper second 2" exception:wrong-type-arg
  1170. (list-index sym2 '(1 2 3) '(1 . 2)))
  1171. (pass-if-exception "improper second 3" exception:wrong-type-arg
  1172. (list-index sym2 '(1 2 3) '(1 2 . 3)))
  1173. (pass-if (eqv? #f (list-index sym2 '(1) '(2))))
  1174. (pass-if (eqv? 0 (list-index sym2 '(1) '(x))))
  1175. (pass-if (eqv? #f (list-index sym2 '(1 2) '(3 4))))
  1176. (pass-if (eqv? 0 (list-index sym2 '(1 2) '(x 3))))
  1177. (pass-if (eqv? 1 (list-index sym2 '(1 2) '(3 x))))
  1178. (pass-if (eqv? #f (list-index sym2 '(1 2 3) '(3 4 5))))
  1179. (pass-if (eqv? 0 (list-index sym2 '(1 2 3) '(x 3 4))))
  1180. (pass-if (eqv? 1 (list-index sym2 '(1 2 3) '(3 x 4))))
  1181. (pass-if (eqv? 2 (list-index sym2 '(1 2 3) '(3 4 x))))
  1182. (with-test-prefix "stop shortest"
  1183. (pass-if (eqv? #f (list-index sym1 '(1 2 x) '(4 5))))
  1184. (pass-if (eqv? #f (list-index sym2 '(4 5) '(1 2 x))))
  1185. (pass-if (eqv? #f (list-index sym1 '(3 4) '(1 2 x y))))
  1186. (pass-if (eqv? #f (list-index sym2 '(1 2 x y) '(3 4))))))
  1187. (with-test-prefix "three lists"
  1188. (define (sym1 x y z)
  1189. (symbol? x))
  1190. (define (sym2 x y z)
  1191. (symbol? y))
  1192. (define (sym3 x y z)
  1193. (symbol? z))
  1194. (pass-if "arg order"
  1195. (eqv? 0 (list-index (lambda (x y z)
  1196. (and (= 1 x)
  1197. (= 2 y)
  1198. (= 3 z)))
  1199. '(1) '(2) '(3))))
  1200. (pass-if "empty lists" (eqv? #f (list-index sym3 '() '() '())))
  1201. ;; currently bad pred argument gives wrong-num-args when 3 or more
  1202. ;; lists, as opposed to wrong-type-arg for 1 or 2 lists
  1203. (pass-if-exception "pred arg count 0" exception:wrong-num-args
  1204. (list-index (lambda () #t) '(1 2 3) '(1 2 3) '(1 2 3)))
  1205. (pass-if-exception "pred arg count 2" exception:wrong-num-args
  1206. (list-index (lambda (x y) x) '(1 2 3) '(1 2 3)'(1 2 3) ))
  1207. (pass-if-exception "pred arg count 4" exception:wrong-num-args
  1208. (list-index (lambda (w x y z) x) '(1 2 3) '(1 2 3) '(1 2 3)))
  1209. (pass-if-exception "improper first 1" exception:wrong-type-arg
  1210. (list-index sym3 1 '(1 2 3) '(1 2 3)))
  1211. (pass-if-exception "improper first 2" exception:wrong-type-arg
  1212. (list-index sym3 '(1 . 2) '(1 2 3) '(1 2 3)))
  1213. (pass-if-exception "improper first 3" exception:wrong-type-arg
  1214. (list-index sym3 '(1 2 . 3) '(1 2 3) '(1 2 3)))
  1215. (pass-if-exception "improper second 1" exception:wrong-type-arg
  1216. (list-index sym3 '(1 2 3) 1 '(1 2 3)))
  1217. (pass-if-exception "improper second 2" exception:wrong-type-arg
  1218. (list-index sym3 '(1 2 3) '(1 . 2) '(1 2 3)))
  1219. (pass-if-exception "improper second 3" exception:wrong-type-arg
  1220. (list-index sym3 '(1 2 3) '(1 2 . 3) '(1 2 3)))
  1221. (pass-if-exception "improper third 1" exception:wrong-type-arg
  1222. (list-index sym3 '(1 2 3) '(1 2 3) 1))
  1223. (pass-if-exception "improper third 2" exception:wrong-type-arg
  1224. (list-index sym3 '(1 2 3) '(1 2 3) '(1 . 2)))
  1225. (pass-if-exception "improper third 3" exception:wrong-type-arg
  1226. (list-index sym3 '(1 2 3) '(1 2 3) '(1 2 . 3)))
  1227. (pass-if (eqv? #f (list-index sym3 '(#f) '(#f) '(#f))))
  1228. (pass-if (eqv? 0 (list-index sym3 '(#f) '(#f) '(x))))
  1229. (pass-if (eqv? #f (list-index sym3 '(#f #f) '(#f #f) '(#f #f))))
  1230. (pass-if (eqv? 0 (list-index sym3 '(#f #f) '(#f #f) '(x #f))))
  1231. (pass-if (eqv? 1 (list-index sym3 '(#f #f) '(#f #f) '(#f x))))
  1232. (pass-if (eqv? #f (list-index sym3 '(#f #f #f) '(#f #f #f) '(#f #f #f))))
  1233. (pass-if (eqv? 0 (list-index sym3 '(#f #f #f) '(#f #f #f) '(x #f #f))))
  1234. (pass-if (eqv? 1 (list-index sym3 '(#f #f #f) '(#f #f #f) '(#f x #f))))
  1235. (pass-if (eqv? 2 (list-index sym3 '(#f #f #f) '(#f #f #f) '(#f #f x))))
  1236. (with-test-prefix "stop shortest"
  1237. (pass-if (eqv? #f (list-index sym2 '() '(x x x) '(x x))))
  1238. (pass-if (eqv? #f (list-index sym1 '(x x x) '() '(x x))))
  1239. (pass-if (eqv? #f (list-index sym2 '(x x x) '(x x) '())))
  1240. (pass-if (eqv? #f (list-index sym2 '(#t) '(#t x x) '(#t x))))
  1241. (pass-if (eqv? #f (list-index sym1 '(#t x x) '(#t) '(#t x))))
  1242. (pass-if (eqv? #f (list-index sym1 '(#t x x) '(#t x) '(#t)))))
  1243. (pass-if "apply list unchanged"
  1244. (let ((lst (list (list 1 2) (list 3 4) (list 5 6))))
  1245. (and (equal? #f (apply list-index sym3 lst))
  1246. ;; lst unmodified
  1247. (equal? '((1 2) (3 4) (5 6)) lst))))))
  1248. ;;
  1249. ;; list-tabulate
  1250. ;;
  1251. (with-test-prefix "list-tabulate"
  1252. (pass-if-exception "-1" exception:wrong-type-arg
  1253. (list-tabulate -1 identity))
  1254. (pass-if "0"
  1255. (equal? '() (list-tabulate 0 identity)))
  1256. (pass-if "1"
  1257. (equal? '(0) (list-tabulate 1 identity)))
  1258. (pass-if "2"
  1259. (equal? '(0 1) (list-tabulate 2 identity)))
  1260. (pass-if "3"
  1261. (equal? '(0 1 2) (list-tabulate 3 identity)))
  1262. (pass-if "4"
  1263. (equal? '(0 1 2 3) (list-tabulate 4 identity)))
  1264. (pass-if "string ref proc"
  1265. (equal? '(#\a #\b #\c #\d) (list-tabulate 4
  1266. (lambda (i)
  1267. (string-ref "abcd" i))))))
  1268. ;;
  1269. ;; lset=
  1270. ;;
  1271. (with-test-prefix "lset="
  1272. ;; in guile 1.6.7 and earlier, lset= incorrectly demanded at least one
  1273. ;; list arg
  1274. (pass-if "no args"
  1275. (eq? #t (lset= eq?)))
  1276. (with-test-prefix "one arg"
  1277. (pass-if "()"
  1278. (eq? #t (lset= eqv? '())))
  1279. (pass-if "(1)"
  1280. (eq? #t (lset= eqv? '(1))))
  1281. (pass-if "(1 2)"
  1282. (eq? #t (lset= eqv? '(1 2)))))
  1283. (with-test-prefix "two args"
  1284. (pass-if "() ()"
  1285. (eq? #t (lset= eqv? '() '())))
  1286. (pass-if "(1) (1)"
  1287. (eq? #t (lset= eqv? '(1) '(1))))
  1288. (pass-if "(1) (2)"
  1289. (eq? #f (lset= eqv? '(1) '(2))))
  1290. (pass-if "(1) (1 2)"
  1291. (eq? #f (lset= eqv? '(1) '(1 2))))
  1292. (pass-if "(1 2) (2 1)"
  1293. (eq? #t (lset= eqv? '(1 2) '(2 1))))
  1294. (pass-if "called arg order"
  1295. (let ((good #t))
  1296. (lset= (lambda (x y)
  1297. (if (not (= x (1- y)))
  1298. (set! good #f))
  1299. #t)
  1300. '(1 1) '(2 2))
  1301. good)))
  1302. (with-test-prefix "three args"
  1303. (pass-if "() () ()"
  1304. (eq? #t (lset= eqv? '() '() '())))
  1305. (pass-if "(1) (1) (1)"
  1306. (eq? #t (lset= eqv? '(1) '(1) '(1))))
  1307. (pass-if "(1) (1) (2)"
  1308. (eq? #f (lset= eqv? '(1) '(1) '(2))))
  1309. (pass-if "(1) (1) (1 2)"
  1310. (eq? #f (lset= eqv? '(1) '(1) '(1 2))))
  1311. (pass-if "(1 2 3) (3 2 1) (1 3 2)"
  1312. (eq? #t (lset= eqv? '(1 2 3) '(3 2 1) '(1 3 2))))
  1313. (pass-if "called arg order"
  1314. (let ((good #t))
  1315. (lset= (lambda (x y)
  1316. (if (not (= x (1- y)))
  1317. (set! good #f))
  1318. #t)
  1319. '(1 1) '(2 2) '(3 3))
  1320. good))))
  1321. ;;
  1322. ;; lset-adjoin
  1323. ;;
  1324. (with-test-prefix "lset-adjoin"
  1325. ;; in guile 1.6.7 and earlier, lset-adjoin didn't actually use the given
  1326. ;; `=' procedure, all comparisons were just with `equal?
  1327. ;;
  1328. (with-test-prefix "case-insensitive ="
  1329. (pass-if "(\"x\") \"X\""
  1330. (equal? '("x") (lset-adjoin string-ci=? '("x") "X"))))
  1331. (pass-if "called arg order"
  1332. (let ((good #f))
  1333. (lset-adjoin (lambda (x y)
  1334. (set! good (and (= x 1) (= y 2)))
  1335. (= x y))
  1336. '(1) 2)
  1337. good))
  1338. (pass-if (equal? '() (lset-adjoin = '())))
  1339. (pass-if (equal? '(1) (lset-adjoin = '() 1)))
  1340. (pass-if (equal? '(1) (lset-adjoin = '() 1 1)))
  1341. (pass-if (equal? '(2 1) (lset-adjoin = '() 1 2)))
  1342. (pass-if (equal? '(3 1 2) (lset-adjoin = '(1 2) 1 2 3 2 1)))
  1343. (pass-if "apply list unchanged"
  1344. (let ((lst (list 1 2)))
  1345. (and (equal? '(2 1 3) (apply lset-adjoin = '(3) lst))
  1346. ;; lst unmodified
  1347. (equal? '(1 2) lst))))
  1348. (pass-if "(1 1) 1 1"
  1349. (equal? '(1 1) (lset-adjoin = '(1 1) 1 1)))
  1350. ;; duplicates among args are cast out
  1351. (pass-if "(2) 1 1"
  1352. (equal? '(1 2) (lset-adjoin = '(2) 1 1))))
  1353. ;;
  1354. ;; lset-difference
  1355. ;;
  1356. (with-test-prefix "lset-difference"
  1357. (pass-if "called arg order"
  1358. (let ((good #f))
  1359. (lset-difference (lambda (x y)
  1360. (set! good (and (= x 1) (= y 2)))
  1361. (= x y))
  1362. '(1) '(2))
  1363. good)))
  1364. ;;
  1365. ;; lset-difference!
  1366. ;;
  1367. (with-test-prefix "lset-difference!"
  1368. (pass-if-exception "proc - num" exception:wrong-type-arg
  1369. (lset-difference! 123 '(4)))
  1370. (pass-if-exception "proc - list" exception:wrong-type-arg
  1371. (lset-difference! (list 1 2 3) '(4)))
  1372. (pass-if "called arg order"
  1373. (let ((good #f))
  1374. (lset-difference! (lambda (x y)
  1375. (set! good (and (= x 1) (= y 2)))
  1376. (= x y))
  1377. (list 1) (list 2))
  1378. good))
  1379. (pass-if (equal? '() (lset-difference! = '())))
  1380. (pass-if (equal? '(1) (lset-difference! = (list 1))))
  1381. (pass-if (equal? '(1 2) (lset-difference! = (list 1 2))))
  1382. (pass-if (equal? '() (lset-difference! = (list ) '(3))))
  1383. (pass-if (equal? '() (lset-difference! = (list 3) '(3))))
  1384. (pass-if (equal? '(1) (lset-difference! = (list 1 3) '(3))))
  1385. (pass-if (equal? '(1) (lset-difference! = (list 3 1) '(3))))
  1386. (pass-if (equal? '(1) (lset-difference! = (list 1 3 3) '(3))))
  1387. (pass-if (equal? '(1) (lset-difference! = (list 3 1 3) '(3))))
  1388. (pass-if (equal? '(1) (lset-difference! = (list 3 3 1) '(3))))
  1389. (pass-if (equal? '(1) (lset-difference! = (list 1 2 3) '(2 3))))
  1390. (pass-if (equal? '(1) (lset-difference! = (list 1 2 3) '(3 2))))
  1391. (pass-if (equal? '(1) (lset-difference! = (list 1 2 3) '(3) '(2))))
  1392. (pass-if (equal? '(1) (lset-difference! = (list 1 2 3) '(2) '(3))))
  1393. (pass-if (equal? '(1) (lset-difference! = (list 1 2 3) '(2) '(2 3))))
  1394. (pass-if (equal? '(1) (lset-difference! = (list 1 2 3) '(2) '(3 2))))
  1395. (pass-if (equal? '(1 2) (lset-difference! = (list 1 2 3) '(3) '(3))))
  1396. (pass-if (equal? '(1 2) (lset-difference! = (list 1 3 2) '(3) '(3))))
  1397. (pass-if (equal? '(1 2) (lset-difference! = (list 3 1 2) '(3) '(3))))
  1398. (pass-if (equal? '(1 2 3) (lset-difference! = (list 1 2 3 4) '(4))))
  1399. (pass-if (equal? '(1 2 3) (lset-difference! = (list 1 2 4 3) '(4))))
  1400. (pass-if (equal? '(1 2 3) (lset-difference! = (list 1 4 2 3) '(4))))
  1401. (pass-if (equal? '(1 2 3) (lset-difference! = (list 4 1 2 3) '(4))))
  1402. (pass-if (equal? '(1 2) (lset-difference! = (list 1 2 3 4) '(4) '(3))))
  1403. (pass-if (equal? '(1 2) (lset-difference! = (list 1 3 2 4) '(4) '(3))))
  1404. (pass-if (equal? '(1 2) (lset-difference! = (list 3 1 2 4) '(4) '(3))))
  1405. (pass-if (equal? '(1 2) (lset-difference! = (list 1 3 4 2) '(4) '(3))))
  1406. (pass-if (equal? '(1 2) (lset-difference! = (list 3 1 4 2) '(4) '(3))))
  1407. (pass-if (equal? '(1 2) (lset-difference! = (list 3 4 1 2) '(4) '(3)))))
  1408. ;;
  1409. ;; lset-diff+intersection
  1410. ;;
  1411. (with-test-prefix "lset-diff+intersection"
  1412. (pass-if "called arg order"
  1413. (let ((good #f))
  1414. (lset-diff+intersection (lambda (x y)
  1415. (set! good (and (= x 1) (= y 2)))
  1416. (= x y))
  1417. '(1) '(2))
  1418. good)))
  1419. ;;
  1420. ;; lset-diff+intersection!
  1421. ;;
  1422. (with-test-prefix "lset-diff+intersection"
  1423. (pass-if "called arg order"
  1424. (let ((good #f))
  1425. (lset-diff+intersection (lambda (x y)
  1426. (set! good (and (= x 1) (= y 2)))
  1427. (= x y))
  1428. (list 1) (list 2))
  1429. good)))
  1430. ;;
  1431. ;; lset-intersection
  1432. ;;
  1433. (with-test-prefix "lset-intersection"
  1434. (pass-if "called arg order"
  1435. (let ((good #f))
  1436. (lset-intersection (lambda (x y)
  1437. (set! good (and (= x 1) (= y 2)))
  1438. (= x y))
  1439. '(1) '(2))
  1440. good)))
  1441. ;;
  1442. ;; lset-intersection!
  1443. ;;
  1444. (with-test-prefix "lset-intersection"
  1445. (pass-if "called arg order"
  1446. (let ((good #f))
  1447. (lset-intersection (lambda (x y)
  1448. (set! good (and (= x 1) (= y 2)))
  1449. (= x y))
  1450. (list 1) (list 2))
  1451. good)))
  1452. ;;
  1453. ;; lset-union
  1454. ;;
  1455. (with-test-prefix "lset-union"
  1456. (pass-if "no args"
  1457. (eq? '() (lset-union eq?)))
  1458. (pass-if "one arg"
  1459. (equal? '(1 2 3) (lset-union eq? '(1 2 3))))
  1460. (pass-if "'() '()"
  1461. (equal? '() (lset-union eq? '() '())))
  1462. (pass-if "'() '(1 2 3)"
  1463. (equal? '(1 2 3) (lset-union eq? '() '(1 2 3))))
  1464. (pass-if "'(1 2 3) '()"
  1465. (equal? '(1 2 3) (lset-union eq? '(1 2 3) '())))
  1466. (pass-if "'(1 2 3) '(4 3 5)"
  1467. (equal? '(5 4 1 2 3) (lset-union eq? '(1 2 3) '(4 3 5))))
  1468. (pass-if "'(1 2 3) '(4) '(3 5))"
  1469. (equal? '(5 4 1 2 3) (lset-union eq? '(1 2 3) '(4) '(3 5))))
  1470. ;; in guile 1.6.7 and earlier, `=' was called with the arguments the wrong
  1471. ;; way around
  1472. (pass-if "called arg order"
  1473. (let ((good #f))
  1474. (lset-union (lambda (x y)
  1475. (set! good (and (= x 1) (= y 2)))
  1476. (= x y))
  1477. '(1) '(2))
  1478. good)))
  1479. ;;
  1480. ;; member
  1481. ;;
  1482. (with-test-prefix "member"
  1483. (pass-if-exception "no args" exception:wrong-num-args
  1484. (member))
  1485. (pass-if-exception "one arg" exception:wrong-num-args
  1486. (member 1))
  1487. (pass-if "1 (1 2 3)"
  1488. (let ((lst '(1 2 3)))
  1489. (eq? lst (member 1 lst))))
  1490. (pass-if "2 (1 2 3)"
  1491. (let ((lst '(1 2 3)))
  1492. (eq? (cdr lst) (member 2 lst))))
  1493. (pass-if "3 (1 2 3)"
  1494. (let ((lst '(1 2 3)))
  1495. (eq? (cddr lst) (member 3 lst))))
  1496. (pass-if "4 (1 2 3)"
  1497. (let ((lst '(1 2 3)))
  1498. (eq? #f (member 4 lst))))
  1499. (pass-if "called arg order"
  1500. (let ((good #f))
  1501. (member 1 '(2) (lambda (x y)
  1502. (set! good (and (eqv? 1 x)
  1503. (eqv? 2 y)))))
  1504. good)))
  1505. ;;
  1506. ;; ninth
  1507. ;;
  1508. (with-test-prefix "ninth"
  1509. (pass-if-exception "() -1" exception:wrong-type-arg
  1510. (ninth '(a b c d e f g h)))
  1511. (pass-if (eq? 'i (ninth '(a b c d e f g h i))))
  1512. (pass-if (eq? 'i (ninth '(a b c d e f g h i j)))))
  1513. ;;
  1514. ;; not-pair?
  1515. ;;
  1516. (with-test-prefix "not-pair?"
  1517. (pass-if "inum"
  1518. (eq? #t (not-pair? 123)))
  1519. (pass-if "pair"
  1520. (eq? #f (not-pair? '(x . y))))
  1521. (pass-if "symbol"
  1522. (eq? #t (not-pair? 'x))))
  1523. ;;
  1524. ;; take
  1525. ;;
  1526. (with-test-prefix "take"
  1527. (pass-if "'() 0"
  1528. (null? (take '() 0)))
  1529. (pass-if "'(a) 0"
  1530. (null? (take '(a) 0)))
  1531. (pass-if "'(a b) 0"
  1532. (null? (take '() 0)))
  1533. (pass-if "'(a b c) 0"
  1534. (null? (take '() 0)))
  1535. (pass-if "'(a) 1"
  1536. (let* ((lst '(a))
  1537. (got (take lst 1)))
  1538. (and (equal? '(a) got)
  1539. (not (eq? lst got)))))
  1540. (pass-if "'(a b) 1"
  1541. (equal? '(a)
  1542. (take '(a b) 1)))
  1543. (pass-if "'(a b c) 1"
  1544. (equal? '(a)
  1545. (take '(a b c) 1)))
  1546. (pass-if "'(a b) 2"
  1547. (let* ((lst '(a b))
  1548. (got (take lst 2)))
  1549. (and (equal? '(a b) got)
  1550. (not (eq? lst got)))))
  1551. (pass-if "'(a b c) 2"
  1552. (equal? '(a b)
  1553. (take '(a b c) 2)))
  1554. (pass-if "circular '(a) 0"
  1555. (equal? '()
  1556. (take (circular-list 'a) 0)))
  1557. (pass-if "circular '(a) 1"
  1558. (equal? '(a)
  1559. (take (circular-list 'a) 1)))
  1560. (pass-if "circular '(a) 2"
  1561. (equal? '(a a)
  1562. (take (circular-list 'a) 2)))
  1563. (pass-if "circular '(a b) 5"
  1564. (equal? '(a b a b a)
  1565. (take (circular-list 'a 'b) 5)))
  1566. (pass-if "'(a . b) 1"
  1567. (equal? '(a)
  1568. (take '(a . b) 1)))
  1569. (pass-if "'(a b . c) 1"
  1570. (equal? '(a)
  1571. (take '(a b . c) 1)))
  1572. (pass-if "'(a b . c) 2"
  1573. (equal? '(a b)
  1574. (take '(a b . c) 2))))
  1575. ;;
  1576. ;; take-while
  1577. ;;
  1578. (with-test-prefix "take-while"
  1579. (pass-if (equal? '() (take-while odd? '())))
  1580. (pass-if (equal? '(1) (take-while odd? '(1))))
  1581. (pass-if (equal? '(1 3) (take-while odd? '(1 3))))
  1582. (pass-if (equal? '(1 3 5) (take-while odd? '(1 3 5))))
  1583. (pass-if (equal? '() (take-while odd? '(2))))
  1584. (pass-if (equal? '(1) (take-while odd? '(1 2))))
  1585. (pass-if (equal? '(1 3) (take-while odd? '(1 3 4))))
  1586. (pass-if (equal? '() (take-while odd? '(2 1))))
  1587. (pass-if (equal? '(1) (take-while odd? '(1 4 3))))
  1588. (pass-if (equal? '() (take-while odd? '(4 1 3)))))
  1589. ;;
  1590. ;; take-while!
  1591. ;;
  1592. (with-test-prefix "take-while!"
  1593. (pass-if (equal? '() (take-while! odd? '())))
  1594. (pass-if (equal? '(1) (take-while! odd? (list 1))))
  1595. (pass-if (equal? '(1 3) (take-while! odd? (list 1 3))))
  1596. (pass-if (equal? '(1 3 5) (take-while! odd? (list 1 3 5))))
  1597. (pass-if (equal? '() (take-while! odd? (list 2))))
  1598. (pass-if (equal? '(1) (take-while! odd? (list 1 2))))
  1599. (pass-if (equal? '(1 3) (take-while! odd? (list 1 3 4))))
  1600. (pass-if (equal? '() (take-while! odd? (list 2 1))))
  1601. (pass-if (equal? '(1) (take-while! odd? (list 1 4 3))))
  1602. (pass-if (equal? '() (take-while! odd? (list 4 1 3)))))
  1603. ;;
  1604. ;; partition
  1605. ;;
  1606. (define (test-partition pred list kept-good dropped-good)
  1607. (call-with-values (lambda ()
  1608. (partition pred list))
  1609. (lambda (kept dropped)
  1610. (and (equal? kept kept-good)
  1611. (equal? dropped dropped-good)))))
  1612. (with-test-prefix "partition"
  1613. (pass-if "with dropped tail"
  1614. (test-partition even? '(1 2 3 4 5 6 7)
  1615. '(2 4 6) '(1 3 5 7)))
  1616. (pass-if "with kept tail"
  1617. (test-partition even? '(1 2 3 4 5 6)
  1618. '(2 4 6) '(1 3 5)))
  1619. (pass-if "with everything dropped"
  1620. (test-partition even? '(1 3 5 7)
  1621. '() '(1 3 5 7)))
  1622. (pass-if "with everything kept"
  1623. (test-partition even? '(2 4 6)
  1624. '(2 4 6) '()))
  1625. (pass-if "with empty list"
  1626. (test-partition even? '()
  1627. '() '()))
  1628. (pass-if "with reasonably long list"
  1629. ;; the old implementation from SRFI-1 reference implementation
  1630. ;; would signal a stack-overflow for a list of only 500 elements!
  1631. (call-with-values (lambda ()
  1632. (partition even?
  1633. (make-list 10000 1)))
  1634. (lambda (even odd)
  1635. (and (= (length odd) 10000)
  1636. (= (length even) 0)))))
  1637. (pass-if-exception "with improper list"
  1638. exception:wrong-type-arg
  1639. (partition symbol? '(a b . c))))
  1640. ;;
  1641. ;; partition!
  1642. ;;
  1643. (define (test-partition! pred list kept-good dropped-good)
  1644. (call-with-values (lambda ()
  1645. (partition! pred list))
  1646. (lambda (kept dropped)
  1647. (and (equal? kept kept-good)
  1648. (equal? dropped dropped-good)))))
  1649. (with-test-prefix "partition!"
  1650. (pass-if "with dropped tail"
  1651. (test-partition! even? (list 1 2 3 4 5 6 7)
  1652. '(2 4 6) '(1 3 5 7)))
  1653. (pass-if "with kept tail"
  1654. (test-partition! even? (list 1 2 3 4 5 6)
  1655. '(2 4 6) '(1 3 5)))
  1656. (pass-if "with everything dropped"
  1657. (test-partition! even? (list 1 3 5 7)
  1658. '() '(1 3 5 7)))
  1659. (pass-if "with everything kept"
  1660. (test-partition! even? (list 2 4 6)
  1661. '(2 4 6) '()))
  1662. (pass-if "with empty list"
  1663. (test-partition! even? '()
  1664. '() '()))
  1665. (pass-if "with reasonably long list"
  1666. ;; the old implementation from SRFI-1 reference implementation
  1667. ;; would signal a stack-overflow for a list of only 500 elements!
  1668. (call-with-values (lambda ()
  1669. (partition! even?
  1670. (make-list 10000 1)))
  1671. (lambda (even odd)
  1672. (and (= (length odd) 10000)
  1673. (= (length even) 0)))))
  1674. (pass-if-exception "with improper list"
  1675. exception:wrong-type-arg
  1676. (partition! symbol? (cons* 'a 'b 'c))))
  1677. ;;
  1678. ;; reduce
  1679. ;;
  1680. (with-test-prefix "reduce"
  1681. (pass-if "empty"
  1682. (let* ((calls '())
  1683. (ret (reduce (lambda (x prev)
  1684. (set! calls (cons (list x prev) calls))
  1685. x)
  1686. 1 '())))
  1687. (and (equal? calls '())
  1688. (equal? ret 1))))
  1689. (pass-if "one elem"
  1690. (let* ((calls '())
  1691. (ret (reduce (lambda (x prev)
  1692. (set! calls (cons (list x prev) calls))
  1693. x)
  1694. 1 '(2))))
  1695. (and (equal? calls '())
  1696. (equal? ret 2))))
  1697. (pass-if "two elems"
  1698. (let* ((calls '())
  1699. (ret (reduce (lambda (x prev)
  1700. (set! calls (cons (list x prev) calls))
  1701. x)
  1702. 1 '(2 3))))
  1703. (and (equal? calls '((3 2)))
  1704. (equal? ret 3))))
  1705. (pass-if "three elems"
  1706. (let* ((calls '())
  1707. (ret (reduce (lambda (x prev)
  1708. (set! calls (cons (list x prev) calls))
  1709. x)
  1710. 1 '(2 3 4))))
  1711. (and (equal? calls '((4 3)
  1712. (3 2)))
  1713. (equal? ret 4))))
  1714. (pass-if "four elems"
  1715. (let* ((calls '())
  1716. (ret (reduce (lambda (x prev)
  1717. (set! calls (cons (list x prev) calls))
  1718. x)
  1719. 1 '(2 3 4 5))))
  1720. (and (equal? calls '((5 4)
  1721. (4 3)
  1722. (3 2)))
  1723. (equal? ret 5)))))
  1724. ;;
  1725. ;; reduce-right
  1726. ;;
  1727. (with-test-prefix "reduce-right"
  1728. (pass-if "empty"
  1729. (let* ((calls '())
  1730. (ret (reduce-right (lambda (x prev)
  1731. (set! calls (cons (list x prev) calls))
  1732. x)
  1733. 1 '())))
  1734. (and (equal? calls '())
  1735. (equal? ret 1))))
  1736. (pass-if "one elem"
  1737. (let* ((calls '())
  1738. (ret (reduce-right (lambda (x prev)
  1739. (set! calls (cons (list x prev) calls))
  1740. x)
  1741. 1 '(2))))
  1742. (and (equal? calls '())
  1743. (equal? ret 2))))
  1744. (pass-if "two elems"
  1745. (let* ((calls '())
  1746. (ret (reduce-right (lambda (x prev)
  1747. (set! calls (cons (list x prev) calls))
  1748. x)
  1749. 1 '(2 3))))
  1750. (and (equal? calls '((2 3)))
  1751. (equal? ret 2))))
  1752. (pass-if "three 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 4))))
  1758. (and (equal? calls '((2 3)
  1759. (3 4)))
  1760. (equal? ret 2))))
  1761. (pass-if "four elems"
  1762. (let* ((calls '())
  1763. (ret (reduce-right (lambda (x prev)
  1764. (set! calls (cons (list x prev) calls))
  1765. x)
  1766. 1 '(2 3 4 5))))
  1767. (and (equal? calls '((2 3)
  1768. (3 4)
  1769. (4 5)))
  1770. (equal? ret 2)))))
  1771. ;;
  1772. ;; remove
  1773. ;;
  1774. (with-test-prefix "remove"
  1775. (pass-if (equal? '() (remove odd? '())))
  1776. (pass-if (equal? '() (remove odd? '(1))))
  1777. (pass-if (equal? '(2) (remove odd? '(2))))
  1778. (pass-if (equal? '() (remove odd? '(1 3))))
  1779. (pass-if (equal? '(2) (remove odd? '(2 3))))
  1780. (pass-if (equal? '(2) (remove odd? '(1 2))))
  1781. (pass-if (equal? '(2 4) (remove odd? '(2 4))))
  1782. (pass-if (equal? '() (remove odd? '(1 3 5))))
  1783. (pass-if (equal? '(2) (remove odd? '(2 3 5))))
  1784. (pass-if (equal? '(2) (remove odd? '(1 2 5))))
  1785. (pass-if (equal? '(2 4) (remove odd? '(2 4 5))))
  1786. (pass-if (equal? '(6) (remove odd? '(1 3 6))))
  1787. (pass-if (equal? '(2 6) (remove odd? '(2 3 6))))
  1788. (pass-if (equal? '(2 6) (remove odd? '(1 2 6))))
  1789. (pass-if (equal? '(2 4 6) (remove odd? '(2 4 6)))))
  1790. ;;
  1791. ;; remove!
  1792. ;;
  1793. (with-test-prefix "remove!"
  1794. (pass-if (equal? '() (remove! odd? '())))
  1795. (pass-if (equal? '() (remove! odd? (list 1))))
  1796. (pass-if (equal? '(2) (remove! odd? (list 2))))
  1797. (pass-if (equal? '() (remove! odd? (list 1 3))))
  1798. (pass-if (equal? '(2) (remove! odd? (list 2 3))))
  1799. (pass-if (equal? '(2) (remove! odd? (list 1 2))))
  1800. (pass-if (equal? '(2 4) (remove! odd? (list 2 4))))
  1801. (pass-if (equal? '() (remove! odd? (list 1 3 5))))
  1802. (pass-if (equal? '(2) (remove! odd? (list 2 3 5))))
  1803. (pass-if (equal? '(2) (remove! odd? (list 1 2 5))))
  1804. (pass-if (equal? '(2 4) (remove! odd? (list 2 4 5))))
  1805. (pass-if (equal? '(6) (remove! odd? (list 1 3 6))))
  1806. (pass-if (equal? '(2 6) (remove! odd? (list 2 3 6))))
  1807. (pass-if (equal? '(2 6) (remove! odd? (list 1 2 6))))
  1808. (pass-if (equal? '(2 4 6) (remove! odd? (list 2 4 6)))))
  1809. ;;
  1810. ;; seventh
  1811. ;;
  1812. (with-test-prefix "seventh"
  1813. (pass-if-exception "() -1" exception:wrong-type-arg
  1814. (seventh '(a b c d e f)))
  1815. (pass-if (eq? 'g (seventh '(a b c d e f g))))
  1816. (pass-if (eq? 'g (seventh '(a b c d e f g h)))))
  1817. ;;
  1818. ;; sixth
  1819. ;;
  1820. (with-test-prefix "sixth"
  1821. (pass-if-exception "() -1" exception:wrong-type-arg
  1822. (sixth '(a b c d e)))
  1823. (pass-if (eq? 'f (sixth '(a b c d e f))))
  1824. (pass-if (eq? 'f (sixth '(a b c d e f g)))))
  1825. ;;
  1826. ;; split-at
  1827. ;;
  1828. (with-test-prefix "split-at"
  1829. (define (equal-values? lst thunk)
  1830. (call-with-values thunk
  1831. (lambda got
  1832. (equal? lst got))))
  1833. (pass-if-exception "() -1" exception:out-of-range
  1834. (split-at '() -1))
  1835. (pass-if (equal-values? '(() ())
  1836. (lambda () (split-at '() 0))))
  1837. (pass-if-exception "() 1" exception:wrong-type-arg
  1838. (split-at '() 1))
  1839. (pass-if-exception "(1) -1" exception:out-of-range
  1840. (split-at '(1) -1))
  1841. (pass-if (equal-values? '(() (1)) (lambda () (split-at '(1) 0))))
  1842. (pass-if (equal-values? '((1) ()) (lambda () (split-at '(1) 1))))
  1843. (pass-if-exception "(1) 2" exception:wrong-type-arg
  1844. (split-at '(1) 2))
  1845. (pass-if-exception "(4 5) -1" exception:out-of-range
  1846. (split-at '(4 5) -1))
  1847. (pass-if (equal-values? '(() (4 5)) (lambda () (split-at '(4 5) 0))))
  1848. (pass-if (equal-values? '((4) (5)) (lambda () (split-at '(4 5) 1))))
  1849. (pass-if (equal-values? '((4 5) ()) (lambda () (split-at '(4 5) 2))))
  1850. (pass-if-exception "(4 5) 3" exception:wrong-type-arg
  1851. (split-at '(4 5) 3))
  1852. (pass-if-exception "(4 5 6) -1" exception:out-of-range
  1853. (split-at '(4 5 6) -1))
  1854. (pass-if (equal-values? '(() (4 5 6)) (lambda () (split-at '(4 5 6) 0))))
  1855. (pass-if (equal-values? '((4) (5 6)) (lambda () (split-at '(4 5 6) 1))))
  1856. (pass-if (equal-values? '((4 5) (6)) (lambda () (split-at '(4 5 6) 2))))
  1857. (pass-if (equal-values? '((4 5 6) ()) (lambda () (split-at '(4 5 6) 3))))
  1858. (pass-if-exception "(4 5 6) 4" exception:wrong-type-arg
  1859. (split-at '(4 5 6) 4)))
  1860. ;;
  1861. ;; split-at!
  1862. ;;
  1863. (with-test-prefix "split-at!"
  1864. (define (equal-values? lst thunk)
  1865. (call-with-values thunk
  1866. (lambda got
  1867. (equal? lst got))))
  1868. (pass-if-exception "() -1" exception:out-of-range
  1869. (split-at! '() -1))
  1870. (pass-if (equal-values? '(() ())
  1871. (lambda () (split-at! '() 0))))
  1872. (pass-if-exception "() 1" exception:wrong-type-arg
  1873. (split-at! '() 1))
  1874. (pass-if-exception "(1) -1" exception:out-of-range
  1875. (split-at! (list 1) -1))
  1876. (pass-if (equal-values? '(() (1)) (lambda () (split-at! (list 1) 0))))
  1877. (pass-if (equal-values? '((1) ()) (lambda () (split-at! (list 1) 1))))
  1878. (pass-if-exception "(1) 2" exception:wrong-type-arg
  1879. (split-at! (list 1) 2))
  1880. (pass-if-exception "(4 5) -1" exception:out-of-range
  1881. (split-at! (list 4 5) -1))
  1882. (pass-if (equal-values? '(() (4 5)) (lambda () (split-at! (list 4 5) 0))))
  1883. (pass-if (equal-values? '((4) (5)) (lambda () (split-at! (list 4 5) 1))))
  1884. (pass-if (equal-values? '((4 5) ()) (lambda () (split-at! (list 4 5) 2))))
  1885. (pass-if-exception "(4 5) 3" exception:wrong-type-arg
  1886. (split-at! (list 4 5) 3))
  1887. (pass-if-exception "(4 5 6) -1" exception:out-of-range
  1888. (split-at! (list 4 5 6) -1))
  1889. (pass-if (equal-values? '(() (4 5 6)) (lambda () (split-at! (list 4 5 6) 0))))
  1890. (pass-if (equal-values? '((4) (5 6)) (lambda () (split-at! (list 4 5 6) 1))))
  1891. (pass-if (equal-values? '((4 5) (6)) (lambda () (split-at! (list 4 5 6) 2))))
  1892. (pass-if (equal-values? '((4 5 6) ()) (lambda () (split-at! (list 4 5 6) 3))))
  1893. (pass-if-exception "(4 5 6) 4" exception:wrong-type-arg
  1894. (split-at! (list 4 5 6) 4)))
  1895. ;;
  1896. ;; span
  1897. ;;
  1898. (with-test-prefix "span"
  1899. (define (test-span lst want-v1 want-v2)
  1900. (call-with-values
  1901. (lambda ()
  1902. (span positive? lst))
  1903. (lambda (got-v1 got-v2)
  1904. (and (equal? got-v1 want-v1)
  1905. (equal? got-v2 want-v2)))))
  1906. (pass-if "empty"
  1907. (test-span '() '() '()))
  1908. (pass-if "y"
  1909. (test-span '(1) '(1) '()))
  1910. (pass-if "n"
  1911. (test-span '(-1) '() '(-1)))
  1912. (pass-if "yy"
  1913. (test-span '(1 2) '(1 2) '()))
  1914. (pass-if "ny"
  1915. (test-span '(-1 1) '() '(-1 1)))
  1916. (pass-if "yn"
  1917. (test-span '(1 -1) '(1) '(-1)))
  1918. (pass-if "nn"
  1919. (test-span '(-1 -2) '() '(-1 -2)))
  1920. (pass-if "yyy"
  1921. (test-span '(1 2 3) '(1 2 3) '()))
  1922. (pass-if "nyy"
  1923. (test-span '(-1 1 2) '() '(-1 1 2)))
  1924. (pass-if "yny"
  1925. (test-span '(1 -1 2) '(1) '(-1 2)))
  1926. (pass-if "nny"
  1927. (test-span '(-1 -2 1) '() '(-1 -2 1)))
  1928. (pass-if "yyn"
  1929. (test-span '(1 2 -1) '(1 2) '(-1)))
  1930. (pass-if "nyn"
  1931. (test-span '(-1 1 -2) '() '(-1 1 -2)))
  1932. (pass-if "ynn"
  1933. (test-span '(1 -1 -2) '(1) '(-1 -2)))
  1934. (pass-if "nnn"
  1935. (test-span '(-1 -2 -3) '() '(-1 -2 -3))))
  1936. ;;
  1937. ;; span!
  1938. ;;
  1939. (with-test-prefix "span!"
  1940. (define (test-span! lst want-v1 want-v2)
  1941. (call-with-values
  1942. (lambda ()
  1943. (span! positive? lst))
  1944. (lambda (got-v1 got-v2)
  1945. (and (equal? got-v1 want-v1)
  1946. (equal? got-v2 want-v2)))))
  1947. (pass-if "empty"
  1948. (test-span! '() '() '()))
  1949. (pass-if "y"
  1950. (test-span! (list 1) '(1) '()))
  1951. (pass-if "n"
  1952. (test-span! (list -1) '() '(-1)))
  1953. (pass-if "yy"
  1954. (test-span! (list 1 2) '(1 2) '()))
  1955. (pass-if "ny"
  1956. (test-span! (list -1 1) '() '(-1 1)))
  1957. (pass-if "yn"
  1958. (test-span! (list 1 -1) '(1) '(-1)))
  1959. (pass-if "nn"
  1960. (test-span! (list -1 -2) '() '(-1 -2)))
  1961. (pass-if "yyy"
  1962. (test-span! (list 1 2 3) '(1 2 3) '()))
  1963. (pass-if "nyy"
  1964. (test-span! (list -1 1 2) '() '(-1 1 2)))
  1965. (pass-if "yny"
  1966. (test-span! (list 1 -1 2) '(1) '(-1 2)))
  1967. (pass-if "nny"
  1968. (test-span! (list -1 -2 1) '() '(-1 -2 1)))
  1969. (pass-if "yyn"
  1970. (test-span! (list 1 2 -1) '(1 2) '(-1)))
  1971. (pass-if "nyn"
  1972. (test-span! (list -1 1 -2) '() '(-1 1 -2)))
  1973. (pass-if "ynn"
  1974. (test-span! (list 1 -1 -2) '(1) '(-1 -2)))
  1975. (pass-if "nnn"
  1976. (test-span! (list -1 -2 -3) '() '(-1 -2 -3))))
  1977. ;;
  1978. ;; take!
  1979. ;;
  1980. (with-test-prefix "take!"
  1981. (pass-if-exception "() -1" exception:out-of-range
  1982. (take! '() -1))
  1983. (pass-if (equal? '() (take! '() 0)))
  1984. (pass-if-exception "() 1" exception:wrong-type-arg
  1985. (take! '() 1))
  1986. (pass-if-exception "(1) -1" exception:out-of-range
  1987. (take! '(1) -1))
  1988. (pass-if (equal? '() (take! '(1) 0)))
  1989. (pass-if (equal? '(1) (take! '(1) 1)))
  1990. (pass-if-exception "(1) 2" exception:wrong-type-arg
  1991. (take! '(1) 2))
  1992. (pass-if-exception "(4 5) -1" exception:out-of-range
  1993. (take! '(4 5) -1))
  1994. (pass-if (equal? '() (take! '(4 5) 0)))
  1995. (pass-if (equal? '(4) (take! '(4 5) 1)))
  1996. (pass-if (equal? '(4 5) (take! '(4 5) 2)))
  1997. (pass-if-exception "(4 5) 3" exception:wrong-type-arg
  1998. (take! '(4 5) 3))
  1999. (pass-if-exception "(4 5 6) -1" exception:out-of-range
  2000. (take! '(4 5 6) -1))
  2001. (pass-if (equal? '() (take! '(4 5 6) 0)))
  2002. (pass-if (equal? '(4) (take! '(4 5 6) 1)))
  2003. (pass-if (equal? '(4 5) (take! '(4 5 6) 2)))
  2004. (pass-if (equal? '(4 5 6) (take! '(4 5 6) 3)))
  2005. (pass-if-exception "(4 5 6) 4" exception:wrong-type-arg
  2006. (take! '(4 5 6) 4)))
  2007. ;;
  2008. ;; take-right
  2009. ;;
  2010. (with-test-prefix "take-right"
  2011. (pass-if-exception "() -1" exception:out-of-range
  2012. (take-right '() -1))
  2013. (pass-if (equal? '() (take-right '() 0)))
  2014. (pass-if-exception "() 1" exception:wrong-type-arg
  2015. (take-right '() 1))
  2016. (pass-if-exception "(1) -1" exception:out-of-range
  2017. (take-right '(1) -1))
  2018. (pass-if (equal? '() (take-right '(1) 0)))
  2019. (pass-if (equal? '(1) (take-right '(1) 1)))
  2020. (pass-if-exception "(1) 2" exception:wrong-type-arg
  2021. (take-right '(1) 2))
  2022. (pass-if-exception "(4 5) -1" exception:out-of-range
  2023. (take-right '(4 5) -1))
  2024. (pass-if (equal? '() (take-right '(4 5) 0)))
  2025. (pass-if (equal? '(5) (take-right '(4 5) 1)))
  2026. (pass-if (equal? '(4 5) (take-right '(4 5) 2)))
  2027. (pass-if-exception "(4 5) 3" exception:wrong-type-arg
  2028. (take-right '(4 5) 3))
  2029. (pass-if-exception "(4 5 6) -1" exception:out-of-range
  2030. (take-right '(4 5 6) -1))
  2031. (pass-if (equal? '() (take-right '(4 5 6) 0)))
  2032. (pass-if (equal? '(6) (take-right '(4 5 6) 1)))
  2033. (pass-if (equal? '(5 6) (take-right '(4 5 6) 2)))
  2034. (pass-if (equal? '(4 5 6) (take-right '(4 5 6) 3)))
  2035. (pass-if-exception "(4 5 6) 4" exception:wrong-type-arg
  2036. (take-right '(4 5 6) 4)))
  2037. ;;
  2038. ;; tenth
  2039. ;;
  2040. (with-test-prefix "tenth"
  2041. (pass-if-exception "() -1" exception:wrong-type-arg
  2042. (tenth '(a b c d e f g h i)))
  2043. (pass-if (eq? 'j (tenth '(a b c d e f g h i j))))
  2044. (pass-if (eq? 'j (tenth '(a b c d e f g h i j k)))))
  2045. ;;
  2046. ;; xcons
  2047. ;;
  2048. (with-test-prefix "xcons"
  2049. (pass-if (equal? '(y . x) (xcons 'x 'y))))