trace.lsp 5.04 KB
Newer Older
pfdietz's avatar
pfdietz committed
1 2 3 4 5 6 7 8 9 10 11 12
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sun Dec 12 19:53:11 2004
;;;; Contains: Tests of TRACE, UNTRACE

(in-package :cl-test)

(defun function-to-trace (x) (car x))
(defun another-function-to-trace (x) (cdr x))
(defun (setf function-to-trace) (val arg) (setf (car arg) val))

(declaim (notinline function-to-trace
sds's avatar
sds committed
13 14
                    another-function-to-trace
                    (setf function-to-trace)))
pfdietz's avatar
pfdietz committed
15 16 17 18 19 20 21 22 23 24 25 26 27

(deftest trace.1
  (progn
    (untrace)  ;; ensure it's not traced
    (with-output-to-string
      (*trace-output*)
      (assert (eql (function-to-trace '(a)) 'a))))
  "")

(deftest trace.2
  (progn
    (trace function-to-trace)
    (equal "" (with-output-to-string
sds's avatar
sds committed
28 29
                (*trace-output*)
                (assert (eql (function-to-trace '(b)) 'b)))))
pfdietz's avatar
pfdietz committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
  nil)

(deftest trace.3
  (progn
    (untrace)
    (trace function-to-trace)
    (prog1 (trace)
      (untrace)
      (assert (null (trace)))))
  (function-to-trace))


(deftest trace.4
  (progn
    (untrace)
    (trace function-to-trace)
    (handler-bind ((warning #'muffle-warning))
sds's avatar
sds committed
47
                  (trace function-to-trace))
pfdietz's avatar
pfdietz committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    (prog1 (trace)
      (untrace)
      (assert (null (trace)))))
  (function-to-trace))

(deftest trace.5
  (progn
    (untrace)
    (trace (setf function-to-trace))
    (prog1 (trace)
      (untrace)
      (assert (null (trace)))))
  ((setf function-to-trace)))

(deftest trace.6
  (progn
    (untrace)
    (trace (setf function-to-trace))
    (handler-bind ((warning #'muffle-warning))
sds's avatar
sds committed
67
                  (trace (setf function-to-trace)))
pfdietz's avatar
pfdietz committed
68 69 70 71 72 73 74 75 76 77 78
    (prog1 (trace)
      (untrace)
      (assert (null (trace)))))
  ((setf function-to-trace)))

(deftest trace.7
  (progn
    (untrace)
    (with-output-to-string
      (*trace-output*)
      (let ((x (list nil)))
sds's avatar
sds committed
79 80
        (assert (eql (setf (function-to-trace x) 'a) 'a))
        (assert (equal x '(a))))))
pfdietz's avatar
pfdietz committed
81 82 83 84 85 86 87
  "")

(deftest trace.8
  (progn
    (untrace)
    (trace (setf function-to-trace))
    (equal ""
sds's avatar
sds committed
88 89 90 91 92
           (with-output-to-string
             (*trace-output*)
             (let ((x (list nil)))
               (assert (eql (setf (function-to-trace x) 'a) 'a))
               (assert (equal x '(a)))))))
pfdietz's avatar
pfdietz committed
93 94 95 96 97 98 99
  nil)

(deftest trace.9
  (progn
    (untrace)
    (trace function-to-trace another-function-to-trace)
    (assert (not (equal "" (with-output-to-string
sds's avatar
sds committed
100 101
                             (*trace-output*)
                             (assert (eql (function-to-trace '(b)) 'b))))))
pfdietz's avatar
pfdietz committed
102
    (assert (not (equal "" (with-output-to-string
sds's avatar
sds committed
103 104 105
                             (*trace-output*)
                             (assert (eql (another-function-to-trace '(c . d))
                                          'd))))))
pfdietz's avatar
pfdietz committed
106
    (prog1
sds's avatar
sds committed
107 108 109
        (sort (copy-list (trace))
              #'(lambda (k1 k2) (string< (symbol-name k1)
                                         (symbol-name k2))))
pfdietz's avatar
pfdietz committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
      (untrace)))
  (another-function-to-trace function-to-trace))

(deftest trace.10
  (progn
    (untrace)
    (assert (null (trace)))
    (trace function-to-trace)
    (untrace function-to-trace)
    (assert (null (trace)))
    (handler-bind ((warning #'muffle-warning)) (untrace function-to-trace))
    (assert (null (trace)))
    nil)
  nil)

(deftest trace.11
  (progn
    (untrace)
    (trace function-to-trace another-function-to-trace)
    (untrace function-to-trace another-function-to-trace)
    (trace))
  nil)

;;; Tracing a generic function

(declaim (notinline generic-function-to-trace))

(deftest trace.12
  (progn
    (untrace)
140
    (eval '(defgeneric generic-function-to-trace (x y)))
pfdietz's avatar
pfdietz committed
141 142 143 144 145 146 147
    (trace generic-function-to-trace)
    (prog1 (trace) (untrace)))
  (generic-function-to-trace))

(deftest trace.13
  (progn
    (untrace)
148
    (eval '(defgeneric generic-function-to-trace (x y)))
pfdietz's avatar
pfdietz committed
149
    (trace generic-function-to-trace)
150
    (eval '(defmethod generic-function-to-trace ((x t)(y t)) nil))
pfdietz's avatar
pfdietz committed
151 152 153 154 155 156
    (prog1 (trace) (untrace)))
  (generic-function-to-trace))

(deftest trace.14
  (progn
    (untrace)
157
    (eval '(defgeneric generic-function-to-trace (x y)))
pfdietz's avatar
pfdietz committed
158
    (trace generic-function-to-trace)
159
    (eval '(defmethod generic-function-to-trace ((x t)(y t)) nil))
pfdietz's avatar
pfdietz committed
160
    (assert (not (equal (with-output-to-string
sds's avatar
sds committed
161 162 163
                          (*trace-output*)
                          (assert (null (generic-function-to-trace 'a 'b))))
                        "")))
164
    (prog1
sds's avatar
sds committed
165
        (trace)
166 167 168 169 170
      (untrace generic-function-to-trace)
      (assert (null (trace)))))
  (generic-function-to-trace))

(declaim (notinline generic-function-to-trace2))
pfdietz's avatar
pfdietz committed
171

172 173 174
(deftest trace.15
  (progn
    (untrace)
175
    (let* ((gf (eval '(defgeneric generic-function-to-trace2 (x y))))
sds's avatar
sds committed
176 177 178
           (m (eval '(defmethod generic-function-to-trace2
                       ((x integer)(y integer))
                       :foo))))
179
      (eval '(defmethod generic-function-to-trace2
sds's avatar
sds committed
180
               ((x symbol)(y symbol)) :bar))
181 182 183 184 185 186 187
      (assert (eql (generic-function-to-trace2 1 2) :foo))
      (assert (eql (generic-function-to-trace2 'a 'b) :bar))
      (trace generic-function-to-trace2)
      (assert (equal (trace) '(generic-function-to-trace2)))
      (remove-method gf m)
      (prog1 (trace) (untrace))))
  (generic-function-to-trace2))