read-sequence.lsp 9.49 KB
Newer Older
1 2 3 4 5 6 7
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Mon Jan 19 06:55:04 2004
;;;; Contains: Tests of READ-SEQUENCE

(in-package :cl-test)

pfdietz's avatar
pfdietz committed
8 9
;;; Read into a string

pfdietz's avatar
pfdietz committed
10 11 12 13
(defmacro def-read-sequence-test (name init args input &rest expected)
  `(deftest ,name
     (let ((s ,init))
       (with-input-from-string
sds's avatar
sds committed
14 15 16 17
        (is ,input)
        (values
         (read-sequence s is ,@args)
         s)))
pfdietz's avatar
pfdietz committed
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
     ,@expected))

(def-read-sequence-test read-sequence.string.1 (copy-seq "     ")
  () "abcdefghijk" 5 "abcde")

(def-read-sequence-test read-sequence.string.2 (copy-seq "     ")
  () "abc" 3 "abc  ")

(def-read-sequence-test read-sequence.string.3 (copy-seq "     ")
  (:start 1) "abcdefghijk" 5 " abcd")

(def-read-sequence-test read-sequence.string.4 (copy-seq "     ")
  (:end 3) "abcdefghijk" 3 "abc  ")

(def-read-sequence-test read-sequence.string.5 (copy-seq "     ")
  (:start 1 :end 4) "abcdefghijk" 4 " abc ")

(def-read-sequence-test read-sequence.string.6 (copy-seq "     ")
  (:start 0 :end 0) "abcdefghijk" 0 "     ")

(def-read-sequence-test read-sequence.string.7 (copy-seq "     ")
  (:end nil) "abcdefghijk" 5 "abcde")

(def-read-sequence-test read-sequence.string.8 (copy-seq "     ")
  (:allow-other-keys nil) "abcdefghijk" 5 "abcde")

(def-read-sequence-test read-sequence.string.9 (copy-seq "     ")
  (:allow-other-keys t :foo 'bar) "abcdefghijk" 5 "abcde")

(def-read-sequence-test read-sequence.string.10 (copy-seq "     ")
  (:foo 'bar :allow-other-keys 'x) "abcdefghijk" 5 "abcde")

(def-read-sequence-test read-sequence.string.11 (copy-seq "     ")
  (:foo 'bar :allow-other-keys 'x :allow-other-keys nil)
  "abcdefghijk" 5 "abcde")

(def-read-sequence-test read-sequence.string.12 (copy-seq "     ")
  (:end 5 :end 3 :start 0 :start 1) "abcdefghijk" 5 "abcde")
pfdietz's avatar
pfdietz committed
56

pfdietz's avatar
pfdietz committed
57 58
;;; Read into a base string

pfdietz's avatar
pfdietz committed
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
(def-read-sequence-test read-sequence.base-string.1
  (make-array 5 :element-type 'base-char)
  () "abcdefghijk" 5 "abcde")

(def-read-sequence-test read-sequence.base-string.2
  (make-array 5 :element-type 'base-char :initial-element #\Space)
  () "abc" 3 "abc  ")

(def-read-sequence-test read-sequence.base-string.3
  (make-array 5 :element-type 'base-char :initial-element #\Space)
  (:start 1) "abcdefghijk" 5 " abcd")

(def-read-sequence-test read-sequence.base-string.4
  (make-array 5 :element-type 'base-char :initial-element #\Space)
  (:end 3) "abcdefghijk" 3 "abc  ")

(def-read-sequence-test read-sequence.base-string.5
  (make-array 5 :element-type 'base-char :initial-element #\Space)
  (:start 1 :end 4) "abcdefghijk" 4 " abc ")

(def-read-sequence-test read-sequence.base-string.6
  (make-array 5 :element-type 'base-char :initial-element #\Space)
  (:start 0 :end 0) "abcdefghijk" 0 "     ")

(def-read-sequence-test read-sequence.base-string.7
  (make-array 5 :element-type 'base-char :initial-element #\Space)
  (:end nil) "abcdefghijk" 5 "abcde")
pfdietz's avatar
pfdietz committed
86 87 88

;;; Read into a list

pfdietz's avatar
pfdietz committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
(def-read-sequence-test read-sequence.list.1 (make-list 5)
  () "abcdefghijk" 5 (#\a #\b #\c #\d #\e))

(def-read-sequence-test read-sequence.list.2 (make-list 5)
  () "abc" 3 (#\a #\b #\c nil nil))

(def-read-sequence-test read-sequence.list.3 (make-list 5)
  (:start 1) "abcdefghijk" 5 (nil #\a #\b #\c #\d))

(def-read-sequence-test read-sequence.list.4 (make-list 5)
  (:end 3) "abcdefghijk" 3 (#\a #\b #\c nil nil))

(def-read-sequence-test read-sequence.list.5 (make-list 5)
  (:end 4 :start 1) "abcdefghijk" 4 (nil #\a #\b #\c nil))

(def-read-sequence-test read-sequence.list.6 (make-list 5)
  (:start 0 :end 0) "abcdefghijk" 0 (nil nil nil nil nil))

(def-read-sequence-test read-sequence.list.7 (make-list 5)
  (:end nil) "abcdefghijk" 5 (#\a #\b #\c #\d #\e))
pfdietz's avatar
pfdietz committed
109 110 111

;;; Read into a vector

pfdietz's avatar
pfdietz committed
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
(def-read-sequence-test read-sequence.vector.1
  (vector nil nil nil nil nil)
  () "abcdefghijk" 5 #(#\a #\b #\c #\d #\e))

(def-read-sequence-test read-sequence.vector.2
  (vector nil nil nil nil nil)
  () "abc" 3 #(#\a #\b #\c nil nil))

(def-read-sequence-test read-sequence.vector.3
  (vector nil nil nil nil nil)
  (:start 2) "abcdefghijk" 5 #(nil nil #\a #\b #\c))

(def-read-sequence-test read-sequence.vector.4
  (vector nil nil nil nil nil)
  (:start 1 :end 4) "abcdefghijk" 4 #(nil #\a #\b #\c nil))

(def-read-sequence-test read-sequence.vector.5
  (vector nil nil nil nil nil)
  (:end 2) "abcdefghijk" 2 #(#\a #\b nil nil nil))

(def-read-sequence-test read-sequence.vector.6
  (vector nil nil nil nil nil)
  (:end 0 :start 0) "abcdefghijk" 0 #(nil nil nil nil nil))

(def-read-sequence-test read-sequence.vector.7
  (vector nil nil nil nil nil)
  (:end nil) "abcdefghijk" 5 #(#\a #\b #\c #\d #\e))

;;; Read into a vector with a fill pointer

(def-read-sequence-test read-sequence.fill-vector.1
  (make-array 10 :initial-element nil :fill-pointer 5)
  () "abcdefghijk" 5 #(#\a #\b #\c #\d #\e))

(def-read-sequence-test read-sequence.fill-vector.2
  (make-array 10 :initial-element nil :fill-pointer 5)
  () "ab" 2 #(#\a #\b nil nil nil))

(def-read-sequence-test read-sequence.fill-vector.3
  (make-array 10 :initial-element nil :fill-pointer 5)
  () "" 0 #(nil nil nil nil nil))

(def-read-sequence-test read-sequence.fill-vector.4
  (make-array 10 :initial-element nil :fill-pointer 5)
  (:start 2) "abcdefghijk" 5 #(nil nil #\a #\b #\c))

(def-read-sequence-test read-sequence.fill-vector.5
  (make-array 10 :initial-element nil :fill-pointer 5)
  (:start 1 :end 4) "abcdefghijk" 4 #(nil #\a #\b #\c nil))

(def-read-sequence-test read-sequence.fill-vector.6
  (make-array 10 :initial-element nil :fill-pointer 5)
  (:end 2) "abcdefghijk" 2 #(#\a #\b nil nil nil))

(def-read-sequence-test read-sequence.fill-vector.7
  (make-array 10 :initial-element nil :fill-pointer 5)
  (:end 0 :start 0) "abcdefghijk" 0 #(nil nil nil nil nil))

(def-read-sequence-test read-sequence.fill-vector.8
  (make-array 10 :initial-element nil :fill-pointer 5)
  (:end nil) "abcdefghijk" 5 #(#\a #\b #\c #\d #\e))
pfdietz's avatar
pfdietz committed
173

pfdietz's avatar
pfdietz committed
174 175 176 177 178 179 180 181 182 183 184 185
;;; Nil vectors

(deftest read-sequence.nil-vector.1
  :notes (:nil-vectors-are-strings)
  (let ((s (make-array 0 :element-type nil)))
    (with-input-from-string
     (is "abcde")
     (values
      (read-sequence s is)
      s)))
  0 "")

pfdietz's avatar
pfdietz committed
186 187 188 189 190
;;; Read into a bit vector

(defmacro def-read-sequence-bv-test (name init args &rest expected)
  `(deftest ,name
     ;; Create output file
191 192
     (progn
       (let (os)
sds's avatar
sds committed
193 194 195 196 197 198 199 200
         (unwind-protect
             (progn
               (setq os (open "temp.dat" :direction :output
                              :element-type '(unsigned-byte 8)
                              :if-exists :supersede))
               (loop for i in '(0 1 1 0 0 1 1 0 1 0 1 1 1 0)
                     do (write-byte i os)))
           (when os (close os))))
201
       (let (is (bv (copy-seq ,init)))
sds's avatar
sds committed
202 203 204 205 206 207 208 209
         (unwind-protect
             (progn
               (setq is (open "temp.dat" :direction :input
                              :element-type '(unsigned-byte 8)))
               (values
                (read-sequence bv is ,@args)
                bv))
           (when is (close is)))))
pfdietz's avatar
pfdietz committed
210
     ,@expected))
sds's avatar
sds committed
211

pfdietz's avatar
pfdietz committed
212 213
(def-read-sequence-bv-test read-sequence.bv.1 #*00000000000000 ()
  14 #*01100110101110)
sds's avatar
sds committed
214

pfdietz's avatar
pfdietz committed
215 216
(def-read-sequence-bv-test read-sequence.bv.2 #*00000000000000 (:start 0)
  14 #*01100110101110)
sds's avatar
sds committed
217

pfdietz's avatar
pfdietz committed
218 219
(def-read-sequence-bv-test read-sequence.bv.3 #*00000000000000 (:end 14)
  14 #*01100110101110)
sds's avatar
sds committed
220

pfdietz's avatar
pfdietz committed
221 222
(def-read-sequence-bv-test read-sequence.bv.4 #*00000000000000 (:end nil)
  14 #*01100110101110)
sds's avatar
sds committed
223

pfdietz's avatar
pfdietz committed
224 225
(def-read-sequence-bv-test read-sequence.bv.5 #*00000000000000 (:start 2)
  14 #*00011001101011)
sds's avatar
sds committed
226

pfdietz's avatar
pfdietz committed
227 228 229 230 231 232
(def-read-sequence-bv-test read-sequence.bv.6 #*00000000000000
  (:start 2 :end 13)
  13 #*00011001101010)

(def-read-sequence-bv-test read-sequence.bv.7 #*00000000000000 (:end 6)
  6 #*01100100000000)
pfdietz's avatar
pfdietz committed
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

;;; Error cases

(deftest read-sequence.error.1
  (signals-error (read-sequence) program-error)
  t)

(deftest read-sequence.error.2
  (signals-error (read-sequence (make-string 10)) program-error)
  t)

(deftest read-sequence.error.3
  (signals-error
   (read-sequence (make-string 5) (make-string-input-stream "abc") :start)
   program-error)
  t)

(deftest read-sequence.error.4
  (signals-error
   (read-sequence (make-string 5) (make-string-input-stream "abc") :foo 1)
   program-error)
  t)

(deftest read-sequence.error.5
  (signals-error
   (read-sequence (make-string 5) (make-string-input-stream "abc")
sds's avatar
sds committed
259
                  :allow-other-keys nil :bar 2)
pfdietz's avatar
pfdietz committed
260 261 262 263
   program-error)
  t)

(deftest read-sequence.error.6
pfdietz's avatar
pfdietz committed
264
  (check-type-error #'(lambda (x) (read-sequence x (make-string-input-stream "abc")))
sds's avatar
sds committed
265
                    #'sequencep)
pfdietz's avatar
pfdietz committed
266
  nil)
pfdietz's avatar
pfdietz committed
267 268 269 270 271 272 273

(deftest read-sequence.error.7
  (signals-error
   (read-sequence (cons 'a 'b) (make-string-input-stream "abc"))
   type-error)
  t)

274
;;; This test appears to cause Allegro CL to crash
pfdietz's avatar
pfdietz committed
275
(deftest read-sequence.error.8
pfdietz's avatar
pfdietz committed
276
  (signals-type-error x -1
sds's avatar
sds committed
277 278 279
                      (read-sequence (make-string 3)
                                     (make-string-input-stream "abc")
                                     :start x))
pfdietz's avatar
pfdietz committed
280
  t)
281

pfdietz's avatar
pfdietz committed
282
(deftest read-sequence.error.9
pfdietz's avatar
pfdietz committed
283
  (check-type-error #'(lambda (s)
sds's avatar
sds committed
284 285 286
                        (read-sequence (make-string 3) (make-string-input-stream "abc")
                                       :start s))
                    (typef 'unsigned-byte))
pfdietz's avatar
pfdietz committed
287
  nil)
288

pfdietz's avatar
pfdietz committed
289
(deftest read-sequence.error.10
pfdietz's avatar
pfdietz committed
290
  (signals-type-error x -1
sds's avatar
sds committed
291 292
                      (read-sequence (make-string 3) (make-string-input-stream "abc")
                                     :end x))
pfdietz's avatar
pfdietz committed
293
  t)
294

pfdietz's avatar
pfdietz committed
295
(deftest read-sequence.error.11
pfdietz's avatar
pfdietz committed
296
  (check-type-error #'(lambda (e)
sds's avatar
sds committed
297 298 299
                        (read-sequence (make-string 3) (make-string-input-stream "abc")
                                       :end e))
                    (typef '(or unsigned-byte null)))
pfdietz's avatar
pfdietz committed
300
  nil)