1 ;;; cl-tests.el -- Tests for CL
2 ;; Copyright (C) 2006, 2007 Sebastian Freundt
4 ;; Author: Sebastian Freundt <hroptatyr@sxemacs.org>
7 ;; This file is part of SXEmacs.
9 ;; Redistribution and use in source and binary forms, with or without
10 ;; modification, are permitted provided that the following conditions
13 ;; 1. Redistributions of source code must retain the above copyright
14 ;; notice, this list of conditions and the following disclaimer.
16 ;; 2. Redistributions in binary form must reproduce the above copyright
17 ;; notice, this list of conditions and the following disclaimer in the
18 ;; documentation and/or other materials provided with the distribution.
20 ;; 3. Neither the name of the author nor the names of any contributors
21 ;; may be used to endorse or promote products derived from this
22 ;; software without specific prior written permission.
24 ;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
25 ;; IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 ;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 ;; DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 ;; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 ;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 ;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
31 ;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 ;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
33 ;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
34 ;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 ;;; Synched up with: Not in FSF.
39 ;; See test-harness.el for instructions on how to run these tests.
43 (require 'test-harness)
46 (when (and (boundp 'load-file-name) (stringp load-file-name))
47 (push (file-name-directory load-file-name) load-path))
48 (require 'test-harness))))
51 (and (featurep 'modules)
52 (locate-module "cl-loop")
55 (when (featurep 'cl-loop)
56 ;;; do/dotimes/dolist tests
58 (eq (cl:do ((temp-one 1 (1+ temp-one))
59 (temp-two 0 (1- temp-two)))
60 ((> (- temp-one temp-two) 5) temp-one)) 4))
62 (eq (cl:do ((temp-one 1 (1+ temp-one))
63 (temp-two 0 (1+ temp-one)))
64 ((= 3 temp-two) temp-one)) 3))
66 (eq (cl:do* ((temp-one 1 (1+ temp-one))
67 (temp-two 0 (1+ temp-one)))
68 ((= 3 temp-two) temp-one)) 2))
74 (cl:dolist (i '(1 2 17) j)
78 (Assert (null (cl:do-symbols (i))))
79 (Assert (null (cl:do-all-symbols (i))))
84 (cl:loop for i from -2 do (incf coll))
87 (cl:loop for i downto -2 do (incf coll))
90 (cl:loop for i above -2 do (incf coll))
93 (cl:loop for i below 2 do (incf coll))
96 (cl:loop for i to 20 by 10 do (incf coll))
99 (cl:loop for i below 20 by 10 do (incf coll))
102 (cl:loop for i downto -20 by 10 do (incf coll))
105 (cl:loop for i downfrom 20 by 10 do (incf coll))
113 (Assert (= coll 10)))
115 ;; testing epilogue clauses
132 return (list a b c)))
155 initially (setq a 100)
163 (cl:loop for x from 1 to 10
166 '((1 nil) (2 2) (3 3) (4 4) (5 5) (6 6) (7 7) (8 8) (9 9) (10 10))))
170 (cl:loop for x from 1 to 10
173 '((1 nil) (2 1) (3 2) (4 3) (5 4) (6 5) (7 6) (8 7) (9 8) (10 9))))
175 ;; accumulation COLLECT
179 (cl:loop as i across [1 2 3 4 5 6 7 8 9 10]
180 collect (and (primep i) i))
181 '(nil 2 3 nil 5 nil 7 nil nil nil)))
184 (cl:loop as i across [1 2 3 4 5 6 7 8 9 10]
185 collect (and (evenp i) i))
186 '(nil 2 nil 4 nil 6 nil 8 nil 10))))
188 ;; accumulation COUNT
191 (cl:loop for i in '(a nil b nil c d nil e)
197 (cl:loop for i in '(a nil b nil c d nil e)
204 (cl:loop for i in '(a nil b nil c d nil e)
206 count (null i) into bar
212 (cl:loop for i in '(a nil b nil c d nil e)
214 ;; last count should be returned anyway
215 count (null i) into bar)
220 (cl:loop for i in '(a nil b nil c d nil e)
226 (cl:loop for i in '(a nil b nil c d nil e)
227 count (memq i '(a b)))
233 (cl:loop for i in '(1 2 3 4 5)
239 (cl:loop for i in '(1 2 3 4)
244 (= (let* ((series '(1.25 4.5 5.5)))
245 (cl:loop for v in series
251 (cl:loop for i in '(1 2 3 4)
257 (cl:loop for i in '(1 2 3 4)
258 sum (* -2 i) into bar
262 ;; accumulate MAXIMISE/MINIMISE
265 (cl:loop for i in '(1 2 3 4 3 2 1)
271 (cl:loop for i in '(1 2 3 4 3 2 1)
277 (cl:loop for i in '(1 2 3 4 3 2 1)
278 maximise (1+ i) into foo
279 minimise (1- i) into bar
280 return (list foo bar))
285 (cl:loop as i across [1 2 3 4 3 2 1]
286 maximise (1+ i) into foo
287 minimise (1- i) into bar
288 return (list foo bar))
293 (cl:loop for i on '(1 2 3 4 3 2 1)
297 ;;; cl-loop-tests.el ends here