Revert last change in fontset.el
[emacs.git] / test / automated / bytecomp-tests.el
blobf51710aa8c590903844ea9d9228ed60edc1706d2
1 ;;; bytecomp-testsuite.el
3 ;; Copyright (C) 2008-2015 Free Software Foundation, Inc.
5 ;; Author: Shigeru Fukaya <shigeru.fukaya@gmail.com>
6 ;; Created: November 2008
7 ;; Keywords: internal
8 ;; Human-Keywords: internal
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 (require 'ert)
29 ;;; Code:
30 (defconst byte-opt-testsuite-arith-data
32 ;; some functional tests
33 (let ((a most-positive-fixnum) (b 1) (c 1.0)) (+ a b c))
34 (let ((a most-positive-fixnum) (b -2) (c 1.0)) (- a b c))
35 (let ((a most-positive-fixnum) (b 2) (c 1.0)) (* a b c))
36 (let ((a 3) (b 2) (c 1.0)) (/ a b c))
37 (let ((a (+ 1 (expt 2 -64))) (b (expt 2 -65))) (+ a -1 b))
38 (let ((a (+ 1 (expt 2 -64))) (b (expt 2 -65))) (- a 1 (- b)))
39 ;; This fails. Should it be a bug?
40 ;; (let ((a (expt 2 -1074)) (b 0.125)) (* a 8 b))
41 (let ((a 1.0)) (* a 0))
42 (let ((a 1.0)) (* a 2.0 0))
43 (let ((a 1.0)) (/ 0 a))
44 (let ((a 1.0)) (/ 3 a 2))
45 (let ((a most-positive-fixnum) (b 2.0)) (* a 2 b))
46 (let ((a 3) (b 2)) (/ a b 1.0))
47 (/ 3 -1)
48 (+ 4 3 2 1)
49 (+ 4 3 2.0 1)
50 (- 4 3 2 1) ; not new, for reference
51 (- 4 3 2.0 1) ; not new, for reference
52 (* 4 3 2 1)
53 (* 4 3 2.0 1)
54 (/ 4 3 2 1)
55 (/ 4 3 2.0 1)
56 (let ((a 3) (b 2)) (+ a b 1))
57 (let ((a 3) (b 2)) (+ a b -1))
58 (let ((a 3) (b 2)) (- a b 1))
59 (let ((a 3) (b 2)) (- a b -1))
60 (let ((a 3) (b 2)) (+ a b a 1))
61 (let ((a 3) (b 2)) (+ a b a -1))
62 (let ((a 3) (b 2)) (- a b a 1))
63 (let ((a 3) (b 2)) (- a b a -1))
64 (let ((a 3) (b 2)) (* a b -1))
65 (let ((a 3) (b 2)) (* a -1))
66 (let ((a 3) (b 2)) (/ a b 1))
67 (let ((a 3) (b 2)) (/ (+ a b) 1))
69 ;; coverage test
70 (let ((a 3) (b 2) (c 1.0)) (+))
71 (let ((a 3) (b 2) (c 1.0)) (+ 2))
72 (let ((a 3) (b 2) (c 1.0)) (+ 2 0))
73 (let ((a 3) (b 2) (c 1.0)) (+ 2 0.0))
74 (let ((a 3) (b 2) (c 1.0)) (+ 2.0))
75 (let ((a 3) (b 2) (c 1.0)) (+ 2.0 0))
76 (let ((a 3) (b 2) (c 1.0)) (+ 2.0 0.0))
77 (let ((a 3) (b 2) (c 1.0)) (+ 0 2))
78 (let ((a 3) (b 2) (c 1.0)) (+ 0 2.0))
79 (let ((a 3) (b 2) (c 1.0)) (+ 0.0 2))
80 (let ((a 3) (b 2) (c 1.0)) (+ 0.0 2.0))
81 (let ((a 3) (b 2) (c 1.0)) (+ a))
82 (let ((a 3) (b 2) (c 1.0)) (+ a 0))
83 (let ((a 3) (b 2) (c 1.0)) (+ a 0.0))
84 (let ((a 3) (b 2) (c 1.0)) (+ 0 a))
85 (let ((a 3) (b 2) (c 1.0)) (+ 0.0 a))
86 (let ((a 3) (b 2) (c 1.0)) (+ c 0))
87 (let ((a 3) (b 2) (c 1.0)) (+ c 0.0))
88 (let ((a 3) (b 2) (c 1.0)) (+ 0 c))
89 (let ((a 3) (b 2) (c 1.0)) (+ 0.0 c))
90 (let ((a 3) (b 2) (c 1.0)) (+ a b 0 c 0))
91 (let ((a 3) (b 2) (c 1.0)) (+ 0 a))
92 (let ((a 3) (b 2) (c 1.0)) (+ 0 a b))
93 (let ((a 3) (b 2) (c 1.0)) (+ 0 a b c))
94 (let ((a 3) (b 2) (c 1.0)) (+ 1 2 3))
95 (let ((a 3) (b 2) (c 1.0)) (+ 3.0 2.0 1))
96 (let ((a 3) (b 2) (c 1.0)) (+ 3.0 2.0 1 4))
97 (let ((a 3) (b 2) (c 1.0)) (+ a 1))
98 (let ((a 3) (b 2) (c 1.0)) (+ a -1))
99 (let ((a 3) (b 2) (c 1.0)) (+ 1 a))
100 (let ((a 3) (b 2) (c 1.0)) (+ -1 a))
101 (let ((a 3) (b 2) (c 1.0)) (+ c 1))
102 (let ((a 3) (b 2) (c 1.0)) (+ c -1))
103 (let ((a 3) (b 2) (c 1.0)) (+ 1 c))
104 (let ((a 3) (b 2) (c 1.0)) (+ -1 c))
105 (let ((a 3) (b 2) (c 1.0)) (+ a b 0))
106 (let ((a 3) (b 2) (c 1.0)) (+ a b 1))
107 (let ((a 3) (b 2) (c 1.0)) (+ a b -1))
108 (let ((a 3) (b 2) (c 1.0)) (+ a b 2))
109 (let ((a 3) (b 2) (c 1.0)) (+ 1 a b c))
110 (let ((a 3) (b 2) (c 1.0)) (+ a b c 0))
111 (let ((a 3) (b 2) (c 1.0)) (+ a b c 1))
112 (let ((a 3) (b 2) (c 1.0)) (+ a b c -1))
114 (let ((a 3) (b 2) (c 1.0)) (-))
115 (let ((a 3) (b 2) (c 1.0)) (- 2))
116 (let ((a 3) (b 2) (c 1.0)) (- 2 0))
117 (let ((a 3) (b 2) (c 1.0)) (- 2 0.0))
118 (let ((a 3) (b 2) (c 1.0)) (- 2.0))
119 (let ((a 3) (b 2) (c 1.0)) (- 2.0 0))
120 (let ((a 3) (b 2) (c 1.0)) (- 2.0 0.0))
121 (let ((a 3) (b 2) (c 1.0)) (- 0 2))
122 (let ((a 3) (b 2) (c 1.0)) (- 0 2.0))
123 (let ((a 3) (b 2) (c 1.0)) (- 0.0 2))
124 (let ((a 3) (b 2) (c 1.0)) (- 0.0 2.0))
125 (let ((a 3) (b 2) (c 1.0)) (- a))
126 (let ((a 3) (b 2) (c 1.0)) (- a 0))
127 (let ((a 3) (b 2) (c 1.0)) (- a 0.0))
128 (let ((a 3) (b 2) (c 1.0)) (- 0 a))
129 (let ((a 3) (b 2) (c 1.0)) (- 0.0 a))
130 (let ((a 3) (b 2) (c 1.0)) (- c 0))
131 (let ((a 3) (b 2) (c 1.0)) (- c 0.0))
132 (let ((a 3) (b 2) (c 1.0)) (- 0 c))
133 (let ((a 3) (b 2) (c 1.0)) (- 0.0 c))
134 (let ((a 3) (b 2) (c 1.0)) (- a b 0 c 0))
135 (let ((a 3) (b 2) (c 1.0)) (- 0 a))
136 (let ((a 3) (b 2) (c 1.0)) (- 0 a b))
137 (let ((a 3) (b 2) (c 1.0)) (- 0 a b c))
138 (let ((a 3) (b 2) (c 1.0)) (- 1 2 3))
139 (let ((a 3) (b 2) (c 1.0)) (- 3.0 2.0 1))
140 (let ((a 3) (b 2) (c 1.0)) (- 3.0 2.0 1 4))
141 (let ((a 3) (b 2) (c 1.0)) (- a 1))
142 (let ((a 3) (b 2) (c 1.0)) (- a -1))
143 (let ((a 3) (b 2) (c 1.0)) (- 1 a))
144 (let ((a 3) (b 2) (c 1.0)) (- -1 a))
145 (let ((a 3) (b 2) (c 1.0)) (- c 1))
146 (let ((a 3) (b 2) (c 1.0)) (- c -1))
147 (let ((a 3) (b 2) (c 1.0)) (- 1 c))
148 (let ((a 3) (b 2) (c 1.0)) (- -1 c))
149 (let ((a 3) (b 2) (c 1.0)) (- a b 0))
150 (let ((a 3) (b 2) (c 1.0)) (- a b 1))
151 (let ((a 3) (b 2) (c 1.0)) (- a b -1))
152 (let ((a 3) (b 2) (c 1.0)) (- a b 2))
153 (let ((a 3) (b 2) (c 1.0)) (- 1 a b c))
154 (let ((a 3) (b 2) (c 1.0)) (- a b c 0))
155 (let ((a 3) (b 2) (c 1.0)) (- a b c 1))
156 (let ((a 3) (b 2) (c 1.0)) (- a b c -1))
158 (let ((a 3) (b 2) (c 1.0)) (*))
159 (let ((a 3) (b 2) (c 1.0)) (* 2))
160 (let ((a 3) (b 2) (c 1.0)) (* 2 0))
161 (let ((a 3) (b 2) (c 1.0)) (* 2 0.0))
162 (let ((a 3) (b 2) (c 1.0)) (* 2.0))
163 (let ((a 3) (b 2) (c 1.0)) (* 2.0 0))
164 (let ((a 3) (b 2) (c 1.0)) (* 2.0 0.0))
165 (let ((a 3) (b 2) (c 1.0)) (* 0 2))
166 (let ((a 3) (b 2) (c 1.0)) (* 0 2.0))
167 (let ((a 3) (b 2) (c 1.0)) (* 0.0 2))
168 (let ((a 3) (b 2) (c 1.0)) (* 0.0 2.0))
169 (let ((a 3) (b 2) (c 1.0)) (* a))
170 (let ((a 3) (b 2) (c 1.0)) (* a 0))
171 (let ((a 3) (b 2) (c 1.0)) (* a 0.0))
172 (let ((a 3) (b 2) (c 1.0)) (* 0 a))
173 (let ((a 3) (b 2) (c 1.0)) (* 0.0 a))
174 (let ((a 3) (b 2) (c 1.0)) (* c 0))
175 (let ((a 3) (b 2) (c 1.0)) (* c 0.0))
176 (let ((a 3) (b 2) (c 1.0)) (* 0 c))
177 (let ((a 3) (b 2) (c 1.0)) (* 0.0 c))
178 (let ((a 3) (b 2) (c 1.0)) (* a b 0 c 0))
179 (let ((a 3) (b 2) (c 1.0)) (* 0 a))
180 (let ((a 3) (b 2) (c 1.0)) (* 0 a b))
181 (let ((a 3) (b 2) (c 1.0)) (* 0 a b c))
182 (let ((a 3) (b 2) (c 1.0)) (* 1 2 3))
183 (let ((a 3) (b 2) (c 1.0)) (* 3.0 2.0 1))
184 (let ((a 3) (b 2) (c 1.0)) (* 3.0 2.0 1 4))
185 (let ((a 3) (b 2) (c 1.0)) (* a 1))
186 (let ((a 3) (b 2) (c 1.0)) (* a -1))
187 (let ((a 3) (b 2) (c 1.0)) (* 1 a))
188 (let ((a 3) (b 2) (c 1.0)) (* -1 a))
189 (let ((a 3) (b 2) (c 1.0)) (* c 1))
190 (let ((a 3) (b 2) (c 1.0)) (* c -1))
191 (let ((a 3) (b 2) (c 1.0)) (* 1 c))
192 (let ((a 3) (b 2) (c 1.0)) (* -1 c))
193 (let ((a 3) (b 2) (c 1.0)) (* a b 0))
194 (let ((a 3) (b 2) (c 1.0)) (* a b 1))
195 (let ((a 3) (b 2) (c 1.0)) (* a b -1))
196 (let ((a 3) (b 2) (c 1.0)) (* a b 2))
197 (let ((a 3) (b 2) (c 1.0)) (* 1 a b c))
198 (let ((a 3) (b 2) (c 1.0)) (* a b c 0))
199 (let ((a 3) (b 2) (c 1.0)) (* a b c 1))
200 (let ((a 3) (b 2) (c 1.0)) (* a b c -1))
202 (let ((a 3) (b 2) (c 1.0)) (/))
203 (let ((a 3) (b 2) (c 1.0)) (/ 2))
204 (let ((a 3) (b 2) (c 1.0)) (/ 2 0))
205 (let ((a 3) (b 2) (c 1.0)) (/ 2 0.0))
206 (let ((a 3) (b 2) (c 1.0)) (/ 2.0))
207 (let ((a 3) (b 2) (c 1.0)) (/ 2.0 0))
208 (let ((a 3) (b 2) (c 1.0)) (/ 2.0 0.0))
209 (let ((a 3) (b 2) (c 1.0)) (/ 0 2))
210 (let ((a 3) (b 2) (c 1.0)) (/ 0 2.0))
211 (let ((a 3) (b 2) (c 1.0)) (/ 0.0 2))
212 (let ((a 3) (b 2) (c 1.0)) (/ 0.0 2.0))
213 (let ((a 3) (b 2) (c 1.0)) (/ a))
214 (let ((a 3) (b 2) (c 1.0)) (/ a 0))
215 (let ((a 3) (b 2) (c 1.0)) (/ a 0.0))
216 (let ((a 3) (b 2) (c 1.0)) (/ 0 a))
217 (let ((a 3) (b 2) (c 1.0)) (/ 0.0 a))
218 (let ((a 3) (b 2) (c 1.0)) (/ c 0))
219 (let ((a 3) (b 2) (c 1.0)) (/ c 0.0))
220 (let ((a 3) (b 2) (c 1.0)) (/ 0 c))
221 (let ((a 3) (b 2) (c 1.0)) (/ 0.0 c))
222 (let ((a 3) (b 2) (c 1.0)) (/ a b 0 c 0))
223 (let ((a 3) (b 2) (c 1.0)) (/ 0 a))
224 (let ((a 3) (b 2) (c 1.0)) (/ 0 a b))
225 (let ((a 3) (b 2) (c 1.0)) (/ 0 a b c))
226 (let ((a 3) (b 2) (c 1.0)) (/ 1 2 3))
227 (let ((a 3) (b 2) (c 1.0)) (/ 3.0 2.0 1))
228 (let ((a 3) (b 2) (c 1.0)) (/ 3.0 2.0 1 4))
229 (let ((a 3) (b 2) (c 1.0)) (/ a 1))
230 (let ((a 3) (b 2) (c 1.0)) (/ a -1))
231 (let ((a 3) (b 2) (c 1.0)) (/ 1 a))
232 (let ((a 3) (b 2) (c 1.0)) (/ -1 a))
233 (let ((a 3) (b 2) (c 1.0)) (/ c 1))
234 (let ((a 3) (b 2) (c 1.0)) (/ c -1))
235 (let ((a 3) (b 2) (c 1.0)) (/ 1 c))
236 (let ((a 3) (b 2) (c 1.0)) (/ -1 c))
237 (let ((a 3) (b 2) (c 1.0)) (/ a b 0))
238 (let ((a 3) (b 2) (c 1.0)) (/ a b 1))
239 (let ((a 3) (b 2) (c 1.0)) (/ a b -1))
240 (let ((a 3) (b 2) (c 1.0)) (/ a b 2))
241 (let ((a 3) (b 2) (c 1.0)) (/ 1 a b c))
242 (let ((a 3) (b 2) (c 1.0)) (/ a b c 0))
243 (let ((a 3) (b 2) (c 1.0)) (/ a b c 1))
244 (let ((a 3) (b 2) (c 1.0)) (/ a b c -1)))
245 "List of expression for test.
246 Each element will be executed by interpreter and with
247 bytecompiled code, and their results compared.")
249 (defun bytecomp-check-1 (pat)
250 "Return non-nil if PAT is the same whether directly evalled or compiled."
251 (let ((warning-minimum-log-level :emergency)
252 (byte-compile-warnings nil)
253 (v0 (condition-case nil
254 (eval pat)
255 (error nil)))
256 (v1 (condition-case nil
257 (funcall (byte-compile (list 'lambda nil pat)))
258 (error nil))))
259 (equal v0 v1)))
261 (put 'bytecomp-check-1 'ert-explainer 'bytecomp-explain-1)
263 (defun bytecomp-explain-1 (pat)
264 (let ((v0 (condition-case nil
265 (eval pat)
266 (error nil)))
267 (v1 (condition-case nil
268 (funcall (byte-compile (list 'lambda nil pat)))
269 (error nil))))
270 (format "Expression `%s' gives `%s' if directly evalled, `%s' if compiled."
271 pat v0 v1)))
273 (ert-deftest bytecomp-tests ()
274 "Test the Emacs byte compiler."
275 (dolist (pat byte-opt-testsuite-arith-data)
276 (should (bytecomp-check-1 pat))))
278 (defun test-byte-opt-arithmetic (&optional arg)
279 "Unit test for byte-opt arithmetic operations.
280 Subtests signal errors if something goes wrong."
281 (interactive "P")
282 (switch-to-buffer (generate-new-buffer "*Font Pase Test*"))
283 (let ((warning-minimum-log-level :emergency)
284 (byte-compile-warnings nil)
285 (pass-face '((t :foreground "green")))
286 (fail-face '((t :foreground "red")))
287 (print-escape-nonascii t)
288 (print-escape-newlines t)
289 (print-quoted t)
290 v0 v1)
291 (dolist (pat byte-opt-testsuite-arith-data)
292 (condition-case nil
293 (setq v0 (eval pat))
294 (error (setq v0 nil)))
295 (condition-case nil
296 (setq v1 (funcall (byte-compile (list 'lambda nil pat))))
297 (error (setq v1 nil)))
298 (insert (format "%s" pat))
299 (indent-to-column 65)
300 (if (equal v0 v1)
301 (insert (propertize "OK" 'face pass-face))
302 (insert (propertize "FAIL\n" 'face fail-face))
303 (indent-to-column 55)
304 (insert (propertize (format "[%s] vs [%s]" v0 v1)
305 'face fail-face)))
306 (insert "\n"))))
308 (defun test-byte-comp-compile-and-load (compile &rest forms)
309 (let ((elfile nil)
310 (elcfile nil))
311 (unwind-protect
312 (progn
313 (setf elfile (make-temp-file "test-bytecomp" nil ".el"))
314 (when compile
315 (setf elcfile (make-temp-file "test-bytecomp" nil ".elc")))
316 (with-temp-buffer
317 (dolist (form forms)
318 (print form (current-buffer)))
319 (write-region (point-min) (point-max) elfile nil 'silent))
320 (if compile
321 (let ((byte-compile-dest-file-function
322 (lambda (e) elcfile)))
323 (byte-compile-file elfile t))
324 (load elfile nil 'nomessage)))
325 (when elfile (delete-file elfile))
326 (when elcfile (delete-file elcfile)))))
327 (put 'test-byte-comp-compile-and-load 'lisp-indent-function 1)
329 (ert-deftest test-byte-comp-macro-expansion ()
330 (test-byte-comp-compile-and-load t
331 '(progn (defmacro abc (arg) 1) (defun def () (abc 2))))
332 (should (equal (funcall 'def) 1)))
334 (ert-deftest test-byte-comp-macro-expansion-eval-and-compile ()
335 (test-byte-comp-compile-and-load t
336 '(eval-and-compile (defmacro abc (arg) -1) (defun def () (abc 2))))
337 (should (equal (funcall 'def) -1)))
339 (ert-deftest test-byte-comp-macro-expansion-eval-when-compile ()
340 ;; Make sure we interpret eval-when-compile forms properly. CLISP
341 ;; and SBCL interpreter eval-when-compile (well, the CL equivalent)
342 ;; in the same way.
343 (test-byte-comp-compile-and-load t
344 '(eval-when-compile
345 (defmacro abc (arg) -10)
346 (defun abc-1 () (abc 2)))
347 '(defmacro abc-2 () (abc-1))
348 '(defun def () (abc-2)))
349 (should (equal (funcall 'def) -10)))
351 (ert-deftest test-byte-comp-macro-expand-lexical-override ()
352 ;; Intuitively, one might expect the defmacro to override the
353 ;; macrolet since macrolet's is explicitly called out as being
354 ;; equivalent to toplevel, but CLISP and SBCL both evaluate the form
355 ;; this way, so we should too.
356 (test-byte-comp-compile-and-load t
357 '(require 'cl-lib)
358 '(cl-macrolet ((m () 4))
359 (defmacro m () 5)
360 (defun def () (m))))
361 (should (equal (funcall 'def) 4)))
363 (ert-deftest bytecomp-tests--warnings ()
364 (with-current-buffer (get-buffer-create "*Compile-Log*")
365 (let ((inhibit-read-only t)) (erase-buffer)))
366 (test-byte-comp-compile-and-load t
367 '(progn
368 (defun my-test0 ()
369 (my--test11 3)
370 (my--test12 3)
371 (my--test2 5))
372 (defmacro my--test11 (arg) (+ arg 1))
373 (eval-and-compile
374 (defmacro my--test12 (arg) (+ arg 1))
375 (defun my--test2 (arg) (+ arg 1)))))
376 (with-current-buffer (get-buffer-create "*Compile-Log*")
377 (goto-char (point-min))
378 ;; Should warn that mt--test1[12] are first used as functions.
379 (should (re-search-forward "my--test11:\n.*macro" nil t))
380 (should (re-search-forward "my--test12:\n.*macro" nil t))
381 (goto-char (point-min))
382 ;; Should not warn that mt--test2 is not known to be defined.
383 (should-not (re-search-forward "my--test2" nil t))))
385 (ert-deftest test-eager-load-macro-expansion ()
386 (test-byte-comp-compile-and-load nil
387 '(progn (defmacro abc (arg) 1) (defun def () (abc 2))))
388 (should (equal (funcall 'def) 1)))
390 (ert-deftest test-eager-load-macro-expansion-eval-and-compile ()
391 (test-byte-comp-compile-and-load nil
392 '(eval-and-compile (defmacro abc (arg) -1) (defun def () (abc 2))))
393 (should (equal (funcall 'def) -1)))
395 (ert-deftest test-eager-load-macro-expansion-eval-when-compile ()
396 ;; Make sure we interpret eval-when-compile forms properly. CLISP
397 ;; and SBCL interpreter eval-when-compile (well, the CL equivalent)
398 ;; in the same way.
399 (test-byte-comp-compile-and-load nil
400 '(eval-when-compile
401 (defmacro abc (arg) -10)
402 (defun abc-1 () (abc 2)))
403 '(defmacro abc-2 () (abc-1))
404 '(defun def () (abc-2)))
405 (should (equal (funcall 'def) -10)))
407 (ert-deftest test-eager-load-macro-expand-lexical-override ()
408 ;; Intuitively, one might expect the defmacro to override the
409 ;; macrolet since macrolet's is explicitly called out as being
410 ;; equivalent to toplevel, but CLISP and SBCL both evaluate the form
411 ;; this way, so we should too.
412 (test-byte-comp-compile-and-load nil
413 '(require 'cl-lib)
414 '(cl-macrolet ((m () 4))
415 (defmacro m () 5)
416 (defun def () (m))))
417 (should (equal (funcall 'def) 4)))
420 ;; Local Variables:
421 ;; no-byte-compile: t
422 ;; End:
424 (provide 'byte-opt-testsuite)