Debian Codegraph @

src/tests/ecl-tests.lisp

raw
1 ;;;; ecl-tests.lisp
2
3 (defpackage #:cl-test
4 (:use #:cl #:2am-ecl))
5
6 (in-package #:cl-test)
7
8
9 ;;; Set pathnames
10 (defparameter *aux-dir*
11 (merge-pathnames
12 "auxiliary/"
13 (make-pathname :directory (pathname-directory
14 (asdf:system-definition-pathname 'ecl-tests)))))
15
16 (defparameter *tmp-dir*
17 (merge-pathnames
18 "temporary/"
19 (make-pathname :directory (pathname-directory *default-pathname-defaults*))))
20
21
22 ;;;; Declare the suites
23 (suite 'make-check
24 '(executable ieee-fp eprocess package-ext hash-tables ansi+ mixed
25 cmp emb ffi mop run-program mp complex wscl #+unicode unicode
26 #+clos clos))
27
28 (suite 'ecl-tests
29 '(make-check eformat))
30
31
32 (defmacro is-true (form)
33 (ext:once-only (form)
34 `(is (eql ,form t) "Expected T, but got ~s" ,form)))
35
36 (defmacro is-false (form)
37 (ext:once-only (form)
38 `(is (null ,form) "Expected NIL, but got ~s" ,form)))
39
40 (defmacro is-equal (what form)
41 (ext:once-only (what form)
42 `(is (equal ,what ,form) "EQUAL: ~s to ~s" ,what ,form)))
43
44 (defmacro is-eql (what form)
45 (ext:once-only (what form)
46 `(is (eql ,what ,form) "EQL: ~s to ~a" ,what ,form)))
47
48 (defmacro pass (form &rest args)
49 (declare (ignore form args))
50 `(passed))
51
52 (defmacro fail (form &rest args
53 &aux
54 (fmt-ctrl (or (car args) ""))
55 (fmt-args (cdr args)))
56 (declare (ignore form))
57 `(failed (make-condition 'test-failure
58 :name *test-name*
59 :format-control ,fmt-ctrl
60 :format-arguments (list ,@fmt-args))))
61
62
63 ;;;; Author: Juan Jose Garcia-Ripoll
64 ;;;; Created: Fri Apr 14 11:13:17 CEST 2006
65 ;;;; Contains: Tools for doing tests, intercepting functions, etc.
66
67 (defmacro with-dflet (functions &body body)
68 "Syntax:
69 (with-dflet ((fname form*)*) body)
70 Evaluate BODY in an environment in which the function FNAME has been
71 redefined to evaluate the given forms _before_ executing the orginal
72 code."
73 (let ((vars '()) (in-forms '()) (out-forms '()))
74 (loop for (name . forms) in functions
75 do (let ((var (gensym)))
76 (push `(,var #',name) vars)
77 (push `(setf (fdefinition ',name)
78 #'(lambda (&rest args) ,@forms (apply ,var args)))
79 in-forms)
80 (push `(setf (fdefinition ',name) ,var) out-forms)))
81 `(let ,vars
82 (unwind-protect
83 (progn ,@in-forms ,@body)
84 (progn ,@out-forms)))))
85
86 (defmacro with-compiler ((filename &rest compiler-args) &body forms)
87 "Create a lisp file with the given forms and compile it. The forms
88 are evaluated unless they are strings. Strings are simply inlined to
89 allow using reader macros. The output is stored in a string and output
90 as a second value."
91 `(progn
92 (with-open-file (s ,filename :direction :output :if-exists :supersede
93 :if-does-not-exist :create)
94 (let ((*print-circle* t)
95 (*print-readably* t))
96 ,@(loop for f in forms collect (if (stringp f)
97 `(format s "~A" ,f)
98 `(print ,f s)))))
99 (let* ((compiled-file t)
100 (output
101 (with-output-to-string (*standard-output*)
102 (let ((*error-output* *standard-output*)
103 (*compile-verbose* t)
104 (*compile-print* t))
105 (setf compiled-file (compile-file ,filename ,@compiler-args))))))
106 ;; todo: add delete-files flag
107 ;; (when delete-files
108 ;; (delete-file filename)
109 ;; (delete-file compiled-file))
110 (when (null compiled-file)
111 (delete-file ,filename)
112 (error "Compiling file ~a failed:~%~a" ,filename output))
113 (values compiled-file output))))
114
115 (defmacro cmplambda (args &body body)
116 `(compile nil '(lambda ,args ,@body)))
117
118 (defmacro with-temporary-file ((var string &rest args) &body body)
119 (ext:with-unique-names (stream)
120 `(let ((,var (ext:mkstemp "ecl-tests")))
121 (with-open-file (,stream ,var :direction :output)
122 (format ,stream ,string ,@args))
123 (multiple-value-prog1 (progn ,@body)
124 (delete-file ,var)))))
125
126
127 ;;; Approximate equality function
128 (defun approx= (x y &optional (eps (epsilon x)))
129 (or (= x y)
130 (<= (abs (/ (- x y) (max (abs x) 1))) eps)))
131
132 (defun epsilon (number)
133 (etypecase number
134 (complex (* 2 (epsilon (realpart number)))) ;; crude
135 (short-float short-float-epsilon)
136 (single-float single-float-epsilon)
137 (double-float double-float-epsilon)
138 (long-float long-float-epsilon)
139 (rational 0)))
140
141 (defun test-C-program (c-code &key capture-output (args '()) (environ '()))
142 (ensure-directories-exist "tmp/")
143 (with-open-file (s "tmp/ecl-aux.c" :direction :output :if-exists :supersede
144 :if-does-not-exist :create)
145 (princ c-code s))
146 (c::compiler-cc "tmp/ecl-aux.c" "tmp/ecl-aux.o")
147 (c::linker-cc "tmp/ecl-aux.exe" '("tmp/ecl-aux.o"))
148 (let ((environment
149 (append #+windows (list (format nil "PATH=~a;~a"
150 (ext:getenv "PATH")
151 c::*ecl-library-directory*))
152 #+cygwin (list (format nil "PATH=~a:~a"
153 (ext:getenv "PATH")
154 c::*ecl-library-directory*))
155 #-(or windows cygwin) (list (format nil "LD_LIBRARY_PATH=~a:~a"
156 (ext:getenv "LD_LIBRARY_PATH")
157 c::*ecl-library-directory*))
158 environ
159 (ext:environ))))
160 (ecase capture-output
161 ((nil)
162 (multiple-value-bind (stream return-code)
163 (si::run-program "tmp/ecl-aux.exe" args
164 :output t :error t
165 :environ environment)
166 (declare (ignore stream))
167 (zerop return-code)))
168 ((string :string)
169 (multiple-value-bind (in return-code)
170 (si::run-program "tmp/ecl-aux.exe" args :output :stream :error t
171 :environ environment)
172 (values return-code
173 (with-output-to-string (s)
174 (loop with line
175 do (setf line (read-line in nil))
176 (unless line (return))
177 (write-line line s))))))
178 ((t forms :forms)
179 (do* ((all '())
180 (x t)
181 (in (si::run-program "tmp/ecl-aux.exe" args :output :stream
182 :environ environment)))
183 ((null in) all)
184 (setf x (ignore-errors (read in nil nil)))
185 (unless x (return all))
186 (push x all))))))