make-array.lsp 13.6 KB
Newer Older
1
2
3
4
5
6
7
8
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Fri Sep 20 06:47:37 2002
;;;; Contains: Tests for MAKE-ARRAY

(in-package :cl-test)

(defun make-array-check-upgrading (type)
9
  (subtypep* type (array-element-type (make-array 0 :element-type type))))
10

11
12
13
14
15
16
17
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
56
57
58
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
(defun make-array-with-checks (dimensions
			       &rest options
			       &key
			       (element-type t element-type-p)
			       (initial-contents nil intial-contents-p)
			       (initial-element nil initial-element-p)
			       (adjustable nil)
			       (fill-pointer nil)
			       (displaced-to nil)
			       (displaced-index-offset 0 dio-p)
			       &aux
			       (dimensions-list (if (listp dimensions)
						    dimensions
						  (list dimensions))))
  "Call MAKE-ARRAY and do sanity tests on the output."
  (let ((a (apply #'make-array dimensions options)))
    (cond
     ((not (typep a 'array)) :fail-not-array)
     ((and (eq t element-type)
	   (not adjustable)
	   (not fill-pointer)
	   (not displaced-to)
	   (not (typep a 'simple-array)))
      :fail-not-simple-array)
     ((and (eq (length dimensions-list) 1) 
	   (cond
	    ((not (typep a 'vector))
	     :fail-not-vector)
	    ((and (subtypep 'bit element-type)
		  (subtypep element-type 'bit)
		  (or (not (bit-vector-p a))
		      (not (typep a 'bit-vector))))
	     :fail-not-bit-vector)
	    ((and (not adjustable)
		  (not fill-pointer)
		  (not displaced-to)
		  (cond
		   ((and (eq t element-type)
			 (or (not (simple-vector-p a))
			     (not (typep a 'simple-vector))))
		    :fail-not-simple-vector)
		   ((and (subtypep 'bit element-type)
			 (subtypep element-type 'bit)
			 (or (not (simple-bit-vector-p a))
			     (not (typep a 'simple-bit-vector))))
		    :fail-not-simple-bit-vector) ))) )))
     ((not (equal (array-dimensions a) dimensions-list))
      :fail-array-dimensions)
     ((not (equal (array-rank a) (length dimensions-list)))
      :fail-array-rank)
     ((multiple-value-bind (sub good)
	  (subtypep element-type (array-element-type a))
	(and good
	     (not sub)
	     :failed-array-element-type)))
     ((and adjustable
	   (not (adjustable-array-p a))
	   :fail-adjustable))
     ((and fill-pointer
	   (not (array-has-fill-pointer-p a))
	   :fail-has-fill-pointer))
     ((and (integerp fill-pointer)
	   (not (eql fill-pointer (fill-pointer a)))
	   :fail-fill-pointer-1))
     ((and (eq fill-pointer t)
	   (not (eql (first dimensions-list) (fill-pointer a)))
	   :fail-fill-pointer-2))
     (t a))))

(deftest make-array.1
  (let ((a (make-array-with-checks 10)))
    (and (symbolp a) a))
  nil)

(deftest make-array.1a
  (let ((a (make-array-with-checks '(10))))
    (and (symbolp a) a))
  nil)

(deftest make-array.2
  (make-array-with-checks 3 :initial-element 'z)
  #(z z z))

(deftest make-array.2a
  (make-array-with-checks 3 :initial-contents '(a b c))
  #(a b c))

(deftest make-array.2b
  (make-array-with-checks 3 :initial-contents #(a b c))
  #(a b c))

(deftest make-array.2c
  (make-array-with-checks 3 :initial-contents "abc")
  #(#\a #\b #\c))

(deftest make-array.2d
  (make-array-with-checks 3 :initial-contents #*010)
  #(0 1 0))

(deftest make-array.3
  (let ((a (make-array-with-checks 5 :element-type 'bit)))
    (and (symbolp a) a))
  nil)

(deftest make-array.4
  (make-array-with-checks 5 :element-type 'bit :initial-element 1)
  #*11111)

(deftest make-array.4a
  (make-array-with-checks 5 :element-type 'bit :initial-contents '(1 0 0 1 0))
  #*10010)

(deftest make-array.4b
  (make-array-with-checks 5 :element-type 'bit :initial-contents #(1 0 0 1 0))
  #*10010)

(deftest make-array.4c
  (make-array-with-checks 5 :element-type 'bit :initial-contents #*10010)
  #*10010)

(deftest make-array.5
  (let ((a (make-array-with-checks 4 :element-type 'character)))
    (and (symbolp a) a))
  nil)

(deftest make-array.5a
  (let ((a (make-array-with-checks '(4) :element-type 'character)))
    (and (symbolp a) a))
  nil)

(deftest make-array.6
  (make-array-with-checks 4 :element-type 'character
			  :initial-element #\x)
  "xxxx")

(deftest make-array.6a
  (make-array-with-checks 4 :element-type 'character
			  :initial-contents '(#\a #\b #\c #\d))
  "abcd")

(deftest make-array.6b
  (make-array-with-checks 4 :element-type 'character
			  :initial-contents "abcd")
  "abcd")

(deftest make-array.7
  (make-array-with-checks 5 :element-type 'symbol
			  :initial-element 'a)
  #(a a a a a))

(deftest make-array.7a
  (make-array-with-checks 5 :element-type 'symbol
			  :initial-contents '(a b c d e))
  #(a b c d e))

(deftest make-array.7b
  (make-array-with-checks '(5) :element-type 'symbol
			  :initial-contents '(a b c d e))
  #(a b c d e))

(deftest make-array.8
  (let ((a (make-array-with-checks 8 :element-type '(integer 0 (256)))))
    (and (symbolp a) a))
  nil)

(deftest make-array.8a
  (make-array-with-checks 8 :element-type '(integer 0 (256))
			  :initial-element 9)
  #(9 9 9 9 9 9 9 9))

(deftest make-array.8b
  (make-array-with-checks '(8) :element-type '(integer 0 (256))
			  :initial-contents '(4 3 2 1 9 8 7 6))
  #(4 3 2 1 9 8 7 6))

;;; Zero dimensional arrays

(deftest make-array.9
  (let ((a (make-array-with-checks nil)))
    (and (symbolp a) a))
  nil)

(deftest make-array.10
  (make-array-with-checks nil :initial-element 1)
195
  #0a1)
196
197
198

(deftest make-array.11
  (make-array-with-checks nil :initial-contents 2)
199
  #0a2)
200
201
202

(deftest make-array.12
  (make-array-with-checks nil :element-type 'bit :initial-contents 1)
203
  #0a1)
204
205
206

(deftest make-array.13
  (make-array-with-checks nil :element-type t :initial-contents 'a)
207
  #0aa)
208

209
;;; Higher dimensional arrays
210

211
212
213
214
215
216
217
218
219
220
(deftest make-array.14
  (let ((a (make-array-with-checks '(2 3))))
    (and (symbolp a) a))
  nil)

(deftest make-array.15
  (make-array-with-checks '(2 3) :initial-element 'x)
  #2a((x x x) (x x x)))

(deftest make-array.16
221
222
223
  (equalpt (make-array-with-checks '(0 0))
	   (read-from-string "#2a()"))
  t)
224
225
226
227
228
229
230
231
232

(deftest make-array.17
  (make-array-with-checks '(2 3) :initial-contents '((a b c) (d e f)))
  #2a((a b c) (d e f)))

(deftest make-array.18
  (make-array-with-checks '(2 3) :initial-contents '(#(a b c) #(d e f)))
  #2a((a b c) (d e f)))

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
259
260
261
262
263
264
265
266
267
268
(deftest make-array.19
  (make-array-with-checks '(4) :initial-contents
			  (make-array '(10) :initial-element 1
				      :fill-pointer 4))
  #(1 1 1 1))

(deftest make-array.20
  (let ((a (make-array '(10) :initial-element 1
		       :fill-pointer 4)))
    (make-array-with-checks '(3 4) :initial-contents
			    (list a a a)))
  #2a((1 1 1 1) (1 1 1 1) (1 1 1 1)))

(deftest make-array.21
  (make-array-with-checks '(3 4) :initial-contents
			  (make-array '(10) :initial-element '(1 2 3 4)
				      :fill-pointer 3))
  #2a((1 2 3 4) (1 2 3 4) (1 2 3 4)))

(deftest make-array.22
  (loop for i from 3 below (min array-rank-limit 128)
	always
	(equalpt (make-array-with-checks (make-list i :initial-element 0))
		 (read-from-string (format nil "#~Aa()" i))))
  t)

(deftest make-array.23
  (let ((len (1- array-rank-limit)))
    (equalpt (make-array-with-checks (make-list len :initial-element 0))
	     (read-from-string (format nil "#~Aa()" len))))
  t)

(deftest make-array.24
  (make-array-with-checks '(5) :initial-element 'a :displaced-to nil)
  #(a a a a a))

269
270
271
272
273
274
275
276
277
278
279
(deftest make-array.25
  (make-array '(4) :initial-element 'x :nonsense-argument t
	      :allow-other-keys t)
  #(x x x x))

(deftest make-array.26
  (make-array '(4) :initial-element 'x
	      :allow-other-keys nil)
  #(x x x x))


280

281
282
283
284
285
286
;;; Adjustable arrays

(deftest make-array.adjustable.1
  (let ((a (make-array-with-checks '(10) :adjustable t)))
    (and (symbolp a) a))
  nil)
287

288
289
290
291
(deftest make-array.adjustable.2
 (make-array-with-checks '(4) :adjustable t
			 :initial-element 6)
 #(6 6 6 6))
292

293
294
295
(deftest make-array.adjustable.3
  (make-array-with-checks nil :adjustable t :initial-element 7)
  #0a7)
296

297
298
299
(deftest make-array.adjustable.4
  (make-array-with-checks '(2 3) :adjustable t :initial-element 7)
  #2a((7 7 7) (7 7 7)))
300

301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
(deftest make-array.adjustable.5
  (make-array-with-checks '(2 3) :adjustable t
			  :initial-contents '((1 2 3) "abc"))
  #2a((1 2 3) (#\a #\b #\c)))

(deftest make-array.adjustable.6
 (make-array-with-checks '(4) :adjustable t
			 :initial-contents '(a b c d))
 #(a b c d))

(deftest make-array.adjustable.7
 (make-array-with-checks '(4) :adjustable t
			 :fill-pointer t
			 :initial-contents '(a b c d))
 #(a b c d))
316

317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
(deftest make-array.adjustable.8
 (make-array-with-checks '(4) :adjustable t
			 :element-type '(integer 0 (256))
			 :initial-contents '(1 4 7 9))
 #(1 4 7 9))

(deftest make-array.adjustable.9
 (make-array-with-checks '(4) :adjustable t
			 :element-type 'base-char
			 :initial-contents "abcd")
 "abcd")

(deftest make-array.adjustable.10
 (make-array-with-checks '(4) :adjustable t
			 :element-type 'bit
			 :initial-contents '(0 1 1 0))
 #*0110)

(deftest make-array.adjustable.11
 (make-array-with-checks '(4) :adjustable t
			 :element-type 'symbol
			 :initial-contents '(a b c d))
 #(a b c d))

;;; Displaced arrays

(deftest make-array.displaced.1
  (let ((a (make-array '(10) :initial-contents '(a b c d e f g h i j))))
    (make-array-with-checks '(5) :displaced-to a))
  #(a b c d e))
347

348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
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
(deftest make-array.displaced.2
  (let ((a (make-array '(10) :initial-contents '(a b c d e f g h i j))))
    (make-array-with-checks '(5) :displaced-to a
			    :displaced-index-offset 3))
  #(d e f g h))

(deftest make-array.displaced.3
  (let ((a (make-array '(10) :initial-contents '(a b c d e f g h i j))))
    (make-array-with-checks '(5) :displaced-to a
			    :displaced-index-offset 5))
  #(f g h i j))

(deftest make-array.displaced.4
  (let ((a (make-array '(10) :initial-contents '(a b c d e f g h i j))))
    (make-array-with-checks '(0) :displaced-to a
			    :displaced-index-offset 10))
  #())

(deftest make-array.displaced.5
  (let ((a (make-array '(10) :element-type '(integer 0 (256))
		       :initial-contents '(1 3 5 7 9 11 13 15 17 19))))
    (make-array-with-checks '(5) :element-type '(integer 0 (256))
			    :displaced-to a))
  #(1 3 5 7 9))

(deftest make-array.displaced.6
  (let ((a (make-array '(10) :element-type '(integer 0 (256))
		       :initial-contents '(1 3 5 7 9 11 13 15 17 19))))
    (loop for i from 0 to 5 collect
	  (make-array-with-checks '(5) :element-type '(integer 0 (256))
				  :displaced-to a
				  :displaced-index-offset i)))
  (#(1 3 5 7 9)
   #(3 5 7 9 11)
   #(5 7 9 11 13)
   #(7 9 11 13 15)
   #(9 11 13 15 17)
   #(11 13 15 17 19)))

(deftest make-array.displaced.7
  (let ((a (make-array '(10) :element-type '(integer 0 (256))
		       :initial-contents '(1 3 5 7 9 11 13 15 17 19))))
    (make-array-with-checks '(0) :element-type '(integer 0 (256))
			    :displaced-to a
			    :displaced-index-offset 10))
  #())

(deftest make-array.displaced.8
  (let ((a (make-array '(10) :element-type 'bit
		       :initial-contents '(0 1 1 0 1 1 1 0 1 0))))
    (make-array-with-checks '(5) :element-type 'bit
			    :displaced-to a))
  #*01101)

(deftest make-array.displaced.9
  (let ((a (make-array '(10) :element-type 'bit
		       :initial-contents '(0 1 1 0 1 1 1 0 1 0))))
    (loop for i from 0 to 5 collect
	  (make-array-with-checks '(5) :element-type 'bit
				  :displaced-to a
				  :displaced-index-offset i)))
  (#*01101 #*11011 #*10111 #*01110 #*11101 #*11010))

(deftest make-array.displaced.10
  (let ((a (make-array '(10) :element-type 'bit
		       :initial-contents '(0 1 1 0 1 1 1 0 1 0))))
    (make-array-with-checks '(0) :element-type 'bit
			    :displaced-to a
			    :displaced-index-offset 10))
  #*)
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475

(deftest make-array.displaced.11
  (let ((a (make-array '(10) :element-type 'base-char
		       :initial-contents "abcdefghij")))
    (make-array-with-checks '(5) :element-type 'base-char
			    :displaced-to a))
  "abcde")

(deftest make-array.displaced.12
  (let ((a (make-array '(10) :element-type 'base-char
		       :initial-contents "abcdefghij")))
    (loop for i from 0 to 5 collect
	  (make-array-with-checks '(5) :element-type 'base-char
				  :displaced-to a
				  :displaced-index-offset i)))
  ("abcde"
   "bcdef"
   "cdefg"
   "defgh"
   "efghi"
   "fghij"))

(deftest make-array.displaced.13
  (let ((a (make-array '(10) :element-type 'base-char
		       :initial-contents "abcdefghij")))
    (make-array-with-checks '(0) :element-type 'base-char
			    :displaced-to a
			    :displaced-index-offset 10))
  "")

(deftest make-array.displaced.14
  (let ((a (make-array '(10) :element-type 'character
		       :initial-contents "abcdefghij")))
    (make-array-with-checks '(5) :element-type 'character
			    :displaced-to a))
  "abcde")

(deftest make-array.displaced.15
  (let ((a (make-array '(10) :element-type 'character
		       :initial-contents "abcdefghij")))
    (loop for i from 0 to 5 collect
	  (make-array-with-checks '(5) :element-type 'character
				  :displaced-to a
				  :displaced-index-offset i)))
  ("abcde"
   "bcdef"
   "cdefg"
   "defgh"
   "efghi"
   "fghij"))

(deftest make-array.displaced.16
  (let ((a (make-array '(10) :element-type 'character
		       :initial-contents "abcdefghij")))
    (make-array-with-checks '(0) :element-type 'character
			    :displaced-to a
			    :displaced-index-offset 10))
  "")
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499

;;; Multidimensional displaced arrays

(deftest make-array.displaced.17
  (let ((a (make-array '(3 4) :initial-contents '((1 2 3 4) (5 6 7 8)
						  (9 10 11 12)))))
    (make-array-with-checks '(8) :displaced-to a))
  #(1 2 3 4 5 6 7 8))

(deftest make-array.displaced.18
  (let ((a (make-array '(3 4) :initial-contents '((1 2 3 4) (5 6 7 8)
						  (9 10 11 12)))))
    (make-array-with-checks '(8) :displaced-to a
			    :displaced-index-offset 3))
  #(4 5 6 7 8 9 10 11))

(deftest make-array.displaced.19
  (let ((a (make-array '(3 4) :initial-contents '((1 2 3 4) (5 6 7 8)
						  (9 10 11 12)))))
    (make-array-with-checks '(2 4) :displaced-to a
			    :displaced-index-offset 4))
  #2a((5 6 7 8) (9 10 11 12)))