1 ;;; generator-tests.el --- Testing generators -*- lexical-binding: t -*-
3 ;; Copyright (C) 2015-2016 Free Software Foundation, Inc.
5 ;; Author: Daniel Colascione <dancol@dancol.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 (defun generator-list-subrs ()
30 (cl-loop for x being the symbols
32 (cps--special-form-p (symbol-function x
)))
35 (defmacro cps-testcase
(name &rest body
)
36 "Perform a simple test of the continuation-transforming code.
38 `cps-testcase' defines an ERT testcase called NAME that evaluates
39 BODY twice: once using ordinary `eval' and once using
40 lambda-generators. The test ensures that the two forms produce
47 (funcall (lambda () ,@body
))
50 (iter-lambda () (iter-yield (progn ,@body
))))))))
51 (ert-deftest ,(intern (format "%s-noopt" name
)) ()
54 (funcall (lambda () ,@body
))
57 (let ((cps-inhibit-atomic-optimization t
))
58 (iter-lambda () (iter-yield (progn ,@body
)))))))))))
60 (put 'cps-testcase
'lisp-indent-function
1)
62 (defvar *cps-test-i
* nil
)
63 (defun cps-get-test-i ()
66 (cps-testcase cps-simple-1
(progn 1 2 3))
67 (cps-testcase cps-empty-progn
(progn))
68 (cps-testcase cps-inline-not-progn
(inline 1 2 3))
69 (cps-testcase cps-prog1-a
(prog1 1 2 3))
70 (cps-testcase cps-prog1-b
(prog1 1))
71 (cps-testcase cps-prog1-c
(prog2 1 2 3))
72 (cps-testcase cps-quote
(progn 'hello
))
73 (cps-testcase cps-function
(progn #'hello
))
75 (cps-testcase cps-and-fail
(and 1 nil
2))
76 (cps-testcase cps-and-succeed
(and 1 2 3))
77 (cps-testcase cps-and-empty
(and))
79 (cps-testcase cps-or-fallthrough
(or nil
1 2))
80 (cps-testcase cps-or-alltrue
(or 1 2 3))
81 (cps-testcase cps-or-empty
(or))
83 (cps-testcase cps-let
* (let* ((i 10)) i
))
84 (cps-testcase cps-let
*-shadow-empty
(let* ((i 10)) (let (i) i
)))
85 (cps-testcase cps-let
(let ((i 10)) i
))
86 (cps-testcase cps-let-shadow-empty
(let ((i 10)) (let (i) i
)))
87 (cps-testcase cps-let-novars
(let nil
42))
88 (cps-testcase cps-let
*-novars
(let* nil
42))
90 (cps-testcase cps-let-parallel
91 (let ((a 5) (b 6)) (let ((a b
) (b a
)) (list a b
))))
93 (cps-testcase cps-let
*-parallel
94 (let* ((a 5) (b 6)) (let* ((a b
) (b a
)) (list a b
))))
96 (cps-testcase cps-while-dynamic
98 (while (< *cps-test-i
* 10)
99 (setf *cps-test-i
* (+ *cps-test-i
* 1)))
102 (cps-testcase cps-while-lexical
106 (setf j
(+ j
(* i
10))))
109 (cps-testcase cps-while-incf
113 (setf j
(+ j
(* i
10))))
116 (cps-testcase cps-dynbind
117 (setf *cps-test-i
* 0)
118 (let* ((*cps-test-i
* 5))
121 (cps-testcase cps-nested-application
124 (cps-testcase cps-unwind-protect
125 (setf *cps-test-i
* 0)
127 (setf *cps-test-i
* 1)
128 (setf *cps-test-i
* 2))
131 (cps-testcase cps-catch-unused
134 (cps-testcase cps-catch-thrown
136 (throw 'mytag
(+ 2 2)))))
138 (cps-testcase cps-loop
139 (cl-loop for x from
1 to
10 collect x
))
141 (cps-testcase cps-loop-backquote
142 `(a b
,(cl-loop for x from
1 to
10 collect x
) -
1))
144 (cps-testcase cps-if-branch-a
147 (cps-testcase cps-if-branch-b
150 (cps-testcase cps-if-condition-fail
153 (cps-testcase cps-cond-empty
156 (cps-testcase cps-cond-atomi
159 (cps-testcase cps-cond-complex
160 (cond (nil 22) ((1+ 1) 42) (t 'bad
)))
162 (put 'cps-test-error
'error-conditions
'(cps-test-condition))
164 (cps-testcase cps-condition-case
167 (signal 'cps-test-error
'test-data
)
168 (cps-test-condition condvar
)))
170 (cps-testcase cps-condition-case-no-error
174 (cps-test-condition condvar
)))
176 (ert-deftest cps-generator-basic
()
177 (let* ((gen (iter-lambda ()
182 (gen-inst (funcall gen
)))
183 (should (eql (iter-next gen-inst
) 1))
184 (should (eql (iter-next gen-inst
) 2))
185 (should (eql (iter-next gen-inst
) 3))
187 ;; should-error doesn't catch the generator-end condition (which
188 ;; isn't an error), so we write our own.
192 (iter-end-of-sequence
193 (setf errored
(cdr x
))))
194 (should (eql errored
4)))))
196 (iter-defun mygenerator (i)
201 (ert-deftest cps-test-iter-do
()
203 (iter-do (x (mygenerator 4))
205 (should (equal mylist
'(2 4 1)))))
207 (iter-defun gen-using-yield-value ()
209 (setf f
(iter-yield 42))
213 (ert-deftest cps-yield-value
()
214 (let ((it (gen-using-yield-value)))
215 (should (eql (iter-next it -
1) 42))
216 (should (eql (iter-next it -
1) -
1))))
218 (ert-deftest cps-loop
()
220 (equal (cl-loop for x iter-by
(mygenerator 42)
224 (iter-defun gen-using-yield-from ()
225 (let ((sub-iter (gen-using-yield-value)))
226 (iter-yield (1+ (iter-yield-from sub-iter
)))))
228 (ert-deftest cps-test-yield-from-works
()
229 (let ((it (gen-using-yield-from)))
230 (should (eql (iter-next it -
1) 42))
231 (should (eql (iter-next it -
1) -
1))
232 (should (eql (iter-next it -
1) -
7))))
234 (defvar cps-test-closed-flag nil
)
236 (ert-deftest cps-test-iter-close
()
238 (let ((cps-test-closed-flag nil
))
241 (unwind-protect (iter-yield 1)
242 (setf cps-test-closed-flag t
))))))
243 (should (equal (iter-next iter
) 1))
244 (should (not cps-test-closed-flag
))
246 (should cps-test-closed-flag
))))
248 (ert-deftest cps-test-iter-close-idempotent
()
250 (let ((cps-test-closed-flag nil
))
253 (unwind-protect (iter-yield 1)
254 (setf cps-test-closed-flag t
))))))
255 (should (equal (iter-next iter
) 1))
256 (should (not cps-test-closed-flag
))
258 (should cps-test-closed-flag
)
259 (setf cps-test-closed-flag nil
)
261 (should (not cps-test-closed-flag
)))))
263 (ert-deftest cps-test-iter-cleanup-once-only
()
264 (let* ((nr-unwound 0)
266 (funcall (iter-lambda ()
272 (cl-incf nr-unwound
))))))
273 (should (equal (iter-next iter
) 1))
274 (should-error (iter-next iter
))
275 (should (equal nr-unwound
1))))
277 (iter-defun generator-with-docstring ()
282 (ert-deftest cps-test-declarations-preserved
()
283 (should (equal (documentation 'generator-with-docstring
) "Documentation!"))
284 (should (equal (get 'generator-with-docstring
'lisp-indent-function
) 5)))