print-array.lsp 17.8 KB
Newer Older
1 2 3 4 5 6 7 8 9
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Thu Apr 22 22:38:11 2004
;;;; Contains: Tests of printing of arrays (other than vectors)

(compile-and-load "printer-aux.lsp")

(in-package :cl-test)

pfdietz's avatar
pfdietz committed
10
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
11 12
;; Zero dimensional arrays

pfdietz's avatar
pfdietz committed
13
(deftest print.array.0.1
14 15 16 17 18
  (let ((a (make-array nil :initial-element 0)))
    (with-standard-io-syntax
     (write-to-string a :readably nil :array t)))
  "#0A0")

pfdietz's avatar
pfdietz committed
19
(deftest print.array.0.2
pfdietz's avatar
pfdietz committed
20 21
  (with-standard-io-syntax
   (let ((a (make-array nil :initial-element '|A|))
sds's avatar
sds committed
22
         (*package* (find-package "CL-TEST")))
23 24 25
     (write-to-string a :readably nil :array t)))
  "#0AA")

pfdietz's avatar
pfdietz committed
26
(deftest print.array.0.3
27
  (let* ((a (make-array nil :initial-element 0))
sds's avatar
sds committed
28
         (result (write-to-string a :readably nil :array nil)))
29 30 31 32
    (values
     (subseq result 0 2)
     (subseq result (1- (length result)))))
  "#<" ">")
33

pfdietz's avatar
pfdietz committed
34 35 36 37 38 39 40 41
(deftest print.array.0.4
   (let ((a (make-array nil :initial-element 0 :adjustable t)))
    (with-standard-io-syntax
     (write-to-string a :readably nil :array t)))
  "#0A0")

(deftest print.array.0.5
   (let* ((a (make-array nil :initial-element 0 :adjustable t))
sds's avatar
sds committed
42
          (b (make-array nil :displaced-to a :displaced-index-offset 0)))
pfdietz's avatar
pfdietz committed
43 44 45 46 47 48
    (with-standard-io-syntax
     (write-to-string b :readably nil :array t)))
  "#0A0")

(deftest print.array.0.6
  (let ((a (make-array nil :initial-element 0
sds's avatar
sds committed
49
                       :element-type '(integer 0 2))))
pfdietz's avatar
pfdietz committed
50 51 52 53
    (with-standard-io-syntax
     (write-to-string a :readably nil :array t)))
  "#0A0")

54
(deftest print.array.0.7
pfdietz's avatar
pfdietz committed
55
  (loop for a = (make-array nil :initial-element (- (random 1000000) 500000))
sds's avatar
sds committed
56
        repeat 30 nconc (randomly-check-readability a :test #'is-similar))
57 58 59 60
  nil)

(deftest print.array.0.8
  (loop for i from 1 to 64
sds's avatar
sds committed
61 62 63 64 65
        for type = `(unsigned-byte ,i)
        nconc
        (let ((a (make-array nil :initial-element 1 :element-type type)))
          (loop repeat 5 nconc (randomly-check-readability a :test #'is-similar
                                                           :can-fail t))))
66 67
  nil)

pfdietz's avatar
pfdietz committed
68 69
(deftest print.array.0.9
  (loop for a = (make-array nil :initial-element (random 1000000) :adjustable t)
sds's avatar
sds committed
70 71
        repeat 30
        nconc (randomly-check-readability a :test #'is-similar))
pfdietz's avatar
pfdietz committed
72 73 74 75
  nil)

(deftest print.array.0.10
  (loop for a = (make-array nil :initial-element (random 1000000000))
sds's avatar
sds committed
76 77
        for b = (make-array nil :displaced-to a :displaced-index-offset 0)
        repeat 30 nconc (randomly-check-readability b :test #'is-similar))
pfdietz's avatar
pfdietz committed
78 79 80 81
  nil)

(deftest print.array.0.11
  (loop for type in '(short-float single-float double-float long-float float)
sds's avatar
sds committed
82 83 84 85 86 87
        for zero = (coerce 0 type)
        for a = (make-array nil :initial-element zero
                            :element-type type)
        nconc
        (loop repeat 30 nconc (randomly-check-readability a :test #'is-similar
                                                          :can-fail t)))
pfdietz's avatar
pfdietz committed
88 89 90 91
  nil)

(deftest print.array.0.12
  (loop for type0 in '(short-float single-float double-float long-float float)
sds's avatar
sds committed
92 93 94 95 96 97 98
        for type = `(complex ,type0)
        for zero = (complex (coerce 0.0s0 type0))
        for a = (make-array nil :initial-element zero
                            :element-type type)
        nconc
        (loop repeat 30 nconc (randomly-check-readability a :test #'is-similar
                                                          :can-fail t)))
pfdietz's avatar
pfdietz committed
99 100
  nil)

pfdietz's avatar
pfdietz committed
101
(deftest print.array.0.13
102
  (let ((result (write-to-string (make-array nil :initial-element 0)
sds's avatar
sds committed
103
                                 :readably nil :array nil)))
104 105 106 107
    (values
     (subseq result 0 2)
     (subseq result (1- (length result)))))
  "#<" ">")
pfdietz's avatar
pfdietz committed
108 109 110

(deftest print.array.0.14
  (loop for i from 1 to 64
sds's avatar
sds committed
111 112 113 114 115 116
        for type = `(unsigned-byte ,i)
        for a = (make-array nil :element-type type :initial-element 1)
        for result = (write-to-string a :readably nil :array nil)
        unless (and (string= (subseq result 0 2) "#<")
                    (string= (subseq result (1- (length result))) ">"))
        collect (list i result))
pfdietz's avatar
pfdietz committed
117 118 119 120
  nil)

(deftest print.array.0.15
  (loop for i from 1 to 64
sds's avatar
sds committed
121 122 123 124 125 126
        for type = `(signed-byte ,i)
        for a = (make-array nil :element-type type :initial-element -1)
        for result = (write-to-string a :readably nil :array nil)
        unless (and (string= (subseq result 0 2) "#<")
                    (string= (subseq result (1- (length result))) ">"))
        collect (list i result))
pfdietz's avatar
pfdietz committed
127 128 129 130
  nil)

(deftest print.array.0.16
  (loop for type in '(short-float single-float double-float long-float)
sds's avatar
sds committed
131 132 133 134 135 136
        for a = (make-array nil :element-type type
                            :initial-element (coerce 17 type))
        for result = (write-to-string a :readably nil :array nil)
        unless (and (string= (subseq result 0 2) "#<")
                    (string= (subseq result (1- (length result))) ">"))
        collect (list type result))
pfdietz's avatar
pfdietz committed
137
  nil)
pfdietz's avatar
pfdietz committed
138

pfdietz's avatar
pfdietz committed
139 140
(deftest print.array.0.17
  (loop for type0 in '(short-float single-float double-float
sds's avatar
sds committed
141 142 143 144 145 146 147 148
                                   long-float float real)
        for type = `(complex ,type0)
        for a = (make-array nil :element-type type
                            :initial-element (complex 0 (coerce 3 type0)))
        for result = (write-to-string a :readably nil :array nil)
        unless (and (string= (subseq result 0 2) "#<")
                    (string= (subseq result (1- (length result))) ">"))
        collect (list type result))
pfdietz's avatar
pfdietz committed
149
  nil)
pfdietz's avatar
pfdietz committed
150

pfdietz's avatar
pfdietz committed
151
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pfdietz's avatar
pfdietz committed
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
;; Two-d arrays
(deftest print.array.2.1
  (let ((a (make-array '(1 1) :initial-contents '((1)))))
    (with-standard-io-syntax
     (write-to-string a :readably nil :array t)))
  "#2A((1))")

(deftest print.array.2.2
  (let ((a (make-array '(2 3) :initial-contents '((1 3 8)(2 6 10)))))
    (with-standard-io-syntax
     (write-to-string a :readably nil :array t)))
  "#2A((1 3 8) (2 6 10))")

(deftest print.array.2.3
  (let ((a (make-array '(0 1))))
    (with-standard-io-syntax
     (write-to-string a :readably nil :array t)))
  "#2A()")

(deftest print.array.2.4
  (let ((a (make-array '(1 0))))
    (with-standard-io-syntax
     (write-to-string a :readably nil :array t)))
  "#2A(())")

(deftest print.array.2.5
  (let ((a (make-array '(0 0))))
    (with-standard-io-syntax
     (write-to-string a :readably nil :array t)))
  "#2A()")

pfdietz's avatar
pfdietz committed
183 184 185 186 187 188 189 190
(deftest print.array.2.6
  (let ((a (make-array '(10 0))))
    (with-standard-io-syntax
     (write-to-string a :readably nil :array t)))
  "#2A(() () () () () () () () () ())")

(deftest print.array.2.7
  (let* ((a (make-array '(3 3) :initial-contents '((1 3 8) (2 67 121) (65 432 6))))
sds's avatar
sds committed
191 192
         (b (make-array '(3 3) :displaced-to a
                        :displaced-index-offset 0)))
pfdietz's avatar
pfdietz committed
193 194 195 196 197 198
    (with-standard-io-syntax
     (write-to-string b :readably nil :array t)))
  "#2A((1 3 8) (2 67 121) (65 432 6))")

(deftest print.array.2.8
  (let* ((a (make-array '(3 3) :initial-contents '((1 3 8) (2 67 121) (65 432 6))))
sds's avatar
sds committed
199 200
         (b (make-array '(2 3) :displaced-to a
                        :displaced-index-offset 0)))
pfdietz's avatar
pfdietz committed
201 202 203 204 205 206
    (with-standard-io-syntax
     (write-to-string b :readably nil :array t)))
  "#2A((1 3 8) (2 67 121))")

(deftest print.array.2.9
  (let* ((a (make-array '(3 3) :initial-contents '((1 3 8) (2 67 121) (65 432 6))))
sds's avatar
sds committed
207 208
         (b (make-array '(2 2) :displaced-to a
                        :displaced-index-offset 4)))
pfdietz's avatar
pfdietz committed
209 210 211 212 213 214
    (with-standard-io-syntax
     (write-to-string b :readably nil :array t)))
  "#2A((67 121) (65 432))")

(deftest print.array.2.10
  (let* ((a (make-array '(3 3) :initial-contents '((1 3 8) (2 67 121) (65 432 6))))
sds's avatar
sds committed
215 216 217
         (b (make-array '(2 2) :displaced-to a
                        :displaced-index-offset 4
                        :adjustable t)))
pfdietz's avatar
pfdietz committed
218 219 220 221 222 223
    (with-standard-io-syntax
     (write-to-string b :readably nil :array t)))
  "#2A((67 121) (65 432))")

(deftest print.array.2.11
  (let* ((a (make-array '(3 4)
sds's avatar
sds committed
224 225
                        :initial-contents '((7 8 9 10) (65 12 42 -1) (:|W| :|X| :|Y| :|Z| ))
                        :adjustable t)))
pfdietz's avatar
pfdietz committed
226 227 228 229 230 231
    (with-standard-io-syntax
     (write-to-string a :readably nil :array t)))
  "#2A((7 8 9 10) (65 12 42 -1) (:W :X :Y :Z))")

(deftest print.array.2.12
  (let ((desired-result "#2A((0 1 1) (1 1 0))"))
pfdietz's avatar
pfdietz committed
232
    (loop for i from 2 to 64
sds's avatar
sds committed
233 234 235 236 237 238
          for a = (make-array '(2 3) :element-type `(unsigned-byte ,i)
                              :initial-contents '((0 1 1) (1 1 0)))
          for result = (with-standard-io-syntax
                        (write-to-string a :readably nil :array t))
          unless (string= desired-result result)
          collect (list i a result)))
pfdietz's avatar
pfdietz committed
239 240 241 242 243
  nil)

(deftest print.array.2.13
  (let ((desired-result "#2A((0 -1 -1) (-1 -1 0))"))
    (loop for i from 1 to 64
sds's avatar
sds committed
244 245 246 247 248 249
          for a = (make-array '(2 3) :element-type `(signed-byte ,i)
                              :initial-contents '((0 -1 -1) (-1 -1 0)))
          for result = (with-standard-io-syntax
                        (write-to-string a :readably nil :array t))
          unless (string= desired-result result)
          collect (list i a result)))
pfdietz's avatar
pfdietz committed
250
  nil)
pfdietz's avatar
pfdietz committed
251

pfdietz's avatar
pfdietz committed
252 253
(deftest print.array.2.14
  (let ((desired-result "#2A((0 1 1) (1 1 0))"))
pfdietz's avatar
pfdietz committed
254
    (loop for i from 2 to 64
sds's avatar
sds committed
255 256 257 258 259 260 261
          for a = (make-array '(2 3) :element-type `(unsigned-byte ,i)
                              :adjustable t
                              :initial-contents '((0 1 1) (1 1 0)))
          for result = (with-standard-io-syntax
                        (write-to-string a :readably nil :array t))
          unless (string= desired-result result)
          collect (list i a result)))
pfdietz's avatar
pfdietz committed
262
  nil)
pfdietz's avatar
pfdietz committed
263

pfdietz's avatar
pfdietz committed
264 265 266
(deftest print.array.2.15
  (let ((desired-result "#2A((0 -1 -1) (-1 -1 0))"))
    (loop for i from 1 to 64
sds's avatar
sds committed
267 268 269 270 271 272 273
          for a = (make-array '(2 3) :element-type `(signed-byte ,i)
                              :adjustable t
                              :initial-contents '((0 -1 -1) (-1 -1 0)))
          for result = (with-standard-io-syntax
                        (write-to-string a :readably nil :array t))
          unless (string= desired-result result)
          collect (list i a result)))
pfdietz's avatar
pfdietz committed
274
  nil)
pfdietz's avatar
pfdietz committed
275

pfdietz's avatar
pfdietz committed
276 277
(deftest print.array.2.16
  (let ((desired-result "#2A((1 1) (1 0))"))
pfdietz's avatar
pfdietz committed
278
    (loop for i from 2 to 64
sds's avatar
sds committed
279 280 281 282 283 284 285 286 287 288
          for type = `(unsigned-byte ,i)
          for a = (make-array '(2 3) :element-type type
                              :adjustable t
                              :initial-contents '((0 1 1) (1 1 0)))
          for b = (make-array '(2 2) :displaced-to a :displaced-index-offset 2
                              :element-type type)
          for result = (with-standard-io-syntax
                        (write-to-string b :readably nil :array t))
          unless (string= desired-result result)
          collect (list i b result)))
pfdietz's avatar
pfdietz committed
289
  nil)
pfdietz's avatar
pfdietz committed
290

pfdietz's avatar
pfdietz committed
291 292 293
(deftest print.array.2.17
  (let ((desired-result "#2A((1 -1) (-2 0))"))
    (loop for i from 2 to 64
sds's avatar
sds committed
294 295 296 297 298 299 300 301 302 303
          for type = `(signed-byte ,i)
          for a = (make-array '(2 3) :element-type type
                              :adjustable t
                              :initial-contents '((0 1 1) (-1 -2 0)))
          for b = (make-array '(2 2) :displaced-to a :displaced-index-offset 2
                              :element-type type)
          for result = (with-standard-io-syntax
                        (write-to-string b :readably nil :array t))
          unless (string= desired-result result)
          collect (list i b result)))
pfdietz's avatar
pfdietz committed
304 305 306 307
  nil)

(deftest print.array.2.20
  (let* ((a (make-array '(9) :initial-contents '(1 3 8 2 67 121 65 432 6)))
sds's avatar
sds committed
308 309
         (b (make-array '(2 2) :displaced-to a
                        :displaced-index-offset 1)))
pfdietz's avatar
pfdietz committed
310 311 312 313
    (with-standard-io-syntax
     (write-to-string b :readably nil :array t)))
  "#2A((3 8) (2 67))")

pfdietz's avatar
pfdietz committed
314
(deftest print.array.2.21
315 316 317 318 319 320
  (trim-list
   (loop
      for dims = (list (random 4) (random 4))
      for a = (make-array dims :initial-element (- (random 1000000) 500000))
      repeat 100
      nconc (let ((result (randomly-check-readability a :test #'is-similar :can-fail t)))
sds's avatar
sds committed
321
              (and result (list (cons dims (first result))))))
322
   10)
pfdietz's avatar
pfdietz committed
323 324
  nil)

pfdietz's avatar
pfdietz committed
325
(deftest print.array.2.22
pfdietz's avatar
pfdietz committed
326
  (loop for a = (make-array (list (random 4) (random 4))
sds's avatar
sds committed
327 328 329 330
                            :initial-element (- (random 1000000) 500000)
                            :adjustable t)
        repeat 100 nconc (randomly-check-readability a :test #'is-similar
                                                     :can-fail t))
pfdietz's avatar
pfdietz committed
331 332
  nil)

pfdietz's avatar
pfdietz committed
333
(deftest print.array.2.23
pfdietz's avatar
pfdietz committed
334
  (loop for d1 = (random 10)
sds's avatar
sds committed
335 336 337 338 339 340 341 342 343 344
        for d2 = (random 10)
        for a = (make-array (list d1 d2)
                            :initial-element (- (random 1000000) 500000))
        for d1a = (random (1+ d1))
        for d2a = (random (1+ d2))
        for offset = (random (1+ (- (* d1 d2) (* d1a d2a))))
        for b = (make-array (list d1a d2a) :displaced-to a
                            :displaced-index-offset offset)
        repeat 100 nconc (randomly-check-readability b :test #'is-similar
                                                     :can-fail t))
pfdietz's avatar
pfdietz committed
345 346 347 348
  nil)

(deftest print.array.2.24
  (loop for i from 1 to 64
sds's avatar
sds committed
349 350 351 352 353
        for type = `(unsigned-byte ,i)
        nconc
        (let ((a (make-array '(3 4) :initial-element 1 :element-type type)))
          (loop repeat 5 nconc (randomly-check-readability a :test #'is-similar
                                                           :can-fail t))))
pfdietz's avatar
pfdietz committed
354 355 356 357 358
  nil)

(deftest print.array.2.25
  (let ((a (make-array '(3 4) :initial-element #\a :element-type 'character)))
    (loop repeat 10 nconc (randomly-check-readability a :test #'is-similar
sds's avatar
sds committed
359
                                                      :can-fail t)))
pfdietz's avatar
pfdietz committed
360 361 362 363 364
  nil)

(deftest print.array.2.26
  (let ((a (make-array '(3 4) :initial-element #\a :element-type 'base-char)))
    (loop repeat 10 nconc (randomly-check-readability a :test #'is-similar
sds's avatar
sds committed
365
                                                      :can-fail t)))
pfdietz's avatar
pfdietz committed
366 367 368
  nil)


pfdietz's avatar
pfdietz committed
369
(deftest print.array.2.27
370
  (let ((str (write-to-string (make-array '(2 3) :initial-element 0)
sds's avatar
sds committed
371
                              :readably nil :array nil)))
372 373
    (values (subseq str 0 2) (subseq str (1- (length str)))))
  "#<" ">")
pfdietz's avatar
pfdietz committed
374 375 376

(deftest print.array.2.28
  (loop for i from 1 to 64
sds's avatar
sds committed
377 378 379 380 381 382
        for type = `(unsigned-byte ,i)
        for a = (make-array '(4 3) :element-type type :initial-element 1)
        for result = (write-to-string a :readably nil :array nil)
        unless (and (string= (subseq result 0 2) "#<")
                    (string= (subseq result (1- (length result))) ">"))
        collect (list i result))
pfdietz's avatar
pfdietz committed
383 384 385 386
  nil)

(deftest print.array.2.29
  (loop for i from 1 to 64
sds's avatar
sds committed
387 388 389 390 391 392
        for type = `(signed-byte ,i)
        for a = (make-array '(4 8) :element-type type :initial-element -1)
        for result = (write-to-string a :readably nil :array nil)
        unless (and (string= (subseq result 0 2) "#<")
                    (string= (subseq result (1- (length result))) ">"))
        collect (list i result))
pfdietz's avatar
pfdietz committed
393 394 395 396
  nil)

(deftest print.array.2.30
  (loop for type in '(short-float single-float double-float long-float)
sds's avatar
sds committed
397 398 399 400 401 402
        for a = (make-array '(5 7) :element-type type
                            :initial-element (coerce 17 type))
        for result = (write-to-string a :readably nil :array nil)
        unless (and (string= (subseq result 0 2) "#<")
                    (string= (subseq result (1- (length result))) ">"))
        collect (list type result))
pfdietz's avatar
pfdietz committed
403 404 405 406
  nil)

(deftest print.array.2.31
  (loop for type0 in '(short-float single-float double-float
sds's avatar
sds committed
407 408 409 410 411 412 413 414
                                   long-float float real)
        for type = `(complex ,type0)
        for a = (make-array '(13 5) :element-type type
                            :initial-element (complex 0 (coerce 3 type0)))
        for result = (write-to-string a :readably nil :array nil)
        unless (and (string= (subseq result 0 2) "#<")
                    (string= (subseq result (1- (length result))) ">"))
        collect (list type result))
pfdietz's avatar
pfdietz committed
415
  nil)
pfdietz's avatar
pfdietz committed
416

pfdietz's avatar
pfdietz committed
417 418 419 420 421
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Three D arrays

(deftest print.array.3.1
  (let* ((a (make-array '(1 2 3) :initial-contents '(((:|A| :|B| :|C|) (:|D| :|E| :|F|)))))
sds's avatar
sds committed
422 423
         (b (make-array '(3 2 1) :displaced-to a
                        :displaced-index-offset 0)))
pfdietz's avatar
pfdietz committed
424 425 426 427 428 429 430 431 432 433 434 435
    (with-standard-io-syntax
     (values
      (write-to-string a :readably nil :array t)
      (write-to-string b :readably nil :array t))))
  "#3A(((:A :B :C) (:D :E :F)))"
  "#3A(((:A) (:B)) ((:C) (:D)) ((:E) (:F)))")


;;; Multidimensional arrays

(deftest print.array.multi-dim.1
  (with-standard-io-syntax
436
   (loop for d in (remove array-rank-limit
sds's avatar
sds committed
437 438 439 440 441 442 443 444 445 446 447 448 449 450
                          '(4 5 6 7 8 9 10 12 16 20 30 40 100 200 400 600 800 1023)
                          :test #'<=)
         for dims = (make-list d :initial-element 1)
         for a = (make-array dims :initial-element 0)
         for result = (with-standard-io-syntax
                       (write-to-string a :readably nil :array t))
         for expected-result =
         (concatenate 'string
                      (format nil "#~DA" d)
                      (make-string d :initial-element #\()
                      "0"
                      (make-string d :initial-element #\)))
         unless (string= result expected-result)
         collect (list d result expected-result)))
pfdietz's avatar
pfdietz committed
451 452 453 454
  nil)

(deftest print.array.multi-dim.2
  (with-standard-io-syntax
455
   (loop for d = (+ 4 (random (min (- array-rank-limit 4) 1000)))
sds's avatar
sds committed
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
         for p = (random d)
         for dims = (let ((list (make-list d :initial-element 1)))
                      (setf (elt list p) 0)
                      list)
         for a = (make-array dims :initial-element 0)
         for result = (with-standard-io-syntax
                       (write-to-string a :readably nil :array t))
         for expected-result =
         (concatenate 'string
                      (format nil "#~DA" d)
                      (make-string (1+ p) :initial-element #\()
                      (make-string (1+ p) :initial-element #\)))
         repeat 50
         unless (string= result expected-result)
         collect (list d result expected-result)))
pfdietz's avatar
pfdietz committed
471
  nil)
pfdietz's avatar
pfdietz committed
472 473 474

;;; To add: more tests for high dimensional arrays, including arrays with
;;; element types