Rename *ll* and *ul* to ll and ul in method-radical-poly
[maxima.git] / src / mload.lisp
blob43bfb13b1ab55f4e75316f51eab32ef0025a5260
1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancements. ;;;;;
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 (defun load-and-tell (filename)
16 (loadfile filename t ;; means this is a lisp-level call, not user-level.
17 $loadprint))
19 (defun errset-namestring (x)
20 (let ((errset nil))
21 (errset (pathname x))))
23 (defmfun $filename_merge (&rest file-specs)
24 (when (or (null file-specs) (cddr file-specs))
25 (wna-err '$filename_merge))
26 (setq file-specs (mapcar #'macsyma-namestring-sub file-specs))
27 (pathname (if (null (cdr file-specs))
28 (car file-specs)
29 (merge-pathnames (cadr file-specs) (car file-specs)))))
31 (defun macsyma-namestring-sub (user-object)
32 (if (pathnamep user-object) user-object
33 (let* ((system-object
34 (cond ((and (atom user-object) (not (symbolp user-object)))
35 user-object)
36 ((atom user-object) ;hence a symbol in view of the
37 (print-invert-case (fullstrip1 user-object))) ; first clause
38 (($listp user-object)
39 (fullstrip (cdr user-object)))
41 (merror (intl:gettext "filename_merge: unexpected argument: ~M") user-object))))
42 (namestring-try (errset-namestring system-object)))
43 (if namestring-try (car namestring-try)
44 ;; know its small now, so print on same line.
45 (merror (intl:gettext "filename_merge: unexpected argument: ~:M") user-object)))))
47 ;; Returns the truename corresponding to a stream, or nil (for non-file streams).
48 ;; Previously we used (subtypep (type-of stream) 'file-stream) to determine whether
49 ;; a stream is a file stream, but this doesn't work on GCL.
50 (defun get-stream-truename (stream)
51 (handler-case
52 (probe-file stream)
53 (error () nil)))
55 (defmfun $batchload (filename-or-stream &aux (*mread-prompt* ""))
56 (declare (special *mread-prompt*))
57 (if (streamp filename-or-stream)
58 (batchload-stream filename-or-stream)
59 (let
60 ((filename ($file_search1 filename-or-stream '((mlist) $file_search_maxima))))
61 (with-open-file (in-stream filename)
62 (batchload-stream in-stream)))))
64 (defun batchload-stream (in-stream &key autoloading-p)
65 (let ($load_pathname)
66 (let*
67 ((noevalargs nil)
68 (*read-base* 10.)
69 (stream-truename (get-stream-truename in-stream))
70 (in-stream-string-rep
71 (if stream-truename
72 (setq $load_pathname (cl:namestring stream-truename))
73 (format nil "~A" in-stream)))
74 ;; If we arrived here from autoloading, call MEVAL instead of MEVAL*
75 ;; since MEVAL* is intended to be called from the interpreter top level;
76 ;; MEVAL* modifies global state, resetting VARLIST and calling CLEARSIGN.
77 (meval-fcn (symbol-function (if autoloading-p 'meval 'meval*)))
78 (expr nil))
79 (declare (special *prompt-on-read-hang*))
80 (when $loadprint
81 (format t (intl:gettext "~&read and interpret ~A~&") in-stream-string-rep))
82 (cleanup)
83 (newline in-stream)
84 (loop while (and
85 (setq expr (let (*prompt-on-read-hang*) (mread in-stream nil)))
86 (consp expr))
87 do (funcall meval-fcn (third expr)))
88 in-stream-string-rep)))
90 ;;returns appropriate error or existing pathname.
91 ;; the second argument is a maxima list of variables
92 ;; each of which contains a list of paths. This is
93 ;; so users can correct the variable..
94 (defmfun $file_search1 (name search-lists &aux lis)
95 (if (pathnamep name)
96 (setq name (namestring name)))
97 (setq lis (apply '$append (mapcar 'symbol-value (cdr search-lists))))
98 (let ((res ($file_search name lis)))
99 (or res
100 (merror (intl:gettext "file_search1: ~M not found in ~A.")
101 name
102 (string-trim "[]" ($sconcat search-lists))))))
104 (defmfun $load (filename)
105 "This is the generic file loading function.
106 LOAD(filename) will either BATCHLOAD or LOADFILE the file,
107 depending on whether the file contains Macsyma, Lisp, or Compiled
108 code. The file specifications default such that a compiled file
109 is searched for first, then a lisp file, and finally a macsyma batch
110 file. This command is designed to provide maximum utility and
111 convenience for writers of packages and users of the macsyma->lisp
112 translator."
114 (if (or (stringp filename) (symbolp filename) (pathnamep filename))
115 (let ((searched-for
116 ($file_search1 filename
117 '((mlist) $file_search_maxima $file_search_lisp )))
118 type)
119 (setq type ($file_type searched-for))
120 (case type
121 (($maxima)
122 ($batchload searched-for))
123 (($lisp $object)
124 ;; do something about handling errors
125 ;; during loading. Foobar fail act errors.
126 (load-and-tell searched-for))
128 ;; UNREACHABLE MESSAGE: DEFAULT TYPE IS '$OBJECT (SEE $FILE_TYPE BELOW)
129 (merror "Maxima bug: Unknown file type ~M" type)))
130 searched-for)
131 (merror "load: argument must be a string, symbol, or pathname; found: ~M" filename)))
133 (defmvar $file_type_lisp
134 (list '(mlist) "l" "lsp" "lisp"))
136 (defmvar $file_type_maxima
137 (list '(mlist) "mac" "mc" "demo" "dem" "dm1" "dm2" "dm3" "dmt" "wxm"))
139 (defmfun $file_type (fil)
140 (let ((typ ($pathname_type fil)))
141 (cond
142 ((member typ (cdr $file_type_lisp) :test #'string=)
143 '$lisp)
144 ((member typ (cdr $file_type_maxima) :test #'string=)
145 '$maxima)
147 '$object))))
149 (defmfun $pathname_directory (path)
150 (let ((pathname (pathname path)))
151 (namestring (make-pathname :directory (pathname-directory pathname)))))
153 (defmfun $pathname_name (path)
154 (let ((pathname (pathname path)))
155 (pathname-name pathname)))
157 (defmfun $pathname_type (path)
158 (let ((pathname (pathname path)))
159 (pathname-type pathname)))
162 (defvar *macsyma-startup-queue* nil)
164 (declaim (special *mread-prompt*))
166 ;;;; batch & demo search hacks
168 (defmfun $batch (filename-or-stream &optional (demo :batch)
169 &aux tem (possible '(:demo :batch :test)))
170 "giving a second argument makes it use demo mode, ie pause after evaluation
171 of each command line"
173 ;; Try to get rid of testsuite failures on machines that are low on RAM.
174 ($garbage_collect)
175 (cond
176 ((setq tem (member ($mkey demo) possible :test #'eq))
177 (setq demo (car tem)))
178 (t (format t (intl:gettext "batch: second argument must be 'demo', 'batch' or 'test'; found: ~A, assumed 'batch'~%") demo)))
179 (if (streamp filename-or-stream)
180 (batch-stream filename-or-stream demo)
181 (let
182 ((filename ($file_search1 filename-or-stream
183 (if (eql demo :demo)
184 '((mlist) $file_search_demo )
185 '((mlist) $file_search_maxima)))))
186 (cond
187 ((eq demo :test)
188 (test-batch filename nil :show-all t)) ;; NEED TO ACCEPT INPUT STREAM HERE TOO
190 (with-open-file (in-stream filename)
191 (batch-stream in-stream demo)))))))
193 (defun batch-stream (in-stream demo)
194 (let ($load_pathname)
195 (let*
196 ((*read-base* 10.)
197 (stream-truename (get-stream-truename in-stream))
198 (in-stream-string-rep
199 (if stream-truename
200 (setq $load_pathname (cl:namestring stream-truename))
201 (format nil "~A" in-stream))))
202 (format t (intl:gettext "~%read and interpret ~A~%") in-stream-string-rep)
203 (catch 'macsyma-quit (continue :stream in-stream :batch-or-demo-flag demo))
204 (incf $linenum)
205 in-stream-string-rep)))
207 ;; When either a or b are special float values (NaN or +/-Inf), return true iff
208 ;; a and b are the both NaN or both +Inf or both -Inf.
209 ;; Note that float_approx_equal(NaN, NaN) returns true.
211 ;; When a and b to finite, nonzero floats, return true iff
213 ;; |a - b| <= float_approx_equal_tolerance * min(2^n, 2^m)
215 ;; where a = af * 2^m, |af| < 1, and m is an integer (similarly for b).
216 ;; See Knuth, "The Art of Computer Programming" (3rd ed.), Vol. 2, Sec. 4.2.2, Eq. 24, page 233.
218 ;; Note that Eq. 24 isn't well-defined for a or b equal to zero.
219 ;; To make progress, let's consider the limit as b --> 0,
220 ;; therefore n --> -inf. For any finite a, that means that min(2^n, 2^m) = 2^n
221 ;; and therefore |a - b| --> |a|, but to pass the test,
222 ;; |a| must be less than or equal to float_approx_equal_tolerance * 2^n --> 0.
223 ;; Therefore the test fails for all nonzero, finite a, when b = 0.
224 ;; i.e. when either a or b is zero but not both, return false.
226 ;; Note also that if a < 0 and b > 0 or vice versa, the test must fail:
227 ;; without loss of generality assume |a| > |b|. Then n <= m and min(2^n, 2^m) = 2^n.
228 ;; Now |a - b| = |a| + |b| since a and b are different signs.
229 ;; 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.
230 ;; So unless float_approx_equal_tolerance is unusually large, the test must fail.
232 ;; Otherwise, either a or b is not a float, so return false.
234 (defmvar $float_approx_equal_tolerance (* 8 +flonum-epsilon+))
236 (defmfun $float_approx_equal (a b)
237 (setq a (if (floatp a) a ($float a)))
238 (setq b (if (floatp b) b ($float b)))
239 (and
240 (floatp a)
241 (floatp b)
242 (cond
243 ;; look for NaN
244 ((/= a a) (/= b b))
245 ((/= b b) nil)
246 ;; look for Inf
247 ((> (abs a) most-positive-double-float) (= a b))
248 ((> (abs b) most-positive-double-float) nil)
249 ;; look for zero
250 ((= a 0d0) (= b 0d0))
251 ((= b 0d0) nil)
252 ;; look for A = B
253 ((= a b))
255 ;; Implement test without involving floating-point arithmetic,
256 ;; to avoid errors which could occur with extreme values.
257 (multiple-value-bind (a-significand a-expt a-sign)
258 (decode-float a)
259 (declare (ignore a-significand))
260 (multiple-value-bind (b-significand b-expt b-sign)
261 (decode-float b)
262 (declare (ignore b-significand))
263 (when (or (= a-sign b-sign)
264 (>= $float_approx_equal_tolerance 1d0))
265 (multiple-value-bind (a-b-significand a-b-expt)
266 (integer-decode-float (abs (- a b)))
267 (multiple-value-bind (tol-significand tol-expt)
268 (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)
313 (equal (array-dimensions f) (array-dimensions g))
314 (approx-alike ($listarray f) ($listarray g))))
316 ((hash-table-p f)
317 (and (hash-table-p g) (approx-alike ($listarray f) ($listarray g))))
319 ((atom f)
320 (and (atom g) (equal f g)))
322 ((op-equalp f 'lambda)
323 (and (op-equalp g 'lambda)
324 (approx-alike-list (mapcar #'(lambda (s) (simplifya s nil)) (margs f))
325 (mapcar #'(lambda (s) (simplifya s nil)) (margs g)))))
327 (($ratp f)
328 (and ($ratp g) (approx-alike (ratdisrep f) (ratdisrep g))))
330 ;; maybe we don't want this.
331 ((op-equalp f 'mquote)
332 (approx-alike (second f) g))
334 ;; I'm pretty sure that (mop f) and (mop g) won't signal errors, but
335 ;; let's be extra careful.
337 ((and (consp f) (consp (car f)) (consp g) (consp (car g))
338 (or (approx-alike (mop f) (mop g))
339 (and (symbolp (mop f)) (symbolp (mop g))
340 (approx-alike ($nounify (mop f)) ($nounify (mop g)))))
341 (eq ($subvarp f) ($subvarp g))
342 (approx-alike-list (margs f) (margs g))))
344 (t nil)))
346 (defun approx-alike-list (p q)
347 (cond ((null p) (null q))
348 ((null q) (null p))
349 (t (and (approx-alike (first p) (first q)) (approx-alike-list (rest p) (rest q))))))
351 (defun simple-equal-p (f g)
352 (approx-alike (simplifya f nil) (simplifya g nil)))
354 (defun batch-equal-check (expected result)
355 (let ((answer (catch 'macsyma-quit (simple-equal-p expected result))))
356 (if (eql answer 'maxima-error) nil answer)))
358 (defvar *collect-errors* t)
360 ;; Execute the code in FILENAME as a batch file. If EXPECTED-ERRORS
361 ;; is non-NIL, it is a list of numbers denoting which tests in this
362 ;; file are expected to fail. OUT specifies the stream where any
363 ;; output goes (defaulting to *standard-output*). SHOW-EXPECTED is
364 ;; non-NIL if the expected results should also be printed. SHOW-ALL
365 ;; is non-NIL if all tests (including expected failures) should be
366 ;; shown. Finally, SHOWTIME is non-NIL if the execution time should
367 ;; be displayed.
369 ;; This function returns four values:
370 ;; 1. the filename
371 ;; 2. NIL or a Maxima list of test numbers that failed
372 ;; 3. NIL or a Maxima list of test numbers that were expected to
373 ;; fail but actually passed.
374 ;; 4. Total number of tests in the file
375 (defun test-batch (filename expected-errors
376 &key (out *standard-output*) (show-expected nil)
377 (show-all nil) (showtime nil))
379 (let (result
380 next-result
381 next
382 error-log
383 all-differences
384 unexpected-pass
385 strm
386 expr
387 problem-lineinfo
388 problem-lineno
389 tmp-output
390 save-output
391 ($ratprint nil)
392 (*mread-prompt* "")
393 (*read-base* 10.)
394 (num-problems 0)
395 (i 0)
396 (start-run-time 0)
397 (end-run-time 0)
398 (start-real-time 0)
399 (end-real-time 0)
400 (test-start-run-time 0)
401 (test-end-run-time 0)
402 (test-start-real-time 0)
403 (test-end-real-time 0))
405 (cond (*collect-errors*
406 (setq error-log
407 (if (streamp *collect-errors*) *collect-errors*
408 (handler-case
409 (open (alter-pathname filename :type "ERR") :direction :output :if-exists :supersede)
410 (file-error () nil))))
411 (when error-log
412 (format t (intl:gettext "~%batch: write error log to ~a") error-log)
413 (format error-log (intl:gettext "~%/* Maxima error log from tests in ~A") filename)
414 (format error-log " */~2%"))))
416 (unwind-protect
417 (progn
418 (setq strm (open filename :direction :input))
419 (setq start-real-time (get-internal-real-time))
420 (setq start-run-time (get-internal-run-time))
421 (while (not (eq 'eof (setq expr (mread strm 'eof))))
422 (incf num-problems)
423 (setq problem-lineinfo (second (first expr)))
424 (setq problem-lineno (if (and (consp problem-lineinfo) (integerp (first problem-lineinfo)))
425 (1+ (first problem-lineinfo))))
426 (incf i)
427 (setf tmp-output (make-string-output-stream))
428 (setf save-output *standard-output*)
429 (setf *standard-output* tmp-output)
431 (unwind-protect
432 (progn
433 (setq test-start-run-time (get-internal-run-time))
434 (setq test-start-real-time (get-internal-real-time))
435 (let (($errormsg t))
436 (setq result (meval* `(($errcatch) ,(third expr)))))
437 (setq result (if ($emptyp result) 'error-catch (second result)))
438 (setq test-end-run-time (get-internal-run-time))
439 (setq test-end-real-time (get-internal-real-time))
440 (setq $% result))
441 (setf *standard-output* save-output))
443 (setq next (mread strm 'eof))
444 (if (eq next 'eof) (merror (intl:gettext "batch: missing expected result in test script.")))
446 (setq next-result (third next))
447 (let* ((correct (batch-equal-check next-result result))
448 (expected-error (member i expected-errors))
449 (pass (or correct expected-error)))
450 (when (or show-all (not pass) (and correct expected-error)
451 (and expected-error show-expected))
452 (format out (intl:gettext "~%********************** Problem ~A~A***************")
453 i (if problem-lineno (format nil " (line ~S) " problem-lineno) " "))
454 (format out (intl:gettext "~%Input:~%"))
455 (displa (third expr))
456 (format out (intl:gettext "~%~%Result:~%"))
457 (format out "~a" (get-output-stream-string tmp-output))
458 (displa $%)
459 (when (eq showtime '$all)
460 (format out (intl:gettext "~%Time: ~,3F sec (~,3F elapsed)")
461 (float (/ (- test-end-run-time test-start-run-time)
462 internal-time-units-per-second))
463 (float (/ (- test-end-real-time test-start-real-time)
464 internal-time-units-per-second)))))
465 (cond ((and correct expected-error)
466 (push i unexpected-pass)
467 (format t
468 (intl:gettext "~%... Which was correct, but was expected ~
469 to be wrong due to a known bug in~% Maxima or ~A.~%")
470 (lisp-implementation-type)))
471 (correct
472 (if show-all (format t (intl:gettext "~%... Which was correct.~%"))))
473 ((and (not correct) expected-error)
474 (when (or show-all show-expected)
475 (format t
476 (intl:gettext "~%This is a known error in Maxima or in ~A. ~
477 The correct result is:~%")
478 (lisp-implementation-type))
479 (displa next-result)))
480 (t (format t (intl:gettext "~%This differed from the expected result:~%"))
481 (push i all-differences)
482 (displa next-result)
483 (cond ((and *collect-errors* error-log)
484 (format error-log (intl:gettext "/* Problem ~A~A*/~%")
485 i (if problem-lineno (format nil " (line ~S) " problem-lineno) " "))
486 (mgrind (third expr) error-log)
487 (list-variable-bindings (third expr) error-log)
488 (format error-log ";~%")
489 (format error-log (intl:gettext "/* Erroneous Result?:~%"))
490 (mgrind result error-log) (format error-log " */ ")
491 (terpri error-log)
492 (format error-log (intl:gettext "/* Expected result: */~%"))
493 (mgrind next-result error-log)
494 (format error-log ";~%~%"))))))))
495 (close strm))
496 (setq end-run-time (get-internal-run-time))
497 (setq end-real-time (get-internal-real-time))
498 (cond (error-log
499 (or (streamp *collect-errors*)
500 (close error-log))))
501 (let*
502 ((n-expected-errors (length expected-errors))
503 (expected-errors-trailer
504 (if (= n-expected-errors 0)
506 (format nil (intl:gettext " (not counting ~a expected errors)") n-expected-errors)))
507 (time (if showtime
508 (format nil (intl:gettext " using ~,3F seconds (~,3F elapsed).~%")
509 (float (/ (- end-run-time start-run-time) internal-time-units-per-second))
510 (float (/ (- end-real-time start-real-time) internal-time-units-per-second)))
511 "")))
512 (cond ((null all-differences)
513 (format t (intl:gettext "~a/~a tests passed~a~%~A")
514 (- num-problems n-expected-errors) (- num-problems n-expected-errors)
515 expected-errors-trailer
516 time)
517 (when unexpected-pass
518 (multiple-value-bind (plural was-were)
519 (if (> (length unexpected-pass) 1)
520 (values "s" "were")
521 (values "" "was"))
522 (format t (intl:gettext "~%The following ~A problem~A passed but ~A expected to fail: ~A~%")
523 (length unexpected-pass) plural was-were (reverse unexpected-pass))))
524 (values filename
526 `((mlist) ,@(reverse unexpected-pass))
527 num-problems))
529 (format t (intl:gettext "~%~a/~a tests passed~a~%~A")
530 (- num-problems n-expected-errors (length all-differences)) (- num-problems n-expected-errors) expected-errors-trailer
531 time)
532 (let ((s (if (> (length all-differences) 1) "s" "")))
533 (format t (intl:gettext "~%The following ~A problem~A failed: ~A~%")
534 (length all-differences) s (reverse all-differences)))
535 (when unexpected-pass
536 (multiple-value-bind (plural was-were)
537 (if (> (length unexpected-pass) 1)
538 (values "s" "were")
539 (values "" "was"))
540 (format t (intl:gettext "~%The following ~A problem~A passed but ~A expected to fail: ~A~%")
541 (length unexpected-pass) plural was-were (reverse unexpected-pass))))
542 (values filename
543 `((mlist) ,@(reverse all-differences))
544 `((mlist) ,@(reverse unexpected-pass))
545 num-problems))))))
547 ;;to keep track of global values during the error:
548 (defun list-variable-bindings (expr &optional str &aux tem)
549 (loop for v in(cdr ($listofvars expr))
550 when (member v $values :test #'equal)
551 collecting (setq tem`((mequal) ,v ,(meval* v)))
553 do (cond (str (format str ",")(mgrind tem str)))))
555 ;;in init_max
556 ;; name = foo or foo.type or dir/foo.type or dir/foo
557 ;; the empty parts are filled successively from defaults in templates in
558 ;; the path. A template may use multiple {a,b,c} constructions to indicate
559 ;; multiple possibilities. eg foo.l{i,}sp or foo.{dem,dm1,dm2}
560 (defmfun $file_search (name &optional paths)
561 (if (and (symbolp name)
562 (char= (char (symbol-name name) 0) #\$))
563 (setq name (subseq (print-invert-case name) 1)))
564 (if (symbolp name) (setf name (string name)))
565 (if (file-exists-p name) (return-from $file_search name))
566 (or paths (setq paths ($append $file_search_lisp $file_search_maxima
567 $file_search_demo)))
568 (atomchk paths '$file_search t)
569 (new-file-search (string name) (cdr paths)))
571 ;; Returns T if NAME exists and it does not appear to be a directory.
572 ;; Note that Clisp throws an error from PROBE-FILE if NAME exists
573 ;; and is a directory; hence the use of IGNORE-ERRORS.
575 (defun file-exists-p (name)
576 (let ((foo (ignore-errors (probe-file name))))
577 (if foo (not (apparently-a-directory-p foo)))))
579 (defun apparently-a-directory-p (path)
580 (eq (pathname-name path) nil))
582 ;; We keep these here in case we want to optimize the search. To
583 ;; speed things up, we might want to support search lists like
584 ;; "share/**/*.{mac,wxm,mc}" so that we only descend the directory
585 ;; once. Then we would take the list of paths and try to match the
586 ;; one with the given extensions.
588 ;; Currently, the search list is ["share/**/*.mac", "share/**/*.wxm",
589 ;; "share/**/*.mc"]. Thus to find "foo.mc", we end up doing a
590 ;; directory 3 times.
591 #+nil
592 (defun new-file-search (name template)
593 (cond ((file-exists-p name))
594 ((atom template)
595 (let ((lis (loop for w in (split-string template "{}")
596 when (null (position #\, w))
597 collect w
598 else
599 collect (split-string w ","))))
600 (new-file-search1 name "" lis)))
602 (let ((temp nil))
603 (loop for v in template
604 when (setq temp (new-file-search name v))
605 do (return temp))))))
607 #+nil
608 (defun new-file-search1 (name begin lis)
609 (cond ((null lis)
610 (let ((file (namestring ($filename_merge begin name))))
611 (if (file-exists-p file) file nil)))
612 ((atom (car lis))
613 (new-file-search1 name
614 (if begin
615 ($sconcat begin (car lis)) (car lis))
616 (cdr lis)))
617 (t (loop for v in (car lis) with tem
618 when (setq tem (new-file-search1 name begin (cons v (cdr lis))))
619 do (return tem)))))
621 (defvar *debug-new-file-search* nil)
623 ;; Search for a file named NAME. If the file exists, return it.
624 ;; Otherwise, TEMPLATE is a list of wildcard paths to be searched for
625 ;; the NAME. Each entry in TEMPLATE should be a Lisp wildcard
626 ;; pathname.
627 (defun new-file-search (name template)
628 (cond ((file-exists-p name))
630 (let ((filename (pathname name)))
631 (dolist (path template)
632 (let ((pathnames (directory (merge-pathnames filename path))))
633 (when *debug-new-file-search*
634 (format *debug-io* "wildpath ~S~%" (merge-pathnames filename path)))
635 (when pathnames
636 ;; We MUST sort the results in alphabetical order
637 ;; because that's how the old search paths were
638 ;; sorted.
639 (setf pathnames (sort pathnames #'string< :key #'namestring))
640 (when *debug-new-file-search*
641 (format *debug-io* "pathname = ~S~%" pathnames))
642 ;; If more than one path is returned, print a warning
643 ;; that we're selecting the first file. Print all
644 ;; the matches too so the user knows.
645 (unless (= 1 (length pathnames))
646 (mwarning
647 (format nil
648 "More than one file matches. Selecting the first file from:~
649 ~%~{ ~A~^~%~}~%"
650 (mapcar #'namestring pathnames))))
651 (return-from new-file-search (namestring (first pathnames))))))))))
653 (defun save-linenumbers (&key (c-lines t) d-lines (from 1) (below $linenum) a-list
654 (file "/tmp/lines")
655 &aux input-symbol ($linel 79))
656 (cond ((null a-list) (setq a-list (loop for i from from below below collecting i))))
657 (with-open-file (st file :direction :output)
658 (format st "/* -*- Mode: MACSYMA; Package: MACSYMA -*- */")
659 (format st "~%~% /* ~A */ ~%"
660 (let ((tem (cdddr
661 (multiple-value-list (get-decoded-time)))))
662 (format nil "~a:~a:~a" (car tem) (cadr tem) (caadr tem))))
663 (loop for i in a-list
664 when (and c-lines (boundp (setq input-symbol (intern (format nil "$~A~A" '#:c i)))))
666 (format st "~% C~3A; " i)
667 (mgrind (symbol-value input-symbol) st)
668 (format st ";")
669 when (and d-lines
670 (boundp (setq input-symbol (intern (format nil "$~A~A" '#:d i)))))
672 (format st "~% D~3A: " i)
673 (mgrind (symbol-value input-symbol) st)
674 (format st "$"))))
677 (defmfun $printfile (file)
678 (setq file ($file_search1 file '((mlist) $file_search_usage)))
679 (with-open-file (st file)
680 (loop
681 with tem
682 while (setq tem (read-char st nil 'eof))
684 (if (eq tem 'eof) (return t))
685 (princ tem))
686 (namestring file)))
688 (defun disable-some-lisp-warnings ()
689 ;; Suppress warnings about redefining functions;
690 ;; it appears that only Clisp and SBCL emit these warnings
691 ;; (ECL, GCL, CMUCL, and Clozure CL apparently do not).
692 ;; Such warnings are generated by the autoload mechanism.
693 ;; I guess it is plausible that we could also avoid the warnings by
694 ;; reworking autoload to not trigger them. I don't have enough
695 ;; motivation to attempt that right now.
696 #+sbcl
697 (setq sb-ext:*muffled-warnings* '(or sb-kernel:redefinition-with-defun sb-kernel:uninteresting-redefinition))
698 #+sbcl
699 (declaim (sb-ext:muffle-conditions sb-ext:compiler-note))
700 #+clisp
701 (setq custom:*suppress-check-redefinition* t)
703 ;; Suppress compiler output messages.
704 ;; These include the "0 errors, 0 warnings" message output from Clisp,
705 ;; and maybe other messages from other Lisps.
706 (setq *compile-verbose* nil))
708 (defun enable-some-lisp-warnings ()
709 ;; SB-KERNEL:UNINTERESTING-REDEFINITION appears to be the default value.
710 #+sbcl
711 (setq sb-ext:*muffled-warnings* 'sb-kernel:uninteresting-redefinition)
712 #+sbcl
713 (declaim (sb-ext:unmuffle-conditions sb-ext:compiler-note))
714 #+clisp
715 (setq custom:*suppress-check-redefinition* nil)
716 (setq *compile-verbose* t))
718 (defun simple-remove-dollarsign (x)
719 "Like stripdollar, but less heavy. Intended for use with the
720 testsuite implementation."
721 (if (symbolp x)
722 (subseq (maxima-string x) 1)
725 (defun intersect-tests (tests)
726 ;; If TESTS is non-NIL, we assume it's a Maxima list of (maxima)
727 ;; strings naming the tests we want to run. They must match the
728 ;; file names in $testsuite_files. We ignore any items that aren't
729 ;; in $testsuite_files.
730 (mapcar #'simple-remove-dollarsign
731 (cond (tests
732 (let ((results nil))
733 ;; Using INTERSECTION would be convenient, but
734 ;; INTERSECTION can return the result in any
735 ;; order, and we'd prefer that the order of the
736 ;; tests be preserved. CMUCL and CCL returns the
737 ;; intersection in reverse order. Clisp produces
738 ;; the original order. Fortunately, this doesn't
739 ;; have to be very fast, so we do it very naively.
740 (dolist (test (mapcar #'simple-remove-dollarsign (cdr tests)))
741 (let ((matching-test (find test (cdr $testsuite_files)
742 :key #'(lambda (x)
743 (maxima-string (if (listp x)
744 (second x)
745 x)))
746 :test #'string=)))
747 (when matching-test
748 (push matching-test results))))
749 (nreverse results)))
751 (cdr $testsuite_files)))))
753 (defun print-testsuite-summary (errs unexpected-pass error-count total-count)
754 (flet
755 ((problem-summary (x)
756 ;; We want to print all the elements in the list.
757 (let ((*print-length* nil)
758 (s (if (> (length (rest x)) 1) "s" "")))
759 (format t
760 (intl:gettext
761 " ~a problem~a:~% ~a~%")
762 (first x)
764 (sort (rest x) #'<)))))
765 (if (null errs)
766 (format t
767 (intl:gettext
768 "~%~%No unexpected errors found out of ~:d tests.~%")
769 total-count)
770 (format t (intl:gettext "~%Error summary:~%")))
771 (when errs
772 (format t (intl:gettext "Error(s) found:~%"))
773 (mapcar #'problem-summary (reverse errs)))
774 (when unexpected-pass
775 (format t (intl:gettext "Tests that were expected to fail but passed:~%"))
776 (mapcar #'problem-summary (reverse unexpected-pass)))
777 (when errs
778 (format t
779 (intl:gettext
780 "~&~:d test~p failed out of ~:d total tests.~%")
781 error-count
782 error-count
783 total-count))))
785 (defun validate-given-tests (tests share-tests-p)
786 ;; Check the test names and print out some warnings if it
787 ;; doesn't exist, or if it does and is part of the share test
788 ;; suite, but share_tests was not set.
789 (dolist (test (mapcar #'simple-remove-dollarsign
790 (if (listp tests)
791 (cdr tests)
792 (list tests))))
793 (cond ((and (not share-tests-p)
794 (find test (cdr $share_testsuite_files)
795 :key #'(lambda (x)
796 (maxima-string (if (listp x)
797 (second x)
798 x)))
799 :test #'string=))
800 (mwarning test "is a share test, but share_tests was not set"))
801 ((not (find test (cdr $testsuite_files)
802 :key #'(lambda (x)
803 (maxima-string (if (listp x)
804 (second x)
805 x)))
806 :test #'string=))
807 (mwarning "Unknown test: " test)))))
809 (defmfun $run_testsuite (&key tests display_all display_known_bugs share_tests time debug)
810 "Run the testsuite. Options are
811 tests List of tests to run
812 display_all Display output from each test entry
813 display_known_bugs Include tests that are known to fail.
814 time Display time to run each test entry
815 share_tests Whether to include the share testsuite or not
816 debug Set to enable some debugging prints.
818 (enable-some-lisp-warnings)
819 (let ((test-file)
820 (expected-failures)
821 (test-file-path))
822 (format t "Testsuite run for ~a ~a:~%"
823 (lisp-implementation-type) (lisp-implementation-version))
824 ;; Allow only T and NIL for display_known_bugs and display_all
825 (unless (member display_known_bugs '(t nil))
826 (merror (intl:gettext "run_testsuite: display_known_bugs must be true or false; found: ~M") display_known_bugs))
827 (unless (member display_all '(t nil))
828 (merror (intl:gettext "run_testsuite: display_all must be true or false; found: ~M") display_all))
829 (unless (member time '(t nil $all))
830 (merror (intl:gettext "run_testsuite: time must be true, false, or all; found: ~M") time))
832 (unless (member share_tests '(t nil $only))
833 (merror (intl:gettext "run_testsuite: share_tests must be true, false or only: found ~M") share_tests))
835 (setq *collect-errors* nil)
837 (multiple-value-bind (desired-tests desired-search-path)
838 (ecase share_tests
839 ((nil)
840 ;; Do nothing
841 (values $testsuite_files $file_search_tests))
842 ((t)
843 ;; Append the share files and concatenate the search paths
844 ;; for tests and maxima so we can find both sets of tests.
845 (values ($append $testsuite_files $share_testsuite_files)
846 ;; Is there a better way to do this?
847 (concatenate 'list
848 '((mlist))
849 (rest $file_search_tests)
850 (rest $file_search_maxima))))
851 ($only
852 ;; Only the share test files
853 (values $share_testsuite_files $file_search_maxima)))
854 (let* (($testsuite_files desired-tests)
855 ($file_search_tests desired-search-path)
856 (error-break-file)
857 (tests-to-run (intersect-tests (cond ((consp tests) tests)
858 (tests (list '(mlist) tests)))))
859 (test-count 0)
860 (total-count 0)
861 (error-count 0)
862 filename
863 diff
864 upass)
866 (validate-given-tests tests share_tests)
868 (when debug
869 (let (($stringdisp t))
870 (mformat t "$testsuite_files = ~M~%" $testsuite_files)
871 (mformat t "$file_search_tests = ~M~%" $file_search_tests)))
872 (when debug
873 (let (($stringdisp t))
874 (mformat t "tests-to-run = ~M~%" tests-to-run)))
876 (unless tests-to-run
877 (mwarning "No tests to run")
878 (return-from $run_testsuite '$done))
880 (flet
881 ((testsuite ()
882 (loop with errs = 'nil
883 with unexpected-pass = nil
884 for testentry in tests-to-run
885 do (if (atom testentry)
886 (progn
887 (setf test-file testentry)
888 (setf expected-failures nil))
889 (progn
890 (setf test-file (second testentry))
891 (setf expected-failures
892 ;; Support the expected failures list in
893 ;; two formats:
895 ;; ((mlist) "test" 1 2 3)
896 ;; ((mlist) "test" ((mlist) 1 2 3))
898 ;; The first is the old style whereas the
899 ;; second is the new style. We support
900 ;; the old style for backward
901 ;; compatibility.
902 (if (consp (caddr testentry))
903 (cdaddr testentry)
904 (cddr testentry)))))
905 (setf test-file-path ($file_search test-file $file_search_tests))
906 (format t
907 (intl:gettext "Running tests in ~a: ")
908 (if (symbolp test-file)
909 (subseq (print-invert-case test-file) 1)
910 test-file))
911 (when debug
912 (format t (intl:gettext "(~A) ") test-file-path))
914 (errset
915 (progn
916 (multiple-value-setq (filename diff upass test-count)
917 (test-batch test-file-path
918 expected-failures :show-expected display_known_bugs
919 :show-all display_all :showtime time))
920 (incf total-count test-count)
921 (when (or (rest diff) (rest upass))
922 (incf error-count (length (rest diff)))
923 (when (rest diff)
924 (push (list* filename (rest diff))
925 errs))
926 (when (rest upass)
927 (push (list* filename (rest upass))
928 unexpected-pass)))))
929 (progn
930 (setq error-break-file (format nil "~a" test-file))
931 (push (list error-break-file "error break")
932 errs)
933 (format t
934 (intl:gettext "~%Caused an error break: ~a")
935 test-file)
936 ;; If the test failed because we
937 ;; couldn't find the file, make a note of
938 ;; that.
939 (unless test-file-path
940 (format t (intl:gettext ": test file not found.")))
941 (format t "~%")))
942 finally
943 (print-testsuite-summary errs unexpected-pass error-count total-count))))
944 (time (testsuite))))))
945 (disable-some-lisp-warnings)
946 '$done)