Fix the inefficient evaluation of translated predicates
[maxima.git] / src / mload.lisp
blobefb6c3e0eacb718ff1e8b8e04f5c92d79bc34d12
1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancments. ;;;;;
4 ;;; ;;;;;
5 ;;; Copyright (c) 1984,1987 by William Schelter,University of Texas ;;;;;
6 ;;; All rights reserved ;;;;;
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
9 ;;; (c) Copyright 1982 Massachusetts Institute of Technology ;;;
10 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
12 (in-package :maxima)
13 (macsyma-module mload)
15 (declare-top (special $file_search_lisp $file_search_maxima $file_search_demo $loadprint))
17 (defun load-and-tell (filename)
18 (loadfile filename t ;; means this is a lisp-level call, not user-level.
19 $loadprint))
21 (defun errset-namestring (x)
22 (let ((errset nil))
23 (errset (pathname x))))
25 (defmfun $filename_merge (&rest file-specs)
26 (when (or (null file-specs) (cddr file-specs))
27 (wna-err '$filename_merge))
28 (setq file-specs (mapcar #'macsyma-namestring-sub file-specs))
29 (pathname (if (null (cdr file-specs))
30 (car file-specs)
31 (merge-pathnames (cadr file-specs) (car file-specs)))))
33 (defun macsyma-namestring-sub (user-object)
34 (if (pathnamep user-object) user-object
35 (let* ((system-object
36 (cond ((and (atom user-object) (not (symbolp user-object)))
37 user-object)
38 ((atom user-object) ;hence a symbol in view of the
39 (print-invert-case (fullstrip1 user-object))) ; first clause
40 (($listp user-object)
41 (fullstrip (cdr user-object)))
43 (merror (intl:gettext "filename_merge: unexpected argument: ~M") user-object))))
44 (namestring-try (errset-namestring system-object)))
45 (if namestring-try (car namestring-try)
46 ;; know its small now, so print on same line.
47 (merror (intl:gettext "filename_merge: unexpected argument: ~:M") user-object)))))
49 ;; Returns the truename corresponding to a stream, or nil (for non-file streams).
50 ;; Previously we used (subtypep (type-of stream) 'file-stream) to determine whether
51 ;; a stream is a file stream, but this doesn't work on GCL.
52 (defun get-stream-truename (stream)
53 (handler-case
54 (probe-file stream)
55 (error () nil)))
57 (defmvar $load_pathname nil
58 "The full pathname of the file being loaded")
60 (defmfun $batchload (filename-or-stream &aux (*mread-prompt* ""))
61 (declare (special *mread-prompt*))
62 (if (streamp filename-or-stream)
63 (batchload-stream filename-or-stream)
64 (let
65 ((filename ($file_search1 filename-or-stream '((mlist) $file_search_maxima))))
66 (with-open-file (in-stream filename)
67 (batchload-stream in-stream)))))
69 (defun batchload-stream (in-stream &key autoloading-p)
70 (let ($load_pathname)
71 (let*
72 ((noevalargs nil)
73 (*read-base* 10.)
74 (stream-truename (get-stream-truename in-stream))
75 (in-stream-string-rep
76 (if stream-truename
77 (setq $load_pathname (cl:namestring stream-truename))
78 (format nil "~A" in-stream)))
79 ;; If we arrived here from autoloading, call MEVAL instead of MEVAL*
80 ;; since MEVAL* is intended to be called from the interpreter top level;
81 ;; MEVAL* modifies global state, resetting VARLIST and calling CLEARSIGN.
82 (meval-fcn (symbol-function (if autoloading-p 'meval 'meval*)))
83 (expr nil))
84 (declare (special *prompt-on-read-hang*))
85 (when $loadprint
86 (format t (intl:gettext "~&read and interpret ~A~&") in-stream-string-rep))
87 (cleanup)
88 (newline in-stream)
89 (loop while (and
90 (setq expr (let (*prompt-on-read-hang*) (mread in-stream nil)))
91 (consp expr))
92 do (funcall meval-fcn (third expr)))
93 in-stream-string-rep)))
95 ;;returns appropriate error or existing pathname.
96 ;; the second argument is a maxima list of variables
97 ;; each of which contains a list of paths. This is
98 ;; so users can correct the variable..
99 (defmfun $file_search1 (name search-lists &aux lis)
100 (if (pathnamep name)
101 (setq name (namestring name)))
102 (setq lis (apply '$append (mapcar 'symbol-value (cdr search-lists))))
103 (let ((res ($file_search name lis)))
104 (or res
105 (merror (intl:gettext "file_search1: ~M not found in ~A.")
106 name
107 (string-trim "[]" ($sconcat search-lists))))))
109 (defmfun $load (filename)
110 "This is the generic file loading function.
111 LOAD(filename) will either BATCHLOAD or LOADFILE the file,
112 depending on wether the file contains Macsyma, Lisp, or Compiled
113 code. The file specifications default such that a compiled file
114 is searched for first, then a lisp file, and finally a macsyma batch
115 file. This command is designed to provide maximum utility and
116 convenience for writers of packages and users of the macsyma->lisp
117 translator."
119 (if (or (stringp filename) (symbolp filename) (pathnamep filename))
120 (let ((searched-for
121 ($file_search1 filename
122 '((mlist) $file_search_maxima $file_search_lisp )))
123 type)
124 (setq type ($file_type searched-for))
125 (case type
126 (($maxima)
127 ($batchload searched-for))
128 (($lisp $object)
129 ;; do something about handling errors
130 ;; during loading. Foobar fail act errors.
131 (load-and-tell searched-for))
133 ;; UNREACHABLE MESSAGE: DEFAULT TYPE IS '$OBJECT (SEE $FILE_TYPE BELOW)
134 (merror "Maxima bug: Unknown file type ~M" type)))
135 searched-for)
136 (merror "load: argument must be a string, symbol, or pathname; found: ~M" filename)))
138 (defmvar $file_type_lisp
139 (list '(mlist) "l" "lsp" "lisp"))
141 (defmvar $file_type_maxima
142 (list '(mlist) "mac" "mc" "demo" "dem" "dm1" "dm2" "dm3" "dmt" "wxm"))
144 (defmfun $file_type (fil)
145 (let ((typ ($pathname_type fil)))
146 (cond
147 ((member typ (cdr $file_type_lisp) :test #'string=)
148 '$lisp)
149 ((member typ (cdr $file_type_maxima) :test #'string=)
150 '$maxima)
152 '$object))))
154 (defmfun $pathname_directory (path)
155 (let ((pathname (pathname path)))
156 (namestring (make-pathname :directory (pathname-directory pathname)))))
158 (defmfun $pathname_name (path)
159 (let ((pathname (pathname path)))
160 (pathname-name pathname)))
162 (defmfun $pathname_type (path)
163 (let ((pathname (pathname path)))
164 (pathname-type pathname)))
167 (defvar *macsyma-startup-queue* nil)
169 (declaim (special *mread-prompt*))
171 ;;;; batch & demo search hacks
173 (defmfun $batch (filename-or-stream &optional (demo :batch)
174 &aux tem (possible '(:demo :batch :test)))
175 "giving a second argument makes it use demo mode, ie pause after evaluation
176 of each command line"
178 ;; Try to get rid of testsuite failures on machines that are low on RAM.
179 ($garbage_collect)
180 (cond
181 ((setq tem (member ($mkey demo) possible :test #'eq))
182 (setq demo (car tem)))
183 (t (format t (intl:gettext "batch: second argument must be 'demo', 'batch' or 'test'; found: ~A, assumed 'batch'~%") demo)))
184 (if (streamp filename-or-stream)
185 (batch-stream filename-or-stream demo)
186 (let
187 ((filename ($file_search1 filename-or-stream
188 (if (eql demo :demo)
189 '((mlist) $file_search_demo )
190 '((mlist) $file_search_maxima)))))
191 (cond
192 ((eq demo :test)
193 (test-batch filename nil :show-all t)) ;; NEED TO ACCEPT INPUT STREAM HERE TOO
195 (with-open-file (in-stream filename)
196 (batch-stream in-stream demo)))))))
198 (defun batch-stream (in-stream demo)
199 (let ($load_pathname)
200 (let*
201 ((*read-base* 10.)
202 (stream-truename (get-stream-truename in-stream))
203 (in-stream-string-rep
204 (if stream-truename
205 (setq $load_pathname (cl:namestring stream-truename))
206 (format nil "~A" in-stream))))
207 (format t (intl:gettext "~%read and interpret ~A~%") in-stream-string-rep)
208 (catch 'macsyma-quit (continue :stream in-stream :batch-or-demo-flag demo))
209 (incf $linenum)
210 in-stream-string-rep)))
212 ;; When either a or b are special float values (NaN or +/-Inf), return true iff
213 ;; a and b are the both NaN or both +Inf or both -Inf.
214 ;; Note that float_approx_equal(NaN, NaN) returns true.
216 ;; When a and b to finite, nonzero floats, return true iff
218 ;; |a - b| <= float_approx_equal_tolerance * min(2^n, 2^m)
220 ;; where a = af * 2^m, |af| < 1, and m is an integer (similarly for b).
221 ;; See Knuth, "The Art of Computer Programming" (3rd ed.), Vol. 2, Sec. 4.2.2, Eq. 24, page 233.
223 ;; Note that Eq. 24 isn't well-defined for a or b equal to zero.
224 ;; To make progress, let's consider the limit as b --> 0,
225 ;; therefore n --> -inf. For any finite a, that means that min(2^n, 2^m) = 2^n
226 ;; and therefore |a - b| --> |a|, but to pass the test,
227 ;; |a| must be less than or equal to float_approx_equal_tolerance * 2^n --> 0.
228 ;; Therefore the test fails for all nonzero, finite a, when b = 0.
229 ;; i.e. when either a or b is zero but not both, return false.
231 ;; Note also that if a < 0 and b > 0 or vice versa, the test must fail:
232 ;; without loss of generality assume |a| > |b|. Then n <= m and min(2^n, 2^m) = 2^n.
233 ;; Now |a - b| = |a| + |b| since a and b are different signs.
234 ;; Then |a - b| / min(2^n, 2^m) = |a/min(2^n, 2^m)| + |b/min(2^n, 2^m)| = |af|*2^m/2^n + |bf| >= |af| + |bf| > 1.
235 ;; So unless float_approx_equal_tolerance is unusually large, the test must fail.
237 ;; Otherwise, either a or b is not a float, so return false.
239 (defmvar $float_approx_equal_tolerance (* 8 flonum-epsilon))
241 (defmfun $float_approx_equal (a b)
242 (setq a (if (floatp a) a ($float a)))
243 (setq b (if (floatp b) b ($float b)))
244 (and
245 (floatp a)
246 (floatp b)
247 (cond
248 ;; look for NaN
249 ((/= a a) (/= b b))
250 ((/= b b) nil)
251 ;; look for Inf
252 ((> (abs a) most-positive-double-float) (= a b))
253 ((> (abs b) most-positive-double-float) nil)
254 ;; look for zero
255 ((= a 0d0) (= b 0d0))
256 ((= b 0d0) nil)
257 ;; look for A = B
258 ((= a b))
260 ;; Implement test without involving floating-point arithmetic,
261 ;; to avoid errors which could occur with extreme values.
262 (let (a-significand a-expt a-sign b-significand b-expt b-sign)
263 (multiple-value-setq (a-significand a-expt a-sign) (decode-float a))
264 (multiple-value-setq (b-significand b-expt b-sign) (decode-float b))
265 (if (or (= a-sign b-sign) (>= $float_approx_equal_tolerance 1d0))
266 (let (a-b-significand a-b-expt a-b-sign tol-significand tol-expt tol-sign)
267 (multiple-value-setq (a-b-significand a-b-expt a-b-sign) (integer-decode-float (abs (- a b))))
268 (multiple-value-setq (tol-significand tol-expt tol-sign) (integer-decode-float $float_approx_equal_tolerance))
269 (or (< a-b-expt (+ tol-expt (min a-expt b-expt)))
270 (and (= a-b-expt (+ tol-expt (min a-expt b-expt)))
271 (<= a-b-significand tol-significand))))))))))
273 ;; Big float version of float_approx_equal. But for bfloat_approx_equal, the tolerance isn't
274 ;; user settable; instead, it is 32 / 2^fpprec. The factor of 32 is too large, I suppose. But
275 ;; the test suite gives a few errors with a factor of 16. These errors might be due to
276 ;; float / big float comparisons.
278 (defmfun $bfloat_approx_equal (a b)
279 (setq a (if ($bfloatp a) a ($bfloat a)))
280 (setq b (if ($bfloatp b) b ($bfloat b)))
281 (let ((m) (bits))
282 (and
283 ($bfloatp a)
284 ($bfloatp b)
285 (setq bits (min (third (first a)) (third (first b))))
286 (setq m (* 32 (expt 2 (- bits)) (min (expt 2 (- (car (last a)) 1)) (expt 2 (- (car (last b)) 1)))))
287 (setq m (if (rationalp m) (div (numerator m) (denominator m)) m))
288 (eq t (mgqp m (take '(mabs) (sub a b)))))))
291 ;; The first argument 'f' is the expected result; the second argument
292 ;; 'g' is the output of the test. By explicit evaluation, the expected
293 ;; result *can* be a CL array, CL hashtable, or a taylor polynomial. Such
294 ;; a test would look something like (yes, it's a silly test)
296 ;; taylor(x,x,0,2);
297 ;; ''(taylor(x,x,0,2)
299 (defun approx-alike (f g)
301 (cond ((floatp f) (and (floatp g) ($float_approx_equal f g)))
303 (($bfloatp f) (and ($bfloatp g) ($bfloat_approx_equal f g)))
305 (($taylorp g)
306 (approx-alike 0 (sub (ratdisrep f) (ratdisrep g))))
308 ((stringp f)
309 (and (stringp g) (string= f g)))
311 ((arrayp f)
312 (and (arrayp g) (approx-alike ($listarray f) ($listarray g))))
314 ((hash-table-p f)
315 (and (hash-table-p g) (approx-alike ($listarray f) ($listarray g))))
317 ((atom f)
318 (and (atom g) (equal f g)))
320 ((op-equalp f 'lambda)
321 (and (op-equalp g 'lambda)
322 (approx-alike-list (mapcar #'(lambda (s) (simplifya s nil)) (margs f))
323 (mapcar #'(lambda (s) (simplifya s nil)) (margs g)))))
325 (($ratp f)
326 (and ($ratp g) (approx-alike (ratdisrep f) (ratdisrep g))))
328 ;; maybe we don't want this.
329 ((op-equalp f 'mquote)
330 (approx-alike (second f) g))
332 ;; I'm pretty sure that (mop f) and (mop g) won't signal errors, but
333 ;; let's be extra careful.
335 ((and (consp f) (consp (car f)) (consp g) (consp (car g))
336 (or (approx-alike (mop f) (mop g))
337 (and (symbolp (mop f)) (symbolp (mop g))
338 (approx-alike ($nounify (mop f)) ($nounify (mop g)))))
339 (eq ($subvarp f) ($subvarp g))
340 (approx-alike-list (margs f) (margs g))))
342 (t nil)))
344 (defun approx-alike-list (p q)
345 (cond ((null p) (null q))
346 ((null q) (null p))
347 (t (and (approx-alike (first p) (first q)) (approx-alike-list (rest p) (rest q))))))
349 (defun simple-equal-p (f g)
350 (approx-alike (simplifya f nil) (simplifya g nil)))
352 (defun batch-equal-check (expected result)
353 (let ((answer (catch 'macsyma-quit (simple-equal-p expected result))))
354 (if (eql answer 'maxima-error) nil answer)))
356 (defvar *collect-errors* t)
358 ;; Execute the code in FILENAME as a batch file. If EXPECTED-ERRORS
359 ;; is non-NIL, it is a list of numbers denoting which tests in this
360 ;; file are expected to fail. OUT specifies the stream where any
361 ;; output goes (defaulting to *standard-output*). SHOW-EXPECTED is
362 ;; non-NIL if the expected results should also be printed. SHOW-ALL
363 ;; is non-NIL if all tests (including expected failures) should be
364 ;; shown. Finally, SHOWTIME is non-NIL if the execution time should
365 ;; be displayed.
367 ;; This function returns four values:
368 ;; 1. the filename
369 ;; 2. NIL or a Maxima list of test numbers that failed
370 ;; 3. NIL or a Maxima list of test numbers that were expected to
371 ;; fail but actually passed.
372 ;; 4. Total number of tests in the file
373 (defun test-batch (filename expected-errors
374 &key (out *standard-output*) (show-expected nil)
375 (show-all nil) (showtime nil))
377 (let (result
378 next-result
379 next
380 error-log
381 all-differences
382 unexpected-pass
383 strm
384 expr
385 problem-lineinfo
386 problem-lineno
387 tmp-output
388 save-output
389 ($ratprint nil)
390 (*mread-prompt* "")
391 (*read-base* 10.)
392 (num-problems 0)
393 (i 0)
394 (start-run-time 0)
395 (end-run-time 0)
396 (start-real-time 0)
397 (end-real-time 0)
398 (test-start-run-time 0)
399 (test-end-run-time 0)
400 (test-start-real-time 0)
401 (test-end-real-time 0))
403 (cond (*collect-errors*
404 (setq error-log
405 (if (streamp *collect-errors*) *collect-errors*
406 (handler-case
407 (open (alter-pathname filename :type "ERR") :direction :output :if-exists :supersede)
408 #-gcl (file-error () nil)
409 #+gcl (cl::error () nil))))
410 (when error-log
411 (format t (intl:gettext "~%batch: write error log to ~a") error-log)
412 (format error-log (intl:gettext "~%/* Maxima error log from tests in ~A") filename)
413 (format error-log " */~2%"))))
415 (unwind-protect
416 (progn
417 (setq strm (open filename :direction :input))
418 (setq start-real-time (get-internal-real-time))
419 (setq start-run-time (get-internal-run-time))
420 (while (not (eq 'eof (setq expr (mread strm 'eof))))
421 (incf num-problems)
422 (setq problem-lineinfo (second (first expr)))
423 (setq problem-lineno (if (and (consp problem-lineinfo) (integerp (first problem-lineinfo)))
424 (1+ (first problem-lineinfo))))
425 (incf i)
426 (setf tmp-output (make-string-output-stream))
427 (setf save-output *standard-output*)
428 (setf *standard-output* tmp-output)
430 (unwind-protect
431 (progn
432 (setq test-start-run-time (get-internal-run-time))
433 (setq test-start-real-time (get-internal-real-time))
434 (let (($errormsg t))
435 (setq result (meval* `(($errcatch) ,(third expr)))))
436 (setq result (if ($emptyp result) 'error-catch (second result)))
437 (setq test-end-run-time (get-internal-run-time))
438 (setq test-end-real-time (get-internal-real-time))
439 (setq $% result))
440 (setf *standard-output* save-output))
442 (setq next (mread strm 'eof))
443 (if (eq next 'eof) (merror (intl:gettext "batch: missing expected result in test script.")))
445 (setq next-result (third next))
446 (let* ((correct (batch-equal-check next-result result))
447 (expected-error (member i expected-errors))
448 (pass (or correct expected-error)))
449 (when (or show-all (not pass) (and correct expected-error)
450 (and expected-error show-expected))
451 (format out (intl:gettext "~%********************** Problem ~A~A***************")
452 i (if problem-lineno (format nil " (line ~S) " problem-lineno) " "))
453 (format out (intl:gettext "~%Input:~%"))
454 (displa (third expr))
455 (format out (intl:gettext "~%~%Result:~%"))
456 (format out "~a" (get-output-stream-string tmp-output))
457 (displa $%)
458 (when (eq showtime '$all)
459 (format out (intl:gettext "~%Time: ~,3F sec (~,3F elapsed)")
460 (float (/ (- test-end-run-time test-start-run-time)
461 internal-time-units-per-second))
462 (float (/ (- test-end-real-time test-start-real-time)
463 internal-time-units-per-second)))))
464 (cond ((and correct expected-error)
465 (push i unexpected-pass)
466 (format t
467 (intl:gettext "~%... Which was correct, but was expected ~
468 to be wrong due to a known bug in~% Maxima or ~A.~%")
469 (lisp-implementation-type)))
470 (correct
471 (if show-all (format t (intl:gettext "~%... Which was correct.~%"))))
472 ((and (not correct) expected-error)
473 (when (or show-all show-expected)
474 (format t
475 (intl:gettext "~%This is a known error in Maxima or in ~A. ~
476 The correct result is:~%")
477 (lisp-implementation-type))
478 (displa next-result)))
479 (t (format t (intl:gettext "~%This differed from the expected result:~%"))
480 (push i all-differences)
481 (displa next-result)
482 (cond ((and *collect-errors* error-log)
483 (format error-log (intl:gettext "/* Problem ~A~A*/~%")
484 i (if problem-lineno (format nil " (line ~S) " problem-lineno) " "))
485 (mgrind (third expr) error-log)
486 (list-variable-bindings (third expr) error-log)
487 (format error-log ";~%")
488 (format error-log (intl:gettext "/* Erroneous Result?:~%"))
489 (mgrind result error-log) (format error-log " */ ")
490 (terpri error-log)
491 (format error-log (intl:gettext "/* Expected result: */~%"))
492 (mgrind next-result error-log)
493 (format error-log ";~%~%"))))))))
494 (close strm))
495 (setq end-run-time (get-internal-run-time))
496 (setq end-real-time (get-internal-real-time))
497 (cond (error-log
498 (or (streamp *collect-errors*)
499 (close error-log))))
500 (let*
501 ((n-expected-errors (length expected-errors))
502 (expected-errors-trailer
503 (if (= n-expected-errors 0)
505 (format nil (intl:gettext " (not counting ~a expected errors)") n-expected-errors)))
506 (time (if showtime
507 (format nil (intl:gettext " using ~,3F seconds (~,3F elapsed).~%")
508 (float (/ (- end-run-time start-run-time) internal-time-units-per-second))
509 (float (/ (- end-real-time start-real-time) internal-time-units-per-second)))
510 "")))
511 (cond ((null all-differences)
512 (format t (intl:gettext "~a/~a tests passed~a~%~A")
513 (- num-problems n-expected-errors) (- num-problems n-expected-errors)
514 expected-errors-trailer
515 time)
516 (when unexpected-pass
517 (multiple-value-bind (plural was-were)
518 (if (> (length unexpected-pass) 1)
519 (values "s" "were")
520 (values "" "was"))
521 (format t (intl:gettext "~%The following ~A problem~A passed but ~A expected to fail: ~A~%")
522 (length unexpected-pass) plural was-were (reverse unexpected-pass))))
523 (values filename
525 `((mlist) ,@(reverse unexpected-pass))
526 num-problems))
528 (format t (intl:gettext "~%~a/~a tests passed~a~%~A")
529 (- num-problems n-expected-errors (length all-differences)) (- num-problems n-expected-errors) expected-errors-trailer
530 time)
531 (let ((s (if (> (length all-differences) 1) "s" "")))
532 (format t (intl:gettext "~%The following ~A problem~A failed: ~A~%")
533 (length all-differences) s (reverse all-differences)))
534 (when unexpected-pass
535 (multiple-value-bind (plural was-were)
536 (if (> (length unexpected-pass) 1)
537 (values "s" "were")
538 (values "" "was"))
539 (format t (intl:gettext "~%The following ~A problem~A passed but ~A expected to fail: ~A~%")
540 (length unexpected-pass) plural was-were (reverse unexpected-pass))))
541 (values filename
542 `((mlist) ,@(reverse all-differences))
543 `((mlist) ,@(reverse unexpected-pass))
544 num-problems))))))
546 ;;to keep track of global values during the error:
547 (defun list-variable-bindings (expr &optional str &aux tem)
548 (loop for v in(cdr ($listofvars expr))
549 when (member v $values :test #'equal)
550 collecting (setq tem`((mequal) ,v ,(meval* v)))
552 do (cond (str (format str ",")(mgrind tem str)))))
554 ;;in init_max
555 ;; name = foo or foo.type or dir/foo.type or dir/foo
556 ;; the empty parts are filled successively from defaults in templates in
557 ;; the path. A template may use multiple {a,b,c} constructions to indicate
558 ;; multiple possibilities. eg foo.l{i,}sp or foo.{dem,dm1,dm2}
559 (defmfun $file_search (name &optional paths)
560 (if (and (symbolp name)
561 (char= (char (symbol-name name) 0) #\$))
562 (setq name (subseq (print-invert-case name) 1)))
563 (if (symbolp name) (setf name (string name)))
564 (if (file-exists-p name) (return-from $file_search name))
565 (or paths (setq paths ($append $file_search_lisp $file_search_maxima
566 $file_search_demo)))
567 (atomchk paths '$file_search t)
568 (new-file-search (string name) (cdr paths)))
570 ;; Returns T if NAME exists and it does not appear to be a directory.
571 ;; Note that Clisp throws an error from PROBE-FILE if NAME exists
572 ;; and is a directory; hence the use of IGNORE-ERRORS.
574 (defun file-exists-p (name)
575 (let ((foo (ignore-errors (probe-file name))))
576 (if foo (not (apparently-a-directory-p foo)))))
578 (defun apparently-a-directory-p (path)
579 (eq (pathname-name path) nil))
581 (defun new-file-search (name template)
582 (cond ((file-exists-p name))
583 ((atom template)
584 (let ((lis (loop for w in (split-string template "{}")
585 when (null (position #\, w))
586 collect w
587 else
588 collect (split-string w ","))))
589 (new-file-search1 name "" lis)))
591 (let ((temp nil))
592 (loop for v in template
593 when (setq temp (new-file-search name v))
594 do (return temp))))))
596 (defun new-file-search1 (name begin lis)
597 (cond ((null lis)
598 (let ((file (namestring ($filename_merge begin name))))
599 (if (file-exists-p file) file nil)))
600 ((atom (car lis))
601 (new-file-search1 name
602 (if begin
603 ($sconcat begin (car lis)) (car lis))
604 (cdr lis)))
605 (t (loop for v in (car lis) with tem
606 when (setq tem (new-file-search1 name begin (cons v (cdr lis))))
607 do (return tem)))))
609 (defun save-linenumbers (&key (c-lines t) d-lines (from 1) (below $linenum) a-list
610 (file "/tmp/lines")
611 &aux input-symbol (linel 79))
612 (cond ((null a-list) (setq a-list (loop for i from from below below collecting i))))
613 (with-open-file (st file :direction :output)
614 (format st "/* -*- Mode: MACSYMA; Package: MACSYMA -*- */")
615 (format st "~%~% /* ~A */ ~%"
616 (let ((tem (cdddr
617 (multiple-value-list (get-decoded-time)))))
618 (format nil "~a:~a:~a" (car tem) (cadr tem) (caadr tem))))
619 (loop for i in a-list
620 when (and c-lines (boundp (setq input-symbol (intern (format nil "$~A~A" '#:c i)))))
622 (format st "~% C~3A; " i)
623 (mgrind (symbol-value input-symbol) st)
624 (format st ";")
625 when (and d-lines
626 (boundp (setq input-symbol (intern (format nil "$~A~A" '#:d i)))))
628 (format st "~% D~3A: " i)
629 (mgrind (symbol-value input-symbol) st)
630 (format st "$"))))
633 (defmfun $printfile (file)
634 (setq file ($file_search1 file '((mlist) $file_search_usage)))
635 (with-open-file (st file)
636 (loop
637 with tem
638 while (setq tem (read-char st nil 'eof))
640 (if (eq tem 'eof) (return t))
641 (princ tem))
642 (namestring file)))
644 (defvar *maxima-testsdir*)
646 (defun intersect-tests (tests)
647 ;; If TESTS is non-NIL, we assume it's a Maxima list of (maxima)
648 ;; strings naming the tests we want to run. They must match the
649 ;; file names in $testsuite_files. We ignore any items that aren't
650 ;; in $testsuite_files.
651 (flet ((remove-dollarsign (x)
652 ;; Like stripdollar, but less heavy
653 (if (symbolp x)
654 (subseq (maxima-string x) 1)
655 x)))
656 (mapcar #'remove-dollarsign
657 (cond (tests
658 (let ((results nil))
659 ;; Using INTERSECTION would be convenient, but
660 ;; INTERSECTION can return the result in any
661 ;; order, and we'd prefer that the order of the
662 ;; tests be preserved. CMUCL and CCL returns the
663 ;; intersection in reverse order. Clisp produces
664 ;; the original order. Fortunately, this doesn't
665 ;; have to be very fast, so we do it very naively.
666 (dolist (test (mapcar #'remove-dollarsign (cdr tests)))
667 (let ((matching-test (find test (cdr $testsuite_files)
668 :key #'(lambda (x)
669 (maxima-string (if (listp x)
670 (second x)
671 x)))
672 :test #'string=)))
673 (when matching-test
674 (push matching-test results))))
675 (nreverse results)))
677 (cdr $testsuite_files))))))
679 (defun print-testsuite-summary (errs unexpected-pass error-count total-count)
680 (flet
681 ((problem-summary (x)
682 ;; We want to print all the elements in the list.
683 (let ((*print-length* nil)
684 (s (if (> (length (rest x)) 1) "s" "")))
685 (format t
686 (intl:gettext
687 " ~a problem~a:~% ~a~%")
688 (first x)
690 (sort (rest x) #'<)))))
691 (if (null errs)
692 (format t
693 (intl:gettext
694 "~%~%No unexpected errors found out of ~:d tests.~%")
695 total-count)
696 (format t (intl:gettext "~%Error summary:~%")))
697 (when errs
698 (format t (intl:gettext "Error(s) found:~%"))
699 (mapcar #'problem-summary (reverse errs)))
700 (when unexpected-pass
701 (format t (intl:gettext "Tests that were expected to fail but passed:~%"))
702 (mapcar #'problem-summary (reverse unexpected-pass)))
703 (when errs
704 (format t
705 (intl:gettext
706 "~&~:d test~p failed out of ~:d total tests.~%")
707 error-count
708 error-count
709 total-count))))
711 (defmfun $run_testsuite (&key tests display_all display_known_bugs share_tests time debug)
712 "Run the testsuite. Options are
713 tests List of tests to run
714 display_all Display output from each test entry
715 display_known_bugs Include tests that are known to fail.
716 time Display time to run each test entry
717 share_tests Whether to include the share testsuite or not
718 debug Set to enable some debugging prints.
720 (declare (special $file_search_tests))
721 (enable-some-lisp-warnings)
722 (let ((test-file)
723 (expected-failures)
724 (test-file-path))
725 (format t "Testsuite run for ~a ~a:~%"
726 (lisp-implementation-type) (lisp-implementation-version))
727 ;; Allow only T and NIL for display_known_bugs and display_all
728 (unless (member display_known_bugs '(t nil))
729 (merror (intl:gettext "run_testsuite: display_known_bugs must be true or false; found: ~M") display_known_bugs))
730 (unless (member display_all '(t nil))
731 (merror (intl:gettext "run_testsuite: display_all must be true or false; found: ~M") display_all))
732 (unless (member time '(t nil $all))
733 (merror (intl:gettext "run_testsuite: time must be true, false, or all; found: ~M") time))
735 (unless (member share_tests '(t nil $only))
736 (merror (intl:gettext "run_testsuite: share_tests must be true, false or only: found ~M") share_tests))
738 (setq *collect-errors* nil)
740 (multiple-value-bind (desired-tests desired-search-path)
741 (ecase share_tests
742 ((nil)
743 ;; Do nothing
744 (values $testsuite_files $file_search_tests))
745 ((t)
746 ;; Append the share files and concatenate the search paths
747 ;; for tests and maxima so we can find both sets of tests.
748 (values ($append $testsuite_files $share_testsuite_files)
749 ;; Is there a better way to do this?
750 (concatenate 'list
751 '((mlist))
752 (rest $file_search_tests)
753 (rest $file_search_maxima))))
754 ($only
755 ;; Only the share test files
756 (values $share_testsuite_files $file_search_maxima)))
757 (let* (($testsuite_files desired-tests)
758 ($file_search_tests desired-search-path)
759 (error-break-file)
760 (tests-to-run (intersect-tests (cond ((consp tests) tests)
761 (tests (list '(mlist) tests)))))
762 (test-count 0)
763 (total-count 0)
764 (error-count 0)
765 filename
766 diff
767 upass)
768 (when debug
769 (let (($stringdisp t))
770 (mformat t "$testsuite_files = ~M~%" $testsuite_files)
771 (mformat t "$file_search_tests = ~M~%" $file_search_tests)))
772 (when debug
773 (let (($stringdisp t))
774 (mformat t "tests-to-run = ~M~%" tests-to-run)))
775 (flet
776 ((testsuite ()
777 (loop with errs = 'nil
778 with unexpected-pass = nil
779 for testentry in tests-to-run
780 do (if (atom testentry)
781 (progn
782 (setf test-file testentry)
783 (setf expected-failures nil))
784 (progn
785 (setf test-file (second testentry))
786 (setf expected-failures
787 ;; Support the expected failures list in
788 ;; two formats:
790 ;; ((mlist) "test" 1 2 3)
791 ;; ((mlist) "test" ((mlist) 1 2 3))
793 ;; The first is the old style whereas the
794 ;; second is the new style. We support
795 ;; the old style for backward
796 ;; compatibility.
797 (if (consp (caddr testentry))
798 (cdaddr testentry)
799 (cddr testentry)))))
800 (setf test-file-path ($file_search test-file $file_search_tests))
801 (format t
802 (intl:gettext "Running tests in ~a: ")
803 (if (symbolp test-file)
804 (subseq (print-invert-case test-file) 1)
805 test-file))
806 (when debug
807 (format t (intl:gettext "(~A) ") test-file-path))
809 (errset
810 (progn
811 (multiple-value-setq (filename diff upass test-count)
812 (test-batch test-file-path
813 expected-failures :show-expected display_known_bugs
814 :show-all display_all :showtime time))
815 (incf total-count test-count)
816 (when (or (rest diff) (rest upass))
817 (incf error-count (length (rest diff)))
818 (when (rest diff)
819 (push (list* filename (rest diff))
820 errs))
821 (when (rest upass)
822 (push (list* filename (rest upass))
823 unexpected-pass)))))
824 (progn
825 (setq error-break-file (format nil "~a" test-file))
826 (push (list error-break-file "error break")
827 errs)
828 (format t
829 (intl:gettext "~%Caused an error break: ~a")
830 test-file)
831 ;; If the test failed because we
832 ;; couldn't find the file, make a note of
833 ;; that.
834 (unless test-file-path
835 (format t (intl:gettext ": test file not found.")))
836 (format t "~%")))
837 finally
838 (print-testsuite-summary errs unexpected-pass error-count total-count))))
839 (time (testsuite))))))
840 (disable-some-lisp-warnings)
841 '$done)