mapcan.lsp 2.71 KB
Newer Older
1 2 3 4 5 6 7
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sun Apr 20 07:22:46 2003
;;;; Contains: Tests of MAPCAN

(in-package :cl-test)

8 9
(compile-and-load "cons-aux.lsp")

10 11 12 13 14 15 16 17 18 19
(deftest mapcan.1
  (mapcan #'list nil)
  nil)

(deftest mapcan.2
  (mapcan #'list (copy-list '(a b c d e f)))
  (a b c d e f))

(deftest mapcan.3
  (let* ((x (list 'a 'b 'c 'd))
sds's avatar
sds committed
20 21
         (xcopy (make-scaffold-copy x))
         (result (mapcan #'list x)))
22 23 24 25 26 27 28 29 30 31 32
    (and
     (= (length x) (length result))
     (check-scaffold-copy x xcopy)
     (loop
      for e1 on x
      and e2 on result
      count (or (eqt e1 e2) (not (eql (car e1) (car e2)))))))
  0)

(deftest mapcan.4
  (mapcan #'list
sds's avatar
sds committed
33 34
          (copy-list '(1 2 3 4))
          (copy-list '(a b c d)))
35 36 37 38
  (1 a 2 b 3 c 4 d))

(deftest mapcan.5
  (mapcan #'(lambda (x y) (make-list y :initial-element x))
sds's avatar
sds committed
39 40
          (copy-list '(a b c d))
          (copy-list '(1 2 3 4)))
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
  (a b b c c c d d d d))

(defvar *mapcan.6-var* nil)
(defun mapcan.6-fun (x)
  (push x *mapcan.6-var*)
  (copy-list *mapcan.6-var*))

(deftest mapcan.6
  (progn
    (setf *mapcan.6-var* nil)
    (mapcan 'mapcan.6-fun (copy-list '(a b c d))))
  (a b a c b a d c b a))

(deftest mapcan.order.1
  (let ((i 0) x y z)
    (values
     (mapcan (progn (setf x (incf i))
sds's avatar
sds committed
58 59 60 61 62
                    #'list)
             (progn (setf y (incf i))
                    '(a b c))
             (progn (setf z (incf i))
                    '(1 2 3)))
63 64 65 66 67 68
     i x y z))
  (a 1 b 2 c 3)
  3 1 2 3)

(deftest mapcan.8
  (mapcan #'(lambda (x y) (make-list y :initial-element x))
sds's avatar
sds committed
69 70
          (copy-list '(a b c d))
          (copy-list '(1 2 3 4 5 6)))
71 72 73 74
  (a b b c c c d d d d))

(deftest mapcan.9
  (mapcan #'(lambda (x y) (make-list y :initial-element x))
sds's avatar
sds committed
75 76
          (copy-list '(a b c d e f))
          (copy-list '(1 2 3 4)))
77 78 79 80
  (a b b c c c d d d d))

(deftest mapcan.10
  (mapcan #'list
sds's avatar
sds committed
81 82 83
          (copy-list '(a b c d))
          (copy-list '(1 2 3 4))
          nil)
84 85 86 87 88 89 90
  nil)

(deftest mapcan.11
  (mapcan (constantly 1) (list 'a))
  1)

(deftest mapcan.error.1
pfdietz's avatar
pfdietz committed
91 92
  (check-type-error #'(lambda (x) (mapcan #'identity x)) #'listp)
  nil)
93 94

(deftest mapcan.error.2
95 96
  (signals-error (mapcan) program-error)
  t)
97 98

(deftest mapcan.error.3
99 100
  (signals-error (mapcan #'append) program-error)
  t)
101 102

(deftest mapcan.error.4
103 104
  (signals-error (locally (mapcan #'identity 1) t) type-error)
  t)
105 106

(deftest mapcan.error.5
107 108
  (signals-error (mapcan #'car '(a b c)) type-error)
  t)
109 110

(deftest mapcan.error.6
111 112
  (signals-error (mapcan #'cons '(a b c)) program-error)
  t)
113 114

(deftest mapcan.error.7
115
  (signals-error (mapcan #'cons '(a b c) '(1 2 3) '(4 5 6))
sds's avatar
sds committed
116
                 program-error)
117
  t)
118 119

(deftest mapcan.error.8
120
  (signals-error (mapcan #'identity (list* (list 1) (list 2) 3))
sds's avatar
sds committed
121
                 type-error)
122
  t)