source: trunk/source/tests/ansi-tests/defgeneric-method-combination-plus.lsp @ 8991

Last change on this file since 8991 was 8991, checked in by gz, 11 years ago

Check in the gcl ansi test suite (original, in preparation for making local changes)

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