Debian Codegraph @

src/tests/normal-tests/ansi.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 (in-package :cl-test)
5
6 (suite 'ansi+)
7
8
9 ;; HyperSpec – 3.*
10
11 ;;;;;;;;;;;;;;;;;;;
12 ;; Deftype tests ;;
13 ;;;;;;;;;;;;;;;;;;;
14
15 (ext:with-clean-symbols (ordinary1 ordinary2)
16 (test ansi.0001.ordinary
17 (deftype ordinary1 ()
18 `(member nil t))
19
20 (deftype ordinary2 (a b)
21 (if a 'CONS `(INTEGER 0 ,b)))
22
23 (is-true (typep T 'ordinary1))
24 (is-false (typep :a 'ordinary1))
25 (is-false (typep T '(ordinary2 nil 3)))
26 (is-true (typep 3 '(ordinary2 nil 4)))
27 (is-false (typep T '(ordinary2 T nil)))
28 (is-true (typep '(1 . 2) '(ordinary2 T nil)))))
29
30 (ext:with-clean-symbols (opt)
31 (test ansi.0002.optional
32 (deftype opt (a &optional b)
33 (if a 'CONS `(INTEGER 0 ,b)))
34
35 (is-true (typep 5 '(opt nil)))
36 (is-false (typep 5 '(opt nil 4)))))
37
38 (ext:with-clean-symbols (nest)
39 (test ansi.0003.nested
40 (deftype nest ((a &optional b) c . d)
41 (assert (listp d))
42 `(member ,a ,b ,c))
43 (is-true (typep 1 '(nest (1 2) 3 4 5 6)))
44 (is-false (typep 1 '(nest (2 2) 3 4 5 6)))
45 (is-true (typep '* '(nest (3) 3)))
46 (is-true (typep 3 '(nest (2) 3)))))
47
48
49 ;;; 6. Iteration
50
51 ;;; Regression test for #605.
52 (test ansi.6.1.1.7-destructuring
53 (finishes
54 (loop with (a b) = '(1)
55 do (return (list a b))))
56 (finishes
57 (loop with (a b . rest) = '(1)
58 do (return (list a b rest))))
59 (is-equal '(1 nil 2 nil)
60 (loop with (a b) = '(1)
61 for (c d) = '(2)
62 do (return (list a b c d))))
63 (is-equal '(1 nil 2 nil nil)
64 (loop with (a b . rest) = '(1)
65 for (c d) = '(2)
66 do (return (list a b c d rest))))
67 (is-equal '(1 2 nil)
68 (loop for (a (b) ((c))) ='(1 (2))
69 do (return (list a b c))))
70 (signals error
71 (loop for (a (b)) ='(1 2)
72 do (return (list a b)))))
73
74
75 ;;; 8. Structures
76 (ext:with-clean-symbols
77 (my-struct make-my-struct my-struct-2 make-my-struct-2 my-struct-compatible-type)
78 (test ansi.8.redefine-compatible
79 (let (foo-1 foo-2 foo-3 foo-4)
80 (defstruct (my-struct (:constructor make-my-struct)) slot-1 slot-2)
81 (setq foo-1 (make-my-struct :slot-1 3 :slot-2 4))
82 (finishes (defstruct (my-struct (:constructor make-my-struct))
83 (slot-1 nil)
84 (slot-2 t)))
85 (setq foo-2 (make-my-struct :slot-1 3 :slot-2 4))
86 (finishes (defstruct (my-struct (:constructor make-my-struct))
87 (slot-1 3)
88 (slot-2 4)))
89 (setq foo-3 (make-my-struct))
90 (finishes (defstruct (my-struct (:constructor make-my-struct))
91 (slot-1 8 :type t :read-only nil)
92 (slot-2 8 :type t :read-only nil)))
93 (setq foo-4 (make-my-struct :slot-1 3 :slot-2 4))
94 (is (equalp foo-1 foo-2))
95 (is (equalp foo-2 foo-3))
96 (is (equalp foo-3 foo-4)))
97 (deftype my-struct-compatible-type () `(integer 0 10))
98 (defstruct (my-struct-2 (:constructor make-my-struct-2))
99 (slot-1 nil :type my-struct-compatible-type :read-only t))
100 (finishes
101 (defstruct my-struct-2
102 (slot-1 nil :type (integer 0 10) :read-only t)))
103 (finishes
104 (defstruct my-struct-2
105 (slot-1 4 :type (integer 0 10) :read-only t)))
106 (finishes
107 (defstruct my-struct-2
108 (slot-1 4 :type (integer 0 10) :read-only nil)))))
109
110 (ext:with-clean-symbols (my-struct make-my-struct)
111 (test ansi.8.redefine-incompatible
112 (defstruct (my-struct (:constructor make-my-struct)) slot-1 slot-2)
113 ;; different slot type
114 (signals error (defstruct (my-struct (:constructor make-my-struct))
115 (slot-1 nil :type integer)
116 (slot-2 t)))
117 ;; too many slots
118 (signals error (defstruct (my-struct (:constructor make-my-struct)) slot-1 slot-2 slot-3))
119 ;; too few slots
120 (signals error (defstruct (my-struct (:constructor make-my-struct)) slot-1))
121 (finishes (make-my-struct))))
122
123
124
125 ;;;;;;;;;;;;;;;;;;;;;;;;;;
126 ;; 12.2.* Numbers tests ;;
127 ;;;;;;;;;;;;;;;;;;;;;;;;;;
128 (test ansi.12.2.incf
129 (let ((foo 0)
130 (bar 0))
131 (flet ((inc () (incf foo)))
132 (incf (the fixnum bar) (inc)))
133 (is (= foo 1))))
134
135 ;;;;;;;;;;;;;;;;;;;;;;;;;
136 ;; 19.* Pathname tests ;;
137 ;;;;;;;;;;;;;;;;;;;;;;;;;
138
139 ;; Issue #103 ;; logical-pathname-translations not translating
140 ;; https://gitlab.com/embeddable-common-lisp/ecl/issues/103
141 (test ansi.0004.wildcards
142 (setf (logical-pathname-translations "prog")
143 '(("CODE;*.*.*" "/tmp/prog/")))
144 (is (equal
145 (namestring (translate-logical-pathname "prog:code;documentation.lisp"))
146 "/tmp/prog/documentation.lisp")))
147
148 ;; Issue #351 ;; (probe-file #P"/") maybe shouldn't return nil
149 ;; https://gitlab.com/embeddable-common-lisp/ecl/issues/351
150 #-windows
151 (test ansi.19.probe-file
152 (is (probe-file #P"/"))
153 (is (probe-file #P"/tmp"))
154 (is (probe-file #P"/tmp/")))
155
156
157 ;;;;;;;;;;;;;;;;;;;;;;;;;
158 ;; 21.2 Stream tests ;;
159 ;;;;;;;;;;;;;;;;;;;;;;;;;
160
161 ;;; Issue #452: broadcast-streams
162 ;;; FILE-STRING-LENGTH for broadcast-streams (non-empty) broken
163 ;;; https://gitlab.com/embeddable-common-lisp/ecl/issues/452
164 ;;;
165 ;;; We also fix the non-conformance where we pick the first (not the last)
166 ;;; component when we delegate question.
167 (test ansi.21-2.broadcast-non-empty
168 (ensure-directories-exist *tmp-dir*)
169 (let ((path (merge-pathnames "sxx.txt" *tmp-dir*)))
170 (with-open-file (sxx path :direction :output :if-exists :supersede :if-does-not-exist :create)
171 (let ((broadcast (make-broadcast-stream sxx)))
172 (finishes (file-position broadcast))
173 (finishes (file-length broadcast))
174 (finishes (file-string-length broadcast "jd"))))))
175
176 (test ansi.21-2.file-string-length=nil
177 (let ((stream (make-string-output-stream)))
178 (finishes (file-position stream))
179 (signals error (file-length stream))
180 ;; Undefined behavior, should either signal error (because stream is not
181 ;; file output character stream), return NIL (because can't be determined)
182 ;; or return integer (because progress can be determined).
183 #+ (or) (signals error (file-string-length stream "jd"))
184 #+ (or) (is (typep (file-string-length stream "jd") '(or null integer)))))
185
186 ;;; file-* should be passed to the /last/ component.
187 (test ansi.21-2.last-component
188 (ensure-directories-exist *tmp-dir*)
189 (let ((first-stream (make-string-output-stream)))
190 (with-open-file (last-stream (merge-pathnames "ss.txt" *tmp-dir*)
191 :direction :output
192 :if-exists :supersede
193 :if-does-not-exist :create)
194 (format last-stream "Hello world!~%")
195 (finish-output last-stream) ; for buffered streams
196 (is (= (file-length last-stream) 13))
197 (let ((broadcast (make-broadcast-stream first-stream last-stream)))
198 (is (= 13 (file-length broadcast) (file-length last-stream)))
199 (is (= 13 (file-position broadcast) (file-position last-stream)))
200 (is (= 2
201 (file-string-length broadcast "jd")
202 (file-string-length last-stream "jd")))))))
203
204
205 ;;;;;;;;;;;;;;;;;;;;;;;;;
206 ;; 22.* Format tests ;;
207 ;;;;;;;;;;;;;;;;;;;;;;;;;
208 (defun cl-user::fmt (stream argument colonp atsignp &rest params)
209 (declare (ignore argument colonp atsignp))
210 (format stream "~S~%" params))
211
212 ;;; Tests for CDR 7
213 ;;; See: https://common-lisp.net/project/cdr/document/7/index.html
214 (test ansi.22.cdr-7
215 ;; trailing commas
216 (let ((expected (format nil "(1 2)~%")))
217 (is-equal expected (format nil "~1,2/fmt/" t))
218 (is-equal expected (format nil "~1,2,/fmt/" t))
219 (is-equal expected (format nil "~1,2:/fmt/" t))
220 (is-equal expected (format nil "~1,2,:/fmt/" t)))
221 ;; final V parameters
222 (let ((expected-1 (format nil "(1 T)~%"))
223 (expected-2 (format nil "(1 NIL)~%")))
224 (is-equal expected-1 (format nil "~1,v/fmt/" t t))
225 (is-equal expected-1 (format nil "~1,v,/fmt/" t t))
226 (is-equal expected-1 (format nil "~1,v:/fmt/" t t))
227 (is-equal expected-1 (format nil "~1,v,:/fmt/" t t))
228
229 (is-equal expected-2 (format nil "~1,v/fmt/" nil t))
230 (is-equal expected-2 (format nil "~1,v,/fmt/" nil t))
231 (is-equal expected-2 (format nil "~1,v:/fmt/" nil t))
232 (is-equal expected-2 (format nil "~1,v,:/fmt/" nil t))))
233
234 ;;; Tests for correct rounding in ~e directive with k parameter
235 (test ansi.22.format-e
236 (is-equal (format nil "~,2,,-1e" 0.9) "0.09e+1")
237 (is-equal (format nil "~,2,,-1e" 0.99) "0.01e+2")
238 (is-equal (format nil "~,1,,0e" 0.9) "0.9e+0")
239 (is-equal (format nil "~,1,,0e" 0.99) "0.1e+1")
240 (is-equal (format nil "~,1,,1e" 9.9) "9.9e+0")
241 (is-equal (format nil "~,1,,1e" 9.99) "1.0e+1")
242 (is-equal (format nil "~6e" 9.9) "9.9e+0")
243 (is-equal (format nil "~6e" 9.99) "1.0e+1"))
244
245
246 ;;;;;;;;;;;;;;;;;;;;;;;
247 ;; 23.* Reader tests ;;
248 ;;;;;;;;;;;;;;;;;;;;;;;
249 (progn
250 (defstruct example-struct a)
251 (test ansi.0005.sharp-s-reader
252 (finishes
253 (read-from-string
254 "(#1=\"Hello\" #S(cl-test::example-struct :A #1#))"))))
255
256