0.6.11.13:
[sbcl.git] / tests / type.before-xc.lisp
1 ;;;; tests of the type system, intended to be executed as soon as 
2 ;;;; the cross-compiler is built
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; While most of SBCL is derived from the CMU CL system, the test
8 ;;;; files (like this one) were written from scratch after the fork
9 ;;;; from CMU CL.
10 ;;;; 
11 ;;;; This software is in the public domain and is provided with
12 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
13 ;;;; more information.
14
15 (in-package "SB!KERNEL")
16
17 (/show "beginning tests/type.before-xc.lisp")
18
19 (assert (type= (specifier-type '(and fixnum (satisfies foo)))
20                (specifier-type '(and (satisfies foo) fixnum))))
21 (assert (type= (specifier-type '(member 1 2 3))
22                (specifier-type '(member 2 3 1))))
23 (assert (type= (specifier-type '(and (member 1.0 2 3) single-float))
24                (specifier-type '(member 1.0))))
25
26 (assert (sb-xc:typep #(1 2 3) 'simple-vector))
27 (assert (sb-xc:typep #(1 2 3) 'vector))
28 (assert (not (sb-xc:typep '(1 2 3) 'vector)))
29 (assert (not (sb-xc:typep 1 'vector)))
30
31 (assert (sb-xc:typep '(1 2 3) 'list))
32 (assert (sb-xc:typep '(1 2 3) 'cons))
33 (assert (not (sb-xc:typep '(1 2 3) 'null)))
34 (assert (not (sb-xc:typep "1 2 3" 'list)))
35 (assert (not (sb-xc:typep 1 'list)))
36
37 (assert (sb-xc:typep nil 'null))
38 (assert (sb-xc:typep nil '(member nil)))
39 (assert (sb-xc:typep nil '(member 1 2 nil 3)))
40 (assert (not (sb-xc:typep nil '(member 1 2 3))))
41
42 (assert (type= *empty-type*
43                (type-intersection (specifier-type 'list)
44                                   (specifier-type 'vector))))
45 (assert (eql *empty-type*
46              (type-intersection (specifier-type 'list)
47                                 (specifier-type 'vector))))
48 (assert (type= (specifier-type 'null)
49                (type-intersection (specifier-type 'list)
50                                   (specifier-type '(or vector null)))))
51 (assert (type= (specifier-type 'null)
52                (type-intersection (specifier-type 'sequence)
53                                   (specifier-type 'symbol))))
54 (assert (type= (specifier-type 'cons)
55                (type-intersection (specifier-type 'sequence)
56                                   (specifier-type '(or cons number)))))
57 (assert (eql *empty-type*
58              (type-intersection (specifier-type '(satisfies keywordp))
59                                 *empty-type*)))
60
61 ;;; Identities should be identities.
62 (dolist (type-specifier '(nil
63                           t
64                           null
65                           (satisfies keywordp) 
66                           (satisfies foo) 
67                           (not fixnum)
68                           (not null)
69                           (and symbol (satisfies foo))
70                           (and (satisfies foo) string)
71                           (or symbol sequence)
72                           (or single-float character)
73                           (or float (satisfies bar))
74                           integer (integer 0 1)
75                           character standard-char
76                           (member 1 2 3)))
77   (/show type-specifier)
78   (let ((ctype (specifier-type type-specifier)))
79
80     (assert (eql *empty-type* (type-intersection ctype *empty-type*)))
81     (assert (eql *empty-type* (type-intersection *empty-type* ctype)))
82     (assert (eql *empty-type* (type-intersection2 ctype *empty-type*)))
83     (assert (eql *empty-type* (type-intersection2 *empty-type* ctype)))
84
85     (assert (type= ctype (type-intersection ctype *universal-type*)))
86     (assert (type= ctype (type-intersection *universal-type* ctype)))
87     (assert (type= ctype (type-intersection2 ctype *universal-type*)))
88     (assert (type= ctype (type-intersection2 *universal-type* ctype)))
89       
90     ;; FIXME: TYPE-UNION still acts CMU-CL-ish as of 0.6.11.13, so
91     ;; e.g. (TYPE-UNION #<HAIRY-TYPE (SATISFIES KEYWORDP)> *EMPTY-TYPE*)
92     ;; returns a UNION-TYPE instead of the HAIRY-TYPE. When that's
93     ;; fixed, these tests should be enabled.
94     ;;(assert (eql ctype (type-union ctype *empty-type*)))
95     ;;(assert (eql ctype (type-union *empty-type* ctype)))
96
97     ;; FIXME: TYPE-UNION2 is not defined yet as of 0.6.11.13, and when
98     ;; it's defined, these tests should be enabled.
99     ;;(assert (eql *empty-type* (type-union2 ctype *empty-type*)))
100     ;;(assert (eql *empty-type* (type-union2 *empty-type* ctype)))
101
102     ;;(assert (eql *universal-type* (type-union ctype *universal-type*)))
103     ;;(assert (eql *universal-type* (type-union *universal-type* ctype)))
104     ;;(assert (eql ctype (type-union2 ctype *universal-type*)))
105     ;;(assert (eql ctype (type-union2 *universal-type* ctype)))
106
107     (assert (csubtypep *empty-type* ctype))
108     (assert (csubtypep ctype *universal-type*))))
109 (/show "done with identities-should-be-identities block")
110
111 (assert (sb-xc:subtypep 'simple-vector 'vector))
112 (assert (sb-xc:subtypep 'simple-vector 'simple-array))
113 (assert (sb-xc:subtypep 'vector 'array))
114 (assert (not (sb-xc:subtypep 'vector 'simple-vector)))
115 (assert (not (sb-xc:subtypep 'vector 'simple-array)))
116
117 (macrolet ((assert-secondnil (expr) `(assert (null (nth-value 1 ,expr)))))
118   (assert-secondnil (sb-xc:subtypep t '(satisfies foo)))
119   (assert-secondnil (sb-xc:subtypep t '(and (satisfies foo) (satisfies bar))))
120   (assert-secondnil (sb-xc:subtypep t '(or (satisfies foo) (satisfies bar))))
121   ;; FIXME: Enable these tests when bug 84 is fixed.
122   #|
123   (assert-secondnil (sb-xc:subtypep '(satisfies foo) nil))
124   (assert-secondnil (sb-xc:subtypep '(and (satisfies foo) (satisfies bar))
125                                     nil))
126   (assert-secondnil (sb-xc:subtypep '(or (satisfies foo) (satisfies bar))
127                                     nil))
128   |#)
129
130 ;;; various dead bugs
131 (assert (union-type-p (type-intersection (specifier-type 'list)
132                                          (specifier-type '(or list vector)))))
133 (assert (type= (type-intersection (specifier-type 'list)
134                                   (specifier-type '(or list vector)))
135                (specifier-type 'list)))
136 (assert (array-type-p (type-intersection (specifier-type 'vector)
137                                          (specifier-type '(or list vector)))))
138 (assert (type= (type-intersection (specifier-type 'vector)
139                                   (specifier-type '(or list vector)))
140                (specifier-type 'vector)))
141 (assert (type= (type-intersection (specifier-type 'number)
142                                   (specifier-type 'integer))
143                (specifier-type 'integer)))
144 (assert (null (type-intersection2 (specifier-type 'symbol)
145                                   (specifier-type '(satisfies foo)))))
146 (assert (intersection-type-p (specifier-type '(and symbol (satisfies foo)))))
147
148 (/show "done with tests/type.before-xc.lisp")