iterate-test.lisp 58.2 KB
Newer Older
Attila Lendvai's avatar
Attila Lendvai committed
1
2
3
;;; Test cases for Iterate.

;;; Copyright (c) 2003 Andreas Fuchs <asf@boinkor.net>
4
;;; Copyright (c) 2004-2007 Joerg Hoehle <hoehle@users.sourceforge.net>
Attila Lendvai's avatar
Attila Lendvai committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

;;; License:
;; Permission is hereby granted, free of charge, to any person
;; obtaining a copy of this software and associated documentation
;; files (the "Software"), to deal in the Software without
;; restriction, including without limitation the rights to use, copy,
;; modify, merge, publish, distribute, sublicense, and/or sell copies
;; of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:

;; The above copyright notice and this permission notice shall be
;; included in all copies or substantial portions of the Software.

;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
;; BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
;; ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
;; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;; SOFTWARE.


;;; Commentary:

;; Although growing, this testsuite does not yet cover every documented
;; feature of Iterate.

(cl:defpackage #:iterate.test
34
35
36
  (:import-from #+sbcl #:sb-rt
                #-sbcl #:regression-test
                #:*expected-failures*)
Attila Lendvai's avatar
Attila Lendvai committed
37
  (:use #:cl #:iterate
38
39
        #+sbcl #:sb-rt
        #-sbcl #:regression-test))
Attila Lendvai's avatar
Attila Lendvai committed
40
41
42
43
44

(cl:in-package #:iterate.test)

(rem-all-tests)

45
46
47
48
49
50
51
52
53
;; This is here to document which tests are failing at the time of
;; writing. It's not used directly here, but the tests of the
;; iterate-compat package of hu.dwim.reiterate expect it.
(defvar *tests-expected-to-fail*
  '(always.finally
    never.finally
    thereis.finally
    bug/walk.2
    bug/collect-at-beginning
54
55
56
57
58
59
60
    #+(or clozure ecl)
    bug/previously-initially.1
    #+ (or abcl clozure ecl sbcl)
    IN-STREAM.2
    #+ecl code-movement.else
    #+ecl code-movement.finally
    #+ecl code-movement.finally-protected
61
62
    ))

Attila Lendvai's avatar
Attila Lendvai committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
(deftest dsetq.1
    (let (x y)
      (dsetq (x y) (list* 4 5 6))
      (list x y))
  (4 5))

(deftest dsetq.2
    (let (x y)
      (dsetq (x nil y) (list* 4 5 6 7))
      (list x y))
  (4 6))

(deftest dsetq.3
    (let ((a '((1 2) 3)) b)
      (dsetq (a b) a)
      (values a b))
  (1 2) 3)

(deftest dsetq.destructuring.1
    (let (x y)
      (dsetq (x . y) (list* 4 5 6))
      (list x y))
  (4 (5 . 6)))

(deftest dsetq.destructuring.2
    (let (x y z)
      (dsetq (x nil (y . z)) (list* 4 5 '(6 7 . 8) 9))
      (list x y z))
  (4 6 (7 . 8)))

(deftest dsetq.values.1
    (let (x y)
      (dsetq (values x y) (values 1 'a))
      (list x y))
  (1 a))

(deftest dsetq.values.2
    (let (x y)
      (dsetq (values x nil y) (values 1 'a "c"))
      (list x y))
  (1 "c"))

(deftest dsetq.values.3
    (let (x)
      (dsetq (values nil (nil . x)) (values 1 '(a b . c)))
      x)
  (b . c))

(deftest dsetq.values.4
    (let (x y z) (dsetq (values nil x (y z))
113
                        (values 1 'a '(b c))) (list x y z))
Attila Lendvai's avatar
Attila Lendvai committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
  (a b c))

(deftest repeat.1
    (iter (repeat 9) (count 1))
  9)

(deftest repeat.2
    (iter (repeat 2.5s0) (counting t))
  3)

(deftest repeat.3
    (iter (repeat -1.5f0) (counting t))
  0)

(deftest locally.1
    (iterate (for i in '(1 2 3)) (repeat 2)
130
             (locally (collect i) (collect 0)))
Attila Lendvai's avatar
Attila Lendvai committed
131
132
133
134
  (1 0 2 0))

(deftest locally.2
    (iterate (for i in '(1 2 3)) (repeat 2)
135
136
137
             (locally
               (declare (optimize safety))
               (declare (fixnum i)) (collect i)))
Attila Lendvai's avatar
Attila Lendvai committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  (1 2))

(deftest always.1
    (iter (repeat 3) (always 2))
  2)

(deftest always.2
    (iter (repeat 0) (always 2))
  t)

(deftest always.3
    (iter (for i in '()) (always i))
  t)

152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
(deftest always.finally
    (with-output-to-string (*standard-output*)
      (iter (repeat 1)
            ;; When ALWAYS fails, it RETURN-FROM's from the block and
            ;; thus skips FINALLY.
            (always nil)
            (finally (princ 42))))
  "42")

(deftest never.finally
    (with-output-to-string (*standard-output*)
      (iter (repeat 1)
            ;; See comment at ALWAYS.4
            (never t)
            (finally (princ 42))))
  "42")

(deftest thereis.finally
    (with-output-to-string (*standard-output*)
      (iter (repeat 1)
            ;; See comment at ALWAYS.4
            (thereis 43)
            (finally (princ 42))))
  "42")

Attila Lendvai's avatar
Attila Lendvai committed
177
178
(deftest always.never.1
    (iterate  (repeat 2)
179
180
              (always 2)
              (never nil))
Attila Lendvai's avatar
Attila Lendvai committed
181
182
183
184
  2)

(deftest always.never.2
    (iter (for x in '(b (2 . a)))
185
186
          (if (consp x) (always (car x)) (always x))
          (never nil))
Attila Lendvai's avatar
Attila Lendvai committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
  2)

(deftest thereis.finally.1
    (iter (repeat 3) (thereis nil) (finally (prin1 "hi")))
  nil)

(deftest thereis.finally.2
    (with-output-to-string (*standard-output*) 
      (iter (repeat 3) (thereis nil) (finally (princ "hi"))))
  "hi")

(deftest thereis.finally.3
    (iter (repeat 3) (thereis nil) (finally (return 2)))
  2)

(deftest thereis.finally-protected.1
    (iter (repeat 3) (thereis 4) (finally-protected (prin1 "hi")))
  4)

(deftest thereis.finally-protected.2
    (with-output-to-string (*standard-output*) 
      (iter (repeat 3) (thereis 4) (finally-protected (princ "hi"))))
  "hi")

(deftest finding.such-that.2
    (iter (for i in '(7 -4 2 -3 4))
213
214
215
          (if (plusp i)
              (finding    i  such-that (evenp i))
              (finding (- i) such-that (oddp i))))
Attila Lendvai's avatar
Attila Lendvai committed
216
217
218
219
  2)

(deftest finding.such-that.nest.1
    (iter (for i in '(1 2 3))
220
221
222
223
          (finding (1+ i)
                   such-that #'(lambda (x)
                                 (declare (ignore x))
                                 (collect (- i) into m))))
Attila Lendvai's avatar
Attila Lendvai committed
224
225
226
227
  2) ; not -1 as some old version did

(deftest finding.such-that.nest.2
    (iter (for i in '(1 2 3))
228
229
230
231
232
233
234
235
          (finding (1+ i)
                   such-that #'(lambda (x)
                                 (finding (- x)
                                          such-that #'(lambda (x) x nil)
                                          into n)
                                 t)
                   into m)
          (finally (return (values m n))))
Attila Lendvai's avatar
Attila Lendvai committed
236
237
238
239
  2 nil) ; not -2 nil as some old version did

(deftest finding.thereis.1
    (iterate (for x in '(a 7 (-4 -3)))
240
241
             (thereis (consp x))
             (finding x such-that (numberp x)))
Attila Lendvai's avatar
Attila Lendvai committed
242
243
244
245
  7)

(deftest finding.thereis.2
    (iterate (for x in '(a (-4 -3) 7))
246
247
             (thereis (consp x))
             (finding x such-that (numberp x)))
Attila Lendvai's avatar
Attila Lendvai committed
248
249
250
251
  t)

(deftest finding.thereis.3
    (iterate (for x in '(a #\b))
252
253
             (thereis (consp x))
             (finding x such-that (numberp x)))
Attila Lendvai's avatar
Attila Lendvai committed
254
255
256
257
  nil)

(deftest finding.always.1
    (iterate (for x in '(-4 -2 -3))
258
259
             (always (numberp x))
             (finding x such-that (plusp x) on-failure t))
Attila Lendvai's avatar
Attila Lendvai committed
260
261
262
263
  t)

(deftest finding.always.2
    (iterate (for x in '(-4 7 -2 -3))
264
265
             (always (numberp x))
             (finding x such-that (plusp x) on-failure t))
Attila Lendvai's avatar
Attila Lendvai committed
266
267
268
269
  7)

(deftest finding.always.3
    (iterate (for x in '(-4 c -3))
270
271
             (always (numberp x))
             (finding x such-that (plusp x) on-failure t))
Attila Lendvai's avatar
Attila Lendvai committed
272
273
274
275
  nil)

(defun setup-hash-table (hash)
  (dotimes (i (random 100)) (declare (ignorable i))
276
277
        (setf (gethash (random 10000) hash) (random 10000))
        (setf (gethash (gensym) hash) (gensym))))
Attila Lendvai's avatar
Attila Lendvai committed
278
279
280

(deftest in-hashtable.keys
    (let* ((hash (make-hash-table))
281
282
283
284
           (all-entries (progn (setup-hash-table hash) '()))
           (generated-entries
            (iterate (for (key) in-hashtable hash)
                     (collect key))))
Attila Lendvai's avatar
Attila Lendvai committed
285
286
      (maphash (lambda (key value) value (push key all-entries)) hash)
      (= (length all-entries)
287
288
289
         (length generated-entries)
         (length (union all-entries generated-entries
                        :test (hash-table-test hash)))))
Attila Lendvai's avatar
Attila Lendvai committed
290
291
292
293
  t)

(deftest in-hashtable.items.1
    (let ((items nil)
294
          (hash (make-hash-table)))
Attila Lendvai's avatar
Attila Lendvai committed
295
296
297
      (setup-hash-table hash)
      (maphash (lambda (key item) key (push item items)) hash)
      (set-difference items
298
299
300
                      (iterate (for (key item) in-hashtable hash)
                               (declare (ignore key))
                               (collect item))))
Attila Lendvai's avatar
Attila Lendvai committed
301
302
303
304
  nil)

(deftest in-hashtable.items.2
    (let ((items nil)
305
          (hash (make-hash-table)))
Attila Lendvai's avatar
Attila Lendvai committed
306
307
308
      (setup-hash-table hash)
      (maphash (lambda (key item) key (push item items)) hash)
      (set-difference items
309
310
                      (iterate (for (nil item) in-hashtable hash)
                               (collect item))))
Attila Lendvai's avatar
Attila Lendvai committed
311
312
313
314
  nil)

(deftest in-hashtable.1
    (let* ((hash (make-hash-table))
315
316
317
318
           (all-entries (progn (setup-hash-table hash) '()))
           (generated-entries
            (iterate (as (key item) in-hashtable hash)
                     (collect (cons key item)))))
Attila Lendvai's avatar
Attila Lendvai committed
319
320
      (maphash #'(lambda (key value) (push (cons key value) all-entries)) hash)
      (= (length all-entries)
321
322
323
         (length generated-entries)
         (length (union all-entries generated-entries
                        :key #'car :test (hash-table-test hash)))))
Attila Lendvai's avatar
Attila Lendvai committed
324
325
326
327
  t)

(deftest in-hashtable.destructuring.1
    (let ((hash (make-hash-table :test #'equal))
328
          (entries '(((a . b) . (1 . 2)) (("c" . 3) . (6 . "7")))))
Attila Lendvai's avatar
Attila Lendvai committed
329
      (iterate (for (k . v) in entries)
330
               (setf (gethash k hash) v))
Attila Lendvai's avatar
Attila Lendvai committed
331
332
      (sort
       (iterate (for ((nil . k2) (v1 . v2)) in-hashtable hash)
333
334
335
336
                (always (numberp v1))
                (while k2)
                (collect (cons v1 k2) into vals)
                (finally (return vals)))
Attila Lendvai's avatar
Attila Lendvai committed
337
338
339
340
341
       #'< :key #'car))
  ((1 . b) (6 . 3)))

(deftest in-package.internals
    (let ((syms nil)
342
343
          (iter-syms (iterate (for sym in-package *package* :external-only nil)
                              (collect sym))))
Attila Lendvai's avatar
Attila Lendvai committed
344
      (do-symbols (sym *package* nil)
345
        (push sym syms))
Attila Lendvai's avatar
Attila Lendvai committed
346
347
348
349
350
351
352
      (list
       (set-difference syms iter-syms :test #'eq)
       (set-difference iter-syms syms)))
  (()()))

(deftest in-package.externals.1
    (let ((syms nil)
353
354
          (iter-syms (iterate (for sym in-package '#:cl-user external-only t)
                              (collect sym))))
Attila Lendvai's avatar
Attila Lendvai committed
355
      (do-external-symbols (sym '#:cl-user nil)
356
        (push sym syms))
Attila Lendvai's avatar
Attila Lendvai committed
357
358
359
360
361
362
363
364
      (list
       (set-difference syms iter-syms :test #'eq)
       (set-difference iter-syms syms)))
  (()()))

(deftest in-package.externals.2
    (let ((sym-count 0))
      (do-external-symbols (sym '#:iterate) (declare (ignore sym))
365
        (incf sym-count))
Attila Lendvai's avatar
Attila Lendvai committed
366
      (= sym-count
367
368
         (iterate (for () in-package '#:iterate external-only t)
                  (count 1))))
Attila Lendvai's avatar
Attila Lendvai committed
369
370
371
372
  t)

(deftest in-package.generator
    (let ((syms nil)
373
374
          (iter-syms (iterate (generate sym in-package *package*)
                              (collect (next sym)))))
Attila Lendvai's avatar
Attila Lendvai committed
375
      (do-symbols (sym *package*)
376
        (push sym syms))
Attila Lendvai's avatar
Attila Lendvai committed
377
378
379
380
381
382
383
      (list
       (set-difference syms iter-syms :test #'eq)
       (set-difference iter-syms syms)))
  (()()))

(deftest in-packages.external
    (let ((syms nil)
384
385
386
          (iter-syms (iterate (as (sym access package) in-packages '(#:cl-user)
                                   :having-access (:external))
                              (collect sym))))
Attila Lendvai's avatar
Attila Lendvai committed
387
      (do-external-symbols (sym '#:cl-user nil)
388
        (push sym syms))
Attila Lendvai's avatar
Attila Lendvai committed
389
390
391
392
393
394
395
      (list
       (set-difference syms iter-syms :test #'eq)
       (set-difference iter-syms syms)))
  (()()))

(deftest in-packages.generator-access
    (let ((iter-syms (iterate (generate (sym access) in-packages (list (find-package "COMMON-LISP")))
396
397
398
                              (repeat 1)
                              (next sym)
                              (collect (list sym access)))))
Attila Lendvai's avatar
Attila Lendvai committed
399
      (equal (multiple-value-list
400
401
              (find-symbol (symbol-name (caar iter-syms)) "COMMON-LISP"))
             (car iter-syms)))
Attila Lendvai's avatar
Attila Lendvai committed
402
403
404
405
  t)

(deftest in-stream.1
    (iter (as x in-stream (make-string-input-stream "#xa()2"))
406
          (collect x))
Attila Lendvai's avatar
Attila Lendvai committed
407
408
409
410
  (10 () 2))

(deftest in-stream.previous
    (iter (for x in-stream (make-string-input-stream "#xa()2"))
411
412
          (as p previous x :initially 1)
          (collect p))
Attila Lendvai's avatar
Attila Lendvai committed
413
414
415
416
417
418
419
420
421
422
423
424
425
  (1 10 ()))

(deftest in-stream.2
    ;; This fails in cmucl, sbcl and gcl, because string-input-streams
    ;; are always open, even after close.
    (let ((s (make-string-input-stream "(")))
      (ignore-errors
       (iter (for x in-stream s :using #'read)))
      (open-stream-p s))
  nil)

(deftest in-stream.3
    (iter (for c in-stream (make-string-input-stream "235")
426
427
               :using #'read-char)
          (accumulating (digit-char-p c) by #'+ initial-value 0))
Attila Lendvai's avatar
Attila Lendvai committed
428
429
430
431
  10)

(deftest in-stream.reducing
    (iter (with s = (make-string-input-stream "(+ 2)(+ 10)(- 5)(+ 6)"))
432
433
          (for (op num) in-stream s)
          (reducing num :by op :initial-value 0))
Attila Lendvai's avatar
Attila Lendvai committed
434
435
436
437
  13)

(deftest in-stream.accumulating
    (iter (with s = (make-string-input-stream "(+ 2)(+ 10)(- 5)(+ 6)"))
438
439
          (for (op num) in-stream s)
          (accumulating num :by op :initial-value 0))
Attila Lendvai's avatar
Attila Lendvai committed
440
441
442
443
444
445
446
447
448
449
450
  -1) ; (6 + (5 - (10 + (2 + 0))))

(deftest in-stream.generate
    (iter (with s = (make-string-input-stream "2 + 10 - 5 + 6"))
      (with start = (read s))
      (generate e in-stream s using #'read)
      (as op = (next e))
      (for arg = (next e))
      (reducing arg by op initial-value start))
  13)

451
452
453
454
455
456
457
458
459
(deftest in-stream.reiter
    (let ((s (make-string-input-stream "1 2 3 4 5")))
      (iter (for n below 2)
            (for line in-stream s))
      (iter (for n below 2)
            (for line in-stream s)
            (collect line)))
  (3 4))

Attila Lendvai's avatar
Attila Lendvai committed
460
461
(deftest reducing.0
    (iter (with expr = '(2 + 10 - 5 + 6))
462
463
464
          (with start = (pop expr))
          (for (op arg) on expr by #'cddr)
          (reducing arg by op initial-value start))
Attila Lendvai's avatar
Attila Lendvai committed
465
466
467
468
  13)

(deftest until.1
    (iter (with rest = 235) (with digit = 0)
469
470
471
472
473
          (multiple-value-setq (rest digit) (floor rest 10))
          (sum digit into sum)
          (multiplying digit into product)
          (until (zerop rest))
          (finally (return (values sum product))))
Attila Lendvai's avatar
Attila Lendvai committed
474
  10 30)
475
          
Attila Lendvai's avatar
Attila Lendvai committed
476
477
(deftest until.2
    (iter (for i in-sequence '#(1 2 -3 6))
478
479
          (until (zerop (sum i into x)))
          (multiplying i))
Attila Lendvai's avatar
Attila Lendvai committed
480
481
482
483
  2)

(deftest while.1
    (iter (for i in-sequence '#(1 2 -3 6))
484
          (while (< (length (collect i)) 2)))
Attila Lendvai's avatar
Attila Lendvai committed
485
486
  (1 2))

487
488
(deftest else.1
    (iter (repeat 0)
489
          (else (return 1)))
490
491
492
493
  1)

(deftest else.2
    (iter (for i below -3)
494
          (else (return 2)))
495
496
  2)

Attila Lendvai's avatar
Attila Lendvai committed
497
498
499
500
501
;;; tests for my examples:

(eval-when (:compile-toplevel :load-toplevel :execute)
  (defparameter *an-alist* '((a . 2) (b . 3) (zero . 10) (c . 4) (one . 20) (d . 5) (e . 99)))
  (defparameter *list-of-lists* (loop for i from 0 to 100
502
503
                                      collect (loop for len from 0 to (random 20)
                                                    collect len)))
Attila Lendvai's avatar
Attila Lendvai committed
504
505
  (defun longest-list (list1 list2)
    (if (< (length list2) (length list1))
506
507
        list1
        list2)))
Attila Lendvai's avatar
Attila Lendvai committed
508
509
510

(deftest collect.1
    (iterate (as (key . item) in *an-alist*)
511
512
513
             (collect key into keys)
             (collect item into items)
             (finally (return (values keys items))))
Attila Lendvai's avatar
Attila Lendvai committed
514
515
516
517
518
519
520
  #.(loop for (key . nil) in *an-alist*
        collect key)
  #.(loop for (key . item) in *an-alist*
        collect item))

(deftest generate.1
    (iterate (generate i from 0 to 6)
521
522
523
             (for (key . value) in *an-alist*)
             (when (>= value 10)
               (collect (cons key (next i)))))
Attila Lendvai's avatar
Attila Lendvai committed
524
525
526
527
528
529
530
  #.(loop with counter = 0
        for (key . value) in *an-alist*
        when (>= value 10)
           collect (cons key (prog1 counter (incf counter)))))

(deftest find-longest-list.1
    (iterate (for elt in *list-of-lists*)
531
             (finding elt maximizing (length elt)))
Attila Lendvai's avatar
Attila Lendvai committed
532
533
534
535
  #.(reduce #'longest-list *list-of-lists*))

(deftest find-longest-list.2
    (iterate (for elt in *list-of-lists*)
536
537
             (finding elt maximizing (length elt) into (e m))
             (finally (return m)))
Attila Lendvai's avatar
Attila Lendvai committed
538
539
540
541
  #.(reduce #'max *list-of-lists* :key #'length))

(deftest find-longest-list.3
    (iterate (for elt in *list-of-lists*)
542
             (finding elt maximizing #'length))
Attila Lendvai's avatar
Attila Lendvai committed
543
544
545
546
  #.(reduce #'longest-list *list-of-lists*))

(deftest find-longest-list.4
    (iterate (for elt in *list-of-lists*)
547
548
             (finding elt maximizing #'length into (e m))
             (finally (return m)))
Attila Lendvai's avatar
Attila Lendvai committed
549
550
551
552
  #.(reduce #'max *list-of-lists* :key #'length))

(deftest maximize.1
    (iterate (for elt in *list-of-lists*)
553
554
             (maximizing (length elt) into m)
             (finally (return m)))
Attila Lendvai's avatar
Attila Lendvai committed
555
556
557
558
  #.(reduce #'max *list-of-lists* :key #'length))

(deftest maximize.2
    (iterate (for elt in *list-of-lists*)
559
             (maximize (length elt)))
Attila Lendvai's avatar
Attila Lendvai committed
560
561
562
563
  #.(reduce #'max *list-of-lists* :key #'length))

(deftest finding.minimizing.1
    (iterate (for elt in *list-of-lists*)
564
565
             (finding elt minimizing #'length into (e m))
             (finally (return m)))
Attila Lendvai's avatar
Attila Lendvai committed
566
567
568
569
  #.(reduce #'min *list-of-lists* :key #'length))

(deftest minimize.1
    (iterate (for elt in *list-of-lists*)
570
571
             (minimizing (length elt) into m)
             (finally (return m)))
Attila Lendvai's avatar
Attila Lendvai committed
572
573
574
575
  #.(reduce #'min *list-of-lists* :key #'length))

(deftest minimize.2
    (iterate (for elt in *list-of-lists*)
576
             (minimize (length elt)))
Attila Lendvai's avatar
Attila Lendvai committed
577
578
579
580
  #.(reduce #'min *list-of-lists* :key #'length))

(deftest subblocks.maximize.1
    (iter outer (for elt in *list-of-lists*)
581
582
583
          (iterate running (for e in elt)
                   (in outer (maximize e)))
          (maximizing (length elt)))
Attila Lendvai's avatar
Attila Lendvai committed
584
585
586
587
  #.(reduce #'max *list-of-lists* :key #'length))

(deftest subblocks.minimize.1
    (iter outer (for elt in *list-of-lists*)
588
589
590
          (minimizing (length elt))
          (iterate running (for e in elt)
                   (in outer (minimize e))))
Attila Lendvai's avatar
Attila Lendvai committed
591
592
593
594
  0)

(deftest maximize.3
    (iterate (for i in-vector '#(-3))
595
             (maximize i))
Attila Lendvai's avatar
Attila Lendvai committed
596
597
598
599
  -3)

(deftest minimize.3
    (iterate (as i in-vector '#(3))
600
             (minimize i))
Attila Lendvai's avatar
Attila Lendvai committed
601
602
603
604
  3)

(deftest maximize.multiple
    (iter (as i from 3 downto -3 by 2) (maximize i)
605
          (for j from -1)            (maximizing j))
Attila Lendvai's avatar
Attila Lendvai committed
606
607
608
609
  3)

(deftest minimize.multiple
    (iter (as i from -3 to 3 by 2) (minimize i into x)
610
611
          (for j downfrom -1)    (minimizing j into x)
          (finally (return x)))
Attila Lendvai's avatar
Attila Lendvai committed
612
613
614
615
  -4)

(deftest accumulate.1
    (iter (for c in-string "235")
616
617
          (declare (type character c))
          (accumulate (digit-char-p c) by '* initial-value 1))
Attila Lendvai's avatar
Attila Lendvai committed
618
619
620
621
  30)

(deftest accumulate.2
    (iter (for c in-sequence "235")
622
          (accumulating (digit-char-p c) by #'* initial-value 1))
Attila Lendvai's avatar
Attila Lendvai committed
623
624
625
626
  30)

(deftest accumulate.3
    (iter (for c in-sequence "235")
627
          (accumulate (digit-char-p c) by 'cons initial-value 1))
Attila Lendvai's avatar
Attila Lendvai committed
628
629
630
631
  (5 3 2 . 1))

(deftest accumulate.4
    (iter (for c in-vector "235")
632
          (accumulating (digit-char-p c) by #'cons))
Attila Lendvai's avatar
Attila Lendvai committed
633
634
635
636
  (5 3 2))

(deftest accumulate.5
    (iter (repeat 0)
637
          (accumulating 1 by #'cons))
Attila Lendvai's avatar
Attila Lendvai committed
638
639
640
641
  nil)

(deftest accumulate.6
    (iter (repeat 0)
642
          (accumulate 1 by #'cons initial-value 2))
Attila Lendvai's avatar
Attila Lendvai committed
643
644
645
646
  2)

(deftest in-string.downto.1
    (iter (for c in-string "235" downto 1)
647
          (accumulate (digit-char-p c) by 'cons))
Attila Lendvai's avatar
Attila Lendvai committed
648
649
650
651
  (3 5))

(deftest in-sequence.downto.1
    (iter (for c in-sequence "235" downto 1)
652
          (accumulate (digit-char-p c) by #'cons))
Attila Lendvai's avatar
Attila Lendvai committed
653
654
655
656
  (3 5))

(deftest reducing.1
    (iter (for c in-string "235")
657
          (reducing (digit-char-p c) by 'list initial-value 1))
Attila Lendvai's avatar
Attila Lendvai committed
658
659
660
661
  (((1 2) 3) 5))

(deftest reducing.2
    (iter (as x index-of-string "235")
662
          (reducing x :by #'list initial-value -1))
Attila Lendvai's avatar
Attila Lendvai committed
663
664
665
666
  (((-1 0) 1) 2))

(deftest reducing.3
    (iter (repeat 0)
667
          (reducing 1 #:by 'cons initial-value -1))
Attila Lendvai's avatar
Attila Lendvai committed
668
669
670
671
  -1)

(deftest reducing.4
    (iter (for i from 3 to 5)
672
          (reducing i by #'- :initial-value '0))
Attila Lendvai's avatar
Attila Lendvai committed
673
674
675
676
  -12)

(deftest reducing.5
    (iter (for x in-vector #(3))
677
          (reducing (cons x x) by #'list))
Attila Lendvai's avatar
Attila Lendvai committed
678
679
680
681
  (3 . 3))

(deftest reducing.6
    (iter (for x in-vector (vector 3))
682
          (reducing (cons x x) by #'list :initial-value nil))
Attila Lendvai's avatar
Attila Lendvai committed
683
684
685
686
687
688
689
  (nil (3 . 3)))


;; synonyms (e.g. GENERATING, COLLECTING) didn't work

(deftest generate.destructuring.1
    (iter (generate (key . item) in '((a . 1) (b . 2) (c .3)))
690
691
          (collect (next key))
          (collect (next item)))
Attila Lendvai's avatar
Attila Lendvai committed
692
693
694
695
  (a 2 c))

(deftest generating.destructuring.1
    (iter (generating (key . item) in '((a . 1) (b . 2) (c .3)))
696
697
          (collect (next key))
          (collect (next item)))
Attila Lendvai's avatar
Attila Lendvai committed
698
699
700
701
  (a 2 c))

(deftest for.generate-t.destructuring.1
    (iter (for (key . item) in '((a . 1) (b . 2) (c .3)) :generate t)
702
703
          (collect (next key))
          (collect (next item)))
Attila Lendvai's avatar
Attila Lendvai committed
704
705
706
707
  (a 2 c))

(deftest generate.next.1
    (iter (generate c in '(a b c d e f g h i j k l m n o p q))
708
709
          (for s in '(1 1 2 3 1 0 1 0 2 1))
          (collect (next c s)))
Attila Lendvai's avatar
Attila Lendvai committed
710
711
712
713
  (a b d g h h i i k l))

(deftest generate.previous.1
    (iter (generate c in '(a b c d e f g h i j k l m n o p q))
714
715
716
717
          (for s in '(1 1 2 3 1 0 1 0 2 1))
          (for x = (next c s))
          (as y previous x)
          (collect (list y x)))
Attila Lendvai's avatar
Attila Lendvai committed
718
719
720
721
722
723
724
725
726
  ((nil a) (a b) (b d) (d g) (g h) (h h) (h i) (i i) (i k) (k l)))

(deftest generate.next.2
    (with-output-to-string (*standard-output*)
      (iter (generate i in '(1 2 3 4 5)) (princ (next i 2))))
  "24")

(deftest if.1
    (iter (generate x in-vector '#(t nil nil t))
727
728
          (as i from 0)
          (if (next x) (collect i)))
Attila Lendvai's avatar
Attila Lendvai committed
729
730
  (0 3))

731
732
(deftest if.2
    (iter (generate x in-vector '#(t nil nil t) with-index i)
733
          (if (next x) (collect i)))
734
735
  (0 3))

Attila Lendvai's avatar
Attila Lendvai committed
736
737
(deftest or.1
    (iter (generate x in '(a nil nil 1))
738
739
          (generate y in-vector '#(2 #\c #\d))
          (collect (or (next x) (next y))))
Attila Lendvai's avatar
Attila Lendvai committed
740
741
742
743
  (a 2 #\c 1))

(deftest or.2
    (iter (generate x in '(a nil nil 1 nil))
744
745
          (generate y in-sequence '#(2 nil #\c #\d))
          (collect (or (next x) (next y) 3)))
Attila Lendvai's avatar
Attila Lendvai committed
746
747
748
749
  (a 2 3 1 #\c))

(deftest setf.1
    (iter (generate i from 0 to 3)
750
751
752
          (with v = (vector 'a 'b 'c 'd))
          (setf (aref v (next i)) i)
          (finally (return v)))
Attila Lendvai's avatar
Attila Lendvai committed
753
754
755
756
757
758
  #(0 1 2 3))

(deftest setf.2
    ;; These setf tests fail in CormanLisp 2.0 because ccl does
    ;; not respect setf evaluation order rules.
    (iter (generate i from 0 to 3)
759
760
761
          (with v = (vector 'a 'b 'c 'd))
          (setf (aref v (next i)) (next i))
          (finally (return v)))
Attila Lendvai's avatar
Attila Lendvai committed
762
763
764
765
  #(1 b 3 d))

(deftest setf.3
    (iter (generate i in '(0 1 2 3 4 5))
766
767
768
          (with v = (vector 'a 'b 'c 'd))
          (setf (aref v (next i)) (next i 2))
          (finally (return v)))
Attila Lendvai's avatar
Attila Lendvai committed
769
770
771
772
  #(2 b c 5))

(deftest setf.4
    (iter (generate i from 0 to 3)
773
774
775
          (with v = (vector 'a 'b 'c 'd))
          (setf (apply #'aref v (list (next i))) (next i))
          (finally (return v)))
Attila Lendvai's avatar
Attila Lendvai committed
776
777
778
779
  #(1 b 3 d))

(deftest after-each.1
    (iter (after-each (collecting 0))
780
781
          (generate i in '(a b c))
          (adjoining (next i)))
Attila Lendvai's avatar
Attila Lendvai committed
782
783
784
785
  (a 0 b 0 c 0))

(deftest after-each.2
    (iter (with i = 0)
786
787
788
          (while (< i 4))
          (after-each (incf i)) ; the C programmer's for (;;) loop
          (collect i))
Attila Lendvai's avatar
Attila Lendvai committed
789
790
791
792
  (0 1 2 3))

(deftest after-each.3
    (iter (with i = 0)
793
794
795
          (while (< i 4))
          (collect i)
          (after-each (incf i)))
Attila Lendvai's avatar
Attila Lendvai committed
796
797
798
799
  (0 1 2 3))

(deftest next-iteration.1
    (iter (for i below 10)
800
801
          (when (oddp i) (next-iteration))
          (count t))
Attila Lendvai's avatar
Attila Lendvai committed
802
803
804
805
  5)

(deftest next-iteration.2
    (iter (for thing in '(var &optional else &key (test #'eql)))
806
807
808
809
          (collect
           (cond ((consp thing) (first thing))
                 ((not (member thing lambda-list-keywords)) thing)
                 (t (next-iteration)))))
Attila Lendvai's avatar
Attila Lendvai committed
810
811
812
813
814
815
  (var else test))

;;;; tests from the documentation:

(deftest collect.2
    (iter (for i from 1 to 10)
816
          (collect i))
Attila Lendvai's avatar
Attila Lendvai committed
817
818
819
820
  (1 2 3 4 5 6 7 8 9 10))

(deftest for-in.2
    (iter (for el in '(1 2 3 4 5 6 f 7 8 9 a 10))
821
822
          (if (and (numberp el) (oddp el))
              (collect el)))
Attila Lendvai's avatar
Attila Lendvai committed
823
824
825
826
  (1 3 5 7 9))

(deftest for.destructuring.1
    (iter (for (key . item) in '((a . 10) (b . 20) (c . 30)))
827
828
829
          (for i from 0)
          (declare (fixnum i))
          (collect (cons i key)))
Attila Lendvai's avatar
Attila Lendvai committed
830
831
832
833
834
  ((0 . a) (1 . b) (2 . c)))

(deftest repeat.0
    (with-output-to-string (*standard-output*)
      (iter (repeat 100)
835
            (print "I will not talk in class.")))
Attila Lendvai's avatar
Attila Lendvai committed
836
837
  #.(with-output-to-string (*standard-output*)
      (dotimes (i 100)
838
839
        (declare (ignorable i)) ; cmucl/sbcl complain about (ignore i)
        (print "I will not talk in class."))))
Attila Lendvai's avatar
Attila Lendvai committed
840
841
842
843
844

;;; for.next.1 and for.do-next.1 used to be broken in older versions;
;;; they didn't WALK their NEXT args.
(deftest for.next.1
    (iterate (initially (setq i 0))
845
846
             (for i next (if (> i 10) (terminate) (1+ i)))
             (finally (return i)))
Attila Lendvai's avatar
Attila Lendvai committed
847
848
849
850
851
  11)

;;; This gave STYLE-WARNINGS for undefined i in old versions.
(deftest for.do-next.1
    (iterate (initially (setq i 0))
852
853
             (as i do-next (if (> i 10) (terminate) (incf i)))
             (finally (return i)))
Attila Lendvai's avatar
Attila Lendvai committed
854
855
856
857
858
  11)

(deftest for.do-next.2
    ;; INITIALLY not needed because 0 is inferred from type declaration
    (iterate (for i do-next (if (> i 7) (terminate) (incf i)))
859
860
             (declare (type fixnum i))
             (finally (return i)))
Attila Lendvai's avatar
Attila Lendvai committed
861
862
863
864
  8)

(deftest for.do-next.3
    (iter (for a from 1 to 3)
865
866
867
868
          (for b = (1+ (* a a)))
          ;; (values ...) is supported, even though (x y) would do
          (for (values x y) do-next (dsetq (values x y) (floor b a)))
          (collect x) (collect y))
Attila Lendvai's avatar
Attila Lendvai committed
869
870
871
872
  (2 0 2 1 3 1))

(deftest for.next.walk
    (iter (repeat 2)
873
874
          (for x next (progn (after-each (collect 1)) 2))
          (collect x))
Attila Lendvai's avatar
Attila Lendvai committed
875
876
877
878
  (2 1 2 1))

(deftest for.do-next.walk
    (iter (repeat 2)
879
880
          (for x do-next (progn (after-each (collect 1)) (dsetq x 2)))
          (collect x))
Attila Lendvai's avatar
Attila Lendvai committed
881
882
883
884
  (2 1 2 1))

(deftest for.next.previous
    (iter (for i from 2 to 4)
885
886
887
          (for x next (progn (after-each (collect i)) (- i)))
          (for z previous x initially 0)
          (nconcing (list z x)))
Attila Lendvai's avatar
Attila Lendvai committed
888
889
890
891
  (0 -2 2 -2 -3 3 -3 -4 4))

(deftest for.do-next.previous
    (iter (for i from 2 to 4)
892
893
894
          (for x do-next (progn (setq x (- i)) (after-each (collect i))))
          (for z previous x initially 0)
          (appending (list z x)))
Attila Lendvai's avatar
Attila Lendvai committed
895
896
897
898
  (0 -2 2 -2 -3 3 -3 -4 4))

(deftest for-nongenerator.1
    (iter (for el in '(a b c d))
899
900
          (generate i upfrom 1)
          (if el (collect (cons el (next i)))))
Attila Lendvai's avatar
Attila Lendvai committed
901
  #.(iter (for el in '(a b c d))
902
903
          (for i upfrom 1)
          (if el (collect (cons el i)))))
Attila Lendvai's avatar
Attila Lendvai committed
904
905
906
907
  

(deftest for.previous.in
    (iter (for el in '(1 2 3 4))
908
909
          (for pp-el previous el back 2 initially 0)
          (collect pp-el))
Attila Lendvai's avatar
Attila Lendvai committed
910
911
912
913
  (0 0 1 2))

(deftest for.previous.type.1
    (iter (for el in '(1 2 3 4))
914
915
916
          (declare (type integer el))
          (for pp-el previous el back 2)
          (collect pp-el))
Attila Lendvai's avatar
Attila Lendvai committed
917
918
919
920
  (0 0 1 2))

(deftest for.previous.index-of-string.1
    (iter (as x index-of-string "235")
921
922
          (as p previous x :initially 9)
          (collecting p))
Attila Lendvai's avatar
Attila Lendvai committed
923
924
925
926
  (9 0 1))

(deftest for.previous.in-string.with-index
    (iter (as x in-string "235" :with-index y)
927
928
          (as p previous y :initially 9)
          (collecting p))
Attila Lendvai's avatar
Attila Lendvai committed
929
930
931
932
  (9 0 1))

(deftest for.previous.index-of-vector
    (iter (as x index-of-vector '#(2 3 4 5))
933
934
          (as p previous x :initially 9 back 2)
          (collecting p))
Attila Lendvai's avatar
Attila Lendvai committed
935
936
937
938
  (9 9 0 1))

(deftest for.previous.in-vector.with-index
    (iter (as x in-vector '#(2 3 4 5) with-index y)
939
940
          (as p previous y :initially 9 back 2)
          (collecting p))
Attila Lendvai's avatar
Attila Lendvai committed
941
942
  (9 9 0 1))

943
944
945
(deftest for.previous.var-with-type-declaration
    (iter (for i from 1 to 5)
          (for (the fixnum i-prev) previous i)
946
          (collect i-prev))
947
948
  (nil 1 2 3 4))

Attila Lendvai's avatar
Attila Lendvai committed
949
950
(deftest for.first.1
    (iter (for num in '(20 19 18 17 16))
951
952
          (for i first num then (1+ i))
          (collect i))
Attila Lendvai's avatar
Attila Lendvai committed
953
954
955
956
  (20 21 22 23 24))

(deftest for.initially.1
    (iter (with (v z))
957
958
959
          (for i initially (length v) then (1+ i))
          (collect (cons i z))
          (while (evenp i)))
Attila Lendvai's avatar
Attila Lendvai committed
960
961
962
963
  ((0) (1)))

(deftest sum.1
    (iter (for el in '(100 200 300))
964
965
966
          (sum el into x)
          (declare (fixnum x))
          (finally (return x)))
Attila Lendvai's avatar
Attila Lendvai committed
967
968
969
970
  600)

(deftest collect.3
    (iter (for i from 1 to 5)
971
          (collect i))
Attila Lendvai's avatar
Attila Lendvai committed
972
973
974
975
  (1 2 3 4 5))

(deftest collect.4
    (iter (for i from 1 to 5)
976
          (collect i at beginning))
Attila Lendvai's avatar
Attila Lendvai committed
977
978
979
980
  (5 4 3 2 1))

(deftest collect.5
    (iter (for i from 1 to 4)
981
          (collect i at :end))
Attila Lendvai's avatar
Attila Lendvai committed
982
983
984
985
  (1 2 3 4))

(deftest collect.6
    (iter (for i from 1 to 3)
986
          (collect i :at start))
Attila Lendvai's avatar
Attila Lendvai committed
987
988
989
990
  (3 2 1))

(deftest collect-by.1
    (iter (for i downfrom 10 by 2) (repeat 3)
991
          (collect i))
Attila Lendvai's avatar
Attila Lendvai committed
992
993
994
995
  (10 8 6))

(deftest in-vector.by.1
    (iter (for i in-vector '#(0 1 2 3 4) by 2)
996
          (collect i))
Attila Lendvai's avatar
Attila Lendvai committed
997
998
999
1000
  (0 2 4))

(deftest index-of-vector.by.1
    (iter (for i index-of-vector '#(0 1 2 3 4) by 2)
1001
          (collect i))
Attila Lendvai's avatar
Attila Lendvai committed
1002
1003
1004
1005
  (0 2 4))

(deftest in-vector.downto.1
    (iter (for i in-vector '#(0 1 2 3 4) downto 0)
1006
          (collect i))
Attila Lendvai's avatar
Attila Lendvai committed
1007
1008
1009
1010
  (4 3 2 1 0))

(deftest index-of-vector.downto.1
    (iter (for i index-of-vector #(0 1 2 3 4) downto 0)
1011
          (collect i))
Attila Lendvai's avatar
Attila Lendvai committed
1012
1013
1014
1015
  (4 3 2 1 0))

(deftest in-vector.downto.2
    (iter (for i in-vector '#(0 1 2 3 4) downto 0 by 2)
1016
          (collect i))
Attila Lendvai's avatar
Attila Lendvai committed
1017
1018
1019
1020
  (4 2 0)) ; erroneously got (3 1) in some past

(deftest index-of-vector.downto.2
    (iter (for i index-of-vector #(0 1 2 3 4) downto 0 by 2)
1021
          (collect i))
Attila Lendvai's avatar
Attila Lendvai committed
1022
1023
1024
1025
  (4 2 0))

(deftest generate.in-vector.downto.1
    (iter (generate i in-vector #(0 1 2 3 4) downto 0 by 2)
1026
          (collect (next i)))
Attila Lendvai's avatar
Attila Lendvai committed
1027
1028
1029
1030
  (4 2 0))

(deftest generate.index-of-vector.downto.1
    (iter (generate i index-of-vector '#(0 1 2 3 4) downto 0 by 2)
1031
          (collect (next i)))
Attila Lendvai's avatar
Attila Lendvai committed
1032
1033
1034
1035
1036
  (4 2 0))

(deftest if-first-time.1
    (with-output-to-string (*standard-output*)
      (iter (for i from 200 to 203)
1037
            (if-first-time (format t "honka"))))
Attila Lendvai's avatar
Attila Lendvai committed
1038
1039
1040
1041
1042
  "honka")

(deftest if-first-time.2
    (with-output-to-string (*standard-output*)
      (iter (for i from 200 to 204)
1043
            (if (oddp i) (if-first-time (princ "honka") (princ "tah")))))
Attila Lendvai's avatar
Attila Lendvai committed
1044
1045
1046
1047
  "honkatah")

(deftest if-first-time.3
    (iter (for i to 5)
1048
1049
1050
          (when (oddp i)
            (if-first-time nil (collect -1))
            (collect i)))
Attila Lendvai's avatar
Attila Lendvai committed
1051
1052
  (1 -1 3 -1 5))

1053
1054
1055
(deftest first-time-p.0
    (with-output-to-string (*standard-output*)
      (iter (for el in '(nil 1 2 nil 3))
1056
1057
1058
1059
            (when el
              (unless (first-time-p)
                (princ ", "))
              (princ el))))
1060
1061
  "1, 2, 3")

1062
1063
1064
1065
(deftest first-time-p.1
    (iter (for i to 5)
          (if (first-time-p) (collect -1))
          (if (first-time-p) (collect -2))
1066
1067
1068
          (when (oddp i)
            (if (first-time-p) nil (collect -1))
            (collect i)))
1069
1070
1071
1072
1073
1074
  (-1 -2 1 -1 3 -1 5))

(deftest first-iteration-p.1
    (iter (for i to 5)
          (if (first-iteration-p) (collect -1))
          (if (first-iteration-p) (collect -2))
1075
1076
1077
          (when (oddp i)
            (if (first-iteration-p) nil (collect -1))
            (collect i)))
1078
1079
  (-1 -2 -1 1 -1 3 -1 5))

Attila Lendvai's avatar
Attila Lendvai committed
1080
1081
(deftest collect.multiple.1
    (iter (for i from 1 to 10)
1082
1083
1084
          (collect i into nums)
          (collect (sqrt i) into nums)
          (finally (return nums)))
Attila Lendvai's avatar
Attila Lendvai committed
1085
  #.(loop for i from 1 to 10
1086
1087
          collect i
          collect (sqrt i)))
Attila Lendvai's avatar
Attila Lendvai committed
1088
1089
1090
1091

(deftest collect.type.string.1
    (locally (declare (optimize safety (debug 2) (speed 0) (space 1)))
      (iter (declare (iterate:declare-variables))
1092
1093
            (for s in-vector '#(\a |b| |cD|))
            (collect (char (symbol-name s) 0) :result-type string)))
Attila Lendvai's avatar
Attila Lendvai committed
1094
1095
1096
1097
  "abc")

(deftest collect.type.string.2
    (iter (for c in-stream (make-string-input-stream "aBc") :using #'read-char)
1098
1099
          (when (digit-char-p c 16)
            (collect c :result-type string)))
Attila Lendvai's avatar
Attila Lendvai committed
1100
1101
1102
1103
  "aBc")

(deftest collect.type.string.3
    (iter (for c in-string "235" downfrom 1)
1104
1105
          (collect c into s result-type string)
          (finally (return s)))
Attila Lendvai's avatar
Attila Lendvai committed
1106
1107
1108
1109
1110
  "32")

(deftest collect.type.vector.1
    (locally (declare (optimize safety (debug 2) (speed 0) (space 1)))
      (iter (declare (iterate:declare-variables))
1111
1112
            (for s in-vector '#(\a |b| |cD|))
            (collect (char (symbol-name s) 0) :result-type vector)))
Attila Lendvai's avatar
Attila Lendvai committed
1113
1114
1115
1116
  #(#\a #\b #\c))

(deftest collect.type.vector.2
    (iter (for c in-vector "235" downfrom 1)
1117
1118
          (collect (digit-char-p c) :into v :result-type vector)
          (finally (return v)))
Attila Lendvai's avatar
Attila Lendvai committed
1119
1120
1121
1122
  #(3 2))

(deftest adjoin.1
    (iter (for i in '("ab" "aB" "abc" "AB"))
1123
          (adjoining i at :start :test #'string-equal))
Attila Lendvai's avatar
Attila Lendvai committed
1124
1125
1126
1127
  ("abc" "ab"))

(deftest adjoin.2
    (iter (for i in '("ab" "aB" "abc" "AB"))
1128
          (adjoining i at :start))
Attila Lendvai's avatar
Attila Lendvai committed
1129
1130
1131
1132
  ("AB" "abc" "aB" "ab"))

(deftest adjoin.3
    (iter (for i in '("ab" "aB" "abc" "AB"))
1133
          (adjoining i :at end #:test #'string-equal))
Attila Lendvai's avatar
Attila Lendvai committed
1134
1135
1136
1137
  ("ab" "abc"))

(deftest adjoin.4
    (iter (for i in '("ab" "aB" "abc" "AB"))
1138
          (adjoining i at :end))
Attila Lendvai's avatar
Attila Lendvai committed
1139
1140
1141
1142
  ("ab" "aB" "abc" "AB"))

(deftest adjoin.5
    (iter (for i in '("ab" "aB" "abc" "AB"))
1143
          (adjoining (string-downcase i) at :start :test #'string-equal))
Attila Lendvai's avatar
Attila Lendvai committed
1144
1145
1146
1147
  ("abc" "ab"))

(deftest adjoin.6
    (iter (for i in '("ab" "aB" "abc" "AB"))
1148
          (adjoining (string-upcase i) #:at :end test #'string=))
Attila Lendvai's avatar
Attila Lendvai committed
1149
1150
1151
1152
  ("AB" "ABC"))

(deftest append.1
    (iter (for l on '(1 2 3))
1153
          (appending l at :start))
Attila Lendvai's avatar
Attila Lendvai committed
1154
1155
1156
1157
  (3 2 3 1 2 3))

(deftest nconc.1
    (iter (for l on (list 1 2 3))
1158
          (nconcing (copy-list l) at :beginning))
Attila Lendvai's avatar
Attila Lendvai committed
1159
1160
1161
1162
  (3 2 3 1 2 3))

(deftest append.2
    (iter (for l on '(1 2 3))
1163
          (appending l :at #:end))
Attila Lendvai's avatar
Attila Lendvai committed
1164
1165
1166
1167
  (1 2 3 2 3 3))

(deftest nconc.2
    (iter (for l on (list 1 2 3))
1168
          (nconcing (copy-list l) at end))
Attila Lendvai's avatar
Attila Lendvai committed
1169
1170
1171
1172
  (1 2 3 2 3 3))

(deftest append.3
    (iter (for l on '(1 2 3))
1173
          (appending l into x) (finally (return x)))
Attila Lendvai's avatar
Attila Lendvai committed
1174
1175
1176
1177
  (1 2 3 2 3 3))

(deftest nconc.3
    (iter (for l on (list 1 2 3))
1178
          (nconcing (copy-list l) into x) (finally (return x)))
Attila Lendvai's avatar
Attila Lendvai committed
1179
1180
1181
1182
  (1 2 3 2 3 3))

(deftest append.4
    (iter (for l on '(1 2 3))
1183
          (appending l into x))
Attila Lendvai's avatar
Attila Lendvai committed
1184
1185
1186
1187
  nil)

(deftest nconc.4
    (iter (for l on (list 1 2 3))
1188
          (nconcing (copy-list l) into x))
Attila Lendvai's avatar
Attila Lendvai committed
1189
1190
1191
1192
  nil)

(deftest append.5
    (iter (for l on '(1 2 3))
1193
1194
          (appending l :at #:end)
          (collect (first l)))
Attila Lendvai's avatar
Attila Lendvai committed
1195
1196
1197
1198
  (1 2 3 1 2 3 2 3 3))

(deftest append.6
    (iter (for l on '(1 2 3))
1199
1200
          (appending l :at :end)
          (collect l))
Attila Lendvai's avatar
Attila Lendvai committed
1201
1202
1203
1204
  (1 2 3 (1 2 3) 2 3 (2 3) 3 (3)))

(deftest nconc.5
    (iter (for l on (list 1 2 3))
1205
1206
          (collect (first l))
          (nconcing (copy-list l) at end))
Attila Lendvai's avatar
Attila Lendvai committed
1207
1208
1209
1210
  (1 1 2 3 2 2 3 3 3))

(deftest union.1
    (iter (for l on '(a b c))
1211
1212
          (unioning l)
          (collect (first l)))
Attila Lendvai's avatar
Attila Lendvai committed
1213
1214
1215
1216
  (a b c a b c))

(deftest union.2
    (iter (for l on '(a b c))
1217
1218
          (collecting (first l))
          (unioning l :test #'eql))
Attila Lendvai's avatar
Attila Lendvai committed
1219
1220
1221
1222
  (a b c b c))

(deftest union.3
    (iter (for l in-vector '#("a" "A" "aB" "ab" "AB"))
1223
          (unioning (list l) :test #'string-equal))
Attila Lendvai's avatar
Attila Lendvai committed
1224
1225
1226
1227
  ("a" "aB"))

(deftest nunion.3
    (iter (for l in-vector '#("a" "A" "aB" "ab" "AB"))
1228
          (nunioning (list l) :test #'string-equal :at :start))
Attila Lendvai's avatar
Attila Lendvai committed
1229
1230
1231
1232
  ("aB" "a"))

(deftest value.minimize
    (iter (for i from 4 downto -3 by 3)
1233
          (collect (minimize (* i i) into foo)))
Attila Lendvai's avatar
Attila Lendvai committed
1234
1235
1236
1237
  (16 1 1))

(deftest value.maximize
    (iter (for i from 3 to 5)
1238
          (sum (maximize (- i 2) into foo)))
Attila Lendvai's avatar
Attila Lendvai committed
1239
1240
1241
1242
  6)

(deftest value.finding-maximizing.1
    (iter (for i from 3 to 6)
1243
1244
          (adjoining (finding (* i i) maximizing #'integer-length
                              :into foo) :test #'=))
Attila Lendvai's avatar
Attila Lendvai committed
1245
1246
1247
1248
  (9 16 36))

(deftest value.finding-maximizing.2
    (iter (for i from 3 to 6)
1249
1250
          (adjoining (finding (* i i) maximizing (integer-length i)
                              :into foo) :test #'=))
Attila Lendvai's avatar
Attila Lendvai committed
1251
1252
1253
1254
  (9 16))

(deftest walk.counting
    (iter (for i from 3 to 5)
1255
          (counting (if-first-time nil t)))
Attila Lendvai's avatar
Attila Lendvai committed
1256
1257
1258
1259
  2)

(deftest value.counting
    (iter (for x in-sequence '#(nil t nil t))
1260
          (collect (counting x into foo)))
Attila Lendvai's avatar
Attila Lendvai committed
1261
1262
1263
1264
  (0 1 1 2))

(deftest value.adjoining
    (iter (for i from 3 to 5)