File: pprint-dispatch.lsp

package info (click to toggle)
cl-ansi-tests 20071218-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 7,000 kB
  • ctags: 22,025
  • sloc: lisp: 134,798; makefile: 144
file content (286 lines) | stat: -rw-r--r-- 8,418 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sat Jun 12 13:14:53 2004
;;;; Contains: Tests of PPRINT-DISPATCH, SET-PPRINT-DISPATCH

(in-package :cl-test)

(deftest pprint-dispatch.1
  (loop for x in (append *universe* *cl-symbols*)
	for vals = (multiple-value-list (pprint-dispatch x))
	for vals2 = (multiple-value-list (pprint-dispatch
					  x
					  *print-pprint-dispatch*))
	unless
	(and (= (length vals) 2)
	     (= (length vals2) 2)
	     (destructuring-bind (fun foundp)
		 vals
	       (if foundp
		   (and (or (typep fun 'function)
			    (and (symbolp fun)
				 (symbol-function fun)))
			(destructuring-bind (fun2 foundp2)
			    vals2
			  (and (equal fun fun2)
			       foundp2)))
		 (not (cadr vals2)))))
	collect (list x vals vals2))
  nil)
#|
(deftest pprint-dispatch.2
  (loop for sym in *cl-symbols*
	for x = (list sym nil nil)
	for vals = (multiple-value-list (pprint-dispatch x))
	for vals2 = (multiple-value-list (pprint-dispatch
					  x
					  *print-pprint-dispatch*))
	unless
	(and (= (length vals) 2)
	     (= (length vals2) 2)
	     (destructuring-bind (fun foundp)
		 vals
	       (if foundp
		   (and (or (typep fun 'function)
			    (and (symbolp fun)
				 (symbol-function fun)))
			(destructuring-bind (fun2 foundp2)
			    vals2
			  (and (equal fun fun2)
			       foundp2)))
		 (not (cadr vals2)))))
	collect (list x vals vals2))
  nil)
|#

;;; Test that setting the pprint dispatch of a symbol causes
;;; the printing to change, and that it can be unset.
(deftest pprint-dispatch.3
  (my-with-standard-io-syntax
   (let ((*print-pprint-dispatch* (copy-pprint-dispatch nil))
	 (*print-readably* nil)
	 (*print-escape* nil)
	 (*print-pretty* t))
     (let ((f #'(lambda (stream obj)
		  (declare (ignore obj))
		  (write "ABC" :stream stream))))
       (values
	(write-to-string '|X|)
	(set-pprint-dispatch '(eql |X|) f)
	(write-to-string '|X|)
	(set-pprint-dispatch '(eql |X|) nil)
	(write-to-string '|X|)))))
  "X" nil "ABC" nil "X")
       
;;; Test that setting the pprint dispatch of a symbol causes
;;; the printing to change for any real weight, and that it can be unset.
(deftest pprint-dispatch.4
  (my-with-standard-io-syntax
   (loop for v1 in (remove-if-not #'realp *universe*)
	 unless
	 (equal
	  (let ((*print-pprint-dispatch* (copy-pprint-dispatch nil))
		(*print-readably* nil)
		(*print-escape* nil)
		(*print-pretty* t))
	    (let ((f #'(lambda (stream obj)
			 (declare (ignore obj))
			 (write "ABC" :stream stream))))
	      (list
	       (write-to-string '|X|)
	       (set-pprint-dispatch '(eql |X|) f v1)
	       (write-to-string '|X|)
	       (set-pprint-dispatch '(eql |X|) nil)
	       (write-to-string '|X|))))
	  '("X" nil "ABC" nil "X"))
	 collect v1))
  nil)
  
;;; Test that setting the pprint dispatch of a symbol causes
;;; the printing to change, and that it can be unset with any real weight
(deftest pprint-dispatch.5
  (my-with-standard-io-syntax
   (loop for v1 in (remove-if-not #'realp *universe*)
	 unless
	 (equal
	  (let ((*print-pprint-dispatch* (copy-pprint-dispatch nil))
		(*print-readably* nil)
		(*print-escape* nil)
		(*print-pretty* t))
	    (let ((f #'(lambda (stream obj)
			 (declare (ignore obj))
			 (write "ABC" :stream stream))))
	      (list
	       (write-to-string '|X|)
	       (set-pprint-dispatch '(eql |X|) f)
	       (write-to-string '|X|)
	      (set-pprint-dispatch '(eql |X|) nil v1)
	      (write-to-string '|X|))))
	  '("X" nil "ABC" nil "X"))
	 collect v1))
  nil)

;;; Check that specifying the pprint-dispatch table argument to set-pprint-dispatch
;;; causes that table to be changed, not *print-pprint-dispatch*.
(deftest pprint-dispatch.6
  (my-with-standard-io-syntax
   (let ((other-ppd-table (copy-pprint-dispatch nil))
	 (*print-pprint-dispatch* (copy-pprint-dispatch nil))
	 (*print-readably* nil)
	 (*print-escape* nil)
	 (*print-pretty* t))
     (let ((f #'(lambda (stream obj)
		  (declare (ignore obj))
		  (write "ABC" :stream stream))))
       (values
	(write-to-string '|X|)
	(set-pprint-dispatch '(eql |X|) f 0 other-ppd-table)
	(write-to-string '|X|)
	(let ((*print-pprint-dispatch* other-ppd-table))
	  (write-to-string '|X|))
	(set-pprint-dispatch '(eql |X|) f)
	(write-to-string '|X|)
	(set-pprint-dispatch '(eql |X|) nil)
	(write-to-string '|X|)))))
  "X" nil "X" "ABC" nil "ABC" nil "X")

;;; Test that the default weight of set-pprint-dispatch is 0

(deftest pprint-dispatch.7
  (my-with-standard-io-syntax
   (let ((*print-pprint-dispatch* (copy-pprint-dispatch nil))
	 (*print-readably* nil)
	 (*print-escape* nil)
	 (*print-pretty* t))
     (let ((f #'(lambda (stream obj)
		  (declare (ignore obj))
		  (write "ABC" :stream stream)))
	   (g #'(lambda (stream obj)
		  (declare (ignore obj))
		  (write "DEF" :stream stream))))
       (values
	(write-to-string '|X|)
	(set-pprint-dispatch '(eql |X|) f)
	(write-to-string '|X|)
	(set-pprint-dispatch '(member |X| |Y|) g .0001)
	(write-to-string '|X|)
	(write-to-string '|Y|)))))
  "X" nil "ABC" nil "DEF" "DEF")

(deftest pprint-dispatch.8
  (my-with-standard-io-syntax
   (let ((*print-pprint-dispatch* (copy-pprint-dispatch nil))
	 (*print-readably* nil)
	 (*print-escape* nil)
	 (*print-pretty* t))
     (let ((f #'(lambda (stream obj)
		  (declare (ignore obj))
		  (write "ABC" :stream stream)))
	   (g #'(lambda (stream obj)
		  (declare (ignore obj))
		  (write "DEF" :stream stream))))
       (values
	(write-to-string '|X|)
	(set-pprint-dispatch '(eql |X|) f)
	(write-to-string '|X|)
	(set-pprint-dispatch '(member |X| |Y|) g -.0001)
	(write-to-string '|X|)
	(write-to-string '|Y|)))))
  "X" nil "ABC" nil "ABC" "DEF")

;;; Funtion designators in pprint-dispatch

(defun pprint-dispatch-test-fn.1 (stream obj) (declare (ignore obj)) (write "ABC" :stream stream))
(defun pprint-dispatch-test-fn.2 (stream obj) (declare (ignore obj)) (write "DEF" :stream stream))

(deftest pprint-dispatch.9
  (my-with-standard-io-syntax
   (let ((*print-pprint-dispatch* (copy-pprint-dispatch nil))
	 (*print-readably* nil)
	 (*print-escape* nil)
	 (*print-pretty* t))
     (values
      (write-to-string '|X|)
      (multiple-value-list (set-pprint-dispatch '(eql |X|) 'pprint-dispatch-test-fn.1))
      (write-to-string '|X|)
      (multiple-value-list (set-pprint-dispatch '(eql |X|) 'pprint-dispatch-test-fn.2))
      (write-to-string '|X|))))
  "X" (nil) "ABC" (nil) "DEF")

#|
(deftest pprint-dispatch.10
  (my-with-standard-io-syntax
   (let ((*print-pprint-dispatch* (copy-pprint-dispatch nil))
	 (*print-readably* nil)
	 (*print-escape* nil)
	 (*print-pretty* t))
     (let ((f #'(lambda (stream obj)
		  (declare (ignore obj))
		  (write "ABC" :stream stream)))
	   (g #'(lambda (stream obj)
		  (declare (ignore obj))
		  (write "DEF" :stream stream)))
	   (sym (gensym)))
       (setf (symbol-function sym) f)
       (values
	(write-to-string '|X|)
	(set-pprint-dispatch '(eql |X|) sym)
	(write-to-string '|X|)
	(progn
	  (setf (symbol-function sym) g)
	  (write-to-string '|X|))))))
  "X" nil "ABC" "DEF")
|#

;;; Error tests

(deftest pprint-dispatch.error.1
  (signals-error (let ((*print-pprint-dispatch* (copy-pprint-dispatch nil)))
		   (pprint-dispatch))
		 program-error)
  t)

(deftest pprint-dispatch.error.2
  (signals-error (let ((*print-pprint-dispatch* (copy-pprint-dispatch nil)))
		   (pprint-dispatch nil nil nil))
		 program-error)
  t)

(deftest set-pprint-dispatch.error.1
  (signals-error (let ((*print-pprint-dispatch* (copy-pprint-dispatch nil)))
		   (set-pprint-dispatch))
		 program-error)
  t)

(deftest set-pprint-dispatch.error.2
  (signals-error (let ((*print-pprint-dispatch* (copy-pprint-dispatch nil)))
		   (set-pprint-dispatch t))
		 program-error)
  t)

(deftest set-pprint-dispatch.error.3
  (signals-error (let ((table (copy-pprint-dispatch nil)))
		   (set-pprint-dispatch t 'identity 0 table nil))
		 program-error)
  t)


(deftest set-pprint-dispatch.error.4
  (loop for x in *mini-universe*
	unless (or (typep x 'real)
		   (eval `(signals-error (let ((table (copy-pprint-dispatch nil)))
					   (set-pprint-dispatch t 'identity ',x))
					 error)))
	collect x)
  nil)

(deftest set-pprint-dispatch.error.4-unsafe
  (loop for x in *mini-universe*
	unless (or (typep x 'real)
		   (eval `(signals-error (let ((table (copy-pprint-dispatch nil)))
					   (declare (optimize (safety 0)))
					   (set-pprint-dispatch t 'identity ',x))
					 error)))
	collect x)
  nil)