defgeneric-method-combination-and.lsp 6.67 KB
Newer Older
1 2 3 4 5 6 7 8 9
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sat May 24 21:31:55 2003
;;;; Contains: Tests of DEFGENERIC with :method-combination AND

(in-package :cl-test)

(declaim (special *x*))

10 11
(compile-and-load "defgeneric-method-combination-aux.lsp")

12 13
(deftest defgeneric-method-combination.and.1
  (let ((*x* nil)
sds's avatar
sds committed
14 15 16 17 18 19 20
        (fn
         (eval '(defgeneric dg-mc.fun.and.1 (x)
                  (:method-combination and)
                  (:method and ((x integer)) (push 4 *x*) t)
                  (:method and ((x rational)) (push 3 *x*) nil)
                  (:method and ((x number)) (push 2 *x*) t)
                  (:method and ((x t)) (push 1 *x*) 'a)))))
21
    (declare (type generic-function fn))
22
    (flet ((%f (y)
sds's avatar
sds committed
23 24
               (let ((*x* nil))
                 (list (funcall fn y) *x*))))
25 26 27 28 29 30 31 32
    (values (%f 1) (%f 2/3) (%f 1.54) (%f 'a))))
  (nil (3 4))
  (nil (3))
  (a (1 2))
  (a (1)))

(deftest defgeneric-method-combination.and.2
  (let ((*x* nil)
sds's avatar
sds committed
33 34 35 36 37 38 39
        (fn
         (eval '(defgeneric dg-mc.fun.and.2 (x)
                  (:method-combination and :most-specific-first)
                  (:method and ((x integer)) (push 4 *x*) t)
                  (:method and ((x rational)) (push 3 *x*) nil)
                  (:method and ((x number)) (push 2 *x*) t)
                  (:method and ((x t)) (push 1 *x*) 'a)))))
40
    (declare (type generic-function fn))
41
    (flet ((%f (y)
sds's avatar
sds committed
42 43
               (let ((*x* nil))
                 (list (funcall fn y) *x*))))
44 45 46 47 48 49 50 51
    (values (%f 1) (%f 2/3) (%f 1.54) (%f 'a))))
  (nil (3 4))
  (nil (3))
  (a (1 2))
  (a (1)))

(deftest defgeneric-method-combination.and.3
  (let ((*x* nil)
sds's avatar
sds committed
52 53 54 55 56 57 58
        (fn
         (eval '(defgeneric dg-mc.fun.and.3 (x)
                  (:method-combination and :most-specific-last)
                  (:method and ((x integer)) (push 4 *x*) t)
                  (:method and ((x rational)) (push 3 *x*) nil)
                  (:method and ((x number)) (push 2 *x*) 'a)
                  (:method and ((x t)) (push 1 *x*) t)))))
59
    (declare (type generic-function fn))
60
    (flet ((%f (y)
sds's avatar
sds committed
61 62
               (let ((*x* nil))
                 (list (funcall fn y) *x*))))
63 64 65 66 67 68 69 70
      (values (%f 1) (%f 2/3) (%f 1.54) (%f 'a))))
  (nil (3 2 1))
  (nil (3 2 1))
  (a (2 1))
  (t (1)))

(deftest defgeneric-method-combination.and.4
  (let ((fn
sds's avatar
sds committed
71 72 73 74 75 76 77
         (eval '(defgeneric dg-mc.and.4 (x)
                  (:method-combination and)
                  (:method and ((x integer)) t)
                  (:method :around ((x rational)) 'foo)
                  (:method and ((x number)) nil)
                  (:method and ((x symbol)) t)
                  (:method and ((x t)) 'a)))))
78
    (declare (type generic-function fn))
79 80 81 82 83 84 85 86 87 88
    (values
     (funcall fn 0)
     (funcall fn 4/3)
     (funcall fn 1.54)
     (funcall fn 'x)
     (funcall fn '(a b c))))
  foo foo nil a a)

(deftest defgeneric-method-combination.and.5
  (let ((fn
sds's avatar
sds committed
89 90 91 92 93 94 95 96
         (eval '(defgeneric dg-mc.and.5 (x)
                  (:method-combination and)
                  (:method and ((x integer)) nil)
                  (:method :around ((x rational))
                           (list 'foo (call-next-method)))
                  (:method and ((x number)) 'a)
                  (:method and ((x symbol)) 'b)
                  (:method and ((x t)) 'c)))))
97
    (declare (type generic-function fn))
98 99 100 101 102 103 104 105 106 107
    (values
     (funcall fn 0)
     (funcall fn 4/3)
     (funcall fn 1.54)
     (funcall fn 'x)
     (funcall fn '(a b c))))
  (foo nil) (foo c) c c c)

(deftest defgeneric-method-combination.and.6
  (let ((fn
sds's avatar
sds committed
108 109 110 111 112 113 114 115 116 117
         (eval '(defgeneric dg-mc.and.6 (x)
                  (:method-combination and)
                  (:method and ((x integer)) 'a)
                  (:method :around ((x rational))
                           (list 'foo (call-next-method)))
                  (:method :around ((x real))
                           (list 'bar (call-next-method)))
                  (:method and ((x number)) nil)
                  (:method and ((x symbol)) 'c)
                  (:method and ((x t)) 'd)))))
118
    (declare (type generic-function fn))
119 120 121 122 123 124 125 126 127 128 129
    (values
     (funcall fn 0)
     (funcall fn 4/3)
     (funcall fn 1.54)
     (funcall fn #c(1.0 2.0))
     (funcall fn 'x)
     (funcall fn '(a b c))))
  (foo (bar nil)) (foo (bar nil)) (bar nil) nil d d)

(deftest defgeneric-method-combination.and.7
  (let ((fn
sds's avatar
sds committed
130 131 132 133 134 135
         (eval '(defgeneric dg-mc.and.7 (x)
                  (:method-combination and)
                  (:method and ((x dgmc-class-04)) 'c)
                  (:method and ((x dgmc-class-03)) 'b)
                  (:method and ((x dgmc-class-02)) nil)
                  (:method and ((x dgmc-class-01)) 'a)))))
136
    (declare (type generic-function fn))
137 138 139 140 141 142
    (values
     (funcall fn (make-instance 'dgmc-class-01))
     (funcall fn (make-instance 'dgmc-class-02))
     (funcall fn (make-instance 'dgmc-class-03))
     (funcall fn (make-instance 'dgmc-class-04))))
  a nil a nil)
sds's avatar
sds committed
143

144 145
(deftest defgeneric-method-combination.and.8
  (let ((fn
sds's avatar
sds committed
146 147 148 149 150 151 152 153
         (eval '(defgeneric dg-mc.and.8 (x)
                  (:method-combination and)
                  (:method and ((x (eql 1000))) 'a)
                  (:method :around ((x symbol)) (values))
                  (:method :around ((x integer)) (values 'a 'b 'c))
                  (:method :around ((x complex)) (call-next-method))
                  (:method :around ((x number)) (values 1 2 3 4 5 6))
                  (:method and ((x t)) 'b)))))
154
    (declare (type generic-function fn))
155 156 157 158 159 160
    (values
     (multiple-value-list (funcall fn 'a))
     (multiple-value-list (funcall fn 10))
     (multiple-value-list (funcall fn #c(9 8)))
     (multiple-value-list (funcall fn '(a b c)))))
  () (a b c) (1 2 3 4 5 6) (b))
161 162 163 164

(deftest defgeneric-method-combination.and.9
  (handler-case
   (let ((fn (eval '(defgeneric dg-mc.and.9 (x)
sds's avatar
sds committed
165
                      (:method-combination and)))))
166
     (declare (type generic-function fn))
167 168 169 170 171
     (funcall fn 'x))
   (error () :error))
  :error)

(deftest defgeneric-method-combination.and.10
172 173
  (progn
    (eval '(defgeneric dg-mc.and.10 (x)
sds's avatar
sds committed
174 175
             (:method-combination and)
             (:method ((x t)) t)))
176 177 178
    (handler-case
     (dg-mc.and.10 'a)
     (error () :error)))
179 180 181
  :error)

(deftest defgeneric-method-combination.and.11
182 183
  (progn
    (eval '(defgeneric dg-mc.and.11 (x)
sds's avatar
sds committed
184 185
             (:method-combination and)
             (:method nonsense ((x t)) t)))
186 187 188
    (handler-case
     (dg-mc.and.11 0)
     (error () :error)))
189 190 191 192
  :error)

(deftest defgeneric-method-combination.and.12
  (let ((fn (eval '(defgeneric dg-mc.and.12 (x)
sds's avatar
sds committed
193 194 195
                     (:method-combination and)
                     (:method :around ((x t)) t)
                     (:method and ((x integer)) x)))))
196
    (declare (type generic-function fn))
197
    (handler-case (funcall fn 'x)
sds's avatar
sds committed
198
                  (error () :error)))
199
  :error)