Add "ru" entry for the hashtable *index-file-name*
[maxima.git] / src / mload.lisp
blobb7b76a5e15cc4a9fd56d3e5982ec39d604aa023e
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"
172 (declare (special $batch_answers_from_file))
174 ;; Try to get rid of testsuite failures on machines that are low on RAM.
175 ($garbage_collect)
176 (cond
177 ((setq tem (member ($mkey demo) possible :test #'eq))
178 (setq demo (car tem)))
179 (t (format t (intl:gettext "batch: second argument must be 'demo', 'batch' or 'test'; found: ~A, assumed 'batch'~%") demo)))
180 (if (streamp filename-or-stream)
181 (batch-stream filename-or-stream demo)
182 (let
183 ((filename ($file_search1 filename-or-stream
184 (if (eql demo :demo)
185 '((mlist) $file_search_demo )
186 '((mlist) $file_search_maxima)))))
187 (cond
188 ((eq demo :test)
189 (test-batch filename nil :show-all t))
191 (with-open-file (in-stream filename)
192 (batch-stream in-stream demo)))))))
194 (defun batch-stream (in-stream demo)
195 (declare (special $batch_answers_from_file))
196 (let ($load_pathname)
197 (let*
198 ((*read-base* 10.)
199 (stream-truename (get-stream-truename in-stream))
200 (in-stream-string-rep
201 (if stream-truename
202 (setq $load_pathname (cl:namestring stream-truename))
203 (format nil "~A" in-stream)))
204 (*query-io* (if $batch_answers_from_file
205 (make-two-way-stream in-stream (make-string-output-stream)) *query-io*)))
206 (format t (intl:gettext "~%read and interpret ~A~%") in-stream-string-rep)
207 (catch 'macsyma-quit (continue :stream in-stream :batch-or-demo-flag demo))
208 (incf $linenum)
209 in-stream-string-rep)))
211 ;; When either a or b are special float values (NaN or +/-Inf), return true iff
212 ;; a and b are the both NaN or both +Inf or both -Inf.
213 ;; Note that float_approx_equal(NaN, NaN) returns true.
215 ;; When a and b to finite, nonzero floats, return true iff
217 ;; |a - b| <= float_approx_equal_tolerance * min(2^n, 2^m)
219 ;; where a = af * 2^m, |af| < 1, and m is an integer (similarly for b).
220 ;; See Knuth, "The Art of Computer Programming" (3rd ed.), Vol. 2, Sec. 4.2.2, Eq. 24, page 233.
222 ;; Note that Eq. 24 isn't well-defined for a or b equal to zero.
223 ;; To make progress, let's consider the limit as b --> 0,
224 ;; therefore n --> -inf. For any finite a, that means that min(2^n, 2^m) = 2^n
225 ;; and therefore |a - b| --> |a|, but to pass the test,
226 ;; |a| must be less than or equal to float_approx_equal_tolerance * 2^n --> 0.
227 ;; Therefore the test fails for all nonzero, finite a, when b = 0.
228 ;; i.e. when either a or b is zero but not both, return false.
230 ;; Note also that if a < 0 and b > 0 or vice versa, the test must fail:
231 ;; without loss of generality assume |a| > |b|. Then n <= m and min(2^n, 2^m) = 2^n.
232 ;; Now |a - b| = |a| + |b| since a and b are different signs.
233 ;; 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.
234 ;; So unless float_approx_equal_tolerance is unusually large, the test must fail.
236 ;; Otherwise, either a or b is not a float, so return false.
238 (defmvar $float_approx_equal_tolerance (* 8 +flonum-epsilon+))
240 (defmfun $float_approx_equal (a b)
241 (setq a (if (floatp a) a ($float a)))
242 (setq b (if (floatp b) b ($float b)))
243 (and
244 (floatp a)
245 (floatp b)
246 (cond
247 ;; look for NaN
248 ((/= a a) (/= b b))
249 ((/= b b) nil)
250 ;; look for Inf
251 ((> (abs a) most-positive-double-float) (= a b))
252 ((> (abs b) most-positive-double-float) nil)
253 ;; look for zero
254 ((= a 0d0) (= b 0d0))
255 ((= b 0d0) nil)
256 ;; look for A = B
257 ((= a b))
259 ;; Implement test without involving floating-point arithmetic,
260 ;; to avoid errors which could occur with extreme values.
261 (multiple-value-bind (a-significand a-expt a-sign)
262 (decode-float a)
263 (declare (ignore a-significand))
264 (multiple-value-bind (b-significand b-expt b-sign)
265 (decode-float b)
266 (declare (ignore b-significand))
267 (when (or (= a-sign b-sign)
268 (>= $float_approx_equal_tolerance 1d0))
269 (multiple-value-bind (a-b-significand a-b-expt)
270 (integer-decode-float (abs (- a b)))
271 (multiple-value-bind (tol-significand tol-expt)
272 (integer-decode-float $float_approx_equal_tolerance)
273 (or (< a-b-expt (+ tol-expt (min a-expt b-expt)))
274 (and (= a-b-expt (+ tol-expt (min a-expt b-expt)))
275 (<= a-b-significand tol-significand))))))))))))
277 ;; Big float version of float_approx_equal. But for bfloat_approx_equal, the tolerance isn't
278 ;; user settable; instead, it is 32 / 2^fpprec. The factor of 32 is too large, I suppose. But
279 ;; the test suite gives a few errors with a factor of 16. These errors might be due to
280 ;; float / big float comparisons.
282 (defmfun $bfloat_approx_equal (a b)
283 (setq a (if ($bfloatp a) a ($bfloat a)))
284 (setq b (if ($bfloatp b) b ($bfloat b)))
285 (let ((m) (bits))
286 (and
287 ($bfloatp a)
288 ($bfloatp b)
289 (setq bits (min (third (first a)) (third (first b))))
290 (setq m (* 32 (expt 2 (- bits)) (min (expt 2 (- (car (last a)) 1)) (expt 2 (- (car (last b)) 1)))))
291 (setq m (if (rationalp m) (div (numerator m) (denominator m)) m))
292 (eq t (mgqp m (take '(mabs) (sub a b)))))))
295 ;; The first argument 'f' is the expected result; the second argument
296 ;; 'g' is the output of the test. By explicit evaluation, the expected
297 ;; result *can* be a CL array, CL hashtable, or a taylor polynomial. Such
298 ;; a test would look something like (yes, it's a silly test)
300 ;; taylor(x,x,0,2);
301 ;; ''(taylor(x,x,0,2)
303 (defun approx-alike (f g)
305 (cond ((floatp f) (and (floatp g) ($float_approx_equal f g)))
307 (($bfloatp f) (and ($bfloatp g) ($bfloat_approx_equal f g)))
309 (($taylorp g)
310 (approx-alike 0 (sub (ratdisrep f) (ratdisrep g))))
312 ((stringp f)
313 (and (stringp g) (string= f g)))
315 ((arrayp f)
316 (and (arrayp g)
317 (equal (array-dimensions f) (array-dimensions g))
318 (approx-alike ($listarray f) ($listarray g))))
320 ((hash-table-p f)
321 (and (hash-table-p g) (approx-alike ($listarray f) ($listarray g))))
323 ((atom f)
324 (and (atom g) (equal f g)))
326 ((op-equalp f 'lambda)
327 (and (op-equalp g 'lambda)
328 (approx-alike-list (mapcar #'(lambda (s) (simplifya s nil)) (margs f))
329 (mapcar #'(lambda (s) (simplifya s nil)) (margs g)))))
331 (($ratp f)
332 (and ($ratp g) (approx-alike (ratdisrep f) (ratdisrep g))))
334 ;; maybe we don't want this.
335 ((op-equalp f 'mquote)
336 (approx-alike (second f) g))
338 ;; I'm pretty sure that (mop f) and (mop g) won't signal errors, but
339 ;; let's be extra careful.
341 ((and (consp f) (consp (car f)) (consp g) (consp (car g))
342 (or (approx-alike (mop f) (mop g))
343 (and (symbolp (mop f)) (symbolp (mop g))
344 (approx-alike ($nounify (mop f)) ($nounify (mop g)))))
345 (eq ($subvarp f) ($subvarp g))
346 (approx-alike-list (margs f) (margs g))))
348 (t nil)))
350 (defun approx-alike-list (p q)
351 (cond ((null p) (null q))
352 ((null q) (null p))
353 (t (and (approx-alike (first p) (first q)) (approx-alike-list (rest p) (rest q))))))
355 (defun simple-equal-p (f g)
356 (approx-alike (simplifya f nil) (simplifya g nil)))
358 (defun batch-equal-check (expected result)
359 (let ((answer (catch 'macsyma-quit (simple-equal-p expected result))))
360 (if (eql answer 'maxima-error) nil answer)))
362 (defvar *collect-errors* t)
364 ;; Execute the code in FILENAME as a batch file. If EXPECTED-ERRORS
365 ;; is non-NIL, it is a list of numbers denoting which tests in this
366 ;; file are expected to fail. OUT specifies the stream where any
367 ;; output goes (defaulting to *standard-output*). SHOW-EXPECTED is
368 ;; non-NIL if the expected results should also be printed. SHOW-ALL
369 ;; is non-NIL if all tests (including expected failures) should be
370 ;; shown. Finally, SHOWTIME is non-NIL if the execution time should
371 ;; be displayed.
373 ;; This function returns four values:
374 ;; 1. the filename
375 ;; 2. NIL or a Maxima list of test numbers that failed
376 ;; 3. NIL or a Maxima list of test numbers that were expected to
377 ;; fail but actually passed.
378 ;; 4. Total number of tests in the file
379 (defun test-batch (filename expected-errors
380 &key (out *standard-output*) (show-expected nil)
381 (show-all nil) (showtime nil))
382 (declare (special $batch_answers_from_file))
383 (let (result
384 next-result
385 next
386 error-log
387 all-differences
388 unexpected-pass
389 strm
390 expr
391 problem-lineinfo
392 problem-lineno
393 tmp-output
394 save-output
395 ($ratprint nil)
396 (*mread-prompt* "")
397 (*read-base* 10.)
398 (num-problems 0)
399 (i 0)
400 (start-run-time 0)
401 (end-run-time 0)
402 (start-real-time 0)
403 (end-real-time 0)
404 (test-start-run-time 0)
405 (test-end-run-time 0)
406 (test-start-real-time 0)
407 (test-end-real-time 0)
408 (*query-io* *query-io*)
409 (*standard-input* *standard-input*))
411 (cond (*collect-errors*
412 (setq error-log
413 (if (streamp *collect-errors*) *collect-errors*
414 (handler-case
415 (open (alter-pathname filename :type "ERR") :direction :output :if-exists :supersede)
416 (file-error () nil))))
417 (when error-log
418 (format t (intl:gettext "~%batch: write error log to ~a") error-log)
419 (format error-log (intl:gettext "~%/* Maxima error log from tests in ~A") filename)
420 (format error-log " */~2%"))))
422 (unwind-protect
423 (progn
424 (setq strm (open filename :direction :input))
425 (when $batch_answers_from_file
426 (setq *query-io* (make-two-way-stream strm out)))
427 (setq start-real-time (get-internal-real-time))
428 (setq start-run-time (get-internal-run-time))
429 (while (not (eq 'eof (setq expr (mread strm 'eof))))
430 (incf num-problems)
431 (setq problem-lineinfo (second (first expr)))
432 (setq problem-lineno (if (and (consp problem-lineinfo) (integerp (first problem-lineinfo)))
433 (1+ (first problem-lineinfo))))
434 (incf i)
435 (setf tmp-output (make-string-output-stream))
436 (setf save-output *standard-output*)
437 (setf *standard-output* tmp-output)
439 (unwind-protect
440 (progn
441 (setq test-start-run-time (get-internal-run-time))
442 (setq test-start-real-time (get-internal-real-time))
443 (let (($errormsg t))
444 (setq result (meval* `(($errcatch) ,(third expr)))))
445 (setq result (if ($emptyp result) 'error-catch (second result)))
446 (setq test-end-run-time (get-internal-run-time))
447 (setq test-end-real-time (get-internal-real-time))
448 (setq $% result))
449 (setf *standard-output* save-output))
451 (setq next (mread strm 'eof))
452 (if (eq next 'eof) (merror (intl:gettext "batch: missing expected result in test script.")))
454 (setq next-result (third next))
455 (let* ((correct (batch-equal-check next-result result))
456 (expected-error (member i expected-errors))
457 (pass (or correct expected-error)))
458 (when (or show-all (not pass) (and correct expected-error)
459 (and expected-error show-expected))
460 (format out (intl:gettext "~%********************** Problem ~A~A***************")
461 i (if problem-lineno (format nil " (line ~S) " problem-lineno) " "))
462 (format out (intl:gettext "~%Input:~%"))
463 (displa (third expr))
464 (format out (intl:gettext "~%~%Result:~%"))
465 (format out "~a" (get-output-stream-string tmp-output))
466 (displa $%)
467 (when (eq showtime '$all)
468 (format out (intl:gettext "~%Time: ~,3F sec (~,3F elapsed)")
469 (float (/ (- test-end-run-time test-start-run-time)
470 internal-time-units-per-second))
471 (float (/ (- test-end-real-time test-start-real-time)
472 internal-time-units-per-second)))))
473 (cond ((and correct expected-error)
474 (push i unexpected-pass)
475 (format t
476 (intl:gettext "~%... Which was correct, but was expected ~
477 to be wrong due to a known bug in~% Maxima or ~A.~%")
478 (lisp-implementation-type)))
479 (correct
480 (if show-all (format t (intl:gettext "~%... Which was correct.~%"))))
481 ((and (not correct) expected-error)
482 (when (or show-all show-expected)
483 (format t
484 (intl:gettext "~%This is a known error in Maxima or in ~A. ~
485 The correct result is:~%")
486 (lisp-implementation-type))
487 (displa next-result)))
488 (t (format t (intl:gettext "~%This differed from the expected result:~%"))
489 (push i all-differences)
490 (displa next-result)
491 (cond ((and *collect-errors* error-log)
492 (format error-log (intl:gettext "/* Problem ~A~A*/~%")
493 i (if problem-lineno (format nil " (line ~S) " problem-lineno) " "))
494 (mgrind (third expr) error-log)
495 (list-variable-bindings (third expr) error-log)
496 (format error-log ";~%")
497 (format error-log (intl:gettext "/* Erroneous Result?:~%"))
498 (mgrind result error-log) (format error-log " */ ")
499 (terpri error-log)
500 (format error-log (intl:gettext "/* Expected result: */~%"))
501 (mgrind next-result error-log)
502 (format error-log ";~%~%"))))))))
503 (close strm))
504 (setq end-run-time (get-internal-run-time))
505 (setq end-real-time (get-internal-real-time))
506 (cond (error-log
507 (or (streamp *collect-errors*)
508 (close error-log))))
509 (let*
510 ((n-expected-errors (length expected-errors))
511 (expected-errors-trailer
512 (if (= n-expected-errors 0)
514 (format nil (intl:gettext " (not counting ~a expected errors)") n-expected-errors)))
515 (time (if showtime
516 (format nil (intl:gettext " using ~,3F seconds (~,3F elapsed).~%")
517 (float (/ (- end-run-time start-run-time) internal-time-units-per-second))
518 (float (/ (- end-real-time start-real-time) internal-time-units-per-second)))
519 "")))
520 (cond ((null all-differences)
521 (format t (intl:gettext "~a/~a tests passed~a~%~A")
522 (- num-problems n-expected-errors) (- num-problems n-expected-errors)
523 expected-errors-trailer
524 time)
525 (when unexpected-pass
526 (multiple-value-bind (plural was-were)
527 (if (> (length unexpected-pass) 1)
528 (values "s" "were")
529 (values "" "was"))
530 (format t (intl:gettext "~%The following ~A problem~A passed but ~A expected to fail: ~A~%")
531 (length unexpected-pass) plural was-were (reverse unexpected-pass))))
532 (values filename
534 `((mlist) ,@(reverse unexpected-pass))
535 num-problems))
537 (format t (intl:gettext "~%~a/~a tests passed~a~%~A")
538 (- num-problems n-expected-errors (length all-differences)) (- num-problems n-expected-errors) expected-errors-trailer
539 time)
540 (let ((s (if (> (length all-differences) 1) "s" "")))
541 (format t (intl:gettext "~%The following ~A problem~A failed: ~A~%")
542 (length all-differences) s (reverse all-differences)))
543 (when unexpected-pass
544 (multiple-value-bind (plural was-were)
545 (if (> (length unexpected-pass) 1)
546 (values "s" "were")
547 (values "" "was"))
548 (format t (intl:gettext "~%The following ~A problem~A passed but ~A expected to fail: ~A~%")
549 (length unexpected-pass) plural was-were (reverse unexpected-pass))))
550 (values filename
551 `((mlist) ,@(reverse all-differences))
552 `((mlist) ,@(reverse unexpected-pass))
553 num-problems))))))
555 ;;to keep track of global values during the error:
556 (defun list-variable-bindings (expr &optional str &aux tem)
557 (loop for v in(cdr ($listofvars expr))
558 when (member v $values :test #'equal)
559 collecting (setq tem`((mequal) ,v ,(meval* v)))
561 do (cond (str (format str ",")(mgrind tem str)))))
563 ;;in init_max
564 ;; name = foo or foo.type or dir/foo.type or dir/foo
565 ;; the empty parts are filled successively from defaults in templates in
566 ;; the path. A template may use multiple {a,b,c} constructions to indicate
567 ;; multiple possibilities. eg foo.l{i,}sp or foo.{dem,dm1,dm2}
568 (defmfun $file_search (name &optional paths)
569 (if (and (symbolp name)
570 (char= (char (symbol-name name) 0) #\$))
571 (setq name (subseq (print-invert-case name) 1)))
572 (if (symbolp name) (setf name (string name)))
573 (if (file-exists-p name) (return-from $file_search name))
574 (or paths (setq paths ($append $file_search_lisp $file_search_maxima
575 $file_search_demo)))
576 (atomchk paths '$file_search t)
577 (new-file-search (string name) (cdr paths)))
579 ;; Returns T if NAME exists and it does not appear to be a directory.
580 ;; Note that Clisp throws an error from PROBE-FILE if NAME exists
581 ;; and is a directory; hence the use of IGNORE-ERRORS.
583 (defun file-exists-p (name)
584 (let ((foo (ignore-errors (probe-file name))))
585 (if foo (not (apparently-a-directory-p foo)))))
587 (defun apparently-a-directory-p (path)
588 (eq (pathname-name path) nil))
590 ;; We keep these here in case we want to optimize the search. To
591 ;; speed things up, we might want to support search lists like
592 ;; "share/**/*.{mac,wxm,mc}" so that we only descend the directory
593 ;; once. Then we would take the list of paths and try to match the
594 ;; one with the given extensions.
596 ;; Currently, the search list is ["share/**/*.mac", "share/**/*.wxm",
597 ;; "share/**/*.mc"]. Thus to find "foo.mc", we end up doing a
598 ;; directory 3 times.
599 #+nil
600 (defun new-file-search (name template)
601 (cond ((file-exists-p name))
602 ((atom template)
603 (let ((lis (loop for w in (split-string template "{}")
604 when (null (position #\, w))
605 collect w
606 else
607 collect (split-string w ","))))
608 (new-file-search1 name "" lis)))
610 (let ((temp nil))
611 (loop for v in template
612 when (setq temp (new-file-search name v))
613 do (return temp))))))
615 #+nil
616 (defun new-file-search1 (name begin lis)
617 (cond ((null lis)
618 (let ((file (namestring ($filename_merge begin name))))
619 (if (file-exists-p file) file nil)))
620 ((atom (car lis))
621 (new-file-search1 name
622 (if begin
623 ($sconcat begin (car lis)) (car lis))
624 (cdr lis)))
625 (t (loop for v in (car lis) with tem
626 when (setq tem (new-file-search1 name begin (cons v (cdr lis))))
627 do (return tem)))))
629 (defvar *debug-new-file-search* nil)
631 ;; Search for a file named NAME. If the file exists, return it.
632 ;; Otherwise, TEMPLATE is a list of wildcard paths to be searched for
633 ;; the NAME. Each entry in TEMPLATE should be a Lisp wildcard
634 ;; pathname.
635 (defun new-file-search (name template)
636 (cond ((file-exists-p name))
638 (let ((filename (pathname name)))
639 (dolist (path template)
640 (let ((pathnames (directory (merge-pathnames filename path))))
641 (when *debug-new-file-search*
642 (format *debug-io* "wildpath ~S~%" (merge-pathnames filename path)))
643 (when pathnames
644 ;; We MUST sort the results in alphabetical order
645 ;; because that's how the old search paths were
646 ;; sorted.
647 (setf pathnames (sort pathnames #'string< :key #'namestring))
648 (when *debug-new-file-search*
649 (format *debug-io* "pathname = ~S~%" pathnames))
650 ;; If more than one path is returned, print a warning
651 ;; that we're selecting the first file. Print all
652 ;; the matches too so the user knows.
653 (unless (= 1 (length pathnames))
654 (mwarning
655 (format nil
656 "More than one file matches. Selecting the first file from:~
657 ~%~{ ~A~^~%~}~%"
658 (mapcar #'namestring pathnames))))
659 (return-from new-file-search (namestring (first pathnames))))))))))
661 (defun save-linenumbers (&key (c-lines t) d-lines (from 1) (below $linenum) a-list
662 (file "/tmp/lines")
663 &aux input-symbol ($linel 79))
664 (cond ((null a-list) (setq a-list (loop for i from from below below collecting i))))
665 (with-open-file (st file :direction :output)
666 (format st "/* -*- Mode: MACSYMA; Package: MACSYMA -*- */")
667 (format st "~%~% /* ~A */ ~%"
668 (let ((tem (cdddr
669 (multiple-value-list (get-decoded-time)))))
670 (format nil "~a:~a:~a" (car tem) (cadr tem) (caadr tem))))
671 (loop for i in a-list
672 when (and c-lines (boundp (setq input-symbol (intern (format nil "$~A~A" '#:c i)))))
674 (format st "~% C~3A; " i)
675 (mgrind (symbol-value input-symbol) st)
676 (format st ";")
677 when (and d-lines
678 (boundp (setq input-symbol (intern (format nil "$~A~A" '#:d i)))))
680 (format st "~% D~3A: " i)
681 (mgrind (symbol-value input-symbol) st)
682 (format st "$"))))
685 (defmfun $printfile (file)
686 (setq file ($file_search1 file '((mlist) $file_search_usage)))
687 (with-open-file (st file)
688 (loop
689 with tem
690 while (setq tem (read-char st nil 'eof))
692 (if (eq tem 'eof) (return t))
693 (princ tem))
694 (namestring file)))
696 (defun disable-some-lisp-warnings ()
697 ;; Suppress warnings about redefining functions;
698 ;; it appears that only Clisp and SBCL emit these warnings
699 ;; (ECL, GCL, CMUCL, and Clozure CL apparently do not).
700 ;; Such warnings are generated by the autoload mechanism.
701 ;; I guess it is plausible that we could also avoid the warnings by
702 ;; reworking autoload to not trigger them. I don't have enough
703 ;; motivation to attempt that right now.
704 #+sbcl
705 (setq sb-ext:*muffled-warnings* '(or sb-kernel:redefinition-with-defun sb-kernel:uninteresting-redefinition))
706 #+sbcl
707 (declaim (sb-ext:muffle-conditions sb-ext:compiler-note))
708 #+clisp
709 (setq custom:*suppress-check-redefinition* t)
711 ;; Suppress compiler output messages.
712 ;; These include the "0 errors, 0 warnings" message output from Clisp,
713 ;; and maybe other messages from other Lisps.
714 (setq *compile-verbose* nil))
716 (defun enable-some-lisp-warnings ()
717 ;; SB-KERNEL:UNINTERESTING-REDEFINITION appears to be the default value.
718 #+sbcl
719 (setq sb-ext:*muffled-warnings* 'sb-kernel:uninteresting-redefinition)
720 #+sbcl
721 (declaim (sb-ext:unmuffle-conditions sb-ext:compiler-note))
722 #+clisp
723 (setq custom:*suppress-check-redefinition* nil)
724 (setq *compile-verbose* t))
726 (defun simple-remove-dollarsign (x)
727 "Like stripdollar, but less heavy. Intended for use with the
728 testsuite implementation."
729 (if (symbolp x)
730 (subseq (maxima-string x) 1)
733 (defun intersect-tests (tests)
734 ;; If TESTS is non-NIL, we assume it's a Maxima list of (maxima)
735 ;; strings naming the tests we want to run. They must match the
736 ;; file names in $testsuite_files. We ignore any items that aren't
737 ;; in $testsuite_files.
738 (mapcar #'simple-remove-dollarsign
739 (cond (tests
740 (let ((results nil))
741 ;; Using INTERSECTION would be convenient, but
742 ;; INTERSECTION can return the result in any
743 ;; order, and we'd prefer that the order of the
744 ;; tests be preserved. CMUCL and CCL returns the
745 ;; intersection in reverse order. Clisp produces
746 ;; the original order. Fortunately, this doesn't
747 ;; have to be very fast, so we do it very naively.
748 (dolist (test (mapcar #'simple-remove-dollarsign (cdr tests)))
749 (let ((matching-test (find test (cdr $testsuite_files)
750 :key #'(lambda (x)
751 (maxima-string (if (listp x)
752 (second x)
753 x)))
754 :test #'string=)))
755 (when matching-test
756 (push matching-test results))))
757 (nreverse results)))
759 (cdr $testsuite_files)))))
761 (defun print-testsuite-summary (errs unexpected-pass error-count total-count)
762 (flet
763 ((problem-summary (x)
764 ;; We want to print all the elements in the list.
765 (let ((*print-length* nil)
766 (s (if (> (length (rest x)) 1) "s" "")))
767 (format t
768 (intl:gettext
769 " ~a problem~a:~% ~a~%")
770 (first x)
772 (sort (rest x) #'<)))))
773 (if (null errs)
774 (format t
775 (intl:gettext
776 "~%~%No unexpected errors found out of ~:d tests.~%")
777 total-count)
778 (format t (intl:gettext "~%Error summary:~%")))
779 (when errs
780 (format t (intl:gettext "Error(s) found:~%"))
781 (mapcar #'problem-summary (reverse errs)))
782 (when unexpected-pass
783 (format t (intl:gettext "Tests that were expected to fail but passed:~%"))
784 (mapcar #'problem-summary (reverse unexpected-pass)))
785 (when errs
786 (format t
787 (intl:gettext
788 "~&~:d test~p failed out of ~:d total tests.~%")
789 error-count
790 error-count
791 total-count))))
793 (defun validate-given-tests (tests share-tests-p)
794 ;; Check the test names and print out some warnings if it
795 ;; doesn't exist, or if it does and is part of the share test
796 ;; suite, but share_tests was not set.
797 (dolist (test (mapcar #'simple-remove-dollarsign
798 (if (listp tests)
799 (cdr tests)
800 (list tests))))
801 (cond ((and (not share-tests-p)
802 (find test (cdr $share_testsuite_files)
803 :key #'(lambda (x)
804 (maxima-string (if (listp x)
805 (second x)
806 x)))
807 :test #'string=))
808 (mwarning test "is a share test, but share_tests was not set"))
809 ((not (find test (cdr $testsuite_files)
810 :key #'(lambda (x)
811 (maxima-string (if (listp x)
812 (second x)
813 x)))
814 :test #'string=))
815 (mwarning "Unknown test: " test)))))
817 (defmfun $run_testsuite (&key tests display_all display_known_bugs share_tests time debug)
818 "Run the testsuite. Options are
819 tests List of tests to run
820 display_all Display output from each test entry
821 display_known_bugs Include tests that are known to fail.
822 time Display time to run each test entry
823 share_tests Whether to include the share testsuite or not
824 debug Set to enable some debugging prints.
826 (enable-some-lisp-warnings)
827 (let ((test-file)
828 (expected-failures)
829 (test-file-path))
830 (format t "Testsuite run for ~a ~a:~%"
831 (lisp-implementation-type) (lisp-implementation-version))
832 ;; Allow only T and NIL for display_known_bugs and display_all
833 (unless (member display_known_bugs '(t nil))
834 (merror (intl:gettext "run_testsuite: display_known_bugs must be true or false; found: ~M") display_known_bugs))
835 (unless (member display_all '(t nil))
836 (merror (intl:gettext "run_testsuite: display_all must be true or false; found: ~M") display_all))
837 (unless (member time '(t nil $all))
838 (merror (intl:gettext "run_testsuite: time must be true, false, or all; found: ~M") time))
840 (unless (member share_tests '(t nil $only))
841 (merror (intl:gettext "run_testsuite: share_tests must be true, false or only: found ~M") share_tests))
843 (setq *collect-errors* nil)
845 (multiple-value-bind (desired-tests desired-search-path)
846 (ecase share_tests
847 ((nil)
848 ;; Do nothing
849 (values $testsuite_files $file_search_tests))
850 ((t)
851 ;; Append the share files and concatenate the search paths
852 ;; for tests and maxima so we can find both sets of tests.
853 (values ($append $testsuite_files $share_testsuite_files)
854 ;; Is there a better way to do this?
855 (concatenate 'list
856 '((mlist))
857 (rest $file_search_tests)
858 (rest $file_search_maxima))))
859 ($only
860 ;; Only the share test files
861 (values $share_testsuite_files $file_search_maxima)))
862 (let* (($testsuite_files desired-tests)
863 ($file_search_tests desired-search-path)
864 (error-break-file)
865 (tests-to-run (intersect-tests (cond ((consp tests) tests)
866 (tests (list '(mlist) tests)))))
867 (test-count 0)
868 (total-count 0)
869 (error-count 0)
870 filename
871 diff
872 upass)
874 (validate-given-tests tests share_tests)
876 (when debug
877 (let (($stringdisp t))
878 (mformat t "$testsuite_files = ~M~%" $testsuite_files)
879 (mformat t "$file_search_tests = ~M~%" $file_search_tests)))
880 (when debug
881 (let (($stringdisp t))
882 (mformat t "tests-to-run = ~M~%" tests-to-run)))
884 (unless tests-to-run
885 (mwarning "No tests to run")
886 (return-from $run_testsuite '$done))
888 (flet
889 ((testsuite ()
890 (loop with errs = 'nil
891 with unexpected-pass = nil
892 for testentry in tests-to-run
893 do (if (atom testentry)
894 (progn
895 (setf test-file testentry)
896 (setf expected-failures nil))
897 (progn
898 (setf test-file (second testentry))
899 (setf expected-failures
900 ;; Support the expected failures list in
901 ;; two formats:
903 ;; ((mlist) "test" 1 2 3)
904 ;; ((mlist) "test" ((mlist) 1 2 3))
906 ;; The first is the old style whereas the
907 ;; second is the new style. We support
908 ;; the old style for backward
909 ;; compatibility.
910 (if (consp (caddr testentry))
911 (cdaddr testentry)
912 (cddr testentry)))))
913 (setf test-file-path ($file_search test-file $file_search_tests))
914 (format t
915 (intl:gettext "Running tests in ~a: ")
916 (if (symbolp test-file)
917 (subseq (print-invert-case test-file) 1)
918 test-file))
919 (when debug
920 (format t (intl:gettext "(~A) ") test-file-path))
922 (errset
923 (progn
924 (multiple-value-setq (filename diff upass test-count)
925 (test-batch test-file-path
926 expected-failures :show-expected display_known_bugs
927 :show-all display_all :showtime time))
928 (incf total-count test-count)
929 (when (or (rest diff) (rest upass))
930 (incf error-count (length (rest diff)))
931 (when (rest diff)
932 (push (list* filename (rest diff))
933 errs))
934 (when (rest upass)
935 (push (list* filename (rest upass))
936 unexpected-pass)))))
937 (progn
938 (setq error-break-file (format nil "~a" test-file))
939 (push (list error-break-file "error break")
940 errs)
941 (format t
942 (intl:gettext "~%Caused an error break: ~a")
943 test-file)
944 ;; If the test failed because we
945 ;; couldn't find the file, make a note of
946 ;; that.
947 (unless test-file-path
948 (format t (intl:gettext ": test file not found.")))
949 (format t "~%")))
950 finally
951 (print-testsuite-summary errs unexpected-pass error-count total-count))))
952 (time (testsuite))))))
953 (disable-some-lisp-warnings)
954 '$done)