print-vector.lsp 13.7 KB
Newer Older
1 2 3 4 5
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Tue Apr 20 22:36:53 2004
;;;; Contains: Tests of vector printing

pfdietz's avatar
pfdietz committed
6 7
(compile-and-load "printer-aux.lsp")

8 9
(in-package :cl-test)

pfdietz's avatar
pfdietz committed
10 11
;;; Empty vector tests

12 13 14 15 16 17 18 19
(deftest print.vector.1
  (with-standard-io-syntax
   (write-to-string #() :readably nil :array t))
  "#()")

(deftest print.vector.2
  (with-standard-io-syntax
   (loop for i from 2 to 100
sds's avatar
sds committed
20 21 22 23
         for a = (make-array '(0) :element-type `(unsigned-byte ,i))
         for s = (write-to-string a :readably nil :array t :pretty nil)
         unless (string= s "#()")
         collect (list i s)))
24 25 26 27 28
  nil)

(deftest print.vector.3
  (with-standard-io-syntax
   (loop for i from 1 to 100
sds's avatar
sds committed
29 30 31 32
         for a = (make-array '(0) :element-type `(signed-byte ,i))
         for s = (write-to-string a :readably nil :array t :pretty nil)
         unless (string= s "#()")
         collect (list i s)))
33 34 35 36 37
  nil)

(deftest print.vector.4
  (with-standard-io-syntax
   (loop for type in '(short-float single-float double-float long-float)
sds's avatar
sds committed
38 39 40 41
         for a = (make-array '(0) :element-type type)
         for s = (write-to-string a :readably nil :array t :pretty nil)
         unless (string= s "#()")
         collect (list type s)))
42
  nil)
pfdietz's avatar
pfdietz committed
43 44 45 46 47 48

;;; Nonempty vectors

(deftest print.vector.5
  (with-standard-io-syntax
   (let* ((*package* (find-package "CL-TEST"))
sds's avatar
sds committed
49 50 51 52
          (result
           (write-to-string #(a b c)
                            :readably nil :array t
                            :pretty nil :case :downcase)))
pfdietz's avatar
pfdietz committed
53
     (or (and (string= result "#(a b c)") t)
sds's avatar
sds committed
54
         result)))
pfdietz's avatar
pfdietz committed
55 56 57 58 59 60 61
  t)

(deftest print.vector.6
  (with-standard-io-syntax
   (loop
    for i from 2 to 100
    for a = (make-array '(4) :element-type `(unsigned-byte ,i)
sds's avatar
sds committed
62
                        :initial-contents '(3 0 2 1))
pfdietz's avatar
pfdietz committed
63 64 65 66 67 68 69 70 71 72
    for s = (write-to-string a :readably nil :array t :pretty nil)
    unless (string= s "#(3 0 2 1)")
    collect (list i a s)))
  nil)

(deftest print.vector.7
  (with-standard-io-syntax
   (loop
    for i from 2 to 100
    for a = (make-array '(4) :element-type `(signed-byte ,i)
sds's avatar
sds committed
73
                        :initial-contents '(-2 -1 0 1))
pfdietz's avatar
pfdietz committed
74 75 76 77 78
    for s = (write-to-string a :readably nil :array t :pretty nil)
    unless (string= s "#(-2 -1 0 1)")
    collect (list i a s)))
  nil)

pfdietz's avatar
pfdietz committed
79 80 81 82 83
;;; Vectors with fill pointers

(deftest print.vector.fill.1
  (with-standard-io-syntax
   (let ((v (make-array '(10) :initial-contents '(a b c d e f g h i j)
sds's avatar
sds committed
84 85
                        :fill-pointer 0))
         (*package* (find-package "CL-TEST")))
pfdietz's avatar
pfdietz committed
86
     (loop for i from 0 to 10
sds's avatar
sds committed
87 88 89
           do (setf (fill-pointer v) i)
           collect (write-to-string v :readably nil :array t :pretty nil
                                    :case :downcase))))
pfdietz's avatar
pfdietz committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  ("#()"
   "#(a)"
   "#(a b)"
   "#(a b c)"
   "#(a b c d)"
   "#(a b c d e)"
   "#(a b c d e f)"
   "#(a b c d e f g)"
   "#(a b c d e f g h)"
   "#(a b c d e f g h i)"
   "#(a b c d e f g h i j)"))

(deftest print.vector.fill.2
  (with-standard-io-syntax
   (let ((expected '("#()" "#(0)" "#(0 1)" "#(0 1 2)" "#(0 1 2 3)")))
     (loop for i from 2 to 100
sds's avatar
sds committed
106 107 108 109 110 111 112 113 114 115 116 117
           nconc
           (let ((v (make-array '(4) :initial-contents '(0 1 2 3)
                                :element-type `(unsigned-byte ,i)
                                :fill-pointer 0)))
             (loop for fp from 0 to 4
                   for expected-result in expected
                   for actual-result =
                   (progn
                     (setf (fill-pointer v) fp)
                     (write-to-string v :readably nil :array t :pretty nil))
                   unless (string= expected-result actual-result)
                   collect (list i fp expected-result actual-result))))))
pfdietz's avatar
pfdietz committed
118 119 120 121 122 123
  nil)

(deftest print.vector.fill.3
  (with-standard-io-syntax
   (let ((expected '("#()" "#(0)" "#(0 -1)" "#(0 -1 -2)" "#(0 -1 -2 1)")))
     (loop for i from 2 to 100
sds's avatar
sds committed
124 125 126 127 128 129 130 131 132 133 134 135
           nconc
           (let ((v (make-array '(4) :initial-contents '(0 -1 -2 1)
                                :element-type `(signed-byte ,i)
                                :fill-pointer 0)))
             (loop for fp from 0 to 4
                   for expected-result in expected
                   for actual-result =
                   (progn
                     (setf (fill-pointer v) fp)
                     (write-to-string v :readably nil :array t :pretty nil))
                   unless (string= expected-result actual-result)
                   collect (list i fp expected-result actual-result))))))
pfdietz's avatar
pfdietz committed
136 137 138 139 140 141
  nil)

;;; Displaced vectors

(deftest print.vector.displaced.1
  (let* ((v1 (vector 'a 'b 'c 'd 'e 'f 'g))
sds's avatar
sds committed
142
         (v2 (make-array 3 :displaced-to v1 :displaced-index-offset 4)))
pfdietz's avatar
pfdietz committed
143 144
    (with-standard-io-syntax
     (write-to-string v2 :readably nil :array t :case :downcase :pretty nil
sds's avatar
sds committed
145
                      :escape nil)))
pfdietz's avatar
pfdietz committed
146 147 148 149 150
  "#(e f g)")

(deftest print.vector.displaced.2
  (with-standard-io-syntax
   (loop for i from 2 to 100
sds's avatar
sds committed
151 152 153 154 155 156 157 158 159 160 161
         nconc
         (let* ((type `(unsigned-byte ,i))
                (v1 (make-array 8 :element-type type
                                :initial-contents '(0 1 2 3 0 1 2 3)))
                (v2 (make-array 5 :displaced-to v1
                                :displaced-index-offset 2
                                :element-type type))
                (result
                 (write-to-string v2 :readably nil :array t :pretty nil)))
           (unless (string= result "#(2 3 0 1 2)")
             (list (list i v1 v2 result))))))
pfdietz's avatar
pfdietz committed
162 163 164 165 166 167
  nil)


(deftest print.vector.displaced.3
  (with-standard-io-syntax
   (loop for i from 2 to 100
sds's avatar
sds committed
168 169 170 171 172 173 174 175 176 177 178
         nconc
         (let* ((type `(signed-byte ,i))
                (v1 (make-array 8 :element-type type
                                :initial-contents '(0 1 -1 -2 0 1 -1 -2)))
                (v2 (make-array 5 :displaced-to v1
                                :displaced-index-offset 2
                                :element-type type))
                (result
                 (write-to-string v2 :readably nil :array t :pretty nil)))
           (unless (string= result "#(-1 -2 0 1 -1)")
             (list (list i v1 v2 result))))))
pfdietz's avatar
pfdietz committed
179 180 181 182 183 184 185 186
  nil)


;;; Adjustable vectors

(deftest print.vector.adjustable.1
  (with-standard-io-syntax
   (let ((v (make-array '(10) :initial-contents '(a b c d e f g h i j)
sds's avatar
sds committed
187
                        :adjustable t)))
pfdietz's avatar
pfdietz committed
188
     (write-to-string v :readably nil :array t :case :downcase :pretty nil
sds's avatar
sds committed
189
                      :escape nil)))
pfdietz's avatar
pfdietz committed
190 191 192 193 194
  "#(a b c d e f g h i j)")

(deftest print.vector.adjustable.2
  (with-standard-io-syntax
   (loop for i from 2 to 100
sds's avatar
sds committed
195 196 197 198 199 200 201 202
         for type = `(unsigned-byte ,i)
         for v = (make-array '(8) :initial-contents '(0 1 2 3 3 0 2 1)
                             :adjustable t)
         for s =
         (write-to-string v :readably nil :array t :case :downcase :pretty nil
                          :escape nil)
         unless (string= s "#(0 1 2 3 3 0 2 1)")
         collect (list i v s)))
pfdietz's avatar
pfdietz committed
203 204 205 206 207
  nil)

(deftest print.vector.adjustable.3
  (with-standard-io-syntax
   (loop for i from 2 to 100
sds's avatar
sds committed
208 209 210 211 212 213 214 215
         for type = `(signed-byte ,i)
         for v = (make-array '(8) :initial-contents '(0 1 -1 -2 -1 0 -2 1)
                             :adjustable t)
         for s =
         (write-to-string v :readably nil :array t :case :downcase :pretty nil
                          :escape nil)
         unless (string= s "#(0 1 -1 -2 -1 0 -2 1)")
         collect (list i v s)))
pfdietz's avatar
pfdietz committed
216 217 218 219 220 221 222 223 224 225 226 227
  nil)

;;; Printing with *print-array* and *print-readably* bound to nil

(deftest print.vector.unreadable.1
  (with-standard-io-syntax
   (subseq (write-to-string #(a b c d e) :array nil :readably nil) 0 2))
  "#<")

(deftest print.vector.unreadable.2
  (with-standard-io-syntax
   (loop for i from 2 to 100
sds's avatar
sds committed
228 229 230 231 232 233
         for type = `(unsigned-byte ,i)
         for v = (make-array '(4) :element-type type
                             :initial-contents '(0 1 2 3))
         for result = (write-to-string v :array nil :readably nil)
         unless (string= (subseq result 0 2) "#<")
         collect (list i type v result)))
pfdietz's avatar
pfdietz committed
234 235 236 237 238 239
  nil)


(deftest print.vector.unreadable.3
  (with-standard-io-syntax
   (loop for i from 2 to 100
sds's avatar
sds committed
240 241 242 243 244 245
         for type = `(signed-byte ,i)
         for v = (make-array '(4) :element-type type
                             :initial-contents '(0 1 -2 -1))
         for result = (write-to-string v :array nil :readably nil)
         unless (string= (subseq result 0 2) "#<")
         collect (list i type v result)))
pfdietz's avatar
pfdietz committed
246 247 248 249 250
  nil)

;;; Readability tests

(deftest print.vector.random.1
pfdietz's avatar
pfdietz committed
251 252
  (trim-list
   (loop for v in *universe*
sds's avatar
sds committed
253 254 255 256 257 258
         when (vectorp v)
         nconc
         (loop repeat 10
               nconc (randomly-check-readability
                      v :test #'equalp
                      :can-fail (not (subtypep t (array-element-type v))))))
pfdietz's avatar
pfdietz committed
259
   10)
pfdietz's avatar
pfdietz committed
260 261 262
  nil)

(deftest print.vector.random.2
pfdietz's avatar
pfdietz committed
263 264
  (trim-list
   (loop for i from 2 to 100
sds's avatar
sds committed
265 266 267 268 269 270 271
         for type = `(unsigned-byte ,i)
         for v = (make-array '(4) :element-type type
                             :initial-contents '(1 3 2 0))
         nconc
         (loop repeat 10
               nconc (randomly-check-readability v :test #'equalp
                                                 :can-fail t)))
pfdietz's avatar
pfdietz committed
272
   10)
pfdietz's avatar
pfdietz committed
273 274 275
  nil)

(deftest print.vector.random.3
pfdietz's avatar
pfdietz committed
276 277
  (trim-list
   (loop for i from 2 to 100
sds's avatar
sds committed
278 279 280 281 282 283 284
         for type = `(signed-byte ,i)
         for v = (make-array '(4) :element-type type
                             :initial-contents '(-1 1 0 -2))
         nconc
         (loop repeat 10
               nconc (randomly-check-readability v :test #'equalp
                                                 :can-fail t)))
pfdietz's avatar
pfdietz committed
285
   10)
pfdietz's avatar
pfdietz committed
286 287
  nil)

pfdietz's avatar
pfdietz committed
288
(deftest print.vector.random.4
pfdietz's avatar
pfdietz committed
289 290
  (trim-list
   (loop for v = (make-random-vector (1+ (random 100)))
sds's avatar
sds committed
291 292
         repeat 1000
         nconc (randomly-check-readability v :test #'equalp))
pfdietz's avatar
pfdietz committed
293
   10)
pfdietz's avatar
pfdietz committed
294 295
  nil)

pfdietz's avatar
pfdietz committed
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
;;; *print-length* checks

(deftest print.vector.length.1
  (with-standard-io-syntax
   (write-to-string #() :pretty nil :length 0 :readably nil))
  "#()")

(deftest print.vector.length.2
  (with-standard-io-syntax
   (write-to-string #(1) :pretty nil :length 0 :readably nil))
  "#(...)")

(deftest print.vector.length.3
  (with-standard-io-syntax
   (write-to-string #(1) :pretty nil :length 1 :readably nil))
  "#(1)")

(deftest print.vector.length.4
  (with-standard-io-syntax
   (write-to-string #(a b c d e f g h)
sds's avatar
sds committed
316 317 318 319
                    :pretty nil
                    :array t :escape nil
                    :length 5 :case :downcase
                    :readably nil))
pfdietz's avatar
pfdietz committed
320 321 322 323 324
  "#(a b c d e ...)")

(deftest print.vector.length.5
  (with-standard-io-syntax
   (loop for i from 2 to 100
sds's avatar
sds committed
325 326 327 328 329 330 331
         for type = `(unsigned-byte ,i)
         for v = (make-array '(0) :element-type type)
         for result = (write-to-string v :array t :readably nil
                                       :pretty nil
                                       :length 0)
         unless (string= result "#()")
         collect (list i type v result)))
pfdietz's avatar
pfdietz committed
332 333 334 335 336
  nil)

(deftest print.vector.length.6
  (with-standard-io-syntax
   (loop for i from 2 to 100
sds's avatar
sds committed
337 338 339 340 341 342 343 344 345
         for type = `(unsigned-byte ,i)
         for v = (make-array '(1) :element-type type :initial-contents '(2))
         for result = (write-to-string v
                                       :pretty nil
                                       :array t
                                       :readably nil
                                       :length 0)
         unless (string= result "#(...)")
         collect (list i type v result)))
pfdietz's avatar
pfdietz committed
346 347 348 349 350
  nil)

(deftest print.vector.length.7
  (with-standard-io-syntax
   (loop for i from 1 to 100
sds's avatar
sds committed
351 352 353 354 355 356 357 358 359
         for type = `(signed-byte ,i)
         for v = (make-array '(1) :element-type type :initial-contents '(-1))
         for result = (write-to-string v
                                       :pretty nil
                                       :array t
                                       :readably nil
                                       :length 0)
         unless (string= result "#(...)")
         collect (list i type v result)))
pfdietz's avatar
pfdietz committed
360 361 362 363 364
  nil)

(deftest print.vector.length.8
  (with-standard-io-syntax
   (loop for i from 2 to 100
sds's avatar
sds committed
365 366 367 368 369 370 371 372 373 374
         for type = `(unsigned-byte ,i)
         for v = (make-array '(4) :element-type type
                             :initial-contents '(1 3 0 2))
         for result = (write-to-string v
                                       :pretty nil
                                       :array t
                                       :readably nil
                                       :length 2)
         unless (string= result "#(1 3 ...)")
         collect (list i type v result)))
pfdietz's avatar
pfdietz committed
375 376 377 378 379
  nil)

(deftest print.vector.length.9
  (with-standard-io-syntax
   (loop for i from 2 to 100
sds's avatar
sds committed
380 381 382 383 384 385 386 387 388 389
         for type = `(signed-byte ,i)
         for v = (make-array '(4) :element-type type
                             :initial-contents '(1 -2 0 -1))
         for result = (write-to-string v
                                       :pretty nil
                                       :array t
                                       :readably nil
                                       :length 2)
         unless (string= result "#(1 -2 ...)")
         collect (list i type v result)))
pfdietz's avatar
pfdietz committed
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
  nil)

;;; Printing with *print-level* bound

(deftest print.vector.level.1
  (with-standard-io-syntax
   (write-to-string #() :level 0 :readably nil :pretty nil))
  "#")

(deftest print.vector.level.2
  (with-standard-io-syntax
   (write-to-string #() :level 1 :readably nil :pretty nil))
  "#()")

(deftest print.vector.level.3
  (with-standard-io-syntax
   (write-to-string #(17) :level 1 :readably nil :pretty nil))
  "#(17)")

(deftest print.vector.level.4
  (with-standard-io-syntax
   (write-to-string #(4 (17) 9 (a) (b) 0) :level 1 :readably nil :pretty nil))
  "#(4 # 9 # # 0)")

(deftest print.vector.level.5
  (with-standard-io-syntax
   (write-to-string '(#(a)) :level 1 :readably nil :pretty nil))
  "(#)")

(deftest print.vector.level.6
  (with-standard-io-syntax
   (write-to-string '#(#(a)) :level 1 :readably nil :pretty nil))
  "#(#)")