subst-if.lsp 2.86 KB
Newer Older
1 2 3 4 5 6 7
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sat Apr 19 21:39:42 2003
;;;; Contains: Tests of SUBST-IF

(in-package :cl-test)

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

10 11 12 13 14 15 16 17 18 19
(deftest subst-if.1
  (check-subst-if 'a #'consp '((100 1) (2 3) (4 3 2 1) (a b c)))
  a)

(deftest subst-if.2
  (check-subst-if 17 (complement #'listp) '(a (a b) (a c d) (a nil e f g)))
  (17 (17 17) (17 17 17) (17 nil 17 17 17)))

(deftest subst-if.3
  (check-subst-if '(z)
sds's avatar
sds committed
20 21
                  (complement #'consp)
                  '(a (a b) (c d e) (f g h i)))
22 23 24 25 26 27 28 29
  ((z)
   ((z) (z) z)
   ((z) (z) (z) z)
   ((z) (z) (z) (z) z)
   z))

(deftest subst-if.4
  (check-subst-if 'b #'identity '((100 1) (2 3) (4 3 2 1) (a b c))
sds's avatar
sds committed
30
                  :key #'listp)
31 32 33 34
  b)

(deftest subst-if.5
  (check-subst-if 4 #'(lambda (x) (eql x 1))
sds's avatar
sds committed
35 36 37 38
                  '((1 3) (1) (1 10 20 30) (1 3 x y))
                  :key #'(lambda (x)
                           (and (consp x)
                                (car x))))
39 40 41 42
  (4 4 4 4))

(deftest subst-if.6
  (check-subst-if 'a  #'(lambda (x) (eql x 'b))
sds's avatar
sds committed
43 44
                  '((a) (b) (c) (d))
                  :key nil)
45 46 47 48 49 50 51 52 53 54 55 56 57 58
  ((a) (a) (c) (d)))

(deftest subst-if.7
  (let ((i 0) w x y z)
    (values
     (subst-if
      (progn (setf w (incf i)) 'a)
      (progn (setf x (incf i)) #'(lambda (x) (eql x 'b)))
      (progn (setf y (incf i)) (copy-list '(1 2 a b c)))
      :key (progn (setf z (incf i)) #'identity))
     i w x y z))
  (1 2 a a c)
  4 1 2 3 4)

pfdietz's avatar
pfdietz committed
59 60
(def-fold-test subst-if.fold.1 (subst-if 'x 'numberp '(a b 3 (4) c d . 12)))

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
;;; Keyword tests for subst-if

(deftest subst-if.allow-other-keys.1
  (subst-if 'a #'null nil :bad t :allow-other-keys t)
  a)

(deftest subst-if.allow-other-keys.2
  (subst-if 'a #'null nil :allow-other-keys t)
  a)

(deftest subst-if.allow-other-keys.3
  (subst-if 'a #'null nil :allow-other-keys nil)
  a)

(deftest subst-if.allow-other-keys.4
  (subst-if 'a #'null nil :allow-other-keys t :bad t)
  a)

(deftest subst-if.allow-other-keys.5
  (subst-if 'a #'null nil :allow-other-keys t :allow-other-keys nil :bad t)
  a)

(deftest subst-if.keywords.6
  (subst-if 'a #'null nil :key nil :key (constantly 'b))
  a)

;;; Error tests

(deftest subst-if.error.1
90 91
  (signals-error (subst-if) program-error)
  t)
92 93

(deftest subst-if.error.2
94 95
  (signals-error (subst-if 'a) program-error)
  t)
96 97

(deftest subst-if.error.3
98 99
  (signals-error (subst-if 'a #'null) program-error)
  t)
100 101

(deftest subst-if.error.4
102 103
  (signals-error (subst-if 'a #'null nil :foo nil) program-error)
  t)
104 105

(deftest subst-if.error.5
106 107
  (signals-error (subst-if 'a #'null nil :test) program-error)
  t)
108 109

(deftest subst-if.error.6
110 111
  (signals-error (subst-if 'a #'null nil 1) program-error)
  t)
112 113

(deftest subst-if.error.7
114 115
  (signals-error (subst-if 'a #'null nil :bad t :allow-other-keys nil) program-error)
  t)
116 117

(deftest subst-if.error.8
118 119
  (signals-error (subst-if 'a #'null (list 'a nil 'c) :key #'cons) program-error)
  t)