loop2.lsp 3.29 KB
Newer Older
1 2 3 4 5 6
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sat Oct 26 13:45:45 2002
;;;; Contains: Tests of the FOR-AS-IN-LIST loop iteration control form,
;;;;      and of destructuring in loop forms

7

8 9 10 11 12 13 14

(deftest loop.2.1
  (loop for x in '(1 2 3) sum x)
  6)

(deftest loop.2.2
  (loop for x in '(1 2 3 4)
sds's avatar
sds committed
15
        do (when (evenp x) (return x)))
16 17 18
  2)

(deftest loop.2.3
19
  (signals-error (loop for x in '(a . b) collect x)
sds's avatar
sds committed
20
                 type-error)
21
  t)
22 23 24 25 26 27 28 29 30

(deftest loop.2.4
  (let ((x nil))
    (loop for e in '(a b c d) do (push e x))
    x)
  (d c b a))

(deftest loop.2.5
  (loop for e in '(a b c d e f) by #'cddr
sds's avatar
sds committed
31
        collect e)
32 33 34 35
  (a c e))

(deftest loop.2.6
  (loop for e in '(a b c d e f g) by #'cddr
sds's avatar
sds committed
36
        collect e)
37 38 39 40
  (a c e g))

(deftest loop.2.7
  (loop for e in '(a b c d e f)
sds's avatar
sds committed
41 42
        by #'(lambda (l) (and (cdr l) (cons (car l) (cddr l))))
        collect e)
43 44 45 46
  (a a a a a a))

(deftest loop.2.8
  (loop for (x . y) in '((a . b) (c . d) (e . f))
sds's avatar
sds committed
47
        collect (list x y))
48 49 50 51
  ((a b) (c d) (e f)))

(deftest loop.2.9
  (loop for (x nil y) in '((a b c) (d e f) (g h i))
sds's avatar
sds committed
52
        collect (list x y))
53 54 55 56
  ((a c) (d f) (g i)))

(deftest loop.2.10
  (loop for (x y) of-type fixnum in '((1 2) (3 4) (5 6))
sds's avatar
sds committed
57
        collect (+ x y))
58 59
  (3 7 11))

60
(deftest loop.2.11
61
  (loop for (x y) of-type fixnum in '((1 2) (3 4) (5 6))
sds's avatar
sds committed
62
        collect (+ x y))
63 64
  (3 7 11))

65
(deftest loop.2.12
66
  (loop for (x y) of-type (fixnum fixnum) in '((1 2) (3 4) (5 6))
sds's avatar
sds committed
67
        collect (+ x y))
68 69 70
  (3 7 11))


71
(deftest loop.2.13
72
  (loop for (x . y) of-type (fixnum . fixnum) in '((1 . 2) (3 . 4) (5 . 6))
sds's avatar
sds committed
73
        collect (+ x y))
74 75
  (3 7 11))

76
(deftest loop.2.14
77
  (signals-error
78
   (loop for x in '(a b c)
sds's avatar
sds committed
79
         for x in '(d e f) collect x)
80 81
   program-error)
  t)
82

83
(deftest loop.2.15
84 85 86 87
  (signals-error
   (loop for (x . x) in '((a b) (c d)) collect x)
   program-error)
  t)
88

89
(deftest loop.2.16
90 91 92
  (loop for nil in nil do (return t))
  nil)

93 94 95 96 97 98 99 100 101 102 103 104 105
(deftest loop.2.17
  (let ((x '(a b c)))
    (values
     x
     (loop for x in '(d e f) collect (list x))
     x))
  (a b c)
  ((d) (e) (f))
  (a b c))

(deftest loop.2.18
  (loop for x of-type (integer 0 10) in '(2 4 6 7) sum x)
  19)
sds's avatar
sds committed
106

107 108 109 110 111
;;; Tests of the 'AS' form

(deftest loop.2.19
  (loop as x in '(1 2 3) sum x)
  6)
112

113 114
(deftest loop.2.20
  (loop as x in '(a b c)
sds's avatar
sds committed
115 116
        as y in '(1 2 3)
        collect (list x y))
117
  ((a 1) (b 2) (c 3)))
118

119 120
(deftest loop.2.21
  (loop as x in '(a b c)
sds's avatar
sds committed
121 122
        for y in '(1 2 3)
        collect (list x y))
123 124 125 126
  ((a 1) (b 2) (c 3)))

(deftest loop.2.22
  (loop for x in '(a b c)
sds's avatar
sds committed
127 128
        as y in '(1 2 3)
        collect (list x y))
129 130 131 132 133 134
  ((a 1) (b 2) (c 3)))

(deftest loop.2.23
  (let (a b (i 0))
    (values
     (loop for e in (progn (setf a (incf i))
sds's avatar
sds committed
135 136 137
                           '(a b c d e f g))
           by (progn (setf b (incf i)) #'cddr)
           collect e)
138 139 140
     a b i))
  (a c e g)
  1 2 2)
141 142


143 144 145 146 147 148 149 150 151 152 153 154 155
;;; Test that explicit calls to macroexpand in subforms
;;; are done in the correct environment

(deftest loop.2.24
  (macrolet
   ((%m (z) z))
   (loop for x in (expand-in-current-env (%m '(1 2 3))) sum x))
  6)

(deftest loop.2.25
  (macrolet
   ((%m (z) z))
   (loop for (x . y) in (expand-in-current-env (%m '((a . b) (c . d) (e . f))))
sds's avatar
sds committed
156
         collect (list x y)))
157 158 159 160 161 162
  ((a b) (c d) (e f)))

(deftest loop.2.26
  (macrolet
   ((%m (z) z))
   (loop as x in (expand-in-current-env (%m '(1 2 3))) sum x))
sds's avatar
sds committed
163
  6)