1 | ;-*- Mode: Lisp -*- |
---|
2 | ;;;; Author: Paul Dietz |
---|
3 | ;;;; Created: Sat May 24 21:31:55 2003 |
---|
4 | ;;;; Contains: Tests of DEFGENERIC with :method-combination LIST |
---|
5 | |
---|
6 | (in-package :cl-test) |
---|
7 | |
---|
8 | (declaim (special *x*)) |
---|
9 | |
---|
10 | (compile-and-load "defgeneric-method-combination-aux.lsp") |
---|
11 | |
---|
12 | (deftest defgeneric-method-combination.list.1 |
---|
13 | (let ((*x* nil) |
---|
14 | (fn |
---|
15 | (eval '(defgeneric dg-mc.fun.list.1 (x) |
---|
16 | (:method-combination list) |
---|
17 | (:method list ((x integer)) (car (push 'd *x*))) |
---|
18 | (:method list ((x rational)) (car (push 'c *x*))) |
---|
19 | (:method list ((x number)) (car (push 'b *x*))) |
---|
20 | (:method list ((x t)) (car (push 'a *x*))))))) |
---|
21 | (declare (type generic-function fn)) |
---|
22 | (flet ((%f (y) |
---|
23 | (let ((*x* nil)) |
---|
24 | (list (funcall fn y) *x*)))) |
---|
25 | (values (%f 1) (%f 2/3) (%f 1.54) (%f 'a)))) |
---|
26 | ((d c b a) (a b c d)) |
---|
27 | ((c b a) (a b c)) |
---|
28 | ((b a) (a b)) |
---|
29 | ((a) (a))) |
---|
30 | |
---|
31 | (deftest defgeneric-method-combination.list.2 |
---|
32 | (let ((*x* nil) |
---|
33 | (fn |
---|
34 | (eval '(defgeneric dg-mc.fun.list.2 (x) |
---|
35 | (:method-combination list :most-specific-first) |
---|
36 | (:method list ((x integer)) (car (push 'd *x*))) |
---|
37 | (:method list ((x rational)) (car (push 'c *x*))) |
---|
38 | (:method list ((x number)) (car (push 'b *x*))) |
---|
39 | (:method list ((x t)) (car (push 'a *x*))))))) |
---|
40 | (declare (type generic-function fn)) |
---|
41 | (flet ((%f (y) |
---|
42 | (let ((*x* nil)) |
---|
43 | (list (funcall fn y) *x*)))) |
---|
44 | (values (%f 1) (%f 2/3) (%f 1.54) (%f 'a)))) |
---|
45 | ((d c b a) (a b c d)) |
---|
46 | ((c b a) (a b c)) |
---|
47 | ((b a) (a b)) |
---|
48 | ((a) (a))) |
---|
49 | |
---|
50 | (deftest defgeneric-method-combination.list.3 |
---|
51 | (let ((*x* nil) |
---|
52 | (fn |
---|
53 | (eval '(defgeneric dg-mc.fun.list.3 (x) |
---|
54 | (:method-combination list :most-specific-last) |
---|
55 | (:method list ((x integer)) (car (push 'd *x*))) |
---|
56 | (:method list ((x rational)) (car (push 'c *x*))) |
---|
57 | (:method list ((x number)) (car (push 'b *x*))) |
---|
58 | (:method list ((x t)) (car (push 'a *x*))))))) |
---|
59 | (declare (type generic-function fn)) |
---|
60 | (flet ((%f (y) |
---|
61 | (let ((*x* nil)) |
---|
62 | (list (funcall fn y) *x*)))) |
---|
63 | (values (%f 1) (%f 2/3) (%f 1.54) (%f 'a)))) |
---|
64 | ((a b c d) (d c b a)) |
---|
65 | ((a b c) (c b a)) |
---|
66 | ((a b) (b a)) |
---|
67 | ((a) (a))) |
---|
68 | |
---|
69 | (deftest defgeneric-method-combination.list.4 |
---|
70 | (let ((fn |
---|
71 | (eval '(defgeneric dg-mc.fun.list.4 (x) |
---|
72 | (:method-combination list) |
---|
73 | (:method list ((x integer)) '(a b)) |
---|
74 | (:method :around ((x rational)) 'foo) |
---|
75 | (:method list ((x number)) '(c d)) |
---|
76 | (:method list ((x symbol)) '(e f)) |
---|
77 | (:method list ((x t)) '(g h)))))) |
---|
78 | (declare (type generic-function fn)) |
---|
79 | (values |
---|
80 | (funcall fn 0) |
---|
81 | (funcall fn 4/3) |
---|
82 | (funcall fn 1.54) |
---|
83 | (funcall fn 'x) |
---|
84 | (funcall fn '(a b c)))) |
---|
85 | foo foo ((c d) (g h)) ((e f) (g h)) ((g h))) |
---|
86 | |
---|
87 | (deftest defgeneric-method-combination.list.5 |
---|
88 | (let ((fn |
---|
89 | (eval '(defgeneric dg-mc.fun.list.5 (x) |
---|
90 | (:method-combination list) |
---|
91 | (:method list ((x integer)) 'a) |
---|
92 | (:method :around ((x rational)) |
---|
93 | (list 'foo (call-next-method))) |
---|
94 | (:method list ((x number)) 'b) |
---|
95 | (:method list ((x symbol)) 'c) |
---|
96 | (:method list ((x t)) 'd))))) |
---|
97 | (declare (type generic-function fn)) |
---|
98 | (values |
---|
99 | (funcall fn 0) |
---|
100 | (funcall fn 4/3) |
---|
101 | (funcall fn 1.54) |
---|
102 | (funcall fn 'x) |
---|
103 | (funcall fn '(a b c)))) |
---|
104 | (foo (a b d)) (foo (b d)) (b d) (c d) (d)) |
---|
105 | |
---|
106 | (deftest defgeneric-method-combination.list.6 |
---|
107 | (let ((fn |
---|
108 | (eval '(defgeneric dg-mc.fun.list.6 (x) |
---|
109 | (:method-combination list) |
---|
110 | (:method list ((x integer)) 'a) |
---|
111 | (:method :around ((x rational)) |
---|
112 | (list 'foo (call-next-method))) |
---|
113 | (:method :around ((x real)) |
---|
114 | (list 'bar (call-next-method))) |
---|
115 | (:method list ((x number)) 'b) |
---|
116 | (:method list ((x symbol)) 'c) |
---|
117 | (:method list ((x t)) 'd))))) |
---|
118 | (declare (type generic-function fn)) |
---|
119 | (values |
---|
120 | (funcall fn 0) |
---|
121 | (funcall fn 4/3) |
---|
122 | (funcall fn 1.54) |
---|
123 | (funcall fn #c(1.0 2.0)) |
---|
124 | (funcall fn 'x) |
---|
125 | (funcall fn '(a b c)))) |
---|
126 | (foo (bar (a b d))) (foo (bar (b d))) (bar (b d)) (b d) (c d) (d)) |
---|
127 | |
---|
128 | (deftest defgeneric-method-combination.list.7 |
---|
129 | (let ((fn |
---|
130 | (eval '(defgeneric dg-mc.fun.list.7 (x) |
---|
131 | (:method-combination list) |
---|
132 | (:method list ((x dgmc-class-04)) 'a) |
---|
133 | (:method list ((x dgmc-class-03)) 'b) |
---|
134 | (:method list ((x dgmc-class-02)) 'c) |
---|
135 | (:method list ((x dgmc-class-01)) 'd))))) |
---|
136 | (declare (type generic-function fn)) |
---|
137 | (values |
---|
138 | (funcall fn (make-instance 'dgmc-class-01)) |
---|
139 | (funcall fn (make-instance 'dgmc-class-02)) |
---|
140 | (funcall fn (make-instance 'dgmc-class-03)) |
---|
141 | (funcall fn (make-instance 'dgmc-class-04)))) |
---|
142 | (d) |
---|
143 | (c d) |
---|
144 | (b d) |
---|
145 | (a c b d)) |
---|
146 | |
---|
147 | (deftest defgeneric-method-combination.list.8 |
---|
148 | (let ((fn |
---|
149 | (eval '(defgeneric dg-mc.list.8 (x) |
---|
150 | (:method-combination list) |
---|
151 | (:method list ((x (eql 1000))) 'a) |
---|
152 | (:method :around ((x symbol)) (values)) |
---|
153 | (:method :around ((x integer)) (values 'a 'b 'c)) |
---|
154 | (:method :around ((x complex)) (call-next-method)) |
---|
155 | (:method :around ((x number)) (values 1 2 3 4 5 6)) |
---|
156 | (:method list ((x t)) 'b))))) |
---|
157 | (declare (type generic-function fn)) |
---|
158 | (values |
---|
159 | (multiple-value-list (funcall fn 'a)) |
---|
160 | (multiple-value-list (funcall fn 10)) |
---|
161 | (multiple-value-list (funcall fn #c(9 8))) |
---|
162 | (multiple-value-list (funcall fn '(a b c))))) |
---|
163 | () (a b c) (1 2 3 4 5 6) ((b))) |
---|
164 | |
---|
165 | (deftest defgeneric-method-combination.list.9 |
---|
166 | (handler-case |
---|
167 | (let ((fn (eval '(defgeneric dg-mc.list.9 (x) |
---|
168 | (:method-combination list))))) |
---|
169 | (declare (type generic-function fn)) |
---|
170 | (funcall fn (list 'a))) |
---|
171 | (error () :error)) |
---|
172 | :error) |
---|
173 | |
---|
174 | (deftest defgeneric-method-combination.list.10 |
---|
175 | (progn |
---|
176 | (eval '(defgeneric dg-mc.list.10 (x) |
---|
177 | (:method-combination list) |
---|
178 | (:method ((x t)) (list 'a)))) |
---|
179 | (handler-case |
---|
180 | (dg-mc.list.10 'a) |
---|
181 | (error () :error))) |
---|
182 | :error) |
---|
183 | |
---|
184 | (deftest defgeneric-method-combination.list.11 |
---|
185 | (progn |
---|
186 | (eval '(defgeneric dg-mc.list.11 (x) |
---|
187 | (:method-combination list) |
---|
188 | (:method nonsense ((x t)) (list 'a)))) |
---|
189 | (handler-case |
---|
190 | (dg-mc.list.11 0) |
---|
191 | (error () :error))) |
---|
192 | :error) |
---|
193 | |
---|
194 | (deftest defgeneric-method-combination.list.12 |
---|
195 | (let ((fn (eval '(defgeneric dg-mc.list.12 (x) |
---|
196 | (:method-combination list) |
---|
197 | (:method :around ((x t)) (list 'a)) |
---|
198 | (:method list ((x integer)) x))))) |
---|
199 | (declare (type generic-function fn)) |
---|
200 | (handler-case (funcall fn (list 'b)) |
---|
201 | (error () :error))) |
---|
202 | :error) |
---|