map.lsp 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sat Aug 17 20:54:48 2002
;;;; Contains: Tests for the MAP function

(in-package :cl-test)

(deftest map-array.1
  (map 'list #'1+ #(1 2 3 4))
  (2 3 4 5))

(deftest map-array.2
  (map 'vector #'+ #(1 2 3 4) #(6 6 6 6))
  #(7 8 9 10))

(deftest map-array.3
  (map 'vector #'+ #(1 2 3 4 5) #(6 6 6 6))
  #(7 8 9 10))

(deftest map-array.4
  (map 'vector #'+ #(1 2 3 4) #(6 6 6 6 6))
  #(7 8 9 10))

(deftest map-array.5
  (map '(vector *) #'+ #(1 2 3 4) #(6 6 6 6))
  #(7 8 9 10))

(deftest map-array.6
  (map '(vector * 4) #'+ #(1 2 3 4) #(6 6 6 6))
  #(7 8 9 10))

32 33 34
;;; (deftest map-array.7
;;;  (map 'array #'identity '(a b c d e f))
;;;  #(a b c d e f))
35

36 37 38
;;; (deftest map-array.8
;;;   (map 'simple-array #'identity '(a b c d e f))
;;;   #(a b c d e f))
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

(deftest map-array.9
  (map 'simple-vector #'identity '(a b c d e f))
  #(a b c d e f))

(deftest map-array.10
  (map 'simple-vector #'cons '(a b c d e f) #(1 2 3 4 5 6))
  #((a . 1) (b . 2) (c . 3) (d . 4) (e . 5) (f . 6)))

(deftest map-array.11
  (map 'vector #'identity '(#\a #\b #\c #\d #\e))
  #(#\a #\b #\c #\d #\e))

(deftest map-array.12
  (map 'vector #'identity "abcde")
  #(#\a #\b #\c #\d #\e))

(deftest map-array.13
  (map 'vector #'identity #*000001)
  #(0 0 0 0 0 1))

(deftest map-array.14
  (map 'list #'identity #*000001)
  (0 0 0 0 0 1))

(deftest map-bit-vector.15
  (map 'bit-vector #'identity '(0 0 0 0 0 1))
  #*000001)

(deftest map-bit-vector.16
  (map 'simple-bit-vector #'identity '(0 0 0 0 0 1))
  #*000001)

(deftest map-bit-vector.17
  (map '(vector bit) #'identity '(0 0 0 0 0 1))
  #*000001)

(deftest map-bit-vector.18
  (map '(simple-vector *) #'identity '(0 0 0 0 0 1))
  #*000001)

(deftest map-bit-vector.19
  (map '(bit-vector 6) #'identity '(0 0 0 0 0 1))
  #*000001)

(deftest map-bit-vector.20
  (map '(bit-vector *) #'identity '(0 0 0 0 0 1))
  #*000001)

(deftest map-bit-vector.21
  (map '(simple-bit-vector 6) #'identity '(0 0 0 0 0 1))
  #*000001)

(deftest map-bit-vector.22
  (map '(simple-bit-vector *) #'identity '(0 0 0 0 0 1))
  #*000001)

(deftest map-bit-vector.23
  (map '(vector bit 6) #'identity '(0 0 0 0 0 1))
  #*000001)

(deftest map-bit-vector.24
  (map '(vector bit *) #'identity '(0 0 0 0 0 1))
  #*000001)

(deftest map-bit-vector.25
  (map '(simple-vector 6) #'identity '(0 0 0 0 0 1))
  #*000001)

(deftest map-string.26
  (map 'string #'identity '(#\a #\b #\c #\d #\e))
  "abcde")

(deftest map-string.27
  (map 'string #'identity "abcde")
  "abcde")

(deftest map-string.28
  (map '(vector character) #'identity '(#\a #\b #\c #\d #\e))
  "abcde")

(deftest map-string.29
  (map '(vector character 5) #'identity '(#\a #\b #\c #\d #\e))
  "abcde")

(deftest map-string.30
125
  (map '(simple-vector 5) #'identity '(#\a #\b #\c #\d #\e))
126 127
  "abcde")

128
;;; Use a more elaborate form of the simple-array type specifier
129 130 131
;;; (deftest map-string.31
;;;  (map '(simple-array character *) #'identity "abcde")
;;;  "abcde")
132

133
;;; Use a more elaborate form of the simple-array type specifier
134 135 136
;;; (deftest map-string.32
;;;  (map '(simple-array character 5) #'identity "abcde")
;;;   "abcde")
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

(deftest map-nil.33
  (let ((a nil))
    (values (map nil #'(lambda (x) (push x a)) "abcdef") a))
  nil (#\f #\e #\d #\c #\b #\a))

(deftest map-nil.34
  (let ((a nil))
    (values (map nil #'(lambda (x) (push x a)) '(a b c d e)) a))
  nil (e d c b a))

(deftest map-nil.35
  (let ((a nil))
    (values (map nil #'(lambda (x) (push x a)) #(a b c d e)) a))
  nil (e d c b a))

(deftest map-nil.36
  (let ((a nil))
    (values (map nil #'(lambda (x) (push x a)) #*001011110) a))
  nil (0 1 1 1 1 0 1 0 0))

(deftest map-null.1
  (map 'null #'identity nil)
  nil)

(deftest map-cons.1
  (map 'cons #'identity '(a b c))
  (a b c))

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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
(deftest map.37
  (map 'simple-string #'identity '(#\a #\b #\c))
  "abc")

(deftest map.38
  (map '(simple-string) #'identity '(#\a #\b #\c))
  "abc")

(deftest map.39
  (map '(simple-string *) #'identity '(#\a #\b #\c))
  "abc")

(deftest map.40
  (map '(simple-string 3) #'identity '(#\a #\b #\c))
  "abc")

(deftest map.41
  (map '(base-string) #'identity '(#\a #\b #\c))
  "abc")

(deftest map.42
  (map '(base-string *) #'identity '(#\a #\b #\c))
  "abc")

(deftest map.43
  (map '(base-string 3) #'identity '(#\a #\b #\c))
  "abc")

(deftest map.44
  (map 'simple-base-string #'identity '(#\a #\b #\c))
  "abc")

(deftest map.45
  (map '(simple-base-string) #'identity '(#\a #\b #\c))
  "abc")

(deftest map.46
  (map '(simple-base-string *) #'identity '(#\a #\b #\c))
  "abc")

(deftest map.47
  (map '(simple-base-string 3) #'identity '(#\a #\b #\c))
  "abc")

210
(deftest map.48
pfdietz's avatar
pfdietz committed
211
  :notes (:result-type-element-type-by-subtype)
212
  (let ((type '(or (vector t 10) (vector t 5))))
213
    (if (subtypep type '(vector t))
sds's avatar
sds committed
214
        (equalpt (map type #'identity '(1 2 3 4 5)) #(1 2 3 4 5))
215 216 217
      t))
  t)

218 219
;;; Error tests

220
(deftest map.error.1
221 222
  (signals-error-always (map 'symbol #'identity '(a b c)) type-error)
  t t)
223

pfdietz's avatar
pfdietz committed
224 225 226 227
(deftest map.error.1a
  (signals-error (map 'symbol #'identity '(a b c)) type-error)
  t)

228
(deftest map.error.2
229 230
  (signals-error (map '(vector * 8) #'identity '(a b c)) type-error)
  t)
231

232
(deftest map.error.3
233 234
  (signals-error (map 'list #'identity '(a b . c)) type-error)
  t)
235

236
(deftest map.error.4
237 238
  (signals-error (map) program-error)
  t)
239 240

(deftest map.error.5
241 242
  (signals-error (map 'list) program-error)
  t)
243 244

(deftest map.error.6
245 246
  (signals-error (map 'list #'null) program-error)
  t)
247

248
(deftest map.error.7
249 250
  (signals-error (map 'list #'cons '(a b c d)) program-error)
  t)
251 252

(deftest map.error.8
253
  (signals-error (map 'list #'cons '(a b c d) '(1 2 3 4) '(5 6 7 8))
sds's avatar
sds committed
254
                 program-error)
255
  t)
256 257

(deftest map.error.9
258 259
  (signals-error (map 'list #'car '(a b c d)) type-error)
  t)
260

261
(deftest map.error.10
262
  :notes (:result-type-element-type-by-subtype)
263 264
  (let ((type '(or (vector bit) (vector t))))
    (if (subtypep type 'vector)
sds's avatar
sds committed
265
        (eval `(signals-error-always (map ',type #'identity '(1 0 1)) error))
266 267 268 269 270 271
      (values t t)))
  t t)

(deftest map.error.11
  (let ((type '(or (vector t 5) (vector t 10))))
    (if (subtypep type 'vector)
sds's avatar
sds committed
272
        (eval `(signals-error (map ',type #'identity '(1 2 3 4 5 6)) type-error))
273 274
      t))
  t)
275

pfdietz's avatar
pfdietz committed
276 277 278 279 280 281 282 283
(deftest map.error.12
  (check-type-error #'(lambda (x) (map 'list #'identity x)) #'sequencep)
  nil)

(deftest map.error.13
  (check-type-error #'(lambda (x) (map 'vector #'cons '(a b c d) x)) #'sequencep)
  nil)

284 285 286 287
;;; Test mapping on arrays with fill pointers

(deftest map.fill.1
  (let ((s1 (make-array '(10) :initial-contents '(a b c d e f g h i j)
sds's avatar
sds committed
288
                        :fill-pointer 8)))
289 290 291 292 293
    (map 'list #'identity s1))
  (a b c d e f g h))

(deftest map.fill.2
  (let ((s1 (make-array '(10) :initial-contents '(a b c d e f g h i j)
sds's avatar
sds committed
294
                        :fill-pointer 8)))
pfdietz's avatar
pfdietz committed
295
    (map 'list #'(lambda (x y) x) '(1 2 3 4 5 6 7 8 9 10) s1))
296 297 298 299
  (1 2 3 4 5 6 7 8))

(deftest map.fill.3
  (let ((s1 (make-array '(10) :initial-element #\a
sds's avatar
sds committed
300 301
                        :element-type 'character
                        :fill-pointer 8)))
302 303
    (map 'string #'identity s1))
  "aaaaaaaa")
sds's avatar
sds committed
304

305 306
(deftest map.fill.4
  (let ((s1 (make-array '(10) :initial-element #\a
sds's avatar
sds committed
307 308
                        :element-type 'base-char
                        :fill-pointer 8)))
pfdietz's avatar
pfdietz committed
309
    (map 'list #'(lambda (x y) x) '(1 2 3 4 5 6 7 8 9 10) s1))
310 311 312 313
  (1 2 3 4 5 6 7 8))

(deftest map.fill.5
  (let ((s1 (make-array '(10) :initial-element 0
sds's avatar
sds committed
314 315
                        :element-type 'bit
                        :fill-pointer 8)))
316
    (map 'bit-vector #'identity s1))
sds's avatar
sds committed
317 318
  #*00000000)

319 320
(deftest map.fill.6
  (let ((s1 (make-array '(10) :initial-element 1
sds's avatar
sds committed
321 322
                        :element-type 'bit
                        :fill-pointer 8)))
pfdietz's avatar
pfdietz committed
323
    (map 'list #'(lambda (x y) x) '(1 2 3 4 5 6 7 8 9 10) s1))
324
  (1 2 3 4 5 6 7 8))
325

326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
;;; Specialized string tests

(deftest map.specialized-string.1
  (do-special-strings
   (s "abcde" nil)
   (let ((s2 (map 'list #'identity s)))
     (assert (equal s2 '(#\a #\b #\c #\d #\e)))))
  nil)

(deftest map.specialized-string.2
  (do-special-strings
   (s "abcde" nil)
   (let ((s2 (map 'list #'(lambda (x y) y) '(1 2 3 4 5) s)))
     (assert (equal s2 '(#\a #\b #\c #\d #\e)))))
  nil)

(deftest map.specialized-string.3
  (let ((s (map 'base-string #'identity '(#\a #\b #\c))))
    (assert (typep s 'base-string))
    s)
  "abc")

;;; FIXME: Add tests for building strings of other character types

350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
;;; Special vector types

(deftest map.specialized-vector.1
  (do-special-integer-vectors
   (v #(0 1 1 0 0 1) nil)
   (assert (equal (map 'list #'list v v) '((0 0) (1 1) (1 1) (0 0) (0 0) (1 1)))))
  nil)

(deftest map.specialized-vector.2
  (do-special-integer-vectors
   (v #(1 2 3 4 5 6 7) nil)
   (assert (equal (map 'list #'identity v) '(1 2 3 4 5 6 7))))
  nil)

(deftest map.specialized-vector.3
  (do-special-integer-vectors
   (v #(-1 -2 -3 -4 -5 -6 -7) nil)
   (assert (equal (map 'list #'- v) '(1 2 3 4 5 6 7))))
  nil)

(deftest map.specialized-vector.4
  (loop for i from 1 to 40
sds's avatar
sds committed
372 373 374 375 376 377 378 379
        for type = `(unsigned-byte ,i)
        for bound = (ash 1 i)
        for len = 10
        for vals = (loop repeat len collect (random i))
        for result = (map `(vector ,type) #'identity vals)
        unless (and (= (length result) len)
                    (every #'eql vals result))
        collect (list i vals result))
380 381 382 383
  nil)

(deftest map.specialized-vector.5
  (loop for i from 1 to 40
sds's avatar
sds committed
384 385 386 387 388 389 390 391
        for type = `(signed-byte ,i)
        for bound = (ash 1 i)
        for len = 10
        for vals = (loop repeat len collect (- (random i) (/ bound 2)))
        for result = (map `(vector ,type) #'identity vals)
        unless (and (= (length result) len)
                    (every #'eql vals result))
        collect (list i vals result))
392 393 394 395
  nil)

(deftest map.specialized-vector.6
  (loop for type in '(short-float single-float long-float double-float)
sds's avatar
sds committed
396 397 398 399 400 401
        for len = 10
        for vals = (loop for i from 1 to len collect (coerce i type))
        for result = (map `(vector ,type) #'identity vals)
        unless (and (= (length result) len)
                    (every #'eql vals result))
        collect (list type vals result))
402 403 404 405
  nil)

(deftest map.specialized-vector.7
  (loop for etype in '(short-float single-float long-float double-float
sds's avatar
sds committed
406 407 408 409 410 411 412 413 414
                       integer rational)
        for type = `(complex ,etype)
        for len = 10
        for vals = (loop for i from 1 to len collect (complex (coerce i etype)
                                                              (coerce (- i) etype)))
        for result = (map `(vector ,type) #'identity vals)
        unless (and (= (length result) len)
                    (every #'eql vals result))
        collect (list type vals result))
415 416
  nil)

417 418 419 420 421 422
;;; Order of evaluation tests

(deftest map.order.1
  (let ((i 0) a b c d)
    (values
     (map (progn (setf a (incf i)) 'list)
sds's avatar
sds committed
423 424 425
          (progn (setf b (incf i)) #'list)
          (progn (setf c (incf i)) '(a b c))
          (progn (setf d (incf i)) '(b c d)))
426 427
     i a b c d))
  ((a b)(b c)(c d)) 4 1 2 3 4)
428 429 430

;;; Constant folding test

431
(def-fold-test map.fold.1 (map 'vector #'identity '(a b c)))