18df67c075cad5ae522729636f304fc677459b87
[sbcl.git] / tests / compiler-1.impure-cload.lisp
1 ;;;; miscellaneous compiler tests with side-effects (e.g. DEFUN
2 ;;;; changing FDEFINITIONs and globaldb stuff)
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 (cl:in-package :cl-user)
16
17 (declaim (optimize (debug 3) (speed 2) (space 1)))
18
19 ;;; Until version 0.6.9 or so, SBCL's version of Python couldn't do
20 ;;; this correctly, due to the bug patched by Rob MacLachlan on the
21 ;;; cmucl-imp list 2000-06-21, and applied to SBCL by Martin Atzmueller.
22 ;;; (The effectiveness of the test also depends on the implicit
23 ;;; function typing of Python (where DEFUN is like DECLAIM FTYPE),
24 ;;; which violates the ANSI spec, and should be fixed. Once that
25 ;;; unrelated bug is fixed, this code will no longer test the type
26 ;;; inference behavior it's intended to test.)
27 (defun emptyvalues (&rest rest) (declare (ignore rest)) (values))
28 (defstruct foo x y)
29 (defun bar ()
30   (let ((res (emptyvalues)))
31     (unless (typep res 'foo)
32       'expected-value)))
33 (assert (eq (bar) 'expected-value))
34
35 (declaim (ftype (function (real) (values integer single-float)) valuesify))
36 (defun valuesify (x)
37   (values (round x)
38           (coerce x 'single-float)))
39 (defun exercise-valuesify (x)
40   (multiple-value-bind (i f) (valuesify x)
41     (declare (type integer i))
42     (declare (type single-float f))
43     (+ i f)))
44 (assert (= (exercise-valuesify 1.25) 2.25))
45
46 ;;; Don Geddis reported this test case 25 December 1999 on a CMU CL
47 ;;; mailing list: dumping circular lists caused the compiler to enter
48 ;;; an infinite loop. Douglas Crosher reported a patch 27 Dec 1999.
49 ;;; The patch was tested on SBCL by Martin Atzmueller 2 Nov 2000, and
50 ;;; merged in sbcl-0.6.8.11.
51 (defun q-dg1999-1 () (dolist (x '#1=("A" "B" . #1#)) x))
52 (defun q-dg1999-2 () (dolist (x '#1=("C" "D" . #1#)) x))
53 (defun q-dg1999-3 () (dolist (x '#1=("E" "F" . #1#)) x))
54 (defun q-dg1999-4 () (dolist (x '#1=("C" "D" . #1#)) x))
55 (defun useful-dg1999 (keys)
56   (declare (type list keys))
57   (loop
58       for c in '#1=("Red" "Blue" . #1#)
59       for key in keys ))
60
61 ;;; An early version (sbcl-0.6.11.33) of code to check FTYPEs from DEFUN
62 ;;; against DECLAIMed FTYPEs blew up when an FTYPE was DECLAIMed
63 ;;; to be pure FUNCTION, because the internal representation of
64 ;;; FUNCTION itself (as opposed to subtypes of FUNCTION, such as
65 ;;; (FUNCTION () T)) is a BUILT-IN-CLASS object, not a FUN-TYPE
66 ;;; object.
67 (declaim (ftype function i-am-just-a-function))
68 (defun i-am-just-a-function (x y) (+ x y 1))
69
70 ;;; Stig E SandPHI (where PHI is some phi-like character not
71 ;;; representable in ASCII) reported in cclan-Bugs-431263 that SBCL
72 ;;; couldn't compile this. sbcl-0.6.12.26 died in CIRCULAR-LIST-P with
73 ;;; "The value \"EST\" is not of type LIST." Dan Barlow fixed it.
74 (defvar +time-zones+
75   '((5 "EDT" . "EST") (6 "CDT" . "CST") (7 "MDT" .
76 "MST") (8 "PDT" . "PST")
77     (0 "GMT" . "GDT") (-2 "MET" . "MET DST"))
78   "*The string representations of the time zones.")
79
80 ;;; The old CMU CL Python compiler assumed that it was safe to infer
81 ;;; function types (including return types) from function definitions
82 ;;; and then use them to optimize code later. This is of course bad
83 ;;; when functions are redefined. The problem was fixed in
84 ;;; sbcl-0.6.12.57.
85 (defun foo (x)
86   (if (plusp x)
87       1.0
88       0))
89 (defun bar (x)
90   (typecase (foo x)
91     (fixnum :fixnum)
92     (real :real)
93     (string :string)
94     (t :t)))
95 (assert (eql (bar 11) :real))
96 (assert (eql (bar -11) :fixnum))
97 (setf (symbol-function 'foo) #'identity)
98 (assert (eql (bar 11) :fixnum))
99 (assert (eql (bar -11.0) :real))
100 (assert (eql (bar "this is a test") :string))
101 (assert (eql (bar (make-hash-table)) :t))
102
103 ;;; bug reported by Brian Spilsbury sbcl-devel 2001-09-30, fixed by
104 ;;; Alexey Dejneka patch sbcl-devel 2001-10-02
105 (defun pixarray-element-size (pixarray)
106   (let ((eltype (array-element-type pixarray)))
107     (cond ((eq eltype 'bit) 1)
108           ((and (listp eltype)
109                 (eq (first eltype) 'unsigned-byte))
110            (second eltype))
111           (t
112            (error "Invalid pixarray: ~S." pixarray)))))
113 (assert (eql 1 (pixarray-element-size #*110)))
114
115 (sb-ext:quit :unix-status 104) ; success