source: trunk/source/tests/ansi-tests/read-sequence.lsp @ 8991

Last change on this file since 8991 was 8991, checked in by gz, 11 years ago

Check in the gcl ansi test suite (original, in preparation for making local changes)

File size: 9.0 KB
Line 
1;-*- Mode:     Lisp -*-
2;;;; Author:   Paul Dietz
3;;;; Created:  Mon Jan 19 06:55:04 2004
4;;;; Contains: Tests of READ-SEQUENCE
5
6(in-package :cl-test)
7
8;;; Read into a string
9
10(defmacro def-read-sequence-test (name init args input &rest expected)
11  `(deftest ,name
12     (let ((s ,init))
13       (with-input-from-string
14        (is ,input)
15        (values
16         (read-sequence s is ,@args)
17         s)))
18     ,@expected))
19
20(def-read-sequence-test read-sequence.string.1 (copy-seq "     ")
21  () "abcdefghijk" 5 "abcde")
22
23(def-read-sequence-test read-sequence.string.2 (copy-seq "     ")
24  () "abc" 3 "abc  ")
25
26(def-read-sequence-test read-sequence.string.3 (copy-seq "     ")
27  (:start 1) "abcdefghijk" 5 " abcd")
28
29(def-read-sequence-test read-sequence.string.4 (copy-seq "     ")
30  (:end 3) "abcdefghijk" 3 "abc  ")
31
32(def-read-sequence-test read-sequence.string.5 (copy-seq "     ")
33  (:start 1 :end 4) "abcdefghijk" 4 " abc ")
34
35(def-read-sequence-test read-sequence.string.6 (copy-seq "     ")
36  (:start 0 :end 0) "abcdefghijk" 0 "     ")
37
38(def-read-sequence-test read-sequence.string.7 (copy-seq "     ")
39  (:end nil) "abcdefghijk" 5 "abcde")
40
41(def-read-sequence-test read-sequence.string.8 (copy-seq "     ")
42  (:allow-other-keys nil) "abcdefghijk" 5 "abcde")
43
44(def-read-sequence-test read-sequence.string.9 (copy-seq "     ")
45  (:allow-other-keys t :foo 'bar) "abcdefghijk" 5 "abcde")
46
47(def-read-sequence-test read-sequence.string.10 (copy-seq "     ")
48  (:foo 'bar :allow-other-keys 'x) "abcdefghijk" 5 "abcde")
49
50(def-read-sequence-test read-sequence.string.11 (copy-seq "     ")
51  (:foo 'bar :allow-other-keys 'x :allow-other-keys nil)
52  "abcdefghijk" 5 "abcde")
53
54(def-read-sequence-test read-sequence.string.12 (copy-seq "     ")
55  (:end 5 :end 3 :start 0 :start 1) "abcdefghijk" 5 "abcde")
56
57;;; Read into a base string
58
59(def-read-sequence-test read-sequence.base-string.1
60  (make-array 5 :element-type 'base-char)
61  () "abcdefghijk" 5 "abcde")
62
63(def-read-sequence-test read-sequence.base-string.2
64  (make-array 5 :element-type 'base-char :initial-element #\Space)
65  () "abc" 3 "abc  ")
66
67(def-read-sequence-test read-sequence.base-string.3
68  (make-array 5 :element-type 'base-char :initial-element #\Space)
69  (:start 1) "abcdefghijk" 5 " abcd")
70
71(def-read-sequence-test read-sequence.base-string.4
72  (make-array 5 :element-type 'base-char :initial-element #\Space)
73  (:end 3) "abcdefghijk" 3 "abc  ")
74
75(def-read-sequence-test read-sequence.base-string.5
76  (make-array 5 :element-type 'base-char :initial-element #\Space)
77  (:start 1 :end 4) "abcdefghijk" 4 " abc ")
78
79(def-read-sequence-test read-sequence.base-string.6
80  (make-array 5 :element-type 'base-char :initial-element #\Space)
81  (:start 0 :end 0) "abcdefghijk" 0 "     ")
82
83(def-read-sequence-test read-sequence.base-string.7
84  (make-array 5 :element-type 'base-char :initial-element #\Space)
85  (:end nil) "abcdefghijk" 5 "abcde")
86
87;;; Read into a list
88
89(def-read-sequence-test read-sequence.list.1 (make-list 5)
90  () "abcdefghijk" 5 (#\a #\b #\c #\d #\e))
91
92(def-read-sequence-test read-sequence.list.2 (make-list 5)
93  () "abc" 3 (#\a #\b #\c nil nil))
94
95(def-read-sequence-test read-sequence.list.3 (make-list 5)
96  (:start 1) "abcdefghijk" 5 (nil #\a #\b #\c #\d))
97
98(def-read-sequence-test read-sequence.list.4 (make-list 5)
99  (:end 3) "abcdefghijk" 3 (#\a #\b #\c nil nil))
100
101(def-read-sequence-test read-sequence.list.5 (make-list 5)
102  (:end 4 :start 1) "abcdefghijk" 4 (nil #\a #\b #\c nil))
103
104(def-read-sequence-test read-sequence.list.6 (make-list 5)
105  (:start 0 :end 0) "abcdefghijk" 0 (nil nil nil nil nil))
106
107(def-read-sequence-test read-sequence.list.7 (make-list 5)
108  (:end nil) "abcdefghijk" 5 (#\a #\b #\c #\d #\e))
109
110;;; Read into a vector
111
112(def-read-sequence-test read-sequence.vector.1
113  (vector nil nil nil nil nil)
114  () "abcdefghijk" 5 #(#\a #\b #\c #\d #\e))
115
116(def-read-sequence-test read-sequence.vector.2
117  (vector nil nil nil nil nil)
118  () "abc" 3 #(#\a #\b #\c nil nil))
119
120(def-read-sequence-test read-sequence.vector.3
121  (vector nil nil nil nil nil)
122  (:start 2) "abcdefghijk" 5 #(nil nil #\a #\b #\c))
123
124(def-read-sequence-test read-sequence.vector.4
125  (vector nil nil nil nil nil)
126  (:start 1 :end 4) "abcdefghijk" 4 #(nil #\a #\b #\c nil))
127
128(def-read-sequence-test read-sequence.vector.5
129  (vector nil nil nil nil nil)
130  (:end 2) "abcdefghijk" 2 #(#\a #\b nil nil nil))
131
132(def-read-sequence-test read-sequence.vector.6
133  (vector nil nil nil nil nil)
134  (:end 0 :start 0) "abcdefghijk" 0 #(nil nil nil nil nil))
135
136(def-read-sequence-test read-sequence.vector.7
137  (vector nil nil nil nil nil)
138  (:end nil) "abcdefghijk" 5 #(#\a #\b #\c #\d #\e))
139
140;;; Read into a vector with a fill pointer
141
142(def-read-sequence-test read-sequence.fill-vector.1
143  (make-array 10 :initial-element nil :fill-pointer 5)
144  () "abcdefghijk" 5 #(#\a #\b #\c #\d #\e))
145
146(def-read-sequence-test read-sequence.fill-vector.2
147  (make-array 10 :initial-element nil :fill-pointer 5)
148  () "ab" 2 #(#\a #\b nil nil nil))
149
150(def-read-sequence-test read-sequence.fill-vector.3
151  (make-array 10 :initial-element nil :fill-pointer 5)
152  () "" 0 #(nil nil nil nil nil))
153
154(def-read-sequence-test read-sequence.fill-vector.4
155  (make-array 10 :initial-element nil :fill-pointer 5)
156  (:start 2) "abcdefghijk" 5 #(nil nil #\a #\b #\c))
157
158(def-read-sequence-test read-sequence.fill-vector.5
159  (make-array 10 :initial-element nil :fill-pointer 5)
160  (:start 1 :end 4) "abcdefghijk" 4 #(nil #\a #\b #\c nil))
161
162(def-read-sequence-test read-sequence.fill-vector.6
163  (make-array 10 :initial-element nil :fill-pointer 5)
164  (:end 2) "abcdefghijk" 2 #(#\a #\b nil nil nil))
165
166(def-read-sequence-test read-sequence.fill-vector.7
167  (make-array 10 :initial-element nil :fill-pointer 5)
168  (:end 0 :start 0) "abcdefghijk" 0 #(nil nil nil nil nil))
169
170(def-read-sequence-test read-sequence.fill-vector.8
171  (make-array 10 :initial-element nil :fill-pointer 5)
172  (:end nil) "abcdefghijk" 5 #(#\a #\b #\c #\d #\e))
173
174;;; Nil vectors
175
176(deftest read-sequence.nil-vector.1
177  :notes (:nil-vectors-are-strings)
178  (let ((s (make-array 0 :element-type nil)))
179    (with-input-from-string
180     (is "abcde")
181     (values
182      (read-sequence s is)
183      s)))
184  0 "")
185
186;;; Read into a bit vector
187
188(defmacro def-read-sequence-bv-test (name init args &rest expected)
189  `(deftest ,name
190     ;; Create output file
191     (progn
192       (let (os)
193         (unwind-protect
194             (progn
195               (setq os (open "temp.dat" :direction :output
196                              :element-type '(unsigned-byte 8)
197                              :if-exists :supersede))
198               (loop for i in '(0 1 1 0 0 1 1 0 1 0 1 1 1 0)
199                     do (write-byte i os)))
200           (when os (close os))))
201       (let (is (bv (copy-seq ,init)))
202         (unwind-protect
203             (progn
204               (setq is (open "temp.dat" :direction :input
205                              :element-type '(unsigned-byte 8)))
206               (values
207                (read-sequence bv is ,@args)
208                bv))
209           (when is (close is)))))
210     ,@expected))
211     
212(def-read-sequence-bv-test read-sequence.bv.1 #*00000000000000 ()
213  14 #*01100110101110)
214 
215(def-read-sequence-bv-test read-sequence.bv.2 #*00000000000000 (:start 0)
216  14 #*01100110101110)
217 
218(def-read-sequence-bv-test read-sequence.bv.3 #*00000000000000 (:end 14)
219  14 #*01100110101110)
220 
221(def-read-sequence-bv-test read-sequence.bv.4 #*00000000000000 (:end nil)
222  14 #*01100110101110)
223 
224(def-read-sequence-bv-test read-sequence.bv.5 #*00000000000000 (:start 2)
225  14 #*00011001101011)
226 
227(def-read-sequence-bv-test read-sequence.bv.6 #*00000000000000
228  (:start 2 :end 13)
229  13 #*00011001101010)
230
231(def-read-sequence-bv-test read-sequence.bv.7 #*00000000000000 (:end 6)
232  6 #*01100100000000)
233
234;;; Error cases
235
236(deftest read-sequence.error.1
237  (signals-error (read-sequence) program-error)
238  t)
239
240(deftest read-sequence.error.2
241  (signals-error (read-sequence (make-string 10)) program-error)
242  t)
243
244(deftest read-sequence.error.3
245  (signals-error
246   (read-sequence (make-string 5) (make-string-input-stream "abc") :start)
247   program-error)
248  t)
249
250(deftest read-sequence.error.4
251  (signals-error
252   (read-sequence (make-string 5) (make-string-input-stream "abc") :foo 1)
253   program-error)
254  t)
255
256(deftest read-sequence.error.5
257  (signals-error
258   (read-sequence (make-string 5) (make-string-input-stream "abc")
259                  :allow-other-keys nil :bar 2)
260   program-error)
261  t)
262
263(deftest read-sequence.error.6
264  (check-type-error #'(lambda (x) (read-sequence x (make-string-input-stream "abc")))
265                    #'sequencep)
266  nil)
267
268(deftest read-sequence.error.7
269  (signals-error
270   (read-sequence (cons 'a 'b) (make-string-input-stream "abc"))
271   type-error)
272  t)
273
274;;; This test appears to cause Allegro CL to crash
275(deftest read-sequence.error.8
276  (signals-type-error x -1
277                      (read-sequence (make-string 3)
278                                     (make-string-input-stream "abc")
279                                     :start x))
280  t)
281
282(deftest read-sequence.error.9
283  (check-type-error #'(lambda (s)
284                        (read-sequence (make-string 3) (make-string-input-stream "abc")
285                                       :start s))
286                    (typef 'unsigned-byte))
287  nil)
288
289(deftest read-sequence.error.10
290  (signals-type-error x -1
291                      (read-sequence (make-string 3) (make-string-input-stream "abc")
292                                     :end x))
293  t)
294
295(deftest read-sequence.error.11
296  (check-type-error #'(lambda (e)
297                        (read-sequence (make-string 3) (make-string-input-stream "abc")
298                                       :end e))
299                    (typef '(or unsigned-byte null)))
300  nil)
Note: See TracBrowser for help on using the repository browser.